MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / exercise / assessments.php
blobd6b4e2290ca8d08a444afaccca0c1cfecb721c6e
1 <?php // $Id$
3 /*************************************************
4 ACTIONS handled are:
6 adminamendgradinggrading
7 adminconfirmdelete
8 admindelete
9 adminedit
10 adminlist
11 adminlistbystudent
12 assessresubmission
13 assesssubmission
14 displaygradingform
15 editelements (teachers only)
16 insertelements (for teachers)
17 listungradedstudentsubmissions (for teachers)
18 listungradedstudentassessments (for teachers)
19 listteachersubmissions
20 regradestudentassessments (for teachers)
21 teacherassessment (for teachers)
22 teachertable
23 updateassessment
24 updateteacherassessment
25 userconfirmdelete
26 userdelete
27 viewassessment
29 ************************************************/
31 require_once("../../config.php");
32 require_once("lib.php");
33 require_once("locallib.php");
35 $id = required_param('id', PARAM_INT); // Course Module ID
36 $action = required_param('action', PARAM_ALPHA);
37 $aid = optional_param('aid', 0, PARAM_INT);
38 $sid = optional_param('sid', 0, PARAM_INT);
39 $userid = optional_param('userid', 0, PARAM_INT);
41 // get some esential stuff...
42 if (! $cm = get_coursemodule_from_id('exercise', $id)) {
43 error("Course Module ID was incorrect");
46 if (! $course = get_record("course", "id", $cm->course)) {
47 error("Course is misconfigured");
50 if (! $exercise = get_record("exercise", "id", $cm->instance)) {
51 error("Course module is incorrect");
54 exercise_add_custom_scales($exercise);
56 require_login($course->id, false, $cm);
58 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
60 $strexercises = get_string("modulenameplural", "exercise");
61 $strexercise = get_string("modulename", "exercise");
62 $strassessments = get_string("assessments", "exercise");
64 // ... print the header and...
65 $navlinks = array();
66 $navlinks[] = array('name' => $strexercises, 'link' => "index.php?id=$course->id", 'type' => 'activity');
67 $navlinks[] = array('name' => format_string($exercise->name), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
68 $navlinks[] = array('name' => $strassessments, 'link' => '', 'type' => 'title');
70 $navigation = build_navigation($navlinks);
72 print_header_simple(format_string($exercise->name), "", $navigation,
73 "", "", true);
75 //...get the action
78 /******************* admin amend Grading Grade ************************************/
79 if ($action == 'adminamendgradinggrade' ) {
81 if (!has_capability('mod/exercise:assess', $context)) {
82 error("Only teachers can look at this page");
84 if (empty($aid)) {
85 error("Admin Amend Grading grade: assessment id missing");
88 if (!$assessment = get_record("exercise_assessments", "id", $aid)) {
89 error("Amin Amend Grading grade: assessment not found");
91 print_heading(get_string("amend", "exercise")." ".get_string("gradeforstudentsassessment",
92 "exercise", $course->student));
93 echo "<form id=\"amendgrade\" method=\"post\" action=\"assessments.php\">\n";
94 echo "<input type=\"hidden\" name=\"aid\" value=\"$aid\" />\n";
95 echo "<input type=\"hidden\" name=\"action\" value=\"updategradinggrade\" />\n";
96 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
97 echo "<table width=\"50%\" align=\"center\" border=\"1\" />\n";
98 echo "<tr><td align=\"right\"><b>".get_string("gradeforstudentsassessment", "exercise",
99 $course->student)." :</td><td>\n";
100 // set up coment scale
101 for ($i=$exercise->gradinggrade; $i>=0; $i--) {
102 $num[$i] = $i;
104 choose_from_menu($num, "gradinggrade",
105 number_format($exercise->gradinggrade * $assessment->gradinggrade / 100, 0), "");
106 echo "</td></tr>\n";
107 echo "<tr><td colspan=\"2\" align=\"center\">";
108 echo "<input type=\"submit\" value=\"".get_string("amend", "exercise")."\" />\n";
109 echo "</td></tr>\n";
110 echo "</table>\n";
111 echo "</center>";
112 echo "</form>\n";
116 /******************* admin confirm delete ************************************/
117 elseif ($action == 'adminconfirmdelete' ) {
119 if (!has_capability('mod/exercise:assess', $context)) {
120 error("Only teachers can look at this page");
122 if (empty($aid)) {
123 error("Admin confirm delete: assessment id missing");
126 notice_yesno(get_string("confirmdeletionofthisitem","exercise", get_string("assessment", "exercise")),
127 "assessments.php?action=admindelete&amp;id=$cm->id&amp;aid=$aid",
128 "submissions.php?action=adminlist&amp;id=$cm->id");
132 /******************* admin delete ************************************/
133 elseif ($action == 'admindelete' ) {
135 if (!has_capability('mod/exercise:assess', $context)) {
136 error("Only teachers can look at this page");
138 if (empty($aid)) {
139 error("Admin delete: submission id missing");
142 print_string("deleting", "exercise");
143 // first delete all the associated records...
144 delete_records("exercise_grades", "assessmentid", $aid);
145 // ...now delete the assessment...
146 delete_records("exercise_assessments", "id", $aid);
148 print_continue("submissions.php?id=$cm->id&amp;action=adminlist");
152 /*********************** admin list of asssessments (of a submission) (by teachers)**************/
153 elseif ($action == 'adminlist') {
155 if (!has_capability('mod/exercise:assess', $context)) {
156 error("Only teachers can look at this page");
159 if (empty($sid)) {
160 error ("exercise asssessments: adminlist called with no sid");
162 $submission = get_record("exercise_submissions", "id", $sid);
163 exercise_print_assessments_for_admin($exercise, $submission);
164 print_continue("submissions.php?action=adminlist&amp;id=$cm->id");
168 /****************** admin list of asssessments by a student (used by teachers only )******************/
169 elseif ($action == 'adminlistbystudent') {
171 if (!has_capability('mod/exercise:assess', $context)) {
172 error("Only teachers can look at this page");
175 if (empty($userid)) {
176 error ("exercise asssessments: adminlistbystudent called with no userid");
178 $user = get_record("user", "id", $userid);
179 exercise_print_assessments_by_user_for_admin($exercise, $user);
180 print_continue("submissions.php?action=adminlist&amp;id=$cm->id");
184 /****************** Assess resubmission (by teacher) ***************************/
185 elseif ($action == 'assessresubmission') {
187 if (empty($sid)) {
188 error ("exercise asssessments: assessresubmission called with no sid");
191 if (! $submission = get_record("exercise_submissions", "id", $sid)) {
192 error("Assess submission is misconfigured - no submission record!");
194 if (!$submissionowner = get_record("user", "id", $submission->userid)) {
195 error("Assess resubmission: user record not found");
198 // there can be an assessment record, if there isn't...
199 if (!$assessment = exercise_get_submission_assessment($submission, $USER)) {
200 if (!$submissions = exercise_get_user_submissions($exercise, $submissionowner)) {
201 error("Assess resubmission: submission records not found");
203 $lastone= '';
204 // just the last but one submission
205 foreach ($submissions as $submission) {
206 $prevsubmission = $lastone;
207 $lastone = $submission;
209 // get the teacher's assessment of the student's previous submission
210 if (!$prevassessment = exercise_get_submission_assessment($prevsubmission, $USER)) {
211 error("Assess resubmission: Previous assessment record not found");
213 // copy this assessment with comments...
214 $assessment = exercise_copy_assessment($prevassessment, $submission, true);
217 print_heading(get_string("thisisaresubmission", "exercise",
218 fullname($submissionowner)));
219 // show assessment and allow changes
220 exercise_print_assessment_form($exercise, $assessment, true, $_SERVER["HTTP_REFERER"]);
224 /****************** Assess submission (by teacher or student) ***************************/
225 elseif ($action == 'assesssubmission') {
227 if (empty($sid)) {
228 error ("exercise asssessments: assesssubmission called with no sid");
231 if (! $submission = get_record("exercise_submissions", "id", $sid)) {
232 error("Assess submission is misconfigured - no submission record!");
235 // there can be an assessment record (for teacher submissions), if there isn't...
236 if (!$assessment = exercise_get_submission_assessment($submission, $USER)) {
237 $yearfromnow = time() + 365 * 86400;
238 // ...create one and set timecreated way in the future, this is reset when record is updated
239 $assessment->exerciseid = $exercise->id;
240 $assessment->submissionid = $submission->id;
241 $assessment->userid = $USER->id;
242 $assessment->grade = -1; // set impossible grade
243 $assessment->timecreated = $yearfromnow;
244 $assessment->timegraded = 0;
245 if (!$assessment->id = insert_record("exercise_assessments", $assessment)) {
246 error("Could not insert exercise assessment!");
250 // show assessment and allow changes
251 exercise_print_assessment_form($exercise, $assessment, true, $_SERVER["HTTP_REFERER"]);
255 /****************** display grading form (viewed by student) *********************************/
256 elseif ($action == 'displaygradingform') {
258 print_heading_with_help(get_string("specimenassessmentform", "exercise"), "specimen", "exercise");
260 exercise_print_assessment_form($exercise); // called with no assessment
261 print_continue("view.php?id=$cm->id");
265 /****************** edit assessment elements (for teachers) ***********************/
266 elseif ($action == 'editelements') {
268 if (!has_capability('mod/exercise:assess', $context)) {
269 error("Only teachers can look at this page");
272 $count = count_records("exercise_grades", "exerciseid", $exercise->id);
273 if ($exercise->phase > 1 and $count) {
274 notify(get_string("warningonamendingelements", "exercise"));
276 // set up heading, form and table
277 print_heading_with_help(get_string("editingassessmentelements", "exercise"), "elements", "exercise");
279 <form id="form" method="post" action="assessments.php">
280 <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
281 <input type="hidden" name="action" value="insertelements" />
282 <center><table cellpadding="5" border="1">
283 <?php
285 // get existing elements, if none set up appropriate default ones
286 if ($elementsraw = get_records("exercise_elements", "exerciseid", $exercise->id, "elementno ASC" )) {
287 foreach ($elementsraw as $element) {
288 $elements[] = $element; // to renumber index 0,1,2...
291 // check for missing elements (this happens either the first time round or when the number
292 // of elements is icreased)
293 for ($i=0; $i<$exercise->nelements; $i++) {
294 if (!isset($elements[$i])) {
295 $elements[$i]->description = '';
296 $elements[$i]->scale =0;
297 $elements[$i]->maxscore = 0;
298 $elements[$i]->weight = 11;
301 switch ($exercise->gradingstrategy) {
302 case 0: // no grading
303 for ($i=0; $i<$exercise->nelements; $i++) {
304 $iplus1 = $i+1;
305 echo "<tr valign=\"top\">\n";
306 echo " <td align=\"right\"><p><b>". get_string("element","exercise")." $iplus1:</b></p></td>\n";
307 echo "<td><textarea name=\"description[]\" rows=\"3\" cols=\"75\" wrap=\"virtual\">".
308 $elements[$i]->description."</textarea>\n";
309 echo " </td></tr>\n";
310 echo "<tr valign=\"top\">\n";
311 echo " <td colspan=\"2\">&nbsp;</td>\n";
312 echo "</tr>\n";
314 break;
316 case 1: // accumulative grading
317 // set up scales name
318 foreach ($EXERCISE_SCALES as $KEY => $SCALE) {
319 $SCALES[] = $SCALE['name'];
322 for ($i=0; $i<$exercise->nelements; $i++) {
323 $iplus1 = $i+1;
324 echo "<tr valign=\"top\">\n";
325 echo " <td align=\"right\"><p><b>". get_string("element","exercise")." $iplus1:</b></p></td>\n";
326 echo "<td><textarea name=\"description[]\" rows=\"3\" cols=\"75\" wrap=\"virtual\">".
327 $elements[$i]->description."</textarea>\n";
328 echo " </td></tr>\n";
329 echo "<tr valign=\"top\">\n";
330 echo " <td align=\"right\"><p><b>". get_string("typeofscale", "exercise"). ":</b></p></td>\n";
331 echo "<td valign=\"top\">\n";
332 choose_from_menu($SCALES, "scale[]", $elements[$i]->scale, "");
333 if ($elements[$i]->weight == '') { // not set
334 $elements[$i]->weight = 11; // unity
336 echo "</td></tr>\n";
337 echo "<tr valign=\"top\"><td align=\"right\"><b>".get_string("elementweight", "exercise").
338 ":</b></td><td>\n";
339 exercise_choose_from_menu($EXERCISE_EWEIGHTS, "weight[]", $elements[$i]->weight, "");
340 echo " </td>\n";
341 echo "</tr>\n";
342 echo "<tr valign=\"top\">\n";
343 echo " <td colspan=\"2\">&nbsp;</td>\n";
344 echo "</tr>\n";
346 break;
348 case 2: // error banded grading
349 for ($i=0; $i<$exercise->nelements; $i++) {
350 $iplus1 = $i+1;
351 echo "<tr valign=\"top\">\n";
352 echo " <td align=\"right\"><p><b>". get_string("element","exercise")." $iplus1:</b></p></td>\n";
353 echo "<td><textarea name=\"description[$i]\" rows=\"3\" cols=\"75\" wrap=\"virtual\">".
354 $elements[$i]->description."</textarea>\n";
355 echo " </td></tr>\n";
356 if ($elements[$i]->weight == '') { // not set
357 $elements[$i]->weight = 11; // unity
359 echo "</tr>\n";
360 echo "<tr valign=\"top\"><td align=\"right\"><b>".get_string("elementweight", "exercise").
361 ":</b></td><td>\n";
362 exercise_choose_from_menu($EXERCISE_EWEIGHTS, "weight[]", $elements[$i]->weight, "");
363 echo " </td>\n";
364 echo "</tr>\n";
365 echo "<tr valign=\"top\">\n";
366 echo " <td colspan=\"2\">&nbsp;</td>\n";
367 echo "</tr>\n";
369 echo "</center></table><br />\n";
370 echo "<p><center><b>".get_string("gradetable","exercise")."</b></p></center>\n";
371 echo "<center><table cellpadding=\"5\" border=\"1\"><tr><td align=\"CENTER\">".
372 get_string("numberofnegativeresponses", "exercise");
373 echo "</td><td>". get_string("suggestedgrade", "exercise")."</td></tr>\n";
374 for ($j = $exercise->grade; $j >= 0; $j--) {
375 $numbers[$j] = $j;
377 for ($i=0; $i<=$exercise->nelements; $i++) {
378 echo "<tr><td align=\"CENTER\">$i</td><td align=\"CENTER\">";
379 if (!isset($elements[$i])) { // the "last one" will be!
380 $elements[$i]->description = "";
381 $elements[$i]->maxscore = 0;
383 choose_from_menu($numbers, "maxscore[$i]", $elements[$i]->maxscore, "");
384 echo "</td></tr>\n";
386 break;
388 case 3: // criterion grading
389 for ($j = $exercise->grade; $j >= 0; $j--) {
390 $numbers[$j] = $j;
392 for ($i=0; $i<$exercise->nelements; $i++) {
393 $iplus1 = $i+1;
394 echo "<tr valign=\"top\">\n";
396 echo " <td align=\"right\"><p><b>". get_string("criterion","exercise")." $iplus1:</b></p></td>\n";
397 echo "<td><textarea name=\"description[$i]\" rows=\"3\" cols=\"75\" wrap=\"virtual\">".
398 $elements[$i]->description."</textarea>\n";
399 echo " </td></tr>\n";
400 echo "<tr><td><b>". get_string("suggestedgrade", "exercise").":</b></td><td>\n";
401 choose_from_menu($numbers, "maxscore[$i]", $elements[$i]->maxscore, "");
402 echo "</td></tr>\n";
403 echo "<tr valign=\"top\">\n";
405 echo " <td colspan=\"2\">&nbsp;</td>\n";
406 echo "</tr>\n";
408 break;
410 case 4: // rubric
411 for ($j = 100; $j >= 0; $j--) {
412 $numbers[$j] = $j;
414 if ($rubricsraw = get_records("exercise_rubrics", "exerciseid", $exercise->id)) {
415 foreach ($rubricsraw as $rubric) {
416 $rubrics[$rubric->elementno][$rubric->rubricno] = $rubric->description; // reindex 0,1,2...
419 for ($i=0; $i<$exercise->nelements; $i++) {
420 $iplus1 = $i+1;
421 echo "<tr valign=\"top\">\n";
422 echo " <td align=\"right\"><p><b>". get_string("element","exercise")." $iplus1:</b></p></td>\n";
423 echo "<td><textarea name=\"description[$i]\" rows=\"3\" cols=\"75\" wrap=\"virtual\">".
424 $elements[$i]->description."</textarea>\n";
425 echo " </td></tr>\n";
426 echo "<tr valign=\"top\"><td align=\"right\"><b>".get_string("elementweight", "exercise").
427 ":</b></td><td>\n";
428 exercise_choose_from_menu($EXERCISE_EWEIGHTS, "weight[]", $elements[$i]->weight, "");
429 echo " </td>\n";
430 echo "</tr>\n";
432 for ($j=0; $j<5; $j++) {
433 $jplus1 = $j+1;
434 if (empty($rubrics[$i][$j])) {
435 $rubrics[$i][$j] = "";
437 echo "<tr valign=\"top\">\n";
438 echo " <td align=\"right\"><p><b>". get_string("grade")." $j:</b></p></td>\n";
439 echo "<td><textarea name=\"rubric[$i][$j]\" rows=\"3\" cols=\"75\" wrap=\"virtual\">".
440 $rubrics[$i][$j]."</textarea>\n";
441 echo " </td></tr>\n";
443 echo "<tr valign=\"top\">\n";
444 echo " <td colspan=\"2\">&nbsp;</td>\n";
445 echo "</tr>\n";
447 break;
449 // close table and form
452 </table><br />
453 <input type="submit" value="<?php print_string("savechanges") ?>" />
454 <input type="submit" name="cancel" value="<?php print_string("cancel") ?>" />
455 </center>
457 </form>
458 <?php
462 /****************** insert/update assignment elements (for teachers)***********************/
463 elseif ($action == 'insertelements') {
465 if (!has_capability('mod/exercise:assess', $context)) {
466 error("Only teachers can look at this page");
469 $form = data_submitted();
471 // let's not fool around here, dump the junk!
472 delete_records("exercise_elements", "exerciseid", $exercise->id);
474 // determine wich type of grading
475 switch ($exercise->gradingstrategy) {
476 case 0: // no grading
477 // Insert all the elements that contain something
478 foreach ($form->description as $key => $description) {
479 if ($description) {
480 unset($element);
481 $element->description = $description;
482 $element->exerciseid = $exercise->id;
483 $element->elementno = clean_param($key, PARAM_INT);
484 if (!$element->id = insert_record("exercise_elements", $element)) {
485 error("Could not insert exercise element!");
489 break;
491 case 1: // accumulative grading
492 // Insert all the elements that contain something
493 foreach ($form->description as $key => $description) {
494 if ($description) {
495 unset($element);
496 $element->description = $description;
497 $element->exerciseid = $exercise->id;
498 $element->elementno = clean_param($key, PARAM_INT);
499 if (isset($form->scale[$key])) {
500 $element->scale = $form->scale[$key];
501 switch ($EXERCISE_SCALES[$form->scale[$key]]['type']) {
502 case 'radio' : $element->maxscore = $EXERCISE_SCALES[$form->scale[$key]]['size'] - 1;
503 break;
504 case 'selection' : $element->maxscore = $EXERCISE_SCALES[$form->scale[$key]]['size'];
505 break;
508 if (isset($form->weight[$key])) {
509 $element->weight = $form->weight[$key];
511 if (!$element->id = insert_record("exercise_elements", $element)) {
512 error("Could not insert exercise element!");
516 break;
518 case 2: // error banded grading...
519 case 3: // ...and criterion grading
520 // Insert all the elements that contain something, the number of descriptions is
521 // one less than the number of grades
522 foreach ($form->maxscore as $key => $themaxscore) {
523 unset($element);
524 $element->exerciseid = $exercise->id;
525 $element->elementno = clean_param($key, PARAM_INT);
526 $element->maxscore = $themaxscore;
527 if (isset($form->description[$key])) {
528 $element->description = $form->description[$key];
530 if (isset($form->weight[$key])) {
531 $element->weight = $form->weight[$key];
533 if (!$element->id = insert_record("exercise_elements", $element)) {
534 error("Could not insert exercise element!");
537 break;
539 case 4: // ...and criteria grading
540 // Insert all the elements that contain something
541 foreach ($form->description as $key => $description) {
542 unset($element);
543 $element->exerciseid = $exercise->id;
544 $element->elementno = clean_param($key, PARAM_INT);
545 $element->description = $description;
546 $element->weight = $form->weight[$key];
547 for ($j=0;$j<5;$j++) {
548 if (empty($form->rubric[$key][$j]))
549 break;
551 $element->maxscore = $j - 1;
552 if (!$element->id = insert_record("exercise_elements", $element)) {
553 error("Could not insert exercise element!");
556 // let's not fool around here, dump the junk!
557 delete_records("exercise_rubrics", "exerciseid", $exercise->id);
558 for ($i=0;$i<$exercise->nelements;$i++) {
559 for ($j=0;$j<5;$j++) {
560 unset($element);
561 if (empty($form->rubric[$i][$j])) { // OK to have an element with fewer than 5 items
562 break;
564 $element->exerciseid = $exercise->id;
565 $element->elementno = $i;
566 $element->rubricno = $j;
567 $element->description = $form->rubric[$i][$j];
568 if (!$element->id = insert_record("exercise_rubrics", $element)) {
569 error("Could not insert exercise element!");
573 break;
574 } // end of switch
575 redirect("view.php?id=$cm->id", get_string("savedok", "exercise"));
579 /****************** list assessments for grading (Student submissions)(by teachers)*********************/
580 elseif ($action == 'listungradedstudentsubmissions') {
582 if (!has_capability('mod/exercise:assess', $context)) {
583 error("Only teachers can look at this page");
585 exercise_list_ungraded_assessments($exercise, "student");
586 print_continue("view.php?id=$cm->id");
590 /***************** list assessments for grading student assessments ( linked to the
591 ******************Teacher's submissions) (by teachers)****/
592 elseif ($action == 'listungradedstudentassessments') {
594 if (!has_capability('mod/exercise:assess', $context)) {
595 error("Only teachers can look at this page");
597 exercise_list_ungraded_assessments($exercise, "teacher");
598 print_continue("view.php?id=$cm->id");
602 /****************** list teacher submissions ***********************/
603 elseif ($action == 'listteachersubmissions') {
605 exercise_list_teacher_submissions($exercise, $USER);
606 print_continue("view.php?id=$cm->id");
610 /******************* regrade student assessments ************************************/
611 elseif ($action == 'regradestudentassessments' ) {
613 if (!has_capability('mod/exercise:assess', $context)) {
614 error("Only teachers can look at this page");
616 // get all the student assessments
617 if ($assessments = exercise_get_teacher_submission_assessments($exercise)) {
618 foreach ($assessments as $studentassessment) {
619 if ($studentassessment->timegraded > 0) {
620 if (!$submissions = get_records_select("exercise_submissions",
621 "userid = $studentassessment->userid AND exerciseid = $exercise->id", "timecreated ASC")) {
622 error("Regrade student assessments: student submission not found");
624 foreach ($submissions as $submission) { // only the first one is relavant
625 if (!$teacherassessments = get_records("exercise_assessments", "submissionid",
626 $submission->id, "timecreated ASC")) {
627 error("Regrade student assessments: teacher assessment(s) not found");
629 foreach ($teacherassessments as $teacherassessment) { // only the first one is relavent
630 $newgrade = exercise_compare_assessments($exercise, $studentassessment, $teacherassessment);
631 set_field("exercise_assessments", "gradinggrade", $newgrade, "id", $studentassessment->id);
632 break;
634 break;
639 redirect("submissions.php?id=$cm->id&amp;action=adminlist");
643 /****************** teacher assessment : grading of assessment and submission (from student) ************/
644 elseif ($action == 'teacherassessment') {
646 if (!has_capability('mod/exercise:assess', $context)) {
647 error("Only teachers can look at this page");
650 if (empty($aid)) {
651 error("assessment id missing");
653 if (empty($sid)) {
654 error ("no sid");
656 if (!$assessment = get_record("exercise_assessments", "id", $aid)) {
657 error("Teacher assessment: User's assessment record not found");
659 if (!$submission = get_record("exercise_submissions", "id", $sid)) {
660 error("Teacher assessment: User's submission record not found");
662 exercise_print_teacher_assessment_form($exercise, $assessment, $submission, $_SERVER["HTTP_REFERER"]);
666 /****************** teacher table : show assessments by exercise and teacher ************/
667 elseif ($action == 'teachertable') {
669 if (!has_capability('mod/exercise:assess', $context)) {
670 error("Only teachers can look at this page");
673 exercise_print_teacher_table($course);
674 print_continue("index.php?id=$course->id");
678 /****************** update assessment (by teacher or student) ***************************/
679 elseif ($action == 'updateassessment') {
681 $timenow = time();
682 $form = data_submitted();
684 if (empty($aid)) {
685 error("assessment id missing");
687 if (! $assessment = get_record("exercise_assessments", "id", $aid)) {
688 error("exercise assessment is misconfigured");
691 // first get the assignment elements for maxscores and weights...
692 if (!$elementsraw = get_records("exercise_elements", "exerciseid", $exercise->id, "elementno ASC")) {
693 print_string("noteonassignmentelements", "exercise");
695 else {
696 foreach ($elementsraw as $element) {
697 $elements[] = $element; // to renumber index 0,1,2...
701 // don't fiddle about, delete all the old and then add the new!
702 delete_records("exercise_grades", "assessmentid", $assessment->id);
704 //determine what kind of grading we have
705 switch ($exercise->gradingstrategy) {
706 case 0: // no grading
707 // Insert all the elements that contain something
708 foreach ($form->feedback as $key => $thefeedback) {
709 unset($element);
710 $element->exerciseid = $exercise->id;
711 $element->assessmentid = $assessment->id;
712 $element->elementno = clean_param($key, PARAM_INT);
713 $element->feedback = clean_param($thefeedback, PARAM_CLEAN);
714 if (!$element->id = insert_record("exercise_grades", $element)) {
715 error("Could not insert exercise element!");
718 $grade = 0; // set to satisfy save to db
719 break;
721 case 1: // accumulative grading
722 // Insert all the elements that contain something
723 foreach ($form->grade as $key => $thegrade) {
724 unset($element);
725 $element->exerciseid = $exercise->id;
726 $element->assessmentid = $assessment->id;
727 $element->elementno = clean_param($key, PARAM_INT);
728 $element->feedback = clean_param($form->feedback[$key], PARAM_CLEAN);
729 $element->grade = $thegrade;
730 if (!$element->id = insert_record("exercise_grades", $element)) {
731 error("Could not insert exercise element!");
734 // now work out the grade...
735 $rawgrade=0;
736 $totalweight=0;
737 foreach ($form->grade as $key => $grade) {
738 $maxscore = $elements[$key]->maxscore;
739 $weight = $EXERCISE_EWEIGHTS[$elements[$key]->weight];
740 if ($weight > 0) {
741 $totalweight += $weight;
743 $rawgrade += ($grade / $maxscore) * $weight;
744 // echo "\$key, \$maxscore, \$weight, \$totalweight, \$grade, \$rawgrade : $key, $maxscore, $weight, $totalweight, $grade, $rawgrade<br />";
746 $grade = 100.0 * ($rawgrade / $totalweight);
747 break;
749 case 2: // error banded graded
750 // Insert all the elements that contain something
751 $error = 0.0;
752 for ($i =0; $i < $exercise->nelements; $i++) {
753 unset($element);
754 $element->exerciseid = $exercise->id;
755 $element->assessmentid = $assessment->id;
756 $element->elementno = $i;
757 $element->feedback = clean_param($form->feedback[$i], PARAM_CLEAN);
758 $element->grade = $form->grade[$i];
759 if (!$element->id = insert_record("exercise_grades", $element)) {
760 error("Could not insert exercise element!");
762 if (empty($form->grade[$i])){
763 $error += $EXERCISE_EWEIGHTS[$elements[$i]->weight];
766 // now save the adjustment
767 unset($element);
768 $i = $exercise->nelements;
769 $element->exerciseid = $exercise->id;
770 $element->assessmentid = $assessment->id;
771 $element->elementno = $i;
772 $element->grade = $form->grade[$i];
773 if (!$element->id = insert_record("exercise_grades", $element)) {
774 error("Could not insert exercise element!");
776 $grade = ($elements[intval($error + 0.5)]->maxscore + $form->grade[$i])
777 * 100.0 / $exercise->grade;
778 // echo "<p><b>".get_string("weightederrorcount", "exercise", intval($error + 0.5)).
779 // " ".get_string("adjustment", "exercise").": ".$form->grade[$i]."</b>\n";
780 // check the grade for sanity!
781 if ($grade > 100.0) {
782 $grade = 100.0;
784 if ($grade < 0.0) {
785 $grade = 0.0;
787 break;
789 case 3: // criteria grading
790 // save in the selected criteria value in element zero,
791 unset($element);
792 $element->exerciseid = $exercise->id;
793 $element->assessmentid = $assessment->id;
794 $element->elementno = 0;
795 $element->grade = $form->grade[0];
796 if (!$element->id = insert_record("exercise_grades", $element)) {
797 error("Could not insert exercise element!");
799 // now save the adjustment in element one
800 unset($element);
801 $element->exerciseid = $exercise->id;
802 $element->assessmentid = $assessment->id;
803 $element->elementno = 1;
804 $element->grade = $form->grade[1];
805 if (!$element->id = insert_record("exercise_grades", $element)) {
806 error("Could not insert exercise element!");
808 $grade = ($elements[$form->grade[0]]->maxscore + $form->grade[1]) * 100 / $exercise->grade;
809 // check the grade for sanity!
810 if ($grade >100.0) {
811 $grade = 100.0;
813 if ($grade < 0.0) {
814 $grade = 0.0;
816 break;
818 case 4: // rubric grading (identical to accumulative grading)
819 // Insert all the elements that contain something
820 foreach ($form->grade as $key => $thegrade) {
821 unset($element);
822 $element->exerciseid = $exercise->id;
823 $element->assessmentid = $assessment->id;
824 $element->elementno = clean_param($key, PARAM_INT);
825 $element->feedback = clean_param($form->feedback[$key], PARAM_CLEAN);
826 $element->grade = $thegrade;
827 if (!$element->id = insert_record("exercise_grades", $element)) {
828 error("Could not insert exercise element!");
831 // now work out the grade...
832 $rawgrade=0;
833 $totalweight=0;
834 foreach ($form->grade as $key => $grade) {
835 $maxscore = $elements[$key]->maxscore;
836 $weight = $EXERCISE_EWEIGHTS[$elements[$key]->weight];
837 if ($weight > 0) {
838 $totalweight += $weight;
840 $rawgrade += ($grade / $maxscore) * $weight;
842 $grade = 100.0 * ($rawgrade / $totalweight);
843 break;
845 } // end of switch
847 // update the time of the assessment record (may be re-edited)...
848 set_field("exercise_assessments", "timecreated", $timenow, "id", $assessment->id);
849 set_field("exercise_assessments", "grade", $grade, "id", $assessment->id);
850 // ...and clear any grading of this assessment (these assessments are never graded but...)
851 set_field("exercise_assessments", "timegraded", 0, "id", $assessment->id);
852 set_field("exercise_assessments", "gradinggrade", 0, "id", $assessment->id);
854 // any comment?
855 if (!empty($form->generalcomment)) {
856 set_field("exercise_assessments", "generalcomment", clean_param($form->generalcomment, PARAM_CLEAN), "id", $assessment->id);
859 // is user allowed to resubmit?
860 if (has_capability('mod/exercise:assess', $context)) {
861 if (!$submission = get_record("exercise_submissions", "id", $assessment->submissionid)) {
862 error ("Updateassessment: submission record not found");
864 if ($form->resubmit == 1) {
865 set_field("exercise_submissions", "resubmit", 1, "id", $submission->id);
867 else {
868 // clear resubmit flag
869 set_field("exercise_submissions", "resubmit", 0, "id", $submission->id);
873 add_to_log($course->id, "exercise", "assess", "view.php?id=$cm->id", "$assessment->id");
875 // set up return address
876 if (!$returnto = $form->returnto) {
877 $returnto = "view.php?id=$cm->id";
880 // show grade if grading strategy is not zero
881 if ($exercise->gradingstrategy) {
882 redirect($returnto, "<p align=\"center\"><b>".get_string("thegradeis", "exercise").": ".
883 number_format($grade * $exercise->grade / 100.0, 1)." (".get_string("maximumgrade").
884 " ".number_format($exercise->grade).")</b></p>", 1);
886 else {
887 redirect($returnto);
892 /****************** update teacher assessment (by teacher only) ***************************/
893 elseif ($action == 'updateteacherassessment') {
895 if (!has_capability('mod/exercise:assess', $context)) {
896 error("Only teachers can look at this page");
899 $timenow = time();
900 $form = data_submitted();
902 // first do the (teacher's) assessment of the student's submission
903 if (! $submission = get_record("exercise_submissions", "id", $form->sid)) {
904 error("Update teacher assessment: student's submission record not found");
906 if (!$assessment = exercise_get_submission_assessment($submission, $USER)) {
907 error("Update teacher assessment: teacher's assessment record not found");
910 // first get the assignment elements for maxscores and weights...
911 if (!$elementsraw = get_records("exercise_elements", "exerciseid", $exercise->id, "elementno ASC")) {
912 print_string("noteonassignmentelements", "exercise");
914 else {
915 foreach ($elementsraw as $element) {
916 $elements[] = $element; // to renumber index 0,1,2...
920 // don't fiddle about, delete all the old and then add the new!
921 delete_records("exercise_grades", "assessmentid", $assessment->id);
923 //determine what kind of grading we have
924 switch ($exercise->gradingstrategy) {
925 case 0: // no grading
926 // Insert all the elements that contain something
927 foreach ($form->feedback as $key => $thefeedback) {
928 unset($element);
929 $element->exerciseid = $exercise->id;
930 $element->assessmentid = $assessment->id;
931 $element->elementno = clean_param($key, PARAM_INT);
932 $element->feedback = clean_param($thefeedback, PARAM_CLEAN);
933 if (!$element->id = insert_record("exercise_grades", $element)) {
934 error("Could not insert exercise element!");
937 $grade = 0; // set to satisfy save to db
938 break;
940 case 1: // accumulative grading
941 // Insert all the elements that contain something
942 foreach ($form->grade as $key => $thegrade) {
943 unset($element);
944 $element->exerciseid = $exercise->id;
945 $element->assessmentid = $assessment->id;
946 $element->elementno = clean_param($key, PARAM_INT);
947 $element->feedback = clean_param($form->feedback[$key], PARAM_CLEAN);
948 $element->grade = $thegrade;
949 if (!$element->id = insert_record("exercise_grades", $element)) {
950 error("Could not insert exercise element!");
953 // now work out the grade...
954 $rawgrade=0;
955 $totalweight=0;
956 foreach ($form->grade as $key => $grade) {
957 $maxscore = $elements[$key]->maxscore;
958 $weight = $EXERCISE_EWEIGHTS[$elements[$key]->weight];
959 if ($weight > 0) {
960 $totalweight += $weight;
962 $rawgrade += ($grade / $maxscore) * $weight;
963 // echo "\$key, \$maxscore, \$weight, \$totalweight, \$grade, \$rawgrade : $key, $maxscore, $weight, $totalweight, $grade, $rawgrade<br />";
965 $grade = 100.0 * ($rawgrade / $totalweight);
966 break;
968 case 2: // error banded graded
969 // Insert all the elements that contain something
970 $error = 0.0;
971 for ($i =0; $i < $exercise->nelements; $i++) {
972 unset($element);
973 $element->exerciseid = $exercise->id;
974 $element->assessmentid = $assessment->id;
975 $element->elementno = $i;
976 $element->feedback = clean_param($form->feedback[$i], PARAM_CLEAN);
977 $element->grade = $form->grade[$i];
978 if (!$element->id = insert_record("exercise_grades", $element)) {
979 error("Could not insert exercise element!");
981 if (empty($form->grade[$i])){
982 $error += $EXERCISE_EWEIGHTS[$elements[$i]->weight];
985 // now save the adjustment
986 unset($element);
987 $i = $exercise->nelements;
988 $element->exerciseid = $exercise->id;
989 $element->assessmentid = $assessment->id;
990 $element->elementno = $i;
991 $element->grade = $form->grade[$i];
992 if (!$element->id = insert_record("exercise_grades", $element)) {
993 error("Could not insert exercise element!");
995 $grade = ($elements[intval($error + 0.5)]->maxscore + $form->grade[$i]) * 100 / $exercise->grade;
996 echo "<p><b>".get_string("weightederrorcount", "exercise", intval($error + 0.5))."</b></p>\n";
997 break;
999 case 3: // criteria grading
1000 // save in the selected criteria value in element zero,
1001 unset($element);
1002 $element->exerciseid = $exercise->id;
1003 $element->assessmentid = $assessment->id;
1004 $element->elementno = 0;
1005 $element->grade = $form->grade[0];
1006 if (!$element->id = insert_record("exercise_grades", $element)) {
1007 error("Could not insert exercise element!");
1009 // now save the adjustment in element one
1010 unset($element);
1011 $element->exerciseid = $exercise->id;
1012 $element->assessmentid = $assessment->id;
1013 $element->elementno = 1;
1014 $element->grade = $form->grade[1];
1015 if (!$element->id = insert_record("exercise_grades", $element)) {
1016 error("Could not insert exercise element!");
1018 $grade = ($elements[$form->grade[0]]->maxscore + $form->grade[1]) * 100 / $exercise->grade;
1019 break;
1021 case 4: // rubric grading (identical to accumulative grading)
1022 // Insert all the elements that contain something
1023 foreach ($form->grade as $key => $thegrade) {
1024 unset($element);
1025 $element->exerciseid = $exercise->id;
1026 $element->assessmentid = $assessment->id;
1027 $element->elementno = clean_param($key, PARAM_INT);
1028 $element->feedback = clean_param($form->feedback[$key], PARAM_CLEAN);
1029 $element->grade = $thegrade;
1030 if (!$element->id = insert_record("exercise_grades", $element)) {
1031 error("Could not insert exercise element!");
1034 // now work out the grade...
1035 $rawgrade=0;
1036 $totalweight=0;
1037 foreach ($form->grade as $key => $grade) {
1038 $maxscore = $elements[$key]->maxscore;
1039 $weight = $EXERCISE_EWEIGHTS[$elements[$key]->weight];
1040 if ($weight > 0) {
1041 $totalweight += $weight;
1043 $rawgrade += ($grade / $maxscore) * $weight;
1045 $grade = 100.0 * ($rawgrade / $totalweight);
1046 break;
1048 } // end of switch
1050 // update the time of the assessment record (may be re-edited)...
1051 set_field("exercise_assessments", "timecreated", $timenow, "id", $assessment->id);
1052 set_field("exercise_assessments", "grade", $grade, "id", $assessment->id);
1053 // ...and clear any grading of this assessment (never needed but...)
1054 set_field("exercise_assessments", "timegraded", 0, "id", $assessment->id);
1055 set_field("exercise_assessments", "gradinggrade", 0, "id", $assessment->id);
1057 // any comment?
1058 if (!empty($form->generalcomment)) {
1059 set_field("exercise_assessments", "generalcomment", clean_param($form->generalcomment, PARAM_CLEAN), "id", $assessment->id);
1062 // now calculate the (grading) grade of the student's assessment...
1063 if (!$stassessment = get_record("exercise_assessments", "id", $form->said)) {
1064 error("Update teacher assessment: student's assessment record not found");
1066 $gradinggrade = exercise_compare_assessments($exercise, $assessment, $stassessment);
1067 // ...and save the grade for the assessment
1068 set_field("exercise_assessments", "gradinggrade", $gradinggrade, "id", $stassessment->id);
1069 set_field("exercise_assessments", "timegraded", $timenow, "id", $stassessment->id);
1070 set_field("exercise_assessments", "mailed", 0, "id", $stassessment->id);
1071 echo "<centre><b>".get_string("savedok", "exercise")."</b></centre><br />\n";
1073 add_to_log($course->id, "exercise", "grade", "view.php?id=$cm->id", "$stassessment->id");
1075 // is user allowed to resubmit?
1076 if ($form->resubmit == 1) {
1077 set_field("exercise_submissions", "resubmit", 1, "id", $submission->id);
1079 else {
1080 // clear resubmit flag
1081 set_field("exercise_submissions", "resubmit", 0, "id", $submission->id);
1084 add_to_log($course->id, "exercise", "assess", "view.php?id=$cm->id", "$assessment->id");
1086 // set up return address
1087 if (!$returnto = $form->returnto) {
1088 $returnto = "view.php?id=$cm->id";
1091 // show grade if grading strategy is not zero
1092 if ($exercise->gradingstrategy) {
1093 redirect($returnto, "<p align=\"center\"><b>".get_string("gradeforstudentsassessment",
1094 "exercise", $course->student).": ".number_format($gradinggrade * $exercise->gradinggrade / 100.0, 1).
1095 " (".get_string("maximumgrade")." ".number_format($exercise->gradinggrade, 1).")</b></p><p><b>".
1096 get_string("thegradeis", "exercise").": ".number_format($grade * $exercise->grade / 100.0, 1).
1097 " (".get_string("maximumgrade")." ".number_format($exercise->grade, 1).")</b></p>");
1099 else {
1100 redirect($returnto);
1105 /****************** update grading grade(by teacher) ***************************/
1106 elseif ($action == 'updategradinggrade') {
1108 if (!has_capability('mod/exercise:assess', $context)) {
1109 error("Only teachers can look at this page");
1112 if (empty($aid)) {
1113 error("submission id missing");
1115 // normalise gradinggrade
1116 $gradinggrade = $_POST['gradinggrade'] * 100 / $exercise->gradinggrade;
1117 if (!set_field("exercise_assessments", "gradinggrade", $gradinggrade, "id",
1118 $aid)) {
1119 error("Update grading grade: asseesment not updated");
1121 redirect("submissions.php?id=$cm->id&amp;action=adminlist", get_string("savedok", "exercise"), 1);
1125 /****************** user confirm delete ************************************/
1126 elseif ($action == 'userconfirmdelete' ) {
1128 if (empty($aid)) {
1129 error("User confirm delete: assessment id missing");
1132 notice_yesno(get_string("confirmdeletionofthisitem","exercise", get_string("assessment", "exercise")),
1133 "assessments.php?action=userdelete&amp;id=$cm->id&amp;aid=$aid", "view.php?id=$cm->id");
1137 /****************** user delete ************************************/
1138 elseif ($action == 'userdelete' ) {
1140 if (empty($aid)) {
1141 error("User delete: assessment id missing");
1144 print_string("deleting", "exercise");
1145 // first delete all the associated records...
1146 delete_records("exercise_grades", "assessmentid", $aid);
1147 // ...now delete the assessment...
1148 delete_records("exercise_assessments", "id", $aid);
1150 print_continue("view.php?id=$cm->id");
1154 /****************** view assessment ***********************/
1155 elseif ($action == 'viewassessment') {
1157 if (empty($aid)) {
1158 error("assessment id missing");
1161 // get the assessment record
1162 if (!$assessment = get_record("exercise_assessments", "id", $aid)) {
1163 error("Assessment record not found");
1166 // show assessment but don't allow changes
1167 exercise_print_assessment_form($exercise, $assessment);
1169 print_continue("view.php?id=$cm->id");
1173 /*************** no man's land **************************************/
1174 else {
1175 error("Fatal Error: Unknown Action: ".$action."\n");
1178 print_footer($course);