MDL-61132 Quiz: "Random" question from "Top" categories

* Support for adding ranodm questions from "Top" categories
This commit is contained in:
Shamim Rezaie 2018-01-30 13:46:39 +11:00
parent 9275220de3
commit 3b8f3198de
6 changed files with 68 additions and 7 deletions

View file

@ -48,11 +48,14 @@ class qtype_random_edit_form extends question_edit_form {
$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);

View file

@ -29,6 +29,11 @@ $string['pluginname'] = 'Random';
$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';

View file

@ -127,12 +127,36 @@ class qtype_random extends question_type {
* @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) {
@ -143,11 +167,17 @@ class qtype_random extends question_type {
}
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';
}