Merge branch 'MDL-48542-master' of git://github.com/jethac/moodle

This commit is contained in:
Andrew Nicols 2015-01-28 12:02:28 +08:00
commit 0977d3d404
4 changed files with 127 additions and 26 deletions

View File

@ -177,7 +177,7 @@ Moodle community|https://moodle.org
#####
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.';

View File

@ -3067,28 +3067,44 @@ EOD;
$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++;

View File

@ -0,0 +1,69 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tests user menu functionality.
*
* @package core
* @copyright 2015 Jetha Chan <jetha@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_user_menu_testcase extends advanced_testcase {
public function test_custom_user_menu() {
global $CFG, $OUTPUT, $USER, $PAGE;
$this->resetAfterTest(true);
$this->expectOutputString('');
// Test using an admin user at the root of Moodle; this way we
// don't have to create a test user with avatar.
$this->setAdminUser();
$PAGE->set_url('/');
// Build a test string for the custom user menu items setting.
$usermenuitems = 'messages,message|/message/index.php|message
myfiles,moodle|/user/files.php|download
###
mybadges,badges|/badges/mybadges.php|award
-|-|-
test
-
#####
#f234|2';
set_config('customusermenuitems', $usermenuitems);
// Fail the test and dump output if an exception is thrown
// during user menu creation.
$valid = true;
try {
$usermenu = $OUTPUT->user_menu($USER);
} catch (moodle_exception $me) {
$valid = false;
printf(
"[%s] %s: %s\n%s\n",
$me->module,
$me->errorcode,
$me->message,
$me->debuginfo
);
}
$this->assertTrue($valid);
}
}

View File

@ -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
@ -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 {
@ -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);
@ -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]));
@ -788,6 +799,7 @@ 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";
@ -795,6 +807,7 @@ function user_get_user_navigation_info($user, $page) {
// 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";
@ -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(),
@ -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()
@ -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');