Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / exercise / assessments.php
blob0e5d2f9b68d11c4a3e5c741189164d2810e96cb5
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 $navigation = build_navigation($strassessments, $cm);
66 print_header_simple(format_string($exercise->name), "", $navigation,
67 "", "", true);
69 //...get the action
72 /******************* admin amend Grading Grade ************************************/
73 if ($action == 'adminamendgradinggrade' ) {
75 if (!has_capability('mod/exercise:assess', $context)) {
76 error("Only teachers can look at this page");
78 if (empty($aid)) {
79 error("Admin Amend Grading grade: assessment id missing");
82 if (!$assessment = get_record("exercise_assessments", "id", $aid)) {
83 error("Amin Amend Grading grade: assessment not found");
85 print_heading(get_string("amend", "exercise")." ".get_string("gradeforstudentsassessment",
86 "exercise", $course->student));
87 echo "<form id=\"amendgrade\" method=\"post\" action=\"assessments.php\">\n";
88 echo "<input type=\"hidden\" name=\"aid\" value=\"$aid\" />\n";
89 echo "<input type=\"hidden\" name=\"action\" value=\"updategradinggrade\" />\n";
90 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
91 echo "<table width=\"50%\" align=\"center\" border=\"1\" />\n";
92 echo "<tr><td align=\"right\"><b>".get_string("gradeforstudentsassessment", "exercise",
93 $course->student)." :</td><td>\n";
94 // set up coment scale
95 for ($i=$exercise->gradinggrade; $i>=0; $i--) {
96 $num[$i] = $i;
98 choose_from_menu($num, "gradinggrade",
99 number_format($exercise->gradinggrade * $assessment->gradinggrade / 100, 0), "");
100 echo "</td></tr>\n";
101 echo "<tr><td colspan=\"2\" align=\"center\">";
102 echo "<input type=\"submit\" value=\"".get_string("amend", "exercise")."\" />\n";
103 echo "</td></tr>\n";
104 echo "</table>\n";
105 echo "</center>";
106 echo "</form>\n";
110 /******************* admin confirm delete ************************************/
111 elseif ($action == 'adminconfirmdelete' ) {
113 if (!has_capability('mod/exercise:assess', $context)) {
114 error("Only teachers can look at this page");
116 if (empty($aid)) {
117 error("Admin confirm delete: assessment id missing");
120 notice_yesno(get_string("confirmdeletionofthisitem","exercise", get_string("assessment", "exercise")),
121 "assessments.php?action=admindelete&amp;id=$cm->id&amp;aid=$aid",
122 "submissions.php?action=adminlist&amp;id=$cm->id");
126 /******************* admin delete ************************************/
127 elseif ($action == 'admindelete' ) {
129 if (!has_capability('mod/exercise:assess', $context)) {
130 error("Only teachers can look at this page");
132 if (empty($aid)) {
133 error("Admin delete: submission id missing");
136 print_string("deleting", "exercise");
137 // first delete all the associated records...
138 delete_records("exercise_grades", "assessmentid", $aid);
139 // ...now delete the assessment...
140 delete_records("exercise_assessments", "id", $aid);
142 print_continue("submissions.php?id=$cm->id&amp;action=adminlist");
146 /*********************** admin list of asssessments (of a submission) (by teachers)**************/
147 elseif ($action == 'adminlist') {
149 if (!has_capability('mod/exercise:assess', $context)) {
150 error("Only teachers can look at this page");
153 if (empty($sid)) {
154 error ("exercise asssessments: adminlist called with no sid");
156 $submission = get_record("exercise_submissions", "id", $sid);
157 exercise_print_assessments_for_admin($exercise, $submission);
158 print_continue("submissions.php?action=adminlist&amp;id=$cm->id");
162 /****************** admin list of asssessments by a student (used by teachers only )******************/
163 elseif ($action == 'adminlistbystudent') {
165 if (!has_capability('mod/exercise:assess', $context)) {
166 error("Only teachers can look at this page");
169 if (empty($userid)) {
170 error ("exercise asssessments: adminlistbystudent called with no userid");
172 $user = get_record("user", "id", $userid);
173 exercise_print_assessments_by_user_for_admin($exercise, $user);
174 print_continue("submissions.php?action=adminlist&amp;id=$cm->id");
178 /****************** Assess resubmission (by teacher) ***************************/
179 elseif ($action == 'assessresubmission') {
181 if (empty($sid)) {
182 error ("exercise asssessments: assessresubmission called with no sid");
185 if (! $submission = get_record("exercise_submissions", "id", $sid)) {
186 error("Assess submission is misconfigured - no submission record!");
188 if (!$submissionowner = get_record("user", "id", $submission->userid)) {
189 error("Assess resubmission: user record not found");
192 // there can be an assessment record, if there isn't...
193 if (!$assessment = exercise_get_submission_assessment($submission, $USER)) {
194 if (!$submissions = exercise_get_user_submissions($exercise, $submissionowner)) {
195 error("Assess resubmission: submission records not found");
197 $lastone= '';
198 // just the last but one submission
199 foreach ($submissions as $submission) {
200 $prevsubmission = $lastone;
201 $lastone = $submission;
203 // get the teacher's assessment of the student's previous submission
204 if (!$prevassessment = exercise_get_submission_assessment($prevsubmission, $USER)) {
205 error("Assess resubmission: Previous assessment record not found");
207 // copy this assessment with comments...
208 $assessment = exercise_copy_assessment($prevassessment, $submission, true);
211 print_heading(get_string("thisisaresubmission", "exercise",
212 fullname($submissionowner)));
213 // show assessment and allow changes
214 exercise_print_assessment_form($exercise, $assessment, true, $_SERVER["HTTP_REFERER"]);
218 /****************** Assess submission (by teacher or student) ***************************/
219 elseif ($action == 'assesssubmission') {
221 if (empty($sid)) {
222 error ("exercise asssessments: assesssubmission called with no sid");
225 if (! $submission = get_record("exercise_submissions", "id", $sid)) {
226 error("Assess submission is misconfigured - no submission record!");
229 // there can be an assessment record (for teacher submissions), if there isn't...
230 if (!$assessment = exercise_get_submission_assessment($submission, $USER)) {
231 $yearfromnow = time() + 365 * 86400;
232 // ...create one and set timecreated way in the future, this is reset when record is updated
233 $assessment->exerciseid = $exercise->id;
234 $assessment->submissionid = $submission->id;
235 $assessment->userid = $USER->id;
236 $assessment->grade = -1; // set impossible grade
237 $assessment->timecreated = $yearfromnow;
238 $assessment->timegraded = 0;
239 if (!$assessment->id = insert_record("exercise_assessments", $assessment)) {
240 error("Could not insert exercise assessment!");
244 // show assessment and allow changes
245 exercise_print_assessment_form($exercise, $assessment, true, $_SERVER["HTTP_REFERER"]);
249 /****************** display grading form (viewed by student) *********************************/
250 elseif ($action == 'displaygradingform') {
252 print_heading_with_help(get_string("specimenassessmentform", "exercise"), "specimen", "exercise");
254 exercise_print_assessment_form($exercise); // called with no assessment
255 print_continue("view.php?id=$cm->id");
259 /****************** edit assessment elements (for teachers) ***********************/
260 elseif ($action == 'editelements') {
262 if (!has_capability('mod/exercise:assess', $context)) {
263 error("Only teachers can look at this page");
266 $count = count_records("exercise_grades", "exerciseid", $exercise->id);
267 if ($exercise->phase > 1 and $count) {
268 notify(get_string("warningonamendingelements", "exercise"));
270 // set up heading, form and table
271 print_heading_with_help(get_string("editingassessmentelements", "exercise"), "elements", "exercise");
273 <form id="form" method="post" action="assessments.php">
274 <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
275 <input type="hidden" name="action" value="insertelements" />
276 <center><table cellpadding="5" border="1">
277 <?php
279 // get existing elements, if none set up appropriate default ones
280 if ($elementsraw = get_records("exercise_elements", "exerciseid", $exercise->id, "elementno ASC" )) {
281 foreach ($elementsraw as $element) {
282 $elements[] = $element; // to renumber index 0,1,2...
285 // check for missing elements (this happens either the first time round or when the number
286 // of elements is icreased)
287 for ($i=0; $i<$exercise->nelements; $i++) {
288 if (!isset($elements[$i])) {
289 $elements[$i]->description = '';
290 $elements[$i]->scale =0;
291 $elements[$i]->maxscore = 0;
292 $elements[$i]->weight = 11;
295 switch ($exercise->gradingstrategy) {
296 case 0: // no grading
297 for ($i=0; $i<$exercise->nelements; $i++) {
298 $iplus1 = $i+1;
299 echo "<tr valign=\"top\">\n";
300 echo " <td align=\"right\"><p><b>". get_string("element","exercise")." $iplus1:</b></p></td>\n";
301 echo "<td><textarea name=\"description[]\" rows=\"3\" cols=\"75\" wrap=\"virtual\">".
302 $elements[$i]->description."</textarea>\n";
303 echo " </td></tr>\n";
304 echo "<tr valign=\"top\">\n";
305 echo " <td colspan=\"2\">&nbsp;</td>\n";
306 echo "</tr>\n";
308 break;
310 case 1: // accumulative grading
311 // set up scales name
312 foreach ($EXERCISE_SCALES as $KEY => $SCALE) {
313 $SCALES[] = $SCALE['name'];
316 for ($i=0; $i<$exercise->nelements; $i++) {
317 $iplus1 = $i+1;
318 echo "<tr valign=\"top\">\n";
319 echo " <td align=\"right\"><p><b>". get_string("element","exercise")." $iplus1:</b></p></td>\n";
320 echo "<td><textarea name=\"description[]\" rows=\"3\" cols=\"75\" wrap=\"virtual\">".
321 $elements[$i]->description."</textarea>\n";
322 echo " </td></tr>\n";
323 echo "<tr valign=\"top\">\n";
324 echo " <td align=\"right\"><p><b>". get_string("typeofscale", "exercise"). ":</b></p></td>\n";
325 echo "<td valign=\"top\">\n";
326 choose_from_menu($SCALES, "scale[]", $elements[$i]->scale, "");
327 if ($elements[$i]->weight == '') { // not set
328 $elements[$i]->weight = 11; // unity
330 echo "</td></tr>\n";
331 echo "<tr valign=\"top\"><td align=\"right\"><b>".get_string("elementweight", "exercise").
332 ":</b></td><td>\n";
333 exercise_choose_from_menu($EXERCISE_EWEIGHTS, "weight[]", $elements[$i]->weight, "");
334 echo " </td>\n";
335 echo "</tr>\n";
336 echo "<tr valign=\"top\">\n";
337 echo " <td colspan=\"2\">&nbsp;</td>\n";
338 echo "</tr>\n";
340 break;
342 case 2: // error banded grading
343 for ($i=0; $i<$exercise->nelements; $i++) {
344 $iplus1 = $i+1;
345 echo "<tr valign=\"top\">\n";
346 echo " <td align=\"right\"><p><b>". get_string("element","exercise")." $iplus1:</b></p></td>\n";
347 echo "<td><textarea name=\"description[$i]\" rows=\"3\" cols=\"75\" wrap=\"virtual\">".
348 $elements[$i]->description."</textarea>\n";
349 echo " </td></tr>\n";
350 if ($elements[$i]->weight == '') { // not set
351 $elements[$i]->weight = 11; // unity
353 echo "</tr>\n";
354 echo "<tr valign=\"top\"><td align=\"right\"><b>".get_string("elementweight", "exercise").
355 ":</b></td><td>\n";
356 exercise_choose_from_menu($EXERCISE_EWEIGHTS, "weight[]", $elements[$i]->weight, "");
357 echo " </td>\n";
358 echo "</tr>\n";
359 echo "<tr valign=\"top\">\n";
360 echo " <td colspan=\"2\">&nbsp;</td>\n";
361 echo "</tr>\n";
363 echo "</center></table><br />\n";
364 echo "<p><center><b>".get_string("gradetable","exercise")."</b></p></center>\n";
365 echo "<center><table cellpadding=\"5\" border=\"1\"><tr><td align=\"CENTER\">".
366 get_string("numberofnegativeresponses", "exercise");
367 echo "</td><td>". get_string("suggestedgrade", "exercise")."</td></tr>\n";
368 for ($j = $exercise->grade; $j >= 0; $j--) {
369 $numbers[$j] = $j;
371 for ($i=0; $i<=$exercise->nelements; $i++) {
372 echo "<tr><td align=\"CENTER\">$i</td><td align=\"CENTER\">";
373 if (!isset($elements[$i])) { // the "last one" will be!
374 $elements[$i]->description = "";
375 $elements[$i]->maxscore = 0;
377 choose_from_menu($numbers, "maxscore[$i]", $elements[$i]->maxscore, "");
378 echo "</td></tr>\n";
380 break;
382 case 3: // criterion grading
383 for ($j = $exercise->grade; $j >= 0; $j--) {
384 $numbers[$j] = $j;
386 for ($i=0; $i<$exercise->nelements; $i++) {
387 $iplus1 = $i+1;
388 echo "<tr valign=\"top\">\n";
390 echo " <td align=\"right\"><p><b>". get_string("criterion","exercise")." $iplus1:</b></p></td>\n";
391 echo "<td><textarea name=\"description[$i]\" rows=\"3\" cols=\"75\" wrap=\"virtual\">".
392 $elements[$i]->description."</textarea>\n";
393 echo " </td></tr>\n";
394 echo "<tr><td><b>". get_string("suggestedgrade", "exercise").":</b></td><td>\n";
395 choose_from_menu($numbers, "maxscore[$i]", $elements[$i]->maxscore, "");
396 echo "</td></tr>\n";
397 echo "<tr valign=\"top\">\n";
399 echo " <td colspan=\"2\">&nbsp;</td>\n";
400 echo "</tr>\n";
402 break;
404 case 4: // rubric
405 for ($j = 100; $j >= 0; $j--) {
406 $numbers[$j] = $j;
408 if ($rubricsraw = get_records("exercise_rubrics", "exerciseid", $exercise->id)) {
409 foreach ($rubricsraw as $rubric) {
410 $rubrics[$rubric->elementno][$rubric->rubricno] = $rubric->description; // reindex 0,1,2...
413 for ($i=0; $i<$exercise->nelements; $i++) {
414 $iplus1 = $i+1;
415 echo "<tr valign=\"top\">\n";
416 echo " <td align=\"right\"><p><b>". get_string("element","exercise")." $iplus1:</b></p></td>\n";
417 echo "<td><textarea name=\"description[$i]\" rows=\"3\" cols=\"75\" wrap=\"virtual\">".
418 $elements[$i]->description."</textarea>\n";
419 echo " </td></tr>\n";
420 echo "<tr valign=\"top\"><td align=\"right\"><b>".get_string("elementweight", "exercise").
421 ":</b></td><td>\n";
422 exercise_choose_from_menu($EXERCISE_EWEIGHTS, "weight[]", $elements[$i]->weight, "");
423 echo " </td>\n";
424 echo "</tr>\n";
426 for ($j=0; $j<5; $j++) {
427 $jplus1 = $j+1;
428 if (empty($rubrics[$i][$j])) {
429 $rubrics[$i][$j] = "";
431 echo "<tr valign=\"top\">\n";
432 echo " <td align=\"right\"><p><b>". get_string("grade")." $j:</b></p></td>\n";
433 echo "<td><textarea name=\"rubric[$i][$j]\" rows=\"3\" cols=\"75\" wrap=\"virtual\">".
434 $rubrics[$i][$j]."</textarea>\n";
435 echo " </td></tr>\n";
437 echo "<tr valign=\"top\">\n";
438 echo " <td colspan=\"2\">&nbsp;</td>\n";
439 echo "</tr>\n";
441 break;
443 // close table and form
446 </table><br />
447 <input type="submit" value="<?php print_string("savechanges") ?>" />
448 <input type="submit" name="cancel" value="<?php print_string("cancel") ?>" />
449 </center>
451 </form>
452 <?php
456 /****************** insert/update assignment elements (for teachers)***********************/
457 elseif ($action == 'insertelements') {
459 if (!has_capability('mod/exercise:assess', $context)) {
460 error("Only teachers can look at this page");
463 $form = data_submitted();
465 // let's not fool around here, dump the junk!
466 delete_records("exercise_elements", "exerciseid", $exercise->id);
468 // determine wich type of grading
469 switch ($exercise->gradingstrategy) {
470 case 0: // no grading
471 // Insert all the elements that contain something
472 foreach ($form->description as $key => $description) {
473 if ($description) {
474 unset($element);
475 $element->description = $description;
476 $element->exerciseid = $exercise->id;
477 $element->elementno = clean_param($key, PARAM_INT);
478 if (!$element->id = insert_record("exercise_elements", $element)) {
479 error("Could not insert exercise element!");
483 break;
485 case 1: // accumulative grading
486 // Insert all the elements that contain something
487 foreach ($form->description as $key => $description) {
488 if ($description) {
489 unset($element);
490 $element->description = $description;
491 $element->exerciseid = $exercise->id;
492 $element->elementno = clean_param($key, PARAM_INT);
493 if (isset($form->scale[$key])) {
494 $element->scale = $form->scale[$key];
495 switch ($EXERCISE_SCALES[$form->scale[$key]]['type']) {
496 case 'radio' : $element->maxscore = $EXERCISE_SCALES[$form->scale[$key]]['size'] - 1;
497 break;
498 case 'selection' : $element->maxscore = $EXERCISE_SCALES[$form->scale[$key]]['size'];
499 break;
502 if (isset($form->weight[$key])) {
503 $element->weight = $form->weight[$key];
505 if (!$element->id = insert_record("exercise_elements", $element)) {
506 error("Could not insert exercise element!");
510 break;
512 case 2: // error banded grading...
513 case 3: // ...and criterion grading
514 // Insert all the elements that contain something, the number of descriptions is
515 // one less than the number of grades
516 foreach ($form->maxscore as $key => $themaxscore) {
517 unset($element);
518 $element->exerciseid = $exercise->id;
519 $element->elementno = clean_param($key, PARAM_INT);
520 $element->maxscore = $themaxscore;
521 if (isset($form->description[$key])) {
522 $element->description = $form->description[$key];
524 if (isset($form->weight[$key])) {
525 $element->weight = $form->weight[$key];
527 if (!$element->id = insert_record("exercise_elements", $element)) {
528 error("Could not insert exercise element!");
531 break;
533 case 4: // ...and criteria grading
534 // Insert all the elements that contain something
535 foreach ($form->description as $key => $description) {
536 unset($element);
537 $element->exerciseid = $exercise->id;
538 $element->elementno = clean_param($key, PARAM_INT);
539 $element->description = $description;
540 $element->weight = $form->weight[$key];
541 for ($j=0;$j<5;$j++) {
542 if (empty($form->rubric[$key][$j]))
543 break;
545 $element->maxscore = $j - 1;
546 if (!$element->id = insert_record("exercise_elements", $element)) {
547 error("Could not insert exercise element!");
550 // let's not fool around here, dump the junk!
551 delete_records("exercise_rubrics", "exerciseid", $exercise->id);
552 for ($i=0;$i<$exercise->nelements;$i++) {
553 for ($j=0;$j<5;$j++) {
554 unset($element);
555 if (empty($form->rubric[$i][$j])) { // OK to have an element with fewer than 5 items
556 break;
558 $element->exerciseid = $exercise->id;
559 $element->elementno = $i;
560 $element->rubricno = $j;
561 $element->description = $form->rubric[$i][$j];
562 if (!$element->id = insert_record("exercise_rubrics", $element)) {
563 error("Could not insert exercise element!");
567 break;
568 } // end of switch
569 redirect("view.php?id=$cm->id", get_string("savedok", "exercise"));
573 /****************** list assessments for grading (Student submissions)(by teachers)*********************/
574 elseif ($action == 'listungradedstudentsubmissions') {
576 if (!has_capability('mod/exercise:assess', $context)) {
577 error("Only teachers can look at this page");
579 exercise_list_ungraded_assessments($exercise, "student");
580 print_continue("view.php?id=$cm->id");
584 /***************** list assessments for grading student assessments ( linked to the
585 ******************Teacher's submissions) (by teachers)****/
586 elseif ($action == 'listungradedstudentassessments') {
588 if (!has_capability('mod/exercise:assess', $context)) {
589 error("Only teachers can look at this page");
591 exercise_list_ungraded_assessments($exercise, "teacher");
592 print_continue("view.php?id=$cm->id");
596 /****************** list teacher submissions ***********************/
597 elseif ($action == 'listteachersubmissions') {
599 exercise_list_teacher_submissions($exercise, $USER);
600 print_continue("view.php?id=$cm->id");
604 /******************* regrade student assessments ************************************/
605 elseif ($action == 'regradestudentassessments' ) {
607 if (!has_capability('mod/exercise:assess', $context)) {
608 error("Only teachers can look at this page");
610 // get all the student assessments
611 if ($assessments = exercise_get_teacher_submission_assessments($exercise)) {
612 foreach ($assessments as $studentassessment) {
613 if ($studentassessment->timegraded > 0) {
614 if (!$submissions = get_records_select("exercise_submissions",
615 "userid = $studentassessment->userid AND exerciseid = $exercise->id", "timecreated ASC")) {
616 error("Regrade student assessments: student submission not found");
618 foreach ($submissions as $submission) { // only the first one is relavant
619 if (!$teacherassessments = get_records("exercise_assessments", "submissionid",
620 $submission->id, "timecreated ASC")) {
621 error("Regrade student assessments: teacher assessment(s) not found");
623 foreach ($teacherassessments as $teacherassessment) { // only the first one is relavent
624 $newgrade = exercise_compare_assessments($exercise, $studentassessment, $teacherassessment);
625 set_field("exercise_assessments", "gradinggrade", $newgrade, "id", $studentassessment->id);
626 break;
628 break;
633 redirect("submissions.php?id=$cm->id&amp;action=adminlist");
637 /****************** teacher assessment : grading of assessment and submission (from student) ************/
638 elseif ($action == 'teacherassessment') {
640 if (!has_capability('mod/exercise:assess', $context)) {
641 error("Only teachers can look at this page");
644 if (empty($aid)) {
645 error("assessment id missing");
647 if (empty($sid)) {
648 error ("no sid");
650 if (!$assessment = get_record("exercise_assessments", "id", $aid)) {
651 error("Teacher assessment: User's assessment record not found");
653 if (!$submission = get_record("exercise_submissions", "id", $sid)) {
654 error("Teacher assessment: User's submission record not found");
656 exercise_print_teacher_assessment_form($exercise, $assessment, $submission, $_SERVER["HTTP_REFERER"]);
660 /****************** teacher table : show assessments by exercise and teacher ************/
661 elseif ($action == 'teachertable') {
663 if (!has_capability('mod/exercise:assess', $context)) {
664 error("Only teachers can look at this page");
667 exercise_print_teacher_table($course);
668 print_continue("index.php?id=$course->id");
672 /****************** update assessment (by teacher or student) ***************************/
673 elseif ($action == 'updateassessment') {
675 $timenow = time();
676 $form = data_submitted();
678 if (empty($aid)) {
679 error("assessment id missing");
681 if (! $assessment = get_record("exercise_assessments", "id", $aid)) {
682 error("exercise assessment is misconfigured");
685 // first get the assignment elements for maxscores and weights...
686 if (!$elementsraw = get_records("exercise_elements", "exerciseid", $exercise->id, "elementno ASC")) {
687 print_string("noteonassignmentelements", "exercise");
689 else {
690 foreach ($elementsraw as $element) {
691 $elements[] = $element; // to renumber index 0,1,2...
695 // don't fiddle about, delete all the old and then add the new!
696 delete_records("exercise_grades", "assessmentid", $assessment->id);
698 //determine what kind of grading we have
699 switch ($exercise->gradingstrategy) {
700 case 0: // no grading
701 // Insert all the elements that contain something
702 foreach ($form->feedback as $key => $thefeedback) {
703 unset($element);
704 $element->exerciseid = $exercise->id;
705 $element->assessmentid = $assessment->id;
706 $element->elementno = clean_param($key, PARAM_INT);
707 $element->feedback = clean_param($thefeedback, PARAM_CLEAN);
708 if (!$element->id = insert_record("exercise_grades", $element)) {
709 error("Could not insert exercise element!");
712 $grade = 0; // set to satisfy save to db
713 break;
715 case 1: // accumulative grading
716 // Insert all the elements that contain something
717 foreach ($form->grade as $key => $thegrade) {
718 unset($element);
719 $element->exerciseid = $exercise->id;
720 $element->assessmentid = $assessment->id;
721 $element->elementno = clean_param($key, PARAM_INT);
722 $element->feedback = clean_param($form->feedback[$key], PARAM_CLEAN);
723 $element->grade = $thegrade;
724 if (!$element->id = insert_record("exercise_grades", $element)) {
725 error("Could not insert exercise element!");
728 // now work out the grade...
729 $rawgrade=0;
730 $totalweight=0;
731 foreach ($form->grade as $key => $grade) {
732 $maxscore = $elements[$key]->maxscore;
733 $weight = $EXERCISE_EWEIGHTS[$elements[$key]->weight];
734 if ($weight > 0) {
735 $totalweight += $weight;
737 $rawgrade += ($grade / $maxscore) * $weight;
738 // echo "\$key, \$maxscore, \$weight, \$totalweight, \$grade, \$rawgrade : $key, $maxscore, $weight, $totalweight, $grade, $rawgrade<br />";
740 $grade = 100.0 * ($rawgrade / $totalweight);
741 break;
743 case 2: // error banded graded
744 // Insert all the elements that contain something
745 $error = 0.0;
746 for ($i =0; $i < $exercise->nelements; $i++) {
747 unset($element);
748 $element->exerciseid = $exercise->id;
749 $element->assessmentid = $assessment->id;
750 $element->elementno = $i;
751 $element->feedback = clean_param($form->feedback[$i], PARAM_CLEAN);
752 $element->grade = $form->grade[$i];
753 if (!$element->id = insert_record("exercise_grades", $element)) {
754 error("Could not insert exercise element!");
756 if (empty($form->grade[$i])){
757 $error += $EXERCISE_EWEIGHTS[$elements[$i]->weight];
760 // now save the adjustment
761 unset($element);
762 $i = $exercise->nelements;
763 $element->exerciseid = $exercise->id;
764 $element->assessmentid = $assessment->id;
765 $element->elementno = $i;
766 $element->grade = $form->grade[$i];
767 if (!$element->id = insert_record("exercise_grades", $element)) {
768 error("Could not insert exercise element!");
770 $grade = ($elements[intval($error + 0.5)]->maxscore + $form->grade[$i])
771 * 100.0 / $exercise->grade;
772 // echo "<p><b>".get_string("weightederrorcount", "exercise", intval($error + 0.5)).
773 // " ".get_string("adjustment", "exercise").": ".$form->grade[$i]."</b>\n";
774 // check the grade for sanity!
775 if ($grade > 100.0) {
776 $grade = 100.0;
778 if ($grade < 0.0) {
779 $grade = 0.0;
781 break;
783 case 3: // criteria grading
784 // save in the selected criteria value in element zero,
785 unset($element);
786 $element->exerciseid = $exercise->id;
787 $element->assessmentid = $assessment->id;
788 $element->elementno = 0;
789 $element->grade = $form->grade[0];
790 if (!$element->id = insert_record("exercise_grades", $element)) {
791 error("Could not insert exercise element!");
793 // now save the adjustment in element one
794 unset($element);
795 $element->exerciseid = $exercise->id;
796 $element->assessmentid = $assessment->id;
797 $element->elementno = 1;
798 $element->grade = $form->grade[1];
799 if (!$element->id = insert_record("exercise_grades", $element)) {
800 error("Could not insert exercise element!");
802 $grade = ($elements[$form->grade[0]]->maxscore + $form->grade[1]) * 100 / $exercise->grade;
803 // check the grade for sanity!
804 if ($grade >100.0) {
805 $grade = 100.0;
807 if ($grade < 0.0) {
808 $grade = 0.0;
810 break;
812 case 4: // rubric grading (identical to accumulative grading)
813 // Insert all the elements that contain something
814 foreach ($form->grade as $key => $thegrade) {
815 unset($element);
816 $element->exerciseid = $exercise->id;
817 $element->assessmentid = $assessment->id;
818 $element->elementno = clean_param($key, PARAM_INT);
819 $element->feedback = clean_param($form->feedback[$key], PARAM_CLEAN);
820 $element->grade = $thegrade;
821 if (!$element->id = insert_record("exercise_grades", $element)) {
822 error("Could not insert exercise element!");
825 // now work out the grade...
826 $rawgrade=0;
827 $totalweight=0;
828 foreach ($form->grade as $key => $grade) {
829 $maxscore = $elements[$key]->maxscore;
830 $weight = $EXERCISE_EWEIGHTS[$elements[$key]->weight];
831 if ($weight > 0) {
832 $totalweight += $weight;
834 $rawgrade += ($grade / $maxscore) * $weight;
836 $grade = 100.0 * ($rawgrade / $totalweight);
837 break;
839 } // end of switch
841 // update the time of the assessment record (may be re-edited)...
842 set_field("exercise_assessments", "timecreated", $timenow, "id", $assessment->id);
843 set_field("exercise_assessments", "grade", $grade, "id", $assessment->id);
844 // ...and clear any grading of this assessment (these assessments are never graded but...)
845 set_field("exercise_assessments", "timegraded", 0, "id", $assessment->id);
846 set_field("exercise_assessments", "gradinggrade", 0, "id", $assessment->id);
848 // any comment?
849 if (!empty($form->generalcomment)) {
850 set_field("exercise_assessments", "generalcomment", clean_param($form->generalcomment, PARAM_CLEAN), "id", $assessment->id);
853 // is user allowed to resubmit?
854 if (has_capability('mod/exercise:assess', $context)) {
855 if (!$submission = get_record("exercise_submissions", "id", $assessment->submissionid)) {
856 error ("Updateassessment: submission record not found");
858 if ($form->resubmit == 1) {
859 set_field("exercise_submissions", "resubmit", 1, "id", $submission->id);
861 else {
862 // clear resubmit flag
863 set_field("exercise_submissions", "resubmit", 0, "id", $submission->id);
867 add_to_log($course->id, "exercise", "assess", "view.php?id=$cm->id", "$assessment->id");
869 // set up return address
870 if (!$returnto = $form->returnto) {
871 $returnto = "view.php?id=$cm->id";
874 // show grade if grading strategy is not zero
875 if ($exercise->gradingstrategy) {
876 redirect($returnto, "<p align=\"center\"><b>".get_string("thegradeis", "exercise").": ".
877 number_format($grade * $exercise->grade / 100.0, 1)." (".get_string("maximumgrade").
878 " ".number_format($exercise->grade).")</b></p>", 1);
880 else {
881 redirect($returnto);
886 /****************** update teacher assessment (by teacher only) ***************************/
887 elseif ($action == 'updateteacherassessment') {
889 if (!has_capability('mod/exercise:assess', $context)) {
890 error("Only teachers can look at this page");
893 $timenow = time();
894 $form = data_submitted();
896 // first do the (teacher's) assessment of the student's submission
897 if (! $submission = get_record("exercise_submissions", "id", $form->sid)) {
898 error("Update teacher assessment: student's submission record not found");
900 if (!$assessment = exercise_get_submission_assessment($submission, $USER)) {
901 error("Update teacher assessment: teacher's assessment record not found");
904 // first get the assignment elements for maxscores and weights...
905 if (!$elementsraw = get_records("exercise_elements", "exerciseid", $exercise->id, "elementno ASC")) {
906 print_string("noteonassignmentelements", "exercise");
908 else {
909 foreach ($elementsraw as $element) {
910 $elements[] = $element; // to renumber index 0,1,2...
914 // don't fiddle about, delete all the old and then add the new!
915 delete_records("exercise_grades", "assessmentid", $assessment->id);
917 //determine what kind of grading we have
918 switch ($exercise->gradingstrategy) {
919 case 0: // no grading
920 // Insert all the elements that contain something
921 foreach ($form->feedback as $key => $thefeedback) {
922 unset($element);
923 $element->exerciseid = $exercise->id;
924 $element->assessmentid = $assessment->id;
925 $element->elementno = clean_param($key, PARAM_INT);
926 $element->feedback = clean_param($thefeedback, PARAM_CLEAN);
927 if (!$element->id = insert_record("exercise_grades", $element)) {
928 error("Could not insert exercise element!");
931 $grade = 0; // set to satisfy save to db
932 break;
934 case 1: // accumulative grading
935 // Insert all the elements that contain something
936 foreach ($form->grade as $key => $thegrade) {
937 unset($element);
938 $element->exerciseid = $exercise->id;
939 $element->assessmentid = $assessment->id;
940 $element->elementno = clean_param($key, PARAM_INT);
941 $element->feedback = clean_param($form->feedback[$key], PARAM_CLEAN);
942 $element->grade = $thegrade;
943 if (!$element->id = insert_record("exercise_grades", $element)) {
944 error("Could not insert exercise element!");
947 // now work out the grade...
948 $rawgrade=0;
949 $totalweight=0;
950 foreach ($form->grade as $key => $grade) {
951 $maxscore = $elements[$key]->maxscore;
952 $weight = $EXERCISE_EWEIGHTS[$elements[$key]->weight];
953 if ($weight > 0) {
954 $totalweight += $weight;
956 $rawgrade += ($grade / $maxscore) * $weight;
957 // echo "\$key, \$maxscore, \$weight, \$totalweight, \$grade, \$rawgrade : $key, $maxscore, $weight, $totalweight, $grade, $rawgrade<br />";
959 $grade = 100.0 * ($rawgrade / $totalweight);
960 break;
962 case 2: // error banded graded
963 // Insert all the elements that contain something
964 $error = 0.0;
965 for ($i =0; $i < $exercise->nelements; $i++) {
966 unset($element);
967 $element->exerciseid = $exercise->id;
968 $element->assessmentid = $assessment->id;
969 $element->elementno = $i;
970 $element->feedback = clean_param($form->feedback[$i], PARAM_CLEAN);
971 $element->grade = $form->grade[$i];
972 if (!$element->id = insert_record("exercise_grades", $element)) {
973 error("Could not insert exercise element!");
975 if (empty($form->grade[$i])){
976 $error += $EXERCISE_EWEIGHTS[$elements[$i]->weight];
979 // now save the adjustment
980 unset($element);
981 $i = $exercise->nelements;
982 $element->exerciseid = $exercise->id;
983 $element->assessmentid = $assessment->id;
984 $element->elementno = $i;
985 $element->grade = $form->grade[$i];
986 if (!$element->id = insert_record("exercise_grades", $element)) {
987 error("Could not insert exercise element!");
989 $grade = ($elements[intval($error + 0.5)]->maxscore + $form->grade[$i]) * 100 / $exercise->grade;
990 echo "<p><b>".get_string("weightederrorcount", "exercise", intval($error + 0.5))."</b></p>\n";
991 break;
993 case 3: // criteria grading
994 // save in the selected criteria value in element zero,
995 unset($element);
996 $element->exerciseid = $exercise->id;
997 $element->assessmentid = $assessment->id;
998 $element->elementno = 0;
999 $element->grade = $form->grade[0];
1000 if (!$element->id = insert_record("exercise_grades", $element)) {
1001 error("Could not insert exercise element!");
1003 // now save the adjustment in element one
1004 unset($element);
1005 $element->exerciseid = $exercise->id;
1006 $element->assessmentid = $assessment->id;
1007 $element->elementno = 1;
1008 $element->grade = $form->grade[1];
1009 if (!$element->id = insert_record("exercise_grades", $element)) {
1010 error("Could not insert exercise element!");
1012 $grade = ($elements[$form->grade[0]]->maxscore + $form->grade[1]) * 100 / $exercise->grade;
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->exerciseid = $exercise->id;
1020 $element->assessmentid = $assessment->id;
1021 $element->elementno = clean_param($key, PARAM_INT);
1022 $element->feedback = clean_param($form->feedback[$key], PARAM_CLEAN);
1023 $element->grade = $thegrade;
1024 if (!$element->id = insert_record("exercise_grades", $element)) {
1025 error("Could not insert exercise element!");
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 = $EXERCISE_EWEIGHTS[$elements[$key]->weight];
1034 if ($weight > 0) {
1035 $totalweight += $weight;
1037 $rawgrade += ($grade / $maxscore) * $weight;
1039 $grade = 100.0 * ($rawgrade / $totalweight);
1040 break;
1042 } // end of switch
1044 // update the time of the assessment record (may be re-edited)...
1045 set_field("exercise_assessments", "timecreated", $timenow, "id", $assessment->id);
1046 set_field("exercise_assessments", "grade", $grade, "id", $assessment->id);
1047 // ...and clear any grading of this assessment (never needed but...)
1048 set_field("exercise_assessments", "timegraded", 0, "id", $assessment->id);
1049 set_field("exercise_assessments", "gradinggrade", 0, "id", $assessment->id);
1051 // any comment?
1052 if (!empty($form->generalcomment)) {
1053 set_field("exercise_assessments", "generalcomment", clean_param($form->generalcomment, PARAM_CLEAN), "id", $assessment->id);
1056 // now calculate the (grading) grade of the student's assessment...
1057 if (!$stassessment = get_record("exercise_assessments", "id", $form->said)) {
1058 error("Update teacher assessment: student's assessment record not found");
1060 $gradinggrade = exercise_compare_assessments($exercise, $assessment, $stassessment);
1061 // ...and save the grade for the assessment
1062 set_field("exercise_assessments", "gradinggrade", $gradinggrade, "id", $stassessment->id);
1063 set_field("exercise_assessments", "timegraded", $timenow, "id", $stassessment->id);
1064 set_field("exercise_assessments", "mailed", 0, "id", $stassessment->id);
1065 echo "<centre><b>".get_string("savedok", "exercise")."</b></centre><br />\n";
1067 add_to_log($course->id, "exercise", "grade", "view.php?id=$cm->id", "$stassessment->id");
1069 // is user allowed to resubmit?
1070 if ($form->resubmit == 1) {
1071 set_field("exercise_submissions", "resubmit", 1, "id", $submission->id);
1073 else {
1074 // clear resubmit flag
1075 set_field("exercise_submissions", "resubmit", 0, "id", $submission->id);
1078 add_to_log($course->id, "exercise", "assess", "view.php?id=$cm->id", "$assessment->id");
1080 // set up return address
1081 if (!$returnto = $form->returnto) {
1082 $returnto = "view.php?id=$cm->id";
1085 // show grade if grading strategy is not zero
1086 if ($exercise->gradingstrategy) {
1087 redirect($returnto, "<p align=\"center\"><b>".get_string("gradeforstudentsassessment",
1088 "exercise", $course->student).": ".number_format($gradinggrade * $exercise->gradinggrade / 100.0, 1).
1089 " (".get_string("maximumgrade")." ".number_format($exercise->gradinggrade, 1).")</b></p><p><b>".
1090 get_string("thegradeis", "exercise").": ".number_format($grade * $exercise->grade / 100.0, 1).
1091 " (".get_string("maximumgrade")." ".number_format($exercise->grade, 1).")</b></p>");
1093 else {
1094 redirect($returnto);
1099 /****************** update grading grade(by teacher) ***************************/
1100 elseif ($action == 'updategradinggrade') {
1102 if (!has_capability('mod/exercise:assess', $context)) {
1103 error("Only teachers can look at this page");
1106 if (empty($aid)) {
1107 error("submission id missing");
1109 // normalise gradinggrade
1110 $gradinggrade = $_POST['gradinggrade'] * 100 / $exercise->gradinggrade;
1111 if (!set_field("exercise_assessments", "gradinggrade", $gradinggrade, "id",
1112 $aid)) {
1113 error("Update grading grade: asseesment not updated");
1115 redirect("submissions.php?id=$cm->id&amp;action=adminlist", get_string("savedok", "exercise"), 1);
1119 /****************** user confirm delete ************************************/
1120 elseif ($action == 'userconfirmdelete' ) {
1122 if (empty($aid)) {
1123 error("User confirm delete: assessment id missing");
1126 notice_yesno(get_string("confirmdeletionofthisitem","exercise", get_string("assessment", "exercise")),
1127 "assessments.php?action=userdelete&amp;id=$cm->id&amp;aid=$aid", "view.php?id=$cm->id");
1131 /****************** user delete ************************************/
1132 elseif ($action == 'userdelete' ) {
1134 if (empty($aid)) {
1135 error("User delete: assessment id missing");
1138 print_string("deleting", "exercise");
1139 // first delete all the associated records...
1140 delete_records("exercise_grades", "assessmentid", $aid);
1141 // ...now delete the assessment...
1142 delete_records("exercise_assessments", "id", $aid);
1144 print_continue("view.php?id=$cm->id");
1148 /****************** view assessment ***********************/
1149 elseif ($action == 'viewassessment') {
1151 if (empty($aid)) {
1152 error("assessment id missing");
1155 // get the assessment record
1156 if (!$assessment = get_record("exercise_assessments", "id", $aid)) {
1157 error("Assessment record not found");
1160 // show assessment but don't allow changes
1161 exercise_print_assessment_form($exercise, $assessment);
1163 print_continue("view.php?id=$cm->id");
1167 /*************** no man's land **************************************/
1168 else {
1169 error("Fatal Error: Unknown Action: ".$action."\n");
1172 print_footer($course);