Skip to content

Commit

Permalink
replacing references to user_* tables
Browse files Browse the repository at this point in the history
  • Loading branch information
toyomoyo committed Sep 19, 2006
1 parent f33beb0 commit d76a5a7
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 69 deletions.
2 changes: 1 addition & 1 deletion admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions admin/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
7 changes: 4 additions & 3 deletions auth/db/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
7 changes: 4 additions & 3 deletions auth/ldap/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
6 changes: 3 additions & 3 deletions course/import/activities/mod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',';
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lang/en_utf8/docs/releaseold.html
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ <h3> New in Moodle 1.2.1 (25th March, 2004)</h3>
<li>Improved handling of ratings for single forums when secureforms is on.</li>
<li>Multichoice question creation now defaults to a single correct answer format</li>
<li>Guests can no longer see email addresses if "show only to course participants" has been set by that user</li>
<li>Primary admin is now the lowest-numbered entry in user_admins, rather than the admin with the lowest userid.</li>
<li>Primary admin is now the lowest-numbered entry in role_assignement with moodle/legacy:admin capability, rather than the admin with the lowest userid.</li>
<li>Modified flash MP3 plugin to improve brightness of Play button on darker screens</li>
<li>Various polishing and improvements in many language packs</li>
</ul>
Expand Down
24 changes: 24 additions & 0 deletions lib/accesslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
?>
10 changes: 0 additions & 10 deletions lib/datalib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}


Expand Down
13 changes: 1 addition & 12 deletions lib/deprecatedlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@




/**
* Ensure that a variable is set
*
Expand All @@ -56,7 +56,6 @@ function require_variable($var) {
}
}


/**
* Ensure that a variable is set
*
Expand All @@ -75,8 +74,6 @@ function optional_variable(&$var, $default=0) {
}
}



/**
* Determines if a user an admin
*
Expand Down Expand Up @@ -192,7 +189,6 @@ function isteacherinanycourse($userid=0, $includeadmin=true) {
return false;
}


/**
* Determines if a user is allowed to edit a given course
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand All @@ -702,7 +694,6 @@ function count_course_students($course, $search='', $firstinitial='', $lastiniti
return 0;
}


/**
* Returns list of all teachers in this course
*
Expand Down Expand Up @@ -754,8 +745,6 @@ function get_course_users($courseid, $sort='ul.timeaccess DESC', $exceptions='',

}



/**
* Returns an array of user objects
*
Expand Down
5 changes: 3 additions & 2 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
38 changes: 17 additions & 21 deletions message/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<select name="courseselect">';
foreach ($teachers as $tcourse) {
$cs .= "<option value=\"$tcourse->course\">".$courses[$tcourse->course]->shortname."</option>\n";
$cs .= "<option value=\"$tcourse->course\">".$courses[$tcourse->id]->shortname."</option>\n";
}
$cs .= '</select>';
}
Expand Down Expand Up @@ -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;
}
}

Expand Down
5 changes: 4 additions & 1 deletion mod/hotpot/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand All @@ -115,6 +117,7 @@
if (is_array($guest_id)) {
$users = array_merge($users, $guest_id);
}
*/
// add students next

case 'students':
Expand Down
17 changes: 8 additions & 9 deletions question/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}
Expand Down

0 comments on commit d76a5a7

Please sign in to comment.