Skip to content

Commit

Permalink
MDL-20636 Remove all references to global $QTYPES.
Browse files Browse the repository at this point in the history
Except in the question types that have not yet been converted to the new question engine.
  • Loading branch information
timhunt committed Feb 24, 2011
1 parent 446166a commit d649fb0
Show file tree
Hide file tree
Showing 21 changed files with 422 additions and 438 deletions.
2 changes: 1 addition & 1 deletion admin/generator.php
Expand Up @@ -622,7 +622,7 @@ public function generate_questions($courses, $modules) {
require_once($CFG->libdir .'/questionlib.php');
require_once($CFG->dirroot .'/mod/quiz/editlib.php');
$questions = array();
$questionsmenu = question_type_menu();
$questionsmenu = question_bank::get_creatable_qtypes();
$questiontypes = array();
foreach ($questionsmenu as $qtype => $qname) {
$questiontypes[] = $qtype;
Expand Down
475 changes: 247 additions & 228 deletions admin/qtypes.php

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions admin/report/questioninstances/index.php
Expand Up @@ -22,8 +22,9 @@
add_to_log(SITEID, "admin", "report questioninstances", "report/questioninstances/index.php?qtype=$requestedqtype", $requestedqtype);

// Prepare the list of capabilities to choose from
$qtypes = question_bank::get_all_qtypes();
$qtypechoices = array();
foreach ($QTYPES as $qtype) {
foreach ($qtypes as $qtype) {
$qtypechoices[$qtype->name()] = $qtype->local_name();
}

Expand All @@ -45,7 +46,7 @@

// Work out the bits needed for the SQL WHERE clauses.
if ($requestedqtype == 'missingtype') {
$othertypes = array_keys($QTYPES);
$othertypes = array_keys($qtypes);
$key = array_search('missingtype', $othertypes);
unset($othertypes[$key]);
list($sqlqtypetest, $params) = $DB->get_in_or_equal($othertypes, SQL_PARAMS_QM, '', false);
Expand All @@ -58,7 +59,8 @@
} else {
$sqlqtypetest = 'WHERE qtype = ?';
$params = array($requestedqtype);
$title = get_string('reportforqtype', 'report_questioninstances', $QTYPES[$requestedqtype]->local_name());
$title = get_string('reportforqtype', 'report_questioninstances',
question_bank::get_qtype($requestedqtype)->local_name());
}

// Get the question counts, and all the context information, for each
Expand Down
99 changes: 11 additions & 88 deletions lib/questionlib.php
Expand Up @@ -82,81 +82,6 @@
*/
define('QUESTION_PREVIEW_POPUP_OPTIONS', 'scrollbars=yes,resizable=yes,width=800,height=600');

/**
* @global array holding question type objects
* @deprecated
*/
global $QTYPES;
$QTYPES = question_bank::get_all_qtypes();
function question_register_questiontype() {
// TODO kill this.
}
// TODO kill this.
class default_questiontype {
function plugin_dir() {
return '';
}
}

/**
* 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.
*
* @return array an array of question type names translated to the user's language.
*/
function question_type_menu() {
static $menuoptions = null;
if (is_null($menuoptions)) {
$config = get_config('question');
$menuoptions = array();
foreach (question_bank::get_all_qtypes() as $name => $qtype) {
$menuname = $qtype->menu_name();
$enabledvar = $name . '_disabled';
if ($menuname && !isset($config->$enabledvar)) {
$menuoptions[$name] = $menuname;
}
}

$menuoptions = question_sort_qtype_array($menuoptions, $config);
}
return $menuoptions;
}

/**
* 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
* at the end, sorted according to textlib_get_instance()->asort($inarray).
* @param $inarray an array $qtypename => $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]);
}
textlib_get_instance()->asort($inarray);
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.
Expand Down Expand Up @@ -692,7 +617,7 @@ function question_delete_activity($cm, $feedback=true) {
* @param integer $newcategoryid the id of the category to move to.
*/
function question_move_questions_to_category($questionids, $newcategoryid) {
global $DB, $QTYPES;
global $DB;

$newcontextid = $DB->get_field('question_categories', 'contextid',
array('id' => $newcategoryid));
Expand All @@ -704,8 +629,8 @@ function question_move_questions_to_category($questionids, $newcategoryid) {
WHERE q.id $questionidcondition", $params);
foreach ($questions as $question) {
if ($newcontextid != $question->contextid) {
$QTYPES[$question->qtype]->move_files($question->id,
$question->contextid, $newcontextid);
question_bank::get_qtype($question->qtype)->move_files(
$question->id, $question->contextid, $newcontextid);
}
}

Expand All @@ -729,12 +654,12 @@ function question_move_questions_to_category($questionids, $newcategoryid) {
* @param integer $newcontextid the new context id.
*/
function question_move_category_to_context($categoryid, $oldcontextid, $newcontextid) {
global $DB, $QTYPES;
global $DB;

$questionids = $DB->get_records_menu('question',
array('category' => $categoryid), '', 'id,qtype');
foreach ($questionids as $questionid => $qtype) {
$QTYPES[$qtype]->move_files($questionid, $oldcontextid, $newcontextid);
question_bank::get_qtype($qtype)->move_files($questionid, $oldcontextid, $newcontextid);
}

$subcatids = $DB->get_records_menu('question_categories',
Expand Down Expand Up @@ -851,12 +776,12 @@ function question_load_questions($questionids, $extrafields = '', $join = '') {
* @param boolean $loadtags load the question tags from the tags table. Optional, default false.
*/
function _tidy_question($question, $loadtags = false) {
global $CFG, $QTYPES;
if (!array_key_exists($question->qtype, $QTYPES)) {
$question->qtype = 'missingtype';
$question->questiontext = '<p>' . get_string('warningmissingtype', 'quiz') . '</p>' . $question->questiontext;
global $CFG;
if (question_bank::is_qtype_installed($question->qtype)) {
$question->questiontext = html_writer::tag('p', get_string('warningmissingtype',
'qtype_missingtype')) . $question->questiontext;
}
$QTYPES[$question->qtype]->get_question_options($question);
question_bank::get_qtype($question->qtype)->get_question_options($question);
if (isset($question->_partiallyloaded)) {
unset($question->_partiallyloaded);
}
Expand Down Expand Up @@ -940,9 +865,7 @@ function question_get_editing_head_contributions($question) {
* Simply calls the question type specific save_question_options() method.
*/
function save_question_options($question) {
global $QTYPES;

$QTYPES[$question->qtype]->save_question_options($question);
question_bank::get_qtype($question->qtype)->save_question_options($question);
}

/// CATEGORY FUNCTIONS /////////////////////////////////////////////////////////////////
Expand Down
16 changes: 0 additions & 16 deletions lib/simpletest/testquestionlib.php
Expand Up @@ -41,22 +41,6 @@ class questionlib_test extends UnitTestCase {

public static $includecoverage = array('lib/questionlib.php');

function test_question_sort_qtype_array() {
$config = new stdClass();
$config->multichoice_sortorder = '1';
$config->calculated_sortorder = '2';
$qtypes = array(
'frog' => 'toad',
'calculated' => 'newt',
'multichoice' => 'eft',
);
$this->assertEqual(question_sort_qtype_array($qtypes), array(
'multichoice' => 'eft',
'calculated' => 'newt',
'frog' => 'toad',
));
}

function test_question_reorder_qtypes() {
$this->assertEqual(question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', +1),
array(0 => 't2', 1 => 't1', 2 => 't3'));
Expand Down
60 changes: 46 additions & 14 deletions question/engine/bank.php
Expand Up @@ -55,6 +55,15 @@ abstract class question_bank {

private static $questionconfig = null;

/**
* @param string $qtypename a question type name, e.g. 'multichoice'.
* @return bool whether that question type is installed in this Moodle.
*/
public static function is_qtype_installed($qtypename) {
$plugindir = get_plugin_directory('qtype', $qtypename);
return $plugindir && is_readable($plugindir . '/questiontype.php');
}

/**
* Get the question type class for a particular question type.
* @param string $qtypename the question type name. For example 'multichoice' or 'shortanswer'.
Expand Down Expand Up @@ -131,43 +140,66 @@ public static function get_all_qtypes() {
try {
$qtypes[$plugin] = self::get_qtype($plugin);
} catch (Exception $e) {
// TODO ingore, but reivew this later.
// TODO ingore, but review this later.
}
}
return $qtypes;
}

/**
* @return array all the question types that users are allowed to create,
* sorted into the preferred order set on the admin screen.
* Sort an array of question types according to the order the admin set up,
* and then alphabetically for the rest.
* @param array qtype->name() => qtype->local_name().
* @return array sorted array.
*/
public static function get_creatable_qtypes() {
$config = self::get_config();
$allqtypes = self::get_all_qtypes();
public static function sort_qtype_array($qtypes, $config = null) {
if (is_null($config)) {
$config = self::get_config();
}

$sortorder = array();
$otherqtypes = array();
foreach ($allqtypes as $name => $qtype) {
if (!self::qtype_enabled($name)) {
unset($allqtypes[$name]);
continue;
}
foreach ($qtypes as $name => $localname) {
$sortvar = $name . '_sortorder';
if (isset($config->$sortvar)) {
$sortorder[$config->$sortvar] = $name;
} else {
$otherqtypes[$name] = $qtype->local_name();
$otherqtypes[$name] = $localname;
}
}

ksort($sortorder);
textlib_get_instance()->asort($otherqtypes);

$creatableqtypes = array();
$sortedqtypes = array();
foreach ($sortorder as $name) {
$creatableqtypes[$name] = $allqtypes[$name];
$sortedqtypes[$name] = $qtypes[$name];
}
foreach ($otherqtypes as $name => $notused) {
$sortedqtypes[$name] = $qtypes[$name];
}
return $sortedqtypes;
}

/**
* @return array all the question types that users are allowed to create,
* sorted into the preferred order set on the admin screen.
*/
public static function get_creatable_qtypes() {
$config = self::get_config();
$allqtypes = self::get_all_qtypes();

$qtypenames = array();
foreach ($allqtypes as $name => $qtype) {
if (self::qtype_enabled($name)) {
$qtypenames[$name] = $qtype->local_name();
}
}

$qtypenames = self::sort_qtype_array($qtypenames);

$creatableqtypes = array();
foreach ($qtypenames as $name => $notused) {
$creatableqtypes[$name] = $allqtypes[$name];
}
return $creatableqtypes;
Expand Down
62 changes: 62 additions & 0 deletions question/engine/simpletest/testquestionbank.php
@@ -0,0 +1,62 @@
<?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/>.

/**
* This file contains tests for the question_bank class.
*
* @package moodlecore
* @subpackage questionbank
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/


defined('MOODLE_INTERNAL') || die();

require_once(dirname(__FILE__) . '/../lib.php');


/**
*Unit tests for the {@link question_bank} class.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_bank_test extends UnitTestCase {

public function setUp() {
}

public function tearDown() {
}

function test_sort_qtype_array() {
$config = new stdClass();
$config->multichoice_sortorder = '1';
$config->calculated_sortorder = '2';
$qtypes = array(
'frog' => 'toad',
'calculated' => 'newt',
'multichoice' => 'eft',
);
$this->assertEqual(question_bank::sort_qtype_array($qtypes, $config), array(
'multichoice' => 'eft',
'calculated' => 'newt',
'frog' => 'toad',
));
}
}
8 changes: 4 additions & 4 deletions question/engine/upgradefromoldqe/upgrade.php
Expand Up @@ -506,7 +506,7 @@ public function __construct($logger) {
}

protected function load_question($questionid, $quizid) {
global $CFG, $QTYPES;
global $CFG;

if ($quizid) {
$question = get_record_sql("
Expand All @@ -531,14 +531,14 @@ protected function load_question($questionid, $quizid) {
unset($question->defaultgrade);
}

if (!array_key_exists($question->qtype, $QTYPES)) {
$qtype = question_bank::get_qtype($question->qtype, false);
if ($qtype->name() === 'missingtype') {
$this->logger->log_assumption("Dealing with question id {$question->id}
that is of an unknown type {$question->qtype}.");
$question->qtype = 'missingtype';
$question->questiontext = '<p>' . get_string('warningmissingtype', 'quiz') . '</p>' . $question->questiontext;
}

$QTYPES[$question->qtype]->get_question_options($question);
$qtype->get_question_options($question);

return $question;
}
Expand Down

0 comments on commit d649fb0

Please sign in to comment.