Skip to content

Commit

Permalink
MDL-48542 user_menu: add proper divider support
Browse files Browse the repository at this point in the history
Added divider support to the user menu; hardened the custom user menu against invalid input.
  • Loading branch information
Jetha Chan committed Jan 19, 2015
1 parent 3014b8c commit 2dfdb12
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lang/en/admin.php
Expand Up @@ -178,7 +178,7 @@
#####
Moodle.com|http://moodle.com/
</pre>';
$string['configcustomusermenuitems'] = 'You can configure the contents of the user menu (with the exception of the log out link, which is automatically added). Each line is separated by | characters and consists of 1) a string in "langstringname, componentname" form or as plain text, 2) a URL, and 3) an icon either as a pix icon or as a URL.';
$string['configcustomusermenuitems'] = 'You can configure the contents of the user menu (with the exception of the log out link, which is automatically added). Each line is separated by | characters and consists of 1) a string in "langstringname, componentname" form or as plain text, 2) a URL, and 3) an icon either as a pix icon or as a URL. Dividers can be used by adding a line of one or more # characters where desired.';
$string['configdbsessions'] = 'If enabled, this setting will use the database to store information about current sessions. Note that changing this setting now will log out all current users (including you). If you are using MySQL please make sure that \'max_allowed_packet\' in my.cnf (or my.ini) is at least 4M. Other session drivers can be configured directly in config.php, see config-dist.php for more information. This option disappears if you specify session driver in config.php file.';
$string['configdebug'] = 'If you turn this on, then PHP\'s error_reporting will be increased so that more warnings are printed. This is only useful for developers.';
$string['configdebugdisplay'] = 'Set to on, the error reporting will go to the HTML page. This is practical, but breaks XHTML, JS, cookies and HTTP headers in general. Set to off, it will send the output to your server logs, allowing better debugging. The PHP setting error_log controls which log this goes to.';
Expand Down
58 changes: 37 additions & 21 deletions lib/outputrenderers.php
Expand Up @@ -3048,28 +3048,44 @@ public function user_menu($user = null, $withlinks = null) {
$navitemcount = count($opts->navitems);
$idx = 0;
foreach ($opts->navitems as $key => $value) {
$pix = null;
if (isset($value->pix) && !empty($value->pix)) {
$pix = new pix_icon($value->pix, $value->title, null, array('class' => 'iconsmall'));
} else if (isset($value->imgsrc) && !empty($value->imgsrc)) {
$value->title = html_writer::img(
$value->imgsrc,
$value->title,
array('class' => 'iconsmall')
) . $value->title;
}
$al = new action_menu_link_secondary(
$value->url,
$pix,
$value->title,
array('class' => 'icon')
);
$am->add($al);

// Add dividers after the first item and before the
// last item.
if ($idx == 0 || $idx == $navitemcount - 2) {
$am->add($divider);
switch ($value->itemtype) {
case 'divider':
// If the nav item is a divider, add one and skip link processing.
$am->add($divider);
$idx++;
break;

case 'invalid':
// Silently skip invalid entries (should we post a notification?).
break;

case 'link':
// Process this as a link item.
$pix = null;
if (isset($value->pix) && !empty($value->pix)) {
$pix = new pix_icon($value->pix, $value->title, null, array('class' => 'iconsmall'));
} else if (isset($value->imgsrc) && !empty($value->imgsrc)) {
$value->title = html_writer::img(
$value->imgsrc,
$value->title,
array('class' => 'iconsmall')
) . $value->title;
}
$al = new action_menu_link_secondary(
$value->url,
$pix,
$value->title,
array('class' => 'icon')
);
$am->add($al);

// Add dividers after the first item and before the
// last item.
if ($idx == 0 || $idx == $navitemcount - 2) {
$am->add($divider);
}
break;
}

$idx++;
Expand Down
26 changes: 21 additions & 5 deletions user/lib.php
Expand Up @@ -615,8 +615,8 @@ function user_count_login_failures($user, $reset = true) {
}

/**
* Converts a string into a flat array of links, where each link is a
* stdClass with fields url, title, pix, and imgsrc.
* Converts a string into a flat array of menu items, where each menu items is a
* stdClass with fields type, url, title, pix, and imgsrc.
*
* @param string $text the menu items definition
* @param moodle_page $page the current page
Expand All @@ -634,7 +634,10 @@ function user_convert_text_to_menu_items($text, $page) {
foreach ($lines as $line) {
$line = trim($line);
$bits = explode('|', $line, 3);
if (!array_key_exists(0, $bits) or empty($bits[0])) {
$itemtype = 'link';
if (preg_match("/^#+$/", $line)) {
$itemtype = 'divider';
} else if (!array_key_exists(0, $bits) or empty($bits[0])) {
// Every item must have a name to be valid.
continue;
} else {
Expand All @@ -643,6 +646,13 @@ function user_convert_text_to_menu_items($text, $page) {

// Create the child.
$child = new stdClass();
$child->itemtype = $itemtype;
if ($itemtype === 'divider') {
// Add the divider to the list of children and skip link
// processing.
$children[] = $child;
continue;
}

// Name processing.
$namebits = explode(',', $bits[0], 2);
Expand All @@ -656,8 +666,9 @@ function user_convert_text_to_menu_items($text, $page) {

// URL processing.
if (!array_key_exists(1, $bits) or empty($bits[1])) {
// Set the url to null.
// Set the url to null, and set the itemtype to invalid.
$bits[1] = null;
$child->itemtype = "invalid";
} else {
// Make sure the url is a moodle url.
$bits[1] = new moodle_url(trim($bits[1]));
Expand Down Expand Up @@ -788,13 +799,15 @@ function user_get_user_navigation_info($user, $page) {

// Links: My Home.
$myhome = new stdClass();
$myhome->itemtype = 'link';
$myhome->url = new moodle_url('/my/');
$myhome->title = get_string('mymoodle', 'admin');
$myhome->pix = "i/course";
$returnobject->navitems[] = $myhome;

// Links: My Profile.
$myprofile = new stdClass();
$myprofile->itemtype = 'link';
$myprofile->url = new moodle_url('/user/profile.php', array('id' => $user->id));
$myprofile->title = get_string('myprofile');
$myprofile->pix = "i/user";
Expand All @@ -808,6 +821,7 @@ function user_get_user_navigation_info($user, $page) {
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(),
Expand Down Expand Up @@ -845,6 +859,7 @@ function user_get_user_navigation_info($user, $page) {

// Build a user-revert link.
$userrevert = new stdClass();
$userrevert->itemtype = 'link';
$userrevert->url = new moodle_url('/course/loginas.php', array(
'id' => $course->id,
'sesskey' => sesskey()
Expand All @@ -859,6 +874,7 @@ function user_get_user_navigation_info($user, $page) {
if ($buildlogout) {
// Build a logout link.
$logout = new stdClass();
$logout->itemtype = 'link';
$logout->url = new moodle_url('/login/logout.php', array('sesskey' => sesskey()));
$logout->pix = "a/logout";
$logout->title = get_string('logout');
Expand All @@ -878,4 +894,4 @@ function user_get_user_navigation_info($user, $page) {
}

return $returnobject;
}
}

0 comments on commit 2dfdb12

Please sign in to comment.