Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
/**
17
 * Page for editing questions.
18
 *
19
 * @package    qbank_editquestion
20
 * @copyright  1999 onwards Martin Dougiamas {@link http://moodle.com}
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
require_once(__DIR__ . '/../../../config.php');
25
require_once(__DIR__ . '/../../editlib.php');
26
require_once($CFG->libdir . '/filelib.php');
27
require_once($CFG->libdir . '/formslib.php');
28
 
29
// Read URL parameters telling us which question to edit.
30
$id = optional_param('id', 0, PARAM_INT); // Question id.
31
$makecopy = optional_param('makecopy', 0, PARAM_BOOL);
32
$qtype = optional_param('qtype', '', PARAM_COMPONENT);
33
$categoryid = optional_param('category', 0, PARAM_INT);
1441 ariadna 34
$cmid = required_param('cmid', PARAM_INT);
1 efrain 35
$wizardnow = optional_param('wizardnow', '', PARAM_ALPHA);
36
$originalreturnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
37
$appendqnumstring = optional_param('appendqnumstring', '', PARAM_ALPHA);
38
$inpopup = optional_param('inpopup', 0, PARAM_BOOL);
39
$mdlscrollto = optional_param('mdlscrollto', 0, PARAM_INT);
40
 
41
\core_question\local\bank\helper::require_plugin_enabled('qbank_editquestion');
42
 
43
$url = new moodle_url('/question/bank/editquestion/question.php');
44
if ($id !== 0) {
45
    $url->param('id', $id);
46
}
47
if ($makecopy) {
48
    $url->param('makecopy', $makecopy);
49
}
50
if ($qtype !== '') {
51
    $url->param('qtype', $qtype);
52
}
53
if ($categoryid !== 0) {
54
    $url->param('category', $categoryid);
55
}
56
if ($cmid !== 0) {
57
    $url->param('cmid', $cmid);
58
}
59
if ($wizardnow !== '') {
60
    $url->param('wizardnow', $wizardnow);
61
}
62
if ($originalreturnurl !== 0) {
63
    $url->param('returnurl', $originalreturnurl);
64
}
65
if ($appendqnumstring !== '') {
66
    $url->param('appendqnumstring', $appendqnumstring);
67
}
68
if ($inpopup !== 0) {
69
    $url->param('inpopup', $inpopup);
70
}
71
if ($mdlscrollto) {
72
    $url->param('mdlscrollto', $mdlscrollto);
73
}
74
$PAGE->set_url($url);
75
 
1441 ariadna 76
$questionbankurl = new moodle_url('/question/edit.php', ['cmid' => $cmid]);
77
 
1 efrain 78
navigation_node::override_active_url($questionbankurl);
79
 
80
if ($originalreturnurl) {
81
    if (strpos($originalreturnurl, '/') !== 0) {
82
        throw new coding_exception("returnurl must be a local URL starting with '/'. $originalreturnurl was given.");
83
    }
84
    $returnurl = new moodle_url($originalreturnurl);
85
} else {
86
    $returnurl = $questionbankurl;
87
}
88
if ($mdlscrollto) {
89
    $returnurl->param('mdlscrollto', $mdlscrollto);
90
}
91
 
1441 ariadna 92
list($module, $cm) = get_module_from_cmid($cmid);
93
require_login($cm->course, false, $cm);
94
$thiscontext = context_module::instance($cmid);
95
 
1 efrain 96
$contexts = new core_question\local\bank\question_edit_contexts($thiscontext);
97
$PAGE->set_pagelayout('admin');
98
 
99
if (optional_param('addcancel', false, PARAM_BOOL)) {
100
    redirect($returnurl);
101
}
102
 
103
if ($id) {
104
    if (!$question = $DB->get_record('question', array('id' => $id))) {
105
        throw new moodle_exception('questiondoesnotexist', 'question', $returnurl);
106
    }
1441 ariadna 107
    get_question_options($question, true);
1 efrain 108
 
109
} else if ($categoryid && $qtype) { // Only for creating new questions.
110
    $question = new stdClass();
111
    $question->category = $categoryid;
112
    $question->qtype = $qtype;
113
    $question->createdby = $USER->id;
114
 
115
    // Check that users are allowed to create this question type at the moment.
116
    if (!question_bank::qtype_enabled($qtype)) {
117
        throw new moodle_exception('cannotenable', 'question', $returnurl, $qtype);
118
    }
119
 
120
} else if ($categoryid) {
121
    // Category, but no qtype. They probably came from the addquestion.php
122
    // script without choosing a question type. Send them back.
123
    $addurl = new moodle_url('/question/bank/editquestion/addquestion.php', $url->params());
124
    $addurl->param('validationerror', 1);
125
    redirect($addurl);
126
 
127
} else {
128
    throw new moodle_exception('notenoughdatatoeditaquestion', 'question', $returnurl);
129
}
130
 
1441 ariadna 131
// If we get to the editing page from somewhere outside the question bank (such as the quiz) while migration is still
132
// pending, redirect back with a warning.
133
if (!\core_question\local\bank\question_bank_helper::has_bank_migration_task_completed_successfully()) {
134
    $defaultactivityname = \core_question\local\bank\question_bank_helper::get_default_question_bank_activity_name();
135
    redirect(
136
        $returnurl,
137
        get_string('transfernotfinished', 'mod_' . $defaultactivityname),
138
        messagetype: \core\output\notification::NOTIFY_WARNING,
139
    );
140
}
141
 
1 efrain 142
$qtypeobj = question_bank::get_qtype($question->qtype);
143
 
144
if (isset($question->categoryobject)) {
145
    $category = $question->categoryobject;
146
} else {
147
    // Validate the question category.
148
    if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
149
        throw new moodle_exception('categorydoesnotexist', 'question', $returnurl);
150
    }
151
}
152
 
153
// Check permissions.
154
$question->formoptions = new stdClass();
155
 
156
$categorycontext = context::instance_by_id($category->contextid);
157
$question->contextid = $category->contextid;
158
$addpermission = has_capability('moodle/question:add', $categorycontext);
159
 
160
if ($id) {
161
    $question->formoptions->canedit = question_has_capability_on($question, 'edit');
162
    $question->formoptions->canmove = $addpermission && question_has_capability_on($question, 'move');
163
    $question->formoptions->cansaveasnew = $addpermission &&
164
            (question_has_capability_on($question, 'view') || $question->formoptions->canedit);
165
    $question->formoptions->repeatelements = $question->formoptions->canedit || $question->formoptions->cansaveasnew;
166
    $formeditable = $question->formoptions->canedit || $question->formoptions->cansaveasnew || $question->formoptions->canmove;
167
    if (!$formeditable) {
168
        question_require_capability_on($question, 'view');
169
    }
170
    $question->beingcopied = false;
171
    if ($makecopy) {
172
        // If we are duplicating a question, add some indication to the question name.
173
        $question->name = get_string('questionnamecopy', 'question', $question->name);
174
        $question->idnumber = isset($question->idnumber) ?
175
            core_question_find_next_unused_idnumber($question->idnumber, $category->id) : '';
176
        $question->beingcopied = true;
177
    }
178
 
179
} else { // Creating a new question.
180
    $question->formoptions->canedit = question_has_capability_on($question, 'edit');
181
    $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission);
182
    $question->formoptions->cansaveasnew = false;
183
    $question->formoptions->repeatelements = true;
184
    $formeditable = true;
185
    require_capability('moodle/question:add', $categorycontext);
186
}
187
$question->formoptions->mustbeusable = (bool) $appendqnumstring;
188
 
189
// Validate the question type.
190
$PAGE->set_pagetype('question-type-' . $question->qtype);
191
 
192
// Create the question editing form.
193
if ($wizardnow !== '') {
194
    $mform = $qtypeobj->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
195
} else {
196
    $mform = $qtypeobj->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
197
}
198
$toform = fullclone($question); // Send the question object and a few more parameters to the form.
199
$toform->category = "{$category->id},{$category->contextid}";
200
$toform->mdlscrollto = $mdlscrollto;
201
if ($formeditable && $id) {
202
    $toform->categorymoveto = $toform->category;
203
}
204
 
205
$toform->appendqnumstring = $appendqnumstring;
206
$toform->returnurl = $originalreturnurl;
207
$toform->makecopy = $makecopy;
208
$toform->idnumber = null;
209
if (!empty($question->id)) {
210
    $questionobject = question_bank::load_question($question->id);
211
    $toform->status = $questionobject->status;
212
    $toform->idnumber = $questionobject->idnumber;
213
} else {
214
    $toform->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
215
}
216
if ($makecopy) {
217
    $toform->idnumber = core_question_find_next_unused_idnumber($toform->idnumber, $category->id);
218
}
219
if ($cm !== null) {
220
    $toform->cmid = $cm->id;
221
    $toform->courseid = $cm->course;
222
} else {
223
    $toform->courseid = $COURSE->id;
224
}
225
 
226
$toform->inpopup = $inpopup;
227
 
228
// Prepare custom fields data.
229
$customfieldhandler = qbank_customfields\customfield\question_handler::create();
230
$customfieldhandler->instance_form_before_set_data($toform);
231
 
232
$mform->set_data($toform);
233
 
234
if ($mform->is_cancelled()) {
235
    if ($inpopup) {
236
        close_window();
237
    } else {
238
        redirect($returnurl);
239
    }
240
 
241
} else if ($fromform = $mform->get_data()) {
242
    // If we are saving as a copy, break the connection to the old question.
243
    if ($makecopy) {
244
        $question->id = 0;
245
        // Copies should not be hidden.
246
        $question->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
247
    }
248
 
249
    // If is will be added directly to a module send the module name to be referenced.
250
    if ($appendqnumstring && $cm) {
251
        $fromform->modulename = 'mod_' . $cm->modname;
252
    }
253
 
254
    // Process the combination of usecurrentcat, categorymoveto and category form
255
    // fields, so the save_question method only has to consider $fromform->category.
256
    if (empty($fromform->usecurrentcat) && !empty($fromform->categorymoveto)) {
257
        $fromform->category = $fromform->categorymoveto;
258
    }
259
 
260
    // If we are moving a question, check we have permission to move it from
261
    // whence it came (Where we are moving to is validated by the form).
262
    list($newcatid, $newcontextid) = explode(',', $fromform->category);
263
    if (!empty($question->id) && $newcatid != $question->categoryobject->id) {
264
        $contextid = $newcontextid;
265
        question_require_capability_on($question, 'move');
266
    } else {
267
        $contextid = $category->contextid;
268
    }
269
 
270
    // Ensure we redirect back to the category the question is being saved into.
271
    $returnurl->param('category', $fromform->category);
272
 
273
    // We are actually saving the question.
274
    if (!empty($question->id)) {
275
        question_require_capability_on($question, 'edit');
276
    } else {
277
        require_capability('moodle/question:add', context::instance_by_id($contextid));
278
        if (!empty($fromform->makecopy) && !$question->formoptions->cansaveasnew) {
279
            throw new moodle_exception('nopermissions', '', '', 'edit');
280
        }
281
    }
282
 
1441 ariadna 283
    // If this is a new question and question defaults save is enabled, save defaults for user in user_preferences table.
284
    if (empty($question->id) && !empty($CFG->questiondefaultssave)) {
1 efrain 285
        $qtypeobj->save_defaults_for_new_questions($fromform);
286
    }
287
    $question = $qtypeobj->save_question($question, $fromform);
288
    if (isset($fromform->tags)) {
289
        // If we have any question context level tags then set those tags now.
290
        core_tag_tag::set_item_tags('core_question', 'question', $question->id,
291
                context::instance_by_id($contextid), $fromform->tags, 0);
292
    }
293
 
294
    if (isset($fromform->coursetags)) {
295
        // If we have and course context level tags then set those now.
296
        core_tag_tag::set_item_tags('core_question', 'question', $question->id,
297
                context_course::instance($fromform->courseid), $fromform->coursetags, 0);
298
    }
299
 
300
    // Update custom fields if there are any of them in the form.
301
    $customfieldhandler->instance_form_save($fromform);
302
 
303
    // Purge this question from the cache.
304
    question_bank::notify_question_edited($question->id);
305
 
306
    // If we are saving and continuing to edit the question.
307
    if (!empty($fromform->updatebutton)) {
308
        $url->param('id', $question->id);
309
        $url->remove_params('makecopy');
310
        redirect($url);
311
    }
312
 
313
    if ($qtypeobj->finished_edit_wizard($fromform)) {
314
        if ($inpopup) {
315
            echo $OUTPUT->notification(get_string('changessaved'), '');
316
            close_window(3);
317
        } else {
318
            $returnurl->param('lastchanged', $question->id);
319
            if ($appendqnumstring) {
320
                $returnurl->param($appendqnumstring, $question->id);
321
                $returnurl->param('sesskey', sesskey());
322
                $returnurl->param('cmid', $cmid);
323
            }
1441 ariadna 324
            // Update the filter param to the updated category if the return have any.
325
            if (!empty($returnurl->param('filter'))) {
326
                $filter = json_decode($returnurl->param('filter'), true);
327
                if (isset($filter['category']['values'])) {
328
                    $filter['category']['values'][0] = $question->category;
329
                    $returnurl->param('filter', json_encode($filter));
330
                }
331
            }
1 efrain 332
            redirect($returnurl);
333
        }
334
 
335
    } else {
336
        $nexturlparams = [
337
                'returnurl' => $originalreturnurl,
338
                'appendqnumstring' => $appendqnumstring,
339
                'mdlscrollto' => $mdlscrollto,
340
        ];
341
        if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)) {
342
            // Useful for passing data to the next page which is not saved in the database.
343
            $nexturlparams += $fromform->nextpageparam;
344
        }
345
        $nexturlparams['id'] = $question->id;
346
        $nexturlparams['wizardnow'] = $fromform->wizard;
347
        $nexturl = new moodle_url($url, $nexturlparams);
348
        if ($cmid) {
349
            $nexturl->param('cmid', $cmid);
350
        } else {
351
            $nexturl->param('courseid', $COURSE->id);
352
        }
1441 ariadna 353
        // Update the filter param to the updated category if the return url have any.
354
        if (!empty($nexturl->param('filter'))) {
355
            $filter = json_decode($nexturl->param('filter'), true);
356
            if (isset($filter['category']['values'])) {
357
                $filter['category']['values'][0] = $question->category;
358
                $nexturl->param('filter', json_encode($filter));
359
            }
360
        }
1 efrain 361
        redirect($nexturl);
362
    }
363
 
364
}
365
 
366
$streditingquestion = $qtypeobj->get_heading();
367
$PAGE->set_title($streditingquestion);
368
$PAGE->set_heading($COURSE->fullname);
369
$PAGE->activityheader->disable();
370
$PAGE->navbar->add($streditingquestion);
371
if ($PAGE->course->id == $SITE->id) {
372
    $PAGE->set_primary_active_tab('home');
373
}
374
 
375
// Display a heading, question editing form and possibly some extra content needed for
376
// for this question type.
377
echo $OUTPUT->header();
378
$qtypeobj->display_question_editing_page($mform, $question, $wizardnow);
379
echo $OUTPUT->footer();