From: tjhunt Date: Tue, 24 Feb 2009 05:16:23 +0000 (+0000) Subject: quiz editing: MDL-18173 Make clear in the UI whether a random question selects from... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=3cac440b7654b00a0def41515e90f3141f5852d6;p=moodle.git quiz editing: MDL-18173 Make clear in the UI whether a random question selects from subcategories or not. --- diff --git a/lang/en_utf8/qtype_random.php b/lang/en_utf8/qtype_random.php index 8fec9a9dd1..efa07f99ef 100644 --- a/lang/en_utf8/qtype_random.php +++ b/lang/en_utf8/qtype_random.php @@ -1,4 +1,6 @@ diff --git a/lang/en_utf8/question.php b/lang/en_utf8/question.php index caced23647..6a540c18af 100644 --- a/lang/en_utf8/question.php +++ b/lang/en_utf8/question.php @@ -46,6 +46,7 @@ $string['categorycurrent'] = 'Current Category'; $string['categorycurrentuse'] = 'Use This Category'; $string['categorymoveto'] = 'Save in Category'; $string['changepublishstatuscat'] = 'caturl\">Category \"$a->name\" in course \"$a->coursename\" will have it\'s sharing status changed from $a->changefrom to $a->changeto.'; +$string['chooseqtypetoadd'] = 'Choose a question type to add'; $string['clicktoflag'] = 'Click to flag this question'; $string['clicktounflag'] = 'Click to un-flag this question'; $string['cwrqpfs'] = 'Random questions selecting questions from sub categories.'; diff --git a/lang/en_utf8/quiz.php b/lang/en_utf8/quiz.php index 0b6ec476ea..045d229fa9 100644 --- a/lang/en_utf8/quiz.php +++ b/lang/en_utf8/quiz.php @@ -549,10 +549,14 @@ $string['quiztimer'] = 'Quiz Timer'; $string['quizwillopen'] = 'This quiz will open $a'; $string['random'] = 'Random Question'; $string['randomcreate'] = 'Create Random Questions'; +$string['randomfromcategory'] = 'Random question from all of category:'; +$string['randomfromcategoryonly'] = 'Random question from just category:'; +$string['randomnosubcat'] = 'Questions from this category only, not its subcategories.'; $string['randomsamatch'] = 'Random Short-Answer Matching'; $string['randomsamatchcreate'] = 'Create Random Short-Answer Matching questions'; $string['randomsamatchintro'] = 'For each of the following questions, select the matching answer from the menu.'; $string['randomsamatchnumber'] = 'Number of questions to select'; +$string['randomwithsubcat'] = 'Questions from this category and its subcategories.'; $string['readytosend'] = 'You are about to send your whole quiz to be graded. Are you sure you want to continue?'; $string['reattemptquiz'] = 'Re-attempt quiz'; $string['recentlyaddedquestion'] = 'Recently added question!'; @@ -722,7 +726,6 @@ $string['withselected'] = 'With selected'; $string['withsummary'] = 'with Summary Statistics'; $string['wronggrade'] = 'Wrong grade (after line $a) :'; $string['wronguse'] = 'You can not use this page like that'; -$string['xfromcategory'] = '$a from category:'; $string['xhtml'] = 'XHTML Format'; $string['xml'] = 'Moodle XML format'; $string['xmlimportnoname'] = 'Missing question name in xml file'; diff --git a/mod/quiz/editlib.php b/mod/quiz/editlib.php index 7a31dafc95..55fb73e0f6 100644 --- a/mod/quiz/editlib.php +++ b/mod/quiz/editlib.php @@ -705,7 +705,13 @@ function quiz_print_randomquestion(&$question, &$pageurl, &$quiz,$quiz_qbanktool echo '
'; print_question_icon($question); - echo ' ' . get_string('xfromcategory', 'quiz', get_string('random', 'quiz')) . '
'; + print_random_option_icon($question); + if (!empty($question->questiontext)) { + $string = 'randomfromcategory'; + } else { + $string = 'randomfromcategoryonly'; + } + echo ' ' . get_string($string, 'quiz') . ''; $a = new stdClass; $a->arrow = $THEME->rarrow; @@ -792,6 +798,7 @@ function quiz_print_singlequestion_reordertool($question, $returnurl, $quiz){ quiz_question_action_icons($quiz, $quiz->cmid, $question, $returnurl) . ''; echo "\n"; } + /** * Print a given random question in quiz for the reordertool tab of edit.php. * Meant to be used from quiz_print_question_list() @@ -800,7 +807,6 @@ function quiz_print_singlequestion_reordertool($question, $returnurl, $quiz){ * @param object $questionurl The url of the question editing page as a moodle_url object * @param object $quiz The quiz in the context of which the question is being displayed */ - function quiz_print_randomquestion_reordertool(&$question, &$pageurl, &$quiz){ global $DB, $QTYPES; @@ -819,6 +825,7 @@ function quiz_print_randomquestion_reordertool(&$question, &$pageurl, &$quiz){ echo '
'; echo $reordercheckboxlabel; print_question_icon($question); + print_random_option_icon($question); if ($questioncount == 0) { echo ''; @@ -841,6 +848,25 @@ function quiz_print_randomquestion_reordertool(&$question, &$pageurl, &$quiz){ echo '
'; } + +/** + * Print an icon to indicate the 'include subcategories' state of a random question. + * @param $question the random question. + */ +function print_random_option_icon($question) { + global $CFG; + if (!empty($question->questiontext)) { + $icon = 'withsubcat'; + $tooltip = get_string('randomwithsubcat', 'quiz'); + } else { + $icon = 'nosubcat'; + $tooltip = get_string('randomnosubcat', 'quiz'); + } + echo '' .
+            $tooltip . ''; + +} + /** * Creates a textual representation of a question for display. * diff --git a/pix/i/nosubcat.png b/pix/i/nosubcat.png new file mode 100644 index 0000000000..09fde4e832 Binary files /dev/null and b/pix/i/nosubcat.png differ diff --git a/pix/i/withsubcat.png b/pix/i/withsubcat.png new file mode 100644 index 0000000000..bafe55aa33 Binary files /dev/null and b/pix/i/withsubcat.png differ diff --git a/question/category_class.php b/question/category_class.php index 633fcc09c9..fb489afe82 100644 --- a/question/category_class.php +++ b/question/category_class.php @@ -467,9 +467,13 @@ class question_category_object { // If the category name has changed, rename any random questions in that category. if ($oldcat->name != $cat->name) { - $randomqname = $QTYPES[RANDOM]->question_name($cat); - $DB->set_field('question', 'name', $randomqname, array('category' => $cat->id), 'qtype', RANDOM); - // Ignore errors here. It is not a big deal if the questions are not renamed. + $where = "qtype = 'random' AND category = ? AND " . $DB->sql_compare_text('questiontext') . " = ?"; + + $randomqname = $QTYPES[RANDOM]->question_name($cat, false); + $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '0')); + + $randomqname = $QTYPES[RANDOM]->question_name($cat, true); + $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '1')); } // Then redirect to an appropriate place. diff --git a/question/type/multichoice/db/upgrade.php b/question/type/multichoice/db/upgrade.php index cc8041c11b..04087a1bc8 100644 --- a/question/type/multichoice/db/upgrade.php +++ b/question/type/multichoice/db/upgrade.php @@ -26,6 +26,33 @@ function xmldb_qtype_multichoice_upgrade($oldversion) { $dbman = $DB->get_manager(); $result = true; + // This upgrade actually belongs to the random question type, + // but that does not have a DB upgrade script. Therefore, multichoice + // is doing it. + // Rename random questions to give them more helpful names. + if ($result && $oldversion < 2008021800) { + require_once($CFG->libdir . '/questionlib.php'); + // Get all categories containing random questions. + $categories = $DB->get_recordset_sql(" + SELECT qc.id, qc.name + FROM {question_categories} qc + JOIN {question} q ON q.category = qc.id + WHERE q.qtype = 'random' + GROUP BY qc.id, qc.name"); + + // Rename the random qusetions in those categories. + $where = "qtype = 'random' AND category = ? AND " . $DB->sql_compare_text('questiontext') . " = ?"; + foreach ($categories as $cat) { + $randomqname = $QTYPES[RANDOM]->question_name($cat, false); + $result = $result && $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '0')); + + $randomqname = $QTYPES[RANDOM]->question_name($cat, true); + $result = $result && $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '1')); + } + + upgrade_plugin_savepoint($result, 2008021800, 'qtype', 'multichoice'); + } + return $result; } diff --git a/question/type/multichoice/version.php b/question/type/multichoice/version.php index 536367b0ac..c52c71debb 100644 --- a/question/type/multichoice/version.php +++ b/question/type/multichoice/version.php @@ -1,6 +1,6 @@ version = 2007081700; +$plugin->version = 2009021800; $plugin->requires = 2007101000; ?> diff --git a/question/type/random/questiontype.php b/question/type/random/questiontype.php index f9089b6db6..0655be19d1 100644 --- a/question/type/random/questiontype.php +++ b/question/type/random/questiontype.php @@ -103,9 +103,17 @@ class random_qtype extends default_questiontype { /** * Random questions always get a question name that is Random (cateogryname). * This function is a centralised place to calculate that, given the category. + * @param object $category the category this question picks from. (Only $category->name is used.) + * @param boolean $includesubcategories whether this question also picks from subcategories. + * @return string the name this question should have. */ - function question_name($category) { - return get_string('random', 'quiz') .' ('. $category->name .')'; + function question_name($category, $includesubcategories) { + if ($includesubcategories) { + $string = 'randomqplusname'; + } else { + $string = 'randomqname'; + } + return get_string($string, 'qtype_random', $category->name); } function save_question($question, $form, $course) { @@ -129,7 +137,7 @@ class random_qtype extends default_questiontype { if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) { print_error('cannotretrieveqcat', 'question'); } - $updateobject->name = $this->question_name($category); + $updateobject->name = $this->question_name($category, !empty($question->questiontext)); return $DB->update_record('question', $updateobject); }