Skip to content

Commit

Permalink
MDL-57001 switchroles: Move to user menu
Browse files Browse the repository at this point in the history
Take this out of the navigation and force it in the user menu.
  • Loading branch information
Damyon Wiese committed Nov 24, 2016
1 parent 7095f5b commit 70b03ef
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 71 deletions.
25 changes: 22 additions & 3 deletions course/switchrole.php
Expand Up @@ -81,9 +81,28 @@
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('switchroleto'));

require_once($CFG->dirroot.'/course/switchrole_form.php');
$form = new switchrole_form(null, ['course' => $course]);
$form->display();
// Overall criteria aggregation.
$roles = array();
$assumedrole = -1;
if (is_role_switched($course->id)) {
$roles[0] = get_string('switchrolereturn');
$assumedrole = $USER->access['rsw'][$context->path];
}
$availableroles = get_switchable_roles($context);
if (is_array($availableroles)) {
foreach ($availableroles as $key => $role) {
if ($assumedrole == (int)$key) {
continue;
}
$roles[$key] = $role;
}
}
echo $OUTPUT->box(markdown_to_html(get_string('switchroleto_help')));

foreach ($roles as $key => $role) {
$url = new moodle_url('/course/switchrole.php', array('id' => $id, 'switchrole' => $key, 'returnurl' => $returnurl));
echo $OUTPUT->container($OUTPUT->single_button($url, $role), 'm-x-3 m-b-1');
}

echo $OUTPUT->footer();
exit;
Expand Down
4 changes: 2 additions & 2 deletions course/tests/behat/rename_roles.feature
Expand Up @@ -26,7 +26,7 @@ Feature: Rename roles within a course
| Your word for 'Non-editing teacher' | Tutor |
| Your word for 'Student' | Learner |
And I press "Save and display"
And I expand "Switch role to..." node
And I follow "Switch role to..." in the user menu
Then I should see "Tutor"
And I should see "Learner"
And I follow "Participants"
Expand All @@ -38,7 +38,7 @@ Feature: Rename roles within a course
| Your word for 'Non-editing teacher' | |
| Your word for 'Student' | |
And I press "Save and display"
And I expand "Switch role to..." node
And I follow "Switch role to..." in the user menu
And I should see "Teacher"
And I should see "Student"
And I should not see "Learner"
Expand Down
3 changes: 3 additions & 0 deletions lang/en/moodle.php
Expand Up @@ -1831,6 +1831,9 @@
$string['switchdevicerecommended'] = 'Switch to the recommended theme for your device';
$string['switchrolereturn'] = 'Return to my normal role';
$string['switchroleto'] = 'Switch role to...';
$string['switchroleto_help'] = 'Choose a role to get an idea of how someone with that role will see this course.
Please note that this view may not be perfect (<a href="https://docs.moodle.org/en/Switch_roles">See details and alternatives</a>).';
$string['tag'] = 'Tag';
$string['tagalready'] = 'This tag already exists';
$string['tagmanagement'] = 'Add/delete tags ...';
Expand Down
29 changes: 0 additions & 29 deletions lib/navigationlib.php
Expand Up @@ -4263,35 +4263,6 @@ protected function load_course_settings($forceopen = false) {

}

// Switch roles
$roles = array();
$assumedrole = $this->in_alternative_role();
if ($assumedrole !== false) {
$roles[0] = get_string('switchrolereturn');
}
if ($adminoptions->roles) {
$availableroles = get_switchable_roles($coursecontext);
if (is_array($availableroles)) {
foreach ($availableroles as $key=>$role) {
if ($assumedrole == (int)$key) {
continue;
}
$roles[$key] = $role;
}
}
}
if (is_array($roles) && count($roles)>0) {
$url = new moodle_url('/course/switchrole.php', array('id'=>$course->id, 'switchrole'=>'-1', 'returnurl'=>$this->page->url->out_as_local_url(false)));
$switchroles = $coursenode->add(get_string('switchroleto'), $url, self::TYPE_CONTAINER, null, 'switchroleto');
if ((count($roles)==1 && array_key_exists(0, $roles))|| $assumedrole!==false) {
$switchroles->force_open();
}
foreach ($roles as $key => $name) {
$url = new moodle_url('/course/switchrole.php', array('id'=>$course->id, 'sesskey'=>sesskey(), 'switchrole'=>$key, 'returnurl'=>$this->page->url->out_as_local_url(false)));
$switchroles->add($name, $url, self::TYPE_SETTING, null, $key, new pix_icon('i/switchrole', ''));
}
}

