MDL-37516 Translate fieldnames on output only.
[moodle.git] / mod / lesson / lesson.php
blob75ee4bb288ceaaa42fc074fbaafc8035dafdc382
1 <?php
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.
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/>.
18 /**
19 * Handles lesson actions
21 * ACTIONS handled are:
22 * confirmdelete
23 * delete
24 * move
25 * moveit
26 * @package mod
27 * @subpackage lesson
28 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 **/
32 require_once("../../config.php");
33 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
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);
39 $cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);
40 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
41 $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
43 require_login($course, false, $cm);
45 $url = new moodle_url('/mod/lesson/lesson.php', array('id'=>$id,'action'=>$action));
46 $PAGE->set_url($url);
48 $context = context_module::instance($cm->id);
49 require_capability('mod/lesson:edit', $context);
50 require_sesskey();
52 $lessonoutput = $PAGE->get_renderer('mod_lesson');
54 /// Process the action
55 switch ($action) {
56 case 'confirmdelete':
57 $PAGE->navbar->add(get_string($action, 'lesson'));
59 $thispage = $lesson->load_page($pageid);
61 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('deletingpage', 'lesson', format_string($thispage->title)));
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');
72 echo $title."<br />\n";
75 echo $OUTPUT->confirm(get_string("confirmdeletionofthispage","lesson"),"lesson.php?action=delete&id=$cm->id&pageid=$pageid","view.php?id=$cm->id");
77 break;
78 case 'move':
79 $PAGE->navbar->add(get_string($action, 'lesson'));
81 $title = $DB->get_field("lesson_pages", "title", array("id" => $pageid));
83 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('moving', 'lesson', format_String($title)));
84 echo $OUTPUT->heading(get_string("moving", "lesson", format_string($title)));
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');
91 echo "<center><table cellpadding=\"5\" border=\"1\">\n";
92 echo "<tr><td><a href=\"lesson.php?id=$cm->id&amp;sesskey=".sesskey()."&amp;action=moveit&amp;pageid=$pageid&amp;after=0\"><small>".
93 get_string("movepagehere", "lesson")."</small></a></td></tr>\n";
94 while (true) {
95 if ($page->id != $pageid) {
96 if (!$title = trim(format_string($page->title))) {
97 $title = "<< ".get_string("notitle", "lesson")." >>";
99 echo "<tr><td><b>$title</b></td></tr>\n";
100 echo "<tr><td><a href=\"lesson.php?id=$cm->id&amp;sesskey=".sesskey()."&amp;action=moveit&amp;pageid=$pageid&amp;after={$page->id}\"><small>".
101 get_string("movepagehere", "lesson")."</small></a></td></tr>\n";
103 if ($page->nextpageid) {
104 if (!$page = $DB->get_record("lesson_pages", array("id" => $page->nextpageid))) {
105 print_error('cannotfindnextpage', 'lesson');
107 } else {
108 // last page reached
109 break;
112 echo "</table>\n";
114 break;
115 case 'delete':
116 $thispage = $lesson->load_page($pageid);
117 $thispage->delete();
118 redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
119 break;
120 case 'moveit':
121 $after = (int)required_param('after', PARAM_INT); // target page
123 $pages = $lesson->load_all_pages();
125 if (!array_key_exists($pageid, $pages) || ($after!=0 && !array_key_exists($after, $pages))) {
126 print_error('cannotfindpages', 'lesson', "$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
128 $pagetomove = clone($pages[$pageid]);
129 unset($pages[$pageid]);
131 $pageids = array();
132 if ($after === 0) {
133 $pageids['p0'] = $pageid;
135 foreach ($pages as $page) {
136 $pageids[] = $page->id;
137 if ($page->id == $after) {
138 $pageids[] = $pageid;
142 $pageidsref = $pageids;
143 reset($pageidsref);
144 $prev = 0;
145 $next = next($pageidsref);
146 foreach ($pageids as $pid) {
147 if ($pid === $pageid) {
148 $page = $pagetomove;
149 } else {
150 $page = $pages[$pid];
152 if ($page->prevpageid != $prev || $page->nextpageid != $next) {
153 $page->move($next, $prev);
155 $prev = $page->id;
156 $next = next($pageidsref);
157 if (!$next) {
158 $next = 0;
162 redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
163 break;
164 default:
165 print_error('unknowaction');
166 break;
169 echo $lessonoutput->footer();