]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-6041 - Proper fix that eliminates the magic number 99999 when getting lists of...
authortjhunt <tjhunt>
Fri, 15 Sep 2006 14:32:35 +0000 (14:32 +0000)
committertjhunt <tjhunt>
Fri, 15 Sep 2006 14:32:35 +0000 (14:32 +0000)
Also a few more ISNULL()s eliminated.

And a typo role_assignment -> role_assignments.

admin/admin.php
admin/creators.php
admin/roles/assign.php
course/report/participation/index.php
course/student.php
lib/datalib.php
lib/deprecatedlib.php
user/index.php

index 2bcb86cb3da32767e0e6b682059d1672a26bb3a3..5841ad1ec7a90c7c1c8108227a7a65aec8a26217 100644 (file)
 
     unset($adminarray);
 
+    $usercount =  get_users(false, '', true, $adminlist);
 
 /// Get search results excluding any current admins
     if (!empty($frm->searchtext) and $previoussearch) {
         $searchusers = get_users(true, $frm->searchtext, true, $adminlist, 'firstname ASC, lastname ASC',
-                                      '', '', 0, 99999, 'id, firstname, lastname, email');
-        $usercount = get_users(false, '', true, $adminlist);
+                                      '', '', 0, MAX_USERS_PER_PAGE, 'id, firstname, lastname, email');
     }
 
 /// If no search results then get potential users excluding current admins
     if (empty($searchusers)) {
-
-        $usercount =  get_users(false, '', true, $adminlist, 'firstname ASC, lastname ASC', '', '',
-                                0, 99999, 'id, firstname, lastname, email');
-
         $users = array();
 
         if ($usercount <= MAX_USERS_PER_PAGE) {
             if (!$users = get_users(true, '', true, $adminlist, 'firstname ASC, lastname ASC', '', '',
-                               0, 99999, 'id, firstname, lastname, email') ) {
+                               0, MAX_USERS_PER_PAGE, 'id, firstname, lastname, email') ) {
                 $users = array();
             }
         }
index 23388005a1429c3864fb540c04de84e8f34ddc50..567a3662bc46380c02417d5aef5949df15ebdbab 100755 (executable)
@@ -5,7 +5,7 @@
     
     require_once('../config.php');
 
-    define("MAX_USERS_PER_PAGE", 50);
+    define("MAX_USERS_PER_PAGE", 5000);
 
     if (! $site = get_site()) {
         redirect("$CFG->wwwroot/$CFG->admin/index.php");
 
     unset($creatorarray);
 
+    $usercount = get_users(false, '', true, $creatorlist);
 
 /// Get search results excluding any current admins
     if (!empty($frm->searchtext) and $previoussearch) {
         $searchusers = get_users(true, $frm->searchtext, true, $creatorlist, 'firstname ASC, lastname ASC',
-                                      '', '', 0, 99999, 'id, firstname, lastname, email');
-        $usercount = get_users(false, '', true, $creatorlist);
+                                      '', '', 0, MAX_USERS_PER_PAGE, 'id, firstname, lastname, email');
     }
 
 /// If no search results then get potential users excluding current creators
     if (empty($searchusers)) {
-        if (!$users = get_users(true, '', true, $creatorlist, 'firstname ASC, lastname ASC', '', '',
-                                0, 99999, 'id, firstname, lastname, email') ) {
-            $users = array();
+        $users = array();
+        if ($usercount <= MAX_USERS_PER_PAGE) {
+            if (!$users = get_users(true, '', true, $creatorlist, 'firstname ASC, lastname ASC', '', '',
+                                    0, MAX_USERS_PER_PAGE, 'id, firstname, lastname, email') ) {
+                $users = array();
+            }
         }
-        $usercount = count($users);
     }
 
     $searchtext = (isset($frm->searchtext)) ? $frm->searchtext : "";
index 6dc894dde41174c217fc06e1166bac9df0bd6990..c868d56782ba8e707beccce4304b0fec4dd93056 100755 (executable)
     $existinguserlist = implode(',', $existinguserarray);
     unset($existinguserarray);
 
+    $usercount = get_users(false, '', true, $existinguserlist);
+
 /// Get search results excluding any users already in this course
     if (($searchtext != '') and $previoussearch) {
         $searchusers = get_users(true, $searchtext, true, $existinguserlist, 'firstname ASC, lastname ASC',
-                                      '', '', 0, 99999, 'id, firstname, lastname, email');
-        $usercount = get_users(false, '', true, $existinguserlist);
+                                      '', '', 0, MAX_USERS_PER_PAGE, 'id, firstname, lastname, email');
     }
 
 /// If no search results then get potential students for this course excluding users already in course
     if (empty($searchusers)) {
-
-        $usercount = get_users(false, '', true, $existinguserlist, 'firstname ASC, lastname ASC', '', '',
-                              0, 99999, 'id, firstname, lastname, email') ;
         $users = array();
-
         if ($usercount <= MAX_USERS_PER_PAGE) {
-            $users = get_users(true, '', true, $existinguserlist, 'firstname ASC, lastname ASC', '', '',
-                               0, 99999, 'id, firstname, lastname, email');
+            if (!$users = get_users(true, '', true, $existinguserlist, 'firstname ASC, lastname ASC', '', '',
+                               0, MAX_USERS_PER_PAGE, 'id, firstname, lastname, email')) {
+                $users = array();
+            }
         }
 
     }
 
     print_footer($course);
 
