<?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/>.
/**
* Defines the custom question bank view used on the Edit quiz page.
*
* @package mod_quiz
* @category question
* @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_quiz\question\bank;
< use coding_exception;
> defined('MOODLE_INTERNAL') || die();
> use core\output\datafilter;
/**
> use core_question\local\bank\column_base;
* Subclass to customise the view of the question bank for the quiz editing screen.
> use core_question\local\bank\condition;
*
> use core_question\local\bank\column_manager_base;
* @copyright 2009 Tim Hunt
> use core_question\local\bank\question_version_status;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
> use mod_quiz\question\bank\filter\custom_category_condition;
*/
> use qbank_managecategories\category_condition;
class custom_view extends \core_question\bank\view {
>
/** @var bool whether the quiz this is used by has been attemptd. */
> require_once($CFG->dirroot . '/mod/quiz/locallib.php');
protected $quizhasattempts = false;
> * @author 2021 Safat Shahin <safatshahin@catalyst-au.net>
< class custom_view extends \core_question\bank\view {
< /** @var bool whether the quiz this is used by has been attemptd. */
> class custom_view extends \core_question\local\bank\view {
> /** @var int number of questions per page to show in the add from question bank modal. */
> const DEFAULT_PAGE_SIZE = 20;
>
> /** @var bool $quizhasattempts whether the quiz this is used by has been attemptd. */
< /** @var \stdClass the quiz settings. */
>
> /** @var \stdClass $quiz the quiz settings. */
< /** @var int The maximum displayed length of the category info. */
< const MAX_TEXT_LENGTH = 200;
/**
< * Constructor
< * @param \question_edit_contexts $contexts
> * @var string $component the component the api is used from.
> */
> public $component = 'mod_quiz';
>
> /**
> * Constructor.
> * @param \core_question\local\bank\question_edit_contexts $contexts
* @param \moodle_url $pageurl
* @param \stdClass $course course settings
* @param \stdClass $cm activity settings.
* @param \stdClass $quiz quiz settings.
*/
< public function __construct($contexts, $pageurl, $course, $cm, $quiz) {
< parent::__construct($contexts, $pageurl, $course, $cm);
< $this->quiz = $quiz;
> public function __construct($contexts, $pageurl, $course, $cm, $params, $extraparams) {
> // Default filter condition.
> if (!isset($params['filter'])) {
> $params['filter'] = [];
> [$categoryid, $contextid] = custom_category_condition::validate_category_param($params['cat']);
> if (!is_null($categoryid)) {
> $category = custom_category_condition::get_category_record($categoryid, $contextid);
> $params['filter']['category'] = [
> 'jointype' => custom_category_condition::JOINTYPE_DEFAULT,
> 'values' => [$category->id],
> 'filteroptions' => ['includesubcategories' => false],
> ];
> }
}
< protected function wanted_columns() {
< global $CFG;
> $this->init_columns($this->wanted_columns(), $this->heading_column());
> parent::__construct($contexts, $pageurl, $course, $cm, $params, $extraparams);
> [$this->quiz, ] = get_module_from_cmid($cm->id);
> $this->set_quiz_has_attempts(quiz_has_attempts($this->quiz->id));
> $this->pagesize = self::DEFAULT_PAGE_SIZE;
> }
< if (empty($CFG->quizquestionbankcolumns)) {
< $quizquestionbankcolumns = array(
< 'add_action_column',
< 'checkbox_column',
< 'question_type_column',
< 'question_name_text_column',
< 'preview_action_column',
< );
< } else {
< $quizquestionbankcolumns = explode(',', $CFG->quizquestionbankcolumns);
> /**
> * Just use the base column manager in this view.
> *
> * @return void
> */
> protected function init_column_manager(): void {
> $this->columnmanager = new column_manager_base();
}
< foreach ($quizquestionbankcolumns as $fullname) {
< if (!class_exists($fullname)) {
< if (class_exists('mod_quiz\\question\\bank\\' . $fullname)) {
< $fullname = 'mod_quiz\\question\\bank\\' . $fullname;
< } else if (class_exists('core_question\\bank\\' . $fullname)) {
< $fullname = 'core_question\\bank\\' . $fullname;
< } else if (class_exists('question_bank_' . $fullname)) {
< debugging('Legacy question bank column class question_bank_' .
< $fullname . ' should be renamed to mod_quiz\\question\\bank\\' .
< $fullname, DEBUG_DEVELOPER);
< $fullname = 'question_bank_' . $fullname;
< } else {
< throw new coding_exception('Invalid quiz question bank column', $fullname);
> /**
> * Don't display plugin controls.
> *
> * @param \core\context $context
> * @param int $categoryid
> * @return string
> */
> protected function get_plugin_controls(\core\context $context, int $categoryid): string {
> return '';
}
>
}
> protected function get_question_bank_plugins(): array {
$this->requiredcolumns[$fullname] = new $fullname($this);
> $questionbankclasscolumns = [];
}
> $customviewcolumns = [
return $this->requiredcolumns;
> 'mod_quiz\question\bank\add_action_column' . column_base::ID_SEPARATOR . 'add_action_column',
}
> 'core_question\local\bank\checkbox_column' . column_base::ID_SEPARATOR . 'checkbox_column',
> 'qbank_viewquestiontype\question_type_column' . column_base::ID_SEPARATOR . 'question_type_column',
/**
> 'mod_quiz\question\bank\question_name_text_column' . column_base::ID_SEPARATOR . 'question_name_text_column',
* Specify the column heading
> 'mod_quiz\question\bank\preview_action_column' . column_base::ID_SEPARATOR . 'preview_action_column',
*
> ];
* @return string Column name for the heading
>
*/
> foreach ($customviewcolumns as $columnid) {
protected function heading_column() {
> [$columnclass, $columnname] = explode(column_base::ID_SEPARATOR, $columnid, 2);
return 'mod_quiz\\question\\bank\\question_name_text_column';
> if (class_exists($columnclass)) {
}
> $questionbankclasscolumns[$columnid] = $columnclass::from_column_name($this, $columnname);
< $this->requiredcolumns[$fullname] = new $fullname($this);
< return $this->requiredcolumns;
>
> return $questionbankclasscolumns;
< /**
< * Specify the column heading
< *
< * @return string Column name for the heading
< */
< protected function heading_column() {
> protected function heading_column(): string {
< protected function default_sort() {
< return array(
< 'core_question\\bank\\question_type_column' => 1,
< 'mod_quiz\\question\\bank\\question_name_text_column' => 1,
< );
> protected function default_sort(): array {
> // Using the extended class for quiz specific sort.
> return [
> 'qbank_viewquestiontype__question_type_column' => SORT_ASC,
> 'mod_quiz__question__bank__question_name_text_column' => SORT_ASC,
> ];
*/
> *
< public function set_quiz_has_attempts($quizhasattempts) {
> private function set_quiz_has_attempts($quizhasattempts): void {
$this->quizhasattempts = $quizhasattempts;
if ($quizhasattempts && isset($this->visiblecolumns['addtoquizaction'])) {
unset($this->visiblecolumns['addtoquizaction']);
}
}
< public function preview_question_url($question) {
< return quiz_question_preview_url($this->quiz, $question);
< }
<
> /**
> * URL of add to quiz.
> *
> * @param $questionid
> * @return \moodle_url
> */
public function add_to_quiz_url($questionid) {
< global $CFG;
$params = $this->baseurl->params();
$params['addquestion'] = $questionid;
$params['sesskey'] = sesskey();
> $params['cmid'] = $this->cm->id;
return new \moodle_url('/mod/quiz/edit.php', $params);
}
/**
* Renders the html question bank (same as display, but returns the result).
*
* Note that you can only output this rendered result once per page, as
* it contains IDs which must be unique.
*
> * @param array $pagevars
* @return string HTML code for the form
> * @param string $tabname
*/
< public function render($tabname, $page, $perpage, $cat, $recurse, $showhidden,
< $showquestiontext, $tagids = []) {
> public function render($pagevars, $tabname): string {
ob_start();
< $this->display($tabname, $page, $perpage, $cat, $recurse, $showhidden, $showquestiontext, $tagids);
> $this->display();
$out = ob_get_contents();
ob_end_clean();
return $out;
}
< /**
< * Display the controls at the bottom of the list of questions.
< * @param int $totalnumber Total number of questions that might be shown (if it was not for paging).
< * @param bool $recurse Whether to include subcategories.
< * @param \stdClass $category The question_category row from the database.
< * @param \context $catcontext The context of the category being displayed.
< * @param array $addcontexts contexts where the user is allowed to add new questions.
< */
< protected function display_bottom_controls($totalnumber, $recurse, $category, \context $catcontext, array $addcontexts) {
> protected function display_bottom_controls(\context $catcontext): void {
$cmoptions = new \stdClass();
$cmoptions->hasattempts = !empty($this->quizhasattempts);
$canuseall = has_capability('moodle/question:useall', $catcontext);
< echo '<div class="modulespecificbuttonscontainer">';
> echo \html_writer::start_tag('div', ['class' => 'pt-2']);
if ($canuseall) {
<
// Add selected questions to the quiz.
< $params = array(
> $params = [
'type' => 'submit',
'name' => 'add',
'class' => 'btn btn-primary',
'value' => get_string('addselectedquestionstoquiz', 'quiz'),
'data-action' => 'toggle',
'data-togglegroup' => 'qbank',
'data-toggle' => 'action',
'disabled' => true,
< );
> ];
echo \html_writer::empty_tag('input', $params);
}
< echo "</div>\n";
> echo \html_writer::end_tag('div');
}
/**
< * Prints a form to choose categories.
< * @param string $categoryandcontext 'categoryID,contextID'.
< * @deprecated since Moodle 2.6 MDL-40313.
< * @see \core_question\bank\search\category_condition
< * @todo MDL-41978 This will be deleted in Moodle 2.8
< */
< protected function print_choose_category_message($categoryandcontext) {
< global $OUTPUT;
< debugging('print_choose_category_message() is deprecated, ' .
< 'please use \core_question\bank\search\category_condition instead.', DEBUG_DEVELOPER);
< echo $OUTPUT->box_start('generalbox questionbank');
< $this->display_category_form($this->contexts->having_one_edit_tab_cap('edit'),
< $this->baseurl, $categoryandcontext);
< echo "<p style=\"text-align:center;\"><b>";
< print_string('selectcategoryabove', 'question');
< echo "</b></p>";
< echo $OUTPUT->box_end();
< }
<
< protected function display_options_form($showquestiontext, $scriptpath = '/mod/quiz/edit.php',
< $showtextoption = false) {
< // Overridden just to change the default values of the arguments.
< parent::display_options_form($showquestiontext, $scriptpath, $showtextoption);
< }
<
< protected function print_category_info($category) {
< $formatoptions = new \stdClass();
< $formatoptions->noclean = true;
< $strcategory = get_string('category', 'quiz');
< echo '<div class="categoryinfo"><div class="categorynamefieldcontainer">' .
< $strcategory;
< echo ': <span class="categorynamefield">';
< echo shorten_text(strip_tags(format_string($category->name)), 60);
< echo '</span></div><div class="categoryinfofieldcontainer">' .
< '<span class="categoryinfofield">';
< echo shorten_text(strip_tags(format_text($category->info, $category->infoformat,
< $formatoptions, $this->course->id)), 200);
< echo '</span></div></div>';
< }
<
< protected function display_options($recurse, $showhidden, $showquestiontext) {
< debugging('display_options() is deprecated, see display_options_form() instead.', DEBUG_DEVELOPER);
< echo '<form method="get" action="edit.php" id="displayoptions">';
< echo "<fieldset class='invisiblefieldset'>";
< echo \html_writer::input_hidden_params($this->baseurl,
< array('recurse', 'showhidden', 'qbshowtext'));
< $this->display_category_form_checkbox('recurse', $recurse,
< get_string('includesubcategories', 'question'));
< $this->display_category_form_checkbox('showhidden', $showhidden,
< get_string('showhidden', 'question'));
< echo '<noscript><div class="centerpara"><input type="submit" value="' .
< get_string('go') . '" />';
< echo '</div></noscript></fieldset></form>';
< }
<
< protected function create_new_question_form($category, $canadd) {
< // Don't display this.
> * Override the base implementation in \core_question\local\bank\view
> * because we don't want to print new question form in the fragment
> * for the modal.
> *
> * @param false|mixed|\stdClass $category
> * @param bool $canadd
> */
> protected function create_new_question_form($category, $canadd): void {
}
/**
< * Override the base implementation in \core_question\bank\view
> * Override the base implementation in \core_question\local\bank\view
* because we don't want to print the headers in the fragment
* for the modal.
*/
< protected function display_question_bank_header() {
> protected function display_question_bank_header(): void {
}
< /**
< * Override the base implementation in \core_question\bank\view
< * because we don't want it to read from the $_POST global variables
< * for the sort parameters since they are not present in a fragment.
< *
< * Unfortunately the best we can do is to look at the URL for
< * those parameters (only marginally better really).
< */
< protected function init_sort_from_params() {
< $this->sort = [];
< for ($i = 1; $i <= self::MAX_SORTS; $i++) {
< if (!$sort = $this->baseurl->param('qbs' . $i)) {
< break;
> protected function build_query(): void {
> // Get the required tables and fields.
> [$fields, $joins] = $this->get_component_requirements(array_merge($this->requiredcolumns, $this->questionactions));
>
> // Build the order by clause.
> $sorts = [];
> foreach ($this->sort as $sortname => $sortorder) {
> [$colname, $subsort] = $this->parse_subsort($sortname);
> $sorts[] = $this->requiredcolumns[$colname]->sort_expression($sortorder == SORT_DESC, $subsort);
> }
>
> // Build the where clause.
> $latestversion = 'qv.version = (SELECT MAX(v.version)
> FROM {question_versions} v
> JOIN {question_bank_entries} be
> ON be.id = v.questionbankentryid
> WHERE be.id = qbe.id)';
> $onlyready = '((' . "qv.status = '" . question_version_status::QUESTION_STATUS_READY . "'" .'))';
> $this->sqlparams = [];
> $conditions = [];
> foreach ($this->searchconditions as $searchcondition) {
> if ($searchcondition->where()) {
> $conditions[] = '((' . $searchcondition->where() .'))';
> }
> if ($searchcondition->params()) {
> $this->sqlparams = array_merge($this->sqlparams, $searchcondition->params());
> }
> }
> $majorconditions = ['q.parent = 0', $latestversion, $onlyready];
> // Get higher level filter condition.
> $jointype = isset($this->pagevars['jointype']) ? (int)$this->pagevars['jointype'] : condition::JOINTYPE_DEFAULT;
> $nonecondition = ($jointype === datafilter::JOINTYPE_NONE) ? ' NOT ' : '';
> $separator = ($jointype === datafilter::JOINTYPE_ALL) ? ' AND ' : ' OR ';
> // Build the SQL.
> $sql = ' FROM {question} q ' . implode(' ', $joins);
> $sql .= ' WHERE ' . implode(' AND ', $majorconditions);
> if (!empty($conditions)) {
> $sql .= ' AND ' . $nonecondition . ' ( ';
> $sql .= implode($separator, $conditions);
> $sql .= ' ) ';
> }
> $this->countsql = 'SELECT count(1)' . $sql;
> $this->loadsql = 'SELECT ' . implode(', ', $fields) . $sql . ' ORDER BY ' . implode(', ', $sorts);
> }
>
> public function add_standard_search_conditions(): void {
> foreach ($this->plugins as $componentname => $plugin) {
> if (\core\plugininfo\qbank::is_plugin_enabled($componentname)) {
> $pluginentrypointobject = new $plugin();
> if ($componentname === 'qbank_managecategories') {
> $pluginentrypointobject = new quiz_managecategories_feature();
> }
> if ($componentname === 'qbank_viewquestiontext' || $componentname === 'qbank_deletequestion') {
> continue;
> }
> $pluginobjects = $pluginentrypointobject->get_question_filters($this);
> foreach ($pluginobjects as $pluginobject) {
> $this->add_searchcondition($pluginobject, $pluginobject->get_condition_key());
}
< // Work out the appropriate order.
< $order = 1;
< if ($sort[0] == '-') {
< $order = -1;
< $sort = substr($sort, 1);
< if (!$sort) {
< break;
}
}
< // Deal with subsorts.
< list($colname, $subsort) = $this->parse_subsort($sort);
< $this->requiredcolumns[$colname] = $this->get_column_type($colname);
< $this->sort[$sort] = $order;
}
>
}
> /**
}
> * Return the quiz settings for the quiz this question bank is displayed in.
> *
> * @return bool|\stdClass
> */
> public function get_quiz() {
> return $this->quiz;