3 * Action for processing the form from addpage action and inserts the page.
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
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");
18 $form = data_submitted();
19 $newpage = new stdClass
;
20 $newanswer = new stdClass
;
22 // the new page is not the first page
23 if (!$page = get_record("lesson_pages", "id", $form->pageid
)) {
24 error("Insert page: page record not found");
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
);
34 $newpage->qoption
= 0;
36 if (isset($form->layout
)) {
37 $newpage->layout
= clean_param($form->layout
, PARAM_INT
);
41 if (isset($form->display
)) {
42 $newpage->display
= clean_param($form->display
, PARAM_INT
);
44 $newpage->display
= 0;
46 $newpage->title
= clean_param($form->title
, PARAM_CLEANHTML
);
47 $newpage->contents
= trim($form->contents
);
48 $newpage->title
= addslashes($newpage->title
);
49 $newpageid = insert_record("lesson_pages", $newpage);
51 error("Insert page: new page not inserted");
53 // update the linked list (point the previous page to this new one)
54 if (!set_field("lesson_pages", "nextpageid", $newpageid, "id", $newpage->prevpageid
)) {
55 error("Insert page: unable to update next link");
57 if ($page->nextpageid
) {
58 // new page is not the last page
59 if (!set_field("lesson_pages", "prevpageid", $newpageid, "id", $page->nextpageid
)) {
60 error("Insert page: unable to update previous link");
64 // new page is the first page
65 // get the existing (first) page (if any)
66 if (!$page = get_record_select("lesson_pages", "lessonid = $lesson->id AND prevpageid = 0")) {
67 // there are no existing pages
68 $newpage->lessonid
= $lesson->id
;
69 $newpage->prevpageid
= 0; // this is a first page
70 $newpage->nextpageid
= 0; // this is the only page
71 $newpage->timecreated
= $timenow;
72 $newpage->qtype
= clean_param($form->qtype
, PARAM_INT
);
73 if (isset($form->qoption
)) {
74 $newpage->qoption
= clean_param($form->qoption
, PARAM_INT
);
76 $newpage->qoption
= 0;
78 if (isset($form->layout
)) {
79 $newpage->layout
= clean_param($form->layout
, PARAM_INT
);
83 if (isset($form->display
)) {
84 $newpage->display
= clean_param($form->display
, PARAM_INT
);
86 $newpage->display
= 0;
88 $newpage->title
= clean_param($form->title
, PARAM_CLEANHTML
);
89 $newpage->contents
= trim($form->contents
);
90 $newpage->title
= addslashes($newpage->title
);
91 $newpageid = insert_record("lesson_pages", $newpage);
93 error("Insert page: new first page not inserted");
96 // there are existing pages put this at the start
97 $newpage->lessonid
= $lesson->id
;
98 $newpage->prevpageid
= 0; // this is a first page
99 $newpage->nextpageid
= $page->id
;
100 $newpage->timecreated
= $timenow;
101 $newpage->qtype
= clean_param($form->qtype
, PARAM_INT
);
102 if (isset($form->qoption
)) {
103 $newpage->qoption
= clean_param($form->qoption
, PARAM_INT
);
105 $newpage->qoption
= 0;
107 if (isset($form->layout
)) {
108 $newpage->layout
= clean_param($form->layout
, PARAM_INT
);
110 $newpage->layout
= 0;
112 if (isset($form->display
)) {
113 $newpage->display
= clean_param($form->display
, PARAM_INT
);
115 $newpage->display
= 0;
117 $newpage->title
= clean_param($form->title
, PARAM_CLEANHTML
);
118 $newpage->contents
= trim($form->contents
);
119 $newpage->title
= addslashes($newpage->title
);
120 $newpageid = insert_record("lesson_pages", $newpage);
122 error("Insert page: first page not inserted");
124 // update the linked list
125 if (!set_field("lesson_pages", "prevpageid", $newpageid, "id", $newpage->nextpageid
)) {
126 error("Insert page: unable to update link");
130 // now add the answers
131 if ($form->qtype
== LESSON_ESSAY
) {
132 $newanswer->lessonid
= $lesson->id
;
133 $newanswer->pageid
= $newpageid;
134 $newanswer->timecreated
= $timenow;
135 if (isset($form->jumpto
[0])) {
136 $newanswer->jumpto
= clean_param($form->jumpto
[0], PARAM_INT
);
138 if (isset($form->score
[0])) {
139 $newanswer->score
= clean_param($form->score
[0], PARAM_INT
);
141 $newanswerid = insert_record("lesson_answers", $newanswer);
143 error("Insert Page: answer record not inserted");
146 if ($form->qtype
== LESSON_MATCHING
) {
147 // need to add two to offset correct response and wrong response
148 $lesson->maxanswers
= $lesson->maxanswers +
2;
150 for ($i = 0; $i < $lesson->maxanswers
; $i++
) {
151 if (!empty($form->answer
[$i]) and trim(strip_tags($form->answer
[$i]))) { // strip_tags because the HTML editor adds <p><br />...
152 $newanswer->lessonid
= $lesson->id
;
153 $newanswer->pageid
= $newpageid;
154 $newanswer->timecreated
= $timenow;
155 $newanswer->answer
= trim($form->answer
[$i]);
156 if (isset($form->response
[$i])) {
157 $newanswer->response
= trim($form->response
[$i]);
159 if (isset($form->jumpto
[$i])) {
160 $newanswer->jumpto
= clean_param($form->jumpto
[$i], PARAM_INT
);
162 if ($lesson->custom
) {
163 if (isset($form->score
[$i])) {
164 $newanswer->score
= clean_param($form->score
[$i], PARAM_INT
);
167 $newanswerid = insert_record("lesson_answers", $newanswer);
169 error("Insert Page: answer record $i not inserted");
172 if ($form->qtype
== LESSON_MATCHING
) {
174 $newanswer->lessonid
= $lesson->id
;
175 $newanswer->pageid
= $newpageid;
176 $newanswer->timecreated
= $timenow;
177 $newanswerid = insert_record("lesson_answers", $newanswer);
179 error("Insert Page: answer record $i not inserted");
189 lesson_set_message(get_string('insertedpage', 'lesson').': '.format_string($newpage->title
, true), 'notifysuccess');
190 redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");