Fixing drift between CVS and git
[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     $DB->update_record("lesson_pages", $page);
51     if ($page->qtype == LESSON_ENDOFBRANCH || $page->qtype == LESSON_ESSAY || $page->qtype == LESSON_CLUSTER || $page->qtype == LESSON_ENDOFCLUSTER) {
52         // there's just a single answer with a jump
53         $oldanswer = new stdClass;
54         $oldanswer->id = $form->answerid[0];
55         $oldanswer->timemodified = $timenow;
56         $oldanswer->jumpto = clean_param($form->jumpto[0], PARAM_INT);
57         if (isset($form->score[0])) {
58             $oldanswer->score = clean_param($form->score[0], PARAM_INT);
59         }
60         // delete other answers  this if mainly for essay questions.  If one switches from using a qtype like Multichoice,
61         // then switches to essay, the old answers need to be removed because essay is
62         // supposed to only have one answer record
63         $params = array ("pageid" => $page->id);
64         if ($answers = $DB->get_records_select("lesson_answers", "pageid = :pageid", $params)) {
65             foreach ($answers as $answer) {
66                 if ($answer->id != clean_param($form->answerid[0], PARAM_INT)) {
67                     $DB->delete_records("lesson_answers", array("id" => $answer->id));
68                 }
69             }
70         }        
71         $DB->update_record("lesson_answers", $oldanswer);
72     } else {
73         // it's an "ordinary" page
74         if ($page->qtype == LESSON_MATCHING) {
75             // need to add two to offset correct response and wrong response
76             $lesson->maxanswers = $lesson->maxanswers + 2;
77         }
78         for ($i = 0; $i < $lesson->maxanswers; $i++) {
79             // strip tags because the editor gives <p><br />...
80             // also save any answers where the editor is (going to be) used
81             if ((isset($form->answer[$i]) and (trim(strip_tags($form->answer[$i]))) != '') or isset($form->answereditor[$i]) or isset($form->responseeditor[$i])) {
82                 if ($form->answerid[$i]) {
83                     $oldanswer = new stdClass;
84                     $oldanswer->id = clean_param($form->answerid[$i], PARAM_INT);
85                     if (!isset($form->answereditor[$i])) {
86                         $form->answereditor[$i] = 0;
87                     }
88                     if (!isset($form->responseeditor[$i])) {
89                         $form->responseeditor[$i] = 0;
90                     }
91                     $oldanswer->flags = $form->answereditor[$i] * LESSON_ANSWER_EDITOR +
92                                         $form->responseeditor[$i] * LESSON_RESPONSE_EDITOR;
93                     $oldanswer->timemodified = $timenow;
94                     $oldanswer->answer = trim($form->answer[$i]);
95                     if (isset($form->response[$i])) {
96                         $oldanswer->response = trim($form->response[$i]);
97                     } else {
98                         $oldanswer->response = '';
99                     }
100                     $oldanswer->jumpto = clean_param($form->jumpto[$i], PARAM_INT);
101                     if (isset($form->score[$i])) {
102                         $oldanswer->score = clean_param($form->score[$i], PARAM_INT);
103                     }
104                     $DB->update_record("lesson_answers", $oldanswer);
105                 } else {
106                     // it's a new answer
107                     $newanswer = new stdClass; // need to clear id if more than one new answer is ben added
108                     $newanswer->lessonid = $lesson->id;
109                     $newanswer->pageid = $page->id;
110                     if (!isset($form->answereditor[$i])) {
111                         $form->answereditor[$i] = 0;
112                     }
113                     if (!isset($form->responseeditor[$i])) {
114                         $form->responseeditor[$i] = 0;
115                     }
116                     $newanswer->flags = $form->answereditor[$i] * LESSON_ANSWER_EDITOR +
117                                         $form->responseeditor[$i] * LESSON_RESPONSE_EDITOR;
118                     $newanswer->timecreated = $timenow;
119                     $newanswer->answer = trim($form->answer[$i]);
120                     if (isset($form->response[$i])) {
121                         $newanswer->response = trim($form->response[$i]);
122                     }
123                     $newanswer->jumpto = clean_param($form->jumpto[$i], PARAM_INT);
124                     if (isset($form->score[$i])) {
125                         $newanswer->score = clean_param($form->score[$i], PARAM_INT);
126                     }
127                     $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
128                 }
129             } else {
130                  if ($form->qtype == LESSON_MATCHING) {
131                     if ($i >= 2) {
132                         if ($form->answerid[$i]) {
133                             // need to delete blanked out answer
134                             $DB->delete_records("lesson_answers", array("id" => clean_param($form->answerid[$i], PARAM_INT)));
135                         }
136                     } else {
137                         $oldanswer = new stdClass;
138                         $oldanswer->id = clean_param($form->answerid[$i], PARAM_INT);
139                         if (!isset($form->answereditor[$i])) {
140                             $form->answereditor[$i] = 0;
141                         }
142                         if (!isset($form->responseeditor[$i])) {
143                             $form->responseeditor[$i] = 0;
144                         }                        
145                         $oldanswer->flags = $form->answereditor[$i] * LESSON_ANSWER_EDITOR +
146                                             $form->responseeditor[$i] * LESSON_RESPONSE_EDITOR;
147                         $oldanswer->timemodified = $timenow;
148                         $oldanswer->answer = NULL;
149                         $DB->update_record("lesson_answers", $oldanswer);
150                     }                        
151                 } elseif (!empty($form->answerid[$i])) {
152                     // need to delete blanked out answer
153                     $DB->delete_records("lesson_answers", array("id" => clean_param($form->answerid[$i], PARAM_INT)));
154                 }
155             }
156         }
157     }
159     if ($form->redisplay) {
160         redirect("$CFG->wwwroot/mod/lesson/lesson.php?id=$cm->id&amp;action=editpage&amp;pageid=$page->id&amp;redirect=$redirect");
161     }
162     
163     lesson_set_message(get_string('updatedpage', 'lesson').': '.format_string($page->title, true), 'notifysuccess');
164     if ($redirect == 'navigation') {
165         // takes us back to viewing the page
166         redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&amp;pageid=$page->id");
167     } else {
168         redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
169     }
170 ?>