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
/**
18
 * Delete question page.
19
 *
20
 * This code is based on question/classes/bank/view.php
21
 *
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
26
 */
27
 
28
require_once(__DIR__ . '/../../../config.php');
29
require_once(__DIR__ . '/../../editlib.php');
30
global $DB, $OUTPUT, $PAGE, $COURSE;
31
 
32
$deleteselected = optional_param('deleteselected', false, PARAM_BOOL);
33
$returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
1441 ariadna 34
$cmid = required_param('cmid', PARAM_INT);
1 efrain 35
$deleteall = optional_param('deleteall', false, PARAM_BOOL);
36
 
37
if ($returnurl) {
38
    $returnurl = new moodle_url($returnurl);
39
}
40
 
41
\core_question\local\bank\helper::require_plugin_enabled('qbank_deletequestion');
42
 
1441 ariadna 43
[$module, $cm] = get_module_from_cmid($cmid);
44
require_login($cm->course, false, $cm);
45
$thiscontext = context_module::instance($cmid);
1 efrain 46
 
47
$contexts = new core_question\local\bank\question_edit_contexts($thiscontext);
48
$url = new moodle_url('/question/bank/deletequestion/delete.php');
49
 
50
$PAGE->set_url($url);
51
$streditingquestions = get_string('deletequestion', 'qbank_deletequestion');
52
$PAGE->set_title($streditingquestions);
53
$PAGE->set_heading($COURSE->fullname);
54
$PAGE->activityheader->disable();
55
$PAGE->set_secondary_active_tab("questionbank");
56
 
57
// Unhide a question.
58
if (($unhide = optional_param('unhide', '', PARAM_INT)) and confirm_sesskey()) {
59
    question_require_capability_on($unhide, 'edit');
60
    $DB->set_field('question_versions', 'status',
61
        \core_question\local\bank\question_version_status::QUESTION_STATUS_READY, ['questionid' => $unhide]);
62
 
63
    // Purge these questions from the cache.
64
    \question_bank::notify_question_edited($unhide);
65
 
66
    redirect($returnurl);
67
}
68
 
69
// If user has already confirmed the action.
70
if ($deleteselected && ($confirm = optional_param('confirm', '', PARAM_ALPHANUM))
71
        && confirm_sesskey()) {
72
    $deleteselected = required_param('deleteselected', PARAM_RAW);
73
    if ($confirm == md5($deleteselected)) {
74
        if ($questionlist = explode(',', $deleteselected)) {
75
            \qbank_deletequestion\helper::delete_questions($questionlist, $deleteall);
76
        }
77
        redirect($returnurl);
78
    } else {
79
        throw new \moodle_exception('invalidconfirm', 'question');
80
    }
81
}
82
 
83
echo $OUTPUT->header();
84
 
85
if ($deleteselected) {
86
    // Make a list of all the questions that are selected.
87
    $rawquestions = $_REQUEST; // This code is called by both POST forms and GET links, so cannot use data_submitted.
88
    $questionlist = '';  // Comma separated list of ids of questions to be deleted.
89
    foreach ($rawquestions as $key => $value) {    // Parse input for question ids.
90
        if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
91
            $key = $matches[1];
92
            $questionlist .= $key.',';
93
            question_require_capability_on((int)$key, 'edit');
94
        }
95
    }
96
    if (!$questionlist) { // No questions were selected.
97
        redirect($returnurl);
98
    }
99
    $questionlist = rtrim($questionlist, ',');
100
 
101
    $deleteurl = new \moodle_url(
102
        '/question/bank/deletequestion/delete.php',
103
        [
104
            'deleteselected' => $questionlist,
105
            'deleteall' => $deleteall,
106
            'confirm' => md5($questionlist),
107
            'sesskey' => sesskey(),
108
            'returnurl' => $returnurl->out_as_local_url(false),
109
            'cmid' => $cmid,
110
        ],
111
    );
112
    $continue = new \single_button($deleteurl, get_string('delete'), 'post');
113
 
114
    $questionids = explode(',', $questionlist);
115
    [$displayoptions, $message] = qbank_deletequestion\helper::get_delete_confirmation_message($questionids,
116
        $deleteall);
117
 
118
    echo $OUTPUT->confirm($message, $continue, $returnurl, $displayoptions);
119
}
120
 
121
echo $OUTPUT->footer();