From 2113a65fddf338253eab9ec5601e63b48383b3d2 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Wed, 18 Mar 2009 04:45:47 +0000 Subject: [PATCH] question bank: MDL-18531 if someone can add questions in the quiz context, but no the course context, choose a more sensible default category in the question bank. --- lib/questionlib.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/questionlib.php b/lib/questionlib.php index 63a25ae6fbc..2d651c4561a 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -1886,7 +1886,14 @@ function question_category_select_menu($contexts, $top = false, $currentcat = 0, * @return object The default category - the category in the course context */ function question_make_default_categories($contexts) { + static $preferredlevels = array( + CONTEXT_COURSE => 4, + CONTEXT_MODULE => 3, + CONTEXT_COURSECAT => 2, + CONTEXT_SYSTEM => 1, + ); $toreturn = null; + $preferredness = 0; // If it already exists, just return it. foreach ($contexts as $key => $context) { if (!$categoryrs = get_recordset_select("question_categories", "contextid = '{$context->id}'", 'sortorder, name', '*', '', 1)) { @@ -1907,12 +1914,15 @@ function question_make_default_categories($contexts) { } } } - if ($context->contextlevel == CONTEXT_COURSE){ - $toreturn = clone($category); + if ($preferredlevels[$context->contextlevel] > $preferredness && has_capability('moodle/question:add', $context)) { + $toreturn = $category; + $preferredness = $preferredlevels[$context->contextlevel]; } } - + if (!is_null($toreturn)) { + $toreturn = clone($toreturn); + } return $toreturn; } @@ -2157,7 +2167,6 @@ class context_to_string_translator{ } - /** * Check capability on category * @param mixed $question object or id