Commit | Line | Data |
---|---|---|
472e5662 | 1 | <?php |
0a4abb73 SH |
2 | |
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
5491947a | 18 | /** |
19 | * Handles lesson actions | |
86342d63 | 20 | * |
5491947a | 21 | * ACTIONS handled are: |
5491947a | 22 | * confirmdelete |
5491947a | 23 | * delete |
5491947a | 24 | * move |
25 | * moveit | |
cc3dbaaa PS |
26 | * @package mod |
27 | * @subpackage lesson | |
0a4abb73 | 28 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} |
cc3dbaaa | 29 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
5491947a | 30 | **/ |
bbcbc0fe | 31 | |
0a4abb73 SH |
32 | require_once("../../config.php"); |
33 | require_once($CFG->dirroot.'/mod/lesson/locallib.php'); | |
86342d63 | 34 | |
0a4abb73 SH |
35 | $id = required_param('id', PARAM_INT); // Course Module ID |
36 | $action = required_param('action', PARAM_ALPHA); // Action | |
37 | $pageid = required_param('pageid', PARAM_INT); | |
86342d63 | 38 | |
0e35ba6f | 39 | $cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST); |
74df2951 | 40 | $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); |
b3daa926 PS |
41 | $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST)); |
42 | ||
0a4abb73 | 43 | require_login($course, false, $cm); |
bbcbc0fe | 44 | |
a6855934 | 45 | $url = new moodle_url('/mod/lesson/lesson.php', array('id'=>$id,'action'=>$action)); |
0a4abb73 | 46 | $PAGE->set_url($url); |
c6949936 | 47 | |
5918e371 | 48 | $context = context_module::instance($cm->id); |
0a4abb73 SH |
49 | require_capability('mod/lesson:edit', $context); |
50 | require_sesskey(); | |
c6949936 | 51 | |
649cf95d | 52 | $lessonoutput = $PAGE->get_renderer('mod_lesson'); |
86342d63 | 53 | |
c5a2e12f | 54 | /// Process the action |
0a4abb73 SH |
55 | switch ($action) { |
56 | case 'confirmdelete': | |
6524006b PS |
57 | $PAGE->navbar->add(get_string($action, 'lesson')); |
58 | ||
0a4abb73 SH |
59 | $thispage = $lesson->load_page($pageid); |
60 | ||
d42bc7dc | 61 | echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('deletingpage', 'lesson', format_string($thispage->title))); |
0a4abb73 SH |
62 | echo $OUTPUT->heading(get_string("deletingpage", "lesson", format_string($thispage->title))); |
63 | // print the jumps to this page | |
64 | $params = array("lessonid" => $lesson->id, "pageid" => $pageid); | |
65 | if ($answers = $DB->get_records_select("lesson_answers", "lessonid = :lessonid AND jumpto = :pageid + 1", $params)) { | |
66 | echo $OUTPUT->heading(get_string("thefollowingpagesjumptothispage", "lesson")); | |
67 | echo "<p align=\"center\">\n"; | |
68 | foreach ($answers as $answer) { | |
69 | if (!$title = $DB->get_field("lesson_pages", "title", array("id" => $answer->pageid))) { | |
70 | print_error('cannotfindpagetitle', 'lesson'); | |
71 | } | |
72 | echo $title."<br />\n"; | |
73 | } | |
74 | } | |
75 | echo $OUTPUT->confirm(get_string("confirmdeletionofthispage","lesson"),"lesson.php?action=delete&id=$cm->id&pageid=$pageid","view.php?id=$cm->id"); | |
76 | ||
77 | break; | |
78 | case 'move': | |
6524006b | 79 | $PAGE->navbar->add(get_string($action, 'lesson')); |
0a4abb73 SH |
80 | |
81 | $title = $DB->get_field("lesson_pages", "title", array("id" => $pageid)); | |
82 | ||
d42bc7dc | 83 | echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('moving', 'lesson', format_String($title))); |
974beea1 | 84 | echo $OUTPUT->heading(get_string("moving", "lesson", format_string($title)), 3); |
0a4abb73 SH |
85 | |
86 | $params = array ("lessonid" => $lesson->id, "prevpageid" => 0); | |
87 | if (!$page = $DB->get_record_select("lesson_pages", "lessonid = :lessonid AND prevpageid = :prevpageid", $params)) { | |
88 | print_error('cannotfindfirstpage', 'lesson'); | |
89 | } | |
90 | ||
974beea1 AD |
91 | echo html_writer::start_tag('div', array('class' => 'move-page')); |
92 | ||
93 | echo html_writer::start_tag('div', array('class' => 'available-position')); | |
94 | $moveurl = "lesson.php?id=$cm->id&sesskey=".sesskey()."&action=moveit&pageid=$pageid&after=0"; | |
95 | echo html_writer::link($moveurl, get_string("movepagehere", "lesson")); | |
96 | echo html_writer::end_tag('div'); | |
97 | ||
0a4abb73 SH |
98 | while (true) { |
99 | if ($page->id != $pageid) { | |
100 | if (!$title = trim(format_string($page->title))) { | |
101 | $title = "<< ".get_string("notitle", "lesson")." >>"; | |
102 | } | |
974beea1 AD |
103 | echo html_writer::tag('div', $title, array('class' => 'page')); |
104 | ||
105 | echo html_writer::start_tag('div', array('class' => 'available-position')); | |
106 | $moveurl = "lesson.php?id=$cm->id&sesskey=".sesskey()."&action=moveit&pageid=$pageid&after={$page->id}"; | |
107 | echo html_writer::link($moveurl, get_string("movepagehere", "lesson")); | |
108 | echo html_writer::end_tag('div'); | |
0a4abb73 SH |
109 | } |
110 | if ($page->nextpageid) { | |
111 | if (!$page = $DB->get_record("lesson_pages", array("id" => $page->nextpageid))) { | |
112 | print_error('cannotfindnextpage', 'lesson'); | |
113 | } | |
114 | } else { | |
115 | // last page reached | |
116 | break; | |
117 | } | |
118 | } | |
974beea1 | 119 | echo html_writer::end_tag('div'); |
0a4abb73 SH |
120 | |
121 | break; | |
122 | case 'delete': | |
123 | $thispage = $lesson->load_page($pageid); | |
124 | $thispage->delete(); | |
0a4abb73 SH |
125 | redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id"); |
126 | break; | |
127 | case 'moveit': | |
128 | $after = (int)required_param('after', PARAM_INT); // target page | |
129 | ||
130 | $pages = $lesson->load_all_pages(); | |
131 | ||
132 | if (!array_key_exists($pageid, $pages) || ($after!=0 && !array_key_exists($after, $pages))) { | |
d16d35dc | 133 | print_error('cannotfindpages', 'lesson', "$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id"); |
0a4abb73 SH |
134 | } |
135 | $pagetomove = clone($pages[$pageid]); | |
136 | unset($pages[$pageid]); | |
137 | ||
138 | $pageids = array(); | |
139 | if ($after === 0) { | |
140 | $pageids['p0'] = $pageid; | |
141 | } | |
142 | foreach ($pages as $page) { | |
143 | $pageids[] = $page->id; | |
144 | if ($page->id == $after) { | |
145 | $pageids[] = $pageid; | |
146 | } | |
147 | } | |
148 | ||
149 | $pageidsref = $pageids; | |
150 | reset($pageidsref); | |
151 | $prev = 0; | |
152 | $next = next($pageidsref); | |
153 | foreach ($pageids as $pid) { | |
154 | if ($pid === $pageid) { | |
155 | $page = $pagetomove; | |
156 | } else { | |
157 | $page = $pages[$pid]; | |
158 | } | |
159 | if ($page->prevpageid != $prev || $page->nextpageid != $next) { | |
160 | $page->move($next, $prev); | |
161 | } | |
162 | $prev = $page->id; | |
163 | $next = next($pageidsref); | |
164 | if (!$next) { | |
165 | $next = 0; | |
166 | } | |
167 | } | |
86342d63 | 168 | |
0a4abb73 SH |
169 | redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id"); |
170 | break; | |
171 | default: | |
172 | print_error('unknowaction'); | |
173 | break; | |
174 | } | |
86342d63 | 175 | |
0e35ba6f | 176 | echo $lessonoutput->footer(); |