From d76a5a7feddf55cca9147a71aa09c71c0d8e8042 Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Tue, 19 Sep 2006 01:44:33 +0000 Subject: [PATCH] replacing references to user_* tables --- admin/index.php | 2 +- admin/user.php | 6 ++--- auth/db/lib.php | 7 +++--- auth/ldap/lib.php | 7 +++--- course/import/activities/mod.php | 6 ++--- lang/en_utf8/docs/releaseold.html | 2 +- lib/accesslib.php | 24 +++++++++++++++++++ lib/datalib.php | 10 -------- lib/deprecatedlib.php | 13 +---------- lib/moodlelib.php | 5 ++-- message/lib.php | 38 ++++++++++++++----------------- mod/hotpot/report.php | 5 +++- question/import.php | 17 +++++++------- 13 files changed, 73 insertions(+), 69 deletions(-) diff --git a/admin/index.php b/admin/index.php index 881671f471..6f9a39aaf9 100644 --- a/admin/index.php +++ b/admin/index.php @@ -98,7 +98,7 @@ $maintables = true; $mtables = array("config", "course", "course_categories", "course_modules", "course_sections", "log", "log_display", "modules", - "user", "user_admins", "user_students", "user_teachers"); + "user"); foreach ($mtables as $mtable) { if (!in_array($CFG->prefix.$mtable, $tables)) { $maintables = false; diff --git a/admin/user.php b/admin/user.php index 593a76e599..3b82eafff0 100644 --- a/admin/user.php +++ b/admin/user.php @@ -185,9 +185,9 @@ $updateuser->idnumber = ""; // Clear this field to free it up $updateuser->timemodified = time(); if (update_record("user", $updateuser)) { - unenrol_student($user->id); // From all courses - remove_teacher($user->id); // From all courses - remove_admin($user->id); + // not sure if this is needed. unenrol_student($user->id); // From all courses + delete_records('role_assignments', 'userid', $user->id); // unassign all roles + // remove all context assigned on this user? notify(get_string("deletedactivity", "", fullname($user, true)) ); } else { notify(get_string("deletednot", "", fullname($user, true))); diff --git a/auth/db/lib.php b/auth/db/lib.php index 838d2647ca..b2f2aead37 100644 --- a/auth/db/lib.php +++ b/auth/db/lib.php @@ -183,9 +183,10 @@ function auth_sync_users ($do_updates=0) { $updateuser->deleted = "1"; $updateuser->timemodified = time(); if (update_record("user", $updateuser)) { - unenrol_student($user->id); // From all courses - remove_teacher($user->id); // From all courses - remove_admin($user->id); + // unenrol_student($user->id); // From all courses + // remove_teacher($user->id); // From all courses + // remove_admin($user->id); + delete_records('role_assignments', 'userid', $user->id); // unassign all roles notify(get_string("deletedactivity", "", fullname($user, true)) ); } else { notify(get_string("deletednot", "", fullname($user, true))); diff --git a/auth/ldap/lib.php b/auth/ldap/lib.php index b0eb9e4092..c314ea1b4b 100644 --- a/auth/ldap/lib.php +++ b/auth/ldap/lib.php @@ -507,9 +507,10 @@ function auth_sync_users ($bulk_insert_records = 1000, $do_updates=1) { //$updateuser->email = ""; // Clear this field to free it up $updateuser->timemodified = time(); if (update_record("user", $updateuser)) { - unenrol_student($user->id); // From all courses - remove_teacher($user->id); // From all courses - remove_admin($user->id); + // unenrol_student($user->id); // From all courses + // remove_teacher($user->id); // From all courses + // remove_admin($user->id); + delete_records('role_assignments', 'userid', $user->id); // unassign all roles notify(get_string("deletedactivity", "", fullname($user, true)) ); } else { notify(get_string("deletednot", "", fullname($user, true))); diff --git a/course/import/activities/mod.php b/course/import/activities/mod.php index 8776e5a28e..0a4776142b 100644 --- a/course/import/activities/mod.php +++ b/course/import/activities/mod.php @@ -18,10 +18,10 @@ $tcourseids = ''; - if ($teachers = get_records_select('user_teachers', "userid = $USER->id AND editall = 1",'','id,course')) { + if ($teachers = get_user_capability_course('moodle/course:edit')) { foreach ($teachers as $teacher) { - if ($teacher->course != $course->id && $teacher->course != SITEID){ - $tcourseids .= $teacher->course.','; + if ($teacher->id != $course->id && $teacher->id != SITEID){ + $tcourseids .= $teacher->id','; } } } diff --git a/lang/en_utf8/docs/releaseold.html b/lang/en_utf8/docs/releaseold.html index 30600502af..7f26fb65f5 100644 --- a/lang/en_utf8/docs/releaseold.html +++ b/lang/en_utf8/docs/releaseold.html @@ -716,7 +716,7 @@
  • Improved handling of ratings for single forums when secureforms is on.
  • Multichoice question creation now defaults to a single correct answer format
  • Guests can no longer see email addresses if "show only to course participants" has been set by that user
  • -
  • Primary admin is now the lowest-numbered entry in user_admins, rather than the admin with the lowest userid.
  • +
  • Primary admin is now the lowest-numbered entry in role_assignement with moodle/legacy:admin capability, rather than the admin with the lowest userid.
  • Modified flash MP3 plugin to improve brightness of Play button on darker screens
  • Various polishing and improvements in many language packs
  • diff --git a/lib/accesslib.php b/lib/accesslib.php index a08efde48b..09148ff146 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -2546,4 +2546,28 @@ function get_role_users($roleid, $context, $parent=false) { return get_records_sql($SQL); } +/** + * This function gets the list of courses that this user has a particular capability in + * This is not the most efficient way of doing this + * @param string capability + * @param int $userid + * @return array + */ +function get_user_capability_course($capability, $userid='') { + + global $USER; + if (!$userid) { + $userid = $USER->id; + } + + $usercourses = array(); + $courses = get_records_select('course', '', '', 'id, id'); + + foreach ($courses as $course) { + if (has_capability($capability, get_context_capability(CONTEXT_COURSE, $course->id))) { + $usercourses[] = $course; + } + } + return $usercourses; +} ?> diff --git a/lib/datalib.php b/lib/datalib.php index e5b8158682..f7e2500557 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -706,16 +706,6 @@ function get_my_courses($userid, $sort='visible DESC,sortorder ASC') { return get_records_list('course', 'id', $courseids, $sort); */ -// The following is correct but VERY slow with large datasets -// -// return get_records_sql("SELECT c.* -// FROM {$CFG->prefix}course c, -// {$CFG->prefix}user_students s, -// {$CFG->prefix}user_teachers t -// WHERE (s.userid = '$userid' AND s.course = c.id) -// OR (t.userid = '$userid' AND t.course = c.id) -// GROUP BY c.id -// ORDER BY $sort"); } diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 4a2f94ba4b..b48f0e43be 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -37,7 +37,7 @@ - + /** * Ensure that a variable is set * @@ -56,7 +56,6 @@ function require_variable($var) { } } - /** * Ensure that a variable is set * @@ -75,8 +74,6 @@ function optional_variable(&$var, $default=0) { } } - - /** * Determines if a user an admin * @@ -192,7 +189,6 @@ function isteacherinanycourse($userid=0, $includeadmin=true) { return false; } - /** * Determines if a user is allowed to edit a given course * @@ -282,8 +278,6 @@ function isguest($userid=0) { return has_capability('moodle/legacy:guest', $context, $userid, false); } - - /** * Enrols (or re-enrols) a student in a given course * @@ -319,7 +313,6 @@ function enrol_student($userid, $courseid, $timestart=0, $timeend=0, $enrol='man return role_assign($role->id, $user->id, 0, $context->id, $timestart, $timeend, 0, $enrol); } - /** * Unenrols a student from a given course * @@ -681,7 +674,6 @@ function get_course_students($courseid, $sort='ul.timeaccess', $dir='', $page='' */ } - /** * Counts the students in a given course (or site), or a subset of them * @@ -702,7 +694,6 @@ function count_course_students($course, $search='', $firstinitial='', $lastiniti return 0; } - /** * Returns list of all teachers in this course * @@ -754,8 +745,6 @@ function get_course_users($courseid, $sort='ul.timeaccess DESC', $exceptions='', } - - /** * Returns an array of user objects * diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 89d298578d..7bcf5b7c19 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -2665,8 +2665,8 @@ function remove_course_contents($courseid, $showfeedback=true) { // This array stores the tables that need to be cleared, as // table_name => column_name that contains the course id. $tablestoclear = array( - 'user_students' => 'course', // Delete any user stuff - 'user_teachers' => 'course', + //'user_students' => 'course', // Delete any user stuff + //'user_teachers' => 'course', 'event' => 'courseid', // Delete events 'log' => 'course', // Delete logs 'course_sections' => 'course', // Delete any course stuff @@ -2788,6 +2788,7 @@ function reset_course_userdata($data, $showfeedback=true) { } else { $result = false; } + /// Delete group members (but keep the groups) if ($groups = get_records('groups', 'courseid', $data->courseid)) { foreach ($groups as $group) { diff --git a/message/lib.php b/message/lib.php index e0fed63fd2..acdec0c456 100644 --- a/message/lib.php +++ b/message/lib.php @@ -232,12 +232,12 @@ function message_print_search() { message_print_search_results($frm); } else { - if ($teachers = get_records('user_teachers', 'userid', $USER->id, '', 'id, course')) { - + // find all courses this use has readallmessages capabilities in + if ($teachers = get_user_capability_course('moodle/site:readallmessages')) { $courses = get_courses('all', 'c.sortorder ASC', 'c.id, c.shortname'); $cs = ''; } @@ -701,24 +701,20 @@ function message_search_users($courseid, $searchtext, $sort='', $exceptions='') $except $order"); } else { - - if (!$teachers = get_records_sql("SELECT $fields - FROM {$CFG->prefix}user u, - {$CFG->prefix}user_teachers s - WHERE $select AND s.course = '$courseid' AND s.userid = u.id - AND ($fullname $LIKE '%$searchtext%') - $except $order")) { - $teachers = array(); - } - if (!$students = get_records_sql("SELECT $fields - FROM {$CFG->prefix}user u, - {$CFG->prefix}user_students s - WHERE $select AND s.course = '$courseid' AND s.userid = u.id - AND ($fullname $LIKE '%$searchtext%') - $except $order")) { - $students = array(); - } - return $teachers + $students; + $context = get_context_instance(CONTEXT_COURSE, $courseid); + $contextlists = get_related_contexts_string($context); + + // everyone who has a role assignement in this course or higher + $users = get_records_sql("SELECT $fields + FROM {$CFG->prefix}user u, + {$CFG->prefix}role_assignments ra + WHERE $select + AND ra.contextid $contextlists + AND u.id = ra.userid + AND ($fullname $LIKE '%$searchtext%') + $except $order"); + + return $users; } } diff --git a/mod/hotpot/report.php b/mod/hotpot/report.php index 22f1ff682b..689f49887e 100644 --- a/mod/hotpot/report.php +++ b/mod/hotpot/report.php @@ -87,7 +87,8 @@ $hotpot_ids = $hotpot->id; break; case 'all' : - $records = get_records_select_menu('user_teachers', "userid='$USER->id'", 'course', 'id, course'); + $records = get_user_capability_course('mod/hotpot:viewreport'); + //$records = get_records_select_menu('user_teachers', "userid='$USER->id'", 'course', 'id, course'); $course_ids = join(',', array_values($records)); $records = get_records_select_menu('hotpot', "reference='$hotpot->reference'", 'reference', 'id, reference'); @@ -99,6 +100,7 @@ $users = array(); switch ($formdata['reportusers']) { case 'all': + /* $admin_ids = get_records_select_menu('user_admins'); if (is_array($admin_ids)) { $users = array_merge($users, $admin_ids); @@ -115,6 +117,7 @@ if (is_array($guest_id)) { $users = array_merge($users, $guest_id); } + */ // add students next case 'students': diff --git a/question/import.php b/question/import.php index d8f21e85f9..ae28974543 100644 --- a/question/import.php +++ b/question/import.php @@ -188,15 +188,14 @@ error("Could not find any question categories!"); // Something is really wrong } } else { // select only the categories to which the teacher has write access - $sql = "SELECT c.* - FROM {$CFG->prefix}question_categories AS c, - {$CFG->prefix}user_teachers AS t - WHERE t.userid = '$USER->id' - AND t.course = c.course - AND (c.course = '$course->id' - OR (c.publish = '1' AND t.editall = '1')) - ORDER BY c.parent ASC, c.sortorder ASC, c.name ASC"; - if (!$categories = get_records_sql($sql)) { + $cats = get_records('question_categories'); + $categories = array(); + foreach ($cats as $cat) { + if (has_capability('moodle/question:managecateory', get_context_instance(CONTEXT_COURSE, $cat->course))) { + $categories[] = $cat; + } + } + if (empty($categories)) { error("Could not find any question categories!"); } } -- 2.39.5