2009-11-04 11:57:52 +00:00
|
|
|
<?php
|
2010-12-20 16:16:09 +00:00
|
|
|
// 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/>.
|
|
|
|
|
2006-02-24 10:21:40 +00:00
|
|
|
/**
|
2007-05-03 10:10:01 +00:00
|
|
|
* Functions used to show question editing interface
|
2007-03-19 17:22:46 +00:00
|
|
|
*
|
2011-02-23 16:25:25 +00:00
|
|
|
* @package moodlecore
|
2010-12-20 16:16:09 +00:00
|
|
|
* @subpackage questionbank
|
2011-02-23 16:25:25 +00:00
|
|
|
* @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2010-12-20 16:16:09 +00:00
|
|
|
*/
|
|
|
|
|
2006-02-24 10:21:40 +00:00
|
|
|
|
2014-01-09 12:43:56 +00:00
|
|
|
use core_question\bank\search\category_condition;
|
|
|
|
|
2011-02-23 15:03:35 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
2006-02-24 10:21:40 +00:00
|
|
|
|
2011-02-23 15:03:35 +00:00
|
|
|
require_once($CFG->libdir . '/questionlib.php');
|
2010-12-20 16:16:09 +00:00
|
|
|
|
2022-03-01 11:25:34 +00:00
|
|
|
define('DEFAULT_QUESTIONS_PER_PAGE', 100);
|
|
|
|
define('MAXIMUM_QUESTIONS_PER_PAGE', 4000);
|
2006-06-12 13:54:01 +00:00
|
|
|
|
2009-01-19 05:30:01 +00:00
|
|
|
function get_module_from_cmid($cmid) {
|
2008-06-06 14:43:15 +00:00
|
|
|
global $CFG, $DB;
|
|
|
|
if (!$cmrec = $DB->get_record_sql("SELECT cm.*, md.name as modname
|
|
|
|
FROM {course_modules} cm,
|
|
|
|
{modules} md
|
|
|
|
WHERE cm.id = ? AND
|
|
|
|
md.id = cm.module", array($cmid))){
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidcoursemodule');
|
2008-06-06 14:43:15 +00:00
|
|
|
} elseif (!$modrec =$DB->get_record($cmrec->modname, array('id' => $cmrec->instance))) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidcoursemodule');
|
2007-05-03 05:17:07 +00:00
|
|
|
}
|
|
|
|
$modrec->instance = $modrec->id;
|
2007-06-15 10:25:09 +00:00
|
|
|
$modrec->cmid = $cmrec->id;
|
2007-11-20 14:46:50 +00:00
|
|
|
$cmrec->name = $modrec->name;
|
2007-08-09 21:51:09 +00:00
|
|
|
|
2007-05-03 05:17:07 +00:00
|
|
|
return array($modrec, $cmrec);
|
|
|
|
}
|
2021-09-03 16:43:49 +10:00
|
|
|
|
2006-05-02 12:26:03 +00:00
|
|
|
/**
|
2021-08-03 16:25:06 +10:00
|
|
|
* Function to read all questions for category into big array
|
|
|
|
*
|
|
|
|
* @param object $category category number
|
|
|
|
* @param bool $noparent if true only questions with NO parent will be selected
|
|
|
|
* @param bool $recurse include subdirectories
|
|
|
|
* @param bool $export set true if this is called by questionbank export
|
2021-10-25 00:17:11 +11:00
|
|
|
* @param bool $latestversion if only the latest versions needed
|
2021-08-03 16:25:06 +10:00
|
|
|
* @return array
|
2021-09-03 16:43:49 +10:00
|
|
|
*/
|
2021-08-03 16:25:06 +10:00
|
|
|
function get_questions_category(object $category, bool $noparent, bool $recurse = true, bool $export = true,
|
2021-09-03 16:43:49 +10:00
|
|
|
bool $latestversion = false): array {
|
2011-02-10 20:44:47 +00:00
|
|
|
global $DB;
|
2006-05-02 12:26:03 +00:00
|
|
|
|
2021-08-03 16:25:06 +10:00
|
|
|
// Build sql bit for $noparent.
|
2006-05-02 12:26:03 +00:00
|
|
|
$npsql = '';
|
|
|
|
if ($noparent) {
|
2021-10-25 00:17:11 +11:00
|
|
|
$npsql = " and q.parent='0' ";
|
2006-05-02 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 16:25:06 +10:00
|
|
|
// Get list of categories.
|
2006-05-13 08:49:46 +00:00
|
|
|
if ($recurse) {
|
2010-11-19 12:13:59 +00:00
|
|
|
$categorylist = question_categorylist($category->id);
|
2011-02-24 17:47:51 +00:00
|
|
|
} else {
|
2021-08-03 16:25:06 +10:00
|
|
|
$categorylist = [$category->id];
|
2006-05-13 08:49:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 16:25:06 +10:00
|
|
|
// Get the list of questions for the category.
|
2011-02-24 17:47:51 +00:00
|
|
|
list($usql, $params) = $DB->get_in_or_equal($categorylist);
|
2006-05-02 12:26:03 +00:00
|
|
|
|
2021-08-03 16:25:06 +10:00
|
|
|
// Get the latest version of a question.
|
|
|
|
$version = '';
|
|
|
|
if ($latestversion) {
|
|
|
|
$version = 'AND (qv.version = (SELECT MAX(v.version)
|
|
|
|
FROM {question_versions} v
|
2021-09-03 16:43:49 +10:00
|
|
|
JOIN {question_bank_entries} be
|
2021-08-03 16:25:06 +10:00
|
|
|
ON be.id = v.questionbankentryid
|
|
|
|
WHERE be.id = qbe.id) OR qv.version is null)';
|
|
|
|
}
|
2021-09-03 16:43:49 +10:00
|
|
|
$questions = $DB->get_records_sql("SELECT q.*, qv.status, qc.id AS category
|
|
|
|
FROM {question} q
|
|
|
|
JOIN {question_versions} qv ON qv.questionid = q.id
|
|
|
|
JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
|
|
|
|
JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid
|
|
|
|
WHERE qc.id {$usql} {$npsql} {$version}
|
|
|
|
ORDER BY qc.id, q.qtype, q.name", $params);
|
2021-08-03 16:25:06 +10:00
|
|
|
|
|
|
|
// Iterate through questions, getting stuff we need.
|
|
|
|
$qresults = [];
|
2012-03-28 16:52:57 +01:00
|
|
|
foreach($questions as $key => $question) {
|
|
|
|
$question->export_process = $export;
|
|
|
|
$qtype = question_bank::get_qtype($question->qtype, false);
|
2021-09-03 16:43:49 +10:00
|
|
|
if ($export && $qtype->name() === 'missingtype') {
|
2012-03-28 16:52:57 +01:00
|
|
|
// Unrecognised question type. Skip this question when exporting.
|
|
|
|
continue;
|
2006-05-02 12:26:03 +00:00
|
|
|
}
|
2012-03-28 16:52:57 +01:00
|
|
|
$qtype->get_question_options($question);
|
|
|
|
$qresults[] = $question;
|
2006-05-02 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $qresults;
|
|
|
|
}
|
|
|
|
|
2008-11-27 11:50:29 +00:00
|
|
|
/**
|
2018-01-30 13:05:19 +11:00
|
|
|
* Checks whether this is the only child of a top category in a context.
|
|
|
|
*
|
2011-02-23 16:25:25 +00:00
|
|
|
* @param int $categoryid a category id.
|
2018-01-30 13:05:19 +11:00
|
|
|
* @return bool
|
2021-05-11 16:22:03 +10:00
|
|
|
* @deprecated since Moodle 4.0 MDL-71585
|
|
|
|
* @see qbank_managecategories\helper
|
|
|
|
* @todo Final deprecation on Moodle 4.4 MDL-72438
|
2008-11-27 11:50:29 +00:00
|
|
|
*/
|
2018-01-30 13:05:19 +11:00
|
|
|
function question_is_only_child_of_top_category_in_context($categoryid) {
|
2021-05-11 16:22:03 +10:00
|
|
|
debugging('Function question_is_only_child_of_top_category_in_context()
|
|
|
|
has been deprecated and moved to qbank_managecategories plugin,
|
|
|
|
Please use qbank_managecategories\helper::question_is_only_child_of_top_category_in_context() instead.',
|
|
|
|
DEBUG_DEVELOPER);
|
2021-08-07 02:03:58 +10:00
|
|
|
return \qbank_managecategories\helper::question_is_only_child_of_top_category_in_context($categoryid);
|
2018-01-30 13:05:19 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the category is a "Top" category (with no parent).
|
|
|
|
*
|
|
|
|
* @param int $categoryid a category id.
|
|
|
|
* @return bool
|
2021-05-11 16:22:03 +10:00
|
|
|
* @deprecated since Moodle 4.0 MDL-71585
|
|
|
|
* @see qbank_managecategories\helper
|
|
|
|
* @todo Final deprecation on Moodle 4.4 MDL-72438
|
2018-01-30 13:05:19 +11:00
|
|
|
*/
|
|
|
|
function question_is_top_category($categoryid) {
|
2021-05-11 16:22:03 +10:00
|
|
|
debugging('Function question_is_top_category() has been deprecated and moved to qbank_managecategories plugin,
|
|
|
|
Please use qbank_managecategories\helper::question_is_top_category() instead.', DEBUG_DEVELOPER);
|
|
|
|
return \qbank_managecategories\helper::question_is_top_category($categoryid);
|
2008-11-27 11:50:29 +00:00
|
|
|
}
|
2006-05-02 12:26:03 +00:00
|
|
|
|
2008-11-27 11:50:29 +00:00
|
|
|
/**
|
2018-01-30 13:05:19 +11:00
|
|
|
* Ensures that this user is allowed to delete this category.
|
2008-11-27 11:50:29 +00:00
|
|
|
*
|
2011-02-23 16:25:25 +00:00
|
|
|
* @param int $todelete a category id.
|
2021-05-11 16:22:03 +10:00
|
|
|
* @deprecated since Moodle 4.0 MDL-71585
|
|
|
|
* @see qbank_managecategories\helper
|
|
|
|
* @todo Final deprecation on Moodle 4.4 MDL-72438
|
2008-11-27 11:50:29 +00:00
|
|
|
*/
|
|
|
|
function question_can_delete_cat($todelete) {
|
2021-05-11 16:22:03 +10:00
|
|
|
debugging('Function question_can_delete_cat() has been deprecated and moved to qbank_managecategories plugin,
|
|
|
|
Please use qbank_managecategories\helper::question_can_delete_cat() instead.', DEBUG_DEVELOPER);
|
|
|
|
\qbank_managecategories\helper::question_can_delete_cat($todelete);
|
2007-08-09 21:51:09 +00:00
|
|
|
}
|
2009-01-19 05:30:01 +00:00
|
|
|
|
2007-05-03 10:10:01 +00:00
|
|
|
/**
|
|
|
|
* Common setup for all pages for editing questions.
|
2010-04-12 13:31:42 +00:00
|
|
|
* @param string $baseurl the name of the script calling this funciton. For examle 'qusetion/edit.php'.
|
2007-08-09 21:51:09 +00:00
|
|
|
* @param string $edittab code for this edit tab
|
2011-02-23 16:25:25 +00:00
|
|
|
* @param bool $requirecmid require cmid? default false
|
2015-11-26 15:04:10 +00:00
|
|
|
* @param bool $unused no longer used, do no pass
|
2007-08-09 21:51:09 +00:00
|
|
|
* @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
|
2007-05-03 10:10:01 +00:00
|
|
|
*/
|
2015-11-26 15:04:10 +00:00
|
|
|
function question_edit_setup($edittab, $baseurl, $requirecmid = false, $unused = null) {
|
2018-02-02 03:32:55 +00:00
|
|
|
global $PAGE;
|
2015-11-26 15:04:10 +00:00
|
|
|
|
|
|
|
if ($unused !== null) {
|
|
|
|
debugging('Deprecated argument passed to question_edit_setup()', DEBUG_DEVELOPER);
|
|
|
|
}
|
2007-08-09 21:51:09 +00:00
|
|
|
|
2018-02-02 03:32:55 +00:00
|
|
|
$params = [];
|
|
|
|
|
|
|
|
if ($requirecmid) {
|
|
|
|
$params['cmid'] = required_param('cmid', PARAM_INT);
|
|
|
|
} else {
|
|
|
|
$params['cmid'] = optional_param('cmid', null, PARAM_INT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$params['cmid']) {
|
|
|
|
$params['courseid'] = required_param('courseid', PARAM_INT);
|
|
|
|
}
|
|
|
|
|
|
|
|
$params['qpage'] = optional_param('qpage', null, PARAM_INT);
|
|
|
|
|
|
|
|
// Pass 'cat' from page to page and when 'category' comes from a drop down menu
|
|
|
|
// then we also reset the qpage so we go to page 1 of
|
|
|
|
// a new cat.
|
|
|
|
$params['cat'] = optional_param('cat', null, PARAM_SEQUENCE); // If empty will be set up later.
|
|
|
|
$params['category'] = optional_param('category', null, PARAM_SEQUENCE);
|
|
|
|
$params['qperpage'] = optional_param('qperpage', null, PARAM_INT);
|
|
|
|
|
|
|
|
// Question table sorting options.
|
2021-08-07 02:03:58 +10:00
|
|
|
for ($i = 1; $i <= core_question\local\bank\view::MAX_SORTS; $i++) {
|
2018-02-02 03:32:55 +00:00
|
|
|
$param = 'qbs' . $i;
|
|
|
|
if ($sort = optional_param($param, '', PARAM_TEXT)) {
|
|
|
|
$params[$param] = $sort;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display options.
|
|
|
|
$params['recurse'] = optional_param('recurse', null, PARAM_BOOL);
|
|
|
|
$params['showhidden'] = optional_param('showhidden', null, PARAM_BOOL);
|
2023-02-20 22:13:12 +00:00
|
|
|
$params['qbshowtext'] = optional_param('qbshowtext', null, PARAM_INT);
|
2018-02-02 03:32:55 +00:00
|
|
|
// Category list page.
|
|
|
|
$params['cpage'] = optional_param('cpage', null, PARAM_INT);
|
|
|
|
$params['qtagids'] = optional_param_array('qtagids', null, PARAM_INT);
|
|
|
|
|
|
|
|
$PAGE->set_pagelayout('admin');
|
|
|
|
|
|
|
|
return question_build_edit_resources($edittab, $baseurl, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Common function for building the generic resources required by the
|
|
|
|
* editing questions pages.
|
|
|
|
*
|
|
|
|
* Either a cmid or a course id must be provided as keys in $params or
|
|
|
|
* an exception will be thrown. All other params are optional and will have
|
|
|
|
* sane default applied if not provided.
|
|
|
|
*
|
|
|
|
* The acceptable keys for $params are:
|
|
|
|
* [
|
|
|
|
* 'cmid' => PARAM_INT,
|
|
|
|
* 'courseid' => PARAM_INT,
|
|
|
|
* 'qpage' => PARAM_INT,
|
|
|
|
* 'cat' => PARAM_SEQUENCE,
|
|
|
|
* 'category' => PARAM_SEQUENCE,
|
|
|
|
* 'qperpage' => PARAM_INT,
|
|
|
|
* 'recurse' => PARAM_INT,
|
|
|
|
* 'showhidden' => PARAM_INT,
|
|
|
|
* 'qbshowtext' => PARAM_INT,
|
|
|
|
* 'cpage' => PARAM_INT,
|
|
|
|
* 'recurse' => PARAM_BOOL,
|
|
|
|
* 'showhidden' => PARAM_BOOL,
|
2023-02-20 22:13:12 +00:00
|
|
|
* 'qbshowtext' => PARAM_INT,
|
2018-02-02 03:32:55 +00:00
|
|
|
* 'qtagids' => [PARAM_INT], (array of integers)
|
|
|
|
* 'qbs1' => PARAM_TEXT,
|
|
|
|
* 'qbs2' => PARAM_TEXT,
|
|
|
|
* 'qbs3' => PARAM_TEXT,
|
2021-08-07 02:03:58 +10:00
|
|
|
* ... and more qbs keys up to core_question\local\bank\view::MAX_SORTS ...
|
2018-02-02 03:32:55 +00:00
|
|
|
* ];
|
|
|
|
*
|
|
|
|
* @param string $edittab Code for this edit tab
|
|
|
|
* @param string $baseurl The name of the script calling this funciton. For examle 'qusetion/edit.php'.
|
|
|
|
* @param array $params The provided parameters to construct the resources with.
|
2022-03-03 12:33:03 +00:00
|
|
|
* @param int $defaultquestionsperpage number of questions per page, if not given in the URL.
|
2018-02-02 03:32:55 +00:00
|
|
|
* @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
|
|
|
|
*/
|
2022-03-03 12:33:03 +00:00
|
|
|
function question_build_edit_resources($edittab, $baseurl, $params,
|
|
|
|
$defaultquestionsperpage = DEFAULT_QUESTIONS_PER_PAGE) {
|
|
|
|
global $DB;
|
2018-02-02 03:32:55 +00:00
|
|
|
|
2010-04-12 13:31:42 +00:00
|
|
|
$thispageurl = new moodle_url($baseurl);
|
2009-12-16 21:33:01 +00:00
|
|
|
$thispageurl->remove_all_params(); // We are going to explicity add back everything important - this avoids unwanted params from being retained.
|
2009-01-07 07:12:37 +00:00
|
|
|
|
2018-02-02 03:32:55 +00:00
|
|
|
$cleanparams = [
|
|
|
|
'qsorts' => [],
|
|
|
|
'qtagids' => []
|
|
|
|
];
|
|
|
|
$paramtypes = [
|
|
|
|
'cmid' => PARAM_INT,
|
|
|
|
'courseid' => PARAM_INT,
|
|
|
|
'qpage' => PARAM_INT,
|
|
|
|
'cat' => PARAM_SEQUENCE,
|
|
|
|
'category' => PARAM_SEQUENCE,
|
|
|
|
'qperpage' => PARAM_INT,
|
|
|
|
'recurse' => PARAM_INT,
|
|
|
|
'showhidden' => PARAM_INT,
|
|
|
|
'qbshowtext' => PARAM_INT,
|
|
|
|
'cpage' => PARAM_INT,
|
|
|
|
'recurse' => PARAM_BOOL,
|
|
|
|
'showhidden' => PARAM_BOOL,
|
2023-02-20 22:13:12 +00:00
|
|
|
'qbshowtext' => PARAM_INT,
|
2018-02-02 03:32:55 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($paramtypes as $name => $type) {
|
|
|
|
if (isset($params[$name])) {
|
|
|
|
$cleanparams[$name] = clean_param($params[$name], $type);
|
|
|
|
} else {
|
|
|
|
$cleanparams[$name] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($params['qtagids'])) {
|
|
|
|
$cleanparams['qtagids'] = clean_param_array($params['qtagids'], PARAM_INT);
|
|
|
|
}
|
|
|
|
|
|
|
|
$cmid = $cleanparams['cmid'];
|
|
|
|
$courseid = $cleanparams['courseid'];
|
|
|
|
$qpage = $cleanparams['qpage'] ?: -1;
|
|
|
|
$cat = $cleanparams['cat'] ?: 0;
|
|
|
|
$category = $cleanparams['category'] ?: 0;
|
|
|
|
$qperpage = $cleanparams['qperpage'];
|
|
|
|
$recurse = $cleanparams['recurse'];
|
|
|
|
$showhidden = $cleanparams['showhidden'];
|
|
|
|
$qbshowtext = $cleanparams['qbshowtext'];
|
|
|
|
$cpage = $cleanparams['cpage'] ?: 1;
|
|
|
|
$recurse = $cleanparams['recurse'];
|
|
|
|
$showhidden = $cleanparams['showhidden'];
|
|
|
|
$qbshowtext = $cleanparams['qbshowtext'];
|
|
|
|
$qsorts = $cleanparams['qsorts'];
|
|
|
|
$qtagids = $cleanparams['qtagids'];
|
|
|
|
|
|
|
|
if (is_null($cmid) && is_null($courseid)) {
|
|
|
|
throw new \moodle_exception('Must provide a cmid or courseid');
|
2007-05-03 10:10:01 +00:00
|
|
|
}
|
2018-02-02 03:32:55 +00:00
|
|
|
|
|
|
|
if ($cmid) {
|
2007-08-09 21:51:09 +00:00
|
|
|
list($module, $cm) = get_module_from_cmid($cmid);
|
2007-05-03 10:10:01 +00:00
|
|
|
$courseid = $cm->course;
|
|
|
|
$thispageurl->params(compact('cmid'));
|
2007-08-09 21:51:09 +00:00
|
|
|
require_login($courseid, false, $cm);
|
2012-07-24 16:57:51 +08:00
|
|
|
$thiscontext = context_module::instance($cmid);
|
2007-05-03 10:10:01 +00:00
|
|
|
} else {
|
|
|
|
$module = null;
|
|
|
|
$cm = null;
|
2015-11-26 15:04:10 +00:00
|
|
|
$thispageurl->params(compact('courseid'));
|
|
|
|
require_login($courseid, false);
|
|
|
|
$thiscontext = context_course::instance($courseid);
|
2007-05-03 10:10:01 +00:00
|
|
|
}
|
2007-08-09 21:51:09 +00:00
|
|
|
|
|
|
|
if ($thiscontext){
|
2021-11-16 23:18:28 +11:00
|
|
|
$contexts = new core_question\local\bank\question_edit_contexts($thiscontext);
|
2007-08-09 21:51:09 +00:00
|
|
|
$contexts->require_one_edit_tab_cap($edittab);
|
|
|
|
} else {
|
|
|
|
$contexts = null;
|
|
|
|
}
|
|
|
|
|
2018-02-02 03:32:55 +00:00
|
|
|
$pagevars['qpage'] = $qpage;
|
2007-08-09 21:51:09 +00:00
|
|
|
|
2018-02-02 03:32:55 +00:00
|
|
|
// Pass 'cat' from page to page and when 'category' comes from a drop down menu
|
|
|
|
// then we also reset the qpage so we go to page 1 of
|
|
|
|
// a new cat.
|
|
|
|
if ($category && $category != $cat) { // Is this a move to a new category?
|
|
|
|
$pagevars['cat'] = $category;
|
|
|
|
$pagevars['qpage'] = 0;
|
|
|
|
} else {
|
|
|
|
$pagevars['cat'] = $cat; // If empty will be set up later.
|
2007-05-07 04:55:55 +00:00
|
|
|
}
|
2018-02-02 03:32:55 +00:00
|
|
|
|
2007-05-07 04:55:55 +00:00
|
|
|
if ($pagevars['cat']){
|
|
|
|
$thispageurl->param('cat', $pagevars['cat']);
|
|
|
|
}
|
2018-02-02 03:32:55 +00:00
|
|
|
|
2011-06-17 18:31:22 +01:00
|
|
|
if (strpos($baseurl, '/question/') === 0) {
|
|
|
|
navigation_node::override_active_url($thispageurl);
|
|
|
|
}
|
|
|
|
|
2018-02-02 03:32:55 +00:00
|
|
|
// This need to occur after the override_active_url call above because
|
|
|
|
// these values change on the page request causing the URLs to mismatch
|
|
|
|
// when trying to work out the active node.
|
2021-08-07 02:03:58 +10:00
|
|
|
for ($i = 1; $i <= core_question\local\bank\view::MAX_SORTS; $i++) {
|
2018-02-02 03:32:55 +00:00
|
|
|
$param = 'qbs' . $i;
|
|
|
|
if (isset($params[$param])) {
|
|
|
|
$value = clean_param($params[$param], PARAM_TEXT);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$thispageurl->param($param, $value);
|
|
|
|
}
|
|
|
|
|
2007-05-03 10:10:01 +00:00
|
|
|
if ($pagevars['qpage'] > -1) {
|
|
|
|
$thispageurl->param('qpage', $pagevars['qpage']);
|
|
|
|
} else {
|
|
|
|
$pagevars['qpage'] = 0;
|
|
|
|
}
|
|
|
|
|
2022-03-03 12:33:03 +00:00
|
|
|
if ($defaultquestionsperpage == DEFAULT_QUESTIONS_PER_PAGE) {
|
|
|
|
$pagevars['qperpage'] = question_set_or_get_user_preference(
|
|
|
|
'qperpage', $qperpage, DEFAULT_QUESTIONS_PER_PAGE, $thispageurl);
|
|
|
|
} else {
|
|
|
|
$pagevars['qperpage'] = $qperpage ?? $defaultquestionsperpage;
|
|
|
|
}
|
2007-05-04 10:46:33 +00:00
|
|
|
|
2007-08-09 21:51:09 +00:00
|
|
|
$defaultcategory = question_make_default_categories($contexts->all());
|
|
|
|
|
2018-02-02 03:32:55 +00:00
|
|
|
$contextlistarr = [];
|
2007-08-09 21:51:09 +00:00
|
|
|
foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
|
2014-06-30 19:09:54 +01:00
|
|
|
$contextlistarr[] = "'{$context->id}'";
|
2007-08-09 21:51:09 +00:00
|
|
|
}
|
2019-11-03 22:59:10 +01:00
|
|
|
$contextlist = join(' ,', $contextlistarr);
|
2007-08-09 21:51:09 +00:00
|
|
|
if (!empty($pagevars['cat'])){
|
|
|
|
$catparts = explode(',', $pagevars['cat']);
|
2011-05-20 18:50:41 +01:00
|
|
|
if (!$catparts[0] || (false !== array_search($catparts[1], $contextlistarr)) ||
|
2008-06-06 14:43:15 +00:00
|
|
|
!$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidcategory', 'question');
|
2007-08-09 21:51:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$category = $defaultcategory;
|
2014-06-30 19:09:54 +01:00
|
|
|
$pagevars['cat'] = "{$category->id},{$category->contextid}";
|
2007-05-04 10:46:33 +00:00
|
|
|
}
|
|
|
|
|
2012-08-09 15:40:08 +01:00
|
|
|
// Display options.
|
2018-02-13 11:25:52 +08:00
|
|
|
$pagevars['recurse'] = question_set_or_get_user_preference('recurse', $recurse, 1, $thispageurl);
|
|
|
|
$pagevars['showhidden'] = question_set_or_get_user_preference('showhidden', $showhidden, 0, $thispageurl);
|
|
|
|
$pagevars['qbshowtext'] = question_set_or_get_user_preference('qbshowtext', $qbshowtext, 0, $thispageurl);
|
2007-08-09 21:51:09 +00:00
|
|
|
|
2012-08-09 15:40:08 +01:00
|
|
|
// Category list page.
|
2018-02-02 03:32:55 +00:00
|
|
|
$pagevars['cpage'] = $cpage;
|
2007-05-07 05:53:20 +00:00
|
|
|
if ($pagevars['cpage'] != 1){
|
|
|
|
$thispageurl->param('cpage', $pagevars['cpage']);
|
|
|
|
}
|
2007-05-04 10:46:33 +00:00
|
|
|
|
2018-02-02 03:32:55 +00:00
|
|
|
$pagevars['qtagids'] = $qtagids;
|
|
|
|
foreach ($pagevars['qtagids'] as $index => $qtagid) {
|
|
|
|
$thispageurl->param("qtagids[{$index}]", $qtagid);
|
|
|
|
}
|
|
|
|
|
2007-08-09 21:51:09 +00:00
|
|
|
return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
|
|
|
|
}
|
|
|
|
|
MDL-43089 quiz: improved interface for building quizzes
This commit is actually the joint work of Mahmoud Kassaei, Colin
Chambers and Tim Hunt from The Open University. We could only use one
persons name for the commit, and this time Colin gets the credit/blame.
The goal of this work was to increase usability, and also clean up
the page enough that it will be possible to add new features in future.
Display of mod/quiz/edit.php is now entirely generated by
mod_quiz\output\edit_renderer. This uses a helper class
mod_quiz\structure to provide details of the structure of the quiz, and
mod_quiz\repaginate to alter that structure. (Acutally, there are still
some modification methods on mod_quiz\structure. Expect that to be
cleaned up in future.)
The new code uses much more ajax, and there are new scripts
mod/quiz/edit_rest.php and mod/quiz/repaginate.php to handle this.
(Again, don't be surprised if those two scripts get merged in future.)
Also questionbank.ajax.php (which may, in future, be made more generic,
and moved into the core question bank code.)
Most of the new JavaScript code has intentionally copied the way things
are done when editing activities on the course page.
As a result of this, mod/quiz/editlib.php is now much shorter than it
was. (In future, expect the remaining code in here to move into
mod/quiz/classes.)
2013-12-05 16:18:24 +00:00
|
|
|
/**
|
|
|
|
* Get the category id from $pagevars.
|
|
|
|
* @param array $pagevars from {@link question_edit_setup()}.
|
|
|
|
* @return int the category id.
|
|
|
|
*/
|
|
|
|
function question_get_category_id_from_pagevars(array $pagevars) {
|
|
|
|
list($questioncategoryid) = explode(',', $pagevars['cat']);
|
|
|
|
return $questioncategoryid;
|
|
|
|
}
|
|
|
|
|
2012-08-09 15:40:08 +01:00
|
|
|
/**
|
|
|
|
* Get a particular question preference that is also stored as a user preference.
|
|
|
|
* If the the value is given in the GET/POST request, then that value is used,
|
|
|
|
* and the user preference is updated to that value. Otherwise, the last set
|
|
|
|
* value of the user preference is used, or if it has never been set the default
|
|
|
|
* passed to this function.
|
|
|
|
*
|
|
|
|
* @param string $param the param name. The URL parameter set, and the GET/POST
|
|
|
|
* parameter read. The user_preference name is 'question_bank_' . $param.
|
|
|
|
* @param mixed $default The default value to use, if not otherwise set.
|
|
|
|
* @param int $type one of the PARAM_... constants.
|
|
|
|
* @param moodle_url $thispageurl if the value has been explicitly set, we add
|
|
|
|
* it to this URL.
|
|
|
|
* @return mixed the parameter value to use.
|
|
|
|
*/
|
|
|
|
function question_get_display_preference($param, $default, $type, $thispageurl) {
|
|
|
|
$submittedvalue = optional_param($param, null, $type);
|
2018-02-13 11:25:52 +08:00
|
|
|
return question_set_or_get_user_preference($param, $submittedvalue, $default, $thispageurl);
|
2018-02-02 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a user preference by name or set the user preference to a given value.
|
|
|
|
*
|
|
|
|
* If $value is null then the function will only attempt to retrieve the
|
|
|
|
* user preference requested by $name. If no user preference is found then the
|
|
|
|
* $default value will be returned. In this case the user preferences are not
|
|
|
|
* modified and nor are the params on $thispageurl.
|
|
|
|
*
|
|
|
|
* If $value is anything other than null then the function will set the user
|
|
|
|
* preference $name to the provided $value and will also set it as a param
|
|
|
|
* on $thispageurl.
|
|
|
|
*
|
|
|
|
* @param string $name The user_preference name is 'question_bank_' . $name.
|
|
|
|
* @param mixed $value The preference value.
|
|
|
|
* @param mixed $default The default value to use, if not otherwise set.
|
|
|
|
* @param moodle_url $thispageurl if the value has been explicitly set, we add
|
|
|
|
* it to this URL.
|
|
|
|
* @return mixed the parameter value to use.
|
|
|
|
*/
|
2018-02-13 11:25:52 +08:00
|
|
|
function question_set_or_get_user_preference($name, $value, $default, $thispageurl) {
|
2018-02-02 03:32:55 +00:00
|
|
|
if (is_null($value)) {
|
|
|
|
return get_user_preferences('question_bank_' . $name, $default);
|
2012-08-09 15:40:08 +01:00
|
|
|
}
|
|
|
|
|
2018-02-02 03:32:55 +00:00
|
|
|
set_user_preference('question_bank_' . $name, $value);
|
|
|
|
$thispageurl->param($name, $value);
|
|
|
|
return $value;
|
2012-08-09 15:40:08 +01:00
|
|
|
}
|
|
|
|
|
2007-08-09 21:51:09 +00:00
|
|
|
/**
|
|
|
|
* Make sure user is logged in as required in this context.
|
|
|
|
*/
|
|
|
|
function require_login_in_context($contextorid = null){
|
2010-09-18 11:45:59 +00:00
|
|
|
global $DB, $CFG;
|
2007-08-09 21:51:09 +00:00
|
|
|
if (!is_object($contextorid)){
|
2012-08-21 15:02:40 +08:00
|
|
|
$context = context::instance_by_id($contextorid, IGNORE_MISSING);
|
2007-08-09 21:51:09 +00:00
|
|
|
} else {
|
|
|
|
$context = $contextorid;
|
|
|
|
}
|
|
|
|
if ($context && ($context->contextlevel == CONTEXT_COURSE)) {
|
|
|
|
require_login($context->instanceid);
|
|
|
|
} else if ($context && ($context->contextlevel == CONTEXT_MODULE)) {
|
2008-06-06 14:43:15 +00:00
|
|
|
if ($cm = $DB->get_record('course_modules',array('id' =>$context->instanceid))) {
|
|
|
|
if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidcourseid');
|
2007-08-09 21:51:09 +00:00
|
|
|
}
|
|
|
|
require_course_login($course, true, $cm);
|
|
|
|
|
|
|
|
} else {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidcoursemodule');
|
2007-08-09 21:51:09 +00:00
|
|
|
}
|
|
|
|
} else if ($context && ($context->contextlevel == CONTEXT_SYSTEM)) {
|
|
|
|
if (!empty($CFG->forcelogin)) {
|
|
|
|
require_login();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
require_login();
|
|
|
|
}
|
2007-05-03 10:10:01 +00:00
|
|
|
}
|
2009-02-25 07:14:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 $hiddenparams hidden parameters to add to the form, in addition to
|
2012-04-24 12:04:06 +01:00
|
|
|
* the qtype radio buttons.
|
|
|
|
* @param $allowedqtypes optional list of qtypes that are allowed. If given, only
|
|
|
|
* those qtypes will be shown. Example value array('description', 'multichoice').
|
2021-06-03 21:05:04 +10:00
|
|
|
* @deprecated since Moodle 4.0
|
|
|
|
* @see \qbank_editquestion\editquestion_helper::print_choose_qtype_to_add_form()
|
2021-08-07 02:03:58 +10:00
|
|
|
* @todo Final deprecation of this class in moodle 4.4 MDL-72438
|
2009-02-25 07:14:03 +00:00
|
|
|
*/
|
2012-05-02 12:33:06 +01:00
|
|
|
function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null, $enablejs = true) {
|
2021-08-07 02:03:58 +10:00
|
|
|
debugging('Function print_choose_qtype_to_add_form() is deprecated,
|
|
|
|
please use \qbank_editquestion\editquestion_helper::print_choose_qtype_to_add_form() instead.', DEBUG_DEVELOPER);
|
2011-02-10 20:44:47 +00:00
|
|
|
global $CFG, $PAGE, $OUTPUT;
|
2012-08-06 21:51:11 +02:00
|
|
|
|
2021-08-07 02:03:58 +10:00
|
|
|
$chooser = \qbank_editquestion\qbank_chooser::get($PAGE->course, $hiddenparams, $allowedqtypes);
|
2012-05-02 12:33:06 +01:00
|
|
|
$renderer = $PAGE->get_renderer('question', 'bank');
|
2016-09-01 17:30:46 +08:00
|
|
|
|
|
|
|
return $renderer->render($chooser);
|
2009-02-25 07:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
*
|
2011-02-23 16:25:25 +00:00
|
|
|
* @param int $categoryid The id of the category that the new question should be added to.
|
2009-02-25 07:14:03 +00:00
|
|
|
* @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 string $caption the text to display on the button.
|
|
|
|
* @param string $tooltip a tooltip to add to the button (optional).
|
2011-02-23 16:25:25 +00:00
|
|
|
* @param bool $disabled if true, the button will be disabled.
|
2021-06-03 21:05:04 +10:00
|
|
|
* @deprecated since Moodle 4.0
|
|
|
|
* @see \qbank_editquestion\editquestion_helper::create_new_question_button()
|
2021-08-07 02:03:58 +10:00
|
|
|
* @todo Final deprecation of this class in moodle 4.4 MDL-72438
|
2009-02-25 07:14:03 +00:00
|
|
|
*/
|
|
|
|
function create_new_question_button($categoryid, $params, $caption, $tooltip = '', $disabled = false) {
|
2021-06-03 21:05:04 +10:00
|
|
|
debugging('Function create_new_question_button() has been deprecated and moved to bank/editquestion,
|
|
|
|
please use qbank_editquestion\editquestion_helper::create_new_question_button() instead.', DEBUG_DEVELOPER);
|
2009-08-18 05:19:00 +00:00
|
|
|
global $CFG, $PAGE, $OUTPUT;
|
2009-02-25 07:14:03 +00:00
|
|
|
static $choiceformprinted = false;
|
|
|
|
$params['category'] = $categoryid;
|
2021-08-07 02:03:58 +10:00
|
|
|
$url = new moodle_url('/question/bank/editquestion/addquestion.php', $params);
|
2010-01-03 15:46:14 +00:00
|
|
|
echo $OUTPUT->single_button($url, $caption, 'get', array('disabled'=>$disabled, 'title'=>$tooltip));
|
2009-08-20 08:49:11 +00:00
|
|
|
|
2009-02-25 07:14:03 +00:00
|
|
|
if (!$choiceformprinted) {
|
|
|
|
echo '<div id="qtypechoicecontainer">';
|
MDL-43089 quiz: improved interface for building quizzes
This commit is actually the joint work of Mahmoud Kassaei, Colin
Chambers and Tim Hunt from The Open University. We could only use one
persons name for the commit, and this time Colin gets the credit/blame.
The goal of this work was to increase usability, and also clean up
the page enough that it will be possible to add new features in future.
Display of mod/quiz/edit.php is now entirely generated by
mod_quiz\output\edit_renderer. This uses a helper class
mod_quiz\structure to provide details of the structure of the quiz, and
mod_quiz\repaginate to alter that structure. (Acutally, there are still
some modification methods on mod_quiz\structure. Expect that to be
cleaned up in future.)
The new code uses much more ajax, and there are new scripts
mod/quiz/edit_rest.php and mod/quiz/repaginate.php to handle this.
(Again, don't be surprised if those two scripts get merged in future.)
Also questionbank.ajax.php (which may, in future, be made more generic,
and moved into the core question bank code.)
Most of the new JavaScript code has intentionally copied the way things
are done when editing activities on the course page.
As a result of this, mod/quiz/editlib.php is now much shorter than it
was. (In future, expect the remaining code in here to move into
mod/quiz/classes.)
2013-12-05 16:18:24 +00:00
|
|
|
echo print_choose_qtype_to_add_form(array());
|
2009-02-25 07:14:03 +00:00
|
|
|
echo "</div>\n";
|
|
|
|
$choiceformprinted = true;
|
|
|
|
}
|
|
|
|
}
|