]> git.mjollnir.org Git - moodle.git/commitdiff
Some fixes to handle very very big categories.
authormoodler <moodler>
Thu, 21 Aug 2003 09:33:07 +0000 (09:33 +0000)
committermoodler <moodler>
Thu, 21 Aug 2003 09:33:07 +0000 (09:33 +0000)
  - paging
  - more efficient SQL

course/category.php
course/lib.php
course/search.php
lib/datalib.php
lib/weblib.php

index c12853c14a1d49de7e24d70ecb892a25727c6ab6..0a556c22b6e949982a9658e28c6ba5e929131cc4 100644 (file)
@@ -7,6 +7,8 @@
        require_once("lib.php");
 
     require_variable($id);    // Category id
+    optional_variable($page, "0");     // which page to show
+    optional_variable($perpage, "20"); // how many per page
 
 
     if (!$site = get_site()) {
 
 /// Print out all the courses
 
-    if (!$courses = get_courses($category->id)) {
+    if (!$courses = get_courses($category->id, "sortorder ASC", "c.*", $totalcount, $page*$perpage, $perpage)) {
         print_heading(get_string("nocoursesyet"));
 
     } else {
 
+        echo "<center>";
+        print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&perpage=$perpage&");
+        echo "</center>";
+
         $strcourses  = get_string("courses");
         $strselect   = get_string("select");
         $stredit     = get_string("edit");
index 5857ca499f10b11afbd04a534d1e20825a4e84b4..c75c24ff8479469d21c6d8de2a9c074f92b18f8c 100644 (file)
@@ -899,7 +899,12 @@ function print_category_info($category, $depth) {
     for ($i=0; $i<$depth;$i++) {
         echo "<ul style=\"margin-bottom:0;margin-top:0\">";
     }
-    echo "<font size=+1><a $catlinkcss ".
+
+    $catimage = "";
+    if ($CFG->frontpage == FRONTPAGECOURSELIST) {
+        $catimage = "<img src=\"$pixpath/i/course.gif\ width=16 height=16 border=0>";
+    }
+    echo "<font size=+1>$catimage <a $catlinkcss ".
          "href=\"$CFG->wwwroot/course/category.php?id=$category->id\">$category->name</a></font>";
 
     if ($CFG->frontpage == FRONTPAGECOURSELIST) {
index b60cd5f6ac321bee93450e865427d605af7ed791..e97ae12f65fe3a4705ad91fe070b4f5c4dc198dd 100644 (file)
@@ -6,8 +6,8 @@
     require_once("lib.php");
 
     optional_variable($search, "");    // search words
-    optional_variable($page, "0");   // which page to show
-    optional_variable($perpage, "10");   // which page to show
+    optional_variable($page, "0");     // which page to show
+    optional_variable($perpage, "10"); // how many per page
 
     $search = trim(strip_tags($search));
 
index 409956c5e753b83f2e211b144f4b286e39a92051..53732ec1f7be443fa2077fbee710a2da4fc6d8a1 100644 (file)
@@ -1108,25 +1108,46 @@ function get_site () {
 }
 
 
-function get_courses($categoryid="all", $sort="sortorder ASC", $fields="*") {
+function get_courses($categoryid="all", $sort="sortorder ASC", $fields="c.*", 
+                     &$totalcount, $limitfrom="", $limitnum="") {
 /// Returns list of courses, for whole site, or category
 
-    if ($categoryid == "all") {
-        $courses = get_records("course", "", "", $sort, $fields);
-    } else {
-        $courses = get_records("course", "category", "$categoryid", $sort, $fields);
+    global $USER, $CFG;
+
+    $categoryselect = "";
+    if ($categoryid != "all") {
+        $categoryselect = "c.category = '$categoryid'";
     }
 
-    if ($courses) {  /// Remove unavailable courses from the list
-        foreach ($courses as $key => $course) {
-            if (!$course->visible) {
-                if (!isteacher($course->id)) {
-                    unset($courses[$key]);
-                }
-            }
+    $teachersonly = "";
+    $teachertable = "";
+    if (!empty($USER)) {  // May need to check they are a teacher
+        if (!isadmin()) {
+            $teachersonly = "AND ((c.visible = '1') OR (t.userid = '$USER->id' AND t.course = c.id))";
+            $teachertable = ", {$CFG->prefix}user_teachers t";
         }
     }
-    return $courses;
+
+    if ($limitfrom !== "") {
+        switch ($CFG->dbtype) {
+            case "mysql":
+                 $limit = "LIMIT $limitfrom,$limitnum";
+                 break;
+            case "postgres7":
+                 $limit = "LIMIT $limitnum OFFSET $limitfrom";
+                 break;
+            default: 
+                 $limit = "LIMIT $limitnum,$limitfrom";
+        }
+    } else {
+        $limit = "";
+    }
+
+    $selectsql = "{$CFG->prefix}course c $teachertable WHERE $categoryselect $teachersonly ";
+
+    $totalcount = count_records_sql("SELECT COUNT(*) FROM $selectsql");
+
+    return get_records_sql("SELECT $fields FROM $selectsql ORDER BY $sort $limit");
 }
 
 
index b2acc194c81f8c5ce1d38ea7b3f82f2a5228c638..06564935f359d927eea64f8ce019e54e77000c34 100644 (file)
@@ -1618,6 +1618,8 @@ function obfuscate_mailto($email, $label="") {
 function print_paging_bar($totalcount, $page, $perpage, $baseurl) {
 /// Prints a single paging bar to provide access to other pages  (usually in a search)
 
+    $maxdisplay = 30;
+
     if ($totalcount > $perpage) {
         echo "<p>".get_string("page").":";
         $count = 0;
@@ -1630,7 +1632,7 @@ function print_paging_bar($totalcount, $page, $perpage, $baseurl) {
             }
             $totalcount -= $perpage;
             $count++;
-            if ($count > 30) {
+            if ($count > $maxdisplay) {
                 echo "&nbsp;...";
                 break;
             }