85eab4e7f66fb2c6d872f553c473ca2ba493002c
[moodle.git] / mod / lesson / action / updatepage.php
1 <?php // $Id$
2 /**
3  * Action for processing the form in editpage action and saves the page
4  *
5  * @version $Id$
6  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7  * @package lesson
8  **/
9     confirm_sesskey();
10     
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);
18     
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");
26         }
27     }
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;
35     }
36     if (isset($form->layout)) {
37         $page->layout = clean_param($form->layout, PARAM_INT);
38     } else {
39         $page->layout = 0;
40     }
41     if (isset($form->display)) {
42         $page->display = clean_param($form->display, PARAM_INT);
43     } else {
44         $page->display = 0;
45     }
46     $page->title = clean_param($form->title, PARAM_CLEANHTML);
47     $page->contents = trim($form->contents);
48     $page->title = $page->title;
49     
50     if (!$DB->update_record("lesson_pages", $page)) {
51         print_error('cannotupdatepage', 'lesson');
52     }
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);
61         }
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         $params = array ("pageid" => $page->id);
66         if ($answers = $DB->get_records_select("lesson_answers", "pageid = :pageid", $params)) {
67             foreach ($answers as $answer) {
68                 if ($answer->id != clean_param($form->answerid[0], PARAM_INT)) {
69                     $DB->delete_records("lesson_answers", array("id" => $answer->id));
70                 }
71             }
72         }        
73         $DB->update_record("lesson_answers", $oldanswer);
74     } else {
75         // it's an "ordinary" page
76         if ($page->qtype == LESSON_MATCHING) {
77             // need to add two to offset correct response and wrong response
78             $lesson->maxanswers = $lesson->maxanswers + 2;
79         }
80         for ($i = 0; $i < $lesson->maxanswers; $i++) {
81             // strip tags because the editor gives <p><br />...
82             // also save any answers where the editor is (going to be) used
83             if ((isset($form->answer[$i]) and (trim(strip_tags($form->answer[$i]))) != '') or isset($form->answereditor[$i]) or isset($form->responseeditor[$i])) {
84                 if ($form->answerid[$i]) {
85                     $oldanswer = new stdClass;
86                     $oldanswer->id = clean_param($form->answerid[$i], PARAM_INT);
87                     if (!isset($form->answereditor[$i])) {
88                         $form->answereditor[$i] = 0;
89                     }
90                     if (!isset($form->responseeditor[$i])) {
91                         $form->responseeditor[$i] = 0;
92                     }
93                     $oldanswer->flags = $form->answereditor[$i] * LESSON_ANSWER_EDITOR +
94                                         $form->responseeditor[$i] * LESSON_RESPONSE_EDITOR;
95                     $oldanswer->timemodified = $timenow;
96                     $oldanswer->answer = trim($form->answer[$i]);
97                     if (isset($form->response[$i])) {
98                         $oldanswer->response = trim($form->response[$i]);
99                     } else {
100                         $oldanswer->response = '';
101                     }
102                     $oldanswer->jumpto = clean_param($form->jumpto[$i], PARAM_INT);
103                     if (isset($form->score[$i])) {
104                         $oldanswer->score = clean_param($form->score[$i], PARAM_INT);
105                     }
106                     $DB->update_record("lesson_answers", $oldanswer);
107                 } else {
108                     // it's a new answer
109                     $newanswer = new stdClass; // need to clear id if more than one new answer is ben added
110                     $newanswer->lessonid = $lesson->id;
111                     $newanswer->pageid = $page->id;
112                     if (!isset($form->answereditor[$i])) {
113                         $form->answereditor[$i] = 0;
114                     }
115                     if (!isset($form->responseeditor[$i])) {
116                         $form->responseeditor[$i] = 0;
117                     }
118                     $newanswer->flags = $form->answereditor[$i] * LESSON_ANSWER_EDITOR +
119                                         $form->responseeditor[$i] * LESSON_RESPONSE_EDITOR;
120                     $newanswer->timecreated = $timenow;
121                     $newanswer->answer = trim($form->answer[$i]);
122                     if (isset($form->response[$i])) {
123                         $newanswer->response = trim($form->response[$i]);
124                     }
125                     $newanswer->jumpto = clean_param($form->jumpto[$i], PARAM_INT);
126                     if (isset($form->score[$i])) {
127                         $newanswer->score = clean_param($form->score[$i], PARAM_INT);
128                     }
129                     $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
130                 }
131             } else {
132                  if ($form->qtype == LESSON_MATCHING) {
133                     if ($i >= 2) {
134                         if ($form->answerid[$i]) {
135                             // need to delete blanked out answer
136                             $DB->delete_records("lesson_answers", array("id" => clean_param($form->answerid[$i], PARAM_INT)));
137                         }
138                     } else {
139                         $oldanswer = new stdClass;
140                         $oldanswer->id = clean_param($form->answerid[$i], PARAM_INT);
141                         if (!isset($form->answereditor[$i])) {
142                             $form->answereditor[$i] = 0;
143                         }
144                         if (!isset($form->responseeditor[$i])) {
145                             $form->responseeditor[$i] = 0;
146                         }                        
147                         $oldanswer->flags = $form->answereditor[$i] * LESSON_ANSWER_EDITOR +
148                                             $form->responseeditor[$i] * LESSON_RESPONSE_EDITOR;
149                         $oldanswer->timemodified = $timenow;
150                         $oldanswer->answer = NULL;
151                         $DB->update_record("lesson_answers", $oldanswer);
152                     }                        
153                 } elseif (!empty($form->answerid[$i])) {
154                     // need to delete blanked out answer
155                     $DB->delete_records("lesson_answers", array("id" => clean_param($form->answerid[$i], PARAM_INT)));
156                 }
157             }
158         }
159     }
161     if ($form->redisplay) {
162         redirect("$CFG->wwwroot/mod/lesson/lesson.php?id=$cm->id&amp;action=editpage&amp;pageid=$page->id&amp;redirect=$redirect");
163     }
164     
165     lesson_set_message(get_string('updatedpage', 'lesson').': '.format_string($page->title, true), 'notifysuccess');
166     if ($redirect == 'navigation') {
167         // takes us back to viewing the page
168         redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&amp;pageid=$page->id");
169     } else {
170         redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
171     }
172 ?>