13a0d1905b1de67cff23ef79819e8c130b3acb10
[moodle.git] / mod / workshop / locallib.php
1 <?php  // $Id$
3 /// Library of extra functions and module workshop 
5 //////////////////////////////////////////////////////////////////////////////////////
7 /*** Functions for the workshop module ******
9 workshop_choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", 
10     $nothingvalue="0", $return=false) {
12 workshop_compare_assessments($workshop, $assessment1, $assessment2) { ---> in lib.php
13 workshop_count_all_submissions_for_assessment($workshop, $user) {
14 workshop_count_assessments($submission) { ---> in lib.php
15 workshop_count_comments($assessment) {
16 workshop_count_peer_assessments($workshop, $user) {
17 workshop_count_self_assessments($workshop, $user) {
18 workshop_count_student_submissions($workshop) {
19 workshop_count_student_submissions_for_assessment($workshop, $user) {
20 workshop_count_teacher_assessments($courseid, $submission) {
21 workshop_count_teacher_assessments_by_user($workshop, $user) {
22 workshop_count_teacher_submissions($workshop) {
23 workshop_count_teacher_submissions_for_assessment($workshop, $user) {
24 workshop_count_ungraded_assessments($workshop) { --->in lib.php
25 workshop_count_ungraded_assessments_student($workshop) {
26 workshop_count_ungraded_assessments_teacher($workshop) {
27 workshop_count_user_assessments($worshop, $user, $type = "all") { $type is all, student or teacher
28 workshop_count_user_submissions($workshop, $user) {
30 workshop_delete_submitted_files($workshop, $submission) {
31 workshop_delete_user_files($workshop, $user, $exception) {
33 workshop_file_area($workshop, $submission) { ---> in lib.php
34 workshop_file_area_name($workshop, $submission) { ---> in lib.php
36 workshop_get_all_teacher_assessments($workshop) {
37 workshop_get_assessments($submission, $all = '') { ---> in lib.php
38 workshop_get_comments($assessment) {
39 workshop_get_participants($workshopid) {
40 workshop_get_student_assessments($workshop, $user) {
41 workshop_get_student_submission($workshop, $user) { ---> in lib.php
42 workshop_get_student_submission_assessments($workshop) {
43 workshop_get_student_submissions($workshop) { ---> in lib.php
44 workshop_get_submission_assessment($submission, $user) {
45 workshop_get_teacher_assessments($courseid, $submission) {
46 workshop_get_teacher_submission_assessments($workshop) {
47 workshop_get_teacher_submissions($workshop) {
48 workshop_get_ungraded_assessments($workshop) {
49 workshop_get_unmailed_assessments($cutofftime) {
50 workshop_get_unmailed_marked_assessments($cutofftime) {
51 workshop_get_user_assessments($workshop, $user) { ---> in lib.php
52 workshop_get_user_submissions($workshop, $user) { ---> in lib.php
53 workshop_get_users_done($workshop) {
55 workshop_grade_assessments($workshop) { ---> in lib.php
57 workshop_list_all_submissions($workshop) {
58 workshop_list_all_ungraded_assessments($workshop) {
59 workshop_list_assessed_submissions($workshop, $user) {
60 workshop_list_peer_assessments($workshop, $user) {
61 workshop_list_student_submissions($workshop, $user) {
62 workshop_list_submissions_for_admin($workshop, $order) {
63 workshop_list_teacher_assessments_by_user($workshop, $user) {
64 workshop_list_teacher_submissions($workshop) {
65 workshop_list_unassessed_student_submissions($workshop, $user) {
66 workshop_list_unassessed_teacher_submissions($workshop, $user) {
67 workshop_list_ungraded_assessments($workshop, $stype) {
68 workshop_list_user_submissions($workshop, $user) {
70 workshop_calculate_phase($workshop, $style='') {
72 workshop_print_assessment($workshop, $assessment, $allowchanges, $showcommentlinks, $returnto)
73 workshop_print_assessments_by_user_for_admin($workshop, $user) {
74 workshop_print_assessments_for_admin($workshop, $submission) {
75 workshop_print_assignment_info($cm, $workshop) {
76 workshop_print_difference($time) {
77 workshop_print_feedback($course, $submission) {
78 workshop_print_league_table($workshop) {
79 workshop_print_submission_assessments($workshop, $submission, $type) {
80 workshop_print_submission_title($workshop, $user) {
81 workshop_print_time_to_deadline($time) {
82 workshop_print_upload_form($workshop) {
83 workshop_print_user_assessments($workshop, $user) {
85 workshop_submission_grade($submission) { ---> in lib.php
86     
87 workshop_test_user_assessments($workshop, $user) {
88 ***************************************/
91 ///////////////////////////////////////////////////////////////////////////////
92 function workshop_choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", 
93         $nothingvalue="0", $return=false) {
94 /// Given an array of value, creates a popup menu to be part of a form
95 /// $options["value"]["label"]
96     
97     if ($nothing == "choose") {
98         $nothing = get_string("choose")."...";
99     }
101     if ($script) {
102         $javascript = "onChange=\"$script\"";
103     } else {
104         $javascript = "";
105     }
107     $output = "<select name=\"$name\" $javascript>\n";
108     if ($nothing) {
109         $output .= "   <option value=\"$nothingvalue\"\n";
110         if ($nothingvalue == $selected) {
111             $output .= " selected=\"selected\"";
112         }
113         $output .= ">$nothing</option>\n";
114     }
115     if (!empty($options)) {
116         foreach ($options as $value => $label) {
117             $output .= "   <option value=\"$value\"";
118             if ($value == $selected) {
119                 $output .= " selected=\"selected\"";
120             }
121             // stop zero label being replaced by array index value
122             // if ($label) {
123             //    $output .= ">$label</option>\n";
124             // } else {
125             //     $output .= ">$value</option>\n";
126             //  }
127             $output .= ">$label</option>\n";
128             
129         }
130     }
131     $output .= "</select>\n";
133     if ($return) {
134         return $output;
135     } else {
136         echo $output;
137     }
138 }   
141 ///////////////////////////////////////////////////////////////////////////////////////////////
142 function workshop_copy_assessment($assessment, $submission, $withfeedback = false) {
143     // adds a copy of the given assessment for the submission specified to the workshop_assessments table. 
144     // The grades and optionally the comments are added to the workshop_grades table. Returns the new
145     // assessment object. The owner of the assessment is not changed.
146     
147     $yearfromnow = time() + 365 * 86400;
148     $newassessment->workshopid = $assessment->workshopid;
149     $newassessment->submissionid = $submission->id;
150     $newassessment->userid = $assessment->userid;
151     $newassessment->timecreated = $yearfromnow;
152     $newassessment->grade = $assessment->grade;
153     if ($withfeedback) {
154         $newassessment->generalcomment = addslashes($assessment->generalcomment);
155         $newassessment->teachercomment = addslashes($assessment->teachercomment);
156     }
157     if (!$newassessment->id = insert_record("workshop_assessments", $newassessment)) {
158         error("Copy Assessment: Could not insert workshop assessment!");
159     }
160     
161     if ($grades = get_records("workshop_grades", "assessmentid", $assessment->id)) {
162         foreach ($grades as $grade) {
163             unset($grade->id); // clear id, insert record now seems to believe it!
164             if (!$withfeedback) {
165                 $grade->feedback = '';
166             }
167             else {
168                 $grade->feedback = addslashes($grade->feedback);
169             }
170             $grade->assessmentid = $newassessment->id;
171             if (!$grade->id = insert_record("workshop_grades", $grade)) {
172                 error("Copy Assessment: Could not insert workshop grade!");
173             }
174         }
175     }
176     if ($withfeedback) {
177         // remove the slashes from comments as the new assessment record might be used, 
178         // currently this function is only called in upload which does not!
179         $newassessment->generalcomment = stripslashes($assessment->generalcomment);
180         $newassessment->teachercomment = stripslashes($assessment->teachercomment);
181     }
182     return $newassessment;
187 //////////////////////////////////////////////////////////////////////////////////////
188 function workshop_count_all_submissions_for_assessment($workshop, $user) {
189     // looks at all submissions and deducts the number which has been assessed by this user
190     $n = 0;
191     if ($submissions = get_records_select("workshop_submissions", "workshopid = $workshop->id AND 
192                 timecreated > 0")) {
193         $n =count($submissions);
194         foreach ($submissions as $submission) {
195             $n -= count_records("workshop_assessments", "submissionid", $submission->id, "userid", $user->id);
196             }
197         }
198     return $n;
199     }
202 //////////////////////////////////////////////////////////////////////////////////////
203 function workshop_count_comments($assessment) {
204     // Return the number of comments for this assessment provided they are newer than the assessment, 
205    return count_records_select("workshop_comments", "(assessmentid = $assessment->id) AND 
206         timecreated > $assessment->timecreated");
210 //////////////////////////////////////////////////////////////////////////////////////
211 function workshop_count_peer_assessments($workshop, $user) {
212     // returns the number of assessments made by students on user's submissions
213     
214     $n = 0;
215     if ($submissions = workshop_get_user_submissions($workshop, $user)) {
216         foreach ($submissions as $submission) {
217             if ($assessments = workshop_get_assessments($submission)) {
218                 foreach ($assessments as $assessment) {
219                     // ignore teacher assessments
220                     if (!isteacher($workshop->course, $assessment->userid)) {
221                         $n++;
222                         }
223                     }
224                 }
225             }
226         }
227     return $n;
228     }
231 //////////////////////////////////////////////////////////////////////////////////////
232 function workshop_count_self_assessments($workshop, $user) {
233     // returns the number of assessments made by user on their own submissions
234     
235     $n = 0;
236     if ($submissions = workshop_get_user_submissions($workshop, $user)) {
237         foreach ($submissions as $submission) {
238             if ($assessment = get_record_select("workshop_assessments", "userid = $user->id AND 
239                     submissionid = $submission->id")) {
240                 $n++;
241                 }
242             }
243         }
244     return $n;
245     }
248 //////////////////////////////////////////////////////////////////////////////////////
249 function workshop_count_student_submissions($workshop) {
250     global $CFG;
252     // make sure it works on the site course
253     $select = "s.course = '$workshop->course' AND";
254     if ($workshop->course == SITEID) {
255         $select = '';
256     }
258     return count_records_sql("SELECT count(*) FROM {$CFG->prefix}workshop_submissions s, 
259                             {$CFG->prefix}user_students u
260                             WHERE $select s.userid = u.userid
261                               AND s.workshopid = $workshop->id
262                               AND timecreated > 0");
263     }
266 //////////////////////////////////////////////////////////////////////////////////////
267 function workshop_count_student_submissions_for_assessment($workshop, $user) {
268     global $CFG;
270     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
271         error("Course Module ID was incorrect");
272     }
273     if (! $course = get_record("course", "id", $workshop->course)) {
274         error("Course is misconfigured");
275         }
276     
277     $timenow = time();
278     if (groupmode($course, $cm) == SEPARATEGROUPS) {
279         $groupid = get_current_group($course->id);
280     } else {
281         $groupid = 0;
282     }
283     
284     $n = 0;
285     if ($submissions = workshop_get_student_submissions($workshop)) {
286         foreach ($submissions as $submission) {
287             // check group membership, if necessary
288             if ($groupid) {
289                 // check user's group
290                 if (!ismember($groupid, $submission->userid)) {
291                     continue; // skip this user
292                 }
293             }
294             // check if submission is cold
295             if (($submission->timecreated + $CFG->maxeditingtime) > $timenow) {
296                 continue; // skip this submission
297             }
298             // has any teacher assessed this submission?
299             if (!workshop_count_teacher_assessments($course->id, $submission)) {
300                 $n++;
301             }
302         }
303     }
304     return $n;
308 //////////////////////////////////////////////////////////////////////////////////////
309 function workshop_count_teacher_assessments($courseid, $submission) {
310 // Return count of (cold) teacher assessments of a submission
311     global $CFG;
312     
313     $timenow = time();
314     return count_records_sql("SELECT count(*) FROM {$CFG->prefix}workshop_assessments a, 
315                             {$CFG->prefix}user_teachers u
316                             WHERE u.course = $courseid
317                               AND a.userid = u.userid
318                               AND a.submissionid = $submission->id
319                               AND $timenow > (a.timecreated + $CFG->maxeditingtime)");
323 //////////////////////////////////////////////////////////////////////////////////////
324 function workshop_count_teacher_assessments_by_user($workshop, $user) {
325     // returns the number of assessments made by teachers on user's submissions
326     
327     $n = 0;
328     if ($submissions = workshop_get_user_submissions($workshop, $user)) {
329         foreach ($submissions as $submission) {
330             if ($assessments = workshop_get_assessments($submission)) {
331                 foreach ($assessments as $assessment) {
332                     // count only teacher assessments
333                     if (isteacher($workshop->course, $assessment->userid)) {
334                         $n++;
335                         }
336                     }
337                 }
338             }
339         }
340     return $n;
341     }
344 //////////////////////////////////////////////////////////////////////////////////////
345 function workshop_count_teacher_submissions($workshop) {
346     global $CFG;
347     
348      return count_records_sql("SELECT count(*) FROM {$CFG->prefix}workshop_submissions s, 
349                      {$CFG->prefix}user_teachers u
350                             WHERE u.course = $workshop->course
351                               AND s.userid = u.userid
352                               AND s.workshopid = $workshop->id");
353     }
356 //////////////////////////////////////////////////////////////////////////////////////
357 function workshop_count_teacher_submissions_for_assessment($workshop, $user) {
359     $n = 0;
360     if ($submissions = workshop_get_teacher_submissions($workshop)) {
361         $n =count($submissions);
362         foreach ($submissions as $submission) {
363             $n -= count_records("workshop_assessments", "submissionid", $submission->id, "userid", $user->id);
364             }
365         }
366     return $n;
367     }
370 //////////////////////////////////////////////////////////////////////////////////////
371 function workshop_count_ungraded_assessments_student($workshop) {
372     // function returns the number of ungraded assessments by students of STUDENT submissions
373     $n = 0;
374     if ($submissions = workshop_get_student_submissions($workshop)) {
375         foreach ($submissions as $submission) {
376             if ($assessments = workshop_get_assessments($submission)) {
377                 foreach ($assessments as $assessment) {
378                     if ($assessment->timegraded == 0) {
379                         // ignore teacher assessments
380                         if (!isteacher($workshop->course, $assessment->userid)) {
381                             $n++;
382                             }
383                         }
384                     }
385                 }
386             }
387         }
388     return $n;
389     }
392 //////////////////////////////////////////////////////////////////////////////////////
393 function workshop_count_ungraded_assessments_teacher($workshop) {
394     // function returns the number of ungraded assessments by students of TEACHER submissions
395     global $CFG;
397     $timenow = time();
398     $n = 0;
399     if ($submissions = workshop_get_teacher_submissions($workshop)) {
400         foreach ($submissions as $submission) {
401             if ($assessments = workshop_get_assessments($submission)) {
402                 foreach ($assessments as $assessment) {
403                     if ($assessment->timegraded == 0) {
404                         // ignore teacher assessments
405                         if (!isteacher($workshop->course, $assessment->userid)) {
406                             // must have created a little time ago
407                             if (($timenow - $assessment->timecreated) > $CFG->maxeditingtime) {
408                                 $n++;
409                                 }
410                             }
411                         }
412                     }
413                 }
414             }
415         }
416     return $n;
417     }
420 //////////////////////////////////////////////////////////////////////////////////////
421 function workshop_count_user_assessments($workshop, $user, $stype = "all") {
422     // returns the number of assessments allocated/made by a user, all of them, or just those 
423     // for the student or teacher submissions. The student's self assessments are included in the count.
424     // The maxeditingtime is NOT taken into account here also, allocated assessments which have not yet
425     // been done are counted as well
426     
427     $n = 0;
428     if ($assessments = workshop_get_user_assessments($workshop, $user)) {
429          foreach ($assessments as $assessment) {
430             switch ($stype) {
431                 case "all" :
432                     $n++;
433                     break;
434                 case "student" :
435                      $submission = get_record("workshop_submissions", "id", $assessment->submissionid);
436                     if (isstudent($workshop->course, $submission->userid)) {
437                         $n++;
438                         }
439                     break;
440                 case "teacher" :
441                      $submission = get_record("workshop_submissions", "id", $assessment->submissionid);
442                     if (isteacher($workshop->course, $submission->userid)) {
443                         $n++;
444                         }
445                     break;
446                 }
447             }
448         }
449     return $n;
450     }
453 //////////////////////////////////////////////////////////////////////////////////////
454 function workshop_count_user_assessments_done($workshop, $user) {
455     // returns the number of assessments actually done by a user
456     // the student's self assessments are included in the count
457     // the maxeditingtime is NOT taken into account here 
458     
459     $n = 0;
460     $timenow = time();
461     if ($assessments = workshop_get_user_assessments($workshop, $user)) {
462          foreach ($assessments as $assessment) {
463             if ($assessment->timecreated < $timenow) {
464                 $n++;
465                 }
466             }
467         }
468     return $n;
469     }
472 //////////////////////////////////////////////////////////////////////////////////////
473 function workshop_count_user_submissions($workshop, $user) {
474     // returns the number of (real) submissions make by this user
475     return count_records_select("workshop_submissions", "workshopid = $workshop->id AND 
476         userid = $user->id AND timecreated > 0");
477     }
480 //////////////////////////////////////////////////////////////////////////////////////
481 function workshop_delete_submitted_files($workshop, $submission) {
482 // Deletes the files in the workshop area for this submission
484     if ($basedir = workshop_file_area($workshop, $submission)) {
485         if ($files = get_directory_list($basedir)) {
486             foreach ($files as $file) {
487                 if (unlink("$basedir/$file")) {
488                     notify("Existing file '$file' has been deleted!");
489                     }
490                 else {
491                     notify("Attempt to delete file $basedir/$file has failed!");
492                     }
493                 }
494             }
495         }
496     }
499 //////////////////////////////////////////////////////////////////////////////////////
500 function workshop_delete_user_files($workshop, $user, $exception) {
501 // Deletes all the user files in the workshop area for a user
502 // EXCEPT for any file named $exception
504     if (!$submissions = workshop_get_submissions($workshop, $user)) {
505         notify("No submissions!");
506         return;
507         }
508     foreach ($submissions as $submission) {
509         if ($basedir = workshop_file_area($workshop, $submission)) {
510             if ($files = get_directory_list($basedir)) {
511                 foreach ($files as $file) {
512                     if ($file != $exception) {
513                         unlink("$basedir/$file");
514                         notify("Existing file '$file' has been deleted!");
515                         }
516                     }
517                 }
518             }
519         }
520     }
523 //////////////////////////////////////////////////////////////////////////////////////
524 function workshop_get_all_teacher_assessments($workshop) {
525 // Return all teacher assessments, ordered by timecreated, oldest first
526     global $CFG;
527     
528     return get_records_sql("SELECT a.* FROM {$CFG->prefix}workshop_assessments a, 
529                             {$CFG->prefix}user_teachers u
530                             WHERE u.course = $workshop->course
531                               AND a.userid = u.userid
532                               AND a.workshopid = $workshop->id 
533                               ORDER BY a.timecreated");
537 //////////////////////////////////////////////////////////////////////////////////////
538 function workshop_get_comments($assessment) {
539     // Return all comments for this assessment provided they are newer than the assessment, 
540     // and ordered oldest first, newest last
541    return get_records_select("workshop_comments", "(assessmentid = $assessment->id) AND 
542         timecreated > $assessment->timecreated",
543         "timecreated DESC");
547 //////////////////////////////////////////////////////////////////////////////////////
548 function workshop_get_student_assessments($workshop, $user) {
549 // Return all assessments on the student submissions by a user, order by youngest first, oldest last
550     global $CFG;
552     // make sure it works on the site course
553     $select = "u.course = '$workshop->course' AND";
554     if ($workshop->course == SITEID) {
555         $select = '';
556     }
558     return get_records_sql("SELECT a.* FROM {$CFG->prefix}workshop_submissions s, 
559                             {$CFG->prefix}user_students u,
560                             {$CFG->prefix}workshop_assessments a
561                             WHERE $select s.userid = u.userid
562                               AND s.workshopid = $workshop->id
563                               AND a.submissionid = s.id
564                               AND a.userid = $user->id
565                               ORDER BY a.timecreated DESC");
569 //////////////////////////////////////////////////////////////////////////////////////
570 function workshop_get_student_submission_assessments($workshop) {
571 // Return all assessments on the student submissions, order by youngest first, oldest last
572     global $CFG;
574     // make sure it works on the site course
575     $select = "u.course = '$workshop->course' AND";
576     if ($workshop->course == SITEID) {
577         $select = '';
578     }
580     return get_records_sql("SELECT a.* FROM {$CFG->prefix}workshop_submissions s, 
581                             {$CFG->prefix}user_students u, {$CFG->prefix}workshop_assessments a
582                             WHERE $select s.userid = u.userid
583                               AND s.workshopid = $workshop->id
584                               AND a.submissionid = s.id
585                               ORDER BY a.timecreated DESC");
589 //////////////////////////////////////////////////////////////////////////////////////
590 function workshop_get_submission_assessment($submission, $user) {
591     // Return the user's assessment for this submission (cold or warm, not hot)
592     
593     $timenow = time();
594     return get_record_select("workshop_assessments", "submissionid = $submission->id AND 
595             userid = $user->id AND timecreated < $timenow");
599 //////////////////////////////////////////////////////////////////////////////////////
600 function workshop_get_teacher_assessments($courseid, $submission) {
601 // Return teacher assessments of a submission, ordered by timecreated, oldest first
602     global $CFG;
603     
604     return get_records_sql("SELECT a.* FROM {$CFG->prefix}workshop_assessments a, 
605                             {$CFG->prefix}user_teachers u
606                             WHERE u.course = $courseid
607                               AND a.userid = u.userid
608                               AND a.submissionid = $submission->id 
609                               ORDER BY a.timecreated");
613 //////////////////////////////////////////////////////////////////////////////////////
614 function workshop_get_teacher_submission_assessments($workshop) {
615 // Return all assessments on the teacher submissions, order by youngest first, oldest last
616     global $CFG;
617     
618     return get_records_sql("SELECT a.* FROM {$CFG->prefix}workshop_submissions s, 
619                             {$CFG->prefix}user_teachers u, {$CFG->prefix}workshop_assessments a
620                             WHERE u.course = $workshop->course
621                               AND s.userid = u.userid
622                               AND s.workshopid = $workshop->id
623                               AND a.submissionid = s.id
624                               ORDER BY a.timecreated DESC");
628 //////////////////////////////////////////////////////////////////////////////////////
629 function workshop_get_teacher_submissions($workshop) {
630 // Return all  teacher submissions, ordered by title
631     global $CFG;
632     
633     return get_records_sql("SELECT s.* FROM {$CFG->prefix}workshop_submissions s, 
634                             {$CFG->prefix}user_teachers u
635                             WHERE u.course = $workshop->course
636                               AND s.userid = u.userid
637                               AND s.workshopid = $workshop->id 
638                               ORDER BY s.title");
642 //////////////////////////////////////////////////////////////////////////////////////
643 function workshop_get_ungraded_assessments($workshop) {
644     global $CFG;
645     // Return all assessments which have not been graded or just graded
646     $cutofftime = time() - $CFG->maxeditingtime;
647     return get_records_select("workshop_assessments", "workshopid = $workshop->id AND (timegraded = 0 OR 
648                 timegraded > $cutofftime)", "timecreated"); 
649     }
652 //////////////////////////////////////////////////////////////////////////////////////
653 function workshop_get_ungraded_assessments_student($workshop) {
654     global $CFG;
655     // Return all assessments which have not been graded or just graded of student's submissions
657     // make sure it works on the site course
658     $select = "u.course = '$workshop->course' AND";
659     if ($workshop->course == SITEID) {
660         $select = '';
661     }
663     $cutofftime = time() - $CFG->maxeditingtime;
664     return get_records_sql("SELECT a.* FROM {$CFG->prefix}workshop_submissions s, 
665                             {$CFG->prefix}user_students u, {$CFG->prefix}workshop_assessments a
666                             WHERE $select s.userid = u.userid
667                               AND s.workshopid = $workshop->id
668                               AND a.submissionid = s.id
669                               AND (a.timegraded = 0 OR a.timegraded > $cutofftime)
670                               AND a.timecreated < $cutofftime
671                               ORDER BY a.timecreated ASC"); 
672     }
675 //////////////////////////////////////////////////////////////////////////////////////
676 function workshop_get_ungraded_assessments_teacher($workshop) {
677     global $CFG;
678     // Return all assessments which have not been graded or just graded of teacher's submissions
679     
680     $cutofftime =time() - $CFG->maxeditingtime;
681     return get_records_sql("SELECT a.* FROM {$CFG->prefix}workshop_submissions s, 
682                             {$CFG->prefix}user_teachers u, {$CFG->prefix}workshop_assessments a
683                             WHERE u.course = $workshop->course
684                               AND s.userid = u.userid
685                               AND s.workshopid = $workshop->id
686                               AND a.submissionid = s.id
687                               AND (a.timegraded = 0 OR a.timegraded > $cutofftime)
688                               AND a.timecreated < $cutofftime
689                               ORDER BY a.timecreated ASC"); 
690     }
693 //////////////////////////////////////////////////////////////////////////////////////
694 function workshop_get_user_assessments_done($workshop, $user) {
695 // Return all the  user's assessments, oldest first, newest last (warm and cold ones only)
696 // ignores maxeditingtime
697     $timenow = time();
698     return get_records_select("workshop_assessments", "workshopid = $workshop->id AND userid = $user->id
699                 AND timecreated < $timenow", 
700                 "timecreated ASC");
704 //////////////////////////////////////////////////////////////////////////////////////
705 function workshop_get_users_done($workshop) {
706     global $CFG;
708     // make sure it works on the site course
709     $select = "s.course = '$workshop->course' AND";
710     if ($workshop->course == SITEID) {
711         $select = '';
712     }
714     return get_records_sql("SELECT u.* 
715                     FROM {$CFG->prefix}user u, {$CFG->prefix}user_students s, 
716                          {$CFG->prefix}workshop_submissions a
717                     WHERE $select s.user = u.id
718                     AND u.id = a.user AND a.workshop = '$workshop->id'
719                     ORDER BY a.timemodified DESC");
723 //////////////////////////////////////////////////////////////////////////////////////
724 function workshop_list_all_submissions($workshop, $user) {
725     // list the teacher sublmissions first
726     global $CFG;
727     
728     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
729         error("Course Module ID was incorrect");
730     }
731     if (! $course = get_record("course", "id", $workshop->course)) {
732         error("Course is misconfigured");
733         }
734     $table->head = array (get_string("title", "workshop"), get_string("action", "workshop"), 
735                         get_string("comment", "workshop"));
736     $table->align = array ("left", "left", "left");
737     $table->size = array ("*", "*", "*");
738     $table->cellpadding = 2;
739     $table->cellspacing = 0;
741     if ($submissions = workshop_get_teacher_submissions($workshop)) {
742         foreach ($submissions as $submission) {
743             if ($submission->userid == $user->id) {
744                 $comment = get_string("ownwork", "workshop")."; ";
745                 }
746             else {
747                 $comment = "";
748                 }
749             // has user already assessed this submission
750             if ($assessment = get_record_select("workshop_assessments", "submissionid = $submission->id
751                     AND userid = $user->id")) {
752                 $curtime = time();
753                 if (($curtime - $assessment->timecreated) > $CFG->maxeditingtime) {
754                     $action = "<a href=\"assessments.php?action=viewassessment&amp;id=$cm->id&amp;aid=$assessment->id\">"
755                         .get_string("view", "workshop")."</a>";
756                     // has teacher graded user's assessment?
757                     if ($assessment->timegraded) {
758                         if (($curtime - $assessment->timegraded) > $CFG->maxeditingtime) {
759                             $comment .= get_string("gradedbyteacher", "workshop", $course->teacher);
760                             }
761                         }
762                     }
763                 else { // there's still time left to edit...
764                     $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
765                         get_string("edit", "workshop")."</a>";
766                     }
767                 }
768             else { // user has not graded this submission
769                 $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
770                     get_string("assess", "workshop")."</a>";
771                 }
772             $table->data[] = array(workshop_print_submission_title($workshop, $submission), $action, 
773                                 $comment);
774             }
775         print_table($table);
776         }
778     echo "<center><p><b>".get_string("studentsubmissions", "workshop", $course->student).
779         "</b></center><br />\n";
780     unset($table);
781     $table->head = array (get_string("title", "workshop"), get_string("action", "workshop"), 
782                         get_string("comment", "workshop"));
783     $table->align = array ("left", "left", "left");
784     $table->size = array ("*", "*", "*");
785     $table->cellpadding = 2;
786     $table->cellspacing = 0;
788     if ($submissions = workshop_get_student_submissions($workshop)) {
789         foreach ($submissions as $submission) {
790             if ($submission->userid == $user->id) {
791                 $comment = get_string("ownwork", "workshop")."; ";
792                 }
793             else {
794                 $comment = "";
795                 }
796             // has user already assessed this submission
797             if ($assessment = get_record_select("workshop_assessments", "submissionid = $submission->id
798                     AND userid = $user->id")) {
799                 $curtime = time();
800                 if (($curtime - $assessment->timecreated) > $CFG->maxeditingtime) {
801                     $action = "<a href=\"assessments.php?action=viewassessment&amp;id=$cm->id&amp;aid=$assessment->id\">".
802                         get_string("view", "workshop")."</a>";
803                     // has teacher graded on user's assessment?
804                     if ($assessment->timegraded) {
805                         if (($curtime - $assessment->timegraded) > $CFG->maxeditingtime) {
806                             $comment .= get_string("gradedbyteacher", "workshop", $course->teacher)."; ";
807                             }
808                         }
809                     $otherassessments = workshop_get_assessments($submission);
810                     if (count($otherassessments) > 1) {
811                         $comment .= "<a href=\"assessments.php?action=viewallassessments&amp;id=$cm->id&amp;sid=$submission->id\">".
812                         get_string("viewotherassessments", "workshop")."</a>";
813                         }
814                     }
815                 else { // there's still time left to edit...
816                     $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
817                         get_string("edit", "workshop")."</a>";
818                     }
819                 }
820             else { // user has not assessed this submission
821                 $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
822                     get_string("assess", "workshop")."</a>";
823                 }
824             $table->data[] = array(workshop_print_submission_title($workshop, $submission), $action, 
825                                 $comment);
826             }
827         print_table($table);
828         }
829     }
832 //////////////////////////////////////////////////////////////////////////////////////
833 function workshop_list_all_ungraded_assessments($workshop) {
834     // lists all the assessments for comment by teacher
835     global $CFG;
837     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
838         error("Course Module ID was incorrect");
839     }
840     
841     $table->head = array (get_string("title", "workshop"), get_string("timeassessed", "workshop"), get_string("action", "workshop"));
842     $table->align = array ("left", "left", "left");
843     $table->size = array ("*", "*", "*");
844     $table->cellpadding = 2;
845     $table->cellspacing = 0;
846     $timenow = time();
847     
848     if ($assessments = workshop_get_ungraded_assessments($workshop)) {
849         foreach ($assessments as $assessment) {
850             if (!isteacher($workshop->course, $assessment->userid)) {
851                 if (($timenow - $assessment->timegraded) < $CFG->maxeditingtime) {
852                     $action = "<a href=\"assessments.php?action=gradeassessment&amp;id=$cm->id&amp;aid=$assessment->id\">".
853                         get_string("edit", "workshop")."</a>";
854                     }
855                 else {
856                     $action = "<a href=\"assessments.php?action=gradeassessment&amp;id=$cm->id&amp;aid=$assessment->id\">".
857                         get_string("gradeassessment", "workshop")."</a>";
858                     }
859                 $submission = get_record("workshop_submissions", "id", $assessment->submissionid);
860                 $table->data[] = array(workshop_print_submission_title($workshop, $submission), 
861                     userdate($assessment->timecreated), $action);
862                 }
863             }
864         if (isset($table->data)) {
865             print_table($table);
866             }
867         }
868     }
869     
871 //////////////////////////////////////////////////////////////////////////////////////
872 function workshop_list_assessed_submissions($workshop, $user) {
873     // list the submissions that have been assessed by this user and are COLD
874     global $CFG, $USER;
875     
876     $timenow = time();
877     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
878         error("Course Module ID was incorrect");
879     }
880     if (! $course = get_record("course", "id", $workshop->course)) {
881         error("Course is misconfigured");
882         }
883     $table->head = array (get_string("title","workshop"), get_string("action","workshop"), 
884                     get_string("comment","workshop"));
885     $table->align = array ("left", "left", "left");
886     $table->size = array ("*", "*", "*");
887     $table->cellpadding = 2;
888     $table->cellspacing = 0;
890     if ($assessments = workshop_get_student_assessments($workshop, $user)) {
891         foreach ($assessments as $assessment) {
892             $comment = "";
893             $submission = get_record("workshop_submissions", "id", $assessment->submissionid);
894             // the assessment may be in three states: 
895             // 1. "hot", just created but not completed (timecreated is in the future)
896             // 2. "warm" just created and still capable of being edited, and 
897             // 3. "cold" after the editing time
898                 
899             if ($assessment->timecreated < ($timenow - $CFG->maxeditingtime)) { // it's cold
900                 if ($workshop->agreeassessments) {
901                     if (!$assessment->timeagreed) {
902                         $action = "<a href=\"assessments.php?action=viewassessment&amp;id=$cm->id&amp;aid=$assessment->id&".
903                             "allowcomments=$workshop->agreeassessments\">".
904                             get_string("view", "workshop")."</a>";
905                         $action .= " | <a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
906                             get_string("reassess", "workshop")."</a>";
907                     } else {
908                         $action = "<a href=\"assessments.php?action=viewassessment&amp;id=$cm->id&amp;aid=$assessment->id&".
909                             "allowcomments=false\">".get_string("view", "workshop")."</a>";
910                     }
911                 } else {
912                     // if it been graded allow student to re-assess, except if it's a self assessment
913                     if ($assessment->timegraded and !($USER->id == $assessment->userid)) {
914                         $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
915                             get_string("reassess", "workshop")."</a>";
916                     } else {
917                         $action = "<a href=\"assessments.php?action=viewassessment&amp;id=$cm->id&amp;aid=$assessment->id\">".
918                             get_string("view", "workshop")."</a>";
919                     }
920                 }          
921                 if ($assessment->timecreated < $timenow) { // only show the date if it's in the past (future dates cause confusion)
922                     $comment = get_string("assessedon", "workshop", userdate($assessment->timecreated));
923                 }
924                 else {
925                     $comment = '';
926                 }
927                 if ($submission->userid == $user->id) { // self assessment?
928                     $comment .= "; ".get_string("ownwork", "workshop"); // just in case they don't know!
929                 }
930                 // has assessment been graded?
931                 if ($assessment->timegraded and ($timenow - $assessment->timegraded > $CFG->maxeditingtime)) {
932                     if ($workshop->gradingstrategy) { // supress grading grade if not graded
933                         $comment .= "; ".get_string("thegradeforthisassessmentis", "workshop", 
934                             number_format($assessment->gradinggrade * $workshop->gradinggrade / 100, 0)).
935                             " / $workshop->gradinggrade";
936                     }
937                 }
938                 // if peer agreements show whether agreement has been reached
939                 if ($workshop->agreeassessments) {
940                     if ($assessment->timeagreed) {
941                         $comment .= "; ".get_string("assessmentwasagreedon", "workshop", 
942                                 userdate($assessment->timeagreed));
943                     }
944                     else {
945                         $comment .= "; ".get_string("assessmentnotyetagreed", "workshop");
946                     }
947                 }
948                 $table->data[] = array(workshop_print_submission_title($workshop, $submission), $action, 
949                                     $comment);
950             }
951         }
952     }
953     if (isset($table->data)) {
954         print_table($table);
955     }
956     else {
957         echo "<center>".get_string("noassessmentsdone", "workshop")."</center>\n";
958     }
963 //////////////////////////////////////////////////////////////////////////////////////
964 function workshop_list_peer_assessments($workshop, $user) {
965     global $CFG;
966     
967     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
968         error("Course Module ID was incorrect");
969     }
970     if (! $course = get_record("course", "id", $workshop->course)) {
971         error("Course is misconfigured");
972         }
973     $table->head = array (get_string("title", "workshop"), get_string("action", "workshop"), 
974                     get_string("comment", "workshop"));
975     $table->align = array ("left", "left", "left");
976     $table->size = array ("*", "*", "*");
977     $table->cellpadding = 2;
978     $table->cellspacing = 0;
980     // get user's submissions
981     if ($submissions = workshop_get_user_submissions($workshop, $user)) {
982         foreach ($submissions as $submission) {
983             // get the assessments
984             if ($assessments = workshop_get_assessments($submission)) {
985                 foreach ($assessments as $assessment) {
986                     // assessments by students only and exclude any self assessments
987                     if (isstudent($workshop->course, $assessment->userid) and 
988                             ($assessment->userid != $user->id)) { 
989                         $timenow = time();
990                         if (($timenow - $assessment->timecreated) > $CFG->maxeditingtime) {
991                             $action = "<a href=\"assessments.php?action=viewassessment&amp;id=$cm->id&amp;aid=$assessment->id&".
992                                 "allowcomments=$workshop->agreeassessments\">".
993                                 get_string("view", "workshop")."</a>";
994                             $comment = get_string("assessedon", "workshop", userdate($assessment->timecreated));
995                             $grade = number_format($assessment->grade * $workshop->grade / 100, 1);
996                             if ($workshop->gradingstrategy) { // supress grade if not graded
997                                 $comment .= "; ".get_string("gradeforsubmission", "workshop").
998                                     ": $grade / $workshop->grade"; 
999                             }
1000                             if ($assessment->timegraded) {
1001                                 if (!$assessment->gradinggrade) {
1002                                     // it's a bad assessment
1003                                     $comment .= "; ".get_string("thisisadroppedassessment", "workshop");
1004                                 }
1005                             }
1006                             if (isteacher($workshop->course, $assessment->userid) and $workshop->teacherweight) {
1007                                 $comment .= "; ".get_string("thisisadroppedassessment", "workshop");
1008                             }
1009                             // if peer agreements show whether agreement has been reached
1010                             if ($workshop->agreeassessments) {
1011                                 if ($assessment->timeagreed) {
1012                                     $comment .= "; ".get_string("assessmentwasagreedon", "workshop", 
1013                                                         userdate($assessment->timeagreed));
1014                                     }
1015                                 else {
1016                                     $comment .= "; ".get_string("assessmentnotyetagreed", "workshop");
1017                                     }
1018                                 }
1019                             $table->data[] = array(workshop_print_submission_title($workshop, $submission), 
1020                                                 $action, $comment);
1021                             }
1022                         }
1023                     }
1024                 }
1025             }
1026         }
1027     if (isset($table->data)) {
1028         print_table($table);
1029         }
1030     else {
1031         echo "<center>".get_string("noassessmentsdone", "workshop")."</center>\n";
1032         }
1033     }
1037 //////////////////////////////////////////////////////////////////////////////////////
1038 function workshop_list_self_assessments($workshop, $user) {
1039     // list  user's submissions for the user to assess
1040     global $CFG;
1041     
1042     $timenow = time();
1043     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
1044         error("Course Module ID was incorrect");
1045     }
1046     if (! $course = get_record("course", "id", $workshop->course)) {
1047         error("Course is misconfigured");
1048         }
1049     $table->head = array (get_string("title", "workshop"), get_string("action", "workshop"), 
1050                        get_string("comment", "workshop"));
1051     $table->align = array ("left", "left", "left");
1052     $table->size = array ("*", "*", "*");
1053     $table->cellpadding = 2;
1054     $table->cellspacing = 0;
1056     // get the user's submissions 
1057     if ($submissions = workshop_get_user_submissions($workshop, $user)) {
1058         foreach ($submissions as $submission) {
1059             $comment = get_string("ownwork", "workshop"); // just in case they don't know!
1060             if (!$assessment = get_record_select("workshop_assessments", "submissionid = $submission->id AND
1061                     userid = $user->id")) {
1062                 $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1063                     get_string("assess", "workshop")."</a>";
1064                 $table->data[] = array(workshop_print_submission_title($workshop, $submission), $action, $comment);
1065             } else {
1066                 // may still be warm
1067                 if (($assessment->timecreated + $CFG->maxeditingtime) > $timenow) {
1068                     $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1069                         get_string("reassess", "workshop")."</a>";
1070                     $table->data[] = array(workshop_print_submission_title($workshop, $submission), $action, $comment);
1071                 }
1072             }
1073         
1074         }
1075     }
1076     if (isset($table->data)) {
1077         echo "<p><center><b>".get_string("pleaseassessyoursubmissions", "workshop", $course->student).
1078             "</b></center><br />\n";
1079         print_table($table);
1080     }
1084 //////////////////////////////////////////////////////////////////////////////////////
1085 function workshop_list_student_submissions($workshop, $user) {
1086     // list available submissions for this user to assess, submissions with the least number 
1087     // of assessments are show first
1088     global $CFG;
1089     
1090     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
1091         error("Course Module ID was incorrect");
1092     }
1093     if (! $course = get_record("course", "id", $workshop->course)) {
1094         error("Course is misconfigured");
1095         }
1097     $timenow = time();
1099     // set student's group if workshop is in SEPARATEGROUPS mode
1100     if (groupmode($course, $cm) == SEPARATEGROUPS) {
1101         $groupid = get_current_group($course->id);
1102     } else {
1103         $groupid = 0;
1104     }
1105     
1106     $table->head = array (get_string("title", "workshop"), get_string("action", "workshop"), get_string("comment", "workshop"));
1107     $table->align = array ("left", "left", "left");
1108     $table->size = array ("*", "*", "*");
1109     $table->cellpadding = 2;
1110     $table->cellspacing = 0;
1112     // get the number of assessments this user has done on student submission, deduct self assessments
1113     $nassessed = workshop_count_user_assessments($workshop, $user, "student") - 
1114         workshop_count_self_assessments($workshop, $user);
1115     // user hasn't been allocated enough, try and get some more
1116     if ($nassessed < $workshop->nsassessments) {
1117         // count the number of assessments for each student submission
1118         if ($submissions = workshop_get_student_submissions($workshop)) {
1119             // srand ((float)microtime()*1000000); // now done automatically in PHP 4.2.0->
1120             foreach ($submissions as $submission) {
1121                 // check group membership, if necessary
1122                 if ($groupid) {
1123                     // check user's group
1124                     if (!ismember($groupid, $submission->userid)) {
1125                         continue; // skip this submission
1126                   }
1127                 }
1128                 // process only cold submissions
1129                 if (($submission->timecreated + $CFG->maxeditingtime) > $timenow) {
1130                     continue;
1131                 }
1132                 $n = count_records("workshop_assessments", "submissionid", $submission->id);
1133                 // ...OK to have zero, we add a small random number to randomise things
1134                 $nassessments[$submission->id] = $n + rand(0, 98) / 100;
1135                 }
1136                 
1137             if (isset($nassessments)) { // make sure we end up with something to play with
1138                 // put the submissions with the lowest number of assessments first
1139                 asort($nassessments);
1140                 reset($nassessments);
1141                 $nsassessments = $workshop->nsassessments;
1142                 foreach ($nassessments as $submissionid =>$n) {
1143                     // only use those submissions which fall below the allocation threshold
1144                     if ($n < ($workshop->nsassessments + $workshop->overallocation)) {
1145                         $comment = "";
1146                         $submission = get_record("workshop_submissions", "id", $submissionid);
1147                         // skip submission if it belongs to this user
1148                         if ($submission->userid != $user->id) {
1149                             // add a "hot" assessment record if user has NOT already assessed this submission
1150                             if (!get_record("workshop_assessments", "submissionid", $submission->id, "userid",
1151                                         $user->id)) {
1152                                 $yearfromnow = time() + 365 * 86400;
1153                                 // ...create one and set timecreated way in the future, this is reset when record is updated
1154                                 unset($assessment); // clear previous version object (if any)
1155                                 $assessment->workshopid = $workshop->id;
1156                                 $assessment->submissionid = $submission->id;
1157                                 $assessment->userid = $user->id;
1158                                 $assessment->grade = -1; // set impossible grade
1159                                 $assessment->timecreated = $yearfromnow;
1160                                 if (!$assessment->id = insert_record("workshop_assessments", $assessment)) {
1161                                     error("List Student submissions: Could not insert workshop assessment!");
1162                                 }
1163                                 $nassessed++;
1164                                 // is user up to quota?
1165                                 if ($nassessed == $nsassessments) {
1166                                     break;
1167                                 }
1168                             }
1169                         }
1170                     }
1171                 }
1172             }
1173         }
1174     }
1175     // now list the student submissions this user has been allocated, list only the hot and warm ones, 
1176     // the cold ones are listed in the "your assessments list" (_list_assessed submissions)
1177     if ($assessments = workshop_get_user_assessments($workshop, $user)) {
1178         $timenow = time();
1179         foreach ($assessments as $assessment) {
1180             if (!$submission = get_record("workshop_submissions", "id", $assessment->submissionid)) {
1181                 error ("workshop_list_student_submissions: unable to get submission");
1182                 }
1183             // submission from a student?
1184             if (isstudent($workshop->course, $submission->userid)) {
1185                 $comment = '';
1186                 // user assessment has three states: record created but not assessed (date created in the future) [hot]; 
1187                 // just assessed but still editable [warm]; and "static" (may or may not have been graded by teacher, that
1188                 // is shown in the comment) [cold] 
1189                 if ($assessment->timecreated > $timenow) { // user needs to assess this submission
1190                     $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1191                         get_string("assess", "workshop")."</a>";
1192                     $table->data[] = array(workshop_print_submission_title($workshop, $submission), $action, $comment);
1193                     }
1194                 elseif ($assessment->timecreated > ($timenow - $CFG->maxeditingtime)) { // there's still time left to edit...
1195                     $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1196                         get_string("edit", "workshop")."</a>";
1197                     $table->data[] = array(workshop_print_submission_title($workshop, $submission), $action, $comment);
1198                     }
1199                 }
1200             }
1201         }
1202     
1203     if (isset($table->data)) {
1204         echo "<p><center><b>".get_string("pleaseassessthesestudentsubmissions", "workshop", $course->student).
1205             "</b></center><br />\n";
1206         print_table($table);
1207         }
1208     else {
1209         echo "<p><center><b>".get_string("nosubmissionsavailableforassessment", "workshop")."</b></center><br />\n";
1210         }
1211     }
1214 //////////////////////////////////////////////////////////////////////////////////////
1215 function workshop_list_submissions_for_admin($workshop, $order) {
1216     // list the teacher sublmissions first
1217     global $CFG, $USER;
1218     
1219     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
1220         error("Course Module ID was incorrect");
1221     }
1222     if (! $course = get_record("course", "id", $workshop->course)) {
1223         error("Course is misconfigured");
1224         }
1225     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $course->id)) {
1226         error("Course Module ID was incorrect");
1227     }
1228     if (groupmode($course, $cm) == SEPARATEGROUPS) {
1229         $groupid = get_current_group($course->id);
1230     } else {
1231         $groupid = 0;
1232     }
1233     
1234     workshop_print_assignment_info($workshop);
1236     if (isteacheredit($course->id)) {
1237         // list any teacher submissions
1238         $table->head = array (get_string("title", "workshop"), get_string("submittedby", "workshop"), 
1239                 get_string("action", "workshop"));
1240         $table->align = array ("left", "left", "left");
1241         $table->size = array ("*", "*", "*");
1242         $table->cellpadding = 2;
1243         $table->cellspacing = 0;
1245         if ($submissions = workshop_get_teacher_submissions($workshop)) {
1246             foreach ($submissions as $submission) {
1247                 $action = "<a href=\"submissions.php?action=adminamendtitle&amp;id=$cm->id&amp;sid=$submission->id\">".
1248                     get_string("amendtitle", "workshop")."</a>";
1249                 // has user already assessed this submission
1250                 if ($assessment = get_record_select("workshop_assessments", "submissionid = $submission->id
1251                             AND userid = $USER->id")) {
1252                     $curtime = time();
1253                 if ($assessment->timecreated > $curtime) { // it's a "hanging" assessment 
1254                     $action .= " | <a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1255                         get_string("assess", "workshop")."</a>";
1256                 }
1257                 elseif (($curtime - $assessment->timecreated) > $CFG->maxeditingtime) {
1258                     $action .= " | <a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">"
1259                         .get_string("reassess", "workshop")."</a>";
1260                 }
1261                 else { // there's still time left to edit...
1262                     $action .= " | <a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1263                         get_string("edit", "workshop")."</a>";
1264                 }
1265             }
1266                 else { // user has not graded this submission
1267                     $action .= " | <a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1268                         get_string("assess", "workshop")."</a>";
1269                 }
1270                 if ($assessments = workshop_get_assessments($submission)) {
1271                     $action .= " | <a href=\"assessments.php?action=adminlist&amp;id=$cm->id&amp;sid=$submission->id\">".
1272                         get_string("listassessments", "workshop")."</a>";
1273                 }
1274                 if (isteacheredit($course->id)) {
1275                     $action .= " | <a href=\"submissions.php?action=adminconfirmdelete&amp;id=$cm->id&amp;sid=$submission->id\">".
1276                         get_string("delete", "workshop")."</a>";
1277                 }
1278                 $table->data[] = array("<a href=\"submissions.php?action=editsubmission&amp;id=$cm->id&amp;sid=$submission->id\">$submission->title</a>", $course->teacher, $action);
1279             }
1280             print_heading(get_string("studentsubmissions", "workshop", $course->teacher), "center");
1281             print_table($table);
1282         }
1283     }
1285     // list student assessments
1286     // Get all the students...
1287     if ($users = get_course_students($course->id, "u.lastname, u.firstname")) {
1288         $timenow = time();
1289         unset($table);
1290         $table->head = array(get_string("name"), get_string("title", "workshop"), get_string("action", "workshop"));
1291         $table->align = array ("left", "left", "left");
1292         $table->size = array ("*", "*", "*");
1293         $table->cellpadding = 2;
1294         $table->cellspacing = 0;
1295         $nassessments = 0;
1296         foreach ($users as $user) {
1297             // check group membership, if necessary
1298             if ($groupid) {
1299                 // check user's group
1300                 if (!ismember($groupid, $user->id)) {
1301                     continue; // skip this user
1302                 }
1303             }
1304             // list the assessments which have been done (exclude the hot ones)
1305             if ($assessments = workshop_get_user_assessments_done($workshop, $user)) {
1306                 $title ='';
1307                 foreach ($assessments as $assessment) {
1308                     if (!$submission = get_record("workshop_submissions", "id", $assessment->submissionid)) {
1309                         error("Workshop_list_submissions_for_admin: Submission record not found!");
1310                     }
1311                     $title .= $submission->title;
1312                     if ($workshop->agreeassessments and !$assessment->timeagreed and 
1313                             isstudent($workshop->course, $submission->userid)) { // agreements for student work only
1314                         $title .= " &lt;&lt;".number_format($assessment->grade * $workshop->grade / 100, 0)." (".
1315                             number_format($assessment->gradinggrade * $workshop->gradinggrade / 100, 0).")&gt;&gt; ";
1316                     } elseif ($assessment->timegraded) {
1317                         if ($assessment->gradinggrade) {
1318                             // a good assessment
1319                             $title .= " {".number_format($assessment->grade * $workshop->grade / 100, 0)." (".
1320                                 number_format($assessment->gradinggrade * $workshop->gradinggrade / 100, 0).")} ";
1321                         } else { 
1322                             // a poor assessment
1323                             $title .= " &lt;".number_format($assessment->grade * $workshop->grade / 100, 0)." (".
1324                                 number_format($assessment->gradinggrade * $workshop->gradinggrade / 100, 0).")&gt; ";
1325                         }
1326                     } else {
1327                         // not yet graded
1328                         $title .= " {".number_format($assessment->grade * $workshop->grade / 100, 0)." ((".
1329                             number_format($assessment->gradinggrade * $workshop->gradinggrade / 100, 0)."))} ";
1330                     }
1331                     if ($realassessments = workshop_count_user_assessments_done($workshop, $user)) {
1332                         $action = "<a href=\"assessments.php?action=adminlistbystudent&amp;id=$cm->id&amp;userid=$user->id\">".
1333                             get_string("liststudentsassessments", "workshop")." ($realassessments)</a>";
1334                     } else {
1335                         $action ="";
1336                     }
1337                 }
1338                 $nassessments++;
1339                 $table->data[] = array(fullname($user), $title, $action);
1340             }
1341         }
1342         if (isset($table->data)) {
1343             print_heading(get_string("studentassessments", "workshop", $course->student)." [$nassessments]");
1344             print_table($table);
1345             workshop_print_key($workshop);
1346             // grading grade analysis
1347             unset($table);
1348             $table->head = array (get_string("count", "workshop"), get_string("mean", "workshop"),
1349                 get_string("standarddeviation", "workshop"), get_string("maximum", "workshop"), 
1350                 get_string("minimum", "workshop"));
1351             $table->align = array ("center", "center", "center", "center", "center");
1352             $table->size = array ("*", "*", "*", "*", "*");
1353             $table->cellpadding = 2;
1354             $table->cellspacing = 0;
1355             if ($groupid) {
1356                 $stats = get_record_sql("SELECT COUNT(*) as count, AVG(gradinggrade) AS mean, 
1357                         STDDEV(gradinggrade) AS stddev, MIN(gradinggrade) AS min, MAX(gradinggrade) AS max 
1358                         FROM {$CFG->prefix}groups_members g, {$CFG->prefix}workshop_assessments a 
1359                         WHERE g.groupid = $groupid AND a.userid = g.userid AND a.timegraded > 0 
1360                         AND a.workshopid = $workshop->id");
1361             } else { // no group/all participants
1362                 $stats = get_record_sql("SELECT COUNT(*) as count, AVG(gradinggrade) AS mean, 
1363                         STDDEV(gradinggrade) AS stddev, MIN(gradinggrade) AS min, MAX(gradinggrade) AS max 
1364                         FROM {$CFG->prefix}workshop_assessments a 
1365                         WHERE a.timegraded > 0 AND a.workshopid = $workshop->id");
1366             }   
1367             $table->data[] = array($stats->count, number_format($stats->mean * $workshop->gradinggrade / 100, 1), 
1368                     number_format($stats->stddev * $workshop->gradinggrade /100, 1), 
1369                     number_format($stats->max * $workshop->gradinggrade / 100, 1), 
1370                     number_format($stats->min* $workshop->gradinggrade / 100, 1));
1371             print_heading(get_string("gradinggrade", "workshop")." ".get_string("analysis", "workshop"));
1372             print_table($table);
1373             echo "<p align=\"center\"><a href=\"assessments.php?id=$cm->id&amp;action=regradestudentassessments\">".
1374                     get_string("regradestudentassessments", "workshop")."</a> ";
1375             helpbutton("regrading", get_string("regradestudentassessments", "workshop"), "workshop");
1376             echo "</p>\n";
1377         }
1378     }
1380     // now the sudent submissions
1381     unset($table);
1382     switch ($order) {
1383         case "title" :
1384             $table->head = array("<a href=\"submissions.php?action=adminlist&amp;id=$cm->id&amp;order=name\">".
1385                  get_string("submittedby", "workshop")."</a>", get_string("title", "workshop"), 
1386                  get_string("submitted", "workshop"), get_string("action", "workshop"));
1387             break;
1388         case "name" :
1389             $table->head = array (get_string("submittedby", "workshop"), 
1390                 "<a href=\"submissions.php?action=adminlist&amp;id=$cm->id&amp;order=title\">".
1391                 get_string("title", "workshop")."</a>", get_string("submitted", "workshop"), 
1392                 get_string("action", "workshop"));
1393             break;
1394     }
1395     $table->align = array ("left", "left", "left", "left");
1396     $table->size = array ("*", "*", "*", "*");
1397     $table->cellpadding = 2;
1398     $table->cellspacing = 0;
1399     
1400     $nsubmissions = 0;
1401     if ($submissions = workshop_get_student_submissions($workshop, $order)) {
1402         foreach ($submissions as $submission) {
1403             if (!$user = get_record("user", "id", $submission->userid)) {
1404                 error("workshop_list_submissions_for_admin: failure to get user record");
1405             }
1406             // check group membership, if necessary
1407             if ($groupid) {
1408                 // check user's group
1409                 if (!ismember($groupid, $user->id)) {
1410                     continue; // skip this user
1411                 }
1412             }
1413             $datesubmitted = userdate($submission->timecreated);
1414             if ($submission->late) {
1415                 $datesubmitted = "<font color=\"red\">".$datesubmitted."</font>";
1416             }
1417             $action = "<a href=\"submissions.php?action=adminamendtitle&amp;id=$cm->id&amp;sid=$submission->id\">".
1418                 get_string("amendtitle", "workshop")."</a>";
1419             // has teacher already assessed this submission
1420             if ($assessment = get_record_select("workshop_assessments", "submissionid = $submission->id
1421                     AND userid = $USER->id")) {
1422                 $curtime = time();
1423                 if (($curtime - $assessment->timecreated) > $CFG->maxeditingtime) {
1424                     $action .= " | <a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1425                         get_string("reassess", "workshop")."</a>";
1426                 }
1427                 else { // there's still time left to edit...
1428                     $action .= " | <a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1429                         get_string("edit", "workshop")."</a>";
1430                 }
1431             }
1432             else { // user has not assessed this submission
1433                 $action .= " | <a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1434                     get_string("assess", "workshop")."</a>";
1435             }
1436             if ($nassessments = workshop_count_assessments($submission)) {
1437                 $action .= " | <a href=\"assessments.php?action=adminlist&amp;id=$cm->id&amp;sid=$submission->id\">".
1438                     get_string("listassessments", "workshop")." ($nassessments)</a>";
1439             }
1440             if ($submission->late) {
1441                 $action .= " | <a href=\"submissions.php?action=adminlateflag&amp;id=$cm->id&amp;sid=$submission->id\">".
1442                     get_string("clearlateflag", "workshop")."</a>";
1443             }
1444             $action .= " | <a href=\"submissions.php?action=adminconfirmdelete&amp;id=$cm->id&amp;sid=$submission->id\">".
1445                 get_string("delete", "workshop")."</a>";
1446             $nsubmissions++;
1447             $table->data[] = array("$user->firstname $user->lastname", $submission->title.
1448                 " (".get_string("grade").": ".workshop_submission_grade($workshop, $submission)." ".
1449                 workshop_print_submission_assessments($workshop, $submission, "teacher").
1450                 " ".workshop_print_submission_assessments($workshop, $submission, "student").")", $datesubmitted, 
1451                 $action);
1452         }
1453         print_heading(get_string("studentsubmissions", "workshop", $course->student)." [$nsubmissions]", "center");
1454         print_table($table);
1455         workshop_print_key($workshop);
1456     }
1460 //////////////////////////////////////////////////////////////////////////////////////
1461 function workshop_list_teacher_assessments_by_user($workshop, $user) {
1462     global $CFG;
1463     
1464     $timenow = time();
1465     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
1466         error("Course Module ID was incorrect");
1467     }
1468     if (! $course = get_record("course", "id", $workshop->course)) {
1469         error("Course is misconfigured");
1470     }
1472     $table->head = array (get_string("title", "workshop"), get_string("action", "workshop"), get_string("comment", "workshop"));
1473     $table->align = array ("left", "left", "left");
1474     $table->size = array ("*", "*", "*");
1475     $table->cellpadding = 2;
1476     $table->cellspacing = 0;
1478     // get user's submissions
1479     if ($submissions = workshop_get_user_submissions($workshop, $user)) {
1480         foreach ($submissions as $submission) {
1481             // get the assessments
1482             if ($assessments = workshop_get_assessments($submission)) {
1483                 foreach ($assessments as $assessment) {
1484                     if (isteacher($workshop->course, $assessment->userid)) { // assessments by teachers only
1485                         $action = "<a href=\"assessments.php?action=viewassessment&amp;id=$cm->id&amp;aid=$assessment->id\">".
1486                             get_string("view", "workshop")."</a>";
1487                         // has teacher commented on teacher's assessment? shouldn't happen but leave test in
1488                         if ($assessment->timegraded and ($timenow - $assessment->timegraded > $CFG->maxeditingtime)) {
1489                             $comment = get_string("gradedbyteacher", "workshop", $course->teacher);
1490                             }
1491                         else {
1492                             $comment = userdate($assessment->timecreated);
1493                             }
1494                         $table->data[] = array(workshop_print_submission_title($workshop, $submission), $action, $comment);
1495                         }
1496                     }
1497                 }
1498             }
1499         }
1500     if (isset($table->data)) {
1501         print_table($table);
1502         }
1503     else {
1504         echo "<center>".get_string("noassessmentsdone", "workshop")."</center>\n";
1505         }
1506     }
1510 //////////////////////////////////////////////////////////////////////////////////////
1511 function workshop_list_teacher_submissions($workshop, $user) {
1512     global $CFG;
1514     // set threshold on re-assessments
1515     $reassessthreshold = 80;
1516     
1517     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
1518         error("Course Module ID was incorrect");
1519     }
1520     if (! $course = get_record("course", "id", $workshop->course)) {
1521         error("Course is misconfigured");
1522     }
1523     $table->head = array (get_string("title", "workshop"), get_string("action", "workshop"), get_string("comment", "workshop"));
1524     $table->align = array ("left", "left", "left");
1525     $table->size = array ("*", "*", "*");
1526     $table->cellpadding = 2;
1527     $table->cellspacing = 0;
1529     // get the number of assessments this user has done
1530     $nassessed = count_records_select("workshop_assessments", "workshopid = $workshop->id
1531                     AND userid = $user->id");
1532     if ($nassessed < $workshop->ntassessments) { 
1533         // if user has not assessed enough, set up "future" assessment records for this user for the teacher submissions...
1534         // ... first count the number of assessments for each teacher submission...
1535         if ($submissions = workshop_get_teacher_submissions($workshop)) {
1536             srand ((float)microtime()*1000000); // initialise random number generator
1537             foreach ($submissions as $submission) {
1538                 $n = count_records("workshop_assessments", "submissionid", $submission->id);
1539                 // ...OK to have zero, we add a small random number to randomise things...
1540                 $nassessments[$submission->id] = $n + rand(0, 99) / 100;
1541             }
1542             // ...put the submissions with the lowest number of assessments first...
1543             asort($nassessments);
1544             reset($nassessments);
1545             foreach ($nassessments as $submissionid => $n) { // break out of loop when we allocated enough assessments...
1546                 $submission = get_record("workshop_submissions", "id", $submissionid);
1547                 // ... provided the user has NOT already assessed that submission...
1548                 if (!get_record("workshop_assessments", "submissionid", $submission->id, "userid",
1549                                     $user->id)) {
1550                     $yearfromnow = time() + 365 * 86400;
1551                     // ...create one and set timecreated way in the future, this is reset when record is updated
1552                     unset($assessment); // clear previous version of object (if any)
1553                     $assessment->workshopid = $workshop->id;
1554                     $assessment->submissionid = $submission->id;
1555                     $assessment->userid = $user->id;
1556                     $assessment->grade = -1; // set impossible grade
1557                     $assessment->timecreated = $yearfromnow;
1558                     if (!$assessment->id = insert_record("workshop_assessments", $assessment)) {
1559                         error("Could not insert workshop assessment!");
1560                     }
1561                     $nassessed++;
1562                     if ($nassessed >= $workshop->ntassessments) {
1563                         break;
1564                     }
1565                 }
1566             }
1567         }
1568     }
1569     // now list user's assessments (but only list those which come from teacher submissions)
1570     if ($assessments = workshop_get_user_assessments($workshop, $user)) {
1571         $timenow = time();
1572         foreach ($assessments as $assessment) {
1573             if (!$submission = get_record("workshop_submissions", "id", $assessment->submissionid)) {
1574                 error ("workshop_list_teacher_submissions: unable to get submission");
1575             }
1576             // submission from a teacher?
1577             if (isteacher($workshop->course, $submission->userid)) {
1578                 $comment = '';
1579                 // user assessment has two states: record created but not assessed (date created in the future); 
1580                 // assessed but always available for re-assessment 
1581                 if ($assessment->timecreated > $timenow) { // user needs to assess this submission
1582                     $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1583                         get_string("assess", "workshop")."</a>";
1584                 }
1585                 elseif ($assessment->timegraded and ($assessment->gradinggrade < $reassessthreshold)) { 
1586                     // allow student to improve on their assessment once it's been graded and is below threshold
1587                     $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1588                         get_string("reassess", "workshop")."</a>";
1589                 } else {
1590                     // allow student  just to see their assessment if it hasn't been graded (or above threshold)
1591                     $action = "<a href=\"assessments.php?action=viewassessment&amp;id=$cm->id&amp;aid=$assessment->id\">".
1592                         get_string("view", "workshop")."</a>";
1593                 }
1594                 // see if the assessment is graded
1595                 if ($assessment->timegraded) {
1596                     // show grading grade (supressed if workshop not graded)
1597                     if ($workshop->gradingstrategy) {
1598                         $comment = get_string("thegradeforthisassessmentis", "workshop", 
1599                             number_format($assessment->gradinggrade * $workshop->gradinggrade / 100, 1))." / ".
1600                             $workshop->gradinggrade;
1601                     }
1602                 } elseif ($assessment->timecreated < $timenow) {
1603                     $comment = get_string("awaitinggradingbyteacher", "workshop", $course->teacher);
1604                 }
1605                 $table->data[] = array(workshop_print_submission_title($workshop, $submission), $action, $comment);
1606             }
1607         }
1608     }
1609     print_table($table);
1613 //////////////////////////////////////////////////////////////////////////////////////
1614 function workshop_list_unassessed_student_submissions($workshop, $user) {
1615     // list the student submissions not assessed by this user
1616     global $CFG;
1617     
1618     $timenow = time();
1619     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
1620         error("Course Module ID was incorrect");
1621     }
1622     if (! $course = get_record("course", "id", $workshop->course)) {
1623         error("Course is misconfigured");
1624         }
1626     if (groupmode($course, $cm) == SEPARATEGROUPS) {
1627         $groupid = get_current_group($course->id);
1628     } else {
1629         $groupid = 0;
1630     }
1632     $table->head = array (get_string("title", "workshop"), get_string("submittedby", "workshop"),
1633         get_string("submitted", "workshop"), get_string("action", "workshop"), get_string("comment", "workshop"));
1634     $table->align = array ("left", "left", "left", "left", "left");
1635     $table->size = array ("*", "*", "*", "*", "*");
1636     $table->cellpadding = 2;
1637     $table->cellspacing = 0;
1639     if ($submissions = workshop_get_student_submissions($workshop, 'time')) { // oldest first 
1640         foreach ($submissions as $submission) {
1641             // check group membership, if necessary
1642             if ($groupid) {
1643                 // check user's group
1644                 if (!ismember($groupid, $submission->userid)) {
1645                     continue; // skip this user
1646                 }
1647             }
1648             // see if submission is cold
1649             if (($submission->timecreated +$CFG->maxeditingtime) > $timenow) {
1650                 continue; // skip this submission
1651             }
1652             $comment = "";
1653             $timegap = get_string("ago", "workshop", format_time($submission->timecreated - $timenow));
1654             // see if user already graded this assessment
1655             if ($assessment = get_record_select("workshop_assessments", "submissionid = $submission->id
1656                     AND userid = $user->id")) {
1657                 if (($timenow - $assessment->timecreated < $CFG->maxeditingtime)) {
1658                     // last chance salon
1659                     $submissionowner = get_record("user", "id", $submission->userid);
1660                     $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1661                         get_string("edit", "workshop")."</a>";
1662                     $table->data[] = array(workshop_print_submission_title($workshop, $submission), 
1663                         fullname($submissionowner), $timegap, $action, $comment);
1664                     }
1665                 }
1666             else { 
1667                 // no assessment by this user, if no other teacher has assessed submission then list it
1668                 if (!workshop_count_teacher_assessments($course->id, $submission)) {
1669                     $submissionowner = get_record("user", "id", $submission->userid);
1670                     $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1671                         get_string("assess", "workshop")."</a>";
1672                     $table->data[] = array(workshop_print_submission_title($workshop, $submission), 
1673                         fullname($submissionowner), $timegap, $action, $comment);
1674                 }
1675                 }
1676             }
1677         if (isset($table->data)) {
1678             print_table($table);
1679             }
1680         }
1681     }
1684 //////////////////////////////////////////////////////////////////////////////////////
1685 function workshop_list_unassessed_teacher_submissions($workshop, $user) {
1686     // list the teacher submissions not assessed by this user
1687     global $CFG;
1688     
1689     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
1690         error("Course Module ID was incorrect");
1691     }
1693     $table->head = array (get_string("title", "workshop"), get_string("action", "workshop"), 
1694             get_string("comment", "workshop"));
1695     $table->align = array ("left", "left", "left");
1696     $table->size = array ("*", "*", "*");
1697     $table->cellpadding = 2;
1698     $table->cellspacing = 0;
1700     if ($submissions = workshop_get_teacher_submissions($workshop)) {
1701         foreach ($submissions as $submission) {
1702             $comment = "";
1703             // see if user already graded this assessment
1704             if ($assessment = get_record_select("workshop_assessments", "submissionid = $submission->id
1705                     AND userid = $user->id")) {
1706                 $timenow = time();
1707                 if (($timenow - $assessment->timecreated < $CFG->maxeditingtime)) {
1708                     // last chance salon
1709                     $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1710                         get_string("edit", "workshop")."</a>";
1711                     $table->data[] = array(workshop_print_submission_title($workshop, $submission), $action, $comment);
1712                     }
1713                 }
1714             else { // no assessment
1715                 $action = "<a href=\"assessments.php?action=assesssubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1716                     get_string("assess", "workshop")."</a>";
1717                 $table->data[] = array(workshop_print_submission_title($workshop, $submission), $action, $comment);
1718                 }
1719             }
1720         if (isset($table->data)) {
1721             print_table($table);
1722             }
1723         }
1724     }
1727 //////////////////////////////////////////////////////////////////////////////////////
1728 function workshop_list_ungraded_assessments($workshop, $stype) {
1729     // lists all the assessments of student submissions for grading by teacher
1730     global $CFG;
1731     
1732     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
1733         error("Course Module ID was incorrect");
1734     }
1736     $table->head = array (get_string("title", "workshop"), get_string("submittedby", "workshop"),
1737     get_string("assessor", "workshop"), get_string("timeassessed", "workshop"), get_string("action", "workshop"));
1738     $table->align = array ("left", "left", "left", "left");
1739     $table->size = array ("*", "*", "*", "*");
1740     $table->cellpadding = 2;
1741     $table->cellspacing = 0;
1742     $timenow = time();
1743     
1744     switch ($stype) {
1745         case "student" :
1746             $assessments = workshop_get_ungraded_assessments_student($workshop);
1747             break;
1748         case "teacher" :
1749             $assessments = workshop_get_ungraded_assessments_teacher($workshop);
1750             break;
1751         }
1752     if ($assessments) {
1753         foreach ($assessments as $assessment) {
1754             if (!isteacher($workshop->course, $assessment->userid)) { // don't let teacher grade their own assessments
1755                 if (($timenow - $assessment->timegraded) < $CFG->maxeditingtime) {
1756                     $action = "<a href=\"assessments.php?action=gradeassessment&amp;id=$cm->id&amp;stype=$stype&amp;aid=$assessment->id\">".
1757                         get_string("edit", "workshop")."</a>";
1758                     }
1759                 else {
1760                     $action = "<a href=\"assessments.php?action=gradeassessment&amp;id=$cm->id&amp;stype=$stype&amp;aid=$assessment->id\">".
1761                         get_string("grade", "workshop")."</a>";
1762                     }
1763                 $submission = get_record("workshop_submissions", "id", $assessment->submissionid);
1764                 $submissionowner = get_record("user", "id", $submission->userid);
1765                 $assessor = get_record("user", "id", $assessment->userid);
1766                 $table->data[] = array(workshop_print_submission_title($workshop, $submission), 
1767                     fullname($submissionowner), fullname($assessor), userdate($assessment->timecreated), $action);
1768                 }
1769             }
1770         if (isset($table->data)) {
1771             print_table($table);
1772             }
1773         }
1774     }
1775     
1777 //////////////////////////////////////////////////////////////////////////////////////
1778 function workshop_list_user_submissions($workshop, $user) {
1779     global $CFG;
1781     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
1782         error("Course Module ID was incorrect");
1783     }
1785     $timenow = time();
1786     $table->head = array (get_string("title", "workshop"),  get_string("action", "workshop"),
1787         get_string("submitted", "assignment"),  get_string("assessments", "workshop"));
1788     $table->align = array ("left", "left", "left", "left");
1789     $table->size = array ("*", "*", "*", "*");
1790     $table->cellpadding = 2;
1791     $table->cellspacing = 0;
1793     if ($submissions = workshop_get_user_submissions($workshop, $user)) {
1794         foreach ($submissions as $submission) {
1795             // allow user to edit or delete a submission if it's warm OR if assessment period has not started
1796             if (($submission->timecreated > ($timenow - $CFG->maxeditingtime)) or ($workshop->assessmentstart > time())) {
1797                 $action = "<a href=\"submissions.php?action=editsubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
1798                     get_string("edit", "workshop")."</a> | ".
1799                     "<a href=\"submissions.php?action=userconfirmdelete&amp;id=$cm->id&amp;sid=$submission->id\">".
1800                     get_string("delete", "workshop")."</a>";
1801             }
1802             else {
1803                 $action = '';
1804             }
1805             $datesubmitted = userdate($submission->timecreated);
1806             if ($submission->late) {
1807                 $datesubmitted = "<font color=\"red\">".$datesubmitted."</font>";
1808             }
1809             $n = count_records_select("workshop_assessments", "submissionid = $submission->id AND
1810                     timecreated < ($timenow - $CFG->maxeditingtime)");
1811             $table->data[] = array(workshop_print_submission_title($workshop, $submission), $action,
1812                 $datesubmitted, $n);
1813         }
1814         print_table($table);
1815     }
1820 ///////////////////////////////////////////////////////////////////////////////
1821 function workshop_phase($workshop, $style='') {
1822     $time = time();
1823     if ($time < $workshop->submissionstart) {
1824         return get_string('phase1'.$style, 'workshop');
1825     } 
1826     else if ($time < $workshop->submissionend) {
1827         if ($time < $workshop->assessmentstart) {
1828             return get_string('phase2'.$style, 'workshop');
1829         } else {
1830             return get_string('phase3'.$style, 'workshop');
1831         }
1832     } 
1833     else if ($time < $workshop->assessmentstart) {
1834         return  get_string('phase0'.$style, 'workshop');
1835     }
1836     else if ($time < $workshop->assessmentend) {
1837         return  get_string('phase4'.$style, 'workshop');
1838     }
1839     else {
1840         return  get_string('phase5'.$style, 'workshop');
1841     }    
1842     error('Something is wrong with the workshop dates');
1846 //////////////////////////////////////////////////////////////////////////////////////
1847 function workshop_print_assessment($workshop, $assessment = false, $allowchanges = false, 
1848     $showcommentlinks = false, $returnto = '') {
1849     // $allowchanges added 14/7/03. The form is inactive unless allowchanges = true
1850     // $returnto added 28/8/03. The page to go to after the assessment has been submitted
1851     global $CFG, $THEME, $USER, $WORKSHOP_SCALES, $WORKSHOP_EWEIGHTS;
1852     
1853     if (! $course = get_record("course", "id", $workshop->course)) {
1854         error("Course is misconfigured");
1855     }
1856     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $course->id)) {
1857         error("Course Module ID was incorrect");
1858     }
1859     
1860     if (!$submission = get_record("workshop_submissions", "id", $assessment->submissionid)) {
1861         error ("Workshop_print_assessment: Submission record not found");
1862     }
1863     
1864     print_heading(get_string('assessmentof', 'workshop', 
1865         "<a href=\"submissions.php?id=$cm->id&action=showsubmission&sid=$submission->id\" target=\"submission\">".
1866         $submission->title.'</a>'));
1867     
1868     $timenow = time();
1870     // reset the internal flags
1871     if ($assessment) {
1872         $showgrades = false;
1873     }
1874     else { // if no assessment, i.e. specimen grade form always show grading scales
1875         $showgrades = true;
1876     }
1877     
1878     if ($assessment) {
1879         // set the internal flag if necessary
1880         if ($allowchanges or !$workshop->agreeassessments or !$workshop->hidegrades or 
1881                 $assessment->timeagreed) {
1882             $showgrades = true;
1883         }
1884         
1885         echo "<center>\n";
1886     
1887         // see if this is a pre-filled assessment for a re-submission...
1888         if ($assessment->resubmission) {
1889             // ...and print an explaination
1890             print_heading(get_string("assessmentofresubmission", "workshop"));
1891         }
1892         
1893         // print agreement time if the workshop requires peer agreement
1894         if ($workshop->agreeassessments and $assessment->timeagreed) {
1895             echo "<p>".get_string("assessmentwasagreedon", "workshop", userdate($assessment->timeagreed));
1896         }
1898         // first print any comments on this assessment
1899         if ($comments = workshop_get_comments($assessment)) {
1900             echo "<table cellpadding=\"2\" border=\"1\">\n";
1901             $firstcomment = TRUE;
1902             foreach ($comments as $comment) {
1903                 echo "<tr valign=\"top\"><td bgcolor=\"$THEME->cellheading2\"><p><b>".
1904                     get_string("commentby","workshop")." ";
1905                 if (isteacher($workshop->course, $comment->userid)) {
1906                     echo $course->teacher;
1907                 }
1908                 elseif ($assessment->userid == $comment->userid) {
1909                     print_string("assessor", "workshop");
1910                 }
1911                 else {
1912                     print_string("authorofsubmission", "workshop");
1913                 }
1914                 echo " ".get_string("on", "workshop", userdate($comment->timecreated))."</b></p></td></tr><tr><td>\n";
1915                 echo format_text($comment->comments)."&nbsp;\n";
1916                 // add the links if needed
1917                 if ($firstcomment and $showcommentlinks and !$assessment->timeagreed) {
1918                     // show links depending on who doing the viewing
1919                     $firstcomment = FALSE;
1920                     if (isteacher($workshop->course, $USER->id) and ($comment->userid != $USER->id)) {
1921                         echo "<p align=\"right\"><a href=\"assessments.php?action=addcomment&amp;id=$cm->id&amp;aid=$assessment->id\">".
1922                             get_string("reply", "workshop")."</a>\n";
1923                     }
1924                     elseif (($comment->userid ==$USER->id) and (($timenow - $comment->timecreated) < $CFG->maxeditingtime)) {
1925                         echo "<p align=\"right\"><a href=\"assessments.php?action=editcomment&amp;id=$cm->id&amp;cid=$comment->id\">".
1926                             get_string("edit", "workshop")."</a>\n";
1927                         if ($USER->id == $submission->userid) {
1928                             echo " | <a href=\"assessments.php?action=agreeassessment&amp;id=$cm->id&amp;aid=$assessment->id\">".
1929                                 get_string("agreetothisassessment", "workshop")."</a>\n";
1930                         }
1931                     }
1932                     elseif (($comment->userid != $USER->id) and (($USER->id == $assessment->userid) or 
1933                         ($USER->id == $submission->userid))) {
1934                         echo "<p align=\"right\"><a href=\"assessments.php?action=addcomment&amp;id=$cm->id&amp;aid=$assessment->id\">".
1935                             get_string("reply", "workshop")."</a>\n";
1936                         if ($USER->id == $submission->userid) {
1937                             echo " | <a href=\"assessments.php?action=agreeassessment&amp;id=$cm->id&amp;aid=$assessment->id\">".
1938                                 get_string("agreetothisassessment", "workshop")."</a>\n";
1939                         }
1940                     }
1941                 }
1942                 echo "</td></tr>\n";
1943             }
1944             echo "</table>\n";
1945         }
1946     }
1947         
1948     // now print the grading form with the grading grade if any
1949     // FORM is needed for Mozilla browsers, else radio bttons are not checked
1950         ?>
1951     <form name="assessmentform" method="post" action="assessments.php">
1952     <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
1953     <input type="hidden" name="aid" value="<?php echo $assessment->id ?>" />
1954     <input type="hidden" name="action" value="updateassessment" />
1955     <input type="hidden" name="returnto" value="<?php echo $returnto ?>" />
1956     <input type="hidden" name="elementno" value="" />
1957     <input type="hidden" name="stockcommentid" value="" />
1958     <center>
1959     <table cellpadding="2" border="1">
1960     <?php
1961     echo "<tr valign=\"top\">\n";
1962     echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\"><center><b>";
1963     if ($assessment and isteacher($course->id)) {
1964         $user = get_record('user', 'id', $assessment->userid);
1965         print_string("assessmentby", "workshop", fullname($user));
1966     } else {
1967         print_string('assessment', 'workshop');
1968     }
1969     echo "</b></center></td>\n";
1970     echo "</tr>\n";
1971     
1972     // only show the grade if grading strategy > 0 and the grade is positive
1973     if ($showgrades and $workshop->gradingstrategy and $assessment->grade >= 0) { 
1974         echo "<tr valign=\"top\">\n
1975             <td colspan=\"2\" align=\"center\">
1976             <b>".get_string("thegradeis", "workshop").": ".
1977             number_format($assessment->grade * $workshop->grade / 100, 2)." (".
1978             get_string("maximumgrade")." ".number_format($workshop->grade, 0).")</b>
1979             </td></tr><tr><td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td></tr>\n";
1980     }
1981     
1982     // get the assignment elements...
1983     $elementsraw = get_records("workshop_elements", "workshopid", $workshop->id, "elementno ASC");
1984     if (count($elementsraw) < $workshop->nelements) {
1985         print_string("noteonassignmentelements", "workshop");
1986     }
1987     if ($elementsraw) {
1988         foreach ($elementsraw as $element) {
1989             $elements[] = $element;   // to renumber index 0,1,2...
1990         }
1991     } else {
1992         $elements = null;
1993     }
1995     if ($assessment) {
1996         // get any previous grades...
1997         if ($gradesraw = get_records_select("workshop_grades", "assessmentid = $assessment->id", "elementno")) {
1998             foreach ($gradesraw as $grade) {
1999                 $grades[] = $grade;   // to renumber index 0,1,2...
2000                 }
2001             }
2002         }
2003     else {
2004         // setup dummy grades array
2005         for($i = 0; $i < count($elementsraw); $i++) { // gives a suitable sized loop
2006             $grades[$i]->feedback = get_string("yourfeedbackgoeshere", "workshop");
2007             $grades[$i]->grade = 0;
2008             }
2009         }
2010                 
2011     // determine what sort of grading
2012     switch ($workshop->gradingstrategy) {
2013         case 0:  // no grading
2014             // now print the form
2015             for ($i=0; $i < count($elements); $i++) {
2016                 $iplus1 = $i+1;
2017                 echo "<tr valign=\"top\">\n";
2018                 echo "  <td align=\"right\"><p><b>". get_string("element","workshop")." $iplus1:</b></p></td>\n";
2019                 echo "  <td>".format_text($elements[$i]->description);
2020                 echo "</td></tr>\n";
2021                 echo "<tr valign=\"top\">\n";
2022                 echo "  <td align=\"right\"><p><b>". get_string("feedback").":</b></p></td>\n";
2023                 echo "  <td>\n";
2024                 if ($allowchanges) {
2025                     echo "      <textarea name=\"feedback_$i\" rows=\"3\" cols=\"75\" wrap=\"virtual\">\n";
2026                     if (isset($grades[$i]->feedback)) {
2027                         echo $grades[$i]->feedback;
2028                         }
2029                     echo "</textarea>\n";
2030                     }
2031                 else {
2032                     echo format_text($grades[$i]->feedback);
2033                     }
2034                 echo "  </td>\n";
2035                 echo "</tr>\n";
2037                 // if active and the teacher show stock comments...
2038                 if ($allowchanges and isteacher($course->id, $USER->id)) {
2039                     echo "<tr><td valign=\"top\" align=\"right\"><input type=\"button\" value=\"".
2040                         get_string("addcomment", "workshop")."\" 
2041                         onclick=\"document.assessmentform.action.value='addstockcomment';
2042                         document.assessmentform.elementno.value=$i;document.assessmentform.submit();\" /> \n";
2043                     helpbutton("addcommenttobank", get_string("addcomment", "workshop"), "workshop");
2044                     echo "</td><td>\n";
2045                     if ($stockcomments = get_records_select("workshop_stockcomments", "workshopid = $workshop->id
2046                             AND elementno = $i", "id")) { // show comments in fixed order (oldest first)
2047                         foreach ($stockcomments as $stockcomment) {
2048                             echo "<a onclick=\"document.assessmentform.feedback_$i.value+=' '+'".
2049                                 addslashes($stockcomment->comments)."';\">&lt;&lt;$stockcomment->comments&gt;&gt;</a>\n";
2050                             if (isteacheredit($course->id, $USER->id)) {
2051                                 echo " <a onclick=\"document.assessmentform.action.value='removestockcomment';document.assessmentform.stockcommentid.value=$stockcomment->id;document.assessmentform.submit();\"> <small><i>&lt;--".get_string("delete","workshop")."</i></small></a>\n";
2052                             }
2053                             echo "<br />\n";
2054                         }
2055                     } 
2056                     echo "</td></tr>\n";
2057                 }
2059                 echo "<tr valign=\"top\">\n";
2060                 echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
2061                 echo "</tr>\n";
2062                 }
2063             break;
2064             
2065         case 1: // accumulative grading
2066             // now print the form
2067             for ($i=0; $i < count($elements); $i++) {
2068                 $iplus1 = $i+1;
2069                 echo "<tr valign=\"top\">\n";
2070                 echo "  <td align=\"right\"><p><b>". get_string("element","workshop")." $iplus1:</b></p></td>\n";
2071                 echo "  <td>".format_text($elements[$i]->description);
2072                 echo "<p align=\"right\"><font size=\"1\">".get_string("weight", "workshop").": ".
2073                     number_format($WORKSHOP_EWEIGHTS[$elements[$i]->weight], 2)."</font>\n";
2074                 echo "</td></tr>\n";
2075                 if ($showgrades) {
2076                     echo "<tr valign=\"top\">\n";
2077                     echo "  <td align=\"right\"><p><b>". get_string("grade"). ":</b></p></td>\n";
2078                     echo "  <td valign=\"top\">\n";
2079                     
2080                     // get the appropriate scale
2081                     $scalenumber=$elements[$i]->scale;
2082                     $SCALE = (object)$WORKSHOP_SCALES[$scalenumber];
2083                     switch ($SCALE->type) {
2084                         case 'radio' :
2085                                 // show selections highest first
2086                                 echo "<center><b>$SCALE->start</b>&nbsp;&nbsp;&nbsp;";
2087                                 for ($j = $SCALE->size - 1; $j >= 0 ; $j--) {
2088                                     $checked = false;
2089                                     if (isset($grades[$i]->grade)) { 
2090                                         if ($j == $grades[$i]->grade) {
2091                                             $checked = true;
2092                                             }
2093                                         }
2094                                     else { // there's no previous grade so check the lowest option
2095                                         if ($j == 0) {
2096                                             $checked = true;
2097                                             }
2098                                         }
2099                                     if ($checked) {
2100                                         echo " <input type=\"RADIO\" name=\"grade[$i]\" value=\"$j\" checked=\"checked\" /> &nbsp;&nbsp;&nbsp;\n";
2101                                         }
2102                                     else {
2103                                         echo " <input type=\"RADIO\" name=\"grade[$i]\" value=\"$j\" /> &nbsp;&nbsp;&nbsp;\n";
2104                                         }
2105                                     }
2106                                 echo "&nbsp;&nbsp;&nbsp;<b>$SCALE->end</b></center>\n";
2107                                 break;
2108                         case 'selection' :  
2109                                 unset($numbers);
2110                                 for ($j = $SCALE->size; $j >= 0; $j--) {
2111                                     $numbers[$j] = $j;
2112                                     }
2113                                 if (isset($grades[$i]->grade)) {
2114                                     choose_from_menu($numbers, "grade[$i]", $grades[$i]->grade, "");
2115                                     }
2116                                 else {
2117                                     choose_from_menu($numbers, "grade[$i]", 0, "");
2118                                     }
2119                                 break;
2120                         }
2121             
2122                     echo "  </td>\n";
2123                     echo "</tr>\n";
2124                     }
2125                 echo "<tr valign=\"top\">\n";
2126                 echo "  <td align=\"right\"><p><b>". get_string("feedback").":</b></p></td>\n";
2127                 echo "  <td>\n";
2128                 if ($allowchanges) {
2129                     echo "      <textarea name=\"feedback_$i\" rows=\"3\" cols=\"75\" wrap=\"virtual\">\n";
2130                     if (isset($grades[$i]->feedback)) {
2131                         echo $grades[$i]->feedback;
2132                         }
2133                     echo "</textarea>\n";
2134                     }
2135                 else {
2136                     echo format_text($grades[$i]->feedback);
2137                     }
2138                 echo "  </td>\n";
2139                 echo "</tr>\n";
2140                 
2141                 // if active and the teacher show stock comments...
2142                 if ($allowchanges and isteacher($course->id, $USER->id)) {
2143                     echo "<tr><td valign=\"top\" align=\"right\"><input type=\"button\" value=\"".
2144                         get_string("addcomment", "workshop")."\" 
2145                         onclick=\"document.assessmentform.action.value='addstockcomment';
2146                         document.assessmentform.elementno.value=$i;document.assessmentform.submit();\" /> \n";
2147                     helpbutton("addcommenttobank", get_string("addcomment", "workshop"), "workshop");
2148                     echo "</td><td>\n";
2149                     if ($stockcomments = get_records_select("workshop_stockcomments", "workshopid = $workshop->id
2150                             AND elementno = $i", "id")) { // get comments in a fixed order - oldest first
2151                         foreach ($stockcomments as $stockcomment) {
2152                             echo "<a onclick=\"document.assessmentform.feedback_$i.value+=' '+'".
2153                                 addslashes($stockcomment->comments).
2154                                 "';\">&lt;&lt;$stockcomment->comments&gt;&gt;</a>\n";
2155                             if (isteacheredit($course->id, $USER->id)) {
2156                                 echo " <a onclick=\"document.assessmentform.action.value='removestockcomment';document.assessmentform.stockcommentid.value=$stockcomment->id;document.assessmentform.submit();\"> <small><i>&lt;--".get_string("delete","workshop")."</i></small></a>\n";
2157                             }
2158                             echo "<br />\n";
2159                         }
2160                     } 
2161                     echo "</td></tr>\n";
2162                 }
2164                 echo "<tr valign=\"top\">\n";
2165                 echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
2166                 echo "</tr>\n";
2167                 }
2168             break;
2169             
2170         case 2: // error banded grading
2171             // now run through the elements
2172             $negativecount = 0;
2173             for ($i=0; $i < count($elements) - 1; $i++) {
2174                 $iplus1 = $i+1;
2175                 echo "<tr valign=\"top\">\n";
2176                 echo "  <td align=\"right\"><p><b>". get_string("element","workshop")." $iplus1:</b></p></td>\n";
2177                 echo "  <td>".format_text($elements[$i]->description);
2178                 echo "<p align=\"right\"><font size=\"1\">".get_string("weight", "workshop").": ".
2179                     number_format($WORKSHOP_EWEIGHTS[$elements[$i]->weight], 2)."</font>\n";
2180                 echo "</td></tr>\n";
2181                 echo "<tr valign=\"top\">\n";
2182                 echo "  <td align=\"right\"><p><b>". get_string("grade"). ":</b></p></td>\n";
2183                 echo "  <td valign=\"top\">\n";
2184                     
2185                 // get the appropriate scale - yes/no scale (0)
2186                 $SCALE = (object) $WORKSHOP_SCALES[0];
2187                 switch ($SCALE->type) {
2188                     case 'radio' :
2189                             // show selections highest first
2190                             echo "<center><b>$SCALE->start</b>&nbsp;&nbsp;&nbsp;";
2191                             for ($j = $SCALE->size - 1; $j >= 0 ; $j--) {
2192                                 $checked = false;
2193                                 if (isset($grades[$i]->grade)) { 
2194                                     if ($j == $grades[$i]->grade) {
2195                                         $checked = true;
2196                                         }
2197                                     }
2198                                 else { // there's no previous grade so check the lowest option
2199                                     if ($j == 0) {
2200                                         $checked = true;
2201                                         }
2202                                     }
2203                                 if ($checked) {
2204                                     echo " <input type=\"RADIO\" name=\"grade[$i]\" value=\"$j\" checked=\"checked\" /> &nbsp;&nbsp;&nbsp;\n";
2205                                     }
2206                                 else {
2207                                     echo " <input type=\"RADIO\" name=\"grade[$i]\" value=\"$j\" /> &nbsp;&nbsp;&nbsp;\n";
2208                                     }
2209                                 }
2210                             echo "&nbsp;&nbsp;&nbsp;<b>$SCALE->end</b></center>\n";
2211                             break;
2212                     case 'selection' :  
2213                             unset($numbers);
2214                             for ($j = $SCALE->size; $j >= 0; $j--) {
2215                                 $numbers[$j] = $j;
2216                                 }
2217                             if (isset($grades[$i]->grade)) {
2218                                 choose_from_menu($numbers, "grade[$i]", $grades[$i]->grade, "");
2219                                 }
2220                             else {
2221                                 choose_from_menu($numbers, "grade[$i]", 0, "");
2222                                 }
2223                             break;
2224                     }
2225         
2226                 echo "  </td>\n";
2227                 echo "</tr>\n";
2228                 echo "<tr valign=\"top\">\n";
2229                 echo "  <td align=\"right\"><p><b>". get_string("feedback").":</b></p></td>\n";
2230                 echo "  <td>\n";
2231                 if ($allowchanges) {
2232                     echo "      <textarea name=\"feedback_$i\" rows=\"3\" cols=\"75\" wrap=\"virtual\">\n";
2233                     if (isset($grades[$i]->feedback)) {
2234                         echo $grades[$i]->feedback;
2235                         }
2236                     echo "</textarea>\n";
2237                     }
2238                 else {
2239                     if (isset($grades[$i]->feedback)) {
2240                         echo format_text($grades[$i]->feedback);
2241                         }
2242                     }
2243                 echo "&nbsp;</td>\n";
2244                 echo "</tr>\n";
2246                 // if active and the teacher show stock comments...
2247                 if ($allowchanges and isteacher($course->id, $USER->id)) {
2248                     echo "<tr><td valign=\"top\" align=\"right\"><input type=\"button\" value=\"".
2249                         get_string("addcomment", "workshop")."\" 
2250                         onclick=\"document.assessmentform.action.value='addstockcomment';
2251                         document.assessmentform.elementno.value=$i;document.assessmentform.submit();\" /> \n";
2252                     helpbutton("addcommenttobank", get_string("addcomment", "workshop"), "workshop");
2253                     echo "</td><td>\n";
2254                     if ($stockcomments = get_records_select("workshop_stockcomments", "workshopid = $workshop->id
2255                             AND elementno = $i", "id")) { // get comments in a fixed order - oldest first
2256                         foreach ($stockcomments as $stockcomment) {
2257                             echo "<a onclick=\"document.assessmentform.feedback_$i.value+=' '+'".
2258                                 addslashes($stockcomment->comments).
2259                                 "';\">&lt;&lt;$stockcomment->comments&gt;&gt;</a>\n";
2260                             if (isteacheredit($course->id, $USER->id)) {
2261                                 echo " <a onclick=\"document.assessmentform.action.value='removestockcomment';document.assessmentform.stockcommentid.value=$stockcomment->id;document.assessmentform.submit();\"> <small><i>&lt;--".get_string("delete","workshop")."</i></small></a>\n";
2262                             }
2263                             echo "<br />\n";
2264                         }
2265                     } 
2266                     echo "</td></tr>\n";
2267                 }
2268                 echo "<tr valign=\"top\">\n";
2269                 echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
2270                 echo "</tr>\n";
2271                 if (empty($grades[$i]->grade)) {
2272                     $negativecount++;
2273                     }
2274                 }
2275             // print the number of negative elements
2276             // echo "<tr><td>".get_string("numberofnegativeitems", "workshop")."</td><td>$negativecount</td></tr>\n";
2277             // echo "<tr valign=\"top\">\n";
2278             // echo "   <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
2279             echo "</table></center>\n";
2280             // now print the grade table
2281             echo "<p><center><b>".get_string("gradetable","workshop")."</b></center>\n";
2282             echo "<center><table cellpadding=\"5\" border=\"1\"><tr><td align=\"CENTER\">".
2283                 get_string("numberofnegativeresponses", "workshop");
2284             echo "</td><td>". get_string("suggestedgrade", "workshop")."</td></tr>\n";
2285             for ($j = 100; $j >= 0; $j--) {
2286                 $numbers[$j] = $j;
2287                 }
2288             for ($i=0; $i<=$workshop->nelements; $i++) {
2289                 if ($i == $negativecount) {
2290                     echo "<tr><td align=\"CENTER\"><img src=\"$CFG->pixpath/t/right.gif\" alt=\"\" /> $i</td><td align=\"center\">{$elements[$i]->maxscore}</td></tr>\n";
2291                     }
2292                 else {
2293                     echo "<tr><td align=\"CENTER\">$i</td><td align=\"CENTER\">{$elements[$i]->maxscore}</td></tr>\n";
2294                     }
2295                 }
2296             echo "</table></center>\n";
2297             echo "<p><center><table cellpadding=\"5\" border=\"1\"><tr><td><b>".get_string("optionaladjustment", 
2298                     "workshop")."</b></td><td>\n";
2299             unset($numbers);
2300             for ($j = 20; $j >= -20; $j--) {
2301                 $numbers[$j] = $j;
2302                 }
2303             if (isset($grades[$workshop->nelements]->grade)) {
2304                 choose_from_menu($numbers, "grade[$workshop->nelements]", $grades[$workshop->nelements]->grade, "");
2305                 }
2306             else {
2307                 choose_from_menu($numbers, "grade[$workshop->nelements]", 0, "");
2308                 }
2309             echo "</td></tr>\n";
2310             break;
2311             
2312         case 3: // criteria grading
2313             echo "<tr valign=\"top\">\n";
2314             echo "  <td bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
2315             echo "  <td bgcolor=\"$THEME->cellheading2\"><b>". get_string("criterion","workshop")."</b></td>\n";
2316             echo "  <td bgcolor=\"$THEME->cellheading2\"><b>".get_string("select", "workshop")."</b></td>\n";
2317             echo "  <td bgcolor=\"$THEME->cellheading2\"><b>".get_string("suggestedgrade", "workshop")."</b></td>\n";
2318             // find which criteria has been selected (saved in the zero element), if any
2319             if (isset($grades[0]->grade)) {
2320                 $selection = $grades[0]->grade;
2321                 }
2322             else {
2323                 $selection = 0;
2324                 }
2325             // now run through the elements
2326             for ($i=0; $i < count($elements); $i++) {
2327                 $iplus1 = $i+1;
2328                 echo "<tr valign=\"top\">\n";
2329                 echo "  <td>$iplus1</td><td>".format_text($elements[$i]->description)."</td>\n";
2330                 if ($selection == $i) {
2331                     echo "  <td align=\"center\"><input type=\"RADIO\" name=\"grade[0]\" value=\"$i\" checked=\"checked\" /></td>\n";
2332                     }
2333                 else {
2334                     echo "  <td align=\"center\"><input type=\"RADIO\" name=\"grade[0]\" value=\"$i\" /></td>\n";
2335                     }
2336                 echo "<td align=\"center\">{$elements[$i]->maxscore}</td></tr>\n";
2337                 }
2338             echo "</table></center>\n";
2339             echo "<p><center><table cellpadding=\"5\" border=\"1\"><tr><td><b>".get_string("optionaladjustment", 
2340                     "workshop")."</b></td><td>\n";
2341             unset($numbers);
2342             for ($j = 20; $j >= -20; $j--) {
2343                 $numbers[$j] = $j;
2344                 }
2345             if (isset($grades[1]->grade)) {
2346                 choose_from_menu($numbers, "grade[1]", $grades[1]->grade, "");
2347                 }
2348             else {
2349                 choose_from_menu($numbers, "grade[1]", 0, "");
2350                 }
2351             echo "</td></tr>\n";
2352             break;
2353             
2354         case 4: // rubric grading
2355             // now run through the elements...
2356             for ($i=0; $i < count($elements); $i++) {
2357                 $iplus1 = $i+1;
2358                 echo "<tr valign=\"top\">\n";
2359                 echo "<td align=\"right\"><b>".get_string("element", "workshop")." $iplus1:</b></td>\n";
2360                 echo "<td>".format_text($elements[$i]->description).
2361                      "<p align=\"right\"><font size=\"1\">".get_string("weight", "workshop").": ".
2362                     number_format($WORKSHOP_EWEIGHTS[$elements[$i]->weight], 2)."</font></td></tr>\n";
2363                 echo "<tr valign=\"top\">\n";
2364                 echo "  <td bgcolor=\"$THEME->cellheading2\" align=\"center\"><b>".get_string("select", "workshop").
2365                     "</b></td>\n";
2366                 echo "  <td bgcolor=\"$THEME->cellheading2\"><b>". get_string("criterion","workshop").
2367                     "</b></td></tr>\n";
2368                 if (isset($grades[$i])) {
2369                     $selection = $grades[$i]->grade;
2370                 } else {
2371                     $selection = 0;
2372                 }
2373                 // ...and the rubrics
2374                 if ($rubricsraw = get_records_select("workshop_rubrics", "workshopid = $workshop->id AND 
2375                         elementno = $i", "rubricno ASC")) {
2376                     unset($rubrics);
2377                     foreach ($rubricsraw as $rubic) {
2378                         $rubrics[] = $rubic;   // to renumber index 0,1,2...
2379                     }
2380                     for ($j=0; $j<5; $j++) {
2381                         if (empty($rubrics[$j]->description)) {
2382                             break; // out of inner for loop
2383                         }
2384                         echo "<tr valign=\"top\">\n";
2385                         if ($selection == $j) {
2386                             echo "  <td align=\"center\"><input type=\"RADIO\" name=\"grade[$i]\" value=\"$j\" 
2387                                 checked=\"checked\" /></td>\n";
2388                         } else {
2389                             echo "  <td align=\"center\"><input type=\"RADIO\" name=\"grade[$i]\" value=\"$j\" 
2390                                 /></td>\n";
2391                         }
2392                         echo "<td>".format_text($rubrics[$j]->description)."</td>\n";
2393                     }
2394                     echo "<tr valign=\"top\">\n";
2395                     echo "  <td align=\"right\"><p><b>". get_string("feedback").":</b></p></td>\n";
2396                     echo "  <td>\n";
2397                     if ($allowchanges) {
2398                         echo "      <textarea name=\"feedback_$i\" rows=\"3\" cols=\"75\" wrap=\"virtual\">\n";
2399                         if (isset($grades[$i]->feedback)) {
2400                             echo $grades[$i]->feedback;
2401                         }
2402                         echo "</textarea>\n";
2403                     } else {
2404                         echo format_text($grades[$i]->feedback);
2405                     }
2406                     echo "  </td>\n";
2407                     echo "</tr>\n";
2409                     // if active and the teacher show stock comments...
2410                     if ($allowchanges and isteacher($course->id, $USER->id)) {
2411                     echo "<tr><td valign=\"top\" align=\"right\"><input type=\"button\" value=\"".
2412                         get_string("addcomment", "workshop")."\" 
2413                         onclick=\"document.assessmentform.action.value='addstockcomment';
2414                         document.assessmentform.elementno.value=$i;document.assessmentform.submit();\" /> \n";
2415                     helpbutton("addcommenttobank", get_string("addcomment", "workshop"), "workshop");
2416                     echo "</td><td>\n";
2417                         if ($stockcomments = get_records_select("workshop_stockcomments", "workshopid = $workshop->id
2418                                     AND elementno = $i", "id")) { // show comments in fixed (creation) order
2419                             foreach ($stockcomments as $stockcomment) {
2420                                 echo "<a onclick=\"document.assessmentform.feedback_$i.value+=' '+'".
2421                                     addslashes($stockcomment->comments).
2422                                     "';\">&lt;&lt;$stockcomment->comments&gt;&gt;</a>\n";
2423                                 if (isteacheredit($course->id, $USER->id)) {
2424                                     echo " <a onclick=\"document.assessmentform.action.value='removestockcomment';document.assessmentform.stockcommentid.value=$stockcomment->id;document.assessmentform.submit();\"> <small><i>&lt;--".get_string("delete","workshop")."</i></small></a>\n";
2425                                 }
2426                                 echo "<br />\n";
2427                             }
2428                         } 
2429                         echo "</td></tr>\n";
2430                     }
2432                     echo "<tr valign=\"top\">\n";
2433                     echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
2434                     echo "</tr>\n";
2435                 }
2436             }
2437             break;
2438         } // end of outer switch
2439     
2440         // now get the general comment (present in all types)
2441         echo "<tr valign=\"top\">\n";
2442         switch ($workshop->gradingstrategy) {
2443             case 0:
2444             case 1:
2445             case 4 : // no grading, accumulative and rubic
2446                 echo "  <td align=\"right\"><p><b>". get_string("generalcomment", "workshop").":</b></p></td>\n";
2447                 break; 
2448             default : 
2449                 echo "  <td align=\"right\"><p><b>".get_string("generalcomment", "workshop")."/<br />".
2450                     get_string("reasonforadjustment", "workshop").":</b></p></td>\n";
2451         }
2452         echo "  <td>\n";
2453         if ($allowchanges) {
2454             echo "      <textarea name=\"generalcomment\" rows=\"5\" cols=\"75\" wrap=\"virtual\">\n";
2455             if (isset($assessment->generalcomment)) {
2456                 echo $assessment->generalcomment;
2457             }
2458             echo "</textarea>\n";
2459         } else {
2460         if ($assessment) {
2461             if (isset($assessment->generalcomment)) {
2462                 echo format_text($assessment->generalcomment);
2463             }
2464         } else {
2465             print_string("yourfeedbackgoeshere", "workshop");
2466         }
2467     }
2468     echo "&nbsp;</td>\n";
2469     echo "</tr>\n";
2470     // if active and the teacher show stock comments...
2471     if ($allowchanges and isteacher($course->id, $USER->id)) {
2472         echo "<tr><td valign=\"top\" align=\"right\"><input type=\"button\" value=\"".
2473             get_string("addcomment", "workshop")."\" 
2474             onclick=\"document.assessmentform.action.value='addstockcomment';
2475         document.assessmentform.elementno.value=99;document.assessmentform.submit();\" /> \n";
2476         helpbutton("addcommenttobank", get_string("addcomment", "workshop"), "workshop");
2477         echo "</td><td>\n";
2478         if ($stockcomments = get_records_select("workshop_stockcomments", "workshopid = $workshop->id
2479                     AND elementno = 99", "id")) { // show in the same order (oldest at the top)
2480             foreach ($stockcomments as $stockcomment) {
2481                 echo "<a onclick=\"document.assessmentform.generalcomment.value+=' '+'".
2482                     addslashes($stockcomment->comments)."';\">&lt;&lt;$stockcomment->comments&gt;&gt;</a>\n";
2483                 if (isteacheredit($course->id, $USER->id)) {
2484                     echo " <a onclick=\"document.assessmentform.action.value='removestockcomment';document.assessmentform.stockcommentid.value=$stockcomment->id;document.assessmentform.submit();\"> <small><i>&lt;--".get_string("delete","workshop")."</i></small></a>\n";
2485                 }
2486                 echo "<br />\n";
2487             }
2488         } 
2489         echo "</td></tr>\n";
2490     }
2491     
2492     $timenow = time();
2493     // now show the grading grade if available...
2494     if ($assessment->timegraded) {
2495         echo "<tr valign=\"top\">\n";
2496         echo "<td colspan=\"2\" bgcolor=\"$THEME->cellheading2\" align=\"center\"><b>".
2497             get_string('gradeforstudentsassessment', 'workshop')."</b></td>\n";
2498         echo "</tr>\n";
2499         
2500         if ($assessment->teachercomment) {
2501             echo "<tr valign=top>\n";
2502             echo "  <td align=\"right\"><p><b>". get_string("teacherscomment", "workshop").":</b></p></td>\n";
2503             echo "  <td>\n";
2504             echo text_to_html($assessment->teachercomment);
2505             echo "&nbsp;</td>\n";
2506             echo "</tr>\n";
2507         }
2509         echo "<tr valign=\"top\">\n";
2510         echo "  <td align=\"right\"><p><b>";
2511         print_string('grade', 'workshop');
2512         echo ":</b></p></td><td>\n";
2513         echo number_format($assessment->gradinggrade * $workshop->gradinggrade / 100, 0);
2514         echo "&nbsp;</td>\n";
2515         echo "</tr>\n";
2516     }
2517     
2518     echo "<tr valign=\"top\">\n";
2519     echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
2520     echo "</tr>\n";
2521             
2522     // ...and close the table, show submit button if needed...
2523     echo "</table>\n";
2524     if ($assessment) {
2525         if ($allowchanges) {  
2526             echo "<input type=\"submit\" value=\"".get_string("savemyassessment", "workshop")."\" />\n";
2527         }
2528         // ...if user is author, assessment not agreed, there's no comments, the showcommentlinks flag is set and 
2529         // it's not self assessment then show some buttons!
2530         if (($submission->userid == $USER->id) and !$assessment->timeagreed and !$comments and $showcommentlinks and 
2531                 $submission->userid != $assessment->userid) {
2532             echo "<input type=\"button\" value=\"".get_string("agreetothisassessment", "workshop")."\" 
2533                 onclick=\"document.assessmentform.action.value='agreeassessment';document.assessmentform.submit();\" />\n";
2534             echo "<input type=\"submit\" value=\"".get_string("disagreewiththisassessment", "workshop")."\"
2535                 onclick=\"document.assessmentform.action.value='addcomment';document.assessmentform.submit();\" />\n";
2536         }
2537     }
2538     echo "</center>";
2539     echo "</form>\n";
2543 //////////////////////////////////////////////////////////////////////////////////////
2544 function workshop_print_assessments_by_user_for_admin($workshop, $user) {
2546     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
2547         error("Course Module ID was incorrect");
2548     }
2550     if ($assessments = workshop_get_user_assessments_done($workshop, $user)) {
2551         foreach ($assessments as $assessment) {
2552             echo "<p><center><b>".get_string("assessmentby", "workshop", fullname($user))."</b></center></p>\n";
2553             workshop_print_assessment($workshop, $assessment);
2554             echo "<p align=\"right\">".
2555                 '<a href="assessments.php?action=gradeassessment&amp;id='.$cm->id.'&amp;stype=student&amp;aid='.$assessment->id.'">'.
2556                 get_string('assessthisassessment', 'workshop').'</a> | '.
2557                 "<a href=\"assessments.php?action=adminconfirmdelete&amp;id=$cm->id&amp;aid=$assessment->id\">".
2558                 get_string("delete", "workshop")."</a></p><hr />\n";
2559             }
2560         }
2561     }
2564 //////////////////////////////////////////////////////////////////////////////////////
2565 function workshop_print_assessments_for_admin($workshop, $submission) {
2567     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
2568         error("Course Module ID was incorrect");
2569     }
2571     if ($assessments =workshop_get_assessments($submission)) {
2572         foreach ($assessments as $assessment) {
2573             if (!$user = get_record("user", "id", $assessment->userid)) {
2574                 error (" workshop_print_assessments_for_admin: unable to get user record");
2575                 }
2576             echo "<p><center><b>".get_string("assessmentby", "workshop", fullname($user))."</b></center></p>\n";
2577             workshop_print_assessment($workshop, $assessment);
2578             echo "<p align=\"right\"><a href=\"assessments.php?action=adminconfirmdelete&amp;id=$cm->id&amp;aid=$assessment->id\">".
2579                 get_string("delete", "workshop")."</a></p><hr />\n";
2580             }
2581         }
2582     }
2585 //////////////////////////////////////////////////////////////////////////////////////
2586 function workshop_print_assignment_info($workshop) {
2588     if (! $course = get_record("course", "id", $workshop->course)) {
2589         error("Course is misconfigured");
2590     }
2591     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $course->id)) {
2592         error("Course Module ID was incorrect");
2593     }
2594     // print standard assignment heading
2595     print_simple_box_start("center");
2596     print_heading($workshop->name, "center");
2597     print_simple_box_start("center");
2599     // print phase and date info
2600     $string = '<b>'.get_string('currentphase', 'workshop').'</b>: '.workshop_phase($workshop).'<br />';
2601     $dates = array(
2602         'submissionstart' => $workshop->submissionstart,
2603         'submissionend' => $workshop->submissionend,
2604         'assessmentstart' => $workshop->assessmentstart,
2605         'assessmentend' => $workshop->assessmentend
2606     );
2607     foreach ($dates as $type => $date) {
2608         if ($date) {
2609             $strdifference = format_time($date - time());
2610             if (($date - time()) < 0) {
2611                 $strdifference = "<font color=\"red\">$strdifference</font>";
2612             }
2613             $string .= '<b>'.get_string($type, 'workshop').'</b>: '.userdate($date)." ($strdifference)<br />";
2614         }
2615     }
2616     echo $string;
2618     $grade = $workshop->gradinggrade + $workshop->grade;
2619     echo "<br /><b>".get_string("maximumgrade")."</b>: $grade<br />";
2620     echo "<b>".get_string("detailsofassessment", "workshop")."</b>: 
2621         <a href=\"assessments.php?id=$cm->id&amp;action=displaygradingform\">".
2622         get_string("specimenassessmentform", "workshop")."</a><br />";
2623     print_simple_box_end();
2624     echo "<br />";
2625     echo format_text($workshop->description, $workshop->format);
2626     print_simple_box_end();
2627     echo "<br />";  
2628     }
2631 //////////////////////////////////////////////////////////////////////////////////////
2632 function workshop_print_difference($time) {
2633     if ($time < 0) {
2634         $timetext = get_string("late", "assignment", format_time($time));
2635         return " (<font color=\"red\">$timetext</font>)";
2636     } else {
2637         $timetext = get_string("early", "assignment", format_time($time));
2638         return " ($timetext)";
2639     }
2643 //////////////////////////////////////////////////////////////////////////////////////
2644 function workshop_print_feedback($course, $submission) {
2645     global $CFG, $THEME, $RATING;
2647     if (! $teacher = get_record("user", "id", $submission->teacher)) {
2648         error("Weird workshop error");
2649     }
2651     echo "\n<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\" align=\"center\"><tr><td bgcolor=#888888>";
2652     echo "\n<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\">";
2654     echo "\n<tr>";
2655     echo "\n<td rowspan=\"3\" bgcolor=\"$THEME->body\" width=\"35\" valign=\"top\">";
2656     print_user_picture($teacher->id, $course->id, $teacher->picture);
2657     echo "</td>";
2658     echo "<td nowrap=\"nowrap\" width=\"100%\" bgcolor=\"$THEME->cellheading\">".fullname($teacher);
2659     echo "&nbsp;&nbsp;<font size=\"2\"><i>".userdate($submission->timemarked)."</i>";
2660     echo "</tr>";
2662     echo "\n<tr><td width=\"100%\" bgcolor=\"$THEME->cellcontent\">";
2664     echo "<p align=\"right\"><font size=\"-1\"><i>";
2665     if ($submission->grade) {
2666         echo get_string("grade").": $submission->grade";
2667     } else {
2668         echo get_string("nograde");
2669     }
2670     echo "</i></font></p>";
2672     echo format_text($submission->assessorcomment);
2673     echo "</td></tr></table>";
2674     echo "</td></tr></table>";
2678 //////////////////////////////////////////////////////////////////////////////////////
2679 function workshop_print_key($workshop) {
2680     // print an explaination of the grades
2681     
2682     if (!$course = get_record("course", "id", $workshop->course)) {
2683         error("Print key: course not found");
2684     }
2685     echo "<div align=\"center\">\n";
2686     echo "<p><small>{} ".get_string("assessmentby", "workshop", $course->student).";&nbsp;&nbsp;\n";
2687     echo "[] ".get_string("assessmentby", "workshop", $course->teacher).";&nbsp;&nbsp;\n";
2688     echo "&lt;&gt; ".get_string("assessmentdropped", "workshop").";\n";
2689     if ($workshop->agreeassessments) echo "&lt;&lt;&gt;&gt; ".get_string("assessmentnotyetagreed", "workshop").";\n";
2690     echo "<br />() ".get_string("automaticgradeforassessment", "workshop").";&nbsp;&nbsp;\n";
2691     echo "[] ".get_string("teachergradeforassessment", "workshop", $course->teacher).".\n";
2692     echo "<br />".get_string("gradesforsubmissionsare", "workshop", $workshop->grade).";&nbsp;&nbsp;\n";
2693     echo get_string("gradesforassessmentsare", "workshop", $workshop->gradinggrade).".</small></p>\n";
2694     echo "</div>\n";
2695     return;    
2697     
2699 //////////////////////////////////////////////////////////////////////////////////////
2700 function workshop_print_league_table($workshop) {
2701     // print an order table of (student) submissions showing teacher's and student's assessments
2702     
2703     if (! $course = get_record("course", "id", $workshop->course)) {
2704         error("Print league table: Course is misconfigured");
2705     }
2706     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
2707             error("Course Module ID was incorrect");
2708     }
2709     // set $groupid if workshop is in SEPARATEGROUPS mode
2710     if (groupmode($course, $cm) == SEPARATEGROUPS) {
2711         $groupid = get_current_group($course->id);
2712     } else {
2713         $groupid = 0;
2714     }
2715  
2716     $nentries = $workshop->showleaguetable;
2717     if ($workshop->anonymous and isstudent($course->id)) {
2718         $table->head = array (get_string("title", "workshop"), 
2719             get_string("teacherassessments", "workshop", $course->teacher),  
2720             get_string("studentassessments", "workshop",    $course->student), get_string("overallgrade", "workshop"));
2721         $table->align = array ("left",  "center", "center", "center");
2722         $table->size = array ("*", "*", "*", "*");
2723     }
2724     else { // show names
2725         $table->head = array (get_string("title", "workshop"),  get_string("name"),
2726             get_string("teacherassessments", "workshop", $course->teacher),  
2727             get_string("studentassessments", "workshop",    $course->student), get_string("overallgrade", "workshop"));
2728         $table->align = array ("left", "left", "center", "center", "center");
2729         $table->size = array ("*", "*", "*", "*", "*");
2730     }
2731     $table->cellpadding = 2;
2732     $table->cellspacing = 0;
2734     if ($submissions = workshop_get_student_submissions($workshop)) {
2735         foreach ($submissions as $submission) {
2736             if ($groupid) {
2737                 // check submission's group
2738                 if (!ismember($groupid, $submission->userid)) {
2739                     continue; // skip this submission
2740                 }
2741             }
2742             $grades[$submission->id] = workshop_submission_grade($workshop, $submission);
2743         }
2744         arsort($grades); // largest grade first
2745         reset($grades);
2746         $n = 1;
2747         while (list($submissionid, $grade) = each($grades)) {
2748             if (!$submission = get_record("workshop_submissions", "id", $submissionid)) {
2749                 error("Print league table: submission not found");
2750             }
2751             if (!$user = get_record("user", "id", $submission->userid)) {
2752                 error("Print league table: user not found");
2753             }
2754             if ($workshop->anonymous and isstudent($course->id)) {
2755                 $table->data[] = array(workshop_print_submission_title($workshop, $submission),
2756                         workshop_print_submission_assessments($workshop, $submission, "teacher"),
2757                         workshop_print_submission_assessments($workshop, $submission, "student"), $grade);
2758             }
2759             else {
2760                 $table->data[] = array(workshop_print_submission_title($workshop, $submission), fullname($user),
2761                         workshop_print_submission_assessments($workshop, $submission, "teacher"),
2762                         workshop_print_submission_assessments($workshop, $submission, "student"), $grade);
2763             }
2764             $n++;
2765             if ($n > $nentries) {
2766                 break;
2767             }
2768         }
2769         print_heading(get_string("leaguetable", "workshop"));
2770         print_table($table);
2771         workshop_print_key($workshop);
2772     }
2774     
2776 //////////////////////////////////////////////////////////////////////////////////////
2777 function workshop_print_submission($workshop, $submission) {
2778     // prints the submission with optional attachments
2779     global $CFG;
2781     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
2782             error("Course Module ID was incorrect");
2783     }
2784     print_simple_box(format_text($submission->description), 'center');
2785     if ($workshop->nattachments) {
2786         $n = 1;
2787         echo "<table align=\"center\">\n";
2788         $filearea = workshop_file_area_name($workshop, $submission);
2789         if ($basedir = workshop_file_area($workshop, $submission)) {
2790             if ($files = get_directory_list($basedir)) {
2791                 foreach ($files as $file) {
2792                     $icon = mimeinfo("icon", $file);
2793                     if ($CFG->slasharguments) {
2794                         $ffurl = "file.php/$filearea/$file";
2795                     } else {
2796                         $ffurl = "file.php?file=/$filearea/$file";
2797                     }
2798                     echo "<tr><td><b>".get_string("attachment", "workshop")." $n:</b> \n";
2799                     echo "<img src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\" 
2800                         border=\"0\" alt=\"File\" />".
2801                         "&nbsp;<a target=\"uploadedfile\" href=\"$CFG->wwwroot/$ffurl\">$file</a></td></tr>";
2802                     $n++;
2803                 }
2804             }
2805         }
2806         echo "</table>\n";
2807     }
2808     return;
2812 //////////////////////////////////////////////////////////////////////////////////////
2813 function workshop_print_submission_assessments($workshop, $submission, $type) {
2814     global $USER;
2815     // Returns the teacher or peer grade and a hyperlinked list of grades for this submission
2817     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
2818             error("Course Module ID was incorrect");
2819     }
2820  
2821     $str = '';
2822     // get the cold assessments in grade order, highest first
2823     if ($assessments = workshop_get_assessments($submission, "", "grade DESC")) {
2824         switch ($type) {
2825             case "teacher" :
2826                 // students can see teacher assessments only if the release date has passed
2827                 $timenow = time();
2828                 if (isteacher($workshop->course, $USER->id) or ($timenow > $workshop->releasegrades)) {
2829                     foreach ($assessments as $assessment) {
2830                         if (isteacher($workshop->course, $assessment->userid)) {
2831                             $str .= "<a href=\"assessments.php?action=viewassessment&amp;id=$cm->id&amp;aid=$assessment->id\">"
2832                                  . "[".number_format($assessment->grade *$workshop->grade / 100, 0)."]</a> ";
2833                         }
2834                     }
2835                 }
2836                 break;
2837             case "student" : 
2838                 foreach ($assessments as $assessment) {
2839                     if (isstudent($workshop->course, $assessment->userid)) {
2840                         $str .= "<a href=\"assessments.php?action=viewassessment&amp;id=$cm->id&amp;aid=$assessment->id\">";
2841                         if ($workshop->agreeassessments and !$assessment->timeagreed and 
2842                                 isstudent($workshop->course, $submission->userid)) { // agreement on student work only
2843                             $str .= "&lt;&lt;".number_format($assessment->grade * $workshop->grade / 100, 0)." (".
2844                                 number_format($assessment->gradinggrade * $workshop->gradinggrade / 100, 0).
2845                                 ")&gt;&gt;</a> ";
2846                         } elseif ($assessment->timegraded) {
2847                             if ($assessment->gradinggrade) {
2848                                 $str .= "{".number_format($assessment->grade * $workshop->grade / 100, 0);
2849                                 if ($assessment->teachergraded) {
2850                                     $str .= " [".number_format($assessment->gradinggrade * $workshop->gradinggrade / 100, 0).
2851                                     "]}</a> ";
2852                                 } else {
2853                                     $str .= " (".number_format($assessment->gradinggrade * $workshop->gradinggrade / 100, 0).
2854                                     ")}</a> ";
2855                                 }
2856                             } else {
2857                                 $str .= "&lt;".number_format($assessment->grade * $workshop->grade / 100, 0).
2858                                     " (0)&gt;</a> ";
2859                             }
2860                         } else {
2861                             $str .= "{".number_format($assessment->grade * $workshop->grade / 100, 0)."}</a> ";
2862                         }
2863                     }
2864                 }
2865                 break;
2866         }
2867     }
2868     if (!$str) {
2869         $str = "&nbsp;";   // be kind to Mozilla browsers!
2870     }
2871     return $str;
2875 //////////////////////////////////////////////////////////////////////////////////////
2876 function workshop_print_submission_title($workshop, $submission) {
2877 // Arguments are objects
2879     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
2880             error("Course Module ID was incorrect");
2881     }
2882     
2883     if (!$submission->timecreated) { // a "no submission"
2884         return $submission->title;
2885     }
2886     return "<a href=\"submissions.php?id=$cm->id&action=showsubmission&sid=$submission->id\">$submission->title</a>";
2891 function workshop_print_time_to_deadline($time) {
2892     if ($time < 0) {
2893         $timetext = get_string("afterdeadline", "workshop", format_time($time));
2894         return " (<font color=\"red\">$timetext</font>)";
2895     } else {
2896         $timetext = get_string("beforedeadline", "workshop", format_time($time));
2897         return " ($timetext)";
2898     }
2902 //////////////////////////////////////////////////////////////////////////////////////
2903 function workshop_print_upload_form($workshop) {
2904     global $CFG;
2906     if (! $course = get_record("course", "id", $workshop->course)) {
2907         error("Course is misconfigured");
2908     }
2909     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $course->id)) {
2910         error("Course Module ID was incorrect");
2911     }
2912     $usehtmleditor = can_use_html_editor();
2914     echo "<div align=\"center\">";
2915     echo "<form enctype=\"multipart/form-data\" method=\"POST\" action=\"upload.php\">";
2916     echo " <input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
2917     echo "<table celpadding=\"5\" border=\"1\" align=\"center\">\n";
2918     // now get the submission
2919     echo "<tr valign=\"top\"><td><b>". get_string("title", "workshop").":</b>\n";
2920     echo "<input type=\"text\" name=\"title\" size=\"60\" maxlength=\"100\" value=\"\" />\n";
2921     echo "</td></tr><tr><td><b>".get_string("submission", "workshop").":</b><br />\n";
2922     print_textarea($usehtmleditor, 25,70, 630, 400, "description");
2923     use_html_editor("description");
2924     echo "</td></tr><tr><td>\n";
2925     if ($workshop->nattachments) {
2926         require_once($CFG->dirroot.'/lib/uploadlib.php');
2927         for ($i=0; $i < $workshop->nattachments; $i++) {
2928             $iplus1 = $i + 1;
2929             $tag[$i] = get_string("attachment", "workshop")." $iplus1:";
2930         }
2931         upload_print_form_fragment($workshop->nattachments,null,$tag,false,null,$course->maxbytes,
2932                 $workshop->maxbytes,false);
2933     }
2934     echo "</td></tr></table>\n";
2935     echo " <input type=\"submit\" name=\"save\" value=\"".get_string("submitassignment","workshop")."\" />";
2936     echo "</form>";
2937     echo "</div>";
2941 //////////////////////////////////////////////////////////////////////////////////////
2942 function workshop_print_user_assessments($workshop, $user) {
2943     // Returns the number of assessments and a hyperlinked list of grading grades for the assessments made by this user
2945     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
2946             error("Course Module ID was incorrect");
2947     }
2948  
2949     if ($assessments = workshop_get_user_assessments_done($workshop, $user)) {
2950         $n = count($assessments);
2951         $str = "$n : ";
2952         foreach ($assessments as $assessment) {
2953             $str .= "<a href=\"assessments.php?action=viewassessment&amp;id=$cm->id&amp;aid=$assessment->id\">";
2954             if ($assessment->timegraded) {
2955                 if ($assessment->gradinggrade) {
2956                     $str .= "{".number_format($assessment->grade * $workshop->grade / 100, 0);
2957                     if ($assessment->teachergraded) {
2958                         $str .= " [".number_format($assessment->gradinggrade * $workshop->gradinggrade / 100)."]}</a> ";
2959                     } else {
2960                         $str .= " (".number_format($assessment->gradinggrade * $workshop->gradinggrade / 100).")}</a> ";
2961                     }
2962                 } else {
2963                     $str .= "&lt;".number_format($assessment->grade * $workshop->grade / 100, 0)." (0)&gt;</a> ";
2964                 }
2965             } else {
2966                 $str .= "{".number_format($assessment->grade * $workshop->grade / 100, 0)." (-)}</a> ";
2967             }
2968         }
2969     }
2970     else {
2971         $str ="0";
2972     }
2973     return $str;
2977 //////////////////////////////////////////////////////////////////////////////////////
2978 function workshop_test_user_assessments($workshop, $user) {
2979     // see if user has assessed required number of assessments of teachers submissions...
2980     global $CFG;
2981     
2982     $result = true;
2983     $n = 0;
2984     $timenow =time();
2985     if ($submissions = workshop_get_teacher_submissions($workshop)) {
2986         foreach ($submissions as $submission) {
2987             if ($assessment = workshop_get_submission_assessment($submission, $user)) {
2988                 // ...the date stamp on the assessment should be in the past 
2989                 if ($assessment->timecreated < $timenow) {
2990                     $n++;
2991                 }
2992             }
2993         }
2994         if ($n < min($workshop->ntassessments, workshop_count_teacher_submissions($workshop))) {
2995             $result = false; 
2996         }
2997     }
2998     return $result;
3000     
3002 ?>