Skip to content

Commit

Permalink
changing sql calls from user_students table to role_assignments table
Browse files Browse the repository at this point in the history
  • Loading branch information
toyomoyo committed Sep 14, 2006
1 parent b1342e1 commit ea8158c
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 56 deletions.
23 changes: 17 additions & 6 deletions admin/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,25 @@
echo "<input type=\"hidden\" name=\"users\" value=\"$count\">\n";
echo '<br />';

$count = count_records('user_students');
echo get_string("enrolments").": ".$count;
echo "<input type=\"hidden\" name=\"enrolments\" value=\"$count\">\n";
// total number of role assignments
$count = count_records('role_assignments');
echo get_string("roleassignments").": ".$count;
echo "<input type=\"hidden\" name=\"roleassignments\" value=\"$count\">\n";
echo '<br />';

$count = count_records('user_teachers');
echo get_string("teachers").": ".$count;
echo "<input type=\"hidden\" name=\"teachers\" value=\"$count\">\n";
// first find all distinct roles with mod/course:update
// please change the name and strings to something appropriate to reflect the new data collected
$sql = "SLECT COUNT(DISTINCT u.id)
FROM {$CFG->prefix}role_capabilities rc,
{$CFG->prefix}role_assignments ra,
{$CFG->prefix}user u
WHERE (rc.capability = 'mod/course:update' or rc.capability='moodle/site:doanything')
AND rc.roleid = ra.roleid
AND u.id = ra.userid";

$count = count_records_sql($sql);
echo get_string("courseupdates").": ".$count;
echo "<input type=\"hidden\" name=\"courseupdates\" value=\"$count\">\n";
echo '<br />';

$count = count_records('forum_posts');
Expand Down
7 changes: 1 addition & 6 deletions blog/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,11 @@ function fetch_entries($userid, $postid='', $fetchlimit=10, $fetchstart='', $fil

// all users with a role assigned
$context = get_context_instance(CONTEXT_COURSE, $filterselect);
if ($parents = get_parent_contexts($context)) {
$contextlists = 'OR ra.contextid IN ('.implode(',', $parents).'))';
} else {
$contextlists = ')';
}

$SQL = '(SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql
.$CFG->prefix.'role_assignments ra, '.$CFG->prefix.'user u
WHERE p.userid = ra.userid '.$tagquerysql.'
AND (ra.contextid = '.$context->id.' '.$contextlists.'
AND ra.contextid '.get_related_contexts_string($context).'
AND u.id = p.userid
AND (p.publishstate = \'site\' OR p.publishstate = \'public\' OR p.userid = '.$USER->id.'))';
} else {
Expand Down
8 changes: 7 additions & 1 deletion course/report/stats/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@
}
$sql .= " ORDER BY s.roleid ";
} else {
$sql = 'SELECT s.userid,u.firstname,u.lastname,u.idnumber,1 AS roleid FROM '.$CFG->prefix.'user_students s JOIN '.$CFG->prefix.'user u ON u.id = s.userid WHERE course = '.$course->id;
$context = get_context_instance(CONTEXT_COURSE, $course->id);

$sql = 'SELECT ra.userid,u.firstname,u.lastname,u.idnumber,1 AS roleid
FROM '.$CFG->prefix.'role_assignments ra,
'.$CFG->prefix.'user u
WHERE u.id = ra.userid
AND ra.contextid '.get_related_contexts_string($context);
}

if (!$us = get_records_sql($sql)) {
Expand Down
3 changes: 1 addition & 2 deletions file.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
$lifetime = 86400; // Seconds for files to remain in caches
} else {
$lifetime = $CFG->filelifetime;
}

}

$relativepath = get_file_argument('file.php');
$forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
Expand Down
14 changes: 12 additions & 2 deletions lib/accesslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1834,7 +1834,6 @@ function role_context_capabilities($roleid, $context, $cap='') {
return $capabilities;
}


/**
* Recursive function which, given a context, find all parent context ids,
* and return the array in reverse order, i.e. parent first, then grand
Expand Down Expand Up @@ -1930,7 +1929,18 @@ function get_parent_contexts($context) {
}
}


/** gets a string for sql calls, searching for stuff
* in this context or above
* @param object $context
* @return string
*/
function get_related_contexts_string($context) {
if ($parents = get_parent_contexts($context)) {
return (' IN ('.$context->id.','.implode(',', $parents).')');
} else {
return (' ='.$context->id);
}
}
/**
* This function gets the capability of a role in a given context.
* It is needed when printing override forms.
Expand Down
23 changes: 7 additions & 16 deletions lib/datalib.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ function get_courses_notin_metacourse($metacourseid,$count=false) {
* @param string $sort ?
* @param string $exceptions ?
* @return object
* @todo XXX Convert to Roles
*/
function search_users($courseid, $groupid, $searchtext, $sort='', $exceptions='') {
global $CFG;
Expand Down Expand Up @@ -149,24 +148,16 @@ function search_users($courseid, $groupid, $searchtext, $sort='', $exceptions=''
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 $select AND s.course = '$courseid' AND s.userid = u.id
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
$context = get_context_instance(CONTEXT_COURSE, $courseid);
$contextlists = get_related_contexts_string($context);
$users = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
FROM {$CFG->prefix}user u,
{$CFG->prefix}user_students s
WHERE $select AND s.course = '$courseid' AND s.userid = u.id
{$CFG->prefix}role_assignments ra
WHERE $select AND ra.contextid $contextlists AND ra.userid = u.id
AND ($fullname $LIKE '%$searchtext%' OR u.email $LIKE '%$searchtext%')
$except $order")) {
$students = array();
}
return $teachers + $students;
$except $order");
}
return $users;
}
}