// Let plugins hook into course navigation.
$pluginsfunction = get_plugins_with_function('extend_navigation_course', 'lib.php');
foreach ($pluginsfunction as $plugintype => $plugins) {
Expand Down
74 changes: 37 additions & 37 deletions user/lib.php
Expand Up @@ -862,51 +862,16 @@ function user_get_user_navigation_info($user, $page, $options = array()) {
$myprofile->pix = "i/user";
$returnobject->navitems[] = $myprofile;

// Links: Role-return or logout link.
$lastobj = null;
$returnobject->metadata['asotherrole'] = false;
if (is_role_switched($course->id)) {
if ($role = $DB->get_record('role', array('id' => $user->access['rsw'][$context->path]))) {
// Build role-return link instead of logout link.
$rolereturn = new stdClass();
$rolereturn->itemtype = 'link';
$rolereturn->url = new moodle_url('/course/switchrole.php', array(
'id' => $course->id,
'sesskey' => sesskey(),
'switchrole' => 0,
'returnurl' => $page->url->out_as_local_url(false)
));
$rolereturn->pix = "a/logout";
$rolereturn->title = get_string('switchrolereturn');
$rolereturn->titleidentifier = 'switchrolereturn,moodle';
$returnobject->navitems[] = $rolereturn;

$returnobject->metadata['asotherrole'] = true;
$returnobject->metadata['rolename'] = role_get_name($role, $context);

}
} else {
// Build role-return link instead of logout link.
$switchrole = new stdClass();
$switchrole->itemtype = 'link';
$switchrole->url = new moodle_url('/course/switchrole.php', array(
'id' => $course->id,
'switchrole' => -1,
'returnurl' => $page->url->out_as_local_url(false)
));
$switchrole->pix = "a/logout";
$switchrole->title = get_string('switchroleto');
$switchrole->titleidentifier = 'switchroleto,moodle';
$returnobject->navitems[] = $switchrole;
}

// Before we add the last item (usually a logout link), add any
// Before we add the last items (usually a logout + switch role link), add any
// custom-defined items.
$customitems = user_convert_text_to_menu_items($CFG->customusermenuitems, $page);
foreach ($customitems as $item) {
$returnobject->navitems[] = $item;
}


if ($returnobject->metadata['asotheruser'] = \core\session\manager::is_loggedinas()) {
$realuser = \core\session\manager::get_realuser();

Expand Down Expand Up @@ -943,6 +908,41 @@ function user_get_user_navigation_info($user, $page, $options = array()) {
$returnobject->navitems[] = $logout;
}

if (is_role_switched($course->id)) {
if ($role = $DB->get_record('role', array('id' => $user->access['rsw'][$context->path]))) {
// Build role-return link instead of logout link.
$rolereturn = new stdClass();
$rolereturn->itemtype = 'link';
$rolereturn->url = new moodle_url('/course/switchrole.php', array(
'id' => $course->id,
'sesskey' => sesskey(),
'switchrole' => 0,
'returnurl' => $page->url->out_as_local_url(false)
));
$rolereturn->pix = "a/logout";
$rolereturn->title = get_string('switchrolereturn');
$rolereturn->titleidentifier = 'switchrolereturn,moodle';
$returnobject->navitems[] = $rolereturn;

$returnobject->metadata['asotherrole'] = true;
$returnobject->metadata['rolename'] = role_get_name($role, $context);

}
} else {
// Build role-return link instead of logout link.
$switchrole = new stdClass();
$switchrole->itemtype = 'link';
$switchrole->url = new moodle_url('/course/switchrole.php', array(
'id' => $course->id,
'switchrole' => -1,
'returnurl' => $page->url->out_as_local_url(false)
));
$switchrole->pix = "i/switchrole";
$switchrole->title = get_string('switchroleto');
$switchrole->titleidentifier = 'switchroleto,moodle';
$returnobject->navitems[] = $switchrole;
}

return $returnobject;
}

Expand Down

0 comments on commit 70b03ef

Please sign in to comment.