]> git.mjollnir.org Git - moodle.git/commitdiff
new sql to fetch question counts for categories all in one sql query. Question count...
authorjamiesensei <jamiesensei>
Mon, 21 May 2007 09:26:17 +0000 (09:26 +0000)
committerjamiesensei <jamiesensei>
Mon, 21 May 2007 09:26:17 +0000 (09:26 +0000)
lib/questionlib.php

index 1c819030ade64fa528796c46e16b3cee6a2900ea..ebd60fd906b57832a378df5e50e02eb0ea83c3cb 100644 (file)
@@ -1680,11 +1680,17 @@ function question_category_options($contexts, $top = false, $currentcat = 0, $po
 
 
     $categories = get_records_sql("
-            SELECT *
-            FROM {$CFG->prefix}question_categories
-            WHERE contextid IN ($contextslist)
+            SELECT c.*, count(q.id) as questioncount
+            FROM {$CFG->prefix}question_categories as c
+            LEFT JOIN {$CFG->prefix}question as q ON c.id = q.category
+            WHERE c.contextid IN ($contextslist) AND 
+            (q.id IS NULL OR (q.hidden='0' AND q.parent='0'))
+            GROUP BY c.id
             ORDER BY parent, sortorder, name ASC");
+
+
     $categories = question_add_context_in_key($categories);
+
     if ($top){
         $categories = question_add_tops($categories, $pcontexts);
     }
@@ -1698,7 +1704,8 @@ function question_category_options($contexts, $top = false, $currentcat = 0, $po
             if ($category->contextid == $pcontext){ 
                 $cid = $category->id;
                 if ($currentcat!= $cid) {
-                    $categoriesarray[$contextstring][$cid] = $category->indentedname;
+                    $countstring = (!empty($category->questioncount))?"($category->questioncount)":'';
+                    $categoriesarray[$contextstring][$cid] = $category->indentedname.$countstring;
                 }
             }
         }