diff --git a/mod/lesson/essay.php b/mod/lesson/essay.php index 981a833c987d4..0e65bbe79b229 100644 --- a/mod/lesson/essay.php +++ b/mod/lesson/essay.php @@ -46,41 +46,59 @@ } $PAGE->set_url($url); +$attempt = new stdClass(); +$user = new stdClass(); +$attemptid = optional_param('attemptid', 0, PARAM_INT); + +if ($attemptid > 0) { + $attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid)); + $answer = $DB->get_record('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $attempt->pageid)); + $user = $DB->get_record('user', array('id' => $attempt->userid)); + $scoreoptions = array(); + if ($lesson->custom) { + $i = $answer->score; + while ($i >= 0) { + $scoreoptions[$i] = (string)$i; + $i--; + } + } else { + $scoreoptions[0] = get_string('nocredit', 'lesson'); + $scoreoptions[1] = get_string('credit', 'lesson'); + } +} + /// Handle any preprocessing before header is printed - based on $mode switch ($mode) { case 'grade': // Grading form - get the necessary data require_sesskey(); - $attemptid = required_param('attemptid', PARAM_INT); - - if (!$attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid))) { + if (empty($attempt)) { print_error('cannotfindattempt', 'lesson'); } - $page = $lesson->load_page($attempt->pageid); - if (!$user = $DB->get_record('user', array('id' => $attempt->userid))) { + if (empty($user)) { print_error('cannotfinduser', 'lesson'); } - if (!$answer = $DB->get_record('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page->id))) { + if (empty($answer)) { print_error('cannotfindanswer', 'lesson'); } break; case 'update': require_sesskey(); - $mform = new essay_grading_form(); - if ($form = $mform->get_data()) { - if (optional_param('cancel', false, PARAM_RAW)) { - redirect("$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id"); - } - - $attemptid = required_param('attemptid', PARAM_INT); - $score = optional_param('score', 0, PARAM_INT); + if (empty($attempt)) { + print_error('cannotfindattempt', 'lesson'); + } + if (empty($user)) { + print_error('cannotfinduser', 'lesson'); + } - if (!$attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid))) { - print_error('cannotfindattempt', 'lesson'); - } + $mform = new essay_grading_form(null, array('scoreoptions'=>$scoreoptions, 'user'=>$user)); + if ($mform->is_cancelled()) { + redirect("$CFG->wwwroot/mod/lesson/essay.php?id=$cm->id"); + } + if ($form = $mform->get_data()) { if (!$grades = $DB->get_records('lesson_grades', array("lessonid"=>$lesson->id, "userid"=>$attempt->userid), 'completed', '*', $attempt->retry, 1)) { print_error('cannotfindgrade', 'lesson'); } @@ -89,7 +107,7 @@ $essayinfo = unserialize($attempt->useranswer); $essayinfo->graded = 1; - $essayinfo->score = $score; + $essayinfo->score = $form->score; $essayinfo->response = clean_param($form->response, PARAM_RAW); $essayinfo->sent = 0; if (!$lesson->custom && $essayinfo->score == 1) { @@ -368,22 +386,9 @@ case 'grade': // Grading form // Expects the following to be set: $attemptid, $answer, $user, $page, $attempt - - $essayinfo = unserialize($attempt->useranswer); - $options = array(); - if ($lesson->custom) { - $i = $answer->score; - while ($i >= 0) { - $options[$i] = (string)$i; - $i--; - } - } else { - $options[0] = get_string('nocredit', 'lesson'); - $options[1] = get_string('credit', 'lesson'); - } - $mform = new essay_grading_form(null, array('scoreoptions'=>$options, 'user'=>$user)); + $mform = new essay_grading_form(null, array('scoreoptions'=>$scoreoptions, 'user'=>$user)); $data = new stdClass; $data->id = $cm->id; $data->attemptid = $attemptid;