MDL-71615 qbank_deletequestion: Add Delete question plugin to core

This implementation will introduce a qbank plugin
"deletequestion" which will implement the delete question
action in the question bank view by replacing the core
class. Having this plugin will give users the
flexibility of enabling or disabling this action.
This commit is contained in:
Safat Shahin 2021-06-03 16:17:37 +10:00 committed by Ilya Tregubov
parent aebcf8d909
commit 38ec802858
7 changed files with 395 additions and 1 deletions

View File

@ -1939,7 +1939,7 @@ class core_plugin_manager {
),
'qbank' => [
''
'deletequestion',
],
'qbehaviour' => array(

View File

@ -0,0 +1,113 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* action to delete (or hide) a question, or restore a previously hidden question.
*
* @package qbank_deletequestion
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace qbank_deletequestion;
defined('MOODLE_INTERNAL') || die();
use core_question\local\bank\menu_action_column_base;
/**
* action to delete (or hide) a question, or restore a previously hidden question.
*
* @package qbank_deletequestion
* @copyright 2009 Tim Hunt
* @author 2021 Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_action_column extends menu_action_column_base {
/**
* @var string $strdelete
*/
protected $strdelete;
/**
* @var string $strrestore
*/
protected $strrestore;
/**
* Contains the url of the delete question page.
* @var \moodle_url|string
*/
public $deletequestionurl;
/**
* Array of the return parameters.
* @var array $returnparams
*/
protected $returnparams;
public function init(): void {
parent::init();
$this->strdelete = get_string('delete');
$this->strrestore = get_string('restore');
$this->deletequestionurl = new \moodle_url('/question/bank/deletequestion/delete.php');
if (!empty($this->qbank->cm->id)) {
$this->returnparams['cmid'] = $this->qbank->cm->id;
}
if (!empty($this->qbank->course->id)) {
$this->returnparams['courseid'] = $this->qbank->course->id;
}
if (!empty($this->qbank->returnurl)) {
$this->returnparams['returnurl'] = $this->qbank->returnurl;
}
}
public function get_name(): string {
return 'deleteaction';
}
protected function get_url_icon_and_label(\stdClass $question): array {
if (!question_has_capability_on($question, 'edit')) {
return [null, null, null];
}
if ($question->hidden) {
$hiddenparams = array(
'unhide' => $question->id,
'sesskey' => sesskey());
$hiddenparams = array_merge($hiddenparams, $this->returnparams);
$url = new \moodle_url($this->deletequestionurl, $hiddenparams);
return [$url, 't/restore', $this->strrestore];
} else {
$deleteparams = array(
'deleteselected' => $question->id,
'q' . $question->id => 1,
'sesskey' => sesskey());
$deleteparams = array_merge($deleteparams, $this->returnparams);
$url = new \moodle_url($this->deletequestionurl, $deleteparams);
return [$url, 't/delete', $this->strdelete];
}
}
public function get_required_fields(): array {
$required = parent::get_required_fields();
$required[] = 'q.hidden';
return $required;
}
}

View File

