]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10161 - Since get_course_... (users, students, teachers) is still being used...
authormchurch <mchurch>
Tue, 19 Jun 2007 20:38:27 +0000 (20:38 +0000)
committermchurch <mchurch>
Tue, 19 Jun 2007 20:38:27 +0000 (20:38 +0000)
admin/settings/users.php
lang/en_utf8/admin.php
lib/deprecatedlib.php

index 8ea22652d505a0ab3d3949ed8e4f98b7ba1994a8..97094d18fd3bf336da425a3dd52807063eb00dee 100644 (file)
@@ -67,6 +67,9 @@ $temp->add(new admin_setting_configselect('guestroleid', get_string('guestroleid
               get_string('configguestroleid', 'admin'), $guestrole->id, $allroles));
 $temp->add(new admin_setting_configselect('defaultuserroleid', get_string('defaultuserroleid', 'admin'),
               get_string('configdefaultuserroleid', 'admin'), $userrole->id, $allroles));
+
+$temp->add(new admin_setting_configcheckbox('nodefaultuserrolelists', get_string('nodefaultuserrolelists', 'admin'), get_string('confignodefaultuserrolelists', 'admin'), 0));
+
 $temp->add(new admin_setting_configselect('defaultcourseroleid', get_string('defaultcourseroleid', 'admin'),
               get_string('configdefaultcourseroleid', 'admin'), $studentrole->id, $allroles));
 $temp->add(new admin_setting_configselect('creatornewroleid', get_string('creatornewroleid', 'admin'),
index 0b2cd4ae320782dd513dd0f432015a9127794723..862aea495cdc2a07dd147cfb7a11b012f60503ed 100644 (file)
@@ -142,6 +142,7 @@ $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 breadcrumb with /my';
+$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 enrolments 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).';
 $string['confignotifyloginfailures'] = 'If login failures have been recorded, email notifications can be sent out.  Who should see these notifications?';
@@ -412,6 +413,7 @@ $string['mymoodleredirect'] = 'Force users to use 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.';
+$string['nodefaultuserrolelists'] = 'Don\'t return all default role users';
 $string['nolangupdateneeded'] = 'All your language packs are up to date, no update is needed';
 $string['nonewsettings'] = 'No new settings were added during this upgrade.';
 $string['nonexistentbookmark'] = 'The bookmark you requested does not exist.';
index 3193082584fd201c11cf2ee16c4770ebaf2c065f..41bf6bb34636851cfdd8b775c1852fd404e17f4c 100644 (file)
@@ -578,28 +578,45 @@ function get_course_students($courseid, $sort='ul.timeaccess', $dir='', $page=''
 
     global $CFG;
 
-    if ($courseid == SITEID and $CFG->allusersaresitestudents) {
-        // return users with confirmed, undeleted accounts who are not site teachers
-        // the following is a mess because of different conventions in the different user functions
-        $sort = str_replace('s.timeaccess', 'lastaccess', $sort); // site users can't be sorted by timeaccess
-        $sort = str_replace('timeaccess', 'lastaccess', $sort); // site users can't be sorted by timeaccess
-        $sort = str_replace('u.', '', $sort); // the get_user function doesn't use the u. prefix to fields
-        $fields = str_replace('u.', '', $fields);
-        if ($sort) {
-            $sort = $sort .' '. $dir;
-        }
-        // Now we have to make sure site teachers are excluded
+    // make sure it works on the site course
+    $context = get_context_instance(CONTEXT_COURSE, $courseid);
 
-        if ($teachers = get_course_teachers(SITEID)) {
-            foreach ($teachers as $teacher) {
-                $exceptions .= ','. $teacher->userid;
+    /// For the site course, old way was to check if $CFG->allusersaresitestudents was set to true. 
+    /// The closest comparible method using roles is if the $CFG->defaultuserroleid is set to the legacy
+    /// student role. This function should be replaced where it is used with something more meaningful. 
+    if (($courseid == SITEID) && !empty($CFG->defaultuserroleid) && empty($CFG->nodefaultuserrolelists)) {
+        if ($roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW, $context)) {
+            $hascap = false;
+            foreach ($roles as $role) {
+                if ($role->id == $CFG->defaultuserroleid) {
+                    $hascap = true;
+                    break;
+                }
             }
-            $exceptions = ltrim($exceptions, ','); 
-          
-        }        
-
-        return get_users(true, $search, true, $exceptions, $sort, $firstinitial, $lastinitial,
-                          $page, $recordsperpage, $fields ? $fields : '*');
+            if ($hascap) {
+                // return users with confirmed, undeleted accounts who are not site teachers
+                // the following is a mess because of different conventions in the different user functions
+                $sort = str_replace('s.timeaccess', 'lastaccess', $sort); // site users can't be sorted by timeaccess
+                $sort = str_replace('timeaccess', 'lastaccess', $sort); // site users can't be sorted by timeaccess
+                $sort = str_replace('u.', '', $sort); // the get_user function doesn't use the u. prefix to fields
+                $fields = str_replace('u.', '', $fields);
+                if ($sort) {
+                    $sort = $sort .' '. $dir;
+                }
+                // Now we have to make sure site teachers are excluded
+        
+                if ($teachers = get_course_teachers(SITEID)) {
+                    foreach ($teachers as $teacher) {
+                        $exceptions .= ','. $teacher->userid;
+                    }
+                    $exceptions = ltrim($exceptions, ','); 
+                  
+                }        
+        
+                return get_users(true, $search, true, $exceptions, $sort, $firstinitial, $lastinitial,
+                                  $page, $recordsperpage, $fields ? $fields : '*');
+            }
+        }
     }
 
     $LIKE      = sql_ilike();
@@ -607,9 +624,6 @@ function get_course_students($courseid, $sort='ul.timeaccess', $dir='', $page=''
 
     $groupmembers = '';
 
-    // make sure it works on the site course
-    $context = get_context_instance(CONTEXT_COURSE, $courseid);
-
     $select = "c.contextlevel=".CONTEXT_COURSE." AND "; // Must be on a course
     if ($courseid != SITEID) {
         // If not site, require specific course
@@ -706,6 +720,28 @@ function get_course_teachers($courseid, $sort='t.authority ASC', $exceptions='')
     $sort = 'ul.timeaccess DESC';
     
     $context = get_context_instance(CONTEXT_COURSE, $courseid);
+
+    /// For the site course, if the $CFG->defaultuserroleid is set to the legacy teacher role, then all
+    /// users are teachers. This function should be replaced where it is used with something more 
+    /// meaningful. 
+    if (($courseid == SITEID) && !empty($CFG->defaultuserroleid) && empty($CFG->nodefaultuserrolelists)) {
+        if ($roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW, $context)) {
+            $hascap = false;
+            foreach ($roles as $role) {
+                if ($role->id == $CFG->defaultuserroleid) {
+                    $hascap = true;
+                    break;
+                }
+            }
+            if ($hascap) {
+                if (empty($fields)) {
+                    $fields = '*';
+                }
+                return get_users(true, '', true, $exceptions, 'lastname ASC', '', '', '', '', $fields);
+            }
+        }
+    }
+
     return get_users_by_capability($context, 'moodle/course:update', 'u.*, ul.timeaccess as lastaccess, ra.hidden', $sort, '','','',$exceptions, false);
     /// some fields will be missing, like authority, editall
     /*
@@ -730,8 +766,30 @@ function get_course_teachers($courseid, $sort='t.authority ASC', $exceptions='')
  * @todo Finish documenting this function
  */
 function get_course_users($courseid, $sort='ul.timeaccess DESC', $exceptions='', $fields='') {
+    global $CFG;
 
     $context = get_context_instance(CONTEXT_COURSE, $courseid);
+
+    /// If the course id is the SITEID, we need to return all the users if the "defaultuserroleid"
+    /// has the capbility of accessing the site course. $CFG->nodefaultuserrolelists set to true can 
+    /// over-rule using this.
+    if (($courseid == SITEID) && !empty($CFG->defaultuserroleid) && empty($CFG->nodefaultuserrolelists)) {
+        if ($roles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $context)) {
+            $hascap = false;
+            foreach ($roles as $role) {
+                if ($role->id == $CFG->defaultuserroleid) {
+                    $hascap = true;
+                    break;
+                }
+            }
+            if ($hascap) {
+                if (empty($fields)) {
+                    $fields = '*';
+                }
+                return get_users(true, '', true, $exceptions, 'lastname ASC', '', '', '', '', $fields);
+            }
+        }
+    }
     return get_users_by_capability($context, 'moodle/course:view', 'u.*, ul.timeaccess as lastaccess', $sort, '','','',$exceptions, false);
 
 }