4f9d37fbcb852feae52bfaf3409fd19f8dee9f82
[moodle.git] / mod / lesson / action / insertpage.php
1 <?php // $Id$
2 /**
3  * Action for processing the form from addpage action and inserts 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();
11     // check to see if the cancel button was pushed
12     if (optional_param('cancel', '', PARAM_ALPHA)) {
13         redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
14     }
16     $timenow = time();
17     
18     $form = data_submitted();
19     $newpage = new stdClass;
20     $newanswer = new stdClass;
21     if ($form->pageid) {
22         // the new page is not the first page
23         if (!$page = $DB->get_record("lesson_pages", array("id" => $form->pageid))) {
24             print_error('cannotfindpages', 'lesson');
25         }
26         $newpage->lessonid = clean_param($lesson->id, PARAM_INT);
27         $newpage->prevpageid = clean_param($form->pageid, PARAM_INT);
28         $newpage->nextpageid = clean_param($page->nextpageid, PARAM_INT);
29         $newpage->timecreated = $timenow;
30         $newpage->qtype = $form->qtype;
31         if (isset($form->qoption)) {
32             $newpage->qoption = clean_param($form->qoption, PARAM_INT);
33         } else {
34             $newpage->qoption = 0;
35         }
36         if (isset($form->layout)) {
37             $newpage->layout = clean_param($form->layout, PARAM_INT);
38         } else {
39             $newpage->layout = 0;
40         }
41         if (isset($form->display)) {
42             $newpage->display = clean_param($form->display, PARAM_INT);
43         } else {
44             $newpage->display = 0;
45         }
46         $newpage->title = clean_param($form->title, PARAM_CLEANHTML);
47         $newpage->contents = trim($form->contents);
48         $newpage->title = $newpage->title;
49         $newpageid = $DB->insert_record("lesson_pages", $newpage);
50         // update the linked list (point the previous page to this new one)
51         $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $newpage->prevpageid));
52         if ($page->nextpageid) {
53             // new page is not the last page
54             $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid));
55         }
56     } else {
57         // new page is the first page
58         // get the existing (first) page (if any)
59         $params = array ("lessonid" => $lesson->id, "prevpageid" => 0);
60         if (!$page = $DB->get_record_select("lesson_pages", "lessonid = :lessonid AND prevpageid = :prevpageid", $params)) {
61             // there are no existing pages
62             $newpage->lessonid = $lesson->id;
63             $newpage->prevpageid = 0; // this is a first page
64             $newpage->nextpageid = 0; // this is the only page
65             $newpage->timecreated = $timenow;
66             $newpage->qtype = clean_param($form->qtype, PARAM_INT);
67             if (isset($form->qoption)) {
68                 $newpage->qoption = clean_param($form->qoption, PARAM_INT);
69             } else {
70                 $newpage->qoption = 0;
71             }
72             if (isset($form->layout)) {
73                 $newpage->layout = clean_param($form->layout, PARAM_INT);
74             } else {
75                 $newpage->layout = 0;
76             }
77             if (isset($form->display)) {
78                 $newpage->display = clean_param($form->display, PARAM_INT);
79             } else {
80                 $newpage->display = 0;
81             }                
82             $newpage->title = clean_param($form->title, PARAM_CLEANHTML);
83             $newpage->contents = trim($form->contents);
84             $newpage->title = $newpage->title;
85             $newpageid = $DB->insert_record("lesson_pages", $newpage);
86         } else {
87             // there are existing pages put this at the start
88             $newpage->lessonid = $lesson->id;
89             $newpage->prevpageid = 0; // this is a first page
90             $newpage->nextpageid = $page->id;
91             $newpage->timecreated = $timenow;
92             $newpage->qtype = clean_param($form->qtype, PARAM_INT);
93             if (isset($form->qoption)) {
94                 $newpage->qoption = clean_param($form->qoption, PARAM_INT);
95             } else {
96                 $newpage->qoption = 0;
97             }
98             if (isset($form->layout)) {
99                 $newpage->layout = clean_param($form->layout, PARAM_INT);
100             } else {
101                 $newpage->layout = 0;
102             }
103             if (isset($form->display)) {
104                 $newpage->display = clean_param($form->display, PARAM_INT);
105             } else {
106                 $newpage->display = 0;
107             }                
108             $newpage->title = clean_param($form->title, PARAM_CLEANHTML);
109             $newpage->contents = trim($form->contents);
110             $newpage->title = $newpage->title;
111             $newpageid = $DB->insert_record("lesson_pages", $newpage);
112             if (!$newpageid) {
113                 print_error('cannotfindfirstpage', 'lesson');
114             }
115             // update the linked list
116             if (!$DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $newpage->nextpageid))) {
117                 print_error('cannotupdatelink', 'lesson');
118             }
119         }
120     }
121     // now add the answers
122     if ($form->qtype == LESSON_ESSAY) {
123         $newanswer->lessonid = $lesson->id;
124         $newanswer->pageid = $newpageid;
125         $newanswer->timecreated = $timenow;
126         if (isset($form->jumpto[0])) {
127             $newanswer->jumpto = clean_param($form->jumpto[0], PARAM_INT);
128         }
129         if (isset($form->score[0])) {
130             $newanswer->score = clean_param($form->score[0], PARAM_INT);
131         }
132         $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
133     } else {
134         if ($form->qtype == LESSON_MATCHING) {
135             // need to add two to offset correct response and wrong response
136             $lesson->maxanswers = $lesson->maxanswers + 2;
137         }
138         for ($i = 0; $i < $lesson->maxanswers; $i++) {
139             if (!empty($form->answer[$i]) and trim(strip_tags($form->answer[$i]))) { // strip_tags because the HTML editor adds <p><br />...
140                 $newanswer->lessonid = $lesson->id;
141                 $newanswer->pageid = $newpageid;
142                 $newanswer->timecreated = $timenow;
143                 $newanswer->answer = trim($form->answer[$i]);
144                 if (isset($form->response[$i])) {
145                     $newanswer->response = trim($form->response[$i]);
146                 }
147                 if (isset($form->jumpto[$i])) {
148                     $newanswer->jumpto = clean_param($form->jumpto[$i], PARAM_INT);
149                 }
150                 if ($lesson->custom) {
151                     if (isset($form->score[$i])) {
152                         $newanswer->score = clean_param($form->score[$i], PARAM_INT);
153                     }
154                 }
155                 $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
156             } else {
157                 if ($form->qtype == LESSON_MATCHING) {
158                     if ($i < 2) {
159                         $newanswer->lessonid = $lesson->id;
160                         $newanswer->pageid = $newpageid;
161                         $newanswer->timecreated = $timenow;
162                         $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
163                     }
164                 } else {
165                     break;
166                 }
167             }
168         }
169     }
170     
171     lesson_set_message(get_string('insertedpage', 'lesson').': '.format_string($newpage->title, true), 'notifysuccess');
172     redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
173 ?>