Skip to content

Commit

Permalink
Some fixes to handle very very big categories.
Browse files Browse the repository at this point in the history
  - paging
  - more efficient SQL
  • Loading branch information
moodler committed Aug 21, 2003
1 parent abe2920 commit 8ef9cb5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
8 changes: 7 additions & 1 deletion course/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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");
Expand Down
7 changes: 6 additions & 1 deletion course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions course/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
47 changes: 34 additions & 13 deletions lib/datalib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}


Expand Down
4 changes: 3 additions & 1 deletion lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -1630,7 +1632,7 @@ function print_paging_bar($totalcount, $page, $perpage, $baseurl) {
}
$totalcount -= $perpage;
$count++;
if ($count > 30) {
if ($count > $maxdisplay) {
echo "&nbsp;...";
break;
}
Expand Down

0 comments on commit 8ef9cb5

Please sign in to comment.