Skip to content

Commit

Permalink
MDL-61132 Quiz: "Random" question from "Top" categories
Browse files Browse the repository at this point in the history
* Support for adding ranodm questions from "Top" categories
  • Loading branch information
rezaies committed Feb 5, 2018
1 parent 9275220 commit 3b8f319
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 7 deletions.
17 changes: 17 additions & 0 deletions lib/questionlib.php
Expand Up @@ -1030,6 +1030,23 @@ function question_get_top_category($contextid, $create = false) {
return $category;
}

/**
* Gets the list of top categories in the given contexts in the array("categoryid,categorycontextid") format.
*
* @param array $contextids List of context ids
* @return array
*/
function question_get_top_categories_for_contexts($contextids) {
global $DB;

$concatsql = $DB->sql_concat_join("','", ['id', 'contextid']);
list($insql, $params) = $DB->get_in_or_equal($contextids);
$sql = "SELECT $concatsql FROM {question_categories} WHERE contextid $insql AND parent = 0";
$topcategories = $DB->get_fieldset_sql($sql, $params);

return $topcategories;
}

/**
* Gets the default category in the most specific context.
* If no categories exist yet then default ones are created in all contexts.
Expand Down
4 changes: 4 additions & 0 deletions mod/quiz/addrandom.php
Expand Up @@ -85,6 +85,10 @@
if (!empty($data->existingcategory)) {
list($categoryid) = explode(',', $data->category);
$includesubcategories = !empty($data->includesubcategories);
if (!$includesubcategories) {
// If the chosen category is a top category.
$includesubcategories = $DB->record_exists('question_categories', ['id' => $categoryid, 'parent' => 0]);
}
$returnurl->param('cat', $data->category);

} else if (!empty($data->newcategory)) {
Expand Down
6 changes: 4 additions & 2 deletions mod/quiz/addrandomform.php
Expand Up @@ -37,7 +37,6 @@
class quiz_add_random_form extends moodleform {

protected function definition() {
global $CFG, $DB;
$mform =& $this->_form;
$mform->setDisableShortforms();

Expand All @@ -49,11 +48,14 @@ protected function definition() {
get_string('randomfromexistingcategory', 'quiz'));

$mform->addElement('questioncategory', 'category', get_string('category'),
array('contexts' => $usablecontexts, 'top' => false));
array('contexts' => $usablecontexts, 'top' => true));
$mform->setDefault('category', $this->_customdata['cat']);

$mform->addElement('checkbox', 'includesubcategories', '', get_string('recurse', 'quiz'));

$tops = question_get_top_categories_for_contexts(array_column($contexts->all(), 'id'));
$mform->hideIf('includesubcategories', 'category', 'in', $tops);

$mform->addElement('select', 'numbertoadd', get_string('randomnumber', 'quiz'),
$this->get_number_of_questions_to_add_choices());

Expand Down
5 changes: 4 additions & 1 deletion question/type/random/edit_random_form.php
Expand Up @@ -48,11 +48,14 @@ protected function definition() {
$mform->addElement('header', 'generalheader', get_string("general", 'form'));

$mform->addElement('questioncategory', 'category', get_string('category', 'question'),
array('contexts' => $this->contexts->having_cap('moodle/question:useall')));
array('contexts' => $this->contexts->having_cap('moodle/question:useall'), 'top' => true));

$mform->addElement('advcheckbox', 'questiontext[text]',
get_string('includingsubcategories', 'qtype_random'), null, null, array(0, 1));

$tops = question_get_top_categories_for_contexts(array_column($this->contexts->all(), 'id'));
$mform->hideIf('questiontext[text]', 'category', 'in', $tops);

$mform->addElement('hidden', 'qtype');
$mform->setType('qtype', PARAM_ALPHA);

Expand Down
5 changes: 5 additions & 0 deletions question/type/random/lang/en/qtype_random.php
Expand Up @@ -29,6 +29,11 @@
$string['pluginname_help'] = 'A random question is not a question type as such, but is a way of inserting a randomly-chosen question from a specified category into an activity.';
$string['pluginnameediting'] = 'Editing a random question';
$string['randomqname'] = 'Random ({$a})';
$string['randomqnamefromtop'] = 'Faulty random question! Please delete this question.';
$string['randomqplusname'] = 'Random ({$a} and subcategories)';
$string['randomqplusnamecourse'] = 'Random (Any category in this course)';
$string['randomqplusnamecoursecat'] = 'Random (Any category inside course category {$a})';
$string['randomqplusnamemodule'] = 'Random (Any category of this quiz)';
$string['randomqplusnamesystem'] = 'Random (Any system-level category)';
$string['selectedby'] = '{$a->questionname} selected by {$a->randomname}';
$string['selectmanualquestions'] = 'Random questions can use manually graded questions';
38 changes: 34 additions & 4 deletions question/type/random/questiontype.php
Expand Up @@ -127,12 +127,36 @@ public function get_question_options($question) {
* @return string the name this question should have.
*/
public function question_name($category, $includesubcategories) {
if ($includesubcategories) {
$string = 'randomqplusname';
if ($category->parent && $includesubcategories) {
$name = get_string('randomqplusname', 'qtype_random', shorten_text($category->name, 100));
} else if ($category->parent) {
$name = get_string('randomqname', 'qtype_random', shorten_text($category->name, 100));
} else if ($includesubcategories) {
$context = context::instance_by_id($category->contextid);

switch ($context->contextlevel) {
case CONTEXT_MODULE:
$name = get_string('randomqplusnamemodule', 'qtype_random');
break;
case CONTEXT_COURSE:
$name = get_string('randomqplusnamecourse', 'qtype_random');
break;
case CONTEXT_COURSECAT:
$name = get_string('randomqplusnamecoursecat', 'qtype_random',
shorten_text($context->get_context_name(false), 100));
break;
case CONTEXT_SYSTEM:
$name = get_string('randomqplusnamesystem', 'qtype_random');
break;
default: // Impossible.
$name = '';
}
} else {
$string = 'randomqname';
// No question will ever be selected. So, let's warn the teacher.
$name = get_string('randomqnamefromtop', 'qtype_random');
}
return get_string($string, 'qtype_random', shorten_text($category->name, 100));

return $name;
}

protected function set_selected_question_name($question, $randomname) {
Expand All @@ -143,11 +167,17 @@ protected function set_selected_question_name($question, $randomname) {
}

public function save_question($question, $form) {
global $DB;

$form->name = '';
list($category) = explode(',', $form->category);

// In case someone set the question text to true/false in the old style, set it properly.
if ($form->questiontext['text']) {
$form->questiontext['text'] = '1';
} else if ($DB->record_exists('question_categories', ['id' => $category, 'parent' => 0])) {
// The chosen category is a top category.
$form->questiontext['text'] = '1';
} else {
$form->questiontext['text'] = '0';
}
Expand Down

0 comments on commit 3b8f319

Please sign in to comment.