Commit 47478d6b authored by Safat Shahin's avatar Safat Shahin
Browse files

MDL-71574 qbank_editquestion: Add Edit question plugin to core

This implementation will introduce a qbank plugin "editquestion"
which will implement the edit question, add question and copy
question actions in the question bank view by replacing the
core classes. Having this plugin will give users the
flexibility of enabling or disabling these actions.
parent 1464843a
......@@ -1939,7 +1939,7 @@ class core_plugin_manager {
),
'qbank' => [
'deletequestion',
'deletequestion', 'editquestion'
],
'qbehaviour' => array(
......
<?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/>.
/**
* Shows a screen where the user can choose a question type, before being redirected to question.php
*
* @package qbank_editquestion
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
require_once(__DIR__ . '/../../editlib.php');
use qbank_editquestion\editquestion_helper;
// Read URL parameters.
$categoryid = required_param('category', PARAM_INT);
$cmid = optional_param('cmid', 0, PARAM_INT);
$courseid = optional_param('courseid', 0, PARAM_INT);
$returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
$appendqnumstring = optional_param('appendqnumstring', '', PARAM_ALPHA);
$validationerror = optional_param('validationerror', false, PARAM_BOOL);
\core_question\local\bank\helper::require_plugin_enabled('qbank_editquestion');
// Place to accumulate hidden params for the form we will print.
$hiddenparams = array('category' => $categoryid);
// Validate params.
if (!$category = $DB->get_record('question_categories', array('id' => $categoryid))) {
throw new moodle_exception('categorydoesnotexist', 'question', $returnurl);
}
if ($cmid) {
list($module, $cm) = get_module_from_cmid($cmid);
require_login($cm->course, false, $cm);
$thiscontext = context_module::instance($cmid);
$hiddenparams['cmid'] = $cmid;
} else if ($courseid) {
require_login($courseid, false);
$thiscontext = context_course::instance($courseid);
$module = null;
$cm = null;
$hiddenparams['courseid'] = $courseid;
} else {
throw new moodle_exception('missingcourseorcmid', 'question');
}
// Check permissions.
$categorycontext = context::instance_by_id($category->contextid);
require_capability('moodle/question:add', $categorycontext);
// Ensure other optional params get passed on to question.php.
if (!empty($returnurl)) {
$hiddenparams['returnurl'] = $returnurl;
}
if (!empty($appendqnumstring)) {
$hiddenparams['appendqnumstring'] = $appendqnumstring;
}
$PAGE->set_url('/question/bank/editquestion/addquestion.php', $hiddenparams);
if ($cmid) {
$questionbankurl = new moodle_url('/question/edit.php', array('cmid' => $cmid));
} else {
$questionbankurl = new moodle_url('/question/edit.php', array('courseid' => $courseid));
}
navigation_node::override_active_url($questionbankurl);
$chooseqtype = get_string('chooseqtypetoadd', 'question');
$PAGE->set_heading($COURSE->fullname);
$PAGE->navbar->add($chooseqtype);
$PAGE->set_title($chooseqtype);
// Display a form to choose the question type.
echo $OUTPUT->header();
echo $OUTPUT->notification(get_string('youmustselectaqtype', 'question'));
echo $OUTPUT->box_start('generalbox boxwidthnormal boxaligncenter', 'chooseqtypebox');
echo editquestion_helper::print_choose_qtype_to_add_form($hiddenparams, null, false);
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
<?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/>.
/**
* Question bank column for the duplicate action icon.
*
* @package qbank_editquestion
* @copyright 2013 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace qbank_editquestion;
use core_question\local\bank\menu_action_column_base;
use moodle_url;
/**
* Question bank column for the duplicate action icon.
*
* @copyright 2013 The Open University
* @author 2021 Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class copy_action_column extends menu_action_column_base {
/** @var string avoids repeated calls to get_string('duplicate'). */
protected $strcopy;
/**
* Contains the url of the edit question page.
* @var moodle_url|string
*/
public $duplicatequestionurl;
public function init(): void {
parent::init();
$this->strcopy = get_string('duplicate');
$this->duplicatequestionurl = new \moodle_url('/question/bank/editquestion/question.php',
array('returnurl' => $this->qbank->returnurl));
if ($this->qbank->cm !== null) {
$this->duplicatequestionurl->param('cmid', $this->qbank->cm->id);
} else {
$this->duplicatequestionurl->param('courseid', $this->qbank->course->id);
}
}
public function get_name() {
return 'copyaction';
}
/**
* Get the URL for duplicating a question as a moodle_url.
*
* @param int $questionid the question id.
* @return \moodle_url the URL.
*/
public function duplicate_question_moodle_url($questionid): moodle_url {
return new \moodle_url($this->duplicatequestionurl, ['id' => $questionid, 'makecopy' => 1]);
}
protected function get_url_icon_and_label(\stdClass $question): array {
if (!\question_bank::is_qtype_installed($question->qtype)) {
// It sometimes happens that people end up with junk questions
// in their question bank of a type that is no longer installed.
// We cannot do most actions on them, because that leads to errors.
return [null, null, null];
}
// To copy a question, you need permission to add a question in the same
// category as the existing question, and ability to access the details of
// the question being copied.
if (question_has_capability_on($question, 'add') &&
(question_has_capability_on($question, 'edit') || question_has_capability_on($question, 'view'))) {
return [$this->duplicate_question_moodle_url($question->id), 't/copy', $this->strcopy];
}
return [null, null, null];
}
}
<?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/>.
/**
* Class for question bank edit question column.
*
* @package qbank_editquestion
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace qbank_editquestion;
use core_question\local\bank\menu_action_column_base;
use moodle_url;
/**
* Class for question bank edit question column.
*
* @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 edit_action_column extends menu_action_column_base {
/**
* Contains the string.
* @var string
*/
protected $stredit;
/**
* Contains the string.
* @var string
*/
protected $strview;
/**
* Contains the url of the edit question page.
* @var moodle_url|string
*/
public $editquestionurl;
public function init(): void {
parent::init();
$this->stredit = get_string('editquestion', 'question');
$this->strview = get_string('view');
$this->editquestionurl = new \moodle_url('/question/bank/editquestion/question.php',
array('returnurl' => $this->qbank->returnurl));
if ($this->qbank->cm !== null) {
$this->editquestionurl->param('cmid', $this->qbank->cm->id);
} else {
$this->editquestionurl->param('courseid', $this->qbank->course->id);
}
}
public function get_name() {
return 'editaction';
}
/**
* Get the URL for editing a question as a link.
*
* @param int $questionid the question id.
* @return moodle_url the URL, HTML-escaped.
*/
public function edit_question_moodle_url($questionid): moodle_url {
return new moodle_url($this->editquestionurl, ['id' => $questionid]);
}
protected function get_url_icon_and_label(\stdClass $question): array {
if (!\question_bank::is_qtype_installed($question->qtype)) {
// It sometimes happens that people end up with junk questions
// in their question bank of a type that is no longer installed.
// We cannot do most actions on them, because that leads to errors.
return [null, null, null];
}
if (question_has_capability_on($question, 'edit')) {
return [$this->edit_question_moodle_url($question->id), 't/edit', $this->stredit];
} else if (question_has_capability_on($question, 'view')) {
return [$this->edit_question_moodle_url($question->id), 'i/info', $this->strview];
} else {
return [null, null, null];
}
}
}
<?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/>.
/**
* Helper class for adding/editing a question.
*
* This code is based on question/editlib.php by Martin Dougiamas.
*
* @package qbank_editquestion
* @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_editquestion;
/**
* Class editquestion_helper for methods related to add/edit/copy
*
* @package qbank_editquestion
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
*/
class editquestion_helper {
/**
* Print a form to let the user choose which question type to add.
* When the form is submitted, it goes to the question.php script.
*
* @param array|null $hiddenparams hidden parameters to add to the form, in addition to
* the qtype radio buttons.
* @param array|null $allowedqtypes optional list of qtypes that are allowed. If given, only
* those qtypes will be shown. Example value array('description', 'multichoice').
* @param bool $enablejs
* @return bool|string
*/
public static function print_choose_qtype_to_add_form(array $hiddenparams, array $allowedqtypes = null, $enablejs = true) {
global $PAGE;
$chooser = \qbank_editquestion\qbank_chooser::get($PAGE->course, $hiddenparams, $allowedqtypes);
$renderer = $PAGE->get_renderer('qbank_editquestion');
return $renderer->render($chooser);
}
/**
* Print a button for creating a new question. This will open question/addquestion.php,
* which in turn goes to question/question.php before getting back to $params['returnurl']
* (by default the question bank screen).
*
* @param int $categoryid The id of the category that the new question should be added to.
* @param array $params Other paramters to add to the URL. You need either $params['cmid'] or
* $params['courseid'], and you should probably set $params['returnurl']
* @param bool $canadd the text to display on the button.
* @param string $tooltip a tooltip to add to the button (optional).
* @param bool $disabled if true, the button will be disabled.
*/
public static function create_new_question_button($categoryid, $params, $canadd, $tooltip = '', $disabled = false) {
global $PAGE, $OUTPUT;
$addquestiondisplay = array();
$addquestiondisplay['canadd'] = $canadd;
if ($canadd) {
$params['category'] = $categoryid;
$url = new \moodle_url('/question/bank/editquestion/addquestion.php', $params);
$addquestiondisplay['buttonhtml'] = $OUTPUT->single_button($url,
get_string('createnewquestion', 'question'),
'get', array('disabled' => $disabled, 'title' => $tooltip));
$addquestiondisplay['qtypeform'] = self::print_choose_qtype_to_add_form(array());
}
return $PAGE->get_renderer('qbank_editquestion')->render_create_new_question_button($addquestiondisplay);
}
}
<?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/>.
/**
* Renderer for adding/editing a question.
*
* This code is based on question/renderer.php by The Open University.
*
* @package qbank_editquestion
* @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_editquestion\output;
/**
* Renderer for add/edit/copy
*
* @package qbank_editquestion
* @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 renderer extends \plugin_renderer_base {
/**
* Render a qbank_chooser.
*
* @param \renderable $qbankchooser The chooser.
* @return string
*/
public function render_qbank_chooser (\renderable $qbankchooser) {
return $this->render_from_template('qbank_editquestion/qbank_chooser', $qbankchooser->export_for_template($this));
}
/**
* Render add question button.
*
* @param array $addquestiondata
* @return bool|string
*/
public function render_create_new_question_button ($addquestiondata) {
return $this->render_from_template('qbank_editquestion/add_new_question', $addquestiondata);
}
}
<?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_editquestion
* @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_editquestion;
/**
* Class columns is the entrypoint for the columns.
*
* @package qbank_editquestion
* @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 \core_question\local\bank\plugin_features_base{
public function get_question_columns($qbank): array {
return [
new edit_action_column($qbank),
new copy_action_column($qbank)
];
}
}
<?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_editquestion.
*
* @package qbank_editquestion
* @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_editquestion\privacy;
/**
* Privacy Subsystem for qbank_editquestion 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';
}
}
<?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/>.
/**
* The qbank_chooser renderable.
*
* @package qbank_editquestion
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace qbank_editquestion;
use context;
use context_course;
use core\output\chooser_section;
use lang_string;
use moodle_url;
use question_bank;
use stdClass;
/**
* The qbank_chooser renderable class.
*
* @package qbank_editquestion
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qbank_chooser extends \core\output\chooser {
/**
* Constructor.
*
* @param array $real The real question types.
* @param array $fake The fake question types.
* @param stdClass $course The course.
* @param array $hiddenparams Hidden parameters.
* @param context $context The relevant context.
*/
public function __construct($real, $fake, $course, $hiddenparams, $context) {
$sections = [];
$sections[] = new chooser_section('questions', new lang_string('questions', 'question'),
array_map(function($qtype) use ($context) {
return new qbank_chooser_item($qtype, $context);
}, $real));
if (!empty($fake)) {
$sections[] = new chooser_section('other', new lang_string('other'),
array_map(function ($qtype) use ($context) {
return new qbank_chooser_item($qtype, $context);
}, $fake));
}
parent::__construct(new moodle_url('/question/bank/editquestion/question.php'),
new lang_string('chooseqtypetoadd', 'question'), $sections, 'qtype');
$this->set_instructions(