More changes to the user interface. See http://moodle.org/mod/forum/view.php?id=740.
[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=\"viewassessment.php?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=\"assess.php?sid=$submission->id\">".
765                         get_string("edit", "workshop")."</a>";
766                     }
767                 }
768             else { // user has not graded this submission
769                 $action = "<a href=\"assess.php?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=\"viewassessment.php?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=\"assess.php?sid=$submission->id\">".
817                         get_string("edit", "workshop")."</a>";
818                     }
819                 }
820             else { // user has not assessed this submission
821                 $action = "<a href=\"assess.php?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=\"viewassessment.php?&amp;id=$cm->id&amp;aid=$assessment->id\">".
853                         get_string("edit", "workshop")."</a>";
854                     }
855                 else {
856                     $action = "<a href=\"viewassessment.php?&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=\"viewassessment.php?id=$cm->id&amp;aid=$assessment->id&".
903                             "allowcomments=$workshop->agreeassessments\">".
904                             get_string("view", "workshop")."</a>";
905                         $action .= " | <a href=\"assess.php?sid=$submission->id\">".
906                             get_string("reassess", "workshop")."</a>";
907                     } else {
908                         $action = "<a href=\"viewassessment.php?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=\"assess.php?sid=$submission->id\">".
915                             get_string("reassess", "workshop")."</a>";
916                     } else {
917                         $action = "<a href=\"viewassessment.php?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=\"viewassessment.php?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=\"assess.php?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=\"assess.php?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=\"assess.php?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=\"assess.php?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=\"assess.php?sid=$submission->id\">".
1255                         get_string("assess", "workshop")."</a>";
1256                 }
1257                 elseif (($curtime - $assessment->timecreated) > $CFG->maxeditingtime) {
1258                     $action .= " | <a href=\"assess.php?sid=$submission->id\">"
1259                         .get_string("reassess", "workshop")."</a>";
1260                 }
1261                 else { // there's still time left to edit...
1262                     $action .= " | <a href=\"assess.php?sid=$submission->id\">".
1263                         get_string("edit", "workshop")."</a>";
1264                 }
1265             }
1266                 else { // user has not graded this submission
1267                     $action .= " | <a href=\"assess.php?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=confirmdelete&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 $assessment->submissionid 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=\"assess.php?sid=$submission->id\">".
1425                         get_string("reassess", "workshop")."</a>";
1426                 }
1427                 else { // there's still time left to edit...
1428                     $action .= " | <a href=\"assess.php?sid=$submission->id\">".
1429                         get_string("edit", "workshop")."</a>";
1430                 }
1431             }
1432             else { // user has not assessed this submission
1433                 $action .= " | <a href=\"assess.php?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=confirmdelete&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=\"viewassessment.php?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=\"assess.php?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=\"assess.php?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=\"viewassessment.php?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=\"assess.php?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=\"assess.php?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=\"assess.php?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=\"assess.php?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=\"viewassessment.php?&amp;id=$cm->id&amp;stype=$stype&amp;aid=$assessment->id\">".
1757                         get_string("edit", "workshop")."</a>";
1758                     }
1759                 else {
1760                     $action = "<a href=\"viewassessment.php?&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=confirmdelete&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     if ($assessment) {
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     
1869     $timenow = time();
1871     // reset the internal flags
1872     if ($assessment) {
1873         $showgrades = false;
1874     }
1875     else { // if no assessment, i.e. specimen grade form always show grading scales
1876         $showgrades = true;
1877     }
1878     
1879     if ($assessment) {
1880         // set the internal flag if necessary
1881         if ($allowchanges or !$workshop->agreeassessments or !$workshop->hidegrades or 
1882                 $assessment->timeagreed) {
1883             $showgrades = true;
1884         }
1885         
1886         echo "<center>\n";
1887     
1888         // see if this is a pre-filled assessment for a re-submission...
1889         if ($assessment->resubmission) {
1890             // ...and print an explaination
1891             print_heading(get_string("assessmentofresubmission", "workshop"));
1892         }
1893         
1894         // print agreement time if the workshop requires peer agreement
1895         if ($workshop->agreeassessments and $assessment->timeagreed) {
1896             echo "<p>".get_string("assessmentwasagreedon", "workshop", userdate($assessment->timeagreed));
1897         }
1899         // first print any comments on this assessment
1900         if ($comments = workshop_get_comments($assessment)) {
1901             echo "<table cellpadding=\"2\" border=\"1\">\n";
1902             $firstcomment = TRUE;
1903             foreach ($comments as $comment) {
1904                 echo "<tr valign=\"top\"><td bgcolor=\"$THEME->cellheading2\"><p><b>".
1905                     get_string("commentby","workshop")." ";
1906                 if (isteacher($workshop->course, $comment->userid)) {
1907                     echo $course->teacher;
1908                 }
1909                 elseif ($assessment->userid == $comment->userid) {
1910                     print_string("assessor", "workshop");
1911                 }
1912                 else {
1913                     print_string("authorofsubmission", "workshop");
1914                 }
1915                 echo " ".get_string("on", "workshop", userdate($comment->timecreated))."</b></p></td></tr><tr><td>\n";
1916                 echo format_text($comment->comments)."&nbsp;\n";
1917                 // add the links if needed
1918                 if ($firstcomment and $showcommentlinks and !$assessment->timeagreed) {
1919                     // show links depending on who doing the viewing
1920                     $firstcomment = FALSE;
1921                     if (isteacher($workshop->course, $USER->id) and ($comment->userid != $USER->id)) {
1922                         echo "<p align=\"right\"><a href=\"assessments.php?action=addcomment&amp;id=$cm->id&amp;aid=$assessment->id\">".
1923                             get_string("reply", "workshop")."</a>\n";
1924                     }
1925                     elseif (($comment->userid ==$USER->id) and (($timenow - $comment->timecreated) < $CFG->maxeditingtime)) {
1926                         echo "<p align=\"right\"><a href=\"assessments.php?action=editcomment&amp;id=$cm->id&amp;cid=$comment->id\">".
1927                             get_string("edit", "workshop")."</a>\n";
1928                         if ($USER->id == $submission->userid) {
1929                             echo " | <a href=\"assessments.php?action=agreeassessment&amp;id=$cm->id&amp;aid=$assessment->id\">".
1930                                 get_string("agreetothisassessment", "workshop")."</a>\n";
1931                         }
1932                     }
1933                     elseif (($comment->userid != $USER->id) and (($USER->id == $assessment->userid) or 
1934                         ($USER->id == $submission->userid))) {
1935                         echo "<p align=\"right\"><a href=\"assessments.php?action=addcomment&amp;id=$cm->id&amp;aid=$assessment->id\">".
1936                             get_string("reply", "workshop")."</a>\n";
1937                         if ($USER->id == $submission->userid) {
1938                             echo " | <a href=\"assessments.php?action=agreeassessment&amp;id=$cm->id&amp;aid=$assessment->id\">".
1939                                 get_string("agreetothisassessment", "workshop")."</a>\n";
1940                         }
1941                     }
1942                 }
1943                 echo "</td></tr>\n";
1944             }
1945             echo "</table>\n";
1946         }
1947     }
1948         
1949     // now print the grading form with the grading grade if any
1950     // FORM is needed for Mozilla browsers, else radio bttons are not checked
1951         ?>
1952     <form name="assessmentform" method="post" action="assessments.php">
1953     <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
1954     <input type="hidden" name="aid" value="<?php echo $assessment->id ?>" />
1955     <input type="hidden" name="action" value="updateassessment" />
1956     <input type="hidden" name="returnto" value="<?php echo $returnto ?>" />
1957     <input type="hidden" name="elementno" value="" />
1958     <input type="hidden" name="stockcommentid" value="" />
1959     <center>
1960     <table cellpadding="2" border="1">
1961     <?php
1962     echo "<tr valign=\"top\">\n";
1963     echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\"><center><b>";
1964     if ($assessment and isteacher($course->id)) {
1965         $user = get_record('user', 'id', $assessment->userid);
1966         print_string("assessmentby", "workshop", fullname($user));
1967     } else {
1968         print_string('assessment', 'workshop');
1969     }
1970     echo '</b><br />'.userdate($assessment->timecreated)."</center></td>\n";
1971     echo "</tr>\n";
1972     
1973     // only show the grade if grading strategy > 0 and the grade is positive
1974     if ($showgrades and $workshop->gradingstrategy and $assessment->grade >= 0) { 
1975         echo "<tr valign=\"top\">\n
1976             <td colspan=\"2\" align=\"center\">
1977             <b>".get_string("thegradeis", "workshop").": ".
1978             number_format($assessment->grade * $workshop->grade / 100, 2)." (".
1979             get_string("maximumgrade")." ".number_format($workshop->grade, 0).")</b>
1980             </td></tr><tr><td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td></tr>\n";
1981     }
1982     
1983     // get the assignment elements...
1984     $elementsraw = get_records("workshop_elements", "workshopid", $workshop->id, "elementno ASC");
1985     if (count($elementsraw) < $workshop->nelements) {
1986         print_string("noteonassignmentelements", "workshop");
1987     }
1988     if ($elementsraw) {
1989         foreach ($elementsraw as $element) {
1990             $elements[] = $element;   // to renumber index 0,1,2...
1991         }
1992     } else {
1993         $elements = null;
1994     }
1996     if ($assessment) {
1997         // get any previous grades...
1998         if ($gradesraw = get_records_select("workshop_grades", "assessmentid = $assessment->id", "elementno")) {
1999             foreach ($gradesraw as $grade) {
2000                 $grades[] = $grade;   // to renumber index 0,1,2...
2001                 }
2002             }
2003         }
2004     else {
2005         // setup dummy grades array
2006         for($i = 0; $i < count($elementsraw); $i++) { // gives a suitable sized loop
2007             $grades[$i]->feedback = get_string("yourfeedbackgoeshere", "workshop");
2008             $grades[$i]->grade = 0;
2009             }
2010         }
2011                 
2012     // determine what sort of grading
2013     switch ($workshop->gradingstrategy) {
2014         case 0:  // no grading
2015             // now print the form
2016             for ($i=0; $i < count($elements); $i++) {
2017                 $iplus1 = $i+1;
2018                 echo "<tr valign=\"top\">\n";
2019                 echo "  <td align=\"right\"><p><b>". get_string("element","workshop")." $iplus1:</b></p></td>\n";
2020                 echo "  <td>".format_text($elements[$i]->description);
2021                 echo "</td></tr>\n";
2022                 echo "<tr valign=\"top\">\n";
2023                 echo "  <td align=\"right\"><p><b>". get_string("feedback").":</b></p></td>\n";
2024                 echo "  <td>\n";
2025                 if ($allowchanges) {
2026                     echo "      <textarea name=\"feedback_$i\" rows=\"3\" cols=\"75\" wrap=\"virtual\">\n";
2027                     if (isset($grades[$i]->feedback)) {
2028                         echo $grades[$i]->feedback;
2029                         }
2030                     echo "</textarea>\n";
2031                     }
2032                 else {
2033                     echo format_text($grades[$i]->feedback);
2034                     }
2035                 echo "  </td>\n";
2036                 echo "</tr>\n";
2038                 // if active and the teacher show stock comments...
2039                 if ($allowchanges and isteacher($course->id, $USER->id)) {
2040                     echo "<tr><td valign=\"top\" align=\"right\"><input type=\"button\" value=\"".
2041                         get_string("addcomment", "workshop")."\" 
2042                         onclick=\"document.assessmentform.action.value='addstockcomment';
2043                         document.assessmentform.elementno.value=$i;document.assessmentform.submit();\" /> \n";
2044                     helpbutton("addcommenttobank", get_string("addcomment", "workshop"), "workshop");
2045                     echo "</td><td>\n";
2046                     if ($stockcomments = get_records_select("workshop_stockcomments", "workshopid = $workshop->id
2047                             AND elementno = $i", "id")) { // show comments in fixed order (oldest first)
2048                         foreach ($stockcomments as $stockcomment) {
2049                             echo "<a onclick=\"document.assessmentform.feedback_$i.value+=' '+'".
2050                                 addslashes($stockcomment->comments)."';\">&lt;&lt;$stockcomment->comments&gt;&gt;</a>\n";
2051                             if (isteacheredit($course->id, $USER->id)) {
2052                                 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";
2053                             }
2054                             echo "<br />\n";
2055                         }
2056                     } 
2057                     echo "</td></tr>\n";
2058                 }
2060                 echo "<tr valign=\"top\">\n";
2061                 echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
2062                 echo "</tr>\n";
2063                 }
2064             break;
2065             
2066         case 1: // accumulative grading
2067             // now print the form
2068             for ($i=0; $i < count($elements); $i++) {
2069                 $iplus1 = $i+1;
2070                 echo "<tr valign=\"top\">\n";
2071                 echo "  <td align=\"right\"><p><b>". get_string("element","workshop")." $iplus1:</b></p></td>\n";
2072                 echo "  <td>".format_text($elements[$i]->description);
2073                 echo "<p align=\"right\"><font size=\"1\">".get_string("weight", "workshop").": ".
2074                     number_format($WORKSHOP_EWEIGHTS[$elements[$i]->weight], 2)."</font>\n";
2075                 echo "</td></tr>\n";
2076                 if ($showgrades) {
2077                     echo "<tr valign=\"top\">\n";
2078                     echo "  <td align=\"right\"><p><b>". get_string("grade"). ":</b></p></td>\n";
2079                     echo "  <td valign=\"top\">\n";
2080                     
2081                     // get the appropriate scale
2082                     $scalenumber=$elements[$i]->scale;
2083                     $SCALE = (object)$WORKSHOP_SCALES[$scalenumber];
2084                     switch ($SCALE->type) {
2085                         case 'radio' :
2086                                 // show selections highest first
2087                                 echo "<center><b>$SCALE->start</b>&nbsp;&nbsp;&nbsp;";
2088                                 for ($j = $SCALE->size - 1; $j >= 0 ; $j--) {
2089                                     $checked = false;
2090                                     if (isset($grades[$i]->grade)) { 
2091                                         if ($j == $grades[$i]->grade) {
2092                                             $checked = true;
2093                                             }
2094                                         }
2095                                     else { // there's no previous grade so check the lowest option
2096                                         if ($j == 0) {
2097                                             $checked = true;
2098                                             }
2099                                         }
2100                                     if ($checked) {
2101                                         echo " <input type=\"RADIO\" name=\"grade[$i]\" value=\"$j\" checked=\"checked\" /> &nbsp;&nbsp;&nbsp;\n";
2102                                         }
2103                                     else {
2104                                         echo " <input type=\"RADIO\" name=\"grade[$i]\" value=\"$j\" /> &nbsp;&nbsp;&nbsp;\n";
2105                                         }
2106                                     }
2107                                 echo "&nbsp;&nbsp;&nbsp;<b>$SCALE->end</b></center>\n";
2108                                 break;
2109                         case 'selection' :  
2110                                 unset($numbers);
2111                                 for ($j = $SCALE->size; $j >= 0; $j--) {
2112                                     $numbers[$j] = $j;
2113                                     }
2114                                 if (isset($grades[$i]->grade)) {
2115                                     choose_from_menu($numbers, "grade[$i]", $grades[$i]->grade, "");
2116                                     }
2117                                 else {
2118                                     choose_from_menu($numbers, "grade[$i]", 0, "");
2119                                     }
2120                                 break;
2121                         }
2122             
2123                     echo "  </td>\n";
2124                     echo "</tr>\n";
2125                     }
2126                 echo "<tr valign=\"top\">\n";
2127                 echo "  <td align=\"right\"><p><b>". get_string("feedback").":</b></p></td>\n";
2128                 echo "  <td>\n";
2129                 if ($allowchanges) {
2130                     echo "      <textarea name=\"feedback_$i\" rows=\"3\" cols=\"75\" wrap=\"virtual\">\n";
2131                     if (isset($grades[$i]->feedback)) {
2132                         echo $grades[$i]->feedback;
2133                         }
2134                     echo "</textarea>\n";
2135                     }
2136                 else {
2137                     echo format_text($grades[$i]->feedback);
2138                     }
2139                 echo "  </td>\n";
2140                 echo "</tr>\n";
2141                 
2142                 // if active and the teacher show stock comments...
2143                 if ($allowchanges and isteacher($course->id, $USER->id)) {
2144                     echo "<tr><td valign=\"top\" align=\"right\"><input type=\"button\" value=\"".
2145                         get_string("addcomment", "workshop")."\" 
2146                         onclick=\"document.assessmentform.action.value='addstockcomment';
2147                         document.assessmentform.elementno.value=$i;document.assessmentform.submit();\" /> \n";
2148                     helpbutton("addcommenttobank", get_string("addcomment", "workshop"), "workshop");
2149                     echo "</td><td>\n";
2150                     if ($stockcomments = get_records_select("workshop_stockcomments", "workshopid = $workshop->id
2151                             AND elementno = $i", "id")) { // get comments in a fixed order - oldest first
2152                         foreach ($stockcomments as $stockcomment) {
2153                             echo "<a onclick=\"document.assessmentform.feedback_$i.value+=' '+'".
2154                                 addslashes($stockcomment->comments).
2155                                 "';\">&lt;&lt;$stockcomment->comments&gt;&gt;</a>\n";
2156                             if (isteacheredit($course->id, $USER->id)) {
2157                                 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";
2158                             }
2159                             echo "<br />\n";
2160                         }
2161                     } 
2162                     echo "</td></tr>\n";
2163                 }
2165                 echo "<tr valign=\"top\">\n";
2166                 echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
2167                 echo "</tr>\n";
2168                 }
2169             break;
2170             
2171         case 2: // error banded grading
2172             // now run through the elements
2173             $negativecount = 0;
2174             for ($i=0; $i < count($elements) - 1; $i++) {
2175                 $iplus1 = $i+1;
2176                 echo "<tr valign=\"top\">\n";
2177                 echo "  <td align=\"right\"><p><b>". get_string("element","workshop")." $iplus1:</b></p></td>\n";
2178                 echo "  <td>".format_text($elements[$i]->description);
2179                 echo "<p align=\"right\"><font size=\"1\">".get_string("weight", "workshop").": ".
2180                     number_format($WORKSHOP_EWEIGHTS[$elements[$i]->weight], 2)."</font>\n";
2181                 echo "</td></tr>\n";
2182                 echo "<tr valign=\"top\">\n";
2183                 echo "  <td align=\"right\"><p><b>". get_string("grade"). ":</b></p></td>\n";
2184                 echo "  <td valign=\"top\">\n";
2185                     
2186                 // get the appropriate scale - yes/no scale (0)
2187                 $SCALE = (object) $WORKSHOP_SCALES[0];
2188                 switch ($SCALE->type) {
2189                     case 'radio' :
2190                             // show selections highest first
2191                             echo "<center><b>$SCALE->start</b>&nbsp;&nbsp;&nbsp;";
2192                             for ($j = $SCALE->size - 1; $j >= 0 ; $j--) {
2193                                 $checked = false;
2194                                 if (isset($grades[$i]->grade)) { 
2195                                     if ($j == $grades[$i]->grade) {
2196                                         $checked = true;
2197                                         }
2198                                     }
2199                                 else { // there's no previous grade so check the lowest option
2200                                     if ($j == 0) {
2201                                         $checked = true;
2202                                         }
2203                                     }
2204                                 if ($checked) {
2205                                     echo " <input type=\"RADIO\" name=\"grade[$i]\" value=\"$j\" checked=\"checked\" /> &nbsp;&nbsp;&nbsp;\n";
2206                                     }
2207                                 else {
2208                                     echo " <input type=\"RADIO\" name=\"grade[$i]\" value=\"$j\" /> &nbsp;&nbsp;&nbsp;\n";
2209                                     }
2210                                 }
2211                             echo "&nbsp;&nbsp;&nbsp;<b>$SCALE->end</b></center>\n";
2212                             break;
2213                     case 'selection' :  
2214                             unset($numbers);
2215                             for ($j = $SCALE->size; $j >= 0; $j--) {
2216                                 $numbers[$j] = $j;
2217                                 }
2218                             if (isset($grades[$i]->grade)) {
2219                                 choose_from_menu($numbers, "grade[$i]", $grades[$i]->grade, "");
2220                                 }
2221                             else {
2222                                 choose_from_menu($numbers, "grade[$i]", 0, "");
2223                                 }
2224                             break;
2225                     }
2226         
2227                 echo "  </td>\n";
2228                 echo "</tr>\n";
2229                 echo "<tr valign=\"top\">\n";
2230                 echo "  <td align=\"right\"><p><b>". get_string("feedback").":</b></p></td>\n";
2231                 echo "  <td>\n";
2232                 if ($allowchanges) {
2233                     echo "      <textarea name=\"feedback_$i\" rows=\"3\" cols=\"75\" wrap=\"virtual\">\n";
2234                     if (isset($grades[$i]->feedback)) {
2235                         echo $grades[$i]->feedback;
2236                         }
2237                     echo "</textarea>\n";
2238                     }
2239                 else {
2240                     if (isset($grades[$i]->feedback)) {
2241                         echo format_text($grades[$i]->feedback);
2242                         }
2243                     }
2244                 echo "&nbsp;</td>\n";
2245                 echo "</tr>\n";
2247                 // if active and the teacher show stock comments...
2248                 if ($allowchanges and isteacher($course->id, $USER->id)) {
2249                     echo "<tr><td valign=\"top\" align=\"right\"><input type=\"button\" value=\"".
2250                         get_string("addcomment", "workshop")."\" 
2251                         onclick=\"document.assessmentform.action.value='addstockcomment';
2252                         document.assessmentform.elementno.value=$i;document.assessmentform.submit();\" /> \n";
2253                     helpbutton("addcommenttobank", get_string("addcomment", "workshop"), "workshop");
2254                     echo "</td><td>\n";
2255                     if ($stockcomments = get_records_select("workshop_stockcomments", "workshopid = $workshop->id
2256                             AND elementno = $i", "id")) { // get comments in a fixed order - oldest first
2257                         foreach ($stockcomments as $stockcomment) {
2258                             echo "<a onclick=\"document.assessmentform.feedback_$i.value+=' '+'".
2259                                 addslashes($stockcomment->comments).
2260                                 "';\">&lt;&lt;$stockcomment->comments&gt;&gt;</a>\n";
2261                             if (isteacheredit($course->id, $USER->id)) {
2262                                 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";
2263                             }
2264                             echo "<br />\n";
2265                         }
2266                     } 
2267                     echo "</td></tr>\n";
2268                 }
2269                 echo "<tr valign=\"top\">\n";
2270                 echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
2271                 echo "</tr>\n";
2272                 if (empty($grades[$i]->grade)) {
2273                     $negativecount++;
2274                     }
2275                 }
2276             // print the number of negative elements
2277             // echo "<tr><td>".get_string("numberofnegativeitems", "workshop")."</td><td>$negativecount</td></tr>\n";
2278             // echo "<tr valign=\"top\">\n";
2279             // echo "   <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
2280             echo "</table></center>\n";
2281             // now print the grade table
2282             echo "<p><center><b>".get_string("gradetable","workshop")."</b></center>\n";
2283             echo "<center><table cellpadding=\"5\" border=\"1\"><tr><td align=\"CENTER\">".
2284                 get_string("numberofnegativeresponses", "workshop");
2285             echo "</td><td>". get_string("suggestedgrade", "workshop")."</td></tr>\n";
2286             for ($j = 100; $j >= 0; $j--) {
2287                 $numbers[$j] = $j;
2288                 }
2289             for ($i=0; $i<=$workshop->nelements; $i++) {
2290                 if ($i == $negativecount) {
2291                     echo "<tr><td align=\"CENTER\"><img src=\"$CFG->pixpath/t/right.gif\" alt=\"\" /> $i</td><td align=\"center\">{$elements[$i]->maxscore}</td></tr>\n";
2292                     }
2293                 else {
2294                     echo "<tr><td align=\"CENTER\">$i</td><td align=\"CENTER\">{$elements[$i]->maxscore}</td></tr>\n";
2295                     }
2296                 }
2297             echo "</table></center>\n";
2298             echo "<p><center><table cellpadding=\"5\" border=\"1\"><tr><td><b>".get_string("optionaladjustment", 
2299                     "workshop")."</b></td><td>\n";
2300             unset($numbers);
2301             for ($j = 20; $j >= -20; $j--) {
2302                 $numbers[$j] = $j;
2303                 }
2304             if (isset($grades[$workshop->nelements]->grade)) {
2305                 choose_from_menu($numbers, "grade[$workshop->nelements]", $grades[$workshop->nelements]->grade, "");
2306                 }
2307             else {
2308                 choose_from_menu($numbers, "grade[$workshop->nelements]", 0, "");
2309                 }
2310             echo "</td></tr>\n";
2311             break;
2312             
2313         case 3: // criteria grading
2314             echo "<tr valign=\"top\">\n";
2315             echo "  <td bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
2316             echo "  <td bgcolor=\"$THEME->cellheading2\"><b>". get_string("criterion","workshop")."</b></td>\n";
2317             echo "  <td bgcolor=\"$THEME->cellheading2\"><b>".get_string("select", "workshop")."</b></td>\n";
2318             echo "  <td bgcolor=\"$THEME->cellheading2\"><b>".get_string("suggestedgrade", "workshop")."</b></td>\n";
2319             // find which criteria has been selected (saved in the zero element), if any
2320             if (isset($grades[0]->grade)) {
2321                 $selection = $grades[0]->grade;
2322                 }
2323             else {
2324                 $selection = 0;
2325                 }
2326             // now run through the elements
2327             for ($i=0; $i < count($elements); $i++) {
2328                 $iplus1 = $i+1;
2329                 echo "<tr valign=\"top\">\n";
2330                 echo "  <td>$iplus1</td><td>".format_text($elements[$i]->description)."</td>\n";
2331                 if ($selection == $i) {
2332                     echo "  <td align=\"center\"><input type=\"RADIO\" name=\"grade[0]\" value=\"$i\" checked=\"checked\" /></td>\n";
2333                     }
2334                 else {
2335                     echo "  <td align=\"center\"><input type=\"RADIO\" name=\"grade[0]\" value=\"$i\" /></td>\n";
2336                     }
2337                 echo "<td align=\"center\">{$elements[$i]->maxscore}</td></tr>\n";
2338                 }
2339             echo "</table></center>\n";
2340             echo "<p><center><table cellpadding=\"5\" border=\"1\"><tr><td><b>".get_string("optionaladjustment", 
2341                     "workshop")."</b></td><td>\n";
2342             unset($numbers);
2343             for ($j = 20; $j >= -20; $j--) {
2344                 $numbers[$j] = $j;
2345                 }
2346             if (isset($grades[1]->grade)) {
2347                 choose_from_menu($numbers, "grade[1]", $grades[1]->grade, "");
2348                 }
2349             else {
2350                 choose_from_menu($numbers, "grade[1]", 0, "");
2351                 }
2352             echo "</td></tr>\n";
2353             break;
2354             
2355         case 4: // rubric grading
2356             // now run through the elements...
2357             for ($i=0; $i < count($elements); $i++) {
2358                 $iplus1 = $i+1;
2359                 echo "<tr valign=\"top\">\n";
2360                 echo "<td align=\"right\"><b>".get_string("element", "workshop")." $iplus1:</b></td>\n";
2361                 echo "<td>".format_text($elements[$i]->description).
2362                      "<p align=\"right\"><font size=\"1\">".get_string("weight", "workshop").": ".
2363                     number_format($WORKSHOP_EWEIGHTS[$elements[$i]->weight], 2)."</font></td></tr>\n";
2364                 echo "<tr valign=\"top\">\n";
2365                 echo "  <td bgcolor=\"$THEME->cellheading2\" align=\"center\"><b>".get_string("select", "workshop").
2366                     "</b></td>\n";
2367                 echo "  <td bgcolor=\"$THEME->cellheading2\"><b>". get_string("criterion","workshop").
2368                     "</b></td></tr>\n";
2369                 if (isset($grades[$i])) {
2370                     $selection = $grades[$i]->grade;
2371                 } else {
2372                     $selection = 0;
2373                 }
2374                 // ...and the rubrics
2375                 if ($rubricsraw = get_records_select("workshop_rubrics", "workshopid = $workshop->id AND 
2376                         elementno = $i", "rubricno ASC")) {
2377                     unset($rubrics);
2378                     foreach ($rubricsraw as $rubic) {
2379                         $rubrics[] = $rubic;   // to renumber index 0,1,2...
2380                     }
2381                     for ($j=0; $j<5; $j++) {
2382                         if (empty($rubrics[$j]->description)) {
2383                             break; // out of inner for loop
2384                         }
2385                         echo "<tr valign=\"top\">\n";
2386                         if ($selection == $j) {
2387                             echo "  <td align=\"center\"><input type=\"RADIO\" name=\"grade[$i]\" value=\"$j\" 
2388                                 checked=\"checked\" /></td>\n";
2389                         } else {
2390                             echo "  <td align=\"center\"><input type=\"RADIO\" name=\"grade[$i]\" value=\"$j\" 
2391                                 /></td>\n";
2392                         }
2393                         echo "<td>".format_text($rubrics[$j]->description)."</td>\n";
2394                     }
2395                     echo "<tr valign=\"top\">\n";
2396                     echo "  <td align=\"right\"><p><b>". get_string("feedback").":</b></p></td>\n";
2397                     echo "  <td>\n";
2398                     if ($allowchanges) {
2399                         echo "      <textarea name=\"feedback_$i\" rows=\"3\" cols=\"75\" wrap=\"virtual\">\n";
2400                         if (isset($grades[$i]->feedback)) {
2401                             echo $grades[$i]->feedback;
2402                         }
2403                         echo "</textarea>\n";
2404                     } else {
2405                         echo format_text($grades[$i]->feedback);
2406                     }
2407                     echo "  </td>\n";
2408                     echo "</tr>\n";
2410                     // if active and the teacher show stock comments...
2411                     if ($allowchanges and isteacher($course->id, $USER->id)) {
2412                     echo "<tr><td valign=\"top\" align=\"right\"><input type=\"button\" value=\"".
2413                         get_string("addcomment", "workshop")."\" 
2414                         onclick=\"document.assessmentform.action.value='addstockcomment';
2415                         document.assessmentform.elementno.value=$i;document.assessmentform.submit();\" /> \n";
2416                     helpbutton("addcommenttobank", get_string("addcomment", "workshop"), "workshop");
2417                     echo "</td><td>\n";
2418                         if ($stockcomments = get_records_select("workshop_stockcomments", "workshopid = $workshop->id
2419                                     AND elementno = $i", "id")) { // show comments in fixed (creation) order
2420                             foreach ($stockcomments as $stockcomment) {
2421                                 echo "<a onclick=\"document.assessmentform.feedback_$i.value+=' '+'".
2422                                     addslashes($stockcomment->comments).
2423                                     "';\">&lt;&lt;$stockcomment->comments&gt;&gt;</a>\n";
2424                                 if (isteacheredit($course->id, $USER->id)) {
2425                                     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";
2426                                 }
2427                                 echo "<br />\n";
2428                             }
2429                         } 
2430                         echo "</td></tr>\n";
2431                     }
2433                     echo "<tr valign=\"top\">\n";
2434                     echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
2435                     echo "</tr>\n";
2436                 }
2437             }
2438             break;
2439         } // end of outer switch
2440     
2441         // now get the general comment (present in all types)
2442         echo "<tr valign=\"top\">\n";
2443         switch ($workshop->gradingstrategy) {
2444             case 0:
2445             case 1:
2446             case 4 : // no grading, accumulative and rubic
2447                 echo "  <td align=\"right\"><p><b>". get_string("generalcomment", "workshop").":</b></p></td>\n";
2448                 break; 
2449             default : 
2450                 echo "  <td align=\"right\"><p><b>".get_string("generalcomment", "workshop")."/<br />".
2451                     get_string("reasonforadjustment", "workshop").":</b></p></td>\n";
2452         }
2453         echo "  <td>\n";
2454         if ($allowchanges) {
2455             echo "      <textarea name=\"generalcomment\" rows=\"5\" cols=\"75\" wrap=\"virtual\">\n";
2456             if (isset($assessment->generalcomment)) {
2457                 echo $assessment->generalcomment;
2458             }
2459             echo "</textarea>\n";
2460         } else {
2461         if ($assessment) {
2462             if (isset($assessment->generalcomment)) {
2463                 echo format_text($assessment->generalcomment);
2464             }
2465         } else {
2466             print_string("yourfeedbackgoeshere", "workshop");
2467         }
2468     }
2469     echo "&nbsp;</td>\n";
2470     echo "</tr>\n";
2471     // if active and the teacher show stock comments...
2472     if ($allowchanges and isteacher($course->id, $USER->id)) {
2473         echo "<tr><td valign=\"top\" align=\"right\"><input type=\"button\" value=\"".
2474             get_string("addcomment", "workshop")."\" 
2475             onclick=\"document.assessmentform.action.value='addstockcomment';
2476         document.assessmentform.elementno.value=99;document.assessmentform.submit();\" /> \n";
2477         helpbutton("addcommenttobank", get_string("addcomment", "workshop"), "workshop");
2478         echo "</td><td>\n";
2479         if ($stockcomments = get_records_select("workshop_stockcomments", "workshopid = $workshop->id
2480                     AND elementno = 99", "id")) { // show in the same order (oldest at the top)
2481             foreach ($stockcomments as $stockcomment) {
2482                 echo "<a onclick=\"document.assessmentform.generalcomment.value+=' '+'".
2483                     addslashes($stockcomment->comments)."';\">&lt;&lt;$stockcomment->comments&gt;&gt;</a>\n";
2484                 if (isteacheredit($course->id, $USER->id)) {
2485                     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";
2486                 }
2487                 echo "<br />\n";
2488             }
2489         } 
2490         echo "</td></tr>\n";
2491     }
2492     
2493     $timenow = time();
2494     // now show the grading grade if available...
2495     if ($assessment->timegraded) {
2496         echo "<tr valign=\"top\">\n";
2497         echo "<td colspan=\"2\" bgcolor=\"$THEME->cellheading2\" align=\"center\"><b>".
2498             get_string('gradeforstudentsassessment', 'workshop')."</b></td>\n";
2499         echo "</tr>\n";
2500         
2501         if ($assessment->teachercomment) {
2502             echo "<tr valign=top>\n";
2503             echo "  <td align=\"right\"><p><b>". get_string("teacherscomment", "workshop").":</b></p></td>\n";
2504             echo "  <td>\n";
2505             echo text_to_html($assessment->teachercomment);
2506             echo "&nbsp;</td>\n";
2507             echo "</tr>\n";
2508         }
2510         echo "<tr valign=\"top\">\n";
2511         echo "  <td align=\"right\"><p><b>";
2512         print_string('grade', 'workshop');
2513         echo ":</b></p></td><td>\n";
2514         echo number_format($assessment->gradinggrade * $workshop->gradinggrade / 100, 0);
2515         echo "&nbsp;</td>\n";
2516         echo "</tr>\n";
2517     }
2518     
2519     echo "<tr valign=\"top\">\n";
2520     echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
2521     echo "</tr>\n";
2522             
2523     // ...and close the table, show submit button if needed...
2524     echo "</table>\n";
2525     if ($assessment) {
2526         if ($allowchanges) {  
2527             echo "<input type=\"submit\" value=\"".get_string("savemyassessment", "workshop")."\" />\n";
2528         }
2529         // ...if user is author, assessment not agreed, there's no comments, the showcommentlinks flag is set and 
2530         // it's not self assessment then show some buttons!
2531         if (($submission->userid == $USER->id) and !$assessment->timeagreed and !$comments and $showcommentlinks and 
2532                 $submission->userid != $assessment->userid) {
2533             echo "<input type=\"button\" value=\"".get_string("agreetothisassessment", "workshop")."\" 
2534                 onclick=\"document.assessmentform.action.value='agreeassessment';document.assessmentform.submit();\" />\n";
2535             echo "<input type=\"submit\" value=\"".get_string("disagreewiththisassessment", "workshop")."\"
2536                 onclick=\"document.assessmentform.action.value='addcomment';document.assessmentform.submit();\" />\n";
2537         }
2538     }
2539     echo "</center>";
2540     echo "</form>\n";
2544 //////////////////////////////////////////////////////////////////////////////////////
2545 function workshop_print_assessments_by_user_for_admin($workshop, $user) {
2547     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
2548         error("Course Module ID was incorrect");
2549     }
2551     if ($assessments = workshop_get_user_assessments_done($workshop, $user)) {
2552         foreach ($assessments as $assessment) {
2553             workshop_print_assessment($workshop, $assessment);
2554             echo "<p align=\"right\">".
2555                 '<a href="viewassessment.php?&amp;id='.$cm->id.'&amp;stype=student&amp;aid='.$assessment->id.'">'.
2556                 get_string('assessthisassessment', 'workshop').'</a> | '.
2557                 "<a href=\"assessments.php?action=confirmdelete&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=confirmdelete&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) {
2587     global $CFG;
2589     if (! $course = get_record("course", "id", $workshop->course)) {
2590         error("Course is misconfigured");
2591     }
2592     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $course->id)) {
2593         error("Course Module ID was incorrect");
2594     }
2595     // print standard assignment heading
2596     print_simple_box_start("center");
2597     print_heading($workshop->name, "center");
2598     print_simple_box_start("center");
2600     // print phase and date info
2601     $string = '<b>'.get_string('currentphase', 'workshop').'</b>: '.workshop_phase($workshop).'<br />';
2602     $dates = array(
2603         'submissionstart' => $workshop->submissionstart,
2604         'submissionend' => $workshop->submissionend,
2605         'assessmentstart' => $workshop->assessmentstart,
2606         'assessmentend' => $workshop->assessmentend
2607     );
2608     foreach ($dates as $type => $date) {
2609         if ($date) {
2610             $strdifference = format_time($date - time());
2611             if (($date - time()) < 0) {
2612                 $strdifference = "<font color=\"red\">$strdifference</font>";
2613             }
2614             $string .= '<b>'.get_string($type, 'workshop').'</b>: '.userdate($date)." ($strdifference)<br />";
2615         }
2616     }
2617     echo $string;
2619     $grade = $workshop->gradinggrade + $workshop->grade;
2620     echo "<br /><b>".get_string("maximumgrade")."</b>: $grade  ";
2621     // print link to specimen assessment form
2622     echo "(<a href=\"assessments.php?id=$cm->id&amp;action=displaygradingform\">".
2623         get_string("specimenassessmentform", "workshop")."</a>";
2624     // print edit icon
2625     if (isteacheredit($course->id) and $workshop->nelements) {
2626         echo " <a href=\"assessments.php?id=$cm->id&action=editelements\">".
2627              "<img src=\"$CFG->pixpath/t/edit.gif\" ".
2628              'height="11" width="11" border="0" alt="'.get_string('amendassessmentelements', 'workshop').'"></a>';
2629     }
2630     echo ")<br />";
2631     print_simple_box_end();
2632     echo "<br />";
2633     echo format_text($workshop->summary, $workshop->format);
2634     print_simple_box_end();
2635     echo "<br />";  
2636     }
2639 //////////////////////////////////////////////////////////////////////////////////////
2640 function workshop_print_difference($time) {
2641     if ($time < 0) {
2642         $timetext = get_string("late", "assignment", format_time($time));
2643         return " (<font color=\"red\">$timetext</font>)";
2644     } else {
2645         $timetext = get_string("early", "assignment", format_time($time));
2646         return " ($timetext)";
2647     }
2651 //////////////////////////////////////////////////////////////////////////////////////
2652 function workshop_print_feedback($course, $submission) {
2653     global $CFG, $THEME, $RATING;
2655     if (! $teacher = get_record("user", "id", $submission->teacher)) {
2656         error("Weird workshop error");
2657     }
2659     echo "\n<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\" align=\"center\"><tr><td bgcolor=#888888>";
2660     echo "\n<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\">";
2662     echo "\n<tr>";
2663     echo "\n<td rowspan=\"3\" bgcolor=\"$THEME->body\" width=\"35\" valign=\"top\">";
2664     print_user_picture($teacher->id, $course->id, $teacher->picture);
2665     echo "</td>";
2666     echo "<td nowrap=\"nowrap\" width=\"100%\" bgcolor=\"$THEME->cellheading\">".fullname($teacher);
2667     echo "&nbsp;&nbsp;<font size=\"2\"><i>".userdate($submission->timemarked)."</i>";
2668     echo "</tr>";
2670     echo "\n<tr><td width=\"100%\" bgcolor=\"$THEME->cellcontent\">";
2672     echo "<p align=\"right\"><font size=\"-1\"><i>";
2673     if ($submission->grade) {
2674         echo get_string("grade").": $submission->grade";
2675     } else {
2676         echo get_string("nograde");
2677     }
2678     echo "</i></font></p>";
2680     echo format_text($submission->assessorcomment);
2681     echo "</td></tr></table>";
2682     echo "</td></tr></table>";
2686 //////////////////////////////////////////////////////////////////////////////////////
2687 function workshop_print_key($workshop) {
2688     // print an explaination of the grades
2689     
2690     if (!$course = get_record("course", "id", $workshop->course)) {
2691         error("Print key: course not found");
2692     }
2693     echo "<div align=\"center\">\n";
2694     echo "<p><small>{} ".get_string("assessmentby", "workshop", $course->student).";&nbsp;&nbsp;\n";
2695     echo "[] ".get_string("assessmentby", "workshop", $course->teacher).";&nbsp;&nbsp;\n";
2696     echo "&lt;&gt; ".get_string("assessmentdropped", "workshop").";\n";
2697     if ($workshop->agreeassessments) echo "&lt;&lt;&gt;&gt; ".get_string("assessmentnotyetagreed", "workshop").";\n";
2698     echo "<br />() ".get_string("automaticgradeforassessment", "workshop").";&nbsp;&nbsp;\n";
2699     echo "[] ".get_string("teachergradeforassessment", "workshop", $course->teacher).".\n";
2700     echo "<br />".get_string("gradesforsubmissionsare", "workshop", $workshop->grade).";&nbsp;&nbsp;\n";
2701     echo get_string("gradesforassessmentsare", "workshop", $workshop->gradinggrade).".</small></p>\n";
2702     echo "</div>\n";
2703     return;    
2705     
2707 //////////////////////////////////////////////////////////////////////////////////////
2708 function workshop_print_league_table($workshop) {
2709     // print an order table of (student) submissions showing teacher's and student's assessments
2710     
2711     if (! $course = get_record("course", "id", $workshop->course)) {
2712         error("Print league table: Course is misconfigured");
2713     }
2714     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
2715             error("Course Module ID was incorrect");
2716     }
2717     // set $groupid if workshop is in SEPARATEGROUPS mode
2718     if (groupmode($course, $cm) == SEPARATEGROUPS) {
2719         $groupid = get_current_group($course->id);
2720     } else {
2721         $groupid = 0;
2722     }
2723  
2724     $nentries = $workshop->showleaguetable;
2725     if ($workshop->anonymous and isstudent($course->id)) {
2726         $table->head = array (get_string("title", "workshop"), 
2727             get_string("teacherassessments", "workshop", $course->teacher),  
2728             get_string("studentassessments", "workshop",    $course->student), get_string("overallgrade", "workshop"));
2729         $table->align = array ("left",  "center", "center", "center");
2730         $table->size = array ("*", "*", "*", "*");
2731     }
2732     else { // show names
2733         $table->head = array (get_string("title", "workshop"),  get_string("name"),
2734             get_string("teacherassessments", "workshop", $course->teacher),  
2735             get_string("studentassessments", "workshop",    $course->student), get_string("overallgrade", "workshop"));
2736         $table->align = array ("left", "left", "center", "center", "center");
2737         $table->size = array ("*", "*", "*", "*", "*");
2738     }
2739     $table->cellpadding = 2;
2740     $table->cellspacing = 0;
2742     if ($submissions = workshop_get_student_submissions($workshop)) {
2743         foreach ($submissions as $submission) {
2744             if ($groupid) {
2745                 // check submission's group
2746                 if (!ismember($groupid, $submission->userid)) {
2747                     continue; // skip this submission
2748                 }
2749             }
2750             $grades[$submission->id] = workshop_submission_grade($workshop, $submission);
2751         }
2752         arsort($grades); // largest grade first
2753         reset($grades);
2754         $n = 1;
2755         while (list($submissionid, $grade) = each($grades)) {
2756             if (!$submission = get_record("workshop_submissions", "id", $submissionid)) {
2757                 error("Print league table: submission not found");
2758             }
2759             if (!$user = get_record("user", "id", $submission->userid)) {
2760                 error("Print league table: user not found");
2761             }
2762             if ($workshop->anonymous and isstudent($course->id)) {
2763                 $table->data[] = array(workshop_print_submission_title($workshop, $submission),
2764                         workshop_print_submission_assessments($workshop, $submission, "teacher"),
2765                         workshop_print_submission_assessments($workshop, $submission, "student"), $grade);
2766             }
2767             else {
2768                 $table->data[] = array(workshop_print_submission_title($workshop, $submission), fullname($user),
2769                         workshop_print_submission_assessments($workshop, $submission, "teacher"),
2770                         workshop_print_submission_assessments($workshop, $submission, "student"), $grade);
2771             }
2772             $n++;
2773             if ($n > $nentries) {
2774                 break;
2775             }
2776         }
2777         print_heading(get_string("leaguetable", "workshop"));
2778         print_table($table);
2779         workshop_print_key($workshop);
2780     }
2782     
2784 //////////////////////////////////////////////////////////////////////////////////////
2785 function workshop_print_submission($workshop, $submission) {
2786     // prints the submission with optional attachments
2787     global $CFG;
2789     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
2790             error("Course Module ID was incorrect");
2791     }
2792     print_simple_box(format_text($submission->description), 'center');
2793     if ($workshop->nattachments) {
2794         $n = 1;
2795         echo "<table align=\"center\">\n";
2796         $filearea = workshop_file_area_name($workshop, $submission);
2797         if ($basedir = workshop_file_area($workshop, $submission)) {
2798             if ($files = get_directory_list($basedir)) {
2799                 foreach ($files as $file) {
2800                     $icon = mimeinfo("icon", $file);
2801                     if ($CFG->slasharguments) {
2802                         $ffurl = "file.php/$filearea/$file";
2803                     } else {
2804                         $ffurl = "file.php?file=/$filearea/$file";
2805                     }
2806                     echo "<tr><td><b>".get_string("attachment", "workshop")." $n:</b> \n";
2807                     echo "<img src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\" 
2808                         border=\"0\" alt=\"File\" />".
2809                         "&nbsp;<a target=\"uploadedfile\" href=\"$CFG->wwwroot/$ffurl\">$file</a></td></tr>";
2810                     $n++;
2811                 }
2812             }
2813         }
2814         echo "</table>\n";
2815     }
2816     return;
2820 //////////////////////////////////////////////////////////////////////////////////////
2821 function workshop_print_submission_assessments($workshop, $submission, $type) {
2822     global $USER, $CFG;
2823     // Returns the teacher or peer grade and a hyperlinked list of grades for this submission
2824  
2825     $str = '';
2826     // get the assessments in grade order, highest first
2827     if ($assessments = workshop_get_assessments($submission, "", "grade DESC")) {
2828         if ($type == 'teacher' or $type == 'all') {
2829             // students can see teacher assessments only if the release date has passed
2830             $timenow = time();
2831             if (isteacher($workshop->course, $USER->id) or ($timenow > $workshop->releasegrades)) {
2832                 foreach ($assessments as $assessment) {
2833                     if (isteacher($workshop->course, $assessment->userid)) {
2834                         if ($type == 'all') {
2835                             $str .= workshop_fullname($assessment->userid, $workshop->course).': ';
2836                         }
2837                         $str .= "<a href=\"viewassessment.php?aid=$assessment->id\">"
2838                              . "[".number_format($assessment->grade *$workshop->grade / 100, 0)."]</a>";
2839                         if (isteacher($workshop->course, $USER->id)) {
2840                             $str .= ' <a title="'.get_string('reassess', 'workshop').
2841                                 "\" href=\"assess.php?sid=$submission->id\"><img src=\"$CFG->pixpath/t/edit.gif\" ".
2842                                 ' height="11" width="11" border="0" alt="'.get_string('reassess', 'workshop').'"></a>';
2843                             $str .= ' <a title="'.get_string('delete', 'workshop').
2844                                 "\" href=\"assessments.php?action=confirmdelete&amp;wid=$workshop->id&amp;aid=$assessment->id\"><img src=\"$CFG->pixpath/t/delete.gif\" ".
2845                                 ' height="11" width="11" border="0" alt="'.get_string('delete', 'workshop').'"></a><br />';
2846                        }
2847                     }
2848                 }
2849             }
2850         }
2851         if ($type == 'student' or $type == 'all') {
2852             foreach ($assessments as $assessment) {
2853                 if (isstudent($workshop->course, $assessment->userid)) {
2854                     if ($type == 'all') {
2855                         $str .= workshop_fullname($assessment->userid, $workshop->course).': ';
2856                     }
2857                     $str .= "<a href=\"viewassessment.php?aid=$assessment->id\">";
2858                     if ($workshop->agreeassessments and !$assessment->timeagreed and 
2859                             isstudent($workshop->course, $submission->userid)) { // agreement on student work only
2860                         $str .= "&lt;&lt;".number_format($assessment->grade * $workshop->grade / 100, 0)." (".
2861                             number_format($assessment->gradinggrade * $workshop->gradinggrade / 100, 0).
2862                             ")&gt;&gt;</a> ";
2863                     } elseif ($assessment->timegraded) {
2864                         if ($assessment->gradinggrade) {
2865                             $str .= "{".number_format($assessment->grade * $workshop->grade / 100, 0);
2866                             if ($assessment->teachergraded) {
2867                                 $str .= " [".number_format($assessment->gradinggrade * $workshop->gradinggrade / 100, 0).
2868                                 "]}</a> ";
2869                             } else {
2870                                 $str .= " (".number_format($assessment->gradinggrade * $workshop->gradinggrade / 100, 0).
2871                                 ")}</a> ";
2872                             }
2873                         } else {
2874                             $str .= "&lt;".number_format($assessment->grade * $workshop->grade / 100, 0).
2875                                 " (0)&gt;</a> ";
2876                         }
2877                     } else {
2878                         $str .= "{".number_format($assessment->grade * $workshop->grade / 100, 0)."}</a> ";
2879                     }
2880                     $str .= '<br />';
2881                 }
2882             }
2883         }
2884     }
2885     if (!$str) {
2886         $str = "&nbsp;";   // be kind to Mozilla browsers!
2887     }
2888     return $str;
2892 //////////////////////////////////////////////////////////////////////////////////////
2893 function workshop_print_submission_title($workshop, $submission) {
2894 // Arguments are objects
2896     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
2897             error("Course Module ID was incorrect");
2898     }
2899     
2900     if (!$submission->timecreated) { // a "no submission"
2901         return $submission->title;
2902     }
2903     return "<a name=\"sid=$submission->id\" href=\"submissions.php?id=$cm->id&action=showsubmission&sid=$submission->id\">$submission->title</a>";
2908 function workshop_print_time_to_deadline($time) {
2909     if ($time < 0) {
2910         $timetext = get_string("afterdeadline", "workshop", format_time($time));
2911         return " (<font color=\"red\">$timetext</font>)";
2912     } else {
2913         $timetext = get_string("beforedeadline", "workshop", format_time($time));
2914         return " ($timetext)";
2915     }
2919 //////////////////////////////////////////////////////////////////////////////////////
2920 function workshop_print_upload_form($workshop) {
2921     global $CFG;
2923     if (! $course = get_record("course", "id", $workshop->course)) {
2924         error("Course is misconfigured");
2925     }
2926     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $course->id)) {
2927         error("Course Module ID was incorrect");
2928     }
2929     $usehtmleditor = can_use_html_editor();
2931     echo "<div align=\"center\">";
2932     echo "<form enctype=\"multipart/form-data\" method=\"POST\" action=\"upload.php\">";
2933     echo " <input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
2934     echo "<table celpadding=\"5\" border=\"1\" align=\"center\">\n";
2935     // now get the submission
2936     echo "<tr valign=\"top\"><td><b>". get_string("title", "workshop").":</b>\n";
2937     echo "<input type=\"text\" name=\"title\" size=\"60\" maxlength=\"100\" value=\"\" />\n";
2938     echo "</td></tr><tr><td><b>".get_string("submission", "workshop").":</b><br />\n";
2939     print_textarea($usehtmleditor, 25,70, 630, 400, "description");
2940     use_html_editor("description");
2941     echo "</td></tr><tr><td>\n";
2942     if ($workshop->nattachments) {
2943         require_once($CFG->dirroot.'/lib/uploadlib.php');
2944         for ($i=0; $i < $workshop->nattachments; $i++) {
2945             $iplus1 = $i + 1;
2946             $tag[$i] = get_string("attachment", "workshop")." $iplus1:";
2947         }
2948         upload_print_form_fragment($workshop->nattachments,null,$tag,false,null,$course->maxbytes,
2949                 $workshop->maxbytes,false);
2950     }
2951     echo "</td></tr></table>\n";
2952     echo " <input type=\"submit\" name=\"save\" value=\"".get_string("submitassignment","workshop")."\" />";
2953     echo "</form>";
2954     echo "</div>";
2958 //////////////////////////////////////////////////////////////////////////////////////
2959 function workshop_print_user_assessments($workshop, $user, &$gradinggrade) {
2960     // Returns the number of assessments and a hyperlinked list of grading grades for the assessments made by this user
2962     if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
2963             error("Course Module ID was incorrect");
2964     }
2965     $gradinggrade = 0;
2966     $n = 0;
2967     $str = '';
2968     if ($assessments = workshop_get_user_assessments_done($workshop, $user)) {
2969         foreach ($assessments as $assessment) {
2970             $gradinggrade += $assessment->gradinggrade;
2971             $n++;
2972             $str .= "<a href=\"viewassessment.php?aid=$assessment->id\">";
2973             if ($assessment->timegraded) {
2974                 if ($assessment->gradinggrade) {
2975                     $str .= "{".number_format($assessment->grade * $workshop->grade / 100, 0);
2976                     if ($assessment->teachergraded) {
2977                         $str .= " [".number_format($assessment->gradinggrade * $workshop->gradinggrade / 100)."]}</a> ";
2978                     } else {
2979                         $str .= " (".number_format($assessment->gradinggrade * $workshop->gradinggrade / 100).")}</a> ";
2980                     }
2981                 } else {
2982                     $str .= "&lt;".number_format($assessment->grade * $workshop->grade / 100, 0)." (0)&gt;</a> ";
2983                 }
2984             } else {
2985                 $str .= "{".number_format($assessment->grade * $workshop->grade / 100, 0)." (-)}</a> ";
2986             }
2987             $str .= '<br />';
2988         }
2989     }
2990     else {
2991         $str ="0";
2992     }
2993     if ($n = max($n, $workshop->ntassessments + $workshop->nsassessments)) {
2994         $gradinggrade = number_format($gradinggrade/$n * $workshop->gradinggrade / 100, 1);
2995     }
2996     
2997     return $str;
3001 //////////////////////////////////////////////////////////////////////////////////////
3002 function workshop_test_user_assessments($workshop, $user) {
3003     // see if user has assessed required number of assessments of teachers submissions...
3004     global $CFG;
3005     
3006     $result = true;
3007     $n = 0;
3008     $timenow =time();
3009     if ($submissions = workshop_get_teacher_submissions($workshop)) {
3010         foreach ($submissions as $submission) {
3011             if ($assessment = workshop_get_submission_assessment($submission, $user)) {
3012                 // ...the date stamp on the assessment should be in the past 
3013                 if ($assessment->timecreated < $timenow) {
3014                     $n++;
3015                 }
3016             }
3017         }
3018         if ($n < min($workshop->ntassessments, workshop_count_teacher_submissions($workshop))) {
3019             $result = false; 
3020         }
3021     }
3022     return $result;
3024     
3026 ?>