From 7dba450fd44c5f1fc6f1dc7d689c14e70c973707 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Wed, 23 Nov 2016 22:57:36 +0800 Subject: [PATCH] MDL-57001 user_menu: Hide switch roles students Check that there are switchable roles before showing the link in the user menu. --- user/lib.php | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/user/lib.php b/user/lib.php index 283e1dc60aa55..cb0183dc3af15 100644 --- a/user/lib.php +++ b/user/lib.php @@ -929,18 +929,21 @@ function user_get_user_navigation_info($user, $page, $options = array()) { } } 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; + // Build switch role link. + $roles = get_switchable_roles($context); + if (is_array($roles) && (count($roles) > 0)) { + $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;