@ -0,0 +1,48 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Plugin entrypoint for columns.
*
* @package qbank_deletequestion
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace qbank_deletequestion;
defined('MOODLE_INTERNAL') || die();
use core_question\local\bank\plugin_features_base;
/**
* Class columns is the entrypoint for the columns.
*
* @package qbank_deletequestion
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plugin_feature extends plugin_features_base {
public function get_question_columns($qbank): array {
return [
new delete_action_column($qbank)
];
}
}

View File

@ -0,0 +1,44 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Version information for qbank_deletequestion.
*
* @package qbank_deletequestion
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace qbank_deletequestion\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for qbank_deletequestion implementing null_provider.
*
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
public static function get_reason(): string {
return 'privacy:metadata';
}
}

View File

@ -0,0 +1,129 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Delete question page.
*
* This code is based on question/classes/bank/view.php
*
* @package qbank_deletequestion
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
require_once(__DIR__ . '/../../editlib.php');
global $DB, $OUTPUT, $PAGE, $COURSE;
$deleteselected = optional_param('deleteselected', false, PARAM_BOOL);
$returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
$cmid = optional_param('cmid', 0, PARAM_INT);
$courseid = optional_param('courseid', 0, PARAM_INT);
\core_question\local\bank\helper::require_plugin_enabled('qbank_deletequestion');
if ($cmid) {
list($module, $cm) = get_module_from_cmid($cmid);
require_login($cm->course, false, $cm);
$thiscontext = context_module::instance($cmid);
} else if ($courseid) {
require_login($courseid, false);
$thiscontext = context_course::instance($courseid);
} else {
throw new moodle_exception('missingcourseorcmid', 'question');
}
$contexts = new question_edit_contexts($thiscontext);
$url = new moodle_url('/question/bank/deletequestion/delete.php');
$PAGE->set_url($url);
$streditingquestions = get_string('deletequestion', 'qbank_deletequestion');
$PAGE->set_title($streditingquestions);
$PAGE->set_heading($COURSE->fullname);
// Unhide a question.
if (($unhide = optional_param('unhide', '', PARAM_INT)) and confirm_sesskey()) {
question_require_capability_on($unhide, 'edit');
$DB->set_field('question', 'hidden', 0, array('id' => $unhide));
// Purge these questions from the cache.
\question_bank::notify_question_edited($unhide);
redirect($returnurl);
}
// If user has already confirmed the action.
if ($deleteselected && ($confirm = optional_param('confirm', '', PARAM_ALPHANUM))
&& confirm_sesskey()) {
$deleteselected = required_param('deleteselected', PARAM_RAW);
if ($confirm == md5($deleteselected)) {
if ($questionlist = explode(',', $deleteselected)) {
// For each question either hide it if it is in use or delete it.
foreach ($questionlist as $questionid) {
$questionid = (int)$questionid;
question_require_capability_on($questionid, 'edit');
if (questions_in_use(array($questionid))) {
$DB->set_field('question', 'hidden', 1, array('id' => $questionid));
} else {
question_delete_question($questionid);
}
}
}
redirect($returnurl);
} else {
throw new \moodle_exception('invalidconfirm', 'question');
}
}
echo $OUTPUT->header();
if ($deleteselected) {
// Make a list of all the questions that are selected.
$rawquestions = $_REQUEST; // This code is called by both POST forms and GET links, so cannot use data_submitted.
$questionlist = ''; // Comma separated list of ids of questions to be deleted.
$questionnames = ''; // String with names of questions separated by <br/> with an asterix in front of those that are in use.
$inuse = false; // Set to true if at least one of the questions is in use.
foreach ($rawquestions as $key => $value) { // Parse input for question ids.
if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
$key = $matches[1];
$questionlist .= $key.',';
question_require_capability_on((int)$key, 'edit');
if (questions_in_use(array($key))) {
$questionnames .= '* ';
$inuse = true;
}
$questionnames .= $DB->get_field('question', 'name', array('id' => $key)) . '<br />';
}
}
if (!$questionlist) { // No questions were selected.
redirect($returnurl);
}
$questionlist = rtrim($questionlist, ',');
// Add an explanation about questions in use.
if ($inuse) {
$questionnames .= '<br />'.get_string('questionsinuse', 'question');
}
$deleteurl = new \moodle_url('/question/bank/deletequestion/delete.php',
array('deleteselected' => $questionlist, 'confirm' => md5($questionlist),
'sesskey' => sesskey(), 'returnurl' => $returnurl, 'cmid' => $cmid, 'courseid' => $courseid));
$continue = new \single_button($deleteurl, get_string('delete'), 'post');
echo $OUTPUT->confirm(get_string('deletequestionscheck', 'question', $questionnames), $continue, $returnurl);
}
echo $OUTPUT->footer();

View File

@ -0,0 +1,28 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Strings for component qbank_deletequestion, language 'en'
*
* @package qbank_deletequestion
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['pluginname'] = 'Delete question bank feature';
$string['privacy:metadata'] = 'Delete question plugin will questions, it does not store any user data.';
$string['deletequestion'] = 'Delete selected question';

View File

@ -0,0 +1,32 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Version information for qbank_deletequestion.
*
* @package qbank_deletequestion
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'qbank_deletequestion';
$plugin->version = 2021070700;
$plugin->requires = 2021052500;
$plugin->maturity = MATURITY_STABLE;