]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-8764 - In menus, show question categories from this course first, then published...
authortjhunt <tjhunt>
Mon, 5 Mar 2007 18:05:51 +0000 (18:05 +0000)
committertjhunt <tjhunt>
Mon, 5 Mar 2007 18:05:51 +0000 (18:05 +0000)
lib/questionlib.php
question/editlib.php
question/export.php
question/import.php

index 20e217541c95dabbc62015cae76b0e947d7521a6..ccbc2c74eebf16593da37dbad826a8aed1f453d3 100644 (file)
@@ -1578,7 +1578,7 @@ function question_category_options($courseid, $published = false, $only_editable
             SELECT cat.*, c.shortname AS coursename
             FROM {$CFG->prefix}question_categories cat, {$CFG->prefix}course c
             WHERE c.id = cat.course AND (cat.course = $courseid $publishsql)
-            ORDER BY cat.parent, cat.sortorder, cat.name ASC");
+            ORDER BY (CASE WHEN cat.course = $courseid THEN 0 ELSE 1 END), cat.parent, cat.sortorder, cat.name ASC");
     $categories = add_indented_names($categories);
 
     $categoriesarray = array();
index 9ad4be49a2127ff9eed8e1dba384fab6ac2dada4..cf429b9abd8b36e81387e0b037536ebf3e4f6508 100644 (file)
@@ -69,20 +69,18 @@ function get_questions_category( $category, $noparent=false, $recurse=true ) {
 * @param integer $courseid  The id of the course whose default category is wanted
 */
 function get_default_question_category($courseid) {
-
-    if ($categories = get_records_select("question_categories", "course = '$courseid' AND parent = '0'", "id")) {
-        foreach ($categories as $category) {
-            return $category;   // Return the first one (lowest id)
-        }
+    // If it already exists, just return it.
+    if ($category = get_records_select("question_categories", "course = '$courseid' AND parent = '0'", 'id', '*', '', 1)) {
+        return reset($category);
     }
 
     // Otherwise, we need to make one
+    $category = new stdClass;
     $category->name = get_string("default", "quiz");
     $category->info = get_string("defaultinfo", "quiz");
     $category->course = $courseid;
     $category->parent = 0;
-    // TODO: Figure out why we use 999 below
-    $category->sortorder = 999;
+    $category->sortorder = 999; // By default, all categories get this number, and are sorted alphabetically.
     $category->publish = 0;
     $category->stamp = make_unique_id_code();
 
@@ -100,27 +98,11 @@ function question_category_form($course, $current, $recurse=1, $showhidden=false
     global $CFG;
 
 /// Make sure the default category exists for this course
-    if (!$categories = get_records("question_categories", "course", $course->id, "id ASC")) {
-        if (!$category = get_default_question_category($course->id)) {
-            notify("Error creating a default category!");
-        }
-    }
+    get_default_question_category($course->id);
 
 /// Get all the existing categories now
-    if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
-        notify("Could not find any question categories!");
-        return false;    // Something is really wrong
-    }
+    $catmenu = question_category_options($course->id, true);
 
-    $categories = add_indented_names( $categories );
-    foreach ($categories as $key => $category) {
-       if ($catcourse = get_record("course", "id", $category->course)) {
-           if ($category->publish && $category->course != $course->id) {
-               $category->indentedname .= " ($catcourse->shortname)";
-           }
-           $catmenu[$category->id] = $category->indentedname;
-       }
-    }
     $strcategory = get_string("category", "quiz");
     $strshow = get_string("show", "quiz");
     $streditcats = get_string("editcategories", "quiz");
index d821827cac2613bf991e95bfc9de8aa47b102cd1..4437e6cef6e604a58a26f0d070d983c4b0d4cd02 100644 (file)
@@ -50,9 +50,7 @@
         }
     }
 
-    if (! $category = get_record("question_categories", "id", $categoryid)) {
-        $category = get_default_question_category($courseid);
-    }
+    $category = get_default_question_category($courseid);
 
     if (! $categorycourse = get_record("course", "id", $category->course)) {
         error( $txt->nocategory );
index 987bdb99def4cafb95ee38992e56a19d915e5633..034c3bbce5c044234aba253de0a33ed5558ce616 100644 (file)
     print_heading_with_help($txt->importquestions, "import", "quiz");
 
     /// Get all the existing categories now
-    if (has_capability('moodle/question:manage', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // the admin can import into all categories
-        if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
-            error("Could not find any question categories!"); // Something is really wrong
-        }
-    } else { // select only the categories to which the teacher has write access
-        $cats = get_records('question_categories');
-        $categories = array();
-        foreach ($cats as $cat) {
-            if (has_capability('moodle/question:managecategory', get_context_instance(CONTEXT_COURSE, $cat->course))) {
-                $categories[] = $cat;
-            }      
-        }
-        if (empty($categories)) {
-            error("Could not find any question categories!");
-        }
-    }
-    $categories = add_indented_names($categories);
-    foreach ($categories as $key => $cat) {
-       if ($catcourse = get_record("course", "id", $cat->course)) {
-           if ($cat->publish && $cat->course != $course->id) {
-               $cat->indentedname .= " ($catcourse->shortname)";
-           }
-           $catmenu[$cat->id] = $cat->indentedname;
-       }
-    }
+    $catmenu = question_category_options($course->id, false, true);
    
     //==========
     // DISPLAY