Merge branch 'MDL-81084-main' of https://github.com/andrewnicols/moodle
[moodle.git] / question / lib.php
blob758c642d979e3f0ca016a25b73f1fda1d0849663
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.
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/>.
17 /**
18 * Question related functions.
20 * This file was created just because Fragment API expects callbacks to be defined on lib.php
22 * Please, do not add new functions to this file.
24 * @package core_question
25 * @copyright 2018 Simey Lameze <simey@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
31 require_once($CFG->dirroot . '/question/editlib.php');
33 /**
34 * Question data fragment to get the question html via ajax call.
36 * @param array $args Arguments for rendering the fragment. Expected keys:
37 * * view - the view class
38 * * cmid - if in an activity, the course module ID.
39 * * filterquery - the current filters encoded as a URL parameter.
40 * * lastchanged - the ID of the last edited question.
41 * * sortdata - Array of sorted columns.
42 * * filtercondition - the current filters encoded as an object.
43 * * extraparams - additional parameters required for a particular view class.
45 * @return array|string
47 function core_question_output_fragment_question_data(array $args): string {
48 if (empty($args)) {
49 return '';
51 [$params, $extraparams] = \core_question\local\bank\filter_condition_manager::extract_parameters_from_fragment_args($args);
53 $thispageurl,
54 $contexts,
56 $cm,
58 $pagevars
59 ] = question_build_edit_resources('questions', '/question/edit.php', $params);
61 if (is_null($cm)) {
62 $course = get_course(clean_param($args['courseid'], PARAM_INT));
63 } else {
64 $course = get_course($cm->course);
67 $viewclass = empty($args['view']) ? \core_question\local\bank\view::class : clean_param($args['view'], PARAM_NOTAGS);
69 if (!empty($args['lastchanged'])) {
70 $thispageurl->param('lastchanged', clean_param($args['lastchanged'], PARAM_INT));
72 // This is highly suspicious, but it is the same approach taken in /question/edit.php. See MDL-79281.
73 $thispageurl->param('deleteall', 1);
74 $questionbank = new $viewclass($contexts, $thispageurl, $course, $cm, $pagevars, $extraparams);
75 $questionbank->add_standard_search_conditions();
76 ob_start();
77 $questionbank->display_question_list();
78 return ob_get_clean();