]> git.mjollnir.org Git - moodle.git/commitdiff
Merged from HEAD
authorgustav_delius <gustav_delius>
Mon, 30 Aug 2004 17:27:00 +0000 (17:27 +0000)
committergustav_delius <gustav_delius>
Mon, 30 Aug 2004 17:27:00 +0000 (17:27 +0000)
lib/datalib.php
lib/moodlelib.php

index 85b7e59753d5cfa50a224e2a363111e56aab24cf..90d9eb223d097200d0b6c010e601cd44f10e051e 100644 (file)
@@ -36,7 +36,7 @@ function execute_sql($command, $feedback=true) {
 /**
 * Run an arbitrary sequence of semicolon-delimited SQL commands
 * 
-* Assumes that the input text (file or string consists of 
+* Assumes that the input text (file or string) consists of 
 * a number of SQL statements ENDING WITH SEMICOLONS.  The 
 * semicolons MUST be the last character in a line.
 * Lines that are blank or that start with "#" are ignored.
@@ -49,7 +49,7 @@ function modify_database($sqlfile="", $sqlstring="") {
 
     global $CFG;
 
-    $success = true;  // Let's be optimistic :-)
+    $success = true;  // Let's be optimistic 
 
     if (!empty($sqlfile)) {
         if (!is_readable($sqlfile)) {
@@ -1133,6 +1133,27 @@ function get_course_students($courseid, $sort="s.timeaccess", $dir="", $page=0,
                              $firstinitial="", $lastinitial="", $group=NULL, $search="", $fields='', $exceptions='') {
 
     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
+        if ($teachers = get_records('user_teachers', 'course', 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 : '*');
+    }
 
     switch ($CFG->dbtype) {
         case "mysql":
@@ -1196,11 +1217,36 @@ function get_course_students($courseid, $sort="s.timeaccess", $dir="", $page=0,
         $sort = " ORDER BY $sort ";
     }
 
-    return get_records_sql("SELECT $fields
+    $students = get_records_sql("SELECT $fields
                             FROM {$CFG->prefix}user u,
                                  {$CFG->prefix}user_students s
                                  $groupmembers
                             WHERE $select $search $sort $dir $limit");
+
+    if ($courseid != SITEID) {
+        return $students;
+    }
+    
+    // We are here because we need the students for the site.
+    // These also include teachers on real courses minus those on the site
+    if ($teachers = get_records('user_teachers', 'course', SITEID)) {
+        foreach ($teachers as $teacher) {
+            $exceptions .= ",$teacher->userid";
+        }
+        $exceptions = ltrim($exceptions, ',');
+        $select .= " AND u.id NOT IN ($exceptions)";
+    }
+    if (!$teachers = get_records_sql("SELECT $fields
+                            FROM {$CFG->prefix}user u,
+                                 {$CFG->prefix}user_teachers s
+                                 $groupmembers
+                            WHERE $select $search $sort $dir $limit")) {
+        return $students;
+    }
+    if (!$students) {
+        return $teachers;
+    }
+    return $teachers + $students;
 }
 
 /**
@@ -1210,52 +1256,10 @@ function get_course_students($courseid, $sort="s.timeaccess", $dir="", $page=0,
 */
 function count_course_students($course, $search="", $firstinitial="", $lastinitial="", $group=NULL, $exceptions='') {
 
-    global $CFG;
-
-    if (!$course->category) {
-        return count(get_site_users($sort, '', $exceptions));
-    }
-
-    switch ($CFG->dbtype) {
-        case "mysql":
-             $fullname = " CONCAT(firstname,\" \",lastname) ";
-             $LIKE = "LIKE";
-             break;
-        default: 
-             $fullname = " firstname||\' \'||lastname ";
-             $LIKE = "ILIKE";
+    if ($students = get_course_students($course->id, '', '', 0, 999999, $firstinitial, $lastinitial, $group, $search, '', $exceptions)) {
+        return count($students);
     }
-    
-    $groupmembers = "";
-        
-    $select = "s.course = '$course->id' AND s.userid = u.id AND u.deleted = '0'";
-
-    if ($search) {
-        $search = " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') ";
-    } 
-    if ($firstinitial) {
-        $select .= " AND u.firstname $LIKE '$firstinitial%'";
-    } 
-    if ($lastinitial) {
-        $select .= " AND u.lastname $LIKE '$lastinitial%'";
-    } 
-
-    if ($group === 0) {   /// Need something here to get all students not in a group
-        return 0;
-
-    } else if ($group !== NULL) {
-        $groupmembers = ", {$CFG->prefix}groups_members gm ";
-        $select .= " AND u.id = gm.userid AND gm.groupid = '$group'";
-    }
-    
-    if (!empty($exceptions)) {
-        $select .= " AND u.id NOT IN ($exceptions)";
-    }
-
-    return count_records_sql("SELECT COUNT(*) 
-                              FROM {$CFG->prefix}user u,
-                                   {$CFG->prefix}user_students s $groupmembers
-                              WHERE $select");
+    return 0;
 }
 
 
@@ -1280,7 +1284,8 @@ function get_course_teachers($courseid, $sort="t.authority ASC", $exceptions='')
                                    u.emailstop, t.authority,t.role,t.editall,t.timeaccess as lastaccess
                             FROM {$CFG->prefix}user u, 
                                  {$CFG->prefix}user_teachers t
-                            WHERE t.course = '$courseid' AND t.userid = u.id AND u.deleted = '0' $except
+                            WHERE t.course = '$courseid' AND t.userid = u.id 
+                              AND u.deleted = '0' AND u.confirmed = '1' $except
                             ORDER BY $sort");
 }
 
@@ -1304,18 +1309,12 @@ function get_course_users($courseid, $sort="timeaccess DESC", $exceptions='') {
 
     return $teachers + $students;
 
-//   This is too inefficient on large sites. 
-//    return get_records_sql("SELECT DISTINCT u.*
-//                              FROM mdl_user u
-//                              LEFT JOIN mdl_user_students s ON s.course = '$courseid'
-//                              LEFT JOIN mdl_user_teachers t ON t.course = '$courseid'
-//                              WHERE (u.id = t.userid OR u.id = s.userid)
-//                              ORDER BY $sort");
 }
 
 
 /**
 * Search through course users
+* If used for the site course searches through all undeleted, confirmed users
 *
 * @param    type description
 */
@@ -1341,54 +1340,35 @@ function search_users($courseid, $groupid, $searchtext, $sort='', $exceptions=''
     } else {
         $except = '';
     }
-    
+
     if (!empty($sort)) {
         $order = " ORDER by $sort";
     } else {
         $order = '';
     }
     
+    $select = "u.deleted = '0' AND u.confirmed = '1'";
+
     if (!$courseid or $courseid == SITEID) {
-        if (!$admins = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
-                      FROM {$CFG->prefix}user u, 
-                           {$CFG->prefix}user_admins s
-                      WHERE s.userid = u.id AND u.deleted = '0'
+        return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
+                      FROM {$CFG->prefix}user u
+                      WHERE $select
                           AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
-                          $except $order")) {
-            $admins = array();
-        }
-        if (!$teachers = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
-                      FROM {$CFG->prefix}user u, 
-                           {$CFG->prefix}user_teachers s
-                      WHERE s.userid = u.id AND u.deleted = '0'
-                          AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
-                          $except $order")) {
-            $teachers = array();
-        }
-        if (!$students = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
-                      FROM {$CFG->prefix}user u, 
-                           {$CFG->prefix}user_students s
-                      WHERE s.userid = u.id AND u.deleted = '0'
-                          AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
-                          $except $order")) {
-            $students = array();
-        }
-        return $admins + $teachers + $students;
-        
+                          $except $order");
     } else { 
-    
+
         if ($groupid) {
             return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
                           FROM {$CFG->prefix}user u, 
                                {$CFG->prefix}groups_members g
-                          WHERE g.groupid = '$groupid' AND g.userid = u.id AND u.deleted = '0'
+                          WHERE $select AND g.groupid = '$groupid' AND g.userid = u.id
                               AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
                               $except $order");
         } else {
             if (!$teachers = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
                           FROM {$CFG->prefix}user u, 
                                {$CFG->prefix}user_teachers s
-                          WHERE s.course = '$courseid' AND s.userid = u.id AND u.deleted = '0'
+                          WHERE $select AND s.course = '$courseid' AND s.userid = u.id
                               AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
                               $except $order")) {
                 $teachers = array();
@@ -1396,7 +1376,7 @@ function search_users($courseid, $groupid, $searchtext, $sort='', $exceptions=''
             if (!$students = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
                           FROM {$CFG->prefix}user u, 
                                {$CFG->prefix}user_students s
-                          WHERE s.course = '$courseid' AND s.userid = u.id AND u.deleted = '0'
+                          WHERE $select AND s.course = '$courseid' AND s.userid = u.id
                               AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
                               $except $order")) {
                 $students = array();
@@ -1406,46 +1386,16 @@ function search_users($courseid, $groupid, $searchtext, $sort='', $exceptions=''
     }
 }
 
+
 /**
-* Returns a list of all active users who are enrolled 
-* 
-* or teaching in courses on this server
+* Returns a list of all site users
+* Obsolete, just calls get_course_users(SITEID)
 *
 * @param    type description
 */
 function get_site_users($sort="u.lastaccess DESC", $select="", $exceptions='') {
 
-    global $CFG;
-
-    if (!empty($exceptions)) {
-        $except = " AND u.id NOT IN ($exceptions) ";
-    } else {
-        $except = '';
-    }
-
-    if ($select) {
-        $selectinfo = $select;
-    } else {
-        $selectinfo = "u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.maildigest,".
-                      "u.email, u.emailstop, u.city, u.country, u.lastaccess, u.lastlogin, u.picture, u.lang, u.timezone";
-    }
-
-    if (!$students = get_records_sql("SELECT DISTINCT $selectinfo from {$CFG->prefix}user u, {$CFG->prefix}user_students s
-                                    WHERE s.userid = u.id $except ORDER BY $sort")) {
-        $students = array();
-    }
-
-    if (!$teachers = get_records_sql("SELECT DISTINCT $selectinfo from {$CFG->prefix}user u, {$CFG->prefix}user_teachers t
-                                      WHERE t.userid = u.id $except ORDER BY $sort")) {
-        $teachers = array();
-    }
-
-    if (!$admins = get_records_sql("SELECT DISTINCT $selectinfo from {$CFG->prefix}user u, {$CFG->prefix}user_admins a
-                                      WHERE a.userid = u.id $except ORDER BY $sort")) {
-        $admins = array();
-    }
-
-    return $admins + $teachers + $students;
+    return get_course_users(SITEID, $sort, '', 0, 999999, '', '', NULL, '', $select, $exceptions);
 }
 
 
@@ -2322,7 +2272,9 @@ function get_logs_usercourse($userid, $courseid, $coursestart) {
     global $CFG;
 
     if ($courseid) {
-       $courseselect = " AND course = '$courseid' ";
+        $courseselect = " AND course = '$courseid' ";
+    } else {
+        $courseselect = '';
     }
 
     return get_records_sql("SELECT floor((`time` - $coursestart)/86400) as day, count(*) as num 
@@ -2343,7 +2295,9 @@ function get_logs_userday($userid, $courseid, $daystart) {
     global $CFG;
 
     if ($courseid) {
-       $courseselect = " AND course = '$courseid' ";
+        $courseselect = " AND course = '$courseid' ";
+    } else {
+        $courseselect = '';
     }
 
     return get_records_sql("SELECT floor((`time` - $daystart)/3600) as hour, count(*) as num
index 72dfc073bb9d471b2c529dd70dbc5b77b6a12a39..b9ece2903f9717ca26754d67117bdd088fd1c35d 100644 (file)
@@ -579,7 +579,7 @@ function isstudent($courseid, $userid=0) {
 /// If course is site, is the user a confirmed user on the site?
     global $USER;
 
-    if (empty($USER->id)) {
+    if (empty($USER->id) and !$userid) {
         return false;
     }
 
@@ -590,7 +590,13 @@ function isstudent($courseid, $userid=0) {
         if (isguest($userid)) {
             return false;
         }
-        return record_exists('user_students', 'userid', $userid);
+        if ($CFG->allusersaresitestudents) {
+            return record_exists('user', 'id', $userid);
+        } else {
+            return (record_exists('user_students', 'userid', $userid)
+                     or (record_exists('user_teachers', 'userid', $userid)
+                       and !record_exists('user_teachers', 'userid', $userid, 'course', SITEID)));
+        }
     }  
 
     if (!$userid) {