From ebc788d13e1ffbf9f37384cc90df9c72f95b8bb0 Mon Sep 17 00:00:00 2001 From: Rossiani Wijaya Date: Fri, 23 Sep 2011 14:17:51 +0800 Subject: [PATCH] MDL-29230 lesson_module: fixed question's answer to have 0 value. --- mod/lesson/locallib.php | 6 ++-- mod/lesson/pagetypes/matching.php | 5 +-- mod/lesson/pagetypes/truefalse.php | 57 ++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index f26f1be3b0f..251f7314b77 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -2154,7 +2154,8 @@ abstract class lesson_page extends lesson_base { $this->answers[$i]->responseformat = $properties->response_editor[$i]['format']; } - if (!empty($this->answers[$i]->answer)) { + // we don't need to check for isset here because properties called it's own isset method. + if ($this->answers[$i]->answer != '') { if (isset($properties->jumpto[$i])) { $this->answers[$i]->jumpto = $properties->jumpto[$i]; } @@ -2246,8 +2247,7 @@ abstract class lesson_page extends lesson_base { $answer->response = $properties->response_editor[$i]['text']; $answer->responseformat = $properties->response_editor[$i]['format']; } - - if (!empty($answer->answer)) { + if (isset($answer->answer) && $answer->answer != '') { if (isset($properties->jumpto[$i])) { $answer->jumpto = $properties->jumpto[$i]; } diff --git a/mod/lesson/pagetypes/matching.php b/mod/lesson/pagetypes/matching.php index e9ffa09428c..3bcdf978881 100644 --- a/mod/lesson/pagetypes/matching.php +++ b/mod/lesson/pagetypes/matching.php @@ -120,7 +120,7 @@ class lesson_page_type_matching extends lesson_page { $answer->responseformat = $properties->response_editor[$i]['format']; } - if (!empty($answer->answer)) { + if (isset($answer->answer) && $answer->answer != '') { if (isset($properties->jumpto[$i])) { $answer->jumpto = $properties->jumpto[$i]; } @@ -320,7 +320,8 @@ class lesson_page_type_matching extends lesson_page { $this->answers[$i]->responseformat = $properties->response_editor[$i]['format']; } - if (!empty($this->answers[$i]->answer)) { + // we don't need to check for isset here because properties called it's own isset method. + if ($this->answers[$i]->answer != '') { if (isset($properties->jumpto[$i])) { $this->answers[$i]->jumpto = $properties->jumpto[$i]; } diff --git a/mod/lesson/pagetypes/truefalse.php b/mod/lesson/pagetypes/truefalse.php index 6b8977bfb1d..05a32de07c9 100644 --- a/mod/lesson/pagetypes/truefalse.php +++ b/mod/lesson/pagetypes/truefalse.php @@ -144,6 +144,63 @@ class lesson_page_type_truefalse extends lesson_page { } return $table; } + + /** + * Updates the page and its answers + * + * @global moodle_database $DB + * @global moodle_page $PAGE + * @param stdClass $properties + * @return bool + */ + public function update($properties) { + global $DB, $PAGE; + $answers = $this->get_answers(); + $properties->id = $this->properties->id; + $properties->lessonid = $this->lesson->id; + $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), get_context_instance(CONTEXT_MODULE, $PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id); + $DB->update_record("lesson_pages", $properties); + + // need to reset offset for correct and wrong responses + $this->lesson->maxanswers = 2; + for ($i = 0; $i < $this->lesson->maxanswers; $i++) { + if (!array_key_exists($i, $this->answers)) { + $this->answers[$i] = new stdClass; + $this->answers[$i]->lessonid = $this->lesson->id; + $this->answers[$i]->pageid = $this->id; + $this->answers[$i]->timecreated = $this->timecreated; + } + + if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) { + $this->answers[$i]->answer = $properties->answer_editor[$i]['text']; + $this->answers[$i]->answerformat = $properties->answer_editor[$i]['format']; + } + if (!empty($properties->response_editor[$i]) && is_array($properties->response_editor[$i])) { + $this->answers[$i]->response = $properties->response_editor[$i]['text']; + $this->answers[$i]->responseformat = $properties->response_editor[$i]['format']; + } + + // we don't need to check for isset here because properties called it's own isset method. + if ($this->answers[$i]->answer != '') { + if (isset($properties->jumpto[$i])) { + $this->answers[$i]->jumpto = $properties->jumpto[$i]; + } + if ($this->lesson->custom && isset($properties->score[$i])) { + $this->answers[$i]->score = $properties->score[$i]; + } + if (!isset($this->answers[$i]->id)) { + $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]); + } else { + $DB->update_record("lesson_answers", $this->answers[$i]->properties()); + } + } else if (isset($this->answers[$i]->id)) { + $DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id)); + unset($this->answers[$i]); + } + } + return true; + } + public function stats(array &$pagestats, $tries) { if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt $temp = $tries[$this->lesson->maxattempts - 1];