More changes to the user interface. See http://moodle.org/mod/forum/view.php?id=740.
[moodle.git] / mod / workshop / assessments.php
1 <?php  // $Id: lib.php,v 1.1 22 Aug 2003
3 /*************************************************
4     ACTIONS handled are:
6     addcomment
7     addstockcomment
8     confirmdelete
9     delete
10     adminlist
11     agreeassessment
12     displaygradingform
13     editcomment
14     editelements (teachers only)
15     gradeallassessments (teachers only)
16     gradeassessment (teachers only)
17     insertcomment
18     insertelements (for teachers)
19     listungradedstudentsubmissions (for teachers)
20     listungradedteachersubmissions (for teachers)
21     listteachersubmissions
22     regradestudentassessments (for teachers)
23     updateassessment
24     updatecomment
25     updategrading
27 ************************************************/
29     require("../../config.php");
30     require("lib.php"); 
31     require("locallib.php");
33     optional_variable($id, '');    // Course Module ID
34     optional_variable($wid, '');    // Workshop ID
36     // get some useful stuff...
37     if ($id) {
38         if (! $cm = get_record("course_modules", "id", $id)) {
39             error("Course Module ID was incorrect");
40         }
41         if (! $workshop = get_record("workshop", "id", $cm->instance)) {
42             error("Course module is incorrect");
43         }
44     } else if ($wid) {
45         if (! $workshop = get_record("workshop", "id", $wid)) {
46             error("Workshop id is incorrect");
47         }
48         if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course)) {
49             error("No coursemodule found");
50         }
51     } else {
52         error("No id given");
53     }
54     if (! $course = get_record("course", "id", $cm->course)) {
55         error("Course is misconfigured");
56     }
58     require_login($course->id);
59     
60     $navigation = "";
61     if ($course->category) {
62         $navigation = "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->";
63     }
65     $strworkshops = get_string("modulenameplural", "workshop");
66     $strworkshop  = get_string("modulename", "workshop");
67     $strassessments = get_string("assessments", "workshop");
69     // ... print the header and...
70     print_header_simple("$workshop->name", "",
71                  "<a href=\"index.php?id=$course->id\">$strworkshops</a> -> 
72                   <a href=\"view.php?id=$cm->id\">$workshop->name</a> -> $strassessments", 
73                   "", "", true);
75     //...get the action 
76     require_variable($action);
77     
79     /*************** add comment to assessment (by author, assessor or teacher) ***************************/
80     if ($action == 'addcomment') {
81         
82         print_heading_with_help(get_string("addacomment", "workshop"), "addingacomment", "workshop");
83         // get assessment record
84         if (!$assessmentid = $_REQUEST['aid']) { // comes from link or hidden form variable
85             error("Assessment id not given");
86         }
87         $assessment = get_record("workshop_assessments", "id", $assessmentid);
88         if (!$submission = get_record("workshop_submissions", "id", $assessment->submissionid)) {
89             error("Submission not found");
90         }
91         ?>
92         <form name="commentform" action="assessments.php" method="post">
93         <input type="hidden" name="action" value="insertcomment" />
94         <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
95         <input type="hidden" name="aid" value="<?php echo $_REQUEST['aid'] ?>" />
96         <center>
97         <table cellpadding="5" border="1">
98         <?php
100         // now get the comment
101         echo "<tr valign=\"top\">\n";
103         echo "  <td align=\"right\"><b>". get_string("comment", "workshop").":</b></td>\n";
105         echo "  <td>\n";
107         echo "      <textarea name=\"comments\" rows=\"5\" cols=\"75\">\n";
108         echo "</textarea>\n";
110         echo "  </td></tr></table>\n";
111         echo "<input type=\"submit\" value=\"".get_string("savemycomment", "workshop")."\" />\n";
112         echo "</center></form>\n";
113         echo "<center><b>".get_string("assessment", "workshop"). "</b></center>\n";
114         workshop_print_assessment($workshop, $assessment);
115     }
118     /*************** add stock comment (by teacher ) ***************************/
119     elseif ($action == 'addstockcomment') {
121         require_variable($aid);
122         require_variable($elementno);
124         if (!isteacher($course->id)) {
125             error("Only teachers can look at this page");
126         }
127     
128         if (!$assessment = get_record("workshop_assessments", "id", $aid)) {
129             error("workshop assessment is misconfigured");
130         }
131         $form = data_submitted("nomatch"); // probably always come from the same page, change this statement
132     
133         // store the comment in the stock comments table
134         if ($elementno == 99) { // it's the general comment
135             $form->feedback_99 = $form->generalcomment;
136         }
137         $comment->workshopid = $workshop->id;
138         $comment->elementno = $elementno;
139         $comment->comments = $form->{"feedback_$elementno"};
140         if (!(trim($comment->comments))) {
141             // no comment given - just redisplay assessment form
142             workshop_print_assessment($workshop, $assessment, true, true, $form->returnto);
143             print_footer($course);
144             exit();
145         }
146   
147         if (!$element->id = insert_record("workshop_stockcomments", $comment)) {
148             error("Could not insert comment into comment bank");
149         }
151         // now upate the assessment (just the elements, the assessment itself is not updated)
153         // first get the assignment elements for maxscores and weights...
154         $elementsraw = get_records("workshop_elements", "workshopid", $workshop->id, "elementno ASC");
155         if (count($elementsraw) < $workshop->nelements) {
156             print_string("noteonassignmentelements", "workshop");
157         }
158         if ($elementsraw) {
159             foreach ($elementsraw as $element) {
160                 $elements[] = $element;   // to renumber index 0,1,2...
161             }
162         } else {
163             $elements = null;
164         }
166         $timenow = time();
167         // don't fiddle about, delete all the old and add the new!
168         delete_records("workshop_grades", "assessmentid",  $assessment->id);
169         
170     
171         //determine what kind of grading we have
172         switch ($workshop->gradingstrategy) {
173             case 0: // no grading
174                 // Insert all the elements that contain something
175                 for ($i = 0; $i < $workshop->nelements; $i++) {
176                     unset($element);
177                     $element->workshopid = $workshop->id;
178                     $element->assessmentid = $assessment->id;
179                     $element->elementno = $i;
180                     $element->feedback   = $form->{"feedback_$i"};
181                     if (!$element->id = insert_record("workshop_grades", $element)) {
182                         error("Could not insert workshop grade!");
183                     }
184                 }
185                 $grade = 0; // set to satisfy save to db
186                 break;
187                 
188             case 1: // accumulative grading
189                 // Insert all the elements that contain something
190                 foreach ($form->grade as $key => $thegrade) {
191                     unset($element);
192                     $element->workshopid = $workshop->id;
193                     $element->assessmentid = $assessment->id;
194                     $element->elementno = $key;
195                     $element->feedback   = $form->{"feedback_$key"};
196                     $element->grade = $thegrade;
197                     if (!$element->id = insert_record("workshop_grades", $element)) {
198                         error("Could not insert workshop grade!");
199                         }
200                     }
201                 // now work out the grade...
202                 $rawgrade=0;
203                 $totalweight=0;
204                 foreach ($form->grade as $key => $grade) {
205                     $maxscore = $elements[$key]->maxscore;
206                     $weight = $WORKSHOP_EWEIGHTS[$elements[$key]->weight];
207                     if ($weight > 0) { 
208                         $totalweight += $weight;
209                     }
210                     $rawgrade += ($grade / $maxscore) * $weight;
211                     // echo "\$key, \$maxscore, \$weight, \$totalweight, \$grade, \$rawgrade : $key, $maxscore, $weight, $totalweight, $grade, $rawgrade<br />";
212                 }
213                 $grade = 100.0 * ($rawgrade / $totalweight);
214                 break;
216             case 2: // error banded graded
217                 // Insert all the elements that contain something
218                 $error = 0.0; 
219                 for ($i =0; $i < $workshop->nelements; $i++) {
220                     unset($element);
221                     $element->workshopid = $workshop->id;
222                     $element->assessmentid = $assessment->id;
223                     $element->elementno = $i;
224                     $element->feedback   = $form->{"feedback_$i"};
225                     $element->grade = $form->grade[$i];
226                     if (!$element->id = insert_record("workshop_grades", $element)) {
227                         error("Could not insert workshop grade!");
228                     }
229                     if (empty($form->grade[$i])){
230                         $error += $WORKSHOP_EWEIGHTS[$elements[$i]->weight];
231                     }
232                 }
233                 // now save the adjustment
234                 unset($element);
235                 $i = $workshop->nelements;
236                 $element->workshopid = $workshop->id;
237                 $element->assessmentid = $assessment->id;
238                 $element->elementno = $i;
239                 $element->grade = $form->grade[$i];
240                 if (!$element->id = insert_record("workshop_grades", $element)) {
241                     error("Could not insert workshop grade!");
242                 }
243                 $grade = ($elements[intval($error + 0.5)]->maxscore + $form->grade[$i]) * 100 / $workshop->grade;
244                 // do sanity check
245                 if ($grade < 0) {
246                     $grade = 0;
247                 } elseif ($grade > 100) {
248                     $grade = 100;
249                 }
250                 echo "<b>".get_string("weightederrorcount", "workshop", intval($error + 0.5))."</b>\n";
251                 break;
252             
253             case 3: // criteria grading
254                 // save in the selected criteria value in element zero, 
255                 unset($element);
256                 $element->workshopid = $workshop->id;
257                 $element->assessmentid = $assessment->id;
258                 $element->elementno = 0;
259                 $element->grade = $form->grade[0];
260                 if (!$element->id = insert_record("workshop_grades", $element)) {
261                     error("Could not insert workshop grade!");
262                 }
263                 // now save the adjustment in element one
264                 unset($element);
265                 $element->workshopid = $workshop->id;
266                 $element->assessmentid = $assessment->id;
267                 $element->elementno = 1;
268                 $element->grade = $form->grade[1];
269                 if (!$element->id = insert_record("workshop_grades", $element)) {
270                     error("Could not insert workshop grade!");
271                 }
272                 $grade = ($elements[$form->grade[0]]->maxscore + $form->grade[1]);
273                 break;
275             case 4: // rubric grading (identical to accumulative grading)
276                 // Insert all the elements that contain something
277                 foreach ($form->grade as $key => $thegrade) {
278                     unset($element);
279                     $element->workshopid = $workshop->id;
280                     $element->assessmentid = $assessment->id;
281                     $element->elementno = $key;
282                     $element->feedback   = $form->{"feedback_$key"};
283                     $element->grade = $thegrade;
284                     if (!$element->id = insert_record("workshop_grades", $element)) {
285                         error("Could not insert workshop grade!");
286                     }
287                 }
288                 // now work out the grade...
289                 $rawgrade=0;
290                 $totalweight=0;
291                 foreach ($form->grade as $key => $grade) {
292                     $maxscore = $elements[$key]->maxscore;
293                     $weight = $WORKSHOP_EWEIGHTS[$elements[$key]->weight];
294                     if ($weight > 0) { 
295                         $totalweight += $weight;
296                     }
297                     $rawgrade += ($grade / $maxscore) * $weight;
298                 }
299                 $grade = 100.0 * ($rawgrade / $totalweight);
300                 break;
302         } // end of switch
303             
304     
305         // any comment?
306         if (!empty($form->generalcomment)) { // update the object (no need to update the db record)
307             $assessment->generalcomment = $form->generalcomment;
308         }
309         
310         // redisplay form, going back to original returnto address
311         workshop_print_assessment($workshop, $assessment, true, true, $form->returnto);
312         
313         // add_to_log($course->id, "workshop", "assess", "viewassessment.php?id=$cm->id&amp;aid=$assessment->id", "$assessment->id", "$cm->id");
314         
315     }
318     /******************* confirm delete ************************************/
319     elseif ($action == 'confirmdelete' ) {
321         if (empty($_GET['aid'])) {
322             error("Confirm delete: assessment id missing");
323             }
324             
325         notice_yesno(get_string("confirmdeletionofthisitem","workshop", get_string("assessment", "workshop")), 
326              "assessments.php?action=delete&amp;id=$cm->id&amp;aid=$_GET[aid]", "submissions.php?action=adminlist&amp;id=$cm->id");
327         }
328     
330     /******************* delete ************************************/
331     elseif ($action == 'delete' ) {
333         if (empty($_GET['aid'])) {
334             error("Delete: submission id missing");
335             }
336             
337         print_string("deleting", "workshop");
338         // first delete all the associated records...
339         delete_records("workshop_comments", "assessmentid", $_GET['aid']);
340         delete_records("workshop_grades", "assessmentid", $_GET['aid']);
341         // ...now delete the assessment...
342         delete_records("workshop_assessments", "id", $_GET['aid']);
343         
344         print_continue("view.php?id=$cm->id");
345         }
346     
348     /*********************** admin list of asssessments (of a submission) (by teachers)**************/
349     elseif ($action == 'adminlist') {
351         if (!isteacher($course->id)) {
352             error("Only teachers can look at this page");
353             }
354             
355         if (empty($_GET['sid'])) {
356             error ("Workshop asssessments: adminlist called with no sid");
357             }
358         $submission = get_record("workshop_submissions", "id", $_GET['sid']);
359         workshop_print_assessments_for_admin($workshop, $submission);
360         print_continue("submissions.php?action=adminlist&amp;id=$cm->id");
361         }
364     /*********************** admin list of asssessments by a student (used by teachers only )******************/
365     elseif ($action == 'adminlistbystudent') {
367         if (!isteacher($course->id)) {
368             error("Only teachers can look at this page");
369             }
370             
371         if (empty($_GET['userid'])) {
372             error ("Workshop asssessments: adminlistbystudent called with no userid");
373             }
374         $user = get_record("user", "id", $_GET['userid']);
375         workshop_print_assessments_by_user_for_admin($workshop, $user);
376         print_continue("submissions.php?action=adminlist&amp;id=$cm->id");
377         }
380     /*************** agree (to) assessment (by student) ***************************/
381     elseif ($action == 'agreeassessment') {
382         $timenow = time();
383         // assessment id comes from link or hidden form variable
384         if (!$assessment = get_record("workshop_assessments", "id", $_REQUEST['aid'])) { 
385             error("Assessment : agree assessment failed");
386             }
387         //save time of agreement
388         set_field("workshop_assessments", "timeagreed", $timenow, "id", $assessment->id);
389         echo "<centre><b>".get_string("savedok", "workshop")."</b></center><br />\n";
390             
391         add_to_log($course->id, "workshop", "agree", "viewassessment.php?id=$cm->id&amp;aid=$assessment->id", "$assessment->id");
392         print_continue("view.php?id=$cm->id");
393         }
397     /*************** display grading form (viewed by student) *********************************/
398     elseif ($action == 'displaygradingform') {
400         print_heading_with_help(get_string("specimenassessmentform", "workshop"), "specimen", "workshop");
401     
402         workshop_print_assessment($workshop); // called with no assessment
403         print_continue("view.php?id=$cm->id");
404     }
407     /*************** edit comment on assessment (by author, assessor or teacher) ***************************/
408     elseif ($action == 'editcomment') {
409         
410         print_heading_with_help(get_string("editacomment", "workshop"), "editingacomment", "workshop");
411         // get the comment record...
412         if (!$comment = get_record("workshop_comments", "id", $_GET['cid'])) {
413             error("Edit Comment: Comment not found");
414             }
415         if (!$assessment = get_record("workshop_assessments", "id", $comment->assessmentid)) {
416             error("Edit Comment: Assessment not found");
417             }
418         if (!$submission = get_record("workshop_submissions", "id", $assessment->submissionid)) {
419             error("Edit Comment: Submission not found");
420             }
421         ?>
422         <form name="gradingform" action="assessments.php" method="post">
423         <input type="hidden" name="action" value="updatecomment" />
424         <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
425         <input type="hidden" name="cid" value="<?php echo $_GET['cid'] ?>" />
426         <center>
427         <table cellpadding="5" border="1">
428         <?php
430         // now show the comment
431         echo "<tr valign=\"top\">\n";
432         echo "  <td align=\"right\"><b>". get_string("comment", "workshop").":</b></td>\n";
433         echo "  <td>\n";
434         echo "      <textarea name=\"comments\" rows=\"5\" cols=\"75\">\n";
435         if (isset($comment->comments)) {
436             echo $comment->comments;
437             }
438         echo "      </textarea>\n";
439         echo "  </td></tr></table>\n";
440         echo "<input type=\"submit\" value=\"".get_string("savemycomment", "workshop")."\" />\n";
441         echo "</center></form>\n";
442         workshop_print_assessment($workshop, $assessment);
443         }
446     /*********************** edit assessment elements (for teachers) ***********************/
447     elseif ($action == 'editelements') {
449         if (!isteacher($course->id)) {
450             error("Only teachers can look at this page");
451         }
453         $count = count_records("workshop_grades", "workshopid", $workshop->id);
454         if ($count) {
455             notify(get_string("warningonamendingelements", "workshop"));
456         }
457         // set up heading, form and table
458         print_heading_with_help(get_string("editingassessmentelements", "workshop"), "elements", "workshop");
459         ?>
460         <form name="form" method="post" action="assessments.php">
461         <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
462         <input type="hidden" name="action" value="insertelements" />
463         <center><table cellpadding="5" border="1">
464         <?php
465         
466         // get existing elements, if none set up appropriate default ones
467         if ($elementsraw = get_records("workshop_elements", "workshopid", $workshop->id, "elementno ASC" )) {
468             foreach ($elementsraw as $element) {
469                 $elements[] = $element;   // to renumber index 0,1,2...
470             }
471         }
472         // check for missing elements (this happens either the first time round or when the number of elements is icreased)
473         for ($i=0; $i<$workshop->nelements; $i++) {
474             if (!isset($elements[$i])) {
475                 $elements[$i]->description = '';
476                 $elements[$i]->scale =0;
477                 $elements[$i]->maxscore = 0;
478                 $elements[$i]->weight = 11;
479             }
480         }
481         
482         switch ($workshop->gradingstrategy) {
483             case 0: // no grading
484                 for ($i=0; $i<$workshop->nelements; $i++) {
485                     $iplus1 = $i+1;
486                     echo "<tr valign=\"top\">\n";
487                     echo "  <td align=\"right\"><b>". get_string("element","workshop")." $iplus1:</b></td>\n";
488                     echo "<td><textarea name=\"description[]\" rows=\"3\" cols=\"75\">".$elements[$i]->description."</textarea>\n";
489                     echo "  </td></tr>\n";
490                     echo "<tr valign=\"top\">\n";
491                     echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
492                     echo "</tr>\n";
493                 }
494                 break;
496             case 1: // accumulative grading
497                 // set up scales name
498                 foreach ($WORKSHOP_SCALES as $KEY => $SCALE) {
499                     $SCALES[] = $SCALE['name'];
500                 }
501                 for ($i=0; $i<$workshop->nelements; $i++) {
502                     $iplus1 = $i+1;
503                     echo "<tr valign=\"top\">\n";
504                     echo "  <td align=\"right\"><b>". get_string("element","workshop")." $iplus1:</b></td>\n";
505                     echo "<td><textarea name=\"description[]\" rows=\"3\" cols=\"75\">".$elements[$i]->description."</textarea>\n";
506                     echo "  </td></tr>\n";
507                     echo "<tr valign=\"top\">\n";
508                     echo "  <td align=\"right\"><b>". get_string("typeofscale", "workshop"). ":</b></td>\n";
509                     echo "<td valign=\"top\">\n";
510                     choose_from_menu($SCALES, "scale[]", $elements[$i]->scale, "");
511                     if ($elements[$i]->weight == '') { // not set
512                         $elements[$i]->weight = 11; // unity
513                     }
514                     echo "</td></tr>\n";
515                     echo "<tr valign=\"top\"><td align=\"right\"><b>".get_string("elementweight", "workshop").":</b></td><td>\n";
516                     workshop_choose_from_menu($WORKSHOP_EWEIGHTS, "weight[]", $elements[$i]->weight, "");
517                     echo "      </td>\n";
518                     echo "</tr>\n";
519                     echo "<tr valign=\"top\">\n";
520                     echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
521                     echo "</tr>\n";
522                 }
523                 break;
524                 
525             case 2: // error banded grading
526                 for ($i=0; $i<$workshop->nelements; $i++) {
527                     $iplus1 = $i+1;
528                     echo "<tr valign=\"top\">\n";
529                     echo "  <td align=\"right\"><b>". get_string("element","workshop")." $iplus1:</b></td>\n";
530                     echo "<td><textarea name=\"description[$i]\" rows=\"3\" cols=\"75\">".$elements[$i]->description."</textarea>\n";
531                     echo "  </td></tr>\n";
532                     if ($elements[$i]->weight == '') { // not set
533                         $elements[$i]->weight = 11; // unity
534                         }
535                     echo "</tr>\n";
536                     echo "<tr valign=\"top\"><td align=\"right\"><b>".get_string("elementweight", "workshop").":</b></td><td>\n";
537                     workshop_choose_from_menu($WORKSHOP_EWEIGHTS, "weight[]", $elements[$i]->weight, "");
538                     echo "      </td>\n";
539                     echo "</tr>\n";
540                     echo "<tr valign=\"top\">\n";
541                     echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
542                     echo "</tr>\n";
543                 }
544                 echo "</center></table><br />\n";
545                 echo "<center><b>".get_string("gradetable","workshop")."</b></center>\n";
546                 echo "<center><table cellpadding=\"5\" border=\"1\"><tr><td align=\"CENTER\">".
547                     get_string("numberofnegativeresponses", "workshop");
548                 echo "</td><td>". get_string("suggestedgrade", "workshop")."</td></tr>\n";
549                 for ($j = $workshop->grade; $j >= 0; $j--) {
550                     $numbers[$j] = $j;
551                 }
552                 for ($i=0; $i<=$workshop->nelements; $i++) {
553                     echo "<tr><td align=\"CENTER\">$i</td><td align=\"CENTER\">";
554                     if (!isset($elements[$i])) {  // the "last one" will be!
555                         $elements[$i]->description = "";
556                         $elements[$i]->maxscore = 0;
557                     }
558                     choose_from_menu($numbers, "maxscore[$i]", $elements[$i]->maxscore, "");
559                     echo "</td></tr>\n";
560                 }
561                 echo "</table></center>\n";
562                 break;
563                 
564             case 3: // criterion grading
565                 for ($j = 100; $j >= 0; $j--) {
566                     $numbers[$j] = $j;
567                 }
568                 for ($i=0; $i<$workshop->nelements; $i++) {
569                     $iplus1 = $i+1;
570                     echo "<tr valign=\"top\">\n";
571                     echo "  <td align=\"right\"><b>". get_string("criterion","workshop")." $iplus1:</b></td>\n";
572                     echo "<td><textarea name=\"description[$i]\" rows=\"3\" cols=\"75\">".$elements[$i]->description."</textarea>\n";
573                     echo "  </td></tr>\n";
574                     echo "<tr><td><b>". get_string("suggestedgrade", "workshop").":</b></td><td>\n";
575                     choose_from_menu($numbers, "maxscore[$i]", $elements[$i]->maxscore, "");
576                     echo "</td></tr>\n";
577                     echo "<tr valign=\"top\">\n";
578                     echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
579                     echo "</tr>\n";
580                 }
581                 break;
583             case 4: // rubric
584                 for ($j = 100; $j >= 0; $j--) {
585                     $numbers[$j] = $j;
586                 }
587                 if ($rubricsraw = get_records("workshop_rubrics", "workshopid", $workshop->id)) {
588                     foreach ($rubricsraw as $rubric) {
589                         $rubrics[$rubric->elementno][$rubric->rubricno] = $rubric->description;   // reindex 0,1,2...
590                     }
591                 }
592                 for ($i=0; $i<$workshop->nelements; $i++) {
593                     $iplus1 = $i+1;
594                     echo "<tr valign=\"top\">\n";
595                     echo "  <td align=\"right\"><b>". get_string("element","workshop")." $iplus1:</b></td>\n";
596                     echo "<td><textarea name=\"description[$i]\" rows=\"3\" cols=\"75\">".$elements[$i]->description."</textarea>\n";
597                     echo "  </td></tr>\n";
598                     echo "<tr valign=\"top\"><td align=\"right\"><b>".get_string("elementweight", "workshop").":</b></td><td>\n";
599                     workshop_choose_from_menu($WORKSHOP_EWEIGHTS, "weight[]", $elements[$i]->weight, "");
600                     echo "      </td>\n";
601                     echo "</tr>\n";
603                     for ($j=0; $j<5; $j++) {
604                         $jplus1 = $j+1;
605                         if (empty($rubrics[$i][$j])) {
606                             $rubrics[$i][$j] = "";
607                         }
608                         echo "<tr valign=\"top\">\n";
609                         echo "  <td align=\"right\"><b>". get_string("grade","workshop")." $j:</b></td>\n";
610                         echo "<td><textarea name=\"rubric[$i][$j]\" rows=\"3\" cols=\"75\">".$rubrics[$i][$j]."</textarea>\n";
611                         echo "  </td></tr>\n";
612                         }
613                     echo "<tr valign=\"top\">\n";
614                     echo "  <td colspan=\"2\" bgcolor=\"$THEME->cellheading2\">&nbsp;</td>\n";
615                     echo "</tr>\n";
616                     }
617                 break;
618             }
619         // close table and form
621         ?>
622         </table><br />
623         <input type="submit" value="<?php  print_string("savechanges") ?>" />
624         <input type="submit" name="cancel" value="<?php  print_string("cancel") ?>" />
625         </center>
626         </form>
627         <?php
628     }
629     
630     
631     /*************** grade all assessments (by teacher) ***************************/
632     elseif ($action == 'gradeallassessments') {
633         
634         if (!isteacher($course->id)) {
635             error("Only teachers can look at this page");
636         }
638         print_heading(get_string("gradingallassessments", "workshop"));
639         workshop_grade_assessments($workshop);
640         print_continue("view.php?id=$cm->id");
641     }
644     /*************** grade (student's) assessment (by teacher) ***************************/
645     elseif ($action == 'gradeassessment') {
647         if (!isteacher($course->id)) {
648             error("Only teachers can look at this page");
649         }
651         print_heading_with_help(get_string("gradeassessment", "workshop"), "gradingassessments", "workshop");
652         // get assessment record
653         if (!$assessmentid = $_GET['aid']) {
654             error("Assessment id not given");
655         }
656         $assessment = get_record("workshop_assessments", "id", $assessmentid);
657         if (!$submission = get_record("workshop_submissions", "id", $assessment->submissionid)) {
658             error("Submission not found");
659         }
660         // get the teacher's assessment first
661         if ($teachersassessment = workshop_get_submission_assessment($submission, $USER)) {
662             echo "<center><b>".get_string("teacherassessments", "workshop", $course->teacher)."</b></center>\n";
663             workshop_print_assessment($workshop, $teachersassessment);
664         }
665         // now the student's assessment (don't allow changes)
666         $user = get_record("user", "id", $assessment->userid);
667         echo "<center><b>".get_string("assessmentby", "workshop", $user->firstname." ".$user->lastname)."</b></center>\n";
668         workshop_print_assessment($workshop, $assessment);
669         
670         include('assessment_grading_form.html');
671         die;
672     }
675     /*************** insert (new) comment (by author, assessor or teacher) ***************************/
676     elseif ($action == 'insertcomment') {
677         $timenow = time();
679         $form = (object)$_POST;
680         
681         if (!$assessment = get_record("workshop_assessments", "id", $_POST['aid'])) {
682             error("Unable to insert comment");
683             }
684         // save the comment...
685         $comment->workshopid = $workshop->id;
686         $comment->assessmentid   = $assessment->id;
687         $comment->userid   = $USER->id;
688         $comment->timecreated   = $timenow;
689         $comment->comments   = $form->comments;
690         if (!$comment->id = insert_record("workshop_comments", $comment)) {
691             error("Could not insert workshop comment!");
692             }
693             
694         add_to_log($course->id, "workshop", "comment", "view.php?id=$cm->id", "$comment->id");
696         print_continue("viewassessment.php?id=$cm->id&amp;aid=$assessment->id");
697         }
700     /*********************** insert/update assignment elements (for teachers)***********************/
701     elseif ($action == 'insertelements') {
703         if (!isteacher($course->id)) {
704             error("Only teachers can look at this page");
705         }
707         $form = (object)$HTTP_POST_VARS;
708         
709         // let's not fool around here, dump the junk!
710         delete_records("workshop_elements", "workshopid", $workshop->id);
711         
712         // determine wich type of grading
713         switch ($workshop->gradingstrategy) {
714             case 0: // no grading
715                 // Insert all the elements that contain something
716                 foreach ($form->description as $key => $description) {
717                     if ($description) {
718                         unset($element);
719                         $element->description   = $description;
720                         $element->workshopid = $workshop->id;
721                         $element->elementno = $key;
722                         if (!$element->id = insert_record("workshop_elements", $element)) {
723                             error("Could not insert workshop element!");
724                         }
725                     }
726                 }
727                 break;
728                 
729             case 1: // accumulative grading
730                 // Insert all the elements that contain something
731                 foreach ($form->description as $key => $description) {
732                     if ($description) {
733                         unset($element);
734                         $element->description   = $description;
735                         $element->workshopid = $workshop->id;
736                         $element->elementno = $key;
737                         if (isset($form->scale[$key])) {
738                             $element->scale = $form->scale[$key];
739                             switch ($WORKSHOP_SCALES[$form->scale[$key]]['type']) {
740                                 case 'radio' :  $element->maxscore = $WORKSHOP_SCALES[$form->scale[$key]]['size'] - 1;
741                                                         break;
742                                 case 'selection' :  $element->maxscore = $WORKSHOP_SCALES[$form->scale[$key]]['size'];
743                                                         break;
744                             }
745                         }
746                         if (isset($form->weight[$key])) {
747                             $element->weight = $form->weight[$key];
748                         }
749                         if (!$element->id = insert_record("workshop_elements", $element)) {
750                             error("Could not insert workshop element!");
751                         }
752                     }
753                 }
754                 break;
755                 
756             case 2: // error banded grading...
757             case 3: // ...and criterion grading
758                 // Insert all the elements that contain something, the number of descriptions is one less than the number of grades
759                 foreach ($form->maxscore as $key => $themaxscore) {
760                     unset($element);
761                     $element->workshopid = $workshop->id;
762                     $element->elementno = $key;
763                     $element->maxscore = $themaxscore;
764                     if (isset($form->description[$key])) {
765                         $element->description   = $form->description[$key];
766                     }
767                     if (isset($form->weight[$key])) {
768                         $element->weight = $form->weight[$key];
769                     }
770                     if (!$element->id = insert_record("workshop_elements", $element)) {
771                         error("Could not insert workshop element!");
772                     }
773                 }
774                 break;
775                 
776             case 4: // ...and criteria grading
777                 // Insert all the elements that contain something
778                 foreach ($form->description as $key => $description) {
779                     unset($element);
780                     $element->workshopid = $workshop->id;
781                     $element->elementno = $key;
782                     $element->description   = $description;
783                     $element->weight = $form->weight[$key];
784                     for ($j=0;$j<5;$j++) {
785                         if (empty($form->rubric[$key][$j]))
786                             break;
787                     }
788                     $element->maxscore = $j - 1;
789                     if (!$element->id = insert_record("workshop_elements", $element)) {
790                         error("Could not insert workshop element!");
791                     }
792                 }
793                 // let's not fool around here, dump the junk!
794                 delete_records("workshop_rubrics", "workshopid", $workshop->id);
795                 for ($i=0;$i<$workshop->nelements;$i++) {
796                     for ($j=0;$j<5;$j++) {
797                         unset($element);
798                         if (empty($form->rubric[$i][$j])) {  // OK to have an element with fewer than 5 items
799                              break;
800                          }
801                         $element->workshopid = $workshop->id;
802                         $element->elementno = $i;
803                         $element->rubricno = $j;
804                         $element->description   = $form->rubric[$i][$j];
805                         if (!$element->id = insert_record("workshop_rubrics", $element)) {
806                             error("Could not insert workshop element!");
807                         }
808                     }
809                 }
810                 break;
811         } // end of switch
813         redirect("view.php?id=$cm->id", get_string("savedok","workshop"));
814     }
817     /*********************** list assessments for grading (Student submissions)(by teachers)***********************/
818     elseif ($action == 'listungradedstudentsubmissions') {
820         if (!isteacher($course->id)) {
821             error("Only teachers can look at this page");
822             }
823         workshop_list_ungraded_assessments($workshop, "student");
824         print_continue("view.php?id=$cm->id");
825         }
828     /*********************** list assessments for grading (Teacher submissions) (by teachers)***********************/
829     elseif ($action == 'listungradedteachersubmissions') {
831         if (!isteacher($course->id)) {
832             error("Only teachers can look at this page");
833             }
834         workshop_list_ungraded_assessments($workshop, "teacher");
835         print_continue("view.php?id=$cm->id");
836         }
839     /****************** list teacher submissions ***********************/
840     elseif ($action == 'listteachersubmissions') {
842         workshop_list_teacher_submissions($workshop, $USER);
843         print_continue("view.php?id=$cm->id");
844     }
847     /******************* regrade student assessments ************************************/
848     elseif ($action == 'regradestudentassessments' ) {
850         $timenow = time();
851         if (!isteacher($course->id)) {
852             error("Only teachers can look at this page");
853         }
854         // get all the submissions...
855         if ($submissions = get_records("workshop_submissions", "workshopid", $workshop->id)) {
856             foreach ($submissions as $submission) {
857                 // ...if cold...
858                 if (($submission->timecreated + $CFG->maxeditingtime) < $timenow) {
859                     // ...clear assessment count so workshop_grade_assessments() can do its thing
860                     set_field("workshop_submissions", "nassessments", 0, "id", $submission->id);
861                 }
862             }
863         }
864         echo "<pre>";
865         workshop_grade_assessments($workshop);
866         echo '</pre>';
867         print_continue("submissions.php?id=$cm->id&action=adminlist");
868     }
869     
871     /*************** remove stock comment (by teacher ) ***************************/
872     elseif ($action == 'removestockcomment') {
874         require_variable($aid);
875         require_variable($stockcommentid);
877         if (!isteacher($course->id)) {
878             error("Only teachers can look at this page");
879         }
880     
881         if (!$assessment = get_record("workshop_assessments", "id", $aid)) {
882             error("workshop assessment is misconfigured");
883         }
884         $form = data_submitted("nomatch"); // probably always come from the same page, change this statement
885     
886         // delete the comment from the stock comments table
887         if (!delete_records("workshop_stockcomments", "id", $stockcommentid)) {
888             error("Could not remove comment from the comment bank");
889         }
891         // now upate the assessment (just the elements, the assessment itself is not updated)
893         // first get the assignment elements for maxscores and weights...
894         $elementsraw = get_records("workshop_elements", "workshopid", $workshop->id, "elementno ASC");
895         if (count($elementsraw) < $workshop->nelements) {
896             print_string("noteonassignmentelements", "workshop");
897         }
898         if ($elementsraw) {
899             foreach ($elementsraw as $element) {
900                 $elements[] = $element;   // to renumber index 0,1,2...
901             }
902         } else {
903             $elements = null;
904         }
906         $timenow = time();
907         // don't fiddle about, delete all the old and add the new!
908         delete_records("workshop_grades", "assessmentid",  $assessment->id);
909         
910     
911         //determine what kind of grading we have
912         switch ($workshop->gradingstrategy) {
913             case 0: // no grading
914                 // Insert all the elements that contain something
915                 for ($i =0; $i < $workshop->nelements; $i++) {
916                     unset($element);
917                     $element->workshopid = $workshop->id;
918                     $element->assessmentid = $assessment->id;
919                     $element->elementno = $i;
920                     $element->feedback   = $form->{"feedback_$i"};
921                     if (!$element->id = insert_record("workshop_grades", $element)) {
922                         error("Could not insert workshop grade!");
923                     }
924                 }
925                 $grade = 0; // set to satisfy save to db
926                 break;
927                 
928             case 1: // accumulative grading
929                 // Insert all the elements that contain something
930                 foreach ($form->grade as $key => $thegrade) {
931                     unset($element);
932                     $element->workshopid = $workshop->id;
933                     $element->assessmentid = $assessment->id;
934                     $element->elementno = $key;
935                     $element->feedback   = $form->{"feedback_$key"};
936                     $element->grade = $thegrade;
937                     if (!$element->id = insert_record("workshop_grades", $element)) {
938                         error("Could not insert workshop grade!");
939                         }
940                     }
941                 // now work out the grade...
942                 $rawgrade=0;
943                 $totalweight=0;
944                 foreach ($form->grade as $key => $grade) {
945                     $maxscore = $elements[$key]->maxscore;
946                     $weight = $WORKSHOP_EWEIGHTS[$elements[$key]->weight];
947                     if ($weight > 0) { 
948                         $totalweight += $weight;
949                     }
950                     $rawgrade += ($grade / $maxscore) * $weight;
951                     // echo "\$key, \$maxscore, \$weight, \$totalweight, \$grade, \$rawgrade : $key, $maxscore, $weight, $totalweight, $grade, $rawgrade<br />";
952                 }
953                 $grade = 100.0 * ($rawgrade / $totalweight);
954                 break;
956             case 2: // error banded graded
957                 // Insert all the elements that contain something
958                 $error = 0.0; 
959                 for ($i =0; $i < $workshop->nelements; $i++) {
960                     unset($element);
961                     $element->workshopid = $workshop->id;
962                     $element->assessmentid = $assessment->id;
963                     $element->elementno = $i;
964                     $element->feedback   = $form->{"feedback_$i"};
965                     $element->grade = $form->grade[$i];
966                     if (!$element->id = insert_record("workshop_grades", $element)) {
967                         error("Could not insert workshop grade!");
968                     }
969                     if (empty($form->grade[$i])){
970                         $error += $WORKSHOP_EWEIGHTS[$elements[$i]->weight];
971                     }
972                 }
973                 // now save the adjustment
974                 unset($element);
975                 $i = $workshop->nelements;
976                 $element->workshopid = $workshop->id;
977                 $element->assessmentid = $assessment->id;
978                 $element->elementno = $i;
979                 $element->grade = $form->grade[$i];
980                 if (!$element->id = insert_record("workshop_grades", $element)) {
981                     error("Could not insert workshop grade!");
982                 }
983                 $grade = ($elements[intval($error + 0.5)]->maxscore + $form->grade[$i]) * 100 / $workshop->grade;
984                 // do sanity check
985                 if ($grade < 0) {
986                     $grade = 0;
987                 } elseif ($grade > 100) {
988                     $grade = 100;
989                 }
990                 echo "<b>".get_string("weightederrorcount", "workshop", intval($error + 0.5))."</b>\n";
991                 break;
992             
993             case 3: // criteria grading
994                 // save in the selected criteria value in element zero, 
995                 unset($element);
996                 $element->workshopid = $workshop->id;
997                 $element->assessmentid = $assessment->id;
998                 $element->elementno = 0;
999                 $element->grade = $form->grade[0];
1000                 if (!$element->id = insert_record("workshop_grades", $element)) {
1001                     error("Could not insert workshop grade!");
1002                 }
1003                 // now save the adjustment in element one
1004                 unset($element);
1005                 $element->workshopid = $workshop->id;
1006                 $element->assessmentid = $assessment->id;
1007                 $element->elementno = 1;
1008                 $element->grade = $form->grade[1];
1009                 if (!$element->id = insert_record("workshop_grades", $element)) {
1010                     error("Could not insert workshop grade!");
1011                 }
1012                 $grade = ($elements[$form->grade[0]]->maxscore + $form->grade[1]);
1013                 break;
1015             case 4: // rubric grading (identical to accumulative grading)
1016                 // Insert all the elements that contain something
1017                 foreach ($form->grade as $key => $thegrade) {
1018                     unset($element);
1019                     $element->workshopid = $workshop->id;
1020                     $element->assessmentid = $assessment->id;
1021                     $element->elementno = $key;
1022                     $element->feedback   = $form->{"feedback_$key"};
1023                     $element->grade = $thegrade;
1024                     if (!$element->id = insert_record("workshop_grades", $element)) {
1025                         error("Could not insert workshop grade!");
1026                     }
1027                 }
1028                 // now work out the grade...
1029                 $rawgrade=0;
1030                 $totalweight=0;
1031                 foreach ($form->grade as $key => $grade) {
1032                     $maxscore = $elements[$key]->maxscore;
1033                     $weight = $WORKSHOP_EWEIGHTS[$elements[$key]->weight];
1034                     if ($weight > 0) { 
1035                         $totalweight += $weight;
1036                     }
1037                     $rawgrade += ($grade / $maxscore) * $weight;
1038                 }
1039                 $grade = 100.0 * ($rawgrade / $totalweight);
1040                 break;
1042         } // end of switch
1043             
1044     
1045         // any comment?
1046         if (!empty($form->generalcomment)) { // update the object (no need to update the db record)
1047             $assessment->generalcomment = $form->generalcomment;
1048         }
1049         
1050         // redisplay form, going back to original returnto address
1051         workshop_print_assessment($workshop, $assessment, true, true, $form->returnto);
1052         
1053         // add_to_log($course->id, "workshop", "assess", "viewassessment.php?id=$cm->id&amp;aid=$assessment->id", "$assessment->id", "$cm->id");
1054         
1055     }
1058     /*************** update assessment (by teacher or student) ***************************/
1059     elseif ($action == 'updateassessment') {
1061         require_variable($aid);
1062         if (! $assessment = get_record("workshop_assessments", "id", $aid)) {
1063             error("workshop assessment is misconfigured");
1064         }
1066         // first get the assignment elements for maxscores and weights...
1067         $elementsraw = get_records("workshop_elements", "workshopid", $workshop->id, "elementno ASC");
1068         if (count($elementsraw) < $workshop->nelements) {
1069             print_string("noteonassignmentelements", "workshop");
1070         }
1071         if ($elementsraw) {
1072             foreach ($elementsraw as $element) {
1073                 $elements[] = $element;   // to renumber index 0,1,2...
1074             }
1075         } else {
1076             $elements = null;
1077         }
1079         $timenow = time();
1080         // don't fiddle about, delete all the old and add the new!
1081         delete_records("workshop_grades", "assessmentid",  $assessment->id);
1082         
1083         $form = data_submitted("nomatch"); // probably always come from the same page, change this statement
1084         
1085         //determine what kind of grading we have
1086         switch ($workshop->gradingstrategy) {
1087             case 0: // no grading
1088                 // Insert all the elements that contain something
1089                 for ($i = 0; $i < $workshop->nelements; $i++) {
1090                     unset($element);
1091                     $element->workshopid = $workshop->id;
1092                     $element->assessmentid = $assessment->id;
1093                     $element->elementno = $i;
1094                     $element->feedback = $form->{"feedback_$i"};
1095                     if (!$element->id = insert_record("workshop_grades", $element)) {
1096                         error("Could not insert workshop grade!");
1097                     }
1098                 }
1099                 $grade = 0; // set to satisfy save to db
1100                 break;
1101                 
1102             case 1: // accumulative grading
1103                 // Insert all the elements that contain something
1104                 foreach ($form->grade as $key => $thegrade) {
1105                     unset($element);
1106                     $element->workshopid = $workshop->id;
1107                     $element->assessmentid = $assessment->id;
1108                     $element->elementno = $key;
1109                     $element->feedback   = $form->{"feedback_$key"};
1110                     $element->grade = $thegrade;
1111                     if (!$element->id = insert_record("workshop_grades", $element)) {
1112                         error("Could not insert workshop grade!");
1113                         }
1114                     }
1115                 // now work out the grade...
1116                 $rawgrade=0;
1117                 $totalweight=0;
1118                 foreach ($form->grade as $key => $grade) {
1119                     $maxscore = $elements[$key]->maxscore;
1120                     $weight = $WORKSHOP_EWEIGHTS[$elements[$key]->weight];
1121                     if ($weight > 0) { 
1122                         $totalweight += $weight;
1123                     }
1124                     $rawgrade += ($grade / $maxscore) * $weight;
1125                     // echo "\$key, \$maxscore, \$weight, \$totalweight, \$grade, \$rawgrade : $key, $maxscore, $weight, $totalweight, $grade, $rawgrade<br />";
1126                 }
1127                 $grade = 100.0 * ($rawgrade / $totalweight);
1128                 break;
1130             case 2: // error banded graded
1131                 // Insert all the elements that contain something
1132                 $error = 0.0; 
1133                 for ($i =0; $i < $workshop->nelements; $i++) {
1134                     unset($element);
1135                     $element->workshopid = $workshop->id;
1136                     $element->assessmentid = $assessment->id;
1137                     $element->elementno = $i;
1138                     $element->feedback   = $form->{"feedback_$i"};
1139                     $element->grade = $form->grade[$i];
1140                     if (!$element->id = insert_record("workshop_grades", $element)) {
1141                         error("Could not insert workshop grade!");
1142                     }
1143                     if (empty($form->grade[$i])){
1144                         $error += $WORKSHOP_EWEIGHTS[$elements[$i]->weight];
1145                     }
1146                 }
1147                 // now save the adjustment
1148                 unset($element);
1149                 $i = $workshop->nelements;
1150                 $element->workshopid = $workshop->id;
1151                 $element->assessmentid = $assessment->id;
1152                 $element->elementno = $i;
1153                 $element->grade = $form->grade[$i];
1154                 if (!$element->id = insert_record("workshop_grades", $element)) {
1155                     error("Could not insert workshop grade!");
1156                 }
1157                 $grade = ($elements[intval($error + 0.5)]->maxscore + $form->grade[$i]) * 100 / $workshop->grade;
1158                 // do sanity check
1159                 if ($grade < 0) {
1160                     $grade = 0;
1161                 } elseif ($grade > 100) {
1162                     $grade = 100;
1163                 }
1164                 echo "<b>".get_string("weightederrorcount", "workshop", intval($error + 0.5))."</b>\n";
1165                 break;
1166             
1167             case 3: // criteria grading
1168                 // save in the selected criteria value in element zero, 
1169                 unset($element);
1170                 $element->workshopid = $workshop->id;
1171                 $element->assessmentid = $assessment->id;
1172                 $element->elementno = 0;
1173                 $element->grade = $form->grade[0];
1174                 if (!$element->id = insert_record("workshop_grades", $element)) {
1175                     error("Could not insert workshop grade!");
1176                 }
1177                 // now save the adjustment in element one
1178                 unset($element);
1179                 $element->workshopid = $workshop->id;
1180                 $element->assessmentid = $assessment->id;
1181                 $element->elementno = 1;
1182                 $element->grade = $form->grade[1];
1183                 if (!$element->id = insert_record("workshop_grades", $element)) {
1184                     error("Could not insert workshop grade!");
1185                 }
1186                 $grade = ($elements[$form->grade[0]]->maxscore + $form->grade[1]);
1187                 break;
1189             case 4: // rubric grading (identical to accumulative grading)
1190                 // Insert all the elements that contain something
1191                 foreach ($form->grade as $key => $thegrade) {
1192                     unset($element);
1193                     $element->workshopid = $workshop->id;
1194                     $element->assessmentid = $assessment->id;
1195                     $element->elementno = $key;
1196                     $element->feedback = $form->{"feedback_$key"};
1197                     $element->grade = $thegrade;
1198                     if (!$element->id = insert_record("workshop_grades", $element)) {
1199                         error("Could not insert workshop grade!");
1200                     }
1201                 }
1202                 // now work out the grade...
1203                 $rawgrade=0;
1204                 $totalweight=0;
1205                 foreach ($form->grade as $key => $grade) {
1206                     $maxscore = $elements[$key]->maxscore;
1207                     $weight = $WORKSHOP_EWEIGHTS[$elements[$key]->weight];
1208                     if ($weight > 0) { 
1209                         $totalweight += $weight;
1210                     }
1211                     $rawgrade += ($grade / $maxscore) * $weight;
1212                 }
1213                 $grade = 100.0 * ($rawgrade / $totalweight);
1214                 break;
1216         } // end of switch
1217             
1218         // update the time of the assessment record (may be re-edited)...
1219         set_field("workshop_assessments", "timecreated", $timenow, "id", $assessment->id);
1220         
1221         if (!$submission = get_record("workshop_submissions", "id", $assessment->submissionid)) {
1222             error ("Updateassessment: submission record not found");
1223         }
1224         
1225         // if the workshop does need peer agreement AND it's self assessment then set timeagreed
1226         if ($workshop->agreeassessments and ($submission->userid == $assessment->userid)) {
1227             set_field("workshop_assessments", "timeagreed", $timenow, "id", $assessment->id);
1228         }
1229         
1230         // set grade...
1231         set_field("workshop_assessments", "grade", $grade, "id", $assessment->id);
1232         // ...and clear the timegraded but set the graddinggrade to maximum, may be reduced subsequently...
1233         set_field("workshop_assessments", "timegraded", 0, "id", $assessment->id);
1234         set_field("workshop_assessments", "gradinggrade", 100, "id", $assessment->id);
1235         // ...and the resubmission flag
1236         set_field("workshop_assessments", "resubmission", 0, "id", $assessment->id);
1238         // if there's examples or peer assessments clear the counter in the submission so that
1239         // all assessments for this submission will be regraded
1240         if ($workshop->ntassessments or $workshop->nsassessments) {
1241             set_field("workshop_submissions", "nassessments", 0, "id", $submission->id);
1242             workshop_grade_assessments($workshop);
1243         } else { // it could be self assessment....
1244             // now see if there's a corresponding assessment so that the gradinggrade can be set
1245             if (isteacher($course->id)) {
1246                 // see if there's are student assessments, if so set their gradinggrade
1247                 if ($assessments = workshop_get_assessments($submission)) {
1248                     foreach($assessments as $studentassessment) {
1249                         // skip if it's not a student assessment
1250                         if (!isstudent($course->id, $studentassessment->userid)) {
1251                             continue;
1252                         }
1253                         $gradinggrade = workshop_compare_assessments($workshop, $assessment, $studentassessment);
1254                         set_field("workshop_assessments", "timegraded", $timenow, "id", $studentassessment->id);
1255                         set_field("workshop_assessments", "gradinggrade", $gradinggrade, "id", $studentassessment->id);
1256                     }
1257                 }
1258             } else { //it's a student assessment, see if there's a corresponding teacher's assessment
1259                 if ($assessments = workshop_get_assessments($submission)) {
1260                     foreach($assessments as $teacherassessment) {
1261                         if (isteacher($course->id, $teacherassessment->userid)) {
1262                             $gradinggrade = workshop_compare_assessments($workshop, $assessment, $teacherassessment);
1263                             set_field("workshop_assessments", "timegraded", $timenow, "id", $assessment->id);
1264                             set_field("workshop_assessments", "gradinggrade", $gradinggrade, "id", $assessment->id);
1265                             break; // only look for the first teacher assessment
1266                         }
1267                     }
1268                 }
1269             }
1270         }
1271         
1272         // any comment?
1273         if (!empty($form->generalcomment)) {
1274             set_field("workshop_assessments", "generalcomment", $form->generalcomment, "id", $assessment->id);
1275         }
1276             
1277         add_to_log($course->id, "workshop", "assess",
1278                 "viewassessment.php?id=$cm->id&amp;aid=$assessment->id", "$assessment->id", "$cm->id");
1279         
1280         // set up return address
1281         if (!$returnto = $form->returnto) {
1282             $returnto = "view.php?id=$cm->id";
1283         }
1284             
1285         // show grade if grading strategy is not zero
1286         if ($workshop->gradingstrategy) {
1287             redirect($returnto, get_string("thegradeis", "workshop").": ".
1288                     number_format($grade * $workshop->grade / 100, 2).
1289                     " (".get_string("maximumgrade")." ".number_format($workshop->grade).")");
1290         }
1291         else {
1292             redirect($returnto);
1293         }
1294     }
1297     /****************** update comment (by author, assessor or teacher) ********************/
1298     elseif ($action == 'updatecomment') {
1299         $timenow = time();
1301         $form = (object)$_POST;
1302         
1303         // get the comment record...
1304         if (!$comment = get_record("workshop_comments", "id", $_POST['cid'])) {
1305             error("Update to Comment failed");
1306         }
1307         if (!$assessment = get_record("workshop_assessments", "id", $comment->assessmentid)) {
1308             error("Update Comment: Assessment not found");
1309         }
1310         //save the comment for the assessment...
1311         if (isset($form->comments)) {
1312             set_field("workshop_comments", "comments", $form->comments, "id", $comment->id);
1313             set_field("workshop_comments", "timecreated", $timenow, "id", $comment->id);
1314             // ..and kick to comment into life (probably not needed but just in case)
1315             set_field("workshop_comments", "mailed", 0, "id", $comment->id);
1316             echo "<centre><b>".get_string("savedok", "workshop")."</b></center><br />\n";
1317             
1318             add_to_log($course->id, "workshop", "comment", 
1319                     "viewassessment.php?id=$cm->id&amp;aid=$assessment->id", "$comment->id");
1320         }
1322         print_continue("viewassessment.php?id=$cm->id&amp;aid=$assessment->id");
1323     }
1326     /****************** update grading (by teacher) ***************************/
1327     elseif ($action == 'updategrading') {
1328         $timenow = time();
1330         if (!isteacher($course->id)) {
1331             error("Only teachers can look at this page");
1332         }
1334         $form = (object)$_POST;
1335         
1336         if (!$assessment = get_record("workshop_assessments", "id", $_POST['aid'])) {
1337             error("Update Grading failed");
1338         }
1339         //save the comment and grade for the assessment 
1340         if (isset($form->teachercomment)) {
1341             set_field("workshop_assessments", "teachercomment", $form->teachercomment, "id", $assessment->id);
1342             set_field("workshop_assessments", "gradinggrade", $form->gradinggrade, "id", $assessment->id);
1343             set_field("workshop_assessments", "timegraded", $timenow, "id", $assessment->id);
1344             set_field("workshop_assessments", "mailed", 0, "id", $assessment->id);
1345             set_field("workshop_assessments", "teachergraded", 1, "id", $assessment->id);
1346             echo "<centre><b>".get_string("savedok", "workshop")."</b></centre><br />\n";
1347             
1348             add_to_log($course->id, "workshop", "grade", 
1349                  "viewassessment.php?id=$cm->id&amp;aid=$assessment->id", "$assessment->id", "$cm->id");
1350         }
1351         redirect($form->redirect);
1352     }
1353     
1355     /****************** view all assessments ***********************/
1356     elseif ($action == 'viewallassessments') {
1357         
1358         if (!$submission = get_record("workshop_submissions", "id", $_GET['sid'])) {
1359             error("View All Assessments: submission record not found");
1360         }       
1361             
1362         if ($assessments = workshop_get_assessments($submission)) {
1363             foreach ($assessments as $assessment) {
1364                 workshop_print_assessment($workshop, $assessment);
1365             }
1366         }
1367         // only called from list all submissions
1368         print_continue("submissions.php?action=listallsubmissions&amp;id=$cm->id");
1369     }
1371     /*************** no man's land **************************************/
1372     else {
1373         error("Fatal Error: Unknown Action: ".$action."\n");
1374     }
1376     print_footer($course);
1377  
1378 ?>