diff --git a/lib/questionlib.php b/lib/questionlib.php index 52d45587d20..225ea880b0e 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -2348,7 +2348,7 @@ function question_categorylist($categoryid) { // returns a comma separated list of ids of the category and all subcategories $categorylist = $categoryid; - if ($subcategories = $DB->get_records('question_categories', array('parent'=>$categoryid), 'sortorder ASC', 'id, id')) { + if ($subcategories = $DB->get_records('question_categories', array('parent'=>$categoryid), 'sortorder ASC', 'id, 1')) { foreach ($subcategories as $subcategory) { $categorylist .= ','. question_categorylist($subcategory->id); } diff --git a/question/category_class.php b/question/category_class.php index 38ec74c850b..633fcc09c9e 100644 --- a/question/category_class.php +++ b/question/category_class.php @@ -435,8 +435,7 @@ class question_category_object { // Get the record we are updating. $oldcat = $DB->get_record('question_categories', array('id' => $updateid)); - $lastcategoryinthiscontext = !$DB->record_exists_select('question_categories', - 'contextid = ? AND id <> ?', array($oldcat->contextid, $updateid)); + $lastcategoryinthiscontext = question_is_only_toplevel_category_in_context($updateid); if (!empty($newparent) && !$lastcategoryinthiscontext) { list($parentid, $tocontextid) = explode(',', $newparent); @@ -451,10 +450,6 @@ class question_category_object { // If moving to another context, check permissions some more. if ($oldcat->contextid != $tocontextid){ - if ($lastcategoryinthiscontext) { - // Don't allow the last category in a context to be moved. - print_error('cannotmovecate', 'question', $this->pageurl->out(), $newname); - } $tocontext = get_context_instance_by_id($tocontextid); require_capability('moodle/question:managecategory', $tocontext); } @@ -470,7 +465,7 @@ class question_category_object { print_error('cannotupdatecate', 'question', $this->pageurl->out(), $newname); } - // If the question name has changed, rename any random questions in that category. + // 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); diff --git a/question/category_form.php b/question/category_form.php index 6f6b1eb6d15..53a8be5cb7c 100644 --- a/question/category_form.php +++ b/question/category_form.php @@ -16,15 +16,7 @@ class question_category_edit_form extends moodleform { $questioncategoryel = $mform->addElement('questioncategory', 'parent', get_string('parent', 'quiz'), array('contexts'=>$contexts, 'top'=>true, 'currentcat'=>$currentcat, 'nochildrenof'=>$currentcat)); $mform->setType('parent', PARAM_SEQUENCE); - // This next test is actually looking to see if $currentcat is the id of - // a category that already exists, and is the only top-level category in - // it context. If so, we stop it from being moved. - if (1 == $DB->count_records_sql("SELECT count(*) - FROM {question_categories} c1, - {question_categories} c2 - WHERE c2.id = ? - AND c1.contextid = c2.contextid - AND c1.parent = 0 AND c2.parent = 0", array($currentcat))){ + if (question_is_only_toplevel_category_in_context($currentcat)) { $mform->hardFreeze('parent'); } $mform->setHelpButton('parent', array('categoryparent', get_string('parent', 'quiz'), 'question')); diff --git a/question/contextmove.php b/question/contextmove.php index 8a0c2b2e522..6953dc15d7a 100644 --- a/question/contextmove.php +++ b/question/contextmove.php @@ -155,8 +155,9 @@ } //adjust sortorder before we make the cat a peer of it's new peers - $peers = $DB->get_records_select_menu('question_categories', "contextid = ? AND parent = ?", array($toparent->contextid, $toparent->id), - "sortorder ASC", "id, id"); + $peers = $DB->get_records_select_menu('question_categories', + 'contextid = ? AND parent = ?', array($toparent->contextid, $toparent->id), + 'sortorder ASC', 'id, 1'); $peers = array_keys($peers); if ($totop){ array_unshift($peers, $cattomove->id); diff --git a/question/editlib.php b/question/editlib.php index 50b58d551ca..bf30d1dc530 100644 --- a/question/editlib.php +++ b/question/editlib.php @@ -77,17 +77,32 @@ function get_questions_category( $category, $noparent=false, $recurse=true, $exp return $qresults; } +/** + * @param integer $categoryid a category id. + * @return boolean whether this is the only top-level category in a context. + */ +function question_is_only_toplevel_category_in_context($categoryid) { + global $DB; + return 1 == $DB->count_records_sql(" + SELECT count(*) + FROM {question_categories} c1, + {question_categories} c2 + WHERE c2.id = ? + AND c1.contextid = c2.contextid + AND c1.parent = 0 AND c2.parent = 0", array($categoryid)); +} -function question_can_delete_cat($todelete){ - global $CFG, $DB; - $record = $DB->get_record_sql("SELECT count(*) as count, c1.contextid as contextid FROM {question_categories} c1, - {question_categories} c2 WHERE c2.id = ? - AND c1.contextid = c2.contextid GROUP BY c1.contextid", array($todelete)); - $contextid = $record->contextid; - $count = $record->count; - if ($count < 2) { +/** + * Check whether this user is allowed to delete this category. + * + * @param integer $todelete a category id. + */ +function question_can_delete_cat($todelete) { + global $DB; + if (question_is_only_toplevel_category_in_context($todelete)) { print_error('cannotdeletecate', 'question'); } else { + $contextid = $DB->get_field('question_categories', 'contextid', array('id' => $todelete)); require_capability('moodle/question:managecategory', get_context_instance_by_id($contextid)); } }