From e957cc26442ae2066de435a709dc1ebed20035e5 Mon Sep 17 00:00:00 2001 From: moodler Date: Mon, 14 Aug 2006 08:38:49 +0000 Subject: [PATCH] Cleaned up a bunch of notices and some logic --- user/index.php | 89 ++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/user/index.php b/user/index.php index 465f2d9ee8..0467e52242 100644 --- a/user/index.php +++ b/user/index.php @@ -9,7 +9,6 @@ define('USER_LARGE_CLASS', 200); // Above this is considered large define('DEFAULT_PAGE_SIZE', 20); - $id = required_param('id', PARAM_INT); // Course id $group = optional_param('group', -1, PARAM_INT); // Group to show $page = optional_param('page', 0, PARAM_INT); // which page to show $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // how many per page @@ -18,17 +17,30 @@ $accesssince = optional_param('accesssince',0,PARAM_INT); // filter by last access. -1 = never $search = optional_param('search','',PARAM_CLEAN); $roleid = optional_param('roleid', 0, PARAM_INT); // optional roleid - $contextid = required_param('contextid', PARAM_INT); // required contextid + + $contextid = optional_param('contextid', PARAM_INT); // one of this or + $courseid = optional_param('id', PARAM_INT); // this are required $showteachers = $showteachers && empty($search); // if we're searching, we just want students. - if (! $course = get_record('course', 'id', $id)) { - error("Course ID is incorrect"); + if ($contextid) { + if (! $context = get_context_instance_by_id($contextid)) { + error("Context ID is incorrect"); + } + if (! $course = get_record('course', 'id', $context->instanceid)) { + error("Course ID is incorrect"); + } + } else { + if (! $course = get_record('course', 'id', $courseid)) { + error("Course ID is incorrect"); + } + if (! $context = get_context_instance(CONTEXT_COURSE, $course->id)) { + error("Context ID is incorrect"); + } } require_login($course->id); - $context = get_context_instance(CONTEXT_COURSE, $id); require_capability('moodle/course:viewparticipants', $context); if (!$course->category) { @@ -93,7 +105,7 @@ } // Should use this variable so that we don't break stuff every time a variable is added or changed. - $baseurl = $CFG->wwwroot.'/user/index.php?contextid='.$contextid.'&roleid='.$roleid.'&id='.$course->id.'&group='.$currentgroup.'&perpage='.$perpage.'&teachers='.$showteachers.'&accesssince='.$accesssince.'&search='.$search; + $baseurl = $CFG->wwwroot.'/user/index.php?contextid='.$context->id.'&roleid='.$roleid.'&id='.$course->id.'&group='.$currentgroup.'&perpage='.$perpage.'&teachers='.$showteachers.'&accesssince='.$accesssince.'&search='.$search; /// Print headers @@ -108,11 +120,11 @@ //setting up tags - if ($id == SITEID) { + if ($course->id == SITEID) { $filtertype = 'site'; - } else if ($id && !$currentgroup) { + } else if ($course->id && !$currentgroup) { $filtertype = 'course'; - $filterselect = $id; + $filterselect = $course->id; } else { $filtertype = 'group'; $filterselect = $currentgroup; @@ -137,28 +149,23 @@ // this needs to check capability too - - - $SQL = 'select distinct r.id, r.name from '.$CFG->prefix.'role_assignments ra, '.$CFG->prefix.'role r WHERE - r.id = ra.roleid AND ra.contextid = '.$contextid.' ORDER BY r.sortorder ASC'; - - $roles = get_records_sql($SQL); - - foreach ($roles as $role) { - $options[$role->id] = $role->name; + if ($roles = get_roles_used_in_context($context)) { + foreach ($roles as $role) { + $options[$role->id] = $role->name; + } } if (!$roleid) { - $rolesarray = array_keys($options); - $roleid = array_shift($rolesarray); // get first element + if ($options) { + $roleid = array_shift(array_keys($options)); // get first element + } } - - print ('
'); - print ('
Current Context: '.print_context_name($contextid).'
'); - print ('Select a Role: '); - print (''); + + echo ''; + echo '
'; + echo ''.get_string('currentrole', 'role').': '; choose_from_menu ($options, 'roleid', $roleid, '', $script='rolesform.submit()'); - print ('
'); + echo '
'; @@ -175,7 +182,7 @@ $my_course[$mycourse->id] = $mycourse->shortname; } //choose_from_menu($my_course, 'id', $course->id, '', 'courseform.submit()'); - popup_form($CFG->wwwroot.'/user/index.php?contextid='.$contextid.'&roleid='.$roleid.'&id=',$my_course,'courseform',$course->id); + popup_form($CFG->wwwroot.'/user/index.php?contextid='.$context->id.'&roleid='.$roleid.'&id=',$my_course,'courseform',$course->id); echo ''; if ($groupmode == VISIBLEGROUPS or ($groupmode and isteacheredit($course->id))) { @@ -292,7 +299,6 @@ function checkchecked(form) { '; echo '
'; - echo ''; echo ''; echo ''; } @@ -354,7 +360,6 @@ function checkchecked(form) { if ($roleid) { // we are looking for all users with this role assigned in this context or higher - $context = get_record('context', 'id', $contextid); $usercontexts = get_parent_contexts($context); $listofcontexts = '('.implode(',', $usercontexts).')'; @@ -362,7 +367,7 @@ function checkchecked(form) { u.picture, u.lang, u.timezone, u.emailstop, u.maildisplay, u.lastaccess AS lastaccess '; // s.lastaccess //$select .= $course->enrolperiod?', s.timeend ':''; $from = 'FROM '.$CFG->prefix.'user u LEFT JOIN '.$CFG->prefix.'role_assignments r ON r.userid = u.id '; - $where = 'WHERE (r.contextid = '.$contextid.' OR r.contextid in '.$listofcontexts.') AND u.deleted = 0 AND r.roleid = '.$roleid.' '; + $where = 'WHERE (r.contextid = '.$context->id.' OR r.contextid in '.$listofcontexts.') AND u.deleted = 0 AND r.roleid = '.$roleid.' '; $where .= get_lastaccess_sql($accesssince); $wheresearch = ''; @@ -379,20 +384,19 @@ function checkchecked(form) { $where .= ' AND gm.groupid = '.$currentgroup; } - if($course->id == SITEID) { + if ($course->id == SITEID) { $where .= ' AND u.id NOT IN ('.implode(',', $exceptions).')'; } $totalcount = count_records_sql('SELECT COUNT(distinct u.id) '.$from.$where); // 1 person can have multiple assignments - if($table->get_sql_where()) { + if ($table->get_sql_where()) { $where .= ' AND '.$table->get_sql_where(); } - if($table->get_sql_sort()) { + if ($table->get_sql_sort()) { $sort = ' ORDER BY '.$table->get_sql_sort(); - } - else { + } else { $sort = ''; } @@ -401,22 +405,23 @@ function checkchecked(form) { $table->initialbars($totalcount > $perpage); $table->pagesize($perpage, $matchcount); - if($table->get_page_start() !== '' && $table->get_page_size() !== '') { + if ($table->get_page_start() !== '' && $table->get_page_size() !== '') { $limit = ' '.sql_paging_limit($table->get_page_start(), $table->get_page_size()); - } - else { + } else { $limit = ''; } $students = get_records_sql($select.$from.$where.$wheresearch.$sort.$limit); - $crole = get_record('role','id',$roleid); - + if (!$currentrole = get_record('role','id',$roleid)) { + error('That role does not exist'); + } + $a->count = $totalcount; - $a->items = $crole->name; + $a->items = $currentrole->name; echo '

'.get_string('counteditems', '', $a); if (isteacheredit($course->id)) { - echo ' '; + echo ' '; echo ''; } echo '

'; -- 2.39.5