dirroot . '/' . $CFG->admin . '/roles/lib.php'); require_js(array('yui_yahoo', 'yui_dom', 'yui_event')); require_js($CFG->admin . '/roles/roles.js'); define("MAX_USERS_TO_LIST_PER_ROLE", 10); $contextid = required_param('contextid',PARAM_INT); $roleid = optional_param('roleid', 0, PARAM_INT); $userid = optional_param('userid', 0, PARAM_INT); // needed for user tabs $courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tabs $hidden = optional_param('hidden', 0, PARAM_BOOL); // whether this assignment is hidden $extendperiod = optional_param('extendperiod', 0, PARAM_INT); $extendbase = optional_param('extendbase', 3, PARAM_INT); $baseurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/assign.php?contextid=' . $contextid; if (!empty($userid)) { $baseurl .= '&userid='.$userid; } if ($courseid && $courseid != SITEID) { $baseurl .= '&courseid='.$courseid; } if (! $context = get_context_instance_by_id($contextid)) { print_error('wrongcontextid', 'error'); } $isfrontpage = $context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID; $contextname = print_context_name($context); $inmeta = 0; if ($context->contextlevel == CONTEXT_COURSE) { $courseid = $context->instanceid; if ($course = $DB->get_record('course', array('id'=>$courseid))) { $inmeta = $course->metacourse; } else { print_error('invalidcourse', 'error'); } } else if (!empty($courseid)){ // we need this for user tabs in user context if (!$course = $DB->get_record('course', array('id'=>$courseid))) { print_error('invalidcourse', 'error'); } } else { $courseid = SITEID; $course = clone($SITE); } /// Check login and permissions. require_login($course); require_capability('moodle/role:assign', $context); /// These are needed early because of tabs.php list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true); $overridableroles = get_overridable_roles($context, ROLENAME_BOTH); /// Make sure this user can assign this role if ($roleid && !isset($assignableroles[$roleid])) { $a = new stdClass; $a->roleid = $roleid; $a->context = $contextname; print_error('cannotassignrolehere', '', get_context_url($context), $a); } /// Get some language strings $straction = get_string('assignroles', 'role'); // Used by tabs.php /// Work out an appropriate page title. if ($roleid) { $a = new stdClass; $a->role = $assignableroles[$roleid]; $a->context = $contextname; $title = get_string('assignrolenameincontext', 'role', $a); } else { if ($isfrontpage) { $title = get_string('frontpageroles', 'admin'); } else { $title = get_string('assignrolesin', 'role', $contextname); } } /// Build the list of options for the enrolment period dropdown. $unlimitedperiod = get_string('unlimited'); for ($i=1; $i<=365; $i++) { $seconds = $i * 86400; $periodmenu[$seconds] = get_string('numdays', '', $i); } /// Work out the apropriate default setting. if ($extendperiod) { $defaultperiod = $extendperiod; } else { $defaultperiod = $course->enrolperiod; } /// Build the list of options for the starting from dropdown. $timeformat = get_string('strftimedatefullshort'); $today = time(); $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0); // MDL-12420, preventing course start date showing up as an option at system context and front page roles. if ($course->startdate > 0) { $basemenu[2] = get_string('coursestart') . ' (' . userdate($course->startdate, $timeformat) . ')'; } if ($course->enrollable != 2 || ($course->enrolstartdate == 0 || $course->enrolstartdate <= $today) && ($course->enrolenddate == 0 || $course->enrolenddate > $today)) { $basemenu[3] = get_string('today') . ' (' . userdate($today, $timeformat) . ')' ; } if($course->enrollable == 2) { if($course->enrolstartdate > 0) { $basemenu[4] = get_string('courseenrolstart') . ' (' . userdate($course->enrolstartdate, $timeformat) . ')'; } if($course->enrolenddate > 0) { $basemenu[5] = get_string('courseenrolend') . ' (' . userdate($course->enrolenddate, $timeformat) . ')'; } } /// Process any incoming role assignments before printing the header. if ($roleid) { /// Create the user selector objects. $options = array('context' => $context, 'roleid' => $roleid); if ($context->contextlevel > CONTEXT_COURSE && !is_inside_frontpage($context)) { $potentialuserselector = new potential_assignees_below_course('addselect', $options); } else { $potentialuserselector = new potential_assignees_course_and_above('addselect', $options); } if ($context->contextlevel == CONTEXT_SYSTEM && is_admin_role($roleid)) { $currentuserselector = new existing_role_holders_site_admin('removeselect', $options); } else { $currentuserselector = new existing_role_holders('removeselect', $options); } /// Process incoming role assignments $errors = array(); if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) { $userstoassign = $potentialuserselector->get_selected_users(); if (!empty($userstoassign)) { foreach ($userstoassign as $adduser) { $allow = true; if ($inmeta) { if (has_capability('moodle/course:managemetacourse', $context, $adduser->id)) { //ok } else { $managerroles = get_roles_with_capability('moodle/course:managemetacourse', CAP_ALLOW, $context); if (!empty($managerroles) and !array_key_exists($roleid, $managerroles)) { $erruser = $DB->get_record('user', array('id'=>$adduser->id), 'id, firstname, lastname'); $errors[] = get_string('metaassignerror', 'role', fullname($erruser)); $allow = false; } } } if ($allow) { switch($extendbase) { case 2: $timestart = $course->startdate; break; case 3: $timestart = $today; break; case 4: $timestart = $course->enrolstartdate; break; case 5: $timestart = $course->enrolenddate; break; } if($extendperiod > 0) { $timeend = $timestart + $extendperiod; } else { $timeend = 0; } if (! role_assign($roleid, $adduser->id, 0, $context->id, $timestart, $timeend, $hidden)) { $a = new stdClass; $a->role = $assignableroles[$roleid]; $a->user = fullname($adduser); $errors[] = get_string('assignerror', 'role', $a); } } } $potentialuserselector->invalidate_selected_users(); $currentuserselector->invalidate_selected_users(); $rolename = $assignableroles[$roleid]; add_to_log($course->id, 'role', 'assign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id); // Counts have changed, so reload. list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true); } } /// Process incoming role unassignments if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) { $userstounassign = $currentuserselector->get_selected_users(); if (!empty($userstounassign)) { foreach ($userstounassign as $removeuser) { if (! role_unassign($roleid, $removeuser->id, 0, $context->id)) { $a = new stdClass; $a->role = $assignableroles[$roleid]; $a->user = fullname($removeuser); $errors[] = get_string('unassignerror', 'role', $a); } else if ($inmeta) { sync_metacourse($courseid); $newroles = get_user_roles($context, $removeuser->id, false); if (empty($newroles) || array_key_exists($roleid, $newroles)) { $errors[] = get_string('metaunassignerror', 'role', fullname($removeuser)); } } } $potentialuserselector->invalidate_selected_users(); $currentuserselector->invalidate_selected_users(); $rolename = $assignableroles[$roleid]; add_to_log($course->id, 'role', 'unassign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id); // Counts have changed, so reload. list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true); } } } /// Print the header and tabs if ($context->contextlevel == CONTEXT_USER) { $user = $DB->get_record('user', array('id'=>$userid)); $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context)); /// course header $navlinks = array(); if ($courseid != SITEID) { if (has_capability('moodle/course:viewparticipants', get_context_instance(CONTEXT_COURSE, $courseid))) { $navlinks[] = array('name' => get_string('participants'), 'link' => "$CFG->wwwroot/user/index.php?id=$courseid", 'type' => 'misc'); } $navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$userid&course=$courseid", 'type' => 'misc'); $navlinks[] = array('name' => $straction, 'link' => null, 'type' => 'misc'); $navigation = build_navigation($navlinks); print_header($title, $fullname, $navigation, '', '', true, ' ', navmenu($course)); /// site header } else { $navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$userid&course=$courseid", 'type' => 'misc'); $navlinks[] = array('name' => $straction, 'link' => null, 'type' => 'misc'); $navigation = build_navigation($navlinks); print_header($title, $course->fullname, $navigation, '', '', true, ' ', navmenu($course)); } $showroles = 1; $currenttab = 'assign'; include_once($CFG->dirroot.'/user/tabs.php'); } else if ($context->contextlevel == CONTEXT_SYSTEM) { admin_externalpage_setup('assignroles', '', array('contextid' => $contextid, 'roleid' => $roleid)); admin_externalpage_print_header(); } else if ($isfrontpage) { admin_externalpage_setup('frontpageroles', '', array('contextid' => $contextid, 'roleid' => $roleid)); admin_externalpage_print_header(); $currenttab = 'assign'; include_once('tabs.php'); } else { $currenttab = 'assign'; include_once('tabs.php'); } /// Print heading. print_heading_with_help($title, 'assignroles'); if ($roleid) { /// Show UI for assigning a particular role to users. /// Print a warning if we are assigning system roles. if ($context->contextlevel == CONTEXT_SYSTEM) { print_box(get_string('globalroleswarning', 'role')); } /// Print the form. check_theme_arrows(); ?>
'; foreach ($errors as $e) { $msg .= $e.'