3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Action for adding a question page. Prints an HTML form.
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("../../config.php");
27 require_once($CFG->dirroot
.'/mod/lesson/locallib.php');
28 require_once('editpage_form.php');
30 // first get the preceeding page
31 $pageid = required_param('pageid', PARAM_INT
);
32 $id = required_param('id', PARAM_INT
); // Course Module ID
33 $qtype = optional_param('qtype', 0, PARAM_INT
);
34 $edit = optional_param('edit', false, PARAM_BOOL
);
35 $returnto = optional_param('returnto', null, PARAM_LOCALURL
);
37 if (!empty($returnto)) {
38 $returnto = new moodle_url($returnto);
40 $returnto = new moodle_url('/mod/lesson/edit.php', array('id' => $id));
41 $returnto->set_anchor('lesson-' . $pageid);
44 $cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST
);
45 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
46 $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance
), '*', MUST_EXIST
));
48 require_login($course, false, $cm);
50 $context = context_module
::instance($cm->id
);
51 require_capability('mod/lesson:edit', $context);
53 $PAGE->set_url('/mod/lesson/editpage.php', array('pageid'=>$pageid, 'id'=>$id, 'qtype'=>$qtype));
54 $PAGE->set_pagelayout('admin');
55 $PAGE->set_secondary_active_tab('modulepage');
56 $PAGE->add_body_class('limitedwidth');
59 $editpage = lesson_page
::load($pageid, $lesson);
60 $qtype = $editpage->qtype
;
66 $jumpto = lesson_page
::get_jumptooptions($pageid, $lesson);
67 $manager = lesson_page_type_manager
::get($lesson);
68 $editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES
, 'maxbytes'=>$CFG->maxbytes
);
70 // If the previous page was the Question type selection form, this form
71 // will have a different name (e.g. _qf__lesson_add_page_form_selection
72 // versus _qf__lesson_add_page_form_multichoice). This causes confusion
73 // in moodleform::_process_submission because the array key check doesn't
74 // tie up with the current form name, which in turn means the "submitted"
75 // check ends up evaluating as false, thus it's not possible to check whether
76 // the Question type selection was cancelled. For this reason, a dummy form
77 // is created here solely to check whether the selection was cancelled.
79 $mformdummy = $manager->get_page_form(0, array(
80 'editoroptions' => $editoroptions,
84 'maxbytes' => $PAGE->course
->maxbytes
,
85 'returnto' => $returnto
87 if ($mformdummy->is_cancelled()) {
93 $mform = $manager->get_page_form($qtype, array(
94 'editoroptions' => $editoroptions,
98 'maxbytes' => $PAGE->course
->maxbytes
,
99 'returnto' => $returnto
102 if ($mform->is_cancelled()) {
108 $data = $editpage->properties();
109 $data->pageid
= $editpage->id
;
111 $editoroptions['context'] = $context;
112 $data = file_prepare_standard_editor($data, 'contents', $editoroptions, $context, 'mod_lesson', 'page_contents', $editpage->id
);
115 $answers = $editpage->get_answers();
116 foreach ($answers as $answer) {
117 $answereditor = 'answer_editor['.$answerscount.']';
118 if (is_array($data->$answereditor)) {
119 $answerdata = $data->$answereditor;
120 if ($mform->get_answer_format() === LESSON_ANSWER_HTML
) {
121 $answerdraftid = file_get_submitted_draft_itemid($answereditor);
122 $answertext = file_prepare_draft_area($answerdraftid, $PAGE->cm
->context
->id
,
123 'mod_lesson', 'page_answers', $answer->id
, $editoroptions, $answerdata['text']);
124 $data->$answereditor = array('text' => $answertext, 'format' => $answerdata['format'], 'itemid' => $answerdraftid);
126 $data->$answereditor = $answerdata['text'];
130 $responseeditor = 'response_editor['.$answerscount.']';
131 if (is_array($data->$responseeditor)) {
132 $responsedata = $data->$responseeditor;
133 if ($mform->get_response_format() === LESSON_ANSWER_HTML
) {
134 $responsedraftid = file_get_submitted_draft_itemid($responseeditor);
135 $responsetext = file_prepare_draft_area($responsedraftid, $PAGE->cm
->context
->id
,
136 'mod_lesson', 'page_responses', $answer->id
, $editoroptions, $responsedata['text']);
137 $data->$responseeditor = array('text' => $responsetext, 'format' => $responsedata['format'],
138 'itemid' => $responsedraftid);
140 $data->$responseeditor = $responsedata['text'];
145 // Let the lesson pages make updates if required.
146 $data = $editpage->update_form_data($data);
148 $mform->set_data($data);
149 $PAGE->navbar
->add(get_string('edit'), new moodle_url('/mod/lesson/edit.php', array('id'=>$id)));
150 $PAGE->navbar
->add(get_string('editingquestionpage', 'lesson', get_string($mform->qtypestring
, 'lesson')));
152 // Give the page type being created a chance to override the creation process
153 // this is used by endofbranch, cluster, and endofcluster to skip the creation form.
154 // IT SHOULD ALWAYS CALL require_sesskey();
155 $mform->construction_override($pageid, $lesson);
157 $data = new stdClass
;
159 $data->pageid
= $pageid;
161 //TODO: the handling of form for the selection of question type is a bloody hack! (skodak)
162 $data->qtype
= $qtype;
164 $data = file_prepare_standard_editor($data, 'contents', $editoroptions, null);
165 $mform->set_data($data);
166 $PAGE->navbar
->add(get_string('addanewpage', 'lesson'), $PAGE->url
);
167 if ($qtype !== 'unknown') {
168 $PAGE->navbar
->add(get_string($mform->qtypestring
, 'lesson'));
172 if ($data = $mform->get_data()) {
175 $data->lessonid
= $data->id
;
176 $data->id
= $data->pageid
;
177 unset($data->pageid
);
179 $editpage->update($data, $context, $PAGE->course
->maxbytes
);
181 $editpage = lesson_page
::create($data, $lesson, $context, $PAGE->course
->maxbytes
);
186 $lessonoutput = $PAGE->get_renderer('mod_lesson');
187 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('edit', 'lesson'));
189 echo $lessonoutput->footer();