MDL-10234
[moodle-linuxchix.git] / mod / lesson / action / addcluster.php
blobbaf5aa0b6ad01d49d12fc14fbb98203cecdaca1b
1 <?php // $Id$
2 /**
3 * Action for adding a cluster 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 // if $pageid = 0, then we are inserting a new page at the beginning of the lesson
13 $pageid = required_param('pageid', PARAM_INT);
15 $timenow = time();
17 if ($pageid == 0) {
18 if (!$page = get_record("lesson_pages", "prevpageid", 0, "lessonid", $lesson->id)) {
19 error("Error: Add cluster: page record not found");
21 } else {
22 if (!$page = get_record("lesson_pages", "id", $pageid)) {
23 error("Error: Add cluster: page record not found");
26 $newpage = new stdClass;
27 $newpage->lessonid = $lesson->id;
28 $newpage->prevpageid = $pageid;
29 if ($pageid != 0) {
30 $newpage->nextpageid = $page->nextpageid;
31 } else {
32 $newpage->nextpageid = $page->id;
34 $newpage->qtype = LESSON_CLUSTER;
35 $newpage->timecreated = $timenow;
36 $newpage->title = get_string("clustertitle", "lesson");
37 $newpage->contents = get_string("clustertitle", "lesson");
38 if (!$newpageid = insert_record("lesson_pages", $newpage)) {
39 error("Insert page: new page not inserted");
41 // update the linked list...
42 if ($pageid != 0) {
43 if (!set_field("lesson_pages", "nextpageid", $newpageid, "id", $pageid)) {
44 error("Add cluster: unable to update link");
48 if ($pageid == 0) {
49 $page->nextpageid = $page->id;
51 if ($page->nextpageid) {
52 // the new page is not the last page
53 if (!set_field("lesson_pages", "prevpageid", $newpageid, "id", $page->nextpageid)) {
54 error("Insert page: unable to update previous link");
57 // ..and the single "answer"
58 $newanswer = new stdClass;
59 $newanswer->lessonid = $lesson->id;
60 $newanswer->pageid = $newpageid;
61 $newanswer->timecreated = $timenow;
62 $newanswer->jumpto = LESSON_CLUSTERJUMP;
63 if(!$newanswerid = insert_record("lesson_answers", $newanswer)) {
64 error("Add cluster: answer record not inserted");
66 lesson_set_message(get_string('addedcluster', 'lesson'), 'notifysuccess');
67 redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");