Expand Down
10 changes: 10 additions & 0 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2750,6 +2750,11 @@ function remove_course_contents($courseid, $showfeedback=true) {
include_once($CFG->libdir.'/questionlib.php');
question_delete_course($course, $showfeedback);

// deletes all role assignments, and local override, these have no courseid in table and needs separate process
$context = get_context_instance(CONTEXT_COUSE, $course->id);
delect_records('role_assignments', 'contextid', $context->id);
delect_records('role_role_capabilities', 'contextid', $context->id);

return $result;

}
Expand Down Expand Up @@ -2863,6 +2868,11 @@ function reset_course_userdata($data, $showfeedback=true) {
}
}

// deletes all role assignments, and local override, these have no courseid in table and needs separate process
$context = get_context_instance(CONTEXT_COUSE, $data->courseid);
delect_records('role_assignments', 'contextid', $context->id);
delect_records('role_role_capabilities', 'contextid', $context->id);

return $result;
}

Expand Down
43 changes: 32 additions & 11 deletions mod/assignment/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2129,7 +2129,17 @@ function assignment_log_info($log) {
function assignment_get_unmailed_submissions($starttime, $endtime) {

global $CFG;

return get_records_sql("SELECT s.*, a.course, a.name
FROM {$CFG->prefix}assignment_submissions s,
{$CFG->prefix}assignment a,
WHERE s.mailed = 0
AND s.timemarked <= $endtime
AND s.timemarked >= $starttime
AND s.assignment = a.id
AND s.userid = us.userid");

/* return get_records_sql("SELECT s.*, a.course, a.name
FROM {$CFG->prefix}assignment_submissions s,
{$CFG->prefix}assignment a,
{$CFG->prefix}user_students us
Expand All @@ -2139,6 +2149,7 @@ function assignment_get_unmailed_submissions($starttime, $endtime) {
AND s.assignment = a.id
AND s.userid = us.userid
AND a.course = us.course");
*/
}

/**
Expand Down Expand Up @@ -2202,18 +2213,28 @@ function assignment_get_all_submissions($assignment, $sort="", $dir="DESC") {
$sort = "a.$sort $dir";
}

/* not sure this is needed at all since assignmenet already has a course define, so this join?
$select = "s.course = '$assignment->course' AND";
if ($assignment->course == SITEID) {
$select = '';
}
}*/

return get_records_sql("SELECT a.*
FROM {$CFG->prefix}assignment_submissions a,
{$CFG->prefix}user u
WHERE u.id = a.userid
AND a.assignment = '$assignment->id'
ORDER BY $sort");

/* return get_records_sql("SELECT a.*
FROM {$CFG->prefix}assignment_submissions a,
{$CFG->prefix}user_students s,
{$CFG->prefix}user u
WHERE a.userid = s.userid
AND u.id = a.userid
AND $select a.assignment = '$assignment->id'
ORDER BY $sort");
*/
}


Expand Down Expand Up @@ -2367,16 +2388,16 @@ function assignment_print_overview($courses, &$htmlarray) {

$context = get_context_instance(CONTEXT_MODULE,$this->cm->id);
if (has_capability('mod/assignment:grade', $context)) {
$submissions = count_records_sql("SELECT COUNT(*)
FROM {$CFG->prefix}assignment_submissions a,
{$CFG->prefix}user_students s,
{$CFG->prefix}user u
WHERE a.userid = s.userid
AND u.id = a.userid
AND s.course = '{$assignment->course}'
AND a.assignment = '{$assignment->id}'
AND a.teacher = 0
AND a.timemarked = 0");

// count how many people can submit
$submissions = 0; // init
$students = get_users_by_capability($context, 'mod/assignment:submit');
foreach ($student as $student) {
if (get_record('assignment_submissions', 'assignment', $assignment->id, 'userid', $student->id)) {
$submissions++;
}
}

if ($submissions) {
$str .= get_string('submissionsnotgraded', 'assignment', $submissions);
}
Expand Down
42 changes: 30 additions & 12 deletions mod/hotpot/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,32 @@
// add students next

case 'students':
$student_ids = get_records_select_menu('user_students', "course IN ($course_ids)", 'course', 'id, userid');

$contexts = array();
// first find all applicable contextids, put them in a bit array
foreach ($course_ids as $course_id) {

$context = get_context_instance(CONTEXT_COURSE, $course_id);

// first add self to list
if (!in_array($context->id, $contexts)) {
$contexts[] = $context->id;
}

// then add all parent contexts
if ($parents = get_parent_contexts($context)) {
foreach ($parents as $parent) {
if (!in_array($parent->id, $contexts)) {
$contexts[] = $parent->id;
}
}
}
}

$contextlists = implode(',', $contexts);

// this sort order might not make sense
$student_ids = get_records_select_menu('role_assignments', "contextid IN ($contextlists)", 'contextid', 'id, userid');
if (is_array($student_ids)) {
$users = array_merge($users, $student_ids);
}
Expand Down Expand Up @@ -439,17 +464,10 @@ function hotpot_print_report_selector(&$course, &$hotpot, &$formdata) {
'all' => get_string('allparticipants'),
'students' => get_string('students')
);
$users = get_records_sql("
SELECT
u.*
FROM
{$CFG->prefix}user AS u,
{$CFG->prefix}user_students AS us
WHERE
u.id = us.userid AND us.course=$course->id
ORDER BY
u.lastname
");

$context = get_context_instance(CONTEXT_COURSE, $course->id);
$users = get_users_by_capability($context, 'mod/hotpot:attempt', 'u.*', $sort='u.lastname');

if ($users) {
$menus['reportusers'][''] = '------'; // separator
foreach ($users as $id=>$user) {
Expand Down

0 comments on commit ea8158c

Please sign in to comment.