MDL-10234
[moodle-linuxchix.git] / mod / lesson / action / updatepage.php
blobd54e179f62162d55b50018f6837ec27b6777dbe7
1 <?php // $Id$
2 /**
3 * Action for processing the form in editpage action and saves the page
5 * @version $Id$
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package lesson
8 **/
9 confirm_sesskey();
11 $redirect = optional_param('redirect', '', PARAM_ALPHA);
13 $timenow = time();
14 $form = data_submitted();
16 $page = new stdClass;
17 $page->id = clean_param($form->pageid, PARAM_INT);
19 // check to see if the cancel button was pushed
20 if (optional_param('cancel', '', PARAM_ALPHA)) {
21 if ($redirect == 'navigation') {
22 // redirect to viewing the page
23 redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&amp;pageid=$page->id");
24 } else {
25 redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
29 $page->timemodified = $timenow;
30 $page->qtype = clean_param($form->qtype, PARAM_INT);
31 if (isset($form->qoption)) {
32 $page->qoption = clean_param($form->qoption, PARAM_INT);
33 } else {
34 $page->qoption = 0;
36 if (isset($form->layout)) {
37 $page->layout = clean_param($form->layout, PARAM_INT);
38 } else {
39 $page->layout = 0;
41 if (isset($form->display)) {
42 $page->display = clean_param($form->display, PARAM_INT);
43 } else {
44 $page->display = 0;
46 $page->title = clean_param($form->title, PARAM_CLEANHTML);
47 $page->contents = trim($form->contents);
48 $page->title = addslashes($page->title);
50 if (!update_record("lesson_pages", $page)) {
51 error("Update page: page not updated");
53 if ($page->qtype == LESSON_ENDOFBRANCH || $page->qtype == LESSON_ESSAY || $page->qtype == LESSON_CLUSTER || $page->qtype == LESSON_ENDOFCLUSTER) {
54 // there's just a single answer with a jump
55 $oldanswer = new stdClass;
56 $oldanswer->id = $form->answerid[0];
57 $oldanswer->timemodified = $timenow;
58 $oldanswer->jumpto = clean_param($form->jumpto[0], PARAM_INT);
59 if (isset($form->score[0])) {
60 $oldanswer->score = clean_param($form->score[0], PARAM_INT);
62 // delete other answers this if mainly for essay questions. If one switches from using a qtype like Multichoice,
63 // then switches to essay, the old answers need to be removed because essay is
64 // supposed to only have one answer record
65 if ($answers = get_records_select("lesson_answers", "pageid = ".$page->id)) {
66 foreach ($answers as $answer) {
67 if ($answer->id != clean_param($form->answerid[0], PARAM_INT)) {
68 if (!delete_records("lesson_answers", "id", $answer->id)) {
69 error("Update page: unable to delete answer record");
74 if (!update_record("lesson_answers", $oldanswer)) {
75 error("Update page: EOB not updated");
77 } else {
78 // it's an "ordinary" page
79 if ($page->qtype == LESSON_MATCHING) {
80 // need to add two to offset correct response and wrong response
81 $lesson->maxanswers = $lesson->maxanswers + 2;
83 for ($i = 0; $i < $lesson->maxanswers; $i++) {
84 // strip tags because the editor gives <p><br />...
85 // also save any answers where the editor is (going to be) used
86 if ((!empty($form->answer[$i]) and trim(strip_tags($form->answer[$i]))) or isset($form->answereditor[$i]) or isset($form->responseeditor[$i])) {
87 if ($form->answerid[$i]) {
88 $oldanswer = new stdClass;
89 $oldanswer->id = clean_param($form->answerid[$i], PARAM_INT);
90 if (!isset($form->answereditor[$i])) {
91 $form->answereditor[$i] = 0;
93 if (!isset($form->responseeditor[$i])) {
94 $form->responseeditor[$i] = 0;
96 $oldanswer->flags = $form->answereditor[$i] * LESSON_ANSWER_EDITOR +
97 $form->responseeditor[$i] * LESSON_RESPONSE_EDITOR;
98 $oldanswer->timemodified = $timenow;
99 $oldanswer->answer = trim($form->answer[$i]);
100 if (isset($form->response[$i])) {
101 $oldanswer->response = trim($form->response[$i]);
102 } else {
103 $oldanswer->response = '';
105 $oldanswer->jumpto = clean_param($form->jumpto[$i], PARAM_INT);
106 if (isset($form->score[$i])) {
107 $oldanswer->score = clean_param($form->score[$i], PARAM_INT);
109 if (!update_record("lesson_answers", $oldanswer)) {
110 error("Update page: answer $i not updated");
112 } else {
113 // it's a new answer
114 $newanswer = new stdClass; // need to clear id if more than one new answer is ben added
115 $newanswer->lessonid = $lesson->id;
116 $newanswer->pageid = $page->id;
117 if (!isset($form->answereditor[$i])) {
118 $form->answereditor[$i] = 0;
120 if (!isset($form->responseeditor[$i])) {
121 $form->responseeditor[$i] = 0;
123 $newanswer->flags = $form->answereditor[$i] * LESSON_ANSWER_EDITOR +
124 $form->responseeditor[$i] * LESSON_RESPONSE_EDITOR;
125 $newanswer->timecreated = $timenow;
126 $newanswer->answer = trim($form->answer[$i]);
127 if (isset($form->response[$i])) {
128 $newanswer->response = trim($form->response[$i]);
130 $newanswer->jumpto = clean_param($form->jumpto[$i], PARAM_INT);
131 if (isset($form->score[$i])) {
132 $newanswer->score = clean_param($form->score[$i], PARAM_INT);
134 $newanswerid = insert_record("lesson_answers", $newanswer);
135 if (!$newanswerid) {
136 error("Update page: answer record not inserted");
139 } else {
140 if ($form->qtype == LESSON_MATCHING) {
141 if ($i >= 2) {
142 if ($form->answerid[$i]) {
143 // need to delete blanked out answer
144 if (!delete_records("lesson_answers", "id", clean_param($form->answerid[$i], PARAM_INT))) {
145 error("Update page: unable to delete answer record");
148 } else {
149 $oldanswer = new stdClass;
150 $oldanswer->id = clean_param($form->answerid[$i], PARAM_INT);
151 if (!isset($form->answereditor[$i])) {
152 $form->answereditor[$i] = 0;
154 if (!isset($form->responseeditor[$i])) {
155 $form->responseeditor[$i] = 0;
157 $oldanswer->flags = $form->answereditor[$i] * LESSON_ANSWER_EDITOR +
158 $form->responseeditor[$i] * LESSON_RESPONSE_EDITOR;
159 $oldanswer->timemodified = $timenow;
160 $oldanswer->answer = NULL;
161 if (!update_record("lesson_answers", $oldanswer)) {
162 error("Update page: answer $i not updated");
165 } elseif (!empty($form->answerid[$i])) {
166 // need to delete blanked out answer
167 if (!delete_records("lesson_answers", "id", clean_param($form->answerid[$i], PARAM_INT))) {
168 error("Update page: unable to delete answer record");
175 if ($form->redisplay) {
176 redirect("$CFG->wwwroot/mod/lesson/lesson.php?id=$cm->id&amp;action=editpage&amp;pageid=$page->id&amp;redirect=$redirect");
179 lesson_set_message(get_string('updatedpage', 'lesson').': '.format_string($page->title, true), 'notifysuccess');
180 if ($redirect == 'navigation') {
181 // takes us back to viewing the page
182 redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&amp;pageid=$page->id");
183 } else {
184 redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");