2009-05-26 03:23:32 +00:00
|
|
|
<?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/>.
|
|
|
|
|
2006-02-24 10:21:40 +00:00
|
|
|
/**
|
2006-03-20 20:45:55 +00:00
|
|
|
* Code for handling and processing questions
|
|
|
|
*
|
|
|
|
* This is code that is module independent, i.e., can be used by any module that
|
|
|
|
* uses questions, like quiz, lesson, ..
|
|
|
|
* This script also loads the questiontype classes
|
|
|
|
* Code for handling the editing of questions is in {@link question/editlib.php}
|
|
|
|
*
|
|
|
|
* TODO: separate those functions which form part of the API
|
|
|
|
* from the helper functions.
|
|
|
|
*
|
2010-12-23 08:41:01 +00:00
|
|
|
* @package moodlecore
|
|
|
|
* @subpackage questionbank
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2006-03-20 20:45:55 +00:00
|
|
|
*/
|
2008-05-26 11:39:51 +00:00
|
|
|
|
2008-09-23 09:52:55 +00:00
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
require_once($CFG->dirroot . '/question/engine/lib.php');
|
|
|
|
require_once($CFG->dirroot . '/question/type/questiontype.php');
|
2008-09-23 09:52:55 +00:00
|
|
|
|
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
2008-07-15 15:30:39 +00:00
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
/// CONSTANTS ///////////////////////////////////
|
2006-02-24 10:25:16 +00:00
|
|
|
|
|
|
|
/**#@+
|
2006-08-15 21:25:38 +00:00
|
|
|
* The core question types.
|
2006-03-20 20:45:55 +00:00
|
|
|
*/
|
2006-03-21 23:08:36 +00:00
|
|
|
define("SHORTANSWER", "shortanswer");
|
|
|
|
define("TRUEFALSE", "truefalse");
|
|
|
|
define("MULTICHOICE", "multichoice");
|
|
|
|
define("RANDOM", "random");
|
|
|
|
define("MATCH", "match");
|
|
|
|
define("RANDOMSAMATCH", "randomsamatch");
|
|
|
|
define("DESCRIPTION", "description");
|
|
|
|
define("NUMERICAL", "numerical");
|
|
|
|
define("MULTIANSWER", "multianswer");
|
|
|
|
define("CALCULATED", "calculated");
|
|
|
|
define("ESSAY", "essay");
|
2006-02-24 10:25:16 +00:00
|
|
|
/**#@-*/
|
|
|
|
|
2006-03-20 20:45:55 +00:00
|
|
|
/**
|
|
|
|
* Constant determines the number of answer boxes supplied in the editing
|
|
|
|
* form for multiple choice and similar question types.
|
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
define("QUESTION_NUMANS", 10);
|
2006-02-24 10:25:16 +00:00
|
|
|
|
2007-01-07 12:46:47 +00:00
|
|
|
/**
|
|
|
|
* Constant determines the number of answer boxes supplied in the editing
|
|
|
|
* form for multiple choice and similar question types to start with, with
|
|
|
|
* the option of adding QUESTION_NUMANS_ADD more answers.
|
|
|
|
*/
|
|
|
|
define("QUESTION_NUMANS_START", 3);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constant determines the number of answer boxes to add in the editing
|
|
|
|
* form for multiple choice and similar question types when the user presses
|
|
|
|
* 'add form fields button'.
|
|
|
|
*/
|
|
|
|
define("QUESTION_NUMANS_ADD", 3);
|
|
|
|
|
2006-08-11 14:59:18 +00:00
|
|
|
/**
|
|
|
|
* The options used when popping up a question preview window in Javascript.
|
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
define('QUESTION_PREVIEW_POPUP_OPTIONS', 'scrollbars=yes,resizable=yes,width=800,height=600');
|
2006-03-20 20:45:55 +00:00
|
|
|
|
2009-05-26 03:23:32 +00:00
|
|
|
/**
|
2010-12-23 08:41:01 +00:00
|
|
|
* @global array holding question type objects
|
|
|
|
* @deprecated
|
2009-05-26 03:23:32 +00:00
|
|
|
*/
|
2008-11-28 06:07:11 +00:00
|
|
|
global $QTYPES;
|
2010-12-23 08:41:01 +00:00
|
|
|
$QTYPES = question_bank::get_all_qtypes();
|
|
|
|
function question_register_questiontype() {
|
|
|
|
// TODO kill this.
|
2006-08-15 21:25:38 +00:00
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
// TODO kill this.
|
|
|
|
class default_questiontype {
|
|
|
|
function plugin_dir() {
|
|
|
|
return '';
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-28 12:53:02 +00:00
|
|
|
/**
|
|
|
|
* An array of question type names translated to the user's language, suitable for use when
|
|
|
|
* creating a drop-down menu of options.
|
|
|
|
*
|
|
|
|
* Long-time Moodle programmers will realise that this replaces the old $QTYPE_MENU array.
|
|
|
|
* The array returned will only hold the names of all the question types that the user should
|
|
|
|
* be able to create directly. Some internal question types like random questions are excluded.
|
2008-06-12 09:15:16 +00:00
|
|
|
*
|
2008-02-28 12:53:02 +00:00
|
|
|
* @return array an array of question type names translated to the user's language.
|
|
|
|
*/
|
|
|
|
function question_type_menu() {
|
2009-03-03 05:13:53 +00:00
|
|
|
static $menuoptions = null;
|
|
|
|
if (is_null($menuoptions)) {
|
2009-03-03 07:47:32 +00:00
|
|
|
$config = get_config('question');
|
2009-03-03 05:13:53 +00:00
|
|
|
$menuoptions = array();
|
2010-12-23 08:41:01 +00:00
|
|
|
foreach (question_bank::get_all_qtypes() as $name => $qtype) {
|
2008-02-28 12:53:02 +00:00
|
|
|
$menuname = $qtype->menu_name();
|
2009-03-03 07:47:32 +00:00
|
|
|
$enabledvar = $name . '_disabled';
|
|
|
|
if ($menuname && !isset($config->$enabledvar)) {
|
2009-03-03 05:13:53 +00:00
|
|
|
$menuoptions[$name] = $menuname;
|
2008-02-28 12:53:02 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-03 07:47:32 +00:00
|
|
|
|
|
|
|
$menuoptions = question_sort_qtype_array($menuoptions, $config);
|
2008-02-28 12:53:02 +00:00
|
|
|
}
|
2009-03-03 05:13:53 +00:00
|
|
|
return $menuoptions;
|
2008-02-28 12:53:02 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 07:47:32 +00:00
|
|
|
/**
|
|
|
|
* Sort an array of question type names according to the question type sort order stored in
|
|
|
|
* config_plugins. Entries for which there is no xxx_sortorder defined will go
|
2010-09-05 13:00:47 +00:00
|
|
|
* at the end, sorted according to textlib_get_instance()->asort($inarray).
|
2009-03-03 07:47:32 +00:00
|
|
|
* @param $inarray an array $qtype => $QTYPES[$qtype]->local_name().
|
|
|
|
* @param $config get_config('question'), if you happen to have it around, to save one DB query.
|
|
|
|
* @return array the sorted version of $inarray.
|
|
|
|
*/
|
|
|
|
function question_sort_qtype_array($inarray, $config = null) {
|
|
|
|
if (is_null($config)) {
|
|
|
|
$config = get_config('question');
|
|
|
|
}
|
|
|
|
|
|
|
|
$sortorder = array();
|
|
|
|
foreach ($inarray as $name => $notused) {
|
|
|
|
$sortvar = $name . '_sortorder';
|
|
|
|
if (isset($config->$sortvar)) {
|
|
|
|
$sortorder[$config->$sortvar] = $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ksort($sortorder);
|
|
|
|
$outarray = array();
|
|
|
|
foreach ($sortorder as $name) {
|
|
|
|
$outarray[$name] = $inarray[$name];
|
|
|
|
unset($inarray[$name]);
|
|
|
|
}
|
2010-09-05 13:00:47 +00:00
|
|
|
textlib_get_instance()->asort($inarray);
|
2009-03-03 07:47:32 +00:00
|
|
|
return array_merge($outarray, $inarray);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move one question type in a list of question types. If you try to move one element
|
|
|
|
* off of the end, nothing will change.
|
2009-11-01 11:31:16 +00:00
|
|
|
*
|
2009-03-03 07:47:32 +00:00
|
|
|
* @param array $sortedqtypes An array $qtype => anything.
|
|
|
|
* @param string $tomove one of the keys from $sortedqtypes
|
|
|
|
* @param integer $direction +1 or -1
|
|
|
|
* @return array an array $index => $qtype, with $index from 0 to n in order, and
|
|
|
|
* the $qtypes in the same order as $sortedqtypes, except that $tomove will
|
|
|
|
* have been moved one place.
|
|
|
|
*/
|
|
|
|
function question_reorder_qtypes($sortedqtypes, $tomove, $direction) {
|
|
|
|
$neworder = array_keys($sortedqtypes);
|
|
|
|
// Find the element to move.
|
|
|
|
$key = array_search($tomove, $neworder);
|
|
|
|
if ($key === false) {
|
|
|
|
return $neworder;
|
|
|
|
}
|
|
|
|
// Work out the other index.
|
|
|
|
$otherkey = $key + $direction;
|
|
|
|
if (!isset($neworder[$otherkey])) {
|
|
|
|
return $neworder;
|
|
|
|
}
|
|
|
|
// Do the swap.
|
|
|
|
$swap = $neworder[$otherkey];
|
|
|
|
$neworder[$otherkey] = $neworder[$key];
|
|
|
|
$neworder[$key] = $swap;
|
|
|
|
return $neworder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a new question type order to the config_plugins table.
|
2009-05-26 03:23:32 +00:00
|
|
|
* @global object
|
2009-03-03 07:47:32 +00:00
|
|
|
* @param $neworder An arra $index => $qtype. Indices should start at 0 and be in order.
|
|
|
|
* @param $config get_config('question'), if you happen to have it around, to save one DB query.
|
|
|
|
*/
|
|
|
|
function question_save_qtype_order($neworder, $config = null) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
if (is_null($config)) {
|
|
|
|
$config = get_config('question');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($neworder as $index => $qtype) {
|
|
|
|
$sortvar = $qtype . '_sortorder';
|
|
|
|
if (!isset($config->$sortvar) || $config->$sortvar != $index + 1) {
|
|
|
|
set_config($sortvar, $index + 1, 'question');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-24 10:21:40 +00:00
|
|
|
/// FUNCTIONS //////////////////////////////////////////////////////
|
|
|
|
|
2006-03-20 23:04:22 +00:00
|
|
|
/**
|
2006-03-21 09:06:34 +00:00
|
|
|
* Returns an array of names of activity modules that use this question
|
2006-03-20 23:04:22 +00:00
|
|
|
*
|
2010-12-23 08:41:01 +00:00
|
|
|
* @deprecated since Moodle 2.1. Use {@link questions_in_use} instead.
|
|
|
|
|
2006-03-21 09:06:34 +00:00
|
|
|
* @param object $questionid
|
|
|
|
* @return array of strings
|
2006-03-20 23:04:22 +00:00
|
|
|
*/
|
2006-03-21 09:06:34 +00:00
|
|
|
function question_list_instances($questionid) {
|
2010-12-23 08:41:01 +00:00
|
|
|
throw new coding_exception('question_list_instances has been deprectated. Please use questions_in_use instead.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $questionids of question ids.
|
|
|
|
* @return boolean whether any of these questions are being used by any part of Moodle.
|
|
|
|
*/
|
|
|
|
function questions_in_use($questionids) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (question_engine::questions_in_use($questionids)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (get_plugin_list('mod') as $module => $path) {
|
|
|
|
$lib = $path . '/lib.php';
|
|
|
|
if (is_readable($lib)) {
|
|
|
|
include_once($lib);
|
|
|
|
|
|
|
|
$fn = $module . '_questions_in_use';
|
2008-05-12 17:29:56 +00:00
|
|
|
if (function_exists($fn)) {
|
2010-12-23 08:41:01 +00:00
|
|
|
if ($fn($questionids)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Fallback for legacy modules.
|
|
|
|
$fn = $module . '_question_list_instances';
|
|
|
|
if (function_exists($fn)) {
|
|
|
|
foreach ($questionids as $questionid) {
|
|
|
|
$instances = $fn($questionid);
|
|
|
|
if (!empty($instances)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-12 17:29:56 +00:00
|
|
|
}
|
2006-03-20 23:04:22 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
|
|
|
|
return false;
|
2006-03-20 23:04:22 +00:00
|
|
|
}
|
2006-02-24 10:21:40 +00:00
|
|
|
|
2008-05-13 21:52:38 +00:00
|
|
|
/**
|
|
|
|
* Determine whether there arey any questions belonging to this context, that is whether any of its
|
|
|
|
* question categories contain any questions. This will return true even if all the questions are
|
|
|
|
* hidden.
|
|
|
|
*
|
|
|
|
* @param mixed $context either a context object, or a context id.
|
|
|
|
* @return boolean whether any of the question categories beloning to this context have
|
|
|
|
* any questions in them.
|
|
|
|
*/
|
|
|
|
function question_context_has_any_questions($context) {
|
2008-06-09 12:16:54 +00:00
|
|
|
global $DB;
|
2008-05-13 21:52:38 +00:00
|
|
|
if (is_object($context)) {
|
|
|
|
$contextid = $context->id;
|
|
|
|
} else if (is_numeric($context)) {
|
|
|
|
$contextid = $context;
|
|
|
|
} else {
|
|
|
|
print_error('invalidcontextinhasanyquestions', 'question');
|
|
|
|
}
|
2008-06-09 12:16:54 +00:00
|
|
|
return $DB->record_exists_sql("SELECT *
|
|
|
|
FROM {question} q
|
|
|
|
JOIN {question_categories} qc ON qc.id = q.category
|
|
|
|
WHERE qc.contextid = ? AND q.parent = 0", array($contextid));
|
2008-05-13 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2007-01-07 12:46:47 +00:00
|
|
|
/**
|
2006-03-22 14:43:55 +00:00
|
|
|
* Returns list of 'allowed' grades for grade selection
|
|
|
|
* formatted suitably for dropdown box function
|
|
|
|
* @return object ->gradeoptionsfull full array ->gradeoptions +ve only
|
|
|
|
*/
|
|
|
|
function get_grade_options() {
|
2008-09-26 03:14:05 +00:00
|
|
|
// define basic array of grades. This list comprises all fractions of the form:
|
|
|
|
// a. p/q for q <= 6, 0 <= p <= q
|
|
|
|
// b. p/10 for 0 <= p <= 10
|
|
|
|
// c. 1/q for 1 <= q <= 10
|
|
|
|
// d. 1/20
|
2006-03-22 14:43:55 +00:00
|
|
|
$grades = array(
|
2008-09-26 03:14:05 +00:00
|
|
|
1.0000000,
|
|
|
|
0.9000000,
|
|
|
|
0.8333333,
|
|
|
|
0.8000000,
|
|
|
|
0.7500000,
|
|
|
|
0.7000000,
|
|
|
|
0.6666667,
|
|
|
|
0.6000000,
|
|
|
|
0.5000000,
|
|
|
|
0.4000000,
|
|
|
|
0.3333333,
|
|
|
|
0.3000000,
|
|
|
|
0.2500000,
|
|
|
|
0.2000000,
|
|
|
|
0.1666667,
|
|
|
|
0.1428571,
|
|
|
|
0.1250000,
|
|
|
|
0.1111111,
|
|
|
|
0.1000000,
|
|
|
|
0.0500000,
|
|
|
|
0.0000000);
|
2006-03-22 14:43:55 +00:00
|
|
|
|
|
|
|
// iterate through grades generating full range of options
|
|
|
|
$gradeoptionsfull = array();
|
|
|
|
$gradeoptions = array();
|
|
|
|
foreach ($grades as $grade) {
|
|
|
|
$percentage = 100 * $grade;
|
2010-12-23 08:41:01 +00:00
|
|
|
$gradeoptions["$grade"] = $percentage . '%';
|
|
|
|
$gradeoptionsfull["$grade"] = $percentage . '%';
|
|
|
|
$gradeoptionsfull['' . (-$grade)] = (-$percentage) . '%';
|
2006-03-22 14:43:55 +00:00
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
$gradeoptionsfull['0'] = $gradeoptions['0'] = get_string('none');
|
2006-03-22 14:43:55 +00:00
|
|
|
|
|
|
|
// sort lists
|
|
|
|
arsort($gradeoptions, SORT_NUMERIC);
|
|
|
|
arsort($gradeoptionsfull, SORT_NUMERIC);
|
|
|
|
|
|
|
|
// construct return object
|
|
|
|
$grades = new stdClass;
|
|
|
|
$grades->gradeoptions = $gradeoptions;
|
|
|
|
$grades->gradeoptionsfull = $gradeoptionsfull;
|
|
|
|
|
|
|
|
return $grades;
|
|
|
|
}
|
|
|
|
|
2006-03-22 16:27:46 +00:00
|
|
|
/**
|
|
|
|
* match grade options
|
|
|
|
* if no match return error or match nearest
|
|
|
|
* @param array $gradeoptionsfull list of valid options
|
|
|
|
* @param int $grade grade to be tested
|
|
|
|
* @param string $matchgrades 'error' or 'nearest'
|
|
|
|
* @return mixed either 'fixed' value or false if erro
|
|
|
|
*/
|
|
|
|
function match_grade_options($gradeoptionsfull, $grade, $matchgrades='error') {
|
|
|
|
// if we just need an error...
|
|
|
|
if ($matchgrades=='error') {
|
|
|
|
foreach($gradeoptionsfull as $value => $option) {
|
2006-04-13 10:29:20 +00:00
|
|
|
// slightly fuzzy test, never check floats for equality :-)
|
|
|
|
if (abs($grade-$value)<0.00001) {
|
2006-03-22 16:27:46 +00:00
|
|
|
return $grade;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// didn't find a match so that's an error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// work out nearest value
|
|
|
|
else if ($matchgrades=='nearest') {
|
|
|
|
$hownear = array();
|
|
|
|
foreach($gradeoptionsfull as $value => $option) {
|
|
|
|
if ($grade==$value) {
|
|
|
|
return $grade;
|
|
|
|
}
|
|
|
|
$hownear[ $value ] = abs( $grade - $value );
|
|
|
|
}
|
|
|
|
// reverse sort list of deltas and grab the last (smallest)
|
|
|
|
asort( $hownear, SORT_NUMERIC );
|
|
|
|
reset( $hownear );
|
|
|
|
return key( $hownear );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
2007-01-07 12:46:47 +00:00
|
|
|
}
|
2006-03-22 16:27:46 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 09:06:34 +00:00
|
|
|
/**
|
2010-12-23 08:41:01 +00:00
|
|
|
* @deprecated Since Moodle 2.1. Use {@link question_category_in_use} instead.
|
|
|
|
* @param integer $categoryid a question category id.
|
|
|
|
* @param boolean $recursive whether to check child categories too.
|
|
|
|
* @return boolean whether any question in this category is in use.
|
2006-03-21 09:06:34 +00:00
|
|
|
*/
|
|
|
|
function question_category_isused($categoryid, $recursive = false) {
|
2010-12-23 08:41:01 +00:00
|
|
|
throw new coding_exception('question_category_isused has been deprectated. Please use question_category_in_use instead.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests whether any question in a category is used by any part of Moodle.
|
|
|
|
*
|
|
|
|
* @param integer $categoryid a question category id.
|
|
|
|
* @param boolean $recursive whether to check child categories too.
|
|
|
|
* @return boolean whether any question in this category is in use.
|
|
|
|
*/
|
|
|
|
function question_category_in_use($categoryid, $recursive = false) {
|
2008-06-09 12:16:54 +00:00
|
|
|
global $DB;
|
2006-03-21 09:06:34 +00:00
|
|
|
|
|
|
|
//Look at each question in the category
|
2010-12-23 08:41:01 +00:00
|
|
|
if ($questions = $DB->get_records_menu('question', array('category' => $categoryid), '', 'id,1')) {
|
|
|
|
if (questions_in_use(array_keys($questions))) {
|
|
|
|
return true;
|
2006-03-21 09:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
if (!$recursive) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-21 09:06:34 +00:00
|
|
|
|
|
|
|
//Look under child categories recursively
|
2010-12-23 08:41:01 +00:00
|
|
|
if ($children = $DB->get_records('question_categories', array('parent' => $categoryid), '', 'id,1')) {
|
|
|
|
foreach ($children as $child) {
|
|
|
|
if (question_category_in_use($child->id, $recursive)) {
|
|
|
|
return true;
|
2006-03-21 09:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-02-24 10:21:40 +00:00
|
|
|
/**
|
2006-03-20 20:45:55 +00:00
|
|
|
* Deletes question and all associated data from the database
|
|
|
|
*
|
2006-03-20 23:04:22 +00:00
|
|
|
* It will not delete a question if it is used by an activity module
|
2006-03-20 20:45:55 +00:00
|
|
|
* @param object $question The question being deleted
|
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
function question_delete_question($questionid) {
|
2008-06-09 12:16:54 +00:00
|
|
|
global $QTYPES, $DB;
|
2007-01-07 12:46:47 +00:00
|
|
|
|
2010-11-12 11:29:53 +00:00
|
|
|
$question = $DB->get_record_sql('
|
|
|
|
SELECT q.*, qc.contextid
|
|
|
|
FROM {question} q
|
|
|
|
JOIN {question_categories} qc ON qc.id = q.category
|
|
|
|
WHERE q.id = ?', array($questionid));
|
|
|
|
if (!$question) {
|
2008-12-11 05:31:33 +00:00
|
|
|
// In some situations, for example if this was a child of a
|
|
|
|
// Cloze question that was previously deleted, the question may already
|
|
|
|
// have gone. In this case, just do nothing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-03-20 23:04:22 +00:00
|
|
|
// Do not delete a question if it is used by an activity module
|
2010-12-23 08:41:01 +00:00
|
|
|
if (questions_in_use(array($questionid))) {
|
2006-03-20 23:04:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
// Check permissions.
|
2007-08-09 21:50:59 +00:00
|
|
|
question_require_capability_on($question, 'edit');
|
2007-01-07 12:46:47 +00:00
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
$dm = new question_engine_data_mapper();
|
|
|
|
$dm->delete_previews($questionid);
|
2006-03-22 18:27:28 +00:00
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
// delete questiontype-specific data
|
|
|
|
question_bank::get_qtype($question->qtype, false)->delete_question(
|
|
|
|
$questionid, $question->contextid);
|
2006-03-20 23:04:22 +00:00
|
|
|
|
|
|
|
// Now recursively delete all child questions
|
2009-02-13 06:27:08 +00:00
|
|
|
if ($children = $DB->get_records('question', array('parent' => $questionid), '', 'id,qtype')) {
|
2006-02-24 10:21:40 +00:00
|
|
|
foreach ($children as $child) {
|
2006-08-16 16:17:18 +00:00
|
|
|
if ($child->id != $questionid) {
|
|
|
|
delete_question($child->id);
|
|
|
|
}
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
|
|
|
}
|
2007-01-07 12:46:47 +00:00
|
|
|
|
2006-03-20 23:04:22 +00:00
|
|
|
// Finally delete the question record itself
|
2010-12-23 08:41:01 +00:00
|
|
|
$DB->delete_records('question', array('id' => $questionid));
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 09:06:34 +00:00
|
|
|
/**
|
2007-08-09 21:50:59 +00:00
|
|
|
* All question categories and their questions are deleted for this course.
|
2006-03-21 09:06:34 +00:00
|
|
|
*
|
2007-08-09 21:50:59 +00:00
|
|
|
* @param object $mod an object representing the activity
|
2006-03-21 09:06:34 +00:00
|
|
|
* @param boolean $feedback to specify if the process must output a summary of its work
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function question_delete_course($course, $feedback=true) {
|
2009-08-20 08:50:50 +00:00
|
|
|
global $DB, $OUTPUT;
|
2008-06-09 12:16:54 +00:00
|
|
|
|
2006-03-21 09:06:34 +00:00
|
|
|
//To store feedback to be showed at the end of the process
|
|
|
|
$feedbackdata = array();
|
|
|
|
|
|
|
|
//Cache some strings
|
|
|
|
$strcatdeleted = get_string('unusedcategorydeleted', 'quiz');
|
2007-08-09 21:50:59 +00:00
|
|
|
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
|
2010-11-08 15:55:45 +00:00
|
|
|
$categoriescourse = $DB->get_records('question_categories', array('contextid'=>$coursecontext->id), 'parent', 'id, parent, name, contextid');
|
2006-03-21 09:06:34 +00:00
|
|
|
|
2007-08-09 21:50:59 +00:00
|
|
|
if ($categoriescourse) {
|
2006-03-21 09:06:34 +00:00
|
|
|
|
|
|
|
//Sort categories following their tree (parent-child) relationships
|
2007-08-09 21:50:59 +00:00
|
|
|
//this will make the feedback more readable
|
|
|
|
$categoriescourse = sort_categories_by_tree($categoriescourse);
|
|
|
|
|
|
|
|
foreach ($categoriescourse as $category) {
|
|
|
|
|
|
|
|
//Delete it completely (questions and category itself)
|
|
|
|
//deleting questions
|
2009-02-13 06:27:08 +00:00
|
|
|
if ($questions = $DB->get_records('question', array('category' => $category->id), '', 'id,qtype')) {
|
2007-08-09 21:50:59 +00:00
|
|
|
foreach ($questions as $question) {
|
|
|
|
delete_question($question->id);
|
2006-03-21 09:06:34 +00:00
|
|
|
}
|
2008-06-09 12:16:54 +00:00
|
|
|
$DB->delete_records("question", array("category"=>$category->id));
|
2007-08-09 21:50:59 +00:00
|
|
|
}
|
|
|
|
//delete the category
|
2008-06-09 12:16:54 +00:00
|
|
|
$DB->delete_records('question_categories', array('id'=>$category->id));
|
2006-03-21 09:06:34 +00:00
|
|
|
|
2007-08-09 21:50:59 +00:00
|
|
|
//Fill feedback
|
|
|
|
$feedbackdata[] = array($category->name, $strcatdeleted);
|
|
|
|
}
|
|
|
|
//Inform about changes performed if feedback is enabled
|
|
|
|
if ($feedback) {
|
2009-08-20 08:50:50 +00:00
|
|
|
$table = new html_table();
|
2007-08-09 21:50:59 +00:00
|
|
|
$table->head = array(get_string('category','quiz'), get_string('action'));
|
|
|
|
$table->data = $feedbackdata;
|
2010-03-20 22:15:54 +00:00
|
|
|
echo html_writer::table($table);
|
2007-08-09 21:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2006-03-21 09:06:34 +00:00
|
|
|
|
2008-05-13 21:52:38 +00:00
|
|
|
/**
|
|
|
|
* Category is about to be deleted,
|
|
|
|
* 1/ All question categories and their questions are deleted for this course category.
|
|
|
|
* 2/ All questions are moved to new category
|
|
|
|
*
|
|
|
|
* @param object $category course category object
|
|
|
|
* @param object $newcategory empty means everything deleted, otherwise id of category where content moved
|
|
|
|
* @param boolean $feedback to specify if the process must output a summary of its work
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function question_delete_course_category($category, $newcategory, $feedback=true) {
|
2009-08-18 05:20:12 +00:00
|
|
|
global $DB, $OUTPUT;
|
2008-06-01 15:52:12 +00:00
|
|
|
|
2008-05-13 21:52:38 +00:00
|
|
|
$context = get_context_instance(CONTEXT_COURSECAT, $category->id);
|
|
|
|
if (empty($newcategory)) {
|
|
|
|
$feedbackdata = array(); // To store feedback to be showed at the end of the process
|
|
|
|
$rescueqcategory = null; // See the code around the call to question_save_from_deletion.
|
|
|
|
$strcatdeleted = get_string('unusedcategorydeleted', 'quiz');
|
|
|
|
|
|
|
|
// Loop over question categories.
|
2008-06-09 12:16:54 +00:00
|
|
|
if ($categories = $DB->get_records('question_categories', array('contextid'=>$context->id), 'parent', 'id, parent, name')) {
|
2008-05-13 21:52:38 +00:00
|
|
|
foreach ($categories as $category) {
|
2008-06-12 09:15:16 +00:00
|
|
|
|
2008-05-13 21:52:38 +00:00
|
|
|
// Deal with any questions in the category.
|
2009-02-13 06:27:08 +00:00
|
|
|
if ($questions = $DB->get_records('question', array('category' => $category->id), '', 'id,qtype')) {
|
2008-05-13 21:52:38 +00:00
|
|
|
|
|
|
|
// Try to delete each question.
|
|
|
|
foreach ($questions as $question) {
|
|
|
|
delete_question($question->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if there were any questions that were kept because they are
|
|
|
|
// still in use somehow, even though quizzes in courses in this category will
|
|
|
|
// already have been deteted. This could happen, for example, if questions are
|
|
|
|
// added to a course, and then that course is moved to another category (MDL-14802).
|
2008-06-01 15:52:12 +00:00
|
|
|
$questionids = $DB->get_records_menu('question', array('category'=>$category->id), '', 'id,1');
|
2008-05-13 21:52:38 +00:00
|
|
|
if (!empty($questionids)) {
|
2010-11-11 21:54:22 +00:00
|
|
|
if (!$rescueqcategory = question_save_from_deletion(array_keys($questionids),
|
2008-05-13 21:52:38 +00:00
|
|
|
get_parent_contextid($context), print_context_name($context), $rescueqcategory)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$feedbackdata[] = array($category->name, get_string('questionsmovedto', 'question', $rescueqcategory->name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now delete the category.
|
2008-06-09 12:16:54 +00:00
|
|
|
if (!$DB->delete_records('question_categories', array('id'=>$category->id))) {
|
2008-05-13 21:52:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$feedbackdata[] = array($category->name, $strcatdeleted);
|
|
|
|
|
|
|
|
} // End loop over categories.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output feedback if requested.
|
|
|
|
if ($feedback and $feedbackdata) {
|
2009-08-20 08:50:50 +00:00
|
|
|
$table = new html_table();
|
2008-05-13 21:52:38 +00:00
|
|
|
$table->head = array(get_string('questioncategory','question'), get_string('action'));
|
|
|
|
$table->data = $feedbackdata;
|
2010-03-20 22:15:54 +00:00
|
|
|
echo html_writer::table($table);
|
2008-05-13 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Move question categories ot the new context.
|
|
|
|
if (!$newcontext = get_context_instance(CONTEXT_COURSECAT, $newcategory->id)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-09-03 18:14:55 +00:00
|
|
|
$DB->set_field('question_categories', 'contextid', $newcontext->id, array('contextid'=>$context->id));
|
2008-05-13 21:52:38 +00:00
|
|
|
if ($feedback) {
|
|
|
|
$a = new stdClass;
|
|
|
|
$a->oldplace = print_context_name($context);
|
|
|
|
$a->newplace = print_context_name($newcontext);
|
2009-08-18 05:20:12 +00:00
|
|
|
echo $OUTPUT->notification(get_string('movedquestionsandcategories', 'question', $a), 'notifysuccess');
|
2008-05-13 21:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param string $questionids list of questionids
|
|
|
|
* @param object $newcontext the context to create the saved category in.
|
2008-06-12 09:15:16 +00:00
|
|
|
* @param string $oldplace a textual description of the think being deleted, e.g. from get_context_name
|
2008-05-13 21:52:38 +00:00
|
|
|
* @param object $newcategory
|
2008-06-12 09:15:16 +00:00
|
|
|
* @return mixed false on
|
2008-05-13 21:52:38 +00:00
|
|
|
*/
|
|
|
|
function question_save_from_deletion($questionids, $newcontextid, $oldplace, $newcategory = null) {
|
2008-06-09 12:16:54 +00:00
|
|
|
global $DB;
|
|
|
|
|
2008-05-13 21:52:38 +00:00
|
|
|
// Make a category in the parent context to move the questions to.
|
|
|
|
if (is_null($newcategory)) {
|
2010-09-21 08:07:44 +00:00
|
|
|
$newcategory = new stdClass();
|
2008-05-13 21:52:38 +00:00
|
|
|
$newcategory->parent = 0;
|
|
|
|
$newcategory->contextid = $newcontextid;
|
2008-06-09 12:16:54 +00:00
|
|
|
$newcategory->name = get_string('questionsrescuedfrom', 'question', $oldplace);
|
|
|
|
$newcategory->info = get_string('questionsrescuedfrominfo', 'question', $oldplace);
|
2008-05-13 21:52:38 +00:00
|
|
|
$newcategory->sortorder = 999;
|
|
|
|
$newcategory->stamp = make_unique_id_code();
|
2009-06-13 18:16:08 +00:00
|
|
|
$newcategory->id = $DB->insert_record('question_categories', $newcategory);
|
2008-05-13 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move any remaining questions to the 'saved' category.
|
|
|
|
if (!question_move_questions_to_category($questionids, $newcategory->id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $newcategory;
|
|
|
|
}
|
|
|
|
|
2007-08-09 21:50:59 +00:00
|
|
|
/**
|
|
|
|
* All question categories and their questions are deleted for this activity.
|
|
|
|
*
|
|
|
|
* @param object $cm the course module object representing the activity
|
|
|
|
* @param boolean $feedback to specify if the process must output a summary of its work
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function question_delete_activity($cm, $feedback=true) {
|
2009-08-20 08:50:50 +00:00
|
|
|
global $DB, $OUTPUT;
|
2008-06-09 12:16:54 +00:00
|
|
|
|
2007-08-09 21:50:59 +00:00
|
|
|
//To store feedback to be showed at the end of the process
|
|
|
|
$feedbackdata = array();
|
2006-03-21 09:06:34 +00:00
|
|
|
|
2007-08-09 21:50:59 +00:00
|
|
|
//Cache some strings
|
|
|
|
$strcatdeleted = get_string('unusedcategorydeleted', 'quiz');
|
|
|
|
$modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
|
2010-11-08 15:55:45 +00:00
|
|
|
if ($categoriesmods = $DB->get_records('question_categories', array('contextid'=>$modcontext->id), 'parent', 'id, parent, name, contextid')){
|
2007-08-09 21:50:59 +00:00
|
|
|
//Sort categories following their tree (parent-child) relationships
|
|
|
|
//this will make the feedback more readable
|
|
|
|
$categoriesmods = sort_categories_by_tree($categoriesmods);
|
2006-03-21 09:06:34 +00:00
|
|
|
|
2007-08-09 21:50:59 +00:00
|
|
|
foreach ($categoriesmods as $category) {
|
2006-03-21 09:06:34 +00:00
|
|
|
|
2007-08-09 21:50:59 +00:00
|
|
|
//Delete it completely (questions and category itself)
|
|
|
|
//deleting questions
|
2009-02-13 06:27:08 +00:00
|
|
|
if ($questions = $DB->get_records('question', array('category' => $category->id), '', 'id,qtype')) {
|
2007-08-09 21:50:59 +00:00
|
|
|
foreach ($questions as $question) {
|
|
|
|
delete_question($question->id);
|
|
|
|
}
|
2008-06-09 12:16:54 +00:00
|
|
|
$DB->delete_records("question", array("category"=>$category->id));
|
2006-03-21 09:06:34 +00:00
|
|
|
}
|
2007-08-09 21:50:59 +00:00
|
|
|
//delete the category
|
2008-06-09 12:16:54 +00:00
|
|
|
$DB->delete_records('question_categories', array('id'=>$category->id));
|
2007-08-09 21:50:59 +00:00
|
|
|
|
|
|
|
//Fill feedback
|
|
|
|
$feedbackdata[] = array($category->name, $strcatdeleted);
|
2006-03-21 09:06:34 +00:00
|
|
|
}
|
|
|
|
//Inform about changes performed if feedback is enabled
|
|
|
|
if ($feedback) {
|
2009-08-20 08:50:50 +00:00
|
|
|
$table = new html_table();
|
2006-03-21 09:06:34 +00:00
|
|
|
$table->head = array(get_string('category','quiz'), get_string('action'));
|
|
|
|
$table->data = $feedbackdata;
|
2010-03-20 22:15:54 +00:00
|
|
|
echo html_writer::table($table);
|
2006-03-21 09:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2008-05-09 15:05:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function should be considered private to the question bank, it is called from
|
|
|
|
* question/editlib.php question/contextmoveq.php and a few similar places to to the work of
|
|
|
|
* acutally moving questions and associated data. However, callers of this function also have to
|
|
|
|
* do other work, which is why you should not call this method directly from outside the questionbank.
|
|
|
|
*
|
|
|
|
* @param string $questionids a comma-separated list of question ids.
|
2010-08-10 09:56:48 +00:00
|
|
|
* @param integer $newcategoryid the id of the category to move to.
|
2008-05-09 15:05:36 +00:00
|
|
|
*/
|
2010-08-10 09:56:48 +00:00
|
|
|
function question_move_questions_to_category($questionids, $newcategoryid) {
|
|
|
|
global $DB, $QTYPES;
|
2010-09-03 18:14:55 +00:00
|
|
|
|
2010-11-11 21:54:22 +00:00
|
|
|
$newcontextid = $DB->get_field('question_categories', 'contextid',
|
|
|
|
array('id' => $newcategoryid));
|
|
|
|
list($questionidcondition, $params) = $DB->get_in_or_equal($questionids);
|
|
|
|
$questions = $DB->get_records_sql("
|
|
|
|
SELECT q.id, q.qtype, qc.contextid
|
|
|
|
FROM {question} q
|
|
|
|
JOIN {question_categories} qc ON q.category = qc.id
|
|
|
|
WHERE q.id $questionidcondition", $params);
|
|
|
|
foreach ($questions as $question) {
|
|
|
|
if ($newcontextid != $question->contextid) {
|
|
|
|
$QTYPES[$question->qtype]->move_files($question->id,
|
|
|
|
$question->contextid, $newcontextid);
|
|
|
|
}
|
2010-08-10 09:56:48 +00:00
|
|
|
}
|
|
|
|
|
2008-05-09 15:05:36 +00:00
|
|
|
// Move the questions themselves.
|
2010-11-11 21:54:22 +00:00
|
|
|
$DB->set_field_select('question', 'category', $newcategoryid, "id $questionidcondition", $params);
|
2008-05-09 15:05:36 +00:00
|
|
|
|
|
|
|
// Move any subquestions belonging to them.
|
2010-11-11 21:54:22 +00:00
|
|
|
$DB->set_field_select('question', 'category', $newcategoryid, "parent $questionidcondition", $params);
|
2008-05-09 15:05:36 +00:00
|
|
|
|
|
|
|
// TODO Deal with datasets.
|
|
|
|
|
2010-09-03 18:14:55 +00:00
|
|
|
return true;
|
2008-05-09 15:05:36 +00:00
|
|
|
}
|
|
|
|
|
2010-11-11 21:54:22 +00:00
|
|
|
/**
|
|
|
|
* This function helps move a question cateogry to a new context by moving all
|
|
|
|
* the files belonging to all the questions to the new context.
|
|
|
|
* Also moves subcategories.
|
|
|
|
* @param integer $categoryid the id of the category being moved.
|
|
|
|
* @param integer $oldcontextid the old context id.
|
|
|
|
* @param integer $newcontextid the new context id.
|
|
|
|
*/
|
|
|
|
function question_move_category_to_context($categoryid, $oldcontextid, $newcontextid) {
|
|
|
|
global $DB, $QTYPES;
|
|
|
|
|
|
|
|
$questionids = $DB->get_records_menu('question',
|
|
|
|
array('category' => $categoryid), '', 'id,qtype');
|
|
|
|
foreach ($questionids as $questionid => $qtype) {
|
|
|
|
$QTYPES[$qtype]->move_files($questionid, $oldcontextid, $newcontextid);
|
|
|
|
}
|
|
|
|
|
|
|
|
$subcatids = $DB->get_records_menu('question_categories',
|
|
|
|
array('parent' => $categoryid), '', 'id,1');
|
|
|
|
foreach ($subcatids as $subcatid => $notused) {
|
|
|
|
$DB->set_field('question_categories', 'contextid', $newcontextid, array('id' => $subcatid));
|
|
|
|
question_move_category_to_context($subcatid, $oldcontextid, $newcontextid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
/**
|
|
|
|
* Generate the URL for starting a new preview of a given question with the given options.
|
|
|
|
* @param integer $questionid the question to preview.
|
|
|
|
* @param string $preferredbehaviour the behaviour to use for the preview.
|
|
|
|
* @param float $maxmark the maximum to mark the question out of.
|
|
|
|
* @param question_display_options $displayoptions the display options to use.
|
|
|
|
* @return string the URL.
|
|
|
|
*/
|
|
|
|
function question_preview_url($questionid, $preferredbehaviour, $maxmark, $displayoptions) {
|
|
|
|
return new moodle_url('/question/preview.php', array(
|
|
|
|
'id' => $questionid,
|
|
|
|
'behaviour' => $preferredbehaviour,
|
|
|
|
'maxmark' => $maxmark,
|
|
|
|
'correctness' => $displayoptions->correctness,
|
|
|
|
'marks' => $displayoptions->marks,
|
|
|
|
'markdp' => $displayoptions->markdp,
|
|
|
|
'feedback' => (bool) $displayoptions->feedback,
|
|
|
|
'generalfeedback' => (bool) $displayoptions->generalfeedback,
|
|
|
|
'rightanswer' => (bool) $displayoptions->rightanswer,
|
|
|
|
'history' => (bool) $displayoptions->history));
|
|
|
|
}
|
|
|
|
|
2008-04-04 02:54:20 +00:00
|
|
|
/**
|
2008-07-08 16:33:47 +00:00
|
|
|
* Given a list of ids, load the basic information about a set of questions from the questions table.
|
|
|
|
* The $join and $extrafields arguments can be used together to pull in extra data.
|
|
|
|
* See, for example, the usage in mod/quiz/attemptlib.php, and
|
|
|
|
* read the code below to see how the SQL is assembled. Throws exceptions on error.
|
2008-04-04 02:54:20 +00:00
|
|
|
*
|
2009-05-26 03:23:32 +00:00
|
|
|
* @global object
|
|
|
|
* @global object
|
2008-07-08 16:33:47 +00:00
|
|
|
* @param array $questionids array of question ids.
|
|
|
|
* @param string $extrafields extra SQL code to be added to the query.
|
|
|
|
* @param string $join extra SQL code to be added to the query.
|
|
|
|
* @param array $extraparams values for any placeholders in $join.
|
|
|
|
* You are strongly recommended to use named placeholder.
|
2008-04-04 02:54:20 +00:00
|
|
|
*
|
2008-07-08 16:33:47 +00:00
|
|
|
* @return array partially complete question objects. You need to call get_question_options
|
|
|
|
* on them before they can be properly used.
|
2008-04-04 02:54:20 +00:00
|
|
|
*/
|
2008-07-08 16:33:47 +00:00
|
|
|
function question_preload_questions($questionids, $extrafields = '', $join = '', $extraparams = array()) {
|
2010-12-23 08:41:01 +00:00
|
|
|
global $DB;
|
2008-07-25 14:18:02 +00:00
|
|
|
if (empty($questionids)) {
|
|
|
|
return array();
|
|
|
|
}
|
2008-04-04 02:54:20 +00:00
|
|
|
if ($join) {
|
2008-07-08 16:33:47 +00:00
|
|
|
$join = ' JOIN '.$join;
|
2008-04-04 02:54:20 +00:00
|
|
|
}
|
|
|
|
if ($extrafields) {
|
|
|
|
$extrafields = ', ' . $extrafields;
|
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
list($questionidcondition, $params) = $DB->get_in_or_equal(
|
|
|
|
$questionids, SQL_PARAMS_NAMED, 'qid0000');
|
2008-06-09 12:16:54 +00:00
|
|
|
$sql = 'SELECT q.*' . $extrafields . ' FROM {question} q' . $join .
|
2008-07-08 16:33:47 +00:00
|
|
|
' WHERE q.id ' . $questionidcondition;
|
2008-04-04 02:54:20 +00:00
|
|
|
|
|
|
|
// Load the questions
|
2008-07-08 16:33:47 +00:00
|
|
|
if (!$questions = $DB->get_records_sql($sql, $extraparams + $params)) {
|
2010-12-23 08:41:01 +00:00
|
|
|
return array();
|
2008-04-04 02:54:20 +00:00
|
|
|
}
|
|
|
|
|
2008-07-08 16:33:47 +00:00
|
|
|
foreach ($questions as $question) {
|
|
|
|
$question->_partiallyloaded = true;
|
|
|
|
}
|
|
|
|
|
2008-07-15 15:30:39 +00:00
|
|
|
// Note, a possible optimisation here would be to not load the TEXT fields
|
|
|
|
// (that is, questiontext and generalfeedback) here, and instead load them in
|
|
|
|
// question_load_questions. That would add one DB query, but reduce the amount
|
|
|
|
// of data transferred most of the time. I am not going to do this optimisation
|
|
|
|
// until it is shown to be worthwhile.
|
|
|
|
|
2008-07-08 16:33:47 +00:00
|
|
|
return $questions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a set of questions, given a list of ids. The $join and $extrafields arguments can be used
|
|
|
|
* together to pull in extra data. See, for example, the usage in mod/quiz/attempt.php, and
|
|
|
|
* read the code below to see how the SQL is assembled. Throws exceptions on error.
|
|
|
|
*
|
|
|
|
* @param array $questionids array of question ids.
|
|
|
|
* @param string $extrafields extra SQL code to be added to the query.
|
|
|
|
* @param string $join extra SQL code to be added to the query.
|
|
|
|
* @param array $extraparams values for any placeholders in $join.
|
|
|
|
* You are strongly recommended to use named placeholder.
|
|
|
|
*
|
|
|
|
* @return array question objects.
|
|
|
|
*/
|
|
|
|
function question_load_questions($questionids, $extrafields = '', $join = '') {
|
|
|
|
$questions = question_preload_questions($questionids, $extrafields, $join);
|
|
|
|
|
2008-04-04 02:54:20 +00:00
|
|
|
// Load the question type specific information
|
|
|
|
if (!get_question_options($questions)) {
|
|
|
|
return 'Could not load the question options';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $questions;
|
|
|
|
}
|
|
|
|
|
2006-02-24 10:21:40 +00:00
|
|
|
/**
|
2006-07-18 15:34:24 +00:00
|
|
|
* Private function to factor common code out of get_question_options().
|
2007-01-07 12:46:47 +00:00
|
|
|
*
|
2006-07-18 15:34:24 +00:00
|
|
|
* @param object $question the question to tidy.
|
2009-01-16 08:00:06 +00:00
|
|
|
* @param boolean $loadtags load the question tags from the tags table. Optional, default false.
|
2007-01-07 12:46:47 +00:00
|
|
|
* @return boolean true if successful, else false.
|
2006-07-18 15:34:24 +00:00
|
|
|
*/
|
2010-12-24 14:51:56 +00:00
|
|
|
function _tidy_question($question, $loadtags = false) {
|
2009-01-16 08:00:06 +00:00
|
|
|
global $CFG, $QTYPES;
|
2006-07-18 15:34:24 +00:00
|
|
|
if (!array_key_exists($question->qtype, $QTYPES)) {
|
|
|
|
$question->qtype = 'missingtype';
|
|
|
|
$question->questiontext = '<p>' . get_string('warningmissingtype', 'quiz') . '</p>' . $question->questiontext;
|
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
if ($success = $QTYPES[$question->qtype]->get_question_options($question)) {
|
|
|
|
if (isset($question->_partiallyloaded)) {
|
|
|
|
unset($question->_partiallyloaded);
|
|
|
|
}
|
|
|
|
}
|
2009-01-16 08:00:06 +00:00
|
|
|
if ($loadtags && !empty($CFG->usetags)) {
|
|
|
|
require_once($CFG->dirroot . '/tag/lib.php');
|
|
|
|
$question->tags = tag_get_tags_array('question', $question->id);
|
|
|
|
}
|
2008-07-08 16:33:47 +00:00
|
|
|
return $success;
|
2006-07-18 15:34:24 +00:00
|
|
|
}
|
2006-02-24 10:21:40 +00:00
|
|
|
|
2006-07-18 15:34:24 +00:00
|
|
|
/**
|
|
|
|
* Updates the question objects with question type specific
|
|
|
|
* information by calling {@link get_question_options()}
|
|
|
|
*
|
|
|
|
* Can be called either with an array of question objects or with a single
|
|
|
|
* question object.
|
2007-01-07 12:46:47 +00:00
|
|
|
*
|
2006-07-18 15:34:24 +00:00
|
|
|
* @param mixed $questions Either an array of question objects to be updated
|
|
|
|
* or just a single question object
|
2009-01-16 08:00:06 +00:00
|
|
|
* @param boolean $loadtags load the question tags from the tags table. Optional, default false.
|
2006-07-18 15:34:24 +00:00
|
|
|
* @return bool Indicates success or failure.
|
|
|
|
*/
|
2009-01-16 08:00:06 +00:00
|
|
|
function get_question_options(&$questions, $loadtags = false) {
|
2006-02-24 10:21:40 +00:00
|
|
|
if (is_array($questions)) { // deal with an array of questions
|
2006-07-18 15:34:24 +00:00
|
|
|
foreach ($questions as $i => $notused) {
|
2009-01-16 08:00:06 +00:00
|
|
|
if (!_tidy_question($questions[$i], $loadtags)) {
|
2006-02-24 10:21:40 +00:00
|
|
|
return false;
|
2006-07-18 15:34:24 +00:00
|
|
|
}
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else { // deal with single question
|
2009-01-16 08:00:06 +00:00
|
|
|
return _tidy_question($questions, $loadtags);
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-23 08:41:01 +00:00
|
|
|
* Print the icon for the question type
|
|
|
|
*
|
|
|
|
* @param object $question The question object for which the icon is required.
|
|
|
|
* Only $question->qtype is used.
|
|
|
|
* @return string the HTML for the img tag.
|
|
|
|
*/
|
|
|
|
function print_question_icon($question) {
|
|
|
|
global $OUTPUT;
|
2006-02-24 10:21:40 +00:00
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
$qtype = question_bank::get_qtype($question->qtype, false);
|
|
|
|
$namestr = $qtype->menu_name();
|
2008-09-04 06:37:43 +00:00
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
// TODO convert to return a moodle_icon object, or whatever the class is.
|
|
|
|
$html = '<img src="' . $OUTPUT->pix_url('icon', $qtype->plugin_name()) . '" alt="' .
|
|
|
|
$namestr . '" title="' . $namestr . '" />';
|
2008-09-04 06:37:43 +00:00
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
return $html;
|
2008-09-04 06:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-23 08:41:01 +00:00
|
|
|
* Creates a stamp that uniquely identifies this version of the question
|
2008-09-04 06:37:43 +00:00
|
|
|
*
|
2010-12-23 08:41:01 +00:00
|
|
|
* In future we want this to use a hash of the question data to guarantee that
|
|
|
|
* identical versions have the same version stamp.
|
|
|
|
*
|
|
|
|
* @param object $question
|
|
|
|
* @return string A unique version stamp
|
2008-09-04 06:37:43 +00:00
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
function question_hash($question) {
|
|
|
|
return make_unique_id_code();
|
2008-09-04 06:37:43 +00:00
|
|
|
}
|
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
/// FUNCTIONS THAT SIMPLY WRAP QUESTIONTYPE METHODS //////////////////////////////////
|
2008-07-15 15:30:39 +00:00
|
|
|
/**
|
2010-12-23 08:41:01 +00:00
|
|
|
* Get anything that needs to be included in the head of the question editing page
|
|
|
|
* for a particular question type. This function is called by question/question.php.
|
2008-07-15 15:30:39 +00:00
|
|
|
*
|
2010-12-23 08:41:01 +00:00
|
|
|
* @param $question A question object. Only $question->qtype is used.
|
|
|
|
* @return string Deprecated. Some HTML code that can go inside the head tag.
|
2008-07-15 15:30:39 +00:00
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
function question_get_editing_head_contributions($question) {
|
|
|
|
question_bank::get_qtype($question->qtype, false)->get_editing_head_contributions();
|
2008-07-15 15:30:39 +00:00
|
|
|
}
|
2006-02-24 10:21:40 +00:00
|
|
|
|
|
|
|
/**
|
2010-12-23 08:41:01 +00:00
|
|
|
* Saves question options
|
2009-05-26 03:23:32 +00:00
|
|
|
*
|
2010-12-23 08:41:01 +00:00
|
|
|
* Simply calls the question type specific save_question_options() method.
|
|
|
|
*/
|
|
|
|
function save_question_options($question) {
|
2006-02-24 13:48:43 +00:00
|
|
|
global $QTYPES;
|
2006-02-24 10:21:40 +00:00
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
$QTYPES[$question->qtype]->save_question_options($question);
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
/// CATEGORY FUNCTIONS /////////////////////////////////////////////////////////////////
|
|
|
|
|
2006-02-24 10:21:40 +00:00
|
|
|
/**
|
2010-12-23 08:41:01 +00:00
|
|
|
* returns the categories with their names ordered following parent-child relationships
|
|
|
|
* finally it tries to return pending categories (those being orphaned, whose parent is
|
|
|
|
* incorrect) to avoid missing any category from original array.
|
|
|
|
*/
|
|
|
|
function sort_categories_by_tree(&$categories, $id = 0, $level = 1) {
|
|
|
|
global $DB;
|
2008-09-03 07:11:59 +00:00
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
$children = array();
|
|
|
|
$keys = array_keys($categories);
|
2006-02-24 10:21:40 +00:00
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
foreach ($keys as $key) {
|
|
|
|
if (!isset($categories[$key]->processed) && $categories[$key]->parent == $id) {
|
|
|
|
$children[$key] = $categories[$key];
|
|
|
|
$categories[$key]->processed = true;
|
|
|
|
$children = $children + sort_categories_by_tree($categories, $children[$key]->id, $level+1);
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
//If level = 1, we have finished, try to look for non processed categories (bad parent) and sort them too
|
|
|
|
if ($level == 1) {
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
// If not processed and it's a good candidate to start (because its parent doesn't exist in the course)
|
|
|
|
if (!isset($categories[$key]->processed) && !$DB->record_exists(
|
|
|
|
'question_categories', array('contextid'=>$categories[$key]->contextid, 'id'=>$categories[$key]->parent))) {
|
|
|
|
$children[$key] = $categories[$key];
|
|
|
|
$categories[$key]->processed = true;
|
|
|
|
$children = $children + sort_categories_by_tree($categories, $children[$key]->id, $level+1);
|
|
|
|
}
|
|
|
|
}
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
return $children;
|
2006-05-08 10:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-07-14 15:36:29 +00:00
|
|
|
* Private method, only for the use of add_indented_names().
|
2007-01-07 12:46:47 +00:00
|
|
|
*
|
2006-07-14 15:36:29 +00:00
|
|
|
* Recursively adds an indentedname field to each category, starting with the category
|
2007-01-07 12:46:47 +00:00
|
|
|
* with id $id, and dealing with that category and all its children, and
|
2006-07-14 15:36:29 +00:00
|
|
|
* return a new array, with those categories in the right order.
|
|
|
|
*
|
2007-01-07 12:46:47 +00:00
|
|
|
* @param array $categories an array of categories which has had childids
|
2006-07-14 15:36:29 +00:00
|
|
|
* fields added by flatten_category_tree(). Passed by reference for
|
|
|
|
* performance only. It is not modfied.
|
|
|
|
* @param int $id the category to start the indenting process from.
|
|
|
|
* @param int $depth the indent depth. Used in recursive calls.
|
|
|
|
* @return array a new array of categories, in the right order for the tree.
|
2006-05-08 10:39:14 +00:00
|
|
|
*/
|
2007-08-09 21:50:59 +00:00
|
|
|
function flatten_category_tree(&$categories, $id, $depth = 0, $nochildrenof = -1) {
|
2007-01-07 12:46:47 +00:00
|
|
|
|
2006-07-14 15:36:29 +00:00
|
|
|
// Indent the name of this category.
|
|
|
|
$newcategories = array();
|
|
|
|
$newcategories[$id] = $categories[$id];
|
|
|
|
$newcategories[$id]->indentedname = str_repeat(' ', $depth) . $categories[$id]->name;
|
2007-01-07 12:46:47 +00:00
|
|
|
|
2006-07-14 15:36:29 +00:00
|
|
|
// Recursively indent the children.
|
|
|
|
foreach ($categories[$id]->childids as $childid) {
|
2007-08-09 21:50:59 +00:00
|
|
|
if ($childid != $nochildrenof){
|
|
|
|
$newcategories = $newcategories + flatten_category_tree($categories, $childid, $depth + 1, $nochildrenof);
|
|
|
|
}
|
2006-05-08 10:39:14 +00:00
|
|
|
}
|
2007-01-07 12:46:47 +00:00
|
|
|
|
2006-07-14 15:36:29 +00:00
|
|
|
// Remove the childids array that were temporarily added.
|
|
|
|
unset($newcategories[$id]->childids);
|
2007-01-07 12:46:47 +00:00
|
|
|
|
2006-07-14 15:36:29 +00:00
|
|
|
return $newcategories;
|
2006-05-08 10:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-07-14 15:36:29 +00:00
|
|
|
* Format categories into an indented list reflecting the tree structure.
|
2007-01-07 12:46:47 +00:00
|
|
|
*
|
2006-07-14 15:36:29 +00:00
|
|
|
* @param array $categories An array of category objects, for example from the.
|
|
|
|
* @return array The formatted list of categories.
|
2006-05-08 10:39:14 +00:00
|
|
|
*/
|
2007-08-09 21:50:59 +00:00
|
|
|
function add_indented_names($categories, $nochildrenof = -1) {
|
2006-05-08 10:39:14 +00:00
|
|
|
|
2007-01-07 12:46:47 +00:00
|
|
|
// Add an array to each category to hold the child category ids. This array will be removed
|
2006-07-14 15:36:29 +00:00
|
|
|
// again by flatten_category_tree(). It should not be used outside these two functions.
|
|
|
|
foreach (array_keys($categories) as $id) {
|
|
|
|
$categories[$id]->childids = array();
|
2006-05-08 10:39:14 +00:00
|
|
|
}
|
|
|
|
|
2006-07-14 15:36:29 +00:00
|
|
|
// Build the tree structure, and record which categories are top-level.
|
2007-01-07 12:46:47 +00:00
|
|
|
// We have to be careful, because the categories array may include published
|
2006-07-14 15:36:29 +00:00
|
|
|
// categories from other courses, but not their parents.
|
|
|
|
$toplevelcategoryids = array();
|
|
|
|
foreach (array_keys($categories) as $id) {
|
|
|
|
if (!empty($categories[$id]->parent) && array_key_exists($categories[$id]->parent, $categories)) {
|
|
|
|
$categories[$categories[$id]->parent]->childids[] = $id;
|
|
|
|
} else {
|
|
|
|
$toplevelcategoryids[] = $id;
|
2006-05-08 10:39:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-14 15:36:29 +00:00
|
|
|
// Flatten the tree to and add the indents.
|
|
|
|
$newcategories = array();
|
|
|
|
foreach ($toplevelcategoryids as $id) {
|
2007-08-09 21:50:59 +00:00
|
|
|
$newcategories = $newcategories + flatten_category_tree($categories, $id, 0, $nochildrenof);
|
2006-05-08 10:39:14 +00:00
|
|
|
}
|
|
|
|
|
2006-07-14 15:36:29 +00:00
|
|
|
return $newcategories;
|
2006-05-08 10:39:14 +00:00
|
|
|
}
|
2006-03-02 14:13:42 +00:00
|
|
|
|
2006-02-24 10:21:40 +00:00
|
|
|
/**
|
2006-07-14 15:36:29 +00:00
|
|
|
* Output a select menu of question categories.
|
2007-01-07 12:46:47 +00:00
|
|
|
*
|
2006-07-14 15:36:29 +00:00
|
|
|
* Categories from this course and (optionally) published categories from other courses
|
2007-01-07 12:46:47 +00:00
|
|
|
* are included. Optionally, only categories the current user may edit can be included.
|
2006-07-14 15:36:29 +00:00
|
|
|
*
|
|
|
|
* @param integer $courseid the id of the course to get the categories for.
|
|
|
|
* @param integer $published if true, include publised categories from other courses.
|
|
|
|
* @param integer $only_editable if true, exclude categories this user is not allowed to edit.
|
|
|
|
* @param integer $selected optionally, the id of a category to be selected by default in the dropdown.
|
|
|
|
*/
|
2007-08-09 21:50:59 +00:00
|
|
|
function question_category_select_menu($contexts, $top = false, $currentcat = 0, $selected = "", $nochildrenof = -1) {
|
2009-08-06 00:35:06 +00:00
|
|
|
global $OUTPUT;
|
2007-08-09 21:50:59 +00:00
|
|
|
$categoriesarray = question_category_options($contexts, $top, $currentcat, false, $nochildrenof);
|
2007-03-01 09:45:53 +00:00
|
|
|
if ($selected) {
|
2010-01-16 19:48:01 +00:00
|
|
|
$choose = '';
|
2007-03-01 09:45:53 +00:00
|
|
|
} else {
|
2010-01-16 19:48:01 +00:00
|
|
|
$choose = 'choosedots';
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
2010-01-16 19:48:01 +00:00
|
|
|
$options = array();
|
|
|
|
foreach($categoriesarray as $group=>$opts) {
|
|
|
|
$options[] = array($group=>$opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
echo html_writer::select($options, 'category', $selected, $choose);
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
|
|
|
|
2008-09-10 05:28:25 +00:00
|
|
|
/**
|
|
|
|
* @param integer $contextid a context id.
|
|
|
|
* @return object the default question category for that context, or false if none.
|
|
|
|
*/
|
|
|
|
function question_get_default_category($contextid) {
|
|
|
|
global $DB;
|
|
|
|
$category = $DB->get_records('question_categories', array('contextid' => $contextid),'id','*',0,1);
|
|
|
|
if (!empty($category)) {
|
|
|
|
return reset($category);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-18 11:23:29 +00:00
|
|
|
/**
|
|
|
|
* Gets the default category in the most specific context.
|
|
|
|
* If no categories exist yet then default ones are created in all contexts.
|
|
|
|
*
|
|
|
|
* @param array $contexts The context objects for this context and all parent contexts.
|
|
|
|
* @return object The default category - the category in the course context
|
|
|
|
*/
|
|
|
|
function question_make_default_categories($contexts) {
|
2008-06-09 12:16:54 +00:00
|
|
|
global $DB;
|
2009-03-18 05:02:27 +00:00
|
|
|
static $preferredlevels = array(
|
|
|
|
CONTEXT_COURSE => 4,
|
|
|
|
CONTEXT_MODULE => 3,
|
|
|
|
CONTEXT_COURSECAT => 2,
|
|
|
|
CONTEXT_SYSTEM => 1,
|
|
|
|
);
|
2008-06-09 12:16:54 +00:00
|
|
|
|
2007-09-18 11:23:29 +00:00
|
|
|
$toreturn = null;
|
2009-03-18 05:02:27 +00:00
|
|
|
$preferredness = 0;
|
2007-09-18 11:23:29 +00:00
|
|
|
// If it already exists, just return it.
|
|
|
|
foreach ($contexts as $key => $context) {
|
2009-03-18 05:02:27 +00:00
|
|
|
if (!$exists = $DB->record_exists("question_categories", array('contextid'=>$context->id))) {
|
2008-06-09 12:16:54 +00:00
|
|
|
// Otherwise, we need to make one
|
|
|
|
$category = new stdClass;
|
|
|
|
$contextname = print_context_name($context, false, true);
|
|
|
|
$category->name = get_string('defaultfor', 'question', $contextname);
|
|
|
|
$category->info = get_string('defaultinfofor', 'question', $contextname);
|
|
|
|
$category->contextid = $context->id;
|
|
|
|
$category->parent = 0;
|
|
|
|
$category->sortorder = 999; // By default, all categories get this number, and are sorted alphabetically.
|
|
|
|
$category->stamp = make_unique_id_code();
|
2009-06-03 20:00:08 +00:00
|
|
|
$category->id = $DB->insert_record('question_categories', $category);
|
2008-06-12 09:15:16 +00:00
|
|
|
} else {
|
2008-09-10 05:28:25 +00:00
|
|
|
$category = question_get_default_category($context->id);
|
2007-09-18 11:23:29 +00:00
|
|
|
}
|
2009-03-18 05:33:56 +00:00
|
|
|
if ($preferredlevels[$context->contextlevel] > $preferredness &&
|
|
|
|
has_any_capability(array('moodle/question:usemine', 'moodle/question:useall'), $context)) {
|
2009-03-18 05:02:27 +00:00
|
|
|
$toreturn = $category;
|
|
|
|
$preferredness = $preferredlevels[$context->contextlevel];
|
2007-09-18 11:23:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-18 05:02:27 +00:00
|
|
|
if (!is_null($toreturn)) {
|
|
|
|
$toreturn = clone($toreturn);
|
|
|
|
}
|
2007-09-18 11:23:29 +00:00
|
|
|
return $toreturn;
|
|
|
|
}
|
|
|
|
|
2007-01-07 13:54:36 +00:00
|
|
|
/**
|
2007-08-09 21:50:59 +00:00
|
|
|
* Get all the category objects, including a count of the number of questions in that category,
|
|
|
|
* for all the categories in the lists $contexts.
|
2007-01-07 13:54:36 +00:00
|
|
|
*
|
2007-08-09 21:50:59 +00:00
|
|
|
* @param mixed $contexts either a single contextid, or a comma-separated list of context ids.
|
|
|
|
* @param string $sortorder used as the ORDER BY clause in the select statement.
|
|
|
|
* @return array of category objects.
|
2007-01-07 13:54:36 +00:00
|
|
|
*/
|
2007-08-09 21:50:59 +00:00
|
|
|
function get_categories_for_contexts($contexts, $sortorder = 'parent, sortorder, name ASC') {
|
2008-06-09 12:16:54 +00:00
|
|
|
global $DB;
|
|
|
|
return $DB->get_records_sql("
|
2008-09-02 06:55:32 +00:00
|
|
|
SELECT c.*, (SELECT count(1) FROM {question} q
|
2008-06-09 12:16:54 +00:00
|
|
|
WHERE c.id = q.category AND q.hidden='0' AND q.parent='0') AS questioncount
|
|
|
|
FROM {question_categories} c
|
|
|
|
WHERE c.contextid IN ($contexts)
|
|
|
|
ORDER BY $sortorder");
|
2007-08-09 21:50:59 +00:00
|
|
|
}
|
2007-05-20 16:17:48 +00:00
|
|
|
|
2007-08-09 21:50:59 +00:00
|
|
|
/**
|
|
|
|
* Output an array of question categories.
|
|
|
|
*/
|
|
|
|
function question_category_options($contexts, $top = false, $currentcat = 0, $popupform = false, $nochildrenof = -1) {
|
|
|
|
global $CFG;
|
|
|
|
$pcontexts = array();
|
|
|
|
foreach($contexts as $context){
|
|
|
|
$pcontexts[] = $context->id;
|
2007-05-25 05:49:51 +00:00
|
|
|
}
|
2007-08-09 21:50:59 +00:00
|
|
|
$contextslist = join($pcontexts, ', ');
|
|
|
|
|
|
|
|
$categories = get_categories_for_contexts($contextslist);
|
2007-01-07 13:54:36 +00:00
|
|
|
|
2007-08-09 21:50:59 +00:00
|
|
|
$categories = question_add_context_in_key($categories);
|
|
|
|
|
|
|
|
if ($top){
|
|
|
|
$categories = question_add_tops($categories, $pcontexts);
|
|
|
|
}
|
|
|
|
$categories = add_indented_names($categories, $nochildrenof);
|
2007-03-01 09:45:53 +00:00
|
|
|
|
2007-08-09 21:50:59 +00:00
|
|
|
//sort cats out into different contexts
|
2007-01-07 13:54:36 +00:00
|
|
|
$categoriesarray = array();
|
2007-08-09 21:50:59 +00:00
|
|
|
foreach ($pcontexts as $pcontext){
|
|
|
|
$contextstring = print_context_name(get_context_instance_by_id($pcontext), true, true);
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
if ($category->contextid == $pcontext){
|
|
|
|
$cid = $category->id;
|
|
|
|
if ($currentcat!= $cid || $currentcat==0) {
|
2007-08-15 12:06:21 +00:00
|
|
|
$countstring = (!empty($category->questioncount))?" ($category->questioncount)":'';
|
2007-08-09 21:50:59 +00:00
|
|
|
$categoriesarray[$contextstring][$cid] = $category->indentedname.$countstring;
|
|
|
|
}
|
|
|
|
}
|
2007-01-07 13:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
2007-08-09 21:50:59 +00:00
|
|
|
if ($popupform){
|
|
|
|
$popupcats = array();
|
|
|
|
foreach ($categoriesarray as $contextstring => $optgroup){
|
2010-02-10 11:51:49 +00:00
|
|
|
$group = array();
|
|
|
|
foreach ($optgroup as $key=>$value) {
|
2010-07-25 13:35:05 +00:00
|
|
|
$key = str_replace($CFG->wwwroot, '', $key);
|
|
|
|
$group[$key] = $value;
|
2010-02-10 11:51:49 +00:00
|
|
|
}
|
|
|
|
$popupcats[] = array($contextstring=>$group);
|
2007-08-09 21:50:59 +00:00
|
|
|
}
|
|
|
|
return $popupcats;
|
|
|
|
} else {
|
|
|
|
return $categoriesarray;
|
|
|
|
}
|
2007-01-07 13:54:36 +00:00
|
|
|
}
|
|
|
|
|
2007-08-09 21:50:59 +00:00
|
|
|
function question_add_context_in_key($categories){
|
|
|
|
$newcatarray = array();
|
|
|
|
foreach ($categories as $id => $category) {
|
|
|
|
$category->parent = "$category->parent,$category->contextid";
|
|
|
|
$category->id = "$category->id,$category->contextid";
|
|
|
|
$newcatarray["$id,$category->contextid"] = $category;
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
2007-08-09 21:50:59 +00:00
|
|
|
return $newcatarray;
|
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
|
2007-08-09 21:50:59 +00:00
|
|
|
function question_add_tops($categories, $pcontexts){
|
|
|
|
$topcats = array();
|
|
|
|
foreach ($pcontexts as $context){
|
2010-09-21 08:07:44 +00:00
|
|
|
$newcat = new stdClass();
|
2007-08-09 21:50:59 +00:00
|
|
|
$newcat->id = "0,$context";
|
|
|
|
$newcat->name = get_string('top');
|
|
|
|
$newcat->parent = -1;
|
|
|
|
$newcat->contextid = $context;
|
|
|
|
$topcats["0,$context"] = $newcat;
|
|
|
|
}
|
|
|
|
//put topcats in at beginning of array - they'll be sorted into different contexts later.
|
|
|
|
return array_merge($topcats, $categories);
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-10-06 16:48:54 +00:00
|
|
|
* Returns a comma separated list of ids of the category and all subcategories
|
|
|
|
*/
|
2006-03-01 07:03:57 +00:00
|
|
|
function question_categorylist($categoryid) {
|
2008-06-09 12:16:54 +00:00
|
|
|
global $DB;
|
|
|
|
|
2006-02-24 10:21:40 +00:00
|
|
|
// returns a comma separated list of ids of the category and all subcategories
|
|
|
|
$categorylist = $categoryid;
|
2010-12-23 08:41:01 +00:00
|
|
|
if ($subcategories = $DB->get_records('question_categories', array('parent' => $categoryid), 'sortorder ASC', 'id, 1')) {
|
2006-02-24 10:21:40 +00:00
|
|
|
foreach ($subcategories as $subcategory) {
|
2006-03-01 07:03:57 +00:00
|
|
|
$categorylist .= ','. question_categorylist($subcategory->id);
|
2006-02-24 10:21:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $categorylist;
|
|
|
|
}
|
|
|
|
|
2006-05-02 09:04:38 +00:00
|
|
|
//===========================
|
|
|
|
// Import/Export Functions
|
|
|
|
//===========================
|
|
|
|
|
2006-02-24 15:44:53 +00:00
|
|
|
/**
|
|
|
|
* Get list of available import or export formats
|
|
|
|
* @param string $type 'import' if import list, otherwise export list assumed
|
2007-01-07 12:46:47 +00:00
|
|
|
* @return array sorted list of import/export formats available
|
2009-05-26 03:23:32 +00:00
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
function get_import_export_formats($type) {
|
2006-02-24 15:44:53 +00:00
|
|
|
|
|
|
|
global $CFG;
|
2010-12-23 08:41:01 +00:00
|
|
|
$fileformats = get_plugin_list('qformat');
|
2006-02-24 15:44:53 +00:00
|
|
|
|
|
|
|
$fileformatname=array();
|
2006-05-02 09:04:38 +00:00
|
|
|
require_once( "{$CFG->dirroot}/question/format.php" );
|
2010-12-23 08:41:01 +00:00
|
|
|
foreach ($fileformats as $fileformat => $fdir) {
|
|
|
|
$formatfile = $fdir . '/format.php';
|
|
|
|
if (is_readable($formatfile)) {
|
|
|
|
include_once($formatfile);
|
|
|
|
} else {
|
2006-02-24 15:44:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
|
2006-03-01 09:30:21 +00:00
|
|
|
$classname = "qformat_$fileformat";
|
2010-12-23 08:41:01 +00:00
|
|
|
$formatclass = new $classname();
|
|
|
|
if ($type == 'import') {
|
2006-02-24 15:44:53 +00:00
|
|
|
$provided = $format_class->provide_import();
|
2010-12-23 08:41:01 +00:00
|
|
|
} else {
|
2006-02-24 15:44:53 +00:00
|
|
|
$provided = $format_class->provide_export();
|
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
|
2006-02-24 15:44:53 +00:00
|
|
|
if ($provided) {
|
2010-12-23 08:41:01 +00:00
|
|
|
$fileformatnames[$fileformat] = get_string($fileformat, 'qformat_'.$fileformat);
|
2006-02-24 15:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
natcasesort($fileformatnames);
|
|
|
|
|
|
|
|
return $fileformatnames;
|
|
|
|
}
|
2006-02-24 18:49:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-11-19 11:02:28 +00:00
|
|
|
* Create a reasonable default file name for exporting questions from a particular
|
|
|
|
* category.
|
|
|
|
* @param object $course the course the questions are in.
|
|
|
|
* @param object $category the question category.
|
|
|
|
* @return string the filename.
|
2006-02-24 18:49:50 +00:00
|
|
|
*/
|
2010-11-19 11:02:28 +00:00
|
|
|
function question_default_export_filename($course, $category) {
|
|
|
|
// We build a string that is an appropriate name (questions) from the lang pack,
|
|
|
|
// then the corse shortname, then the question category name, then a timestamp.
|
|
|
|
|
|
|
|
$base = clean_filename(get_string('exportfilename', 'question'));
|
|
|
|
|
|
|
|
$dateformat = str_replace(' ', '_', get_string('exportnameformat', 'question'));
|
|
|
|
$timestamp = clean_filename(userdate(time(), $dateformat, 99, false));
|
|
|
|
|
|
|
|
$shortname = clean_filename($course->shortname);
|
|
|
|
if ($shortname == '' || $shortname == '_' ) {
|
|
|
|
$shortname = $course->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$categoryname = clean_filename(format_string($category->name));
|
|
|
|
|
|
|
|
return "{$base}-{$shortname}-{$categoryname}-{$timestamp}";
|
2006-02-24 18:49:50 +00:00
|
|
|
|
|
|
|
return $export_name;
|
|
|
|
}
|
2009-05-26 03:23:32 +00:00
|
|
|
|
|
|
|
/**
|
2010-12-23 08:41:01 +00:00
|
|
|
* Converts contextlevels to strings and back to help with reading/writing contexts
|
|
|
|
* to/from import/export files.
|
|
|
|
*
|
2009-05-26 03:23:32 +00:00
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2007-08-09 21:50:59 +00:00
|
|
|
class context_to_string_translator{
|
|
|
|
/**
|
|
|
|
* @var array used to translate between contextids and strings for this context.
|
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
protected $contexttostringarray = array();
|
2007-08-09 21:50:59 +00:00
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
public function __construct($contexts) {
|
2007-08-09 21:50:59 +00:00
|
|
|
$this->generate_context_to_string_array($contexts);
|
|
|
|
}
|
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
public function context_to_string($contextid) {
|
2007-08-09 21:50:59 +00:00
|
|
|
return $this->contexttostringarray[$contextid];
|
|
|
|
}
|
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
public function string_to_context($contextname) {
|
2007-08-09 21:50:59 +00:00
|
|
|
$contextid = array_search($contextname, $this->contexttostringarray);
|
|
|
|
return $contextid;
|
|
|
|
}
|
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
protected function generate_context_to_string_array($contexts) {
|
2007-08-09 21:50:59 +00:00
|
|
|
if (!$this->contexttostringarray){
|
|
|
|
$catno = 1;
|
|
|
|
foreach ($contexts as $context){
|
2010-12-23 08:41:01 +00:00
|
|
|
switch ($context->contextlevel){
|
2007-08-09 21:50:59 +00:00
|
|
|
case CONTEXT_MODULE :
|
|
|
|
$contextstring = 'module';
|
|
|
|
break;
|
|
|
|
case CONTEXT_COURSE :
|
|
|
|
$contextstring = 'course';
|
|
|
|
break;
|
|
|
|
case CONTEXT_COURSECAT :
|
|
|
|
$contextstring = "cat$catno";
|
|
|
|
$catno++;
|
|
|
|
break;
|
|
|
|
case CONTEXT_SYSTEM :
|
|
|
|
$contextstring = 'system';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$this->contexttostringarray[$context->id] = $contextstring;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2006-02-24 18:49:50 +00:00
|
|
|
|
2007-08-09 21:50:59 +00:00
|
|
|
/**
|
|
|
|
* Check capability on category
|
2009-05-26 03:23:32 +00:00
|
|
|
*
|
2007-08-09 21:50:59 +00:00
|
|
|
* @param mixed $question object or id
|
|
|
|
* @param string $cap 'add', 'edit', 'view', 'use', 'move'
|
|
|
|
* @param integer $cachecat useful to cache all question records in a category
|
|
|
|
* @return boolean this user has the capability $cap for this question $question?
|
|
|
|
*/
|
|
|
|
function question_has_capability_on($question, $cap, $cachecat = -1){
|
2008-06-09 12:16:54 +00:00
|
|
|
global $USER, $DB;
|
|
|
|
|
2007-08-09 21:50:59 +00:00
|
|
|
// these are capabilities on existing questions capabilties are
|
|
|
|
//set per category. Each of these has a mine and all version. Append 'mine' and 'all'
|
|
|
|
$question_questioncaps = array('edit', 'view', 'use', 'move');
|
|
|
|
static $questions = array();
|
|
|
|
static $categories = array();
|
|
|
|
static $cachedcat = array();
|
2010-11-12 11:29:53 +00:00
|
|
|
if ($cachecat != -1 && array_search($cachecat, $cachedcat) === false) {
|
|
|
|
$questions += $DB->get_records('question', array('category' => $cachecat));
|
2007-08-09 21:50:59 +00:00
|
|
|
$cachedcat[] = $cachecat;
|
|
|
|
}
|
|
|
|
if (!is_object($question)){
|
|
|
|
if (!isset($questions[$question])){
|
2010-11-12 11:29:53 +00:00
|
|
|
if (!$questions[$question] = $DB->get_record('question', array('id' => $question), 'id,category,createdby')) {
|
2007-11-26 13:32:23 +00:00
|
|
|
print_error('questiondoesnotexist', 'question');
|
2007-08-09 21:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$question = $questions[$question];
|
|
|
|
}
|
|
|
|
if (!isset($categories[$question->category])){
|
2008-06-09 12:16:54 +00:00
|
|
|
if (!$categories[$question->category] = $DB->get_record('question_categories', array('id'=>$question->category))) {
|
2007-08-09 21:50:59 +00:00
|
|
|
print_error('invalidcategory', 'quiz');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$category = $categories[$question->category];
|
2010-11-12 11:29:53 +00:00
|
|
|
$context = get_context_instance_by_id($category->contextid);
|
2007-08-09 21:50:59 +00:00
|
|
|
|
|
|
|
if (array_search($cap, $question_questioncaps)!== FALSE){
|
2010-11-12 11:29:53 +00:00
|
|
|
if (!has_capability('moodle/question:'.$cap.'all', $context)){
|
2007-08-09 21:50:59 +00:00
|
|
|
if ($question->createdby == $USER->id){
|
2010-11-12 11:29:53 +00:00
|
|
|
return has_capability('moodle/question:'.$cap.'mine', $context);
|
2007-08-09 21:50:59 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2010-11-12 11:29:53 +00:00
|
|
|
return has_capability('moodle/question:'.$cap, $context);
|
2007-08-09 21:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Require capability on question.
|
|
|
|
*/
|
|
|
|
function question_require_capability_on($question, $cap){
|
|
|
|
if (!question_has_capability_on($question, $cap)){
|
|
|
|
print_error('nopermissions', '', '', $cap);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-18 14:36:24 +00:00
|
|
|
/**
|
2008-07-30 09:02:44 +00:00
|
|
|
* Get the real state - the correct question id and answer - for a random
|
|
|
|
* question.
|
2008-07-18 14:36:24 +00:00
|
|
|
* @param object $state with property answer.
|
|
|
|
* @return mixed return integer real question id or false if there was an
|
|
|
|
* error..
|
|
|
|
*/
|
2010-11-11 21:54:22 +00:00
|
|
|
function question_get_real_state($state) {
|
2009-08-18 05:20:12 +00:00
|
|
|
global $OUTPUT;
|
2008-07-30 09:02:44 +00:00
|
|
|
$realstate = clone($state);
|
2008-07-18 14:36:24 +00:00
|
|
|
$matches = array();
|
2008-11-10 07:03:25 +00:00
|
|
|
if (!preg_match('|^random([0-9]+)-(.*)|', $state->answer, $matches)){
|
2009-08-18 05:20:12 +00:00
|
|
|
echo $OUTPUT->notification(get_string('errorrandom', 'quiz_statistics'));
|
2008-07-18 14:36:24 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2008-07-30 09:02:44 +00:00
|
|
|
$realstate->question = $matches[1];
|
|
|
|
$realstate->answer = $matches[2];
|
|
|
|
return $realstate;
|
2008-07-18 14:36:24 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-30 09:02:44 +00:00
|
|
|
|
2008-08-29 10:08:27 +00:00
|
|
|
/**
|
2010-12-23 08:41:01 +00:00
|
|
|
* @param object $context a context
|
|
|
|
* @return string A URL for editing questions in this context.
|
2008-09-03 08:32:29 +00:00
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
function question_edit_url($context) {
|
|
|
|
global $CFG, $SITE;
|
|
|
|
if (!has_any_capability(question_get_question_capabilities(), $context)) {
|
|
|
|
return false;
|
2008-09-03 08:32:29 +00:00
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
$baseurl = $CFG->wwwroot . '/question/edit.php?';
|
|
|
|
$defaultcategory = question_get_default_category($context->id);
|
|
|
|
if ($defaultcategory) {
|
|
|
|
$baseurl .= 'cat=' . $defaultcategory->id . ',' . $context->id . '&';
|
2008-09-03 08:32:29 +00:00
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
switch ($context->contextlevel) {
|
|
|
|
case CONTEXT_SYSTEM:
|
|
|
|
return $baseurl . 'courseid=' . $SITE->id;
|
|
|
|
case CONTEXT_COURSECAT:
|
|
|
|
// This is nasty, becuase we can only edit questions in a course
|
|
|
|
// context at the moment, so for now we just return false.
|
|
|
|
return false;
|
|
|
|
case CONTEXT_COURSE:
|
|
|
|
return $baseurl . 'courseid=' . $context->instanceid;
|
|
|
|
case CONTEXT_MODULE:
|
|
|
|
return $baseurl . 'cmid=' . $context->instanceid;
|
2008-08-29 10:08:27 +00:00
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
|
2008-08-29 10:08:27 +00:00
|
|
|
}
|
2010-05-21 03:15:48 +00:00
|
|
|
|
|
|
|
/**
|
2010-07-22 09:43:30 +00:00
|
|
|
* Adds question bank setting links to the given navigation node if caps are met.
|
2010-05-21 03:15:48 +00:00
|
|
|
*
|
|
|
|
* @param navigation_node $navigationnode The navigation node to add the question branch to
|
|
|
|
* @param stdClass $context
|
|
|
|
* @return navigation_node Returns the question branch that was added
|
|
|
|
*/
|
|
|
|
function question_extend_settings_navigation(navigation_node $navigationnode, $context) {
|
|
|
|
global $PAGE;
|
|
|
|
|
|
|
|
if ($context->contextlevel == CONTEXT_COURSE) {
|
|
|
|
$params = array('courseid'=>$context->instanceid);
|
|
|
|
} else if ($context->contextlevel == CONTEXT_MODULE) {
|
|
|
|
$params = array('cmid'=>$context->instanceid);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$questionnode = $navigationnode->add(get_string('questionbank','question'), new moodle_url('/question/edit.php', $params), navigation_node::TYPE_CONTAINER);
|
|
|
|
|
|
|
|
$contexts = new question_edit_contexts($context);
|
|
|
|
if ($contexts->have_one_edit_tab_cap('questions')) {
|
|
|
|
$questionnode->add(get_string('questions', 'quiz'), new moodle_url('/question/edit.php', $params), navigation_node::TYPE_SETTING);
|
|
|
|
}
|
|
|
|
if ($contexts->have_one_edit_tab_cap('categories')) {
|
|
|
|
$questionnode->add(get_string('categories', 'quiz'), new moodle_url('/question/category.php', $params), navigation_node::TYPE_SETTING);
|
|
|
|
}
|
|
|
|
if ($contexts->have_one_edit_tab_cap('import')) {
|
|
|
|
$questionnode->add(get_string('import', 'quiz'), new moodle_url('/question/import.php', $params), navigation_node::TYPE_SETTING);
|
|
|
|
}
|
|
|
|
if ($contexts->have_one_edit_tab_cap('export')) {
|
|
|
|
$questionnode->add(get_string('export', 'quiz'), new moodle_url('/question/export.php', $params), navigation_node::TYPE_SETTING);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $questionnode;
|
|
|
|
}
|
|
|
|
|
2010-12-23 08:41:01 +00:00
|
|
|
/**
|
|
|
|
* @return array all the capabilities that relate to accessing particular questions.
|
|
|
|
*/
|
|
|
|
function question_get_question_capabilities() {
|
|
|
|
return array(
|
|
|
|
'moodle/question:add',
|
|
|
|
'moodle/question:editmine',
|
|
|
|
'moodle/question:editall',
|
|
|
|
'moodle/question:viewmine',
|
|
|
|
'moodle/question:viewall',
|
|
|
|
'moodle/question:usemine',
|
|
|
|
'moodle/question:useall',
|
|
|
|
'moodle/question:movemine',
|
|
|
|
'moodle/question:moveall',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array all the question bank capabilities.
|
|
|
|
*/
|
|
|
|
function question_get_all_capabilities() {
|
|
|
|
$caps = question_get_question_capabilities();
|
|
|
|
$caps[] = 'moodle/question:managecategory';
|
|
|
|
$caps[] = 'moodle/question:flag';
|
|
|
|
return $caps;
|
|
|
|
}
|
|
|
|
|
2010-05-21 03:15:48 +00:00
|
|
|
class question_edit_contexts {
|
|
|
|
|
|
|
|
public static $CAPS = array(
|
|
|
|
'editq' => array('moodle/question:add',
|
|
|
|
'moodle/question:editmine',
|
|
|
|
'moodle/question:editall',
|
|
|
|
'moodle/question:viewmine',
|
|
|
|
'moodle/question:viewall',
|
|
|
|
'moodle/question:usemine',
|
|
|
|
'moodle/question:useall',
|
|
|
|
'moodle/question:movemine',
|
|
|
|
'moodle/question:moveall'),
|
|
|
|
'questions'=>array('moodle/question:add',
|
|
|
|
'moodle/question:editmine',
|
|
|
|
'moodle/question:editall',
|
|
|
|
'moodle/question:viewmine',
|
|
|
|
'moodle/question:viewall',
|
|
|
|
'moodle/question:movemine',
|
|
|
|
'moodle/question:moveall'),
|
|
|
|
'categories'=>array('moodle/question:managecategory'),
|
|
|
|
'import'=>array('moodle/question:add'),
|
|
|
|
'export'=>array('moodle/question:viewall', 'moodle/question:viewmine'));
|
|
|
|
|
|
|
|
protected $allcontexts;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param current context
|
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
public function question_edit_contexts($thiscontext) {
|
2010-05-21 03:15:48 +00:00
|
|
|
$pcontextids = get_parent_contexts($thiscontext);
|
|
|
|
$contexts = array($thiscontext);
|
|
|
|
foreach ($pcontextids as $pcontextid){
|
|
|
|
$contexts[] = get_context_instance_by_id($pcontextid);
|
|
|
|
}
|
|
|
|
$this->allcontexts = $contexts;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array all parent contexts
|
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
public function all() {
|
2010-05-21 03:15:48 +00:00
|
|
|
return $this->allcontexts;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return object lowest context which must be either the module or course context
|
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
public function lowest() {
|
2010-05-21 03:15:48 +00:00
|
|
|
return $this->allcontexts[0];
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param string $cap capability
|
|
|
|
* @return array parent contexts having capability, zero based index
|
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
public function having_cap($cap) {
|
2010-05-21 03:15:48 +00:00
|
|
|
$contextswithcap = array();
|
|
|
|
foreach ($this->allcontexts as $context){
|
|
|
|
if (has_capability($cap, $context)){
|
|
|
|
$contextswithcap[] = $context;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $contextswithcap;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param array $caps capabilities
|
|
|
|
* @return array parent contexts having at least one of $caps, zero based index
|
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
public function having_one_cap($caps) {
|
2010-05-21 03:15:48 +00:00
|
|
|
$contextswithacap = array();
|
|
|
|
foreach ($this->allcontexts as $context){
|
|
|
|
foreach ($caps as $cap){
|
|
|
|
if (has_capability($cap, $context)){
|
|
|
|
$contextswithacap[] = $context;
|
|
|
|
break; //done with caps loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $contextswithacap;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param string $tabname edit tab name
|
|
|
|
* @return array parent contexts having at least one of $caps, zero based index
|
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
public function having_one_edit_tab_cap($tabname) {
|
2010-05-21 03:15:48 +00:00
|
|
|
return $this->having_one_cap(self::$CAPS[$tabname]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Has at least one parent context got the cap $cap?
|
|
|
|
*
|
|
|
|
* @param string $cap capability
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
public function have_cap($cap) {
|
2010-05-21 03:15:48 +00:00
|
|
|
return (count($this->having_cap($cap)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Has at least one parent context got one of the caps $caps?
|
|
|
|
*
|
|
|
|
* @param array $caps capability
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2010-12-23 08:41:01 +00:00
|
|
|
public function have_one_cap($caps) {
|
2010-05-21 03:15:48 +00:00
|
|
|
foreach ($caps as $cap) {
|
|
|
|
if ($this->have_cap($cap)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
|
2010-05-21 03:15:48 +00:00
|
|
|
/**
|
|
|
|
* Has at least one parent context got one of the caps for actions on $tabname
|
|
|
|
*
|
|
|
|
* @param string $tabname edit tab name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function have_one_edit_tab_cap($tabname){
|
|
|
|
return $this->have_one_cap(self::$CAPS[$tabname]);
|
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
|
2010-05-21 03:15:48 +00:00
|
|
|
/**
|
|
|
|
* Throw error if at least one parent context hasn't got the cap $cap
|
|
|
|
*
|
|
|
|
* @param string $cap capability
|
|
|
|
*/
|
|
|
|
public function require_cap($cap){
|
|
|
|
if (!$this->have_cap($cap)){
|
|
|
|
print_error('nopermissions', '', '', $cap);
|
|
|
|
}
|
|
|
|
}
|
2010-12-23 08:41:01 +00:00
|
|
|
|
2010-05-21 03:15:48 +00:00
|
|
|
/**
|
|
|
|
* Throw error if at least one parent context hasn't got one of the caps $caps
|
|
|
|
*
|
|
|
|
* @param array $cap capabilities
|
|
|
|
*/
|
|
|
|
public function require_one_cap($caps) {
|
|
|
|
if (!$this->have_one_cap($caps)) {
|
|
|
|
$capsstring = join($caps, ', ');
|
|
|
|
print_error('nopermissions', '', '', $capsstring);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throw error if at least one parent context hasn't got one of the caps $caps
|
|
|
|
*
|
|
|
|
* @param string $tabname edit tab name
|
|
|
|
*/
|
|
|
|
public function require_one_edit_tab_cap($tabname){
|
|
|
|
if (!$this->have_one_edit_tab_cap($tabname)) {
|
|
|
|
print_error('nopermissions', '', '', 'access question edit tab '.$tabname);
|
|
|
|
}
|
|
|
|
}
|
2010-08-10 09:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rewrite question url, file_rewrite_pluginfile_urls always build url by
|
|
|
|
* $file/$contextid/$component/$filearea/$itemid/$pathname_in_text, so we cannot add
|
|
|
|
* extra questionid and attempted in url by it, so we create quiz_rewrite_question_urls
|
|
|
|
* to build url here
|
|
|
|
*
|
|
|
|
* @param string $text text being processed
|
|
|
|
* @param string $file the php script used to serve files
|
|
|
|
* @param int $contextid
|
|
|
|
* @param string $component component
|
|
|
|
* @param string $filearea filearea
|
|
|
|
* @param array $ids other IDs will be used to check file permission
|
|
|
|
* @param int $itemid
|
|
|
|
* @param array $options
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-12-24 13:22:26 +00:00
|
|
|
function question_rewrite_question_urls($text, $file, $contextid, $component, $filearea, array $ids, $itemid, array $options=null) {
|
2010-08-10 09:56:48 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$options = (array)$options;
|
|
|
|
if (!isset($options['forcehttps'])) {
|
|
|
|
$options['forcehttps'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$CFG->slasharguments) {
|
|
|
|
$file = $file . '?file=';
|
|
|
|
}
|
|
|
|
|
|
|
|
$baseurl = "$CFG->wwwroot/$file/$contextid/$component/$filearea/";
|
|
|
|
|
|
|
|
if (!empty($ids)) {
|
|
|
|
$baseurl .= (implode('/', $ids) . '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($itemid !== null) {
|
|
|
|
$baseurl .= "$itemid/";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options['forcehttps']) {
|
|
|
|
$baseurl = str_replace('http://', 'https://', $baseurl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str_replace('@@PLUGINFILE@@/', $baseurl, $text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by pluginfile.php to serve files related to the 'question' core
|
|
|
|
* component and for files belonging to qtypes.
|
|
|
|
*
|
|
|
|
* For files that relate to questions in a question_attempt, then we delegate to
|
|
|
|
* a function in the component that owns the attempt (for example in the quiz,
|
|
|
|
* or in core question preview) to get necessary inforation.
|
|
|
|
*
|
|
|
|
* (Note that, at the moment, all question file areas relate to questions in
|
|
|
|
* attempts, so the If at the start of the last paragraph is always true.)
|
|
|
|
*
|
|
|
|
* Does not return, either calls send_file_not_found(); or serves the file.
|
|
|
|
*
|
|
|
|
* @param object $course course settings object
|
|
|
|
* @param object $context context object
|
|
|
|
* @param string $component the name of the component we are serving files for.
|
|
|
|
* @param string $filearea the name of the file area.
|
|
|
|
* @param array $args the remaining bits of the file path.
|
|
|
|
* @param bool $forcedownload whether the user must be forced to download the file.
|
|
|
|
*/
|
|
|
|
function question_pluginfile($course, $context, $component, $filearea, $args, $forcedownload) {
|
|
|
|
global $DB, $CFG;
|
|
|
|
|
2010-11-05 06:34:00 +00:00
|
|
|
list($context, $course, $cm) = get_context_info_array($context->id);
|
|
|
|
require_login($course, false, $cm);
|
|
|
|
|
|
|
|
if ($filearea === 'export') {
|
|
|
|
require_once($CFG->dirroot . '/question/editlib.php');
|
|
|
|
$contexts = new question_edit_contexts($context);
|
|
|
|
// check export capability
|
|
|
|
$contexts->require_one_edit_tab_cap('export');
|
|
|
|
$category_id = (int)array_shift($args);
|
|
|
|
$format = array_shift($args);
|
|
|
|
$cattofile = array_shift($args);
|
|
|
|
$contexttofile = array_shift($args);
|
|
|
|
$filename = array_shift($args);
|
|
|
|
|
|
|
|
// load parent class for import/export
|
|
|
|
require_once($CFG->dirroot . '/question/format.php');
|
|
|
|
require_once($CFG->dirroot . '/question/editlib.php');
|
|
|
|
require_once($CFG->dirroot . '/question/format/' . $format . '/format.php');
|
|
|
|
|
|
|
|
$classname = 'qformat_' . $format;
|
|
|
|
if (!class_exists($classname)) {
|
|
|
|
send_file_not_found();
|
|
|
|
}
|
|
|
|
|
|
|
|
$qformat = new $classname();
|
|
|
|
|
|
|
|
if (!$category = $DB->get_record('question_categories', array('id' => $category_id))) {
|
|
|
|
send_file_not_found();
|
|
|
|
}
|
|
|
|
|
|
|
|
$qformat->setCategory($category);
|
|
|
|
$qformat->setContexts($contexts->having_one_edit_tab_cap('export'));
|
|
|
|
$qformat->setCourse($course);
|
|
|
|
|
|
|
|
if ($cattofile == 'withcategories') {
|
|
|
|
$qformat->setCattofile(true);
|
|
|
|
} else {
|
|
|
|
$qformat->setCattofile(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($contexttofile == 'withcontexts') {
|
|
|
|
$qformat->setContexttofile(true);
|
|
|
|
} else {
|
|
|
|
$qformat->setContexttofile(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$qformat->exportpreprocess()) {
|
|
|
|
send_file_not_found();
|
|
|
|
print_error('exporterror', 'question', $thispageurl->out());
|
|
|
|
}
|
|
|
|
|
|
|
|
// export data to moodle file pool
|
|
|
|
if (!$content = $qformat->exportprocess(true)) {
|
|
|
|
send_file_not_found();
|
|
|
|
}
|
|
|
|
|
|
|
|
//DEBUG
|
|
|
|
//echo '<textarea cols=90 rows=20>';
|
|
|
|
//echo $content;
|
|
|
|
//echo '</textarea>';
|
|
|
|
//die;
|
2010-11-08 15:51:10 +00:00
|
|
|
send_file($content, $filename, 0, 0, true, true, $qformat->mime_type());
|
2010-11-05 06:34:00 +00:00
|
|
|
}
|
|
|
|
|
2010-08-10 09:56:48 +00:00
|
|
|
$attemptid = (int)array_shift($args);
|
|
|
|
$questionid = (int)array_shift($args);
|
|
|
|
|
|
|
|
|
|
|
|
if ($attemptid === 0) {
|
|
|
|
// preview
|
|
|
|
require_once($CFG->dirroot . '/question/previewlib.php');
|
|
|
|
return question_preview_question_pluginfile($course, $context,
|
|
|
|
$component, $filearea, $attemptid, $questionid, $args, $forcedownload);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$module = $DB->get_field('question_attempts', 'modulename',
|
|
|
|
array('id' => $attemptid));
|
|
|
|
|
|
|
|
$dir = get_component_directory($module);
|
|
|
|
if (!file_exists("$dir/lib.php")) {
|
|
|
|
send_file_not_found();
|
|
|
|
}
|
|
|
|
include_once("$dir/lib.php");
|
|
|
|
|
|
|
|
$filefunction = $module . '_question_pluginfile';
|
|
|
|
if (!function_exists($filefunction)) {
|
|
|
|
send_file_not_found();
|
|
|
|
}
|
|
|
|
|
|
|
|
$filefunction($course, $context, $component, $filearea, $attemptid, $questionid,
|
|
|
|
$args, $forcedownload);
|
|
|
|
|
|
|
|
send_file_not_found();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Final test for whether a studnet should be allowed to see a particular file.
|
|
|
|
* This delegates the decision to the question type plugin.
|
|
|
|
*
|
|
|
|
* @param object $question The question to be rendered.
|
|
|
|
* @param object $state The state to render the question in.
|
|
|
|
* @param object $options An object specifying the rendering options.
|
|
|
|
* @param string $component the name of the component we are serving files for.
|
|
|
|
* @param string $filearea the name of the file area.
|
|
|
|
* @param array $args the remaining bits of the file path.
|
|
|
|
* @param bool $forcedownload whether the user must be forced to download the file.
|
|
|
|
*/
|
|
|
|
function question_check_file_access($question, $state, $options, $contextid, $component,
|
|
|
|
$filearea, $args, $forcedownload) {
|
|
|
|
global $QTYPES;
|
|
|
|
return $QTYPES[$question->qtype]->check_file_access($question, $state, $options, $contextid, $component,
|
|
|
|
$filearea, $args, $forcedownload);
|
|
|
|
}
|
2010-11-05 06:34:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create url for question export
|
|
|
|
*
|
|
|
|
* @param int $contextid, current context
|
|
|
|
* @param int $categoryid, categoryid
|
|
|
|
* @param string $format
|
|
|
|
* @param string $withcategories
|
|
|
|
* @param string $ithcontexts
|
|
|
|
* @param moodle_url export file url
|
|
|
|
*/
|
2010-11-19 11:02:28 +00:00
|
|
|
function question_make_export_url($contextid, $categoryid, $format, $withcategories, $withcontexts, $filename) {
|
2010-11-05 06:34:00 +00:00
|
|
|
global $CFG;
|
|
|
|
$urlbase = "$CFG->httpswwwroot/pluginfile.php";
|
2010-11-19 11:02:28 +00:00
|
|
|
return moodle_url::make_file_url($urlbase, "/$contextid/question/export/{$categoryid}/{$format}/{$withcategories}/{$withcontexts}/{$filename}", true);
|
2010-11-05 06:34:00 +00:00
|
|
|
}
|