MDL-10234
[moodle-linuxchix.git] / mod / lesson / action / delete.php
blob48aa1a84371e6be591bf75ffe01ab0f3758a0f25
1 <?php // $Id$
2 /**
3 * Action for deleting a page
5 * @version $Id$
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package lesson
8 **/
9 confirm_sesskey();
11 $pageid = required_param('pageid', PARAM_INT);
12 if (!$thispage = get_record("lesson_pages", "id", $pageid)) {
13 error("Delete: page record not found");
16 // first delete all the associated records...
17 delete_records("lesson_attempts", "pageid", $pageid);
18 // ...now delete the answers...
19 delete_records("lesson_answers", "pageid", $pageid);
20 // ..and the page itself
21 delete_records("lesson_pages", "id", $pageid);
23 // repair the hole in the linkage
24 if (!$thispage->prevpageid) {
25 // this is the first page...
26 if (!$page = get_record("lesson_pages", "id", $thispage->nextpageid)) {
27 error("Delete: next page not found");
29 if (!set_field("lesson_pages", "prevpageid", 0, "id", $page->id)) {
30 error("Delete: unable to set prevpage link");
32 } elseif (!$thispage->nextpageid) {
33 // this is the last page...
34 if (!$page = get_record("lesson_pages", "id", $thispage->prevpageid)) {
35 error("Delete: prev page not found");
37 if (!set_field("lesson_pages", "nextpageid", 0, "id", $page->id)) {
38 error("Delete: unable to set nextpage link");
40 } else {
41 // page is in the middle...
42 if (!$prevpage = get_record("lesson_pages", "id", $thispage->prevpageid)) {
43 error("Delete: prev page not found");
45 if (!$nextpage = get_record("lesson_pages", "id", $thispage->nextpageid)) {
46 error("Delete: next page not found");
48 if (!set_field("lesson_pages", "nextpageid", $nextpage->id, "id", $prevpage->id)) {
49 error("Delete: unable to set next link");
51 if (!set_field("lesson_pages", "prevpageid", $prevpage->id, "id", $nextpage->id)) {
52 error("Delete: unable to set prev link");
55 lesson_set_message(get_string('deletedpage', 'lesson').': '.format_string($thispage->title, true), 'notifysuccess');
56 redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");