Index: lib/datalib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/datalib.php,v
retrieving revision 1.356
diff -u -r1.356 datalib.php
--- lib/datalib.php	4 Jan 2007 02:52:44 -0000	1.356
+++ lib/datalib.php	12 Jan 2007 14:08:51 -0000
@@ -670,19 +670,65 @@
 
 
 /**
- * List of courses that a user is a member of.
+ * List of courses that a user has access to view. Note that for admins,
+ * this usually includes every course on the system.
  *
  * @uses $CFG
  * @param int $userid The user of interest
  * @param string $sort the sortorder in the course table
  * @param string $fields  the fields to return
+ * @param bool $doanything True if using the doanything flag
+ * @param int $limit Maximum number of records to return, or 0 for unlimited
  * @return array {@link $COURSE} of course objects
  */
-function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields='*', $doanything=false) {
+function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields='*', $doanything=false,$limit=0) {
 
     $mycourses = array();
+    
+    // Fix fields to refer to the course table c
+    $fields=preg_replace('/([a-z0-9*]+)/','c.$1',$fields);
 
-    $rs = get_recordset('course', '', '', $sort, $fields);
+    // Attempt to filter the list of courses in order to reduce the number
+    // of queries in the next part.
+    
+    // Check root permissions
+    $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); 
+    if(has_capability('moodle/course:view',$sitecontext,$userid,$doanything)) {
+        // User can view all courses, although there might be exceptions
+        // which we will filter later.
+        $rs = get_recordset('course c', '', '', $sort, $fields);        
+    } else {
+        // The only other context level above courses that applies to moodle/course:view 
+        // is category. So we consider:
+        // 1. All courses in which the user is assigned a role
+        // 2. All courses in categories in which the user is assigned a role
+        // 3. All courses which have overrides for moodle/course:view
+        // Remember that this is just a filter. We check each individual course later. 
+        // However for a typical student on a large system this can reduce the
+        // number of courses considered from around 2,000 to around 2, with corresponding
+        // reduction in the number of queries needed.
+        global $CFG;
+        $rs=get_recordset_sql(" 
+SELECT 
+    $fields 
+FROM 
+    {$CFG->prefix}role_assignments ra
+    INNER JOIN {$CFG->prefix}context x ON x.id=ra.contextid
+    LEFT JOIN {$CFG->prefix}course_categories a ON x.instanceid=a.id AND x.contextlevel=40
+    INNER JOIN {$CFG->prefix}course c ON (x.instanceid=c.id AND x.contextlevel=50) OR c.category=a.id
+WHERE 
+    ra.userid=$userid
+UNION DISTINCT
+SELECT 
+    $fields 
+FROM 
+    {$CFG->prefix}role_capabilities ca
+    INNER JOIN {$CFG->prefix}context x ON x.id=ca.contextid AND x.contextlevel=50
+    INNER JOIN {$CFG->prefix}course c ON c.id=x.instanceid
+WHERE 
+    contextid <> 1 AND capability='moodle/course:view'
+ORDER BY $sort");           
+    } 
 
     if ($rs && $rs->RecordCount() > 0) {
         while (!$rs->EOF) {
@@ -697,6 +743,14 @@
                     !has_capability('moodle/legacy:guest', $context, $userid, false) &&
                     ($course->visible || has_capability('moodle/course:viewhiddencourses', $context, $userid))) {
                     $mycourses[$course->id] = $course;
+
+                    // Only return a limited number of courses if limit is set
+                    if($limit>0) {
+                        $limit--;
+                        if($limit==0) {
+                            break;
+                        }
+                    }
                 }
             }
 
Index: user/view.php
===================================================================
RCS file: /cvsroot/moodle/moodle/user/view.php,v
retrieving revision 1.140
diff -u -r1.140 view.php
--- user/view.php	12 Jan 2007 07:41:34 -0000	1.140
+++ user/view.php	12 Jan 2007 14:08:51 -0000
@@ -290,7 +290,8 @@
         print_row(get_string('msnid').':', s($user->msn));
     }
 
-    if ($mycourses = get_my_courses($user->id)) {
+    if ($mycourses = get_my_courses($user->id,'visible DESC,sortorder ASC', '*', false, 21)) {
+        $shown=0;
         $courselisting = '';
         foreach ($mycourses as $mycourse) {
             if ($mycourse->visible and $mycourse->category) {
@@ -301,6 +302,11 @@
                     $courselisting .= "$mycourse->fullname, ";
                 }
             }
+            $shown++;
+            if($shown==20) {
+                $courselisting.= "...";
+                break;
+            }
         }
         print_row(get_string('courses').':', rtrim($courselisting,', '));
     }
