From 5a1e28b4af1f7699974bb557528cc6e8b54a132e Mon Sep 17 00:00:00 2001 From: Steve Clay Date: Thu, 29 Sep 2011 15:46:33 -0400 Subject: [PATCH] My Moodle pagination, with admin settings --- admin/settings/appearance.php | 3 +- lang/en_utf8/admin.php | 6 ++- my/index.php | 78 ++++++++++++++++++++++++++++++++-------- 3 files changed, 68 insertions(+), 19 deletions(-) diff --git a/admin/settings/appearance.php b/admin/settings/appearance.php index 1278df2..c48dbe1 100644 --- a/admin/settings/appearance.php +++ b/admin/settings/appearance.php @@ -77,7 +77,8 @@ if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page $temp = new admin_settingpage('mymoodle', get_string('mymoodle', 'admin')); $temp->add(new admin_setting_configcheckbox('mymoodleredirect', get_string('mymoodleredirect', 'admin'), get_string('configmymoodleredirect', 'admin'), 0)); - $temp->add(new admin_setting_configtext('mycoursesperpage', get_string('mycoursesperpage', 'admin'), get_string('configmycoursesperpage', 'admin'), 21, PARAM_INT)); + $temp->add(new admin_setting_configtext('mycoursesperpage', get_string('mycoursesperpage', 'admin'), get_string('configmycoursesperpage', 'admin'), 20, PARAM_INT)); + $temp->add(new admin_setting_configtext('mycourseslimit', get_string('mycourseslimit', 'admin'), get_string('configmycourseslimit', 'admin'), 200, PARAM_INT)); $ADMIN->add('appearance', $temp); // new CFG variable for coursemanager (what roles to display) diff --git a/lang/en_utf8/admin.php b/lang/en_utf8/admin.php index 7d6a64a..2166ceb 100644 --- a/lang/en_utf8/admin.php +++ b/lang/en_utf8/admin.php @@ -182,7 +182,8 @@ $string['configminpasswordlower'] = 'Passwords must have at least these many low $string['configminpasswordnonalphanum'] = 'Passwords must have at least these many non-alphanumeric characters.'; $string['configminpasswordupper'] = 'Passwords must have at least these many upper case letters.'; $string['configmymoodleredirect'] = 'This setting forces redirects to /my on login for non-admins and replaces the top level site navigation with /my'; -$string['configmycoursesperpage'] = 'Maximum number of courses to display in any list of a user\'s own courses'; +$string['configmycoursesperpage'] = 'Number of courses to display per page in the user\'s course listing'; +$string['configmycourseslimit'] = 'This sets an upper limit on the query that returns courses to be displayed paginated. Since the pagination uses a naive algorithm, later pages still query for the earlier pages\' results, so it\'s wise to set an upper limit here.'; $string['confignodefaultuserrolelists'] = 'This setting prevents all users from being returned from the database from deprecated calls of get_course_user, etc., for the site course if the default role provides that access. Check this, if you suffer a performance hit.'; $string['confignonmetacoursesyncroleids'] = 'By default all role assignments from child courses are synchronised to metacourses. Roles that are selected here will not be included in the synchronisation process.'; $string['confignoreplyaddress'] = 'Emails are sometimes sent out on behalf of a user (eg forum posts). The email address you specify here will be used as the \"From\" address in those cases when the recipients should not be able to reply directly to the user (eg when a user chooses to keep their address private).'; @@ -543,7 +544,8 @@ $string['multilangupgradenotice'] = 'Your site is probably using old multilang s $string['mustenablestats'] = 'Stats have not yet been enabled on this site.'; $string['mymoodle'] = 'My Moodle'; $string['mymoodleredirect'] = 'Force users to use My Moodle'; -$string['mycoursesperpage'] = 'Maximum number of courses'; +$string['mycoursesperpage'] = 'Number of courses per page'; +$string['mycourseslimit'] = 'Limit to user\'s courses available in My Moodle'; $string['mysql416bypassed'] = 'However, if your site is using iso-8859-1 (latin) languages ONLY, you may continue using your currently installed MySQL 4.1.12 (or higher).'; $string['mysql416required'] = 'MySQL 4.1.16 is the minimum version required for Moodle 1.6 in order to guarantee that all data can be converted to UTF-8 in the future.'; $string['nobookmarksforuser'] = 'You do not have any bookmarks.'; diff --git a/my/index.php b/my/index.php index 28f6b18..5518ca4 100644 --- a/my/index.php +++ b/my/index.php @@ -74,29 +74,76 @@ print_container_start(TRUE); /// The main overview in the middle of the page - $courses_limit = 21; + + // Due to the complexity of get_my_courses(), it would be very difficult to have it do partial + // queries, so this algorithm simply requests one more than needed (up to a maximum total) and + // discards courses which appear on "previous" pages. If one more is available, the "next" button + // is shown, but links to previous pages are always provided. + $max_limit = 200; + if (isset($CFG->mycourseslimit)) { + $max_limit = ($CFG->mycourseslimit > 0) + ? $CFG->mycourseslimit + : 9999; + } + $per_page = 20; if (isset($CFG->mycoursesperpage)) { - $courses_limit = $CFG->mycoursesperpage; + $per_page = ($CFG->mycoursesperpage > 0) + ? $CFG->mycoursesperpage + : $max_limit; } - $morecourses = false; - if ($courses_limit > 0) { - $courses_limit = $courses_limit + 1; - } + $page_num = max(1, optional_param('page', 1, PARAM_INT)); + + $skip = ($page_num > 1) + ? $per_page * ($page_num - 1) + : 0; + + $limit = ($per_page * $page_num) + 1; + + $courses = get_my_courses($USER->id, 'visible DESC,sortorder ASC', '*', false, min($max_limit, $limit)); - $courses = get_my_courses($USER->id, 'visible DESC,sortorder ASC', '*', false, $courses_limit); $site = get_site(); + // remove site course + if (array_key_exists($site->id, $courses)) { + unset($courses[$site->id]); + } $course = $site; //just in case we need the old global $course hack - if (($courses_limit > 0) && (count($courses) >= $courses_limit)) { - //remove the 'marker' course that we retrieve just to see if we have more than $courses_limit + $total_returned = count($courses); + if ($skip >= $total_returned) { + // empty page (should be rare unless user entered large page number) + // locate the last page + $page_num = ceil($total_returned / $per_page); + $skip = $per_page * ($page_num - 1); + } + + // remove skipped courses (sure would be nice to do this in get_my_courses) + $courses = array_slice($courses, $skip, null, true); + + $are_more_courses = false; + if (count($courses) > $per_page) { + $are_more_courses = true; array_pop($courses); - $morecourses = true; } - if (array_key_exists($site->id,$courses)) { - unset($courses[$site->id]); + $pagination_elements = array(); + if ($page_num > 1 || $are_more_courses) { + // current page + $pagination_elements[] = " {$page_num} "; + $i = $page_num; + while (--$i) { + $query_string = ($i > 1) ? "?page={$i}" : ""; + array_unshift($pagination_elements, " {$i} "); + } + if ($are_more_courses) { + array_push($pagination_elements, " Next »"); + } } + $pagination = $pagination_elements + ? '

Page: ' . implode(' | ', $pagination_elements) . '

' + : ''; + + foreach ($courses as $c) { if (isset($USER->lastcourseaccess[$c->id])) { @@ -105,6 +152,8 @@ $courses[$c->id]->lastaccess = 0; } } + + echo $pagination; if (empty($courses)) { print_simple_box(get_string('nocourses','my'),'center'); @@ -112,10 +161,7 @@ print_overview($courses); } - // if more than 20 courses - if ($morecourses) { - echo '
...'; - } + echo $pagination; print_container_end(); echo ''; -- 1.7.5.4