From b38f0baad40676f684a4f7469db1db0904474bd1 Mon Sep 17 00:00:00 2001 From: Aparup Banerjee Date: Wed, 4 May 2011 17:17:47 +0800 Subject: [PATCH 1/2] MDL-27386 lesson fix mform->score not saving (wip) --- mod/lesson/essay.php | 51 ++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/mod/lesson/essay.php b/mod/lesson/essay.php index 981a833c987..1067b1ef0af 100644 --- a/mod/lesson/essay.php +++ b/mod/lesson/essay.php @@ -46,6 +46,21 @@ if ($mode !== 'display') { } $PAGE->set_url($url); +$attemptid = optional_param('attemptid', PARAM_INT); +$attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid)); +$answer = $DB->get_record('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $attempt->pageid)); +$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': @@ -57,30 +72,26 @@ switch ($mode) { if (!$attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid))) { print_error('cannotfindattempt', 'lesson'); } - $page = $lesson->load_page($attempt->pageid); if (!$user = $DB->get_record('user', array('id' => $attempt->userid))) { print_error('cannotfinduser', 'lesson'); } - if (!$answer = $DB->get_record('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page->id))) { + if (!$answer = $DB->record_exists('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $attempt->pageid))) { print_error('cannotfindanswer', 'lesson'); } break; case 'update': require_sesskey(); - $mform = new essay_grading_form(); + $attemptid = required_param('attemptid', PARAM_INT); + $attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid), '*', MUST_EXIST); + $user = $DB->get_record('user', array('id' => $attempt->userid), '*', MUST_EXIST); + $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()) { + error_log(print_r($form,true)); - 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 (!$attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid))) { - print_error('cannotfindattempt', 'lesson'); - } if (!$grades = $DB->get_records('lesson_grades', array("lessonid"=>$lesson->id, "userid"=>$attempt->userid), 'completed', '*', $attempt->retry, 1)) { print_error('cannotfindgrade', 'lesson'); } @@ -371,18 +382,8 @@ switch ($mode) { $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; From 4bb7783ef183625a9b2067a5e1a77e2f71c899d3 Mon Sep 17 00:00:00 2001 From: Rossiani Wijaya Date: Fri, 13 May 2011 14:14:19 +0800 Subject: [PATCH 2/2] MDL-27386: fixed mform for lesson essay grading (head) --- mod/lesson/essay.php | 58 +++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/mod/lesson/essay.php b/mod/lesson/essay.php index 1067b1ef0af..a0a611c466a 100644 --- a/mod/lesson/essay.php +++ b/mod/lesson/essay.php @@ -46,52 +46,59 @@ if ($mode !== 'display') { } $PAGE->set_url($url); -$attemptid = optional_param('attemptid', PARAM_INT); -$attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid)); -$answer = $DB->get_record('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $attempt->pageid)); -$scoreoptions = array(); -if ($lesson->custom) { - $i = $answer->score; - while ($i >= 0) { - $scoreoptions[$i] = (string)$i; - $i--; +$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'); } -} 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'); } - if (!$user = $DB->get_record('user', array('id' => $attempt->userid))) { + if (empty($user)) { print_error('cannotfinduser', 'lesson'); } - if (!$answer = $DB->record_exists('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $attempt->pageid))) { + if (empty($answer)) { print_error('cannotfindanswer', 'lesson'); } break; case 'update': require_sesskey(); - $attemptid = required_param('attemptid', PARAM_INT); - $attempt = $DB->get_record('lesson_attempts', array('id' => $attemptid), '*', MUST_EXIST); - $user = $DB->get_record('user', array('id' => $attempt->userid), '*', MUST_EXIST); + + if (empty($attempt)) { + print_error('cannotfindattempt', 'lesson'); + } + if (empty($user)) { + print_error('cannotfinduser', '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()) { - error_log(print_r($form,true)); - if (!$grades = $DB->get_records('lesson_grades', array("lessonid"=>$lesson->id, "userid"=>$attempt->userid), 'completed', '*', $attempt->retry, 1)) { print_error('cannotfindgrade', 'lesson'); } @@ -100,7 +107,7 @@ switch ($mode) { $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) { @@ -379,12 +386,9 @@ switch ($mode) { case 'grade': // Grading form // Expects the following to be set: $attemptid, $answer, $user, $page, $attempt - - $essayinfo = unserialize($attempt->useranswer); $mform = new essay_grading_form(null, array('scoreoptions'=>$scoreoptions, 'user'=>$user)); - $data = new stdClass; $data->id = $cm->id; $data->attemptid = $attemptid;