Skip to content
Browse files

mod-lesson MDL-25632 Fixed bug whereby excess answers were being crea…

…ted when creating a page and then when updating it
  • Loading branch information...
1 parent 1c510e2 commit 981debb706bff124be17c10f782a8fd7f44c06e6 Sam Hemelryk committed Dec 14, 2010
Showing with 54 additions and 25 deletions.
  1. +21 −12 mod/lesson/locallib.php
  2. +33 −13 mod/lesson/pagetypes/matching.php
View
33 mod/lesson/locallib.php
@@ -2135,13 +2135,17 @@ public function update($properties, $context = null, $maxbytes = null) {
$this->answers[$i]->pageid = $this->id;
$this->answers[$i]->timecreated = $this->timecreated;
}
- if (!empty($properties->answer_editor[$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($properties->response_editor[$i])) {
- $this->answers[$i]->response = $properties->response_editor[$i]['text'];
- $this->answers[$i]->responseformat = $properties->response_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'];
+ }
+
+ if (!empty($this->answers[$i]->answer)) {
if (isset($properties->jumpto[$i])) {
$this->answers[$i]->jumpto = $properties->jumpto[$i];
}
@@ -2154,8 +2158,9 @@ public function update($properties, $context = null, $maxbytes = null) {
$DB->update_record("lesson_answers", $this->answers[$i]->properties());
}
- } else {
- break;
+ } else if (isset($this->answers[$i]->id)) {
+ $DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
+ unset($this->answers[$i]);
}
}
return true;
@@ -2223,13 +2228,17 @@ public function create_answers($properties) {
for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
$answer = clone($newanswer);
- if (!empty($properties->answer_editor[$i])) {
+
+ if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
$answer->answer = $properties->answer_editor[$i]['text'];
$answer->answerformat = $properties->answer_editor[$i]['format'];
- if (isset($properties->response_editor[$i])) {
- $answer->response = $properties->response_editor[$i]['text'];
- $answer->responseformat = $properties->response_editor[$i]['format'];
- }
+ }
+ if (!empty($properties->response_editor[$i]) && is_array($properties->response_editor[$i])) {
+ $answer->response = $properties->response_editor[$i]['text'];
+ $answer->responseformat = $properties->response_editor[$i]['format'];
+ }
+
+ if (!empty($answer->answer)) {
if (isset($properties->jumpto[$i])) {
$answer->jumpto = $properties->jumpto[$i];
}
View
46 mod/lesson/pagetypes/matching.php
@@ -105,13 +105,16 @@ public function create_answers($properties) {
$this->lesson->maxanswers = $this->lesson->maxanswers + 2;
for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
$answer = clone($newanswer);
- if (!empty($properties->answer_editor[$i])) {
+ if (!empty($properties->answer_editor[$i]) && is_array($properties->answer_editor[$i])) {
$answer->answer = $properties->answer_editor[$i]['text'];
$answer->answerformat = $properties->answer_editor[$i]['format'];
- if (isset($properties->response_editor[$i])) {
- $answer->response = $properties->response_editor[$i]['text'];
- $answer->responseformat = $properties->response_editor[$i]['format'];
- }
+ }
+ if (!empty($properties->response_editor[$i]) && is_array($properties->response_editor[$i])) {
+ $answer->response = $properties->response_editor[$i]['text'];
+ $answer->responseformat = $properties->response_editor[$i]['format'];
+ }
+
+ if (!empty($answer->answer)) {
if (isset($properties->jumpto[$i])) {
$answer->jumpto = $properties->jumpto[$i];
}
@@ -276,6 +279,14 @@ public function display_answers(html_table $table) {
}
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();
@@ -293,13 +304,17 @@ public function update($properties) {
$this->answers[$i]->pageid = $this->id;
$this->answers[$i]->timecreated = $this->timecreated;
}
- if (!empty($properties->answer_editor[$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($properties->response_editor[$i])) {
- $this->answers[$i]->response = $properties->response_editor[$i]['text'];
- $this->answers[$i]->responseformat = $properties->response_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'];
+ }
+
+ if (!empty($this->answers[$i]->answer)) {
if (isset($properties->jumpto[$i])) {
$this->answers[$i]->jumpto = $properties->jumpto[$i];
}
@@ -319,8 +334,9 @@ public function update($properties) {
$DB->update_record("lesson_answers", $this->answers[$i]->properties());
}
- } else {
- break;
+ } else if (isset($this->answers[$i]->id)) {
+ $DB->delete_records('lesson_answers', array('id'=>$this->answers[$i]->id));
+ unset($this->answers[$i]);
}
}
return true;
@@ -383,7 +399,11 @@ public function report_answers($answerpage, $answerdata, $useranswer, $pagestats
$data = "<select disabled=\"disabled\"><option selected=\"selected\">".strip_tags(format_string($answer->answer))."</option></select>";
if ($useranswer != NULL) {
$userresponse = explode(",", $useranswer->useranswer);
- $data .= "<select disabled=\"disabled\"><option selected=\"selected\">".strip_tags(format_string($answers[$userresponse[$i]]->response))."</option></select>";
+ $data .= "<select disabled=\"disabled\"><option selected=\"selected\">";
+ if (array_key_exists($i, $userresponse)) {
+ $data .= strip_tags(format_string($answers[$userresponse[$i]]->response));
+ }
+ $data .= "</option></select>";
} else {
$data .= "<select disabled=\"disabled\"><option selected=\"selected\">".strip_tags(format_string($answer->response))."</option></select>";
}

0 comments on commit 981debb

Please sign in to comment.
Something went wrong with that request. Please try again.