/** * Initialise the iterator * * @return boolean success */ public function init() { global $CFG, $DB; $this->close(); export_verify_grades($this->course->id); $course_item = grade_item::fetch_course_item($this->course->id); if ($course_item->needsupdate) { // Can not calculate all final grades - sorry. return false; } $coursecontext = context_course::instance($this->course->id); list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx'); list($gradebookroles_sql, $params) = $DB->get_in_or_equal(explode(',', $CFG->gradebookroles), SQL_PARAMS_NAMED, 'grbr'); list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, '', 0, $this->onlyactive); $params = array_merge($params, $enrolledparams, $relatedctxparams); if ($this->groupid) { $groupsql = "INNER JOIN {groups_members} gm ON gm.userid = u.id"; $groupwheresql = "AND gm.groupid = :groupid"; // $params contents: gradebookroles $params['groupid'] = $this->groupid; } else { $groupsql = ""; $groupwheresql = ""; } if (empty($this->sortfield1)) { // We must do some sorting even if not specified. $ofields = ", u.id AS usrt"; $order = "usrt ASC"; } else { $ofields = ", u.{$this->sortfield1} AS usrt1"; $order = "usrt1 {$this->sortorder1}"; if (!empty($this->sortfield2)) { $ofields .= ", u.{$this->sortfield2} AS usrt2"; $order .= ", usrt2 {$this->sortorder2}"; } if ($this->sortfield1 != 'id' and $this->sortfield2 != 'id') { // User order MUST be the same in both queries, // must include the only unique user->id if not already present. $ofields .= ", u.id AS usrt"; $order .= ", usrt ASC"; } } $userfields = 'u.*'; $customfieldssql = ''; if ($this->allowusercustomfields && !empty($CFG->grade_export_customprofilefields)) { $customfieldscount = 0; $customfieldsarray = grade_helper::get_user_profile_fields($this->course->id, $this->allowusercustomfields); foreach ($customfieldsarray as $field) { if (!empty($field->customid)) { $customfieldssql .= "\n LEFT JOIN (SELECT * FROM {user_info_data}\n WHERE fieldid = :cf{$customfieldscount}) cf{$customfieldscount}\n ON u.id = cf{$customfieldscount}.userid"; $userfields .= ", cf{$customfieldscount}.data AS customfield_{$field->shortname}"; $params['cf' . $customfieldscount] = $field->customid; $customfieldscount++; } } } $users_sql = "SELECT {$userfields} {$ofields}\n FROM {user} u\n JOIN ({$enrolledsql}) je ON je.id = u.id\n {$groupsql} {$customfieldssql}\n JOIN (\n SELECT DISTINCT ra.userid\n FROM {role_assignments} ra\n WHERE ra.roleid {$gradebookroles_sql}\n AND ra.contextid {$relatedctxsql}\n ) rainner ON rainner.userid = u.id\n WHERE u.deleted = 0\n {$groupwheresql}\n ORDER BY {$order}"; $this->users_rs = $DB->get_recordset_sql($users_sql, $params); if (!$this->onlyactive) { $context = context_course::instance($this->course->id); $this->suspendedusers = get_suspended_userids($context); } else { $this->suspendedusers = array(); } if (!empty($this->grade_items)) { $itemids = array_keys($this->grade_items); list($itemidsql, $grades_params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED, 'items'); $params = array_merge($params, $grades_params); $grades_sql = "SELECT g.* {$ofields}\n FROM {grade_grades} g\n JOIN {user} u ON g.userid = u.id\n JOIN ({$enrolledsql}) je ON je.id = u.id\n {$groupsql}\n JOIN (\n SELECT DISTINCT ra.userid\n FROM {role_assignments} ra\n WHERE ra.roleid {$gradebookroles_sql}\n AND ra.contextid {$relatedctxsql}\n ) rainner ON rainner.userid = u.id\n WHERE u.deleted = 0\n AND g.itemid {$itemidsql}\n {$groupwheresql}\n ORDER BY {$order}, g.itemid ASC"; $this->grades_rs = $DB->get_recordset_sql($grades_sql, $params); } else { $this->grades_rs = false; } return true; }
/** * Return true is user is active user in course else false * * @param int $userid * @return bool true is user is active in course. */ public function is_active_user($userid) { return !in_array($userid, get_suspended_userids($this->context, true)); }
/** * Return true is user is active user in course else false * * @param int $userid * @return bool true is user is active in course. */ public function is_active_user($userid) { if (is_null($this->susers) && !is_null($this->context)) { $this->susers = get_suspended_userids($this->context); } return !in_array($userid, $this->susers); }
if ($mode === ROSTER_MODE_PRINT) { $PAGE->set_pagelayout('print'); } else { $PAGE->set_pagelayout('report'); } $returnurl = new moodle_url('/course/view.php', array('id' => $id)); // Check permissions. $coursecontext = context_course::instance($course->id); require_capability('report/roster:view', $coursecontext); // Get all the users. $userlist = get_enrolled_users($coursecontext, '', $group, user_picture::fields('u', null, 0, 0, true)); // Get suspended users. $suspended = get_suspended_userids($coursecontext); $data = array(); foreach ($userlist as $user) { if (!in_array($user->id, $suspended)) { $item = $OUTPUT->user_picture($user, array('size' => 100, 'courseid' => $course->id)); $item .= html_writer::tag('span', fullname($user)); $data[] = $item; } } // Finish setting up page. $PAGE->set_title($course->shortname .': '. get_string('roster' , 'report_roster')); $PAGE->set_heading($course->fullname); $PAGE->requires->yui_module('moodle-report_roster-roster', 'M.report_roster.init'); $PAGE->requires->strings_for_js(array(
/** * @dataProvider get_enrolled_sql_provider */ public function test_get_enrolled_sql_course($users, $counts) { global $DB; $this->resetAfterTest(); $course = $this->getDataGenerator()->create_course(); $context = context_course::instance($course->id); $student = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST); $createdusers = array(); foreach ($users as &$userdata) { $user = $this->getDataGenerator()->create_user(); $userdata['id'] = $user->id; $timestart = 0; $timeend = 0; $status = null; if (isset($userdata['timestart'])) { $timestart = time() + $userdata['timestart']; } if (isset($userdata['timeend'])) { $timeend = time() + $userdata['timeend']; } if (isset($userdata['status'])) { $status = $userdata['status']; } // Enrol the user in the course. $this->getDataGenerator()->enrol_user($user->id, $course->id, $student->id, 'manual', $timestart, $timeend, $status); } // After all users have been enroled, check expectations. $enrolled = get_enrolled_users($context, '', 0, 'u.id', null, 0, 0, false); $active = get_enrolled_users($context, '', 0, 'u.id', null, 0, 0, true); $suspended = get_suspended_userids($context); foreach ($users as $userdata) { if (isset($userdata['enrolled']) && $userdata['enrolled']) { $this->assertTrue(isset($enrolled[$userdata['id']])); } else { $this->assertFalse(isset($enrolled[$userdata['id']])); } if (isset($userdata['active']) && $userdata['active']) { $this->assertTrue(isset($active[$userdata['id']])); } else { $this->assertFalse(isset($active[$userdata['id']])); } if (isset($userdata['suspended']) && $userdata['suspended']) { $this->assertTrue(isset($suspended[$userdata['id']])); } else { $this->assertFalse(isset($suspended[$userdata['id']])); } } $this->assertCount($counts['enrolled'], $enrolled); $this->assertCount($counts['active'], $active); $this->assertCount($counts['suspended'], $suspended); }
function get_roster($onlyrole = null) { //edited below, replaced old method $coursecontext = context_course::instance($this->courseid); $allroles = get_roles_used_in_context($coursecontext); arsort($allroles); if (!$this->visible) { foreach ($allroles as $key => $role) { if ($role->shortname != 'editingteacher') { unset($allroles[$key]); } } } $roles = array_keys($allroles); // can't used canned function as its likely to return a student role // when the user has both a student and a teacher role // so this bit will allow the lower roleid (higher value role) to overwrite the lower one foreach ($roles as $role) { $temp = get_role_users($role, $coursecontext, false, '', null, false); if ($temp !== false && sizeof($temp) !== 0) { if (isset($course->users)) { $course->users = array_merge($course->users, $temp); } else { $course = new stdclass(); $course->users = $temp; } } } $members = array(); $suspended = get_suspended_userids($coursecontext); foreach ($course->users as $cuser) { if (array_key_exists($cuser->id, $suspended)) { unset($course->users[$cuser->id]); } else { if ($onlyrole === null || $onlyrole == $allroles[$cuser->roleid]->shortname) { $members[strtolower($cuser->email)] = $allroles[$cuser->roleid]->shortname; } } } return $members; }
print_error('nocourseid'); } require_login($course); $context = context_course::instance($course->id); require_capability('gradereport/outcomes:view', $context); // First make sure we have proper final grades. grade_regrade_final_grades($courseid); // Grab all outcomes used in course. $report_info = array(); $outcomes = grade_outcome::fetch_all_available($courseid); // Will exclude grades of suspended users if required. $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol); $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol); $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context); if ($showonlyactiveenrol) { $suspendedusers = get_suspended_userids($context); } // Get grade_items that use each outcome. foreach ($outcomes as $outcomeid => $outcome) { $report_info[$outcomeid]['items'] = $DB->get_records_select('grade_items', "outcomeid = ? AND courseid = ?", array($outcomeid, $courseid)); $report_info[$outcomeid]['outcome'] = $outcome; // Get average grades for each item. if (is_array($report_info[$outcomeid]['items'])) { foreach ($report_info[$outcomeid]['items'] as $itemid => $item) { $params = array(); $hidesuspendedsql = ''; if ($showonlyactiveenrol && !empty($suspendedusers)) { list($notinusers, $params) = $DB->get_in_or_equal($suspendedusers, SQL_PARAMS_QM, null, false); $hidesuspendedsql = ' AND userid ' . $notinusers; } $params = array_merge(array($itemid), $params);
function get_roster($onlyrole = null) { //edited below, replaced old method $coursecontext = context_course::instance($this->courseid); $allroles = get_roles_used_in_context($coursecontext); arsort($allroles); if (!$this->visible) { foreach ($allroles as $key => $role) { if ($role->shortname != 'editingteacher') { unset($allroles[$key]); } } } $roles = array_keys($allroles); // can't used canned function as its likely to return a student role // when the user has both a student and a teacher role // so this bit will allow the lower roleid (higher value role) to overwrite the lower one foreach ($roles as $role) { $temp = get_role_users($role, $coursecontext, false, '', null, false); if ($temp !== false && sizeof($temp) !== 0) { if (isset($course->users)) { $course->users = array_merge($course->users, $temp); } else { $course = new stdclass(); $course->users = $temp; } } } $members = array(); $suspended = get_suspended_userids($coursecontext); foreach ($course->users as $cuser) { if (array_key_exists($cuser->id, $suspended)) { /* Explanation of changes made: */ //edited use of unset below: previous implementation wasn't working //because $cuser->id was a number larger than the size of //$course->users. //$course->users is an array of stdClass objects, which have their own IDs, //but those IDs could not be directly accessed in $course->users without callling on the $cuser object. //So the compiler was seeing $cuser->id as an index into the array $course->users //$cuser->id is normally too large to be used to index into $course->users //unset was not working because the index $cuser->id was out of range (larger than the size of $course->users) // ///*unset($course->users[$cuser->id]);*/ //solution has been reimplemented below, and it should now work (hopefully) $deleteUser = array_search($cuser, $course->users); unset($course->users[$deleteUser]); } else { if ($onlyrole === null || $onlyrole == $allroles[$cuser->roleid]->shortname) { $members[strtolower($cuser->email)] = $allroles[$cuser->roleid]->shortname; } } } return $members; }