Some fixes to handle very very big categories.
- paging - more efficient SQL
This commit is contained in:
+7
-1
@@ -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()) {
|
||||
@@ -211,11 +213,15 @@
|
||||
|
||||
/// 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");
|
||||
|
||||
+6
-1
@@ -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) {
|
||||
|
||||
+2
-2
@@ -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));
|
||||
|
||||
|
||||
+34
-13
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
|
||||
+3
-1
@@ -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 " ...";
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user