-?>
+?>
\ No newline at end of file
index 972144b2930c21b13f95f0c8cfaf88a71b8d48db..ab92f96dd6e5555184ba783c98f550ea1d88d7ee 100644 (file)
@@ -3,6 +3,7 @@
     require_once('../../../config.php');
 
     define('DEFAULT_PAGE_SIZE', 20);
+    define('SHOW_ALL_PAGE_SIZE', 5000);
     
     $id         = required_param('id', PARAM_INT); // course id.
     $moduleid   = optional_param('moduleid', 0, PARAM_INT); // module id.
@@ -338,11 +339,11 @@ function checknos() {
 
         $table->print_html();
 
-        if ($perpage == 99999) {
+        if ($perpage == SHOW_ALL_PAGE_SIZE) {
             echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.DEFAULT_PAGE_SIZE.'">'.get_string('showperpage', '', DEFAULT_PAGE_SIZE).'</a></div>';
         }
         else if ($matchcount > 0 && $perpage < $matchcount) {
-            echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage=99999">'.get_string('showall', '', $matchcount).'</a></div>';
+            echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.SHOW_ALL_PAGE_SIZE.'">'.get_string('showall', '', $matchcount).'</a></div>';
         }
     
         echo '<br /><center>';
index 094a6b68d156c118eb7e5e640205d27f009a2516..08387e5cc1121d5834a40235e0b9264cb6f6f577 100644 (file)
@@ -98,7 +98,7 @@
 
 
 /// Get all existing students and teachers for this course.
-    if (!$students = get_course_students($course->id, "u.firstname ASC, u.lastname ASC", "", 0, 99999,
+    if (!$students = get_course_students($course->id, "u.firstname ASC, u.lastname ASC", "", 0, '',
                                          '', '', NULL, '', 'u.id,u.firstname,u.lastname,u.email')) {
         $students = array();
     }
 
     unset($existinguserarray);
 
+    $usercount = get_users(false, '', true, $existinguserlist);
 
 /// Get search results excluding any users already in this course
     if (($searchtext != '') and $previoussearch) {
         $searchusers = get_users(true, $searchtext, true, $existinguserlist, 'firstname ASC, lastname ASC',
-                                      '', '', 0, 99999, 'id, firstname, lastname, email');
-        $usercount = get_users(false, '', true, $existinguserlist);
+                                      '', '', 0, MAX_USERS_PER_PAGE, 'id, firstname, lastname, email');
     }
 
 /// If no search results then get potential students for this course excluding users already in course
     if (empty($searchusers)) {
-
-        $usercount = get_users(false, '', true, $existinguserlist, 'firstname ASC, lastname ASC', '', '',
-                              0, 99999, 'id, firstname, lastname, email') ;
         $users = array();
-
         if ($usercount <= MAX_USERS_PER_PAGE) {
-            $users = get_users(true, '', true, $existinguserlist, 'firstname ASC, lastname ASC', '', '',
-                               0, 99999, 'id, firstname, lastname, email');
+            if (!$users = get_users(true, '', true, $existinguserlist, 'firstname ASC, lastname ASC', '', '',
+                               0, MAX_USERS_PER_PAGE, 'id, firstname, lastname, email')) {
+                $users = array();
+            }
         }
 
     }
index ee0d6fd866e3857d29be140606d4273455b0c07f..338f9ff15a3ff8060fbfc0cfd91fccd019325ac6 100644 (file)
@@ -194,11 +194,17 @@ function get_site_users($sort='u.lastaccess DESC', $fields='*', $exceptions='')
  * @return object|false|int  {@link $USER} records unless get is false in which case the integer count of the records found is returned. False is returned if an error is encountered.
  */
 function get_users($get=true, $search='', $confirmed=false, $exceptions='', $sort='firstname ASC',
-                   $firstinitial='', $lastinitial='', $page=0, $recordsperpage=99999, $fields='*') {
+                   $firstinitial='', $lastinitial='', $page='', $recordsperpage='', $fields='*') {
 
     global $CFG;
+    
+    if ($get && !$recordsperpage) {
+        debugging('Call to get_users with $get = true no $recordsperpage limit. ' .
+                'On large installations, this will probably cause an out of memory error. ' .
+                'Please think again and change your code so that it does not try to ' .
+                'load so much data into memory.', E_USER_WARNING);
+    }
 
-    $limit     = sql_paging_limit($page, $recordsperpage);
     $LIKE      = sql_ilike();
     $fullname  = sql_fullname();
 
@@ -224,16 +230,10 @@ function get_users($get=true, $search='', $confirmed=false, $exceptions='', $sor
         $select .= ' AND lastname '. $LIKE .' \''. $lastinitial .'%\'';
     }
 
-    if ($sort and $get) {
-        $sort = ' ORDER BY '. $sort .' ';
-    } else {
-        $sort = '';
-    }
-
     if ($get) {
-        return get_records_select('user', $select .' '. $sort .' '. $limit, '', $fields);
+        return get_records_select('user', $select, $sort, $fields, $page, $recordsperpage);
     } else {
-        return count_records_select('user', $select .' '. $sort .' '. $limit);
+        return count_records_select('user', $select);
     }
 }
 
@@ -255,12 +255,16 @@ function get_users($get=true, $search='', $confirmed=false, $exceptions='', $sor
  * @todo Finish documenting this function
  */
 
-function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperpage=99999,
+function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperpage=0,
                            $search='', $firstinitial='', $lastinitial='') {
 
     global $CFG;
 
-    $limit     = sql_paging_limit($page, $recordsperpage);
+    if ($recordsperpage) {
+        $limit = sql_paging_limit($page, $recordsperpage);
+    } else {
+        $limit = '';
+    }
     $LIKE      = sql_ilike();
     $fullname  = sql_fullname();
 
@@ -1536,4 +1540,4 @@ function category_parent_visible($parent = 0) {
 }
 
 // vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
-?>
+?>
\ No newline at end of file
index 24bee407489e12eed378bc98b4e026f809a3d2e8..2bc9c695240b7a2499dfa1cfe77854f35113fa60 100644 (file)
@@ -641,7 +641,7 @@ function get_recent_enrolments($courseid, $timestart) {
  * @return object
  * @todo Finish documenting this function
  */
-function get_course_students($courseid, $sort='ul.timeaccess', $dir='', $page=0, $recordsperpage=99999,
+function get_course_students($courseid, $sort='ul.timeaccess', $dir='', $page='', $recordsperpage='',
                              $firstinitial='', $lastinitial='', $group=NULL, $search='', $fields='', $exceptions='') {
 
     global $CFG;
@@ -671,7 +671,6 @@ function get_course_students($courseid, $sort='ul.timeaccess', $dir='', $page=0,
                           $page, $recordsperpage, $fields ? $fields : '*');
     }
 
-    $limit     = sql_paging_limit($page, $recordsperpage);
     $LIKE      = sql_ilike();
     $fullname  = sql_fullname('u.firstname','u.lastname');
 
@@ -679,7 +678,7 @@ function get_course_students($courseid, $sort='ul.timeaccess', $dir='', $page=0,
 
     // make sure it works on the site course
     $context = get_context_instance(CONTEXT_COURSE, $courseid);
-    $select = "(ul.courseid = '$courseid' OR ISNULL(ul.courseid)) AND ";
+    $select = "(ul.courseid = '$courseid' OR ul.courseid IS NULL) AND ";
     if ($courseid == SITEID) {
         $select = '';
     }
@@ -723,10 +722,10 @@ function get_course_students($courseid, $sort='ul.timeaccess', $dir='', $page=0,
 
     $students = get_records_sql("SELECT $fields
                             FROM {$CFG->prefix}user u INNER JOIN
-                                 {$CFG->prefix}role_assignment ra on u.id=ra.userid LEFT OUTER JOIN
+                                 {$CFG->prefix}role_assignments ra on u.id=ra.userid LEFT OUTER JOIN
                                  {$CFG->prefix}user_lastaccess ul on ul.userid=ra.userid
                                  $groupmembers
-                            WHERE $select $search $sort $dir $limit");
+                            WHERE $select $search $sort $dir", $page, $recordsperpage);
 
     //if ($courseid != SITEID) {
     return $students;
index 2c26ace1c8c3688800bf57283ee4790a6ecc877e..7ade006948adb7c703ccf6a0ec85d5568abc2c5c 100644 (file)
@@ -8,6 +8,7 @@
     define('USER_SMALL_CLASS', 20);   // Below this is considered small
     define('USER_LARGE_CLASS', 200);  // Above this is considered large
     define('DEFAULT_PAGE_SIZE', 20);
+    define('SHOW_ALL_PAGE_SIZE', 5000);
 
     $group        = optional_param('group', -1, PARAM_INT);                   // Group to show
     $page         = optional_param('page', 0, PARAM_INT);                     // which page to show
@@ -387,7 +388,7 @@ function checkchecked(form) {
         $where  = "WHERE (r.contextid = $context->id OR r.contextid in $listofcontexts) 
                     AND u.deleted = 0 
                     AND r.roleid = $roleid 
-                    AND (ul.courseid = $course->id OR ISNULL(ul.courseid))";   
+                    AND (ul.courseid = $course->id OR ul.courseid IS NULL)";   
         $where .= get_lastaccess_sql($accesssince);
         
         $wheresearch = '';
@@ -584,11 +585,11 @@ function checkchecked(form) {
             echo '<input type="text" name="search" value="'.$search.'" />&nbsp;<input type="submit" value="'.get_string('search').'" /></p></form>'."\n";
         }
     
-        if ($perpage == 99999) {
+        if ($perpage == SHOW_ALL_PAGE_SIZE) {
             echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.DEFAULT_PAGE_SIZE.'">'.get_string('showperpage', '', DEFAULT_PAGE_SIZE).'</a></div>';
         }
         else if ($matchcount > 0 && $perpage < $matchcount) {
-            echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage=99999">'.get_string('showall', '', $matchcount).'</a></div>';
+            echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.SHOW_ALL_PAGE_SIZE.'">'.get_string('showall', '', $matchcount).'</a></div>';
         }
     } // end of if ($roleid);