]> git.mjollnir.org Git - moodle.git/commitdiff
question bank: MDL-14434 Cannot edit the only question category in a context.
authortjhunt <tjhunt>
Thu, 27 Nov 2008 11:50:29 +0000 (11:50 +0000)
committertjhunt <tjhunt>
Thu, 27 Nov 2008 11:50:29 +0000 (11:50 +0000)
lib/questionlib.php
question/category_class.php
question/category_form.php
question/contextmove.php
question/editlib.php

index 52d45587d20e183313329242b7f7ab2b962a9058..225ea880b0e9290f348618e45480fc59482a606f 100644 (file)
@@ -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);
         }
index 38ec74c850bbbf6640b858ac5688cfe4d183963a..633fcc09c9e3afe473729f2a18fa33611b83ae32 100644 (file)
@@ -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);
index 6f6b1eb6d156353763a708f036ca9cec83e6be08..53a8be5cb7c16ee39abc57745910b16f98826473 100644 (file)
@@ -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'));
index 8a0c2b2e52263982e858e72227dffdfcc13bcc13..6953dc15d7ac89ddadb77a9b83bfedda200dba8e 100644 (file)
         }
 
         //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);
index 50b58d551ca0f97750a4c611247f97ac8f976e59..bf30d1dc530d8eb0e9fb97d2ca0f7fb1e7c7ff67 100644 (file)
@@ -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));
     }
 }