2006-08-08 05:13:06 +00:00
|
|
|
<?php // $Id$
|
2006-09-03 08:03:02 +00:00
|
|
|
// Script to assign users to contexts
|
2006-08-08 05:13:06 +00:00
|
|
|
|
|
|
|
require_once("../../config.php");
|
2006-09-03 08:03:02 +00:00
|
|
|
require_once($CFG->dirroot.'/mod/forum/lib.php');
|
2006-08-08 05:13:06 +00:00
|
|
|
|
|
|
|
define("MAX_USERS_PER_PAGE", 5000);
|
|
|
|
|
|
|
|
$contextid = required_param('contextid',PARAM_INT); // context id
|
2006-08-11 03:24:37 +00:00
|
|
|
$roleid = optional_param('roleid', 0, PARAM_INT); // required role id
|
2006-08-08 05:13:06 +00:00
|
|
|
$add = optional_param('add', 0, PARAM_BOOL);
|
|
|
|
$remove = optional_param('remove', 0, PARAM_BOOL);
|
|
|
|
$showall = optional_param('showall', 0, PARAM_BOOL);
|
|
|
|
$searchtext = optional_param('searchtext', '', PARAM_RAW); // search string
|
|
|
|
$previoussearch = optional_param('previoussearch', 0, PARAM_BOOL);
|
2006-08-09 13:18:33 +00:00
|
|
|
$hidden = optional_param('hidden', 0, PARAM_BOOL); // whether this assignment is hidden
|
2006-08-08 05:13:06 +00:00
|
|
|
$previoussearch = ($searchtext != '') or ($previoussearch) ? 1:0;
|
2006-08-11 03:24:37 +00:00
|
|
|
$timestart = optional_param('timestart', 0, PARAM_INT);
|
2006-08-09 13:18:33 +00:00
|
|
|
$timeend = optional_param('timened', 0, PARAM_INT);
|
2006-08-14 08:46:44 +00:00
|
|
|
$userid = optional_param('userid', 0, PARAM_INT); // needed for user tabs
|
|
|
|
$courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tabs
|
2006-08-08 05:13:06 +00:00
|
|
|
|
2006-08-14 08:46:44 +00:00
|
|
|
if ($courseid) {
|
|
|
|
$course = get_record('course', 'id', $courseid);
|
2006-09-16 12:41:47 +00:00
|
|
|
} else {
|
|
|
|
$course = $SITE;
|
2006-08-14 08:46:44 +00:00
|
|
|
}
|
|
|
|
|
2006-08-14 05:55:40 +00:00
|
|
|
if (! $context = get_context_instance_by_id($contextid)) {
|
2006-08-08 05:13:06 +00:00
|
|
|
error("Context ID was incorrect (can't find it)");
|
|
|
|
}
|
2006-08-14 05:55:40 +00:00
|
|
|
|
|
|
|
require_capability('moodle/role:assign', $context);
|
2006-08-08 05:13:06 +00:00
|
|
|
|
2006-09-14 09:39:23 +00:00
|
|
|
$assignableroles = get_assignable_roles($context);
|
2006-09-03 08:03:02 +00:00
|
|
|
|
2006-09-16 12:41:47 +00:00
|
|
|
|
|
|
|
/// Get some language strings
|
|
|
|
|
2006-08-09 13:18:33 +00:00
|
|
|
$strassignusers = get_string('assignusers', 'role');
|
|
|
|
$strpotentialusers = get_string('potentialusers', 'role');
|
|
|
|
$strexistingusers = get_string('existingusers', 'role');
|
|
|
|
$straction = get_string('assignroles', 'role');
|
2006-08-28 07:12:10 +00:00
|
|
|
$strroletoassign = get_string('roletoassign', 'role');
|
2006-08-09 13:18:33 +00:00
|
|
|
$strcurrentcontext = get_string('currentcontext', 'role');
|
|
|
|
$strsearch = get_string('search');
|
|
|
|
$strshowall = get_string('showall');
|
2006-09-16 12:41:47 +00:00
|
|
|
$strparticipants = get_string("participants");
|
2006-08-09 13:18:33 +00:00
|
|
|
|
|
|
|
|
2006-09-03 08:03:02 +00:00
|
|
|
|
|
|
|
/// Make sure this user can assign that role
|
|
|
|
|
2006-08-18 08:01:16 +00:00
|
|
|
if ($roleid) {
|
|
|
|
if (!user_can_assign($context, $roleid)) {
|
|
|
|
error ('you can not override this role in this context');
|
|
|
|
}
|
|
|
|
}
|
2006-09-16 12:41:47 +00:00
|
|
|
|
|
|
|
if ($userid) {
|
|
|
|
$user = get_record('user', 'id', $userid);
|
|
|
|
$fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
|
|
|
|
}
|
2006-08-21 05:19:16 +00:00
|
|
|
|
2006-09-03 08:03:02 +00:00
|
|
|
|
|
|
|
/// Print the header and tabs
|
|
|
|
|
2006-09-15 14:09:16 +00:00
|
|
|
if ($context->aggregatelevel == CONTEXT_USER) {
|
2006-08-16 09:11:43 +00:00
|
|
|
/// course header
|
|
|
|
if ($courseid!= SITEID) {
|
|
|
|
print_header("$fullname", "$fullname",
|
|
|
|
"<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> ->
|
2006-09-16 12:41:47 +00:00
|
|
|
<a href=\"".$CFG->wwwroot."/user/index.php?id=$course->id\">$strparticipants</a> -> <a href=\"".$CFG->wwwroot."/user/view.php?id=".$userid."&course=".$courseid."\">$fullname</a> ->".$straction,
|
2006-08-16 09:11:43 +00:00
|
|
|
"", "", true, " ", navmenu($course));
|
|
|
|
|
|
|
|
/// site header
|
|
|
|
} else {
|
|
|
|
print_header("$course->fullname: $fullname", "$course->fullname",
|
|
|
|
"<a href=\"".$CFG->wwwroot."/user/view.php?id=".$userid."&course=".$courseid."\">$fullname</a> -> $straction", "", "", true, " ", navmenu($course));
|
|
|
|
}
|
|
|
|
|
2006-08-15 08:29:29 +00:00
|
|
|
$showroles = 1;
|
|
|
|
$currenttab = 'assign';
|
2006-08-14 08:46:44 +00:00
|
|
|
include_once($CFG->dirroot.'/user/tabs.php');
|
|
|
|
} else {
|
|
|
|
$currenttab = '';
|
|
|
|
$tabsmode = 'assign';
|
|
|
|
include_once('tabs.php');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-03 08:03:02 +00:00
|
|
|
/// Process incoming role assignment
|
2006-08-08 05:13:06 +00:00
|
|
|
|
2006-09-03 08:03:02 +00:00
|
|
|
if ($frm = data_submitted()) {
|
2006-08-08 05:13:06 +00:00
|
|
|
|
|
|
|
if ($add and !empty($frm->addselect) and confirm_sesskey()) {
|
2006-09-03 08:03:02 +00:00
|
|
|
|
|
|
|
$timemodified = time();
|
|
|
|
|
2006-08-08 05:13:06 +00:00
|
|
|
foreach ($frm->addselect as $adduser) {
|
|
|
|
$adduser = clean_param($adduser, PARAM_INT);
|
2006-08-14 05:55:40 +00:00
|
|
|
if (! role_assign($roleid, $adduser, 0, $context->id, $timestart, $timeend, $hidden)) {
|
2006-08-08 05:13:06 +00:00
|
|
|
error("Could not add user with id $adduser to this role!");
|
|
|
|
}
|
|
|
|
}
|
2006-09-03 08:03:02 +00:00
|
|
|
|
2006-08-08 05:13:06 +00:00
|
|
|
} else if ($remove and !empty($frm->removeselect) and confirm_sesskey()) {
|
2006-09-03 08:03:02 +00:00
|
|
|
|
2006-08-08 05:13:06 +00:00
|
|
|
foreach ($frm->removeselect as $removeuser) {
|
|
|
|
$removeuser = clean_param($removeuser, PARAM_INT);
|
2006-08-14 05:55:40 +00:00
|
|
|
if (! role_unassign($roleid, $removeuser, 0, $context->id)) {
|
2006-08-08 05:13:06 +00:00
|
|
|
error("Could not remove user with id $removeuser from this role!");
|
|
|
|
}
|
|
|
|
}
|
2006-09-03 08:03:02 +00:00
|
|
|
|
2006-08-08 05:13:06 +00:00
|
|
|
} else if ($showall) {
|
|
|
|
$searchtext = '';
|
|
|
|
$previoussearch = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-03 08:03:02 +00:00
|
|
|
/// Get all existing participants in this course.
|
|
|
|
|
2006-08-08 05:13:06 +00:00
|
|
|
$existinguserarray = array();
|
|
|
|
|
2006-09-12 07:38:07 +00:00
|
|
|
if (!$contextusers = get_role_users($roleid, $context)) {
|
2006-08-09 13:18:33 +00:00
|
|
|
$contextusers = array();
|
2006-08-08 05:13:06 +00:00
|
|
|
}
|
|
|
|
|
2006-08-09 13:18:33 +00:00
|
|
|
foreach ($contextusers as $contextuser) {
|
|
|
|
$existinguserarray[] = $contextuser->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$existinguserlist = implode(',', $existinguserarray);
|
2006-08-08 05:13:06 +00:00
|
|
|
unset($existinguserarray);
|
|
|
|
|
2006-09-15 14:32:35 +00:00
|
|
|
$usercount = get_users(false, '', true, $existinguserlist);
|
|
|
|
|
2006-08-08 05:13:06 +00:00
|
|
|
/// Get search results excluding any users already in this course
|
|
|
|
if (($searchtext != '') and $previoussearch) {
|
|
|
|
$searchusers = get_users(true, $searchtext, true, $existinguserlist, 'firstname ASC, lastname ASC',
|
2006-09-15 14:32:35 +00:00
|
|
|
'', '', 0, MAX_USERS_PER_PAGE, 'id, firstname, lastname, email');
|
2006-08-08 05:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If no search results then get potential students for this course excluding users already in course
|
|
|
|
if (empty($searchusers)) {
|
|
|
|
$users = array();
|
|
|
|
if ($usercount <= MAX_USERS_PER_PAGE) {
|
2006-09-15 14:32:35 +00:00
|
|
|
if (!$users = get_users(true, '', true, $existinguserlist, 'firstname ASC, lastname ASC', '', '',
|
|
|
|
0, MAX_USERS_PER_PAGE, 'id, firstname, lastname, email')) {
|
|
|
|
$users = array();
|
|
|
|
}
|
2006-08-08 05:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-08-09 13:18:33 +00:00
|
|
|
if ($roleid) {
|
2006-09-15 06:04:10 +00:00
|
|
|
/// prints a form to swap roles
|
|
|
|
echo '<form name="rolesform" action="assign.php" method="post">';
|
|
|
|
echo '<div align="center">'.$strcurrentcontext.': '.print_context_name($context).'<br/>';
|
|
|
|
if ($userid) {
|
|
|
|
echo '<input type="hidden" name="userid" value="'.$userid.'" />';
|
|
|
|
}
|
|
|
|
if ($courseid) {
|
|
|
|
echo '<input type="hidden" name="courseid" value="'.$courseid.'" />';
|
|
|
|
}
|
|
|
|
echo '<input type="hidden" name="contextid" value="'.$context->id.'" />'.$strroletoassign.': ';
|
|
|
|
choose_from_menu ($assignableroles, 'roleid', $roleid, 'choose', $script='rolesform.submit()');
|
|
|
|
echo '</div></form>';
|
2006-08-08 05:13:06 +00:00
|
|
|
|
2006-08-09 13:18:33 +00:00
|
|
|
print_simple_box_start("center");
|
|
|
|
include('assign.html');
|
|
|
|
print_simple_box_end();
|
2006-08-08 05:13:06 +00:00
|
|
|
|
2006-09-15 06:04:10 +00:00
|
|
|
} else { // Print overview table
|
|
|
|
|
2006-09-16 12:41:47 +00:00
|
|
|
$userparam = (!empty($userid)) ? '&userid='.$userid : '';
|
|
|
|
|
2006-09-15 06:04:10 +00:00
|
|
|
$table->tablealign = 'center';
|
|
|
|
$table->cellpadding = 5;
|
|
|
|
$table->cellspacing = 0;
|
|
|
|
$table->width = '20%';
|
|
|
|
$table->head = array(get_string('roles', 'role'), get_string('users'));
|
|
|
|
$table->wrap = array('nowrap', 'nowrap');
|
|
|
|
$table->align = array('right', 'center');
|
|
|
|
|
|
|
|
foreach ($assignableroles as $roleid => $rolename) {
|
|
|
|
$countusers = 0;
|
|
|
|
if ($contextusers = get_role_users($roleid, $context)) {
|
|
|
|
$countusers = count($contextusers);
|
|
|
|
}
|
2006-09-16 12:41:47 +00:00
|
|
|
$table->data[] = array('<a href="assign.php?contextid='.$context->id.'&roleid='.$roleid.$userparam.'">'.$rolename.'</a>', $countusers);
|
2006-09-15 06:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
print_table($table);
|
2006-08-09 13:18:33 +00:00
|
|
|
}
|
2006-09-15 06:04:10 +00:00
|
|
|
|
2006-08-08 05:13:06 +00:00
|
|
|
print_footer($course);
|
|
|
|
|
2006-09-16 12:41:47 +00:00
|
|
|
?>
|