MDL-10234
[moodle-linuxchix.git] / mod / lesson / action / addendofbranch.php
blob5decb9f2f9fe58d1d6a3f4b6104e46316aeabe25
1 <?php // $Id$
2 /**
3 * Action for adding an end of branch page
5 * @version $Id$
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package lesson
8 **/
9 confirm_sesskey();
11 // first get the preceeding page
12 $pageid = required_param('pageid', PARAM_INT);
14 $timenow = time();
16 // the new page is not the first page (end of branch always comes after an existing page)
17 if (!$page = get_record("lesson_pages", "id", $pageid)) {
18 error("Add end of branch: page record not found");
20 // chain back up to find the (nearest branch table)
21 $btpageid = $pageid;
22 if (!$btpage = get_record("lesson_pages", "id", $btpageid)) {
23 error("Add end of branch: btpage record not found");
25 while (($btpage->qtype != LESSON_BRANCHTABLE) AND ($btpage->prevpageid > 0)) {
26 $btpageid = $btpage->prevpageid;
27 if (!$btpage = get_record("lesson_pages", "id", $btpageid)) {
28 error("Add end of branch: btpage record not found");
31 if ($btpage->qtype == LESSON_BRANCHTABLE) {
32 $newpage = new stdClass;
33 $newpage->lessonid = $lesson->id;
34 $newpage->prevpageid = $pageid;
35 $newpage->nextpageid = $page->nextpageid;
36 $newpage->qtype = LESSON_ENDOFBRANCH;
37 $newpage->timecreated = $timenow;
38 $newpage->title = get_string("endofbranch", "lesson");
39 $newpage->contents = get_string("endofbranch", "lesson");
40 if (!$newpageid = insert_record("lesson_pages", $newpage)) {
41 error("Insert page: new page not inserted");
43 // update the linked list...
44 if (!set_field("lesson_pages", "nextpageid", $newpageid, "id", $pageid)) {
45 error("Add end of branch: unable to update link");
47 if ($page->nextpageid) {
48 // the new page is not the last page
49 if (!set_field("lesson_pages", "prevpageid", $newpageid, "id", $page->nextpageid)) {
50 error("Insert page: unable to update previous link");
53 // ..and the single "answer"
54 $newanswer = new stdClass;
55 $newanswer->lessonid = $lesson->id;
56 $newanswer->pageid = $newpageid;
57 $newanswer->timecreated = $timenow;
58 $newanswer->jumpto = $btpageid;
59 if(!$newanswerid = insert_record("lesson_answers", $newanswer)) {
60 error("Add end of branch: answer record not inserted");
63 lesson_set_message(get_string('addedanendofbranch', 'lesson'), 'notifysuccess');
64 } else {
65 lesson_set_message(get_string('nobranchtablefound', 'lesson'));
68 redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");