protected function definition()
{
global $CFG, $DB;
$collection = $this->_customdata['collection'];
$context = $this->_customdata['context'];
$mform = $this->_form;
// Text search box.
$mform->addElement('text', 'search', get_string('search'));
$mform->setType('search', PARAM_TEXT);
$options = array(\mod_mediagallery\base::TYPE_ALL => get_string('typeall', 'mediagallery'), \mod_mediagallery\base::TYPE_IMAGE => get_string('typeimage', 'mediagallery'), \mod_mediagallery\base::TYPE_VIDEO => get_string('typevideo', 'mediagallery'), \mod_mediagallery\base::TYPE_AUDIO => get_string('typeaudio', 'mediagallery'));
$mform->addElement('select', 'type', get_string('mediatype', 'mediagallery'), $options);
// Role select dropdown includes all roles, but using course-specific
// names if applied. The reason for not restricting to roles that can
// be assigned at course level is that upper-level roles display in the
// enrolments table so it makes sense to let users filter by them.
$rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true);
if (!empty($CFG->showenrolledusersrolesonly)) {
$rolenames = role_fix_names(get_roles_used_in_context($context));
}
$mform->addElement('select', 'role', get_string('role'), array(0 => get_string('all')) + $rolenames);
// Filter by group.
$allgroups = groups_get_all_groups($collection->course);
$groupsmenu[0] = get_string('allparticipants');
foreach ($allgroups as $gid => $unused) {
$groupsmenu[$gid] = $allgroups[$gid]->name;
}
if (count($groupsmenu) > 1) {
$mform->addElement('select', 'group', get_string('group'), $groupsmenu);
}
// Submit button does not use add_action_buttons because that adds
// another fieldset which causes the CSS style to break in an unfixable
// way due to fieldset quirks.
$group = array();
$group[] = $mform->createElement('submit', 'submitbutton', get_string('filter'));
$group[] = $mform->createElement('submit', 'resetbutton', get_string('reset'));
$group[] = $mform->createElement('submit', 'exportbutton', get_string('exportascsv', 'mediagallery'));
$mform->addGroup($group, 'buttons', '', ' ', false);
$mform->addElement('hidden', 'id', $context->instanceid);
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'action', 'search');
$mform->setType('action', PARAM_ALPHA);
}
/**
* @desc Finds all of the users in the course
* @param $courseid -- the course id
* @return record containing user information ( username, userid)
*/
function _wwassignment_get_course_students($courseid)
{
debugLog("Begin get_course_students({$courseid} )");
debugLog("courseID is " . print_r($courseid, true));
$context = get_context_instance(CONTEXT_COURSE, $courseid);
debugLog("context is " . print_r($context, true));
$users = array();
$roles_used_in_context = get_roles_used_in_context($context);
//debugLog("roles used ". print_r($roles_used_in_context, true));
foreach ($roles_used_in_context as $role) {
$roleid = $role->id;
debugLog("roleid should be 5 for a student {$roleid}");
//debugLog(get_role_users($roleid, $context, true) );
if ($new_users = get_role_users($roleid, $context, true)) {
$users = array_merge($users, $new_users);
//FIXME a user could be liseted twice
}
debugLog("display users " . print_r($users, true));
}
debugLog("display users in course--on");
debugLog("users again" . print_r($users, true));
debugLog("End get_course_students({$courseid} )");
return $users;
}
/**
* Gets potential group members for grouping
* @param int $courseid The id of the course
* @param int $roleid The role to select users from
* @param string $orderby The colum to sort users by
* @return array An array of the users
*/
function groups_get_potential_members($courseid, $roleid = null, $orderby = 'lastname,firstname')
{
global $CFG;
$context = get_context_instance(CONTEXT_COURSE, $courseid);
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
$rolenames = array();
$avoidroles = array();
if ($roles = get_roles_used_in_context($context, true)) {
$canviewroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $context);
$doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext);
foreach ($roles as $role) {
if (!isset($canviewroles[$role->id])) {
// Avoid this role (eg course creator)
$avoidroles[] = $role->id;
unset($roles[$role->id]);
continue;
}
if (isset($doanythingroles[$role->id])) {
// Avoid this role (ie admin)
$avoidroles[] = $role->id;
unset($roles[$role->id]);
continue;
}
$rolenames[$role->id] = strip_tags(role_get_name($role, $context));
// Used in menus etc later on
}
}
$select = 'SELECT u.id, u.username, u.firstname, u.lastname, u.idnumber ';
$from = "FROM {$CFG->prefix}user u INNER JOIN\n {$CFG->prefix}role_assignments r on u.id=r.userid ";
if ($avoidroles) {
$adminroles = 'AND r.roleid NOT IN (';
$adminroles .= implode(',', $avoidroles);
$adminroles .= ')';
} else {
$adminroles = '';
}
// we are looking for all users with this role assigned in this context or higher
if ($usercontexts = get_parent_contexts($context)) {
$listofcontexts = '(' . implode(',', $usercontexts) . ')';
} else {
$listofcontexts = '(' . $sitecontext->id . ')';
// must be site
}
if ($roleid) {
$selectrole = " AND r.roleid = {$roleid} ";
} else {
$selectrole = " ";
}
$where = "WHERE (r.contextid = {$context->id} OR r.contextid in {$listofcontexts})\n AND u.deleted = 0 {$selectrole}\n AND u.username != 'guest'\n {$adminroles} ";
$order = "ORDER BY {$orderby} ";
return get_records_sql($select . $from . $where . $order);
}
} else {
$groupid = 0;
}
$usrcanjoin = false;
$usrgroups = groups_get_user_groups($cm->course, $usrobj->id);
$usrgroups = $usrgroups[0];
// Just want groups and not groupings
// If separate groups is enabled, check if the user is a part of the selected group
if (0 != $cm->groupmode) {
if (false !== array_search($groupid, $usrgroups)) {
$usrcanjoin = true;
}
}
$context = get_context_instance(CONTEXT_COURSE, $cm->course);
// Make sure the user has a role in the course
$crsroles = get_roles_used_in_context($context);
if (empty($crsroles)) {
$crsroles = array();
}
foreach ($crsroles as $roleid => $crsrole) {
if (user_has_role_assignment($usrobj->id, $roleid, $context->id)) {
$usrcanjoin = true;
}
}
// user has to be in a group
if ($usrcanjoin and confirm_sesskey($sesskey)) {
$usrprincipal = 0;
$validuser = true;
$groupobj = groups_get_group($groupid);
// Get the meeting sco-id
$param = array('instanceid' => $cm->instance, 'groupid' => $groupid);
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;
}
/**
* This function returns an object of all users whithin current course who match
* the search query.
* *Modified version of datalib.php's search_user() function
*
* @param object $course Current Course object
* @param string $query Search query
* @param boolean $dispadmins Flag to return course admins or not
* @param boolean $displayunconfirmed Flag to specify to return unconfirmed users
* @return object result set of all matching users
* @todo Add option to remove active user from results
*/
function email_search_course_users($course, $query = '', $dispadmins = false, $dispunconfirmed = true)
{
global $CFG, $USER;
$LIKE = sql_ilike();
$order = 'ORDER BY firstname, lastname, id';
$select = 'u.deleted = \'0\'';
if (!$dispunconfirmed) {
$select .= ' AND u.confirmed = \'1\'';
}
if (!$course or $course->id == SITEID) {
$results = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email\n FROM {$CFG->prefix}user u\n WHERE {$select}\n AND (u.firstname {$LIKE} '{$query}%' OR u.lastname {$LIKE} '{$query}%')\n AND u.username != 'guest'\n {$order}");
} else {
if ($course->id == SITEID) {
$context = get_context_instance(CONTEXT_SYSTEM, SITEID);
} else {
$context = get_context_instance(CONTEXT_COURSE, $course->id);
}
$contextlists = get_related_contexts_string($context);
// Returns only group(s) members for users without the viewallgroups capability
$groupmembers = '';
// Separate groups
$groupmode = groups_get_course_groupmode($course);
if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
// Returns all groups current user is assigned to in course
if ($groups = groups_get_all_groups($course->id, $USER->id)) {
$groupmembers = array();
foreach ($groups as $group) {
$groupmembers += groups_get_members($group->id, 'u.id');
}
if (!empty($groupmembers)) {
$groupmembers = 'AND u.id IN (' . implode(',', array_keys($groupmembers)) . ')';
} else {
// Nobody in their groups :(
return false;
}
} else {
// They have no group :(
return false;
}
}
// Hides course admin roles (eg: admin && course creator) if requested (default)
if (!$dispadmins) {
$avoidroles = array();
if ($roles = get_roles_used_in_context($context, true)) {
$canviewroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $context);
$doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $context);
if (!$CFG->email_add_admins) {
$adminsroles = get_roles_with_capability('moodle/legacy:admin', CAP_ALLOW, $context);
}
foreach ($roles as $role) {
if (!isset($canviewroles[$role->id])) {
// Avoid this role (eg course creator)
$avoidroles[] = $role->id;
unset($roles[$role->id]);
continue;
}
if (isset($doanythingroles[$role->id])) {
// Avoid this role (ie admin)
$avoidroles[] = $role->id;
unset($roles[$role->id]);
continue;
}
if (!$CFG->email_add_admins) {
if (isset($adminsroles[$role->id])) {
// Avoid this role (ie admin)
$avoidroles[] = $role->id;
unset($roles[$role->id]);
continue;
}
}
}
}
// exclude users with roles we are avoiding
if ($avoidroles) {
$adminroles = 'AND ra.roleid NOT IN (';
$adminroles .= implode(',', $avoidroles);
$adminroles .= ')';
} else {
$adminroles = '';
}
} else {
$adminroles = '';
}
$results = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email\n FROM {$CFG->prefix}user u,\n {$CFG->prefix}role_assignments ra\n WHERE {$select} AND ra.contextid {$contextlists} AND ra.userid = u.id\n AND (u.firstname {$LIKE} '{$query}%' OR u.lastname {$LIKE} '{$query}%')\n AND (u.username != 'guest')\n {$adminroles} {$groupmembers} {$order}");
}
return $results;
}
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;
}
/**
* Gets potential group members for grouping
* @param int $courseid The id of the course
* @param int $roleid The role to select users from
* @param string $orderby The colum to sort users by
* @return array An array of the users
*/
function groups_get_potential_members($courseid, $roleid = null, $orderby = 'lastname,firstname')
{
global $DB;
$context = get_context_instance(CONTEXT_COURSE, $courseid);
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
$rolenames = array();
$avoidroles = array();
if ($roles = get_roles_used_in_context($context, true)) {
$canviewroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $context);
$doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext);
foreach ($roles as $role) {
if (!isset($canviewroles[$role->id])) {
// Avoid this role (eg course creator)
$avoidroles[] = $role->id;
unset($roles[$role->id]);
continue;
}
if (isset($doanythingroles[$role->id])) {
// Avoid this role (ie admin)
$avoidroles[] = $role->id;
unset($roles[$role->id]);
continue;
}
$rolenames[$role->id] = strip_tags(role_get_name($role, $context));
// Used in menus etc later on
}
}
if ($avoidroles) {
list($adminroles, $params) = $DB->get_in_or_equal($avoidroles, SQL_PARAMS_NAMED, 'ar0', false);
$adminroles = "AND r.roleid {$adminroles}";
} else {
$adminroles = "";
$params = array();
}
// we are looking for all users with this role assigned in this context or higher
if ($usercontexts = get_parent_contexts($context)) {
$listofcontexts = 'IN (' . implode(',', $usercontexts) . ')';
} else {
$listofcontexts = '=' . $sitecontext->id . ')';
// must be site
}
if ($roleid) {
$selectrole = "AND r.roleid = :roleid";
$params['roleid'] = $roleid;
} else {
$selectrole = "";
}
$sql = "SELECT u.id, u.username, u.firstname, u.lastname, u.idnumber\n FROM {user} u\n JOIN {role_assignments} r on u.id=r.userid\n WHERE (r.contextid = :contextid OR r.contextid {$listofcontexts})\n AND u.deleted = 0 AND u.username != 'guest'\n {$selectrole} {$adminroles}\n ORDER BY {$orderby}";
$params['contextid'] = $context->id;
return $DB->get_records_sql($sql, $params);
}
/**
*this function determins the desktops started and available in this course
*TODO: this function needs performance improvements
* @return array of moodle_urls or empty array
*/
private function available_desktops()
{
global $CFG, $PAGE, $USER, $DB;
$url = array();
if (has_capability('mod/opendesktop:joinactive', $this->context) or has_capability('mod/opendesktop:joinpassive', $this->context)) {
if ($roles = get_roles_used_in_context($this->context, true)) {
$canstartusers = get_users_by_capability($this->context, 'mod/opendesktop:start', 'u.id,username,firstname,lastname');
}
foreach ($canstartusers as $canstartuser) {
$sql = 'SELECT MAX(starttime) AS starttime FROM {opendesktop_sessions} WHERE userid=' . $canstartuser->id;
$maxstarttime = $DB->get_field_sql($sql);
$sessionmode = $DB->get_field('opendesktop_sessions', 'sessionmode', array('userid' => $canstartuser->id, 'starttime' => $maxstarttime));
if ($USER->username != $canstartuser->username && $this->opendesktop->id == $DB->get_field('opendesktop_sessions', 'opendesktop', array('userid' => $canstartuser->id, 'starttime' => $maxstarttime))) {
// check if any user with the capability to start a desktop in this course has started OVD
$sessvaldetect = $this->session->sessionstatus($CFG->opendesktop_useprefix . $canstartuser->username);
// check if the opendesktop was started in the same course, if not do not display the possibility to join
if ($sessvaldetect['status'] == 'active' && $sessvaldetect['isregistered']) {
if (has_capability('mod/opendesktop:joinactive', $this->context)) {
$url[$canstartuser->username]['active'] = new moodle_url($PAGE->url, array('id' => $this->cm->id, 'task' => 'join', 'join' => 'active', 'sesskey' => sesskey(), 'sessionmode' => $sessionmode, 'ovduser' => $canstartuser->id));
}
if (has_capability('mod/opendesktop:joinpassive', $this->context)) {
$url[$canstartuser->username]['passive'] = new moodle_url($PAGE->url, array('id' => $this->cm->id, 'task' => 'join', 'join' => 'passive', 'sesskey' => sesskey(), 'sessionmode' => $sessionmode, 'ovduser' => $canstartuser->id));
}
$url[$canstartuser->username]['fullname'] = $canstartuser->firstname . ' ' . $canstartuser->lastname;
}
}
}
}
return $url;
}
unset($contextid);
unset($courseid);
require_login($course);
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
$frontpagectx = get_context_instance(CONTEXT_COURSE, SITEID);
if ($context->id != $frontpagectx->id) {
require_capability('moodle/course:viewparticipants', $context);
} else {
require_capability('moodle/site:viewparticipants', $systemcontext);
// override the default on frontpage
$roleid = optional_param('roleid', -1, PARAM_INT);
}
/// front page course is different
$rolenames = array();
$avoidroles = array();
if ($roles = get_roles_used_in_context($context, true)) {
// We should ONLY allow roles with moodle/course:view because otherwise we get little niggly issues
// like MDL-8093
// We should further exclude "admin" users (those with "doanything" at site level) because
// Otherwise they appear in every participant list
$canviewroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $context);
$doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $systemcontext);
if ($context->id == $frontpagectx->id) {
//we want admins listed on frontpage too
foreach ($doanythingroles as $dar) {
$canviewroles[$dar->id] = $dar;
}
$doanythingroles = array();
}
foreach ($roles as $role) {
if (!isset($canviewroles[$role->id])) {
请发表评论