MDL-73975 course: Remove course_search_form template
[moodle.git] / question / bank / deletequestion / delete.php
blob396dda83f99b0ccb617da5557c86e8c4f83c765f
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.
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/>.
17 /**
18 * Delete question page.
20 * This code is based on question/classes/bank/view.php
22 * @package qbank_deletequestion
23 * @copyright 2021 Catalyst IT Australia Pty Ltd
24 * @author Safat Shahin <safatshahin@catalyst-au.net>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once(__DIR__ . '/../../../config.php');
29 require_once(__DIR__ . '/../../editlib.php');
30 global $DB, $OUTPUT, $PAGE, $COURSE;
32 $deleteselected = optional_param('deleteselected', false, PARAM_BOOL);
33 $returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
34 $cmid = optional_param('cmid', 0, PARAM_INT);
35 $courseid = optional_param('courseid', 0, PARAM_INT);
36 $deleteall = optional_param('deleteall', false, PARAM_BOOL);
38 if ($returnurl) {
39 $returnurl = new moodle_url($returnurl);
42 \core_question\local\bank\helper::require_plugin_enabled('qbank_deletequestion');
44 if ($cmid) {
45 list($module, $cm) = get_module_from_cmid($cmid);
46 require_login($cm->course, false, $cm);
47 $thiscontext = context_module::instance($cmid);
48 } else if ($courseid) {
49 require_login($courseid, false);
50 $thiscontext = context_course::instance($courseid);
51 } else {
52 throw new moodle_exception('missingcourseorcmid', 'question');
55 $contexts = new core_question\local\bank\question_edit_contexts($thiscontext);
56 $url = new moodle_url('/question/bank/deletequestion/delete.php');
58 $PAGE->set_url($url);
59 $streditingquestions = get_string('deletequestion', 'qbank_deletequestion');
60 $PAGE->set_title($streditingquestions);
61 $PAGE->set_heading($COURSE->fullname);
62 $PAGE->activityheader->disable();
63 $PAGE->set_secondary_active_tab("questionbank");
65 // Unhide a question.
66 if (($unhide = optional_param('unhide', '', PARAM_INT)) and confirm_sesskey()) {
67 question_require_capability_on($unhide, 'edit');
68 $DB->set_field('question_versions', 'status',
69 \core_question\local\bank\question_version_status::QUESTION_STATUS_READY, ['questionid' => $unhide]);
71 // Purge these questions from the cache.
72 \question_bank::notify_question_edited($unhide);
74 redirect($returnurl);
77 // If user has already confirmed the action.
78 if ($deleteselected && ($confirm = optional_param('confirm', '', PARAM_ALPHANUM))
79 && confirm_sesskey()) {
80 $deleteselected = required_param('deleteselected', PARAM_RAW);
81 if ($confirm == md5($deleteselected)) {
82 if ($questionlist = explode(',', $deleteselected)) {
83 \qbank_deletequestion\helper::delete_questions($questionlist, $deleteall);
85 redirect($returnurl);
86 } else {
87 throw new \moodle_exception('invalidconfirm', 'question');
91 echo $OUTPUT->header();
93 if ($deleteselected) {
94 // Make a list of all the questions that are selected.
95 $rawquestions = $_REQUEST; // This code is called by both POST forms and GET links, so cannot use data_submitted.
96 $questionlist = ''; // Comma separated list of ids of questions to be deleted.
97 foreach ($rawquestions as $key => $value) { // Parse input for question ids.
98 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
99 $key = $matches[1];
100 $questionlist .= $key.',';
101 question_require_capability_on((int)$key, 'edit');
104 if (!$questionlist) { // No questions were selected.
105 redirect($returnurl);
107 $questionlist = rtrim($questionlist, ',');
109 $deleteurl = new \moodle_url(
110 '/question/bank/deletequestion/delete.php',
112 'deleteselected' => $questionlist,
113 'deleteall' => $deleteall,
114 'confirm' => md5($questionlist),
115 'sesskey' => sesskey(),
116 'returnurl' => $returnurl->out_as_local_url(false),
117 'cmid' => $cmid,
118 'courseid' => $courseid,
121 $continue = new \single_button($deleteurl, get_string('delete'), 'post');
123 $questionids = explode(',', $questionlist);
124 [$displayoptions, $message] = qbank_deletequestion\helper::get_delete_confirmation_message($questionids,
125 $deleteall);
127 echo $OUTPUT->confirm($message, $continue, $returnurl, $displayoptions);
130 echo $OUTPUT->footer();