It's not well-tested yet and probably doesn't quite work. I will keep debugging this tonight from home.
$section = optional_param('section', 0, PARAM_INT);
$move = optional_param('move', 0, PARAM_INT);
$marker = optional_param('marker',-1 , PARAM_INT);
+ $switchrole = optional_param('switchrole',-1, PARAM_INT);
+
if (empty($id) && empty($name) && empty($idnumber)) {
}
}
+ if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
+ print_error('nocontext');
+ }
+
require_login($course->id);
+ if ($switchrole > -1) {
+ role_switch($switchrole, $context);
+ require_login($course->id);
+ }
+
//If course is hosted on an external server, redirect to corresponding
//url with appropriate authentication attached as parameter
if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
}
}
+
require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
$string['includeuserfiles'] = 'Include User Files';
$string['info'] = 'Information';
$string['institution'] = 'Institution';
-$string['instudentview'] = 'in student view';
$string['invalidemail'] = 'Invalid email address';
$string['invalidlogin'] = 'Invalid login, please try again';
$string['ip_address'] = 'IP Address';
$string['studentnotallowed'] = 'Sorry, but you can not enter this course as \'$a\'';
$string['students'] = 'Students';
$string['studentsandteachers'] = 'Students and teachers';
-$string['studentviewoff'] = 'Turn student view off';
-$string['studentviewon'] = 'Turn student view on';
$string['subcategories'] = 'Sub-categories';
$string['success'] = 'Success';
$string['summary'] = 'Summary';
$string['summaryof'] = 'Summary of $a';
$string['supplyinfo'] = 'Please supply some information about yourself';
+$string['switchrolereturn'] = 'Return to my normal role';
+$string['switchroleto'] = 'Switch role to...';
$string['tag'] = 'Tag';
$string['tagmanagement'] = 'Add/delete tags ...';
$string['tags'] = 'Tags';
}
+/*
+ * Switches the current user to another role for the current session and only
+ * in the given context. If roleid is not valid (eg 0) or the current user
+ * doesn't have permissions to be switching roles then the user's session
+ * is compltely reset to have their normal roles.
+ * @param integer $roleid
+ * @param object $context
+ * @return bool
+ */
+function role_switch($roleid, $context) {
+ global $USER;
+
+ global $db;
+
+/// If we can't use this or are already using it or no role was specified then bail completely and reset
+ if (empty($roleid) || !has_capability('moodle/role:switchroles', $context)
+ || !empty($USER->switchrole) || !confirm_sesskey()) {
+ load_user_capability(); // Reset all permissions to normal
+ unset($USER->switchrole); // Delete old capabilities
+ return true;
+ }
+
+/// We're allowed to switch but can we switch to the specified role? Use assignable roles to check.
+ if (!$roles = get_assignable_roles($context)) {
+ return false;
+ }
+
+ if (empty($roles[$roleid])) { /// We can't switch to this particular role
+ return false;
+ }
+
+ if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID)) {
+ return false;
+ }
+
+/// We have a valid roleid that this user can switch to, so let's set up the session
+
+ $USER->switchrole = $roleid; // So we know later what state we are in
+
+ unset($USER->capabilities[$context->id]); // Delete old capabilities
+
+ if ($capabilities = get_records_select('role_capabilities', "roleid = $roleid AND contextid = $sitecontext->id")) {
+ foreach ($capabilities as $capability) {
+ $USER->capabilities[$context->id][$capability->capability] = $capability->permission;
+ }
+ }
+
+/// Add some capabilities we are really going to always need, even if the role doesn't have them!
+
+ $USER->capabilities[$context->id]['moodle/course:view'] = CAP_ALLOW;
+
+ return true;
+
+}
+
+
// get any role that has an override on exact context
function get_roles_with_override_on_context($context) {
WHERE contextid = $context->id
AND roleid = $role->id");
}
-?>
\ No newline at end of file
+
+?>
// The "Editing On" button will be appearing only in the "main" course screen
// (i.e., no breadcrumbs other than the default one added inside this function)
- $buttons = update_course_icon($this->courserecord->id );
+ $buttons = switchroles_form($this->courserecord->id) . update_course_icon($this->courserecord->id );
$buttons = empty($morebreadcrumbs) ? $buttons : ' ';
print_header($title, $this->courserecord->fullname, $crumbtext,
}
}
+/**
+ * Returns a little popup menu for switching roles
+ *
+ * @uses $CFG
+ * @uses $USER
+ * @param int $courseid The course to update by id as found in 'course' table
+ * @return string
+ */
+function switchroles_form($courseid) {
+
+ global $CFG, $USER;
+
+
+ if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
+ return '';
+ }
+
+ if (has_capability('moodle/role:switchroles', $context)) {
+ if (empty($USER->switchrole)) { // Print a menu
+ if (!$roles = get_assignable_roles($context)) {
+ return ''; // Nothing to show!
+ }
+
+ return popup_form($CFG->wwwroot.'/course/view.php?id='.$courseid.'&sesskey='.sesskey().'&switchrole=',
+ $roles, 'switchrole', '', get_string('switchroleto'), 'switchrole', '', true);
+
+ } else { // Just a button to return to normal
+ $options = array();
+ $options['id'] = $courseid;
+ $options['sesskey'] = sesskey();
+ $options['switchrole'] = 0;
+
+ return print_single_button($CFG->wwwroot.'/course/view.php', $options,
+ get_string('switchrolereturn'), 'post', '_self', true);
+ }
+ }
+
+ return '';
+}
+
/**
* Returns a turn edit on/off button for course in a self contained form.
}
// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
-?>
\ No newline at end of file
+?>