Index: datalib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/datalib.php,v
retrieving revision 1.365.2.16
diff -u -r1.365.2.16 datalib.php
--- datalib.php	14 Jun 2007 08:51:57 -0000	1.365.2.16
+++ datalib.php	26 Jun 2007 13:14:43 -0000
@@ -738,6 +869,7 @@
         // 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
+        // 2BIS. All courses in subcategories in which the user gets assignment because he is assigned in one of its ascendant categories
         // 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
@@ -764,6 +896,16 @@
 WHERE 
     ra.userid=$userid
 UNION
+SELECT
+    $fields
+FROM
+    {$CFG->prefix}role_assignments ra
+    INNER JOIN {$CFG->prefix}context x ON x.id=ra.contextid
+    INNER JOIN {$CFG->prefix}course_categories a ON (LOCATE(CONCAT('/',x.instanceid,'/'), a.path) > 0) AND x.contextlevel=40
+    INNER JOIN {$CFG->prefix}course c ON c.category=a.id
+WHERE
+    ra.userid=$userid
+UNION
 SELECT 
     $fields 
 FROM 
@@ -806,56 +948,6 @@
         }
     }
 
-    /// MDL-9238, course in sub categories are not shown
-    // if the user has course:view at system level, then he can view all course
-    // skip this part
-    if (!$candoanything && !has_capability('moodle/course:view',$sitecontext,$userid,$doanything)) {
-        
-        // get all course categories with an assignment
-        $SQL = "SELECT a.id, a.id FROM {$CFG->prefix}role_assignments ra
-                INNER JOIN {$CFG->prefix}context x ON x.id=ra.contextid
-                INNER JOIN {$CFG->prefix}course_categories a ON x.instanceid=a.id AND x.contextlevel=40
-                WHERE ra.userid=$userid";
-        
-        if ($mcoursecats = get_records_sql($SQL)) {          
-            foreach ($mcoursecats as $mcoursecat) {
-                
-                // run the sql to get the path, find all courses in each sub (sub) categories
-                $pathsql = "SELECT $fields 
-                            FROM {$CFG->prefix}course_categories cc,
-                                 {$CFG->prefix}course c
-                            WHERE cc.path LIKE '%".$mcoursecat->id."/%'
-                            AND c.category = cc.id";
-
-                if ($scourses = get_records_sql($pathsql)) {                   
-                    
-                    // add each course in sub category, if correct permissions are set
-                    // and if the course is not added to my courses list yet
-                    foreach ($scourses as $scourse) {
-                        $context = get_context_instance(CONTEXT_COURSE, $scourse->id);                        
-                        if (!isset($mycourses[$scourse->id]) &&
-                            has_capability('moodle/course:view', $context, $userid, $doanything) && 
-                            !has_capability('moodle/legacy:guest', $context, $userid, false) &&
-                            ($scourse->visible || 
-                             has_capability('moodle/course:viewhiddencourses', $context, $userid))) {
-                                // add it to my course array
-                            $mycourses[$scourse->id] = $scourse;
-                        }
-                        
-                        // Only return a limited number of courses if limit is set
-                        if($limit>0) {
-                            $limit--;
-                            if($limit==0) {
-                                // breaks the 2 foreach loops
-                                break 2;
-                            }
-                        }
-                    }
-                }
-            }
-        }
-    }
-
     // Cache if using default params...
     if (!empty($USER->id) && ($USER->id == $userid) && $usingdefaults) {
         $USER->mycourses[$doanything] = $mycourses;
