From 5d911423dc939b126457a5a35404dc97b571345f Mon Sep 17 00:00:00 2001 From: Jean-Michel Vedrine Date: Sat, 20 Dec 2014 13:35:59 +0100 Subject: [PATCH] MDL-48897 mod_lesson: Error displayed while editing content page jumps This change also fix MDL-32343 and MDL-31778 related to problems when editing cluster pages. This is a backport of MDL-48675. --- mod/lesson/locallib.php | 92 ++++++++++++++++++--------- mod/lesson/pagetypes/endofbranch.php | 37 ----------- mod/lesson/pagetypes/endofcluster.php | 37 ----------- 3 files changed, 62 insertions(+), 104 deletions(-) diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index 11012058b77ac..8d9f5f4c831c9 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -2266,45 +2266,77 @@ public function update($properties, $context = null, $maxbytes = null) { $properties->timemodified = time(); $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$maxbytes), $context, 'mod_lesson', 'page_contents', $properties->id); $DB->update_record("lesson_pages", $properties); - - 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 ($this->type == self::TYPE_STRUCTURE && $this->get_typeid() != LESSON_PAGE_BRANCHTABLE) { + if (count($answers) > 1) { + $answer = array_shift($answers); + foreach ($answers as $a) { + $DB->delete_record('lesson_answers', array('id' => $a->id)); + } + } else if (count($answers) == 1) { + $answer = array_shift($answers); + } else { + $answer = new stdClass; + $answer->lessonid = $properties->lessonid; + $answer->pageid = $properties->id; + $answer->timecreated = time(); } - 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']; + $answer->timemodified = time(); + if (isset($properties->jumpto[0])) { + $answer->jumpto = $properties->jumpto[0]; } - 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']; + if (isset($properties->score[0])) { + $answer->score = $properties->score[0]; } - - // 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 (!empty($answer->id)) { + $DB->update_record("lesson_answers", $answer->properties()); + } else { + $DB->insert_record("lesson_answers", $answer); + } + } else { + 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 ($this->lesson->custom && isset($properties->score[$i])) { - $this->answers[$i]->score = $properties->score[$i]; + + 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 (!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()); + 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']; } - // Save files in answers and responses. - $this->save_answers_files($context, $maxbytes, $this->answers[$i], - $properties->answer_editor[$i], $properties->response_editor[$i]); + if (isset($this->answers[$i]->answer) && $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]); + // Save files in answers and responses. + if (isset($properties->response_editor[$i])) { + $this->save_answers_files($context, $maxbytes, $this->answers[$i], + $properties->answer_editor[$i], $properties->response_editor[$i]); + } else { + $this->save_answers_files($context, $maxbytes, $this->answers[$i], + $properties->answer_editor[$i]); + } + + } else if (isset($this->answers[$i]->id)) { + $DB->delete_records('lesson_answers', array('id' => $this->answers[$i]->id)); + unset($this->answers[$i]); + } } } return true; diff --git a/mod/lesson/pagetypes/endofbranch.php b/mod/lesson/pagetypes/endofbranch.php index fdbfc3e095ec4..c715677f7d69e 100644 --- a/mod/lesson/pagetypes/endofbranch.php +++ b/mod/lesson/pagetypes/endofbranch.php @@ -98,44 +98,7 @@ public function redirect_to_first_answer($canmanage) { public function get_grayout() { return 1; } - public function update($properties, $context = null, $maxbytes = null) { - global $DB, $PAGE; - $properties->id = $this->properties->id; - $properties->lessonid = $this->lesson->id; - if (empty($properties->qoption)) { - $properties->qoption = '0'; - } - $properties->timemodified = time(); - $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), context_module::instance($PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id); - $DB->update_record("lesson_pages", $properties); - - $answers = $this->get_answers(); - if (count($answers)>1) { - $answer = array_shift($answers); - foreach ($answers as $a) { - $DB->delete_record('lesson_answers', array('id'=>$a->id)); - } - } else if (count($answers)==1) { - $answer = array_shift($answers); - } else { - $answer = new stdClass; - } - - $answer->timemodified = time(); - if (isset($properties->jumpto[0])) { - $answer->jumpto = $properties->jumpto[0]; - } - if (isset($properties->score[0])) { - $answer->score = $properties->score[0]; - } - if (!empty($answer->id)) { - $DB->update_record("lesson_answers", $answer->properties()); - } else { - $DB->insert_record("lesson_answers", $answer); - } - return true; - } public function add_page_link($previd) { global $PAGE, $CFG; if ($previd != 0) { diff --git a/mod/lesson/pagetypes/endofcluster.php b/mod/lesson/pagetypes/endofcluster.php index fb8914d7a1b7a..f554353705300 100644 --- a/mod/lesson/pagetypes/endofcluster.php +++ b/mod/lesson/pagetypes/endofcluster.php @@ -67,44 +67,7 @@ public function redirect_to_next_page() { public function get_grayout() { return 1; } - public function update($properties, $context = null, $maxbytes = null) { - global $DB, $PAGE; - $properties->id = $this->properties->id; - $properties->lessonid = $this->lesson->id; - if (empty($properties->qoption)) { - $properties->qoption = '0'; - } - $properties->timemodified = time(); - $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), context_module::instance($PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id); - $DB->update_record("lesson_pages", $properties); - - $answers = $this->get_answers(); - if (count($answers)>1) { - $answer = array_shift($answers); - foreach ($answers as $a) { - $DB->delete_record('lesson_answers', array('id'=>$a->id)); - } - } else if (count($answers)==1) { - $answer = array_shift($answers); - } else { - $answer = new stdClass; - } - - $answer->timemodified = time(); - if (isset($properties->jumpto[0])) { - $answer->jumpto = $properties->jumpto[0]; - } - if (isset($properties->score[0])) { - $answer->score = $properties->score[0]; - } - if (!empty($answer->id)) { - $DB->update_record("lesson_answers", $answer->properties()); - } else { - $DB->insert_record("lesson_answers", $answer); - } - return true; - } public function override_next_page() { global $DB; $jump = $DB->get_field("lesson_answers", "jumpto", array("pageid" => $this->properties->id, "lessonid" => $this->lesson->id));