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
|
|
|
|
2006-06-12 13:54:01 +00:00
|
|
|
define('DEFAULT_QUESTIONS_PER_PAGE', 20);
|
2015-07-08 17:17:02 -04:00
|
|
|
define('MAXIMUM_QUESTIONS_PER_PAGE', 1000);
|
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))){
|
2008-08-27 16:34:23 +00:00
|
|
|
print_error('invalidcoursemodule');
|
2008-06-06 14:43:15 +00:00
|
|
|
} elseif (!$modrec =$DB->get_record($cmrec->modname, array('id' => $cmrec->instance))) {
|
2008-08-27 16:34:23 +00:00
|
|
|
print_error('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);
|
|
|
|
}
|
2006-05-02 12:26:03 +00:00
|
|
|
/**
|
|
|
|
* Function to read all questions for category into big array
|
|
|
|
*
|
|
|
|
* @param int $category category number
|
2006-05-13 08:49:46 +00:00
|
|
|
* @param bool $noparent if true only questions with NO parent will be selected
|
|
|
|
* @param bool $recurse include subdirectories
|
2007-03-28 11:58:44 +00:00
|
|
|
* @param bool $export set true if this is called by questionbank export
|
2006-05-02 12:26:03 +00:00
|
|
|
*/
|
2007-03-28 11:58:44 +00:00
|
|
|
function get_questions_category( $category, $noparent=false, $recurse=true, $export=true ) {
|
2011-02-10 20:44:47 +00:00
|
|
|
global $DB;
|
2006-05-02 12:26:03 +00:00
|
|
|
|
2012-03-28 16:52:57 +01:00
|
|
|
// Build sql bit for $noparent
|
2006-05-02 12:26:03 +00:00
|
|
|
$npsql = '';
|
|
|
|
if ($noparent) {
|
|
|
|
$npsql = " and parent='0' ";
|
|
|
|
}
|
|
|
|
|
2011-02-24 17:47:51 +00: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 {
|
|
|
|
$categorylist = array($category->id);
|
2006-05-13 08:49:46 +00:00
|
|
|
}
|
|
|
|
|
2012-03-28 16:52:57 +01: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);
|
2018-08-03 09:53:54 +01:00
|
|
|
$questions = $DB->get_records_select('question', "category {$usql} {$npsql}", $params, 'category, qtype, name');
|
2006-05-02 12:26:03 +00:00
|
|
|
|
2012-03-28 16:52:57 +01:00
|
|
|
// Iterate through questions, getting stuff we need
|
|
|
|
$qresults = array();
|
|
|
|
foreach($questions as $key => $question) {
|
|
|
|
$question->export_process = $export;
|
|
|
|
$qtype = question_bank::get_qtype($question->qtype, false);
|
|
|
|
if ($export && $qtype->name() == 'missingtype') {
|
|
|
|
// 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
|
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) {
|
2008-11-27 11:50:29 +00:00
|
|
|
global $DB;
|
|
|
|
return 1 == $DB->count_records_sql("
|
|
|
|
SELECT count(*)
|
2018-01-30 13:05:19 +11:00
|
|
|
FROM {question_categories} c
|
|
|
|
JOIN {question_categories} p ON c.parent = p.id
|
|
|
|
JOIN {question_categories} s ON s.parent = c.parent
|
|
|
|
WHERE c.id = ? AND p.parent = 0", array($categoryid));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the category is a "Top" category (with no parent).
|
|
|
|
*
|
|
|
|
* @param int $categoryid a category id.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function question_is_top_category($categoryid) {
|
|
|
|
global $DB;
|
|
|
|
return 0 == $DB->get_field('question_categories', 'parent', array('id' => $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.
|
2008-11-27 11:50:29 +00:00
|
|
|
*/
|
|
|
|
function question_can_delete_cat($todelete) {
|
|
|
|
global $DB;
|
2018-01-30 13:05:19 +11:00
|
|
|
if (question_is_top_category($todelete)) {
|
|
|
|
print_error('cannotdeletetopcat', 'question');
|
|
|
|
} else if (question_is_only_child_of_top_category_in_context($todelete)) {
|
2008-07-24 04:20:44 +00:00
|
|
|
print_error('cannotdeletecate', 'question');
|
2007-08-09 21:51:09 +00:00
|
|
|
} else {
|
2008-11-27 11:50:29 +00:00
|
|
|
$contextid = $DB->get_field('question_categories', 'contextid', array('id' => $todelete));
|
2012-08-21 14:20:30 +08:00
|
|
|
require_capability('moodle/question:managecategory', context::instance_by_id($contextid));
|
2007-08-09 21:51:09 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-19 05:30:01 +00:00
|
|
|
|
2011-02-23 16:25:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for representing a column in a {@link question_bank_view}.
|
|
|
|
*
|
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2014-03-11 09:16:33 -05:00
|
|
|
* @deprecated since Moodle 2.7 MDL-40457
|
2011-02-23 16:25:25 +00:00
|
|
|
*/
|
2014-03-11 09:16:33 -05:00
|
|
|
class_alias('core_question\bank\column_base', 'question_bank_column_base', true);
|
2011-02-23 16:25:25 +00:00
|
|
|
|
2009-01-20 10:11:23 +00:00
|
|
|
/**
|
|
|
|
* A column with a checkbox for each question with name q{questionid}.
|
2011-02-23 16:25:25 +00:00
|
|
|
*
|
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2014-03-11 09:16:33 -05:00
|
|
|
* @deprecated since Moodle 2.7 MDL-40457
|
2009-01-20 10:11:23 +00:00
|
|
|
*/
|
2014-03-11 09:16:33 -05:00
|
|
|
class_alias('core_question\bank\checkbox_column', 'question_bank_checkbox_column', true);
|
2011-02-23 16:25:25 +00:00
|
|
|
|
2009-01-20 10:11:23 +00:00
|
|
|
/**
|
|
|
|
* A column type for the name of the question type.
|
2011-02-23 16:25:25 +00:00
|
|
|
*
|
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2014-03-11 09:16:33 -05:00
|
|
|
* @deprecated since Moodle 2.7 MDL-40457
|
2009-01-20 10:11:23 +00:00
|
|
|
*/
|
2014-03-11 09:16:33 -05:00
|
|
|
class_alias('core_question\bank\question_type_column', 'question_bank_question_type_column', true);
|
2009-01-20 10:11:23 +00:00
|
|
|
|
2011-02-23 16:25:25 +00:00
|
|
|
|
2009-01-20 10:11:23 +00:00
|
|
|
/**
|
|
|
|
* A column type for the name of the question name.
|
2011-02-23 16:25:25 +00:00
|
|
|
*
|
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2014-03-11 09:16:33 -05:00
|
|
|
* @deprecated since Moodle 2.7 MDL-40457
|
2009-01-20 10:11:23 +00:00
|
|
|
*/
|
2014-03-11 09:16:33 -05:00
|
|
|
class_alias('core_question\bank\question_name_column', 'question_bank_question_name_column', true);
|
2009-01-20 10:11:23 +00:00
|
|
|
|
2011-02-23 16:25:25 +00:00
|
|
|
|
2009-01-20 10:11:23 +00:00
|
|
|
/**
|
|
|
|
* A column type for the name of the question creator.
|
2011-02-23 16:25:25 +00:00
|
|
|
*
|
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2014-03-11 09:16:33 -05:00
|
|
|
* @deprecated since Moodle 2.7 MDL-40457
|
2009-01-20 10:11:23 +00:00
|
|
|
*/
|
2014-03-11 09:16:33 -05:00
|
|
|
class_alias('core_question\bank\creator_name_column', 'question_bank_creator_name_column', true);
|
2009-01-20 10:11:23 +00:00
|
|
|
|
2011-02-23 16:25:25 +00:00
|
|
|
|
2009-01-20 10:11:23 +00:00
|
|
|
/**
|
|
|
|
* A column type for the name of the question last modifier.
|
2011-02-23 16:25:25 +00:00
|
|
|
*
|
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2014-03-11 09:16:33 -05:00
|
|
|
* @deprecated since Moodle 2.7 MDL-40457
|
2009-01-20 10:11:23 +00:00
|
|
|
*/
|
2014-03-11 09:16:33 -05:00
|
|
|
class_alias('core_question\bank\modifier_name_column', 'question_bank_modifier_name_column', true);
|
2009-01-20 10:11:23 +00:00
|
|
|
|
2011-02-23 16:25:25 +00:00
|
|
|
|
2009-01-20 10:11:23 +00:00
|
|
|
/**
|
|
|
|
* A base class for actions that are an icon that lets you manipulate the question in some way.
|
2011-02-23 16:25:25 +00:00
|
|
|
*
|
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2014-03-11 09:16:33 -05:00
|
|
|
* @deprecated since Moodle 2.7 MDL-40457
|
2009-01-20 10:11:23 +00:00
|
|
|
*/
|
2014-03-11 09:16:33 -05:00
|
|
|
class_alias('core_question\bank\action_column_base', 'question_bank_action_column_base', true);
|
2009-01-20 10:11:23 +00:00
|
|
|
|
2011-02-23 16:25:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for question bank columns that just contain an action icon.
|
|
|
|
*
|
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2014-03-11 09:16:33 -05:00
|
|
|
* @deprecated since Moodle 2.7 MDL-40457
|
2011-02-23 16:25:25 +00:00
|
|
|
*/
|
2014-03-11 09:16:33 -05:00
|
|
|
class_alias('core_question\bank\edit_action_column', 'question_bank_edit_action_column', true);
|
2009-01-20 10:11:23 +00:00
|
|
|
|
2013-11-04 16:35:27 +00:00
|
|
|
/**
|
|
|
|
* Question bank column for the duplicate action icon.
|
|
|
|
*
|
|
|
|
* @copyright 2013 The Open University
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2014-03-11 09:16:33 -05:00
|
|
|
* @deprecated since Moodle 2.7 MDL-40457
|
2013-11-04 16:35:27 +00:00
|
|
|
*/
|
2014-03-11 09:16:33 -05:00
|
|
|
class_alias('core_question\bank\copy_action_column', 'question_bank_copy_action_column', true);
|
2011-02-23 16:25:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Question bank columns for the preview action icon.
|
|
|
|
*
|
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2014-03-11 09:16:33 -05:00
|
|
|
* @deprecated since Moodle 2.7 MDL-40457
|
2011-02-23 16:25:25 +00:00
|
|
|
*/
|
2014-03-11 09:16:33 -05:00
|
|
|
class_alias('core_question\bank\preview_action_column', 'question_bank_preview_action_column', true);
|
2009-01-20 10:11:23 +00:00
|
|
|
|
2011-02-23 16:25:25 +00:00
|
|
|
|
2009-01-20 10:11:23 +00:00
|
|
|
/**
|
|
|
|
* action to delete (or hide) a question, or restore a previously hidden question.
|
2011-02-23 16:25:25 +00:00
|
|
|
*
|
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2014-03-11 09:16:33 -05:00
|
|
|
* @deprecated since Moodle 2.7 MDL-40457
|
2009-01-20 10:11:23 +00:00
|
|
|
*/
|
2014-03-11 09:16:33 -05:00
|
|
|
class_alias('core_question\bank\delete_action_column', 'question_bank_delete_action_column', true);
|
2009-01-20 10:11:23 +00:00
|
|
|
|
2009-01-22 09:19:37 +00:00
|
|
|
/**
|
|
|
|
* Base class for 'columns' that are actually displayed as a row following the main question row.
|
2011-02-23 16:25:25 +00:00
|
|
|
*
|
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2014-03-11 09:16:33 -05:00
|
|
|
* @deprecated since Moodle 2.7 MDL-40457
|
2009-01-22 09:19:37 +00:00
|
|
|
*/
|
2014-03-11 09:16:33 -05:00
|
|
|
class_alias('core_question\bank\row_base', 'question_bank_row_base', true);
|
2009-01-22 09:19:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A column type for the name of the question name.
|
2011-02-23 16:25:25 +00:00
|
|
|
*
|
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2014-03-11 09:16:33 -05:00
|
|
|
* @deprecated since Moodle 2.7 MDL-40457
|
2009-01-22 09:19:37 +00:00
|
|
|
*/
|
2014-03-11 09:16:33 -05:00
|
|
|
class_alias('core_question\bank\question_text_row', 'question_bank_question_text_row', true);
|
2009-01-22 09:19:37 +00:00
|
|
|
|
2006-05-02 12:26:03 +00:00
|
|
|
/**
|
2011-02-23 16:25:25 +00:00
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2014-03-11 09:16:33 -05:00
|
|
|
* @deprecated since Moodle 2.7 MDL-40457
|
2006-05-02 12:26:03 +00:00
|
|
|
*/
|
2014-03-11 09:16:33 -05:00
|
|
|
class_alias('core_question\bank\view', 'question_bank_view', true);
|
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.
|
|
|
|
for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) {
|
|
|
|
$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);
|
|
|
|
$params['qbshowtext'] = optional_param('qbshowtext', null, PARAM_BOOL);
|
|
|
|
// 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,
|
|
|
|
* 'qbshowtext' => PARAM_BOOL,
|
|
|
|
* 'qtagids' => [PARAM_INT], (array of integers)
|
|
|
|
* 'qbs1' => PARAM_TEXT,
|
|
|
|
* 'qbs2' => PARAM_TEXT,
|
|
|
|
* 'qbs3' => PARAM_TEXT,
|
|
|
|
* ... and more qbs keys up to question_bank_view::MAX_SORTS ...
|
|
|
|
* ];
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
* @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
|
|
|
|
*/
|
|
|
|
function question_build_edit_resources($edittab, $baseurl, $params) {
|
|
|
|
global $DB, $PAGE, $CFG;
|
|
|
|
|
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,
|
|
|
|
'qbshowtext' => PARAM_BOOL
|
|
|
|
];
|
|
|
|
|
|
|
|
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){
|
|
|
|
$contexts = new question_edit_contexts($thiscontext);
|
|
|
|
$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.
|
|
|
|
for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2018-02-13 11:25:52 +08:00
|
|
|
$pagevars['qperpage'] = question_set_or_get_user_preference(
|
2018-02-02 03:32:55 +00:00
|
|
|
'qperpage', $qperpage, DEFAULT_QUESTIONS_PER_PAGE, $thispageurl);
|
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]))) {
|
2011-02-23 18:53:50 +00:00
|
|
|
print_error('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))) {
|
2008-05-21 07:53:23 +00:00
|
|
|
print_error('invalidcourseid');
|
2007-08-09 21:51:09 +00:00
|
|
|
}
|
|
|
|
require_course_login($course, true, $cm);
|
|
|
|
|
|
|
|
} else {
|
2008-05-21 07:53:23 +00:00
|
|
|
print_error('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').
|
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) {
|
2011-02-10 20:44:47 +00:00
|
|
|
global $CFG, $PAGE, $OUTPUT;
|
2012-08-06 21:51:11 +02:00
|
|
|
|
2016-09-01 17:30:46 +08:00
|
|
|
$chooser = core_question\output\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.
|
2009-02-25 07:14:03 +00:00
|
|
|
*/
|
|
|
|
function create_new_question_button($categoryid, $params, $caption, $tooltip = '', $disabled = false) {
|
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;
|
2010-01-16 15:39:56 +00:00
|
|
|
$url = new moodle_url('/question/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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-04 11:57:52 +00:00
|
|
|
|