Commit f33dc783 authored by Ryan Wyllie's avatar Ryan Wyllie
Browse files

MDL-61135 mod_quiz: add question bank fragment

parent 031a2752
<?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 in the question bank modal.
*
* @package mod_quiz
* @category question
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_quiz\question\bank;
defined('MOODLE_INTERNAL') || die();
/**
* Subclass to customise the view of the question bank for a fragment.
*
* This view is to be used when returning the question bank as part of
* a fragment.
*
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class fragment_view extends custom_view {
/**
* Override the base implementation in \core_question\bank\view
* because we don't want to print the headers in the fragment
* for the modal.
*/
protected function display_question_bank_header() {
}
/**
* 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;
}
// 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;
}
}
}
......@@ -2402,3 +2402,44 @@ function mod_quiz_core_calendar_event_timestart_updated(\calendar_event $event,
$event->trigger();
}
}
/**
* Generates the question bank in a fargment output. This allows
* the question bank to be displayed in a modal.
*
* The only expected argument provided in the $args array is
* 'querystring'. The value should be the list of parameters
* URL encoded and used to build the question bank page.
*
* The individual list of parameters expected can be found in
* question_build_edit_resources.
*
* @param array $args The fragment arguments.
* @return string The rendered mform fragment.
*/
function mod_quiz_output_fragment_quiz_question_bank($args) {
global $CFG, $DB, $PAGE;
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
require_once($CFG->dirroot . '/question/editlib.php');
$querystring = preg_replace('/^\?/', '', $args['querystring']);
$params = [];
parse_str($querystring, $params);
// Build the required resources. The $params are all cleaned as
// part of this process.
list($thispageurl, $contexts, $cmid, $cm, $quiz, $pagevars) =
question_build_edit_resources('editq', '/mod/quiz/edit.php', $params);
// Get the course object and related bits.
$course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
require_capability('mod/quiz:manage', $contexts->lowest());
// Create quiz question bank view.
$questionbank = new mod_quiz\question\bank\fragment_view($contexts, $thispageurl, $course, $cm, $quiz);
$questionbank->set_quiz_has_attempts(quiz_has_attempts($quiz->id));
// Output.
$renderer = $PAGE->get_renderer('mod_quiz', 'edit');
return $renderer->question_bank_contents($questionbank, $pagevars);
}
......@@ -470,14 +470,14 @@ class view {
}
$editcontexts = $this->contexts->having_one_edit_tab_cap($tabname);
// Category selection form.
echo $OUTPUT->heading(get_string('questionbank', 'question'), 2);
$this->display_question_bank_header();
array_unshift($this->searchconditions, new \core_question\bank\search\hidden_condition(!$showhidden));
array_unshift($this->searchconditions, new \core_question\bank\search\category_condition(
$cat, $recurse, $editcontexts, $this->baseurl, $this->course));
$this->display_options_form($showquestiontext);
// Continues with list of questions.
$this->display_question_list($this->contexts->having_one_edit_tab_cap($tabname),
$this->display_question_list($editcontexts,
$this->baseurl, $cat, $this->cm,
null, $page, $perpage, $showhidden, $showquestiontext,
$this->contexts->having_cap('moodle/question:add'));
......@@ -634,6 +634,14 @@ class view {
echo "</div>\n";
}
/**
* Display the header element for the question bank.
*/
protected function display_question_bank_header() {
global $OUTPUT;
echo $OUTPUT->heading(get_string('questionbank', 'question'), 2);
}
protected function create_new_question_form($category, $canadd) {
global $CFG;
echo '<div class="createnewquestion">';
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment