mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
navigation MDL-21350 Implemented trim options for the navigation block, can now set trim mode (right/left/center) and trim length
This commit is contained in:
parent
f77f615de6
commit
a9535f7945
@ -45,6 +45,13 @@ class block_navigation extends block_base {
|
||||
/** @var bool|null */
|
||||
protected $docked = null;
|
||||
|
||||
/** @var int Trim characters from the right */
|
||||
const TRIM_RIGHT = 1;
|
||||
/** @var int Trim characters from the left */
|
||||
const TRIM_LEFT = 2;
|
||||
/** @var int Trim characters from the center */
|
||||
const TRIM_CENTER = 3;
|
||||
|
||||
/**
|
||||
* Set the initial properties for the block
|
||||
*/
|
||||
@ -125,8 +132,21 @@ class block_navigation extends block_base {
|
||||
redirect($url);
|
||||
}
|
||||
|
||||
$trimmode = self::TRIM_LEFT;
|
||||
$trimlength = 50;
|
||||
|
||||
if (!empty($this->config->trimmode)) {
|
||||
$trimmode = (int)$this->config->trimmode;
|
||||
}
|
||||
|
||||
if (!empty($this->config->trimlength)) {
|
||||
$trimlength = (int)$this->config->trimlength;
|
||||
}
|
||||
|
||||
// Initialise (only actually happens if it hasn't already been done yet
|
||||
$this->page->navigation->initialise();
|
||||
$navigation = clone($this->page->navigation);
|
||||
$this->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2));
|
||||
|
||||
if (!empty($this->config->showmyhistory) && $this->config->showmyhistory=='yes') {
|
||||
$this->showmyhistory();
|
||||
@ -277,7 +297,7 @@ class block_navigation extends block_base {
|
||||
// If we have `more than nothing` in the history display it :D
|
||||
if ($historycount > 0) {
|
||||
// Add a branch to hold the users history
|
||||
$mymoodle = $PAGE->navigation->get('profile', navigation_node::TYPE_USER);
|
||||
$mymoodle = $PAGE->navigation->get('myprofile', navigation_node::TYPE_USER);
|
||||
$myhistorybranch = $mymoodle->add(get_string('showmyhistorytitle', $this->blockname), null, navigation_node::TYPE_CUSTOM, null, 'myhistory');
|
||||
foreach (array_reverse($history) as $node) {
|
||||
$myhistorybranch->children->add($node);
|
||||
@ -289,4 +309,91 @@ class block_navigation extends block_base {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the text and shorttext properties of this node and optionally
|
||||
* all of its children.
|
||||
*
|
||||
* @param int $mode One of navigation_node::TRIM_*
|
||||
* @param int $long The length to trim text to
|
||||
* @param int $short The length to trim shorttext to
|
||||
* @param bool $recurse Recurse all children
|
||||
* @param textlib|null $textlib
|
||||
*/
|
||||
public function trim(navigation_node $node, $mode=1, $long=50, $short=25, $recurse=true, $textlib=null) {
|
||||
if ($textlib == null) {
|
||||
$textlib = textlib_get_instance();
|
||||
}
|
||||
switch ($mode) {
|
||||
case self::TRIM_RIGHT :
|
||||
if ($textlib->strlen($node->text)>($long+3)) {
|
||||
// Truncate the text to $long characters
|
||||
$node->text = $this->trim_right($textlib, $node->text, $long);
|
||||
}
|
||||
if (is_string($node->shorttext) && $textlib->strlen($node->shorttext)>($short+3)) {
|
||||
// Truncate the shorttext
|
||||
$node->shorttext = $this->trim_right($textlib, $node->shorttext, $short);
|
||||
}
|
||||
break;
|
||||
case self::TRIM_LEFT :
|
||||
if ($textlib->strlen($node->text)>($long+3)) {
|
||||
// Truncate the text to $long characters
|
||||
$node->text = $this->trim_left($textlib, $node->text, $long);
|
||||
}
|
||||
if (is_string($node->shorttext) && strlen($node->shorttext)>($short+3)) {
|
||||
// Truncate the shorttext
|
||||
$node->shorttext = $this->trim_left($textlib, $node->shorttext, $short);
|
||||
}
|
||||
break;
|
||||
case self::TRIM_CENTER :
|
||||
if ($textlib->strlen($node->text)>($long+3)) {
|
||||
// Truncate the text to $long characters
|
||||
$node->text = $this->trim_center($textlib, $node->text, $long);
|
||||
}
|
||||
if (is_string($node->shorttext) && strlen($node->shorttext)>($short+3)) {
|
||||
// Truncate the shorttext
|
||||
$node->shorttext = $this->trim_center($textlib, $node->shorttext, $short);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if ($recurse && $node->children->count()) {
|
||||
foreach ($node->children as &$child) {
|
||||
$this->trim($child, $mode, $long, $short, true, $textlib);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Truncate a string from the left
|
||||
* @param textlib $textlib
|
||||
* @param string $string The string to truncate
|
||||
* @param int $length The length to truncate to
|
||||
* @return string The truncated string
|
||||
*/
|
||||
protected function trim_left($textlib, $string, $length) {
|
||||
return '...'.$textlib->substr($string, $textlib->strlen($string)-$length);
|
||||
}
|
||||
/**
|
||||
* Truncate a string from the right
|
||||
* @param textlib $textlib
|
||||
* @param string $string The string to truncate
|
||||
* @param int $length The length to truncate to
|
||||
* @return string The truncated string
|
||||
*/
|
||||
protected function trim_right($textlib, $string, $length) {
|
||||
return $textlib->substr($string, 0, $length).'...';
|
||||
}
|
||||
/**
|
||||
* Truncate a string in the center
|
||||
* @param textlib $textlib
|
||||
* @param string $string The string to truncate
|
||||
* @param int $length The length to truncate to
|
||||
* @return string The truncated string
|
||||
*/
|
||||
protected function trim_center($textlib, $string, $length) {
|
||||
$trimlength = ceil($length/2);
|
||||
$start = $textlib->substr($string, 0, $trimlength);
|
||||
$end = $textlib->substr($string, $textlib->strlen($string)-$trimlength);
|
||||
$string = $start.'...'.$end;
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
|
@ -32,6 +32,9 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class block_navigation_edit_form extends block_edit_form {
|
||||
/**
|
||||
* @param MoodleQuickForm $mform
|
||||
*/
|
||||
protected function specific_definition($mform) {
|
||||
global $CFG;
|
||||
$mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
|
||||
@ -40,15 +43,19 @@ class block_navigation_edit_form extends block_edit_form {
|
||||
$yesnooptions = array('yes'=>get_string('yes'), 'no'=>get_string('no'));
|
||||
foreach ($mods as $modname=>$default) {
|
||||
$mform->addElement('select', 'config_'.$modname, get_string($modname.'desc', $this->block->blockname), $yesnooptions);
|
||||
if (isset($this->block->config->{$modname}) && $this->block->config->{$modname}!=$default) {
|
||||
if ($default=='no') {
|
||||
$mform->getElement('config_'.$modname)->setSelected('yes');
|
||||
} else {
|
||||
$mform->getElement('config_'.$modname)->setSelected('no');
|
||||
}
|
||||
} else {
|
||||
$mform->getElement('config_'.$modname)->setSelected($default);
|
||||
}
|
||||
$mform->setDefault('config_'.$modname, $default);
|
||||
}
|
||||
|
||||
$options = array(
|
||||
block_navigation::TRIM_RIGHT => get_string('trimmoderight', $this->block->blockname),
|
||||
block_navigation::TRIM_LEFT => get_string('trimmodeleft', $this->block->blockname),
|
||||
block_navigation::TRIM_CENTER => get_string('trimmodecenter', $this->block->blockname)
|
||||
);
|
||||
$mform->addElement('select', 'config_trimmode', get_string('trimmode', $this->block->blockname), $options);
|
||||
$mform->setType('config_trimmode', PARAM_INT);
|
||||
|
||||
$mform->addElement('text', 'config_trimlength', get_string('trimlength', $this->block->blockname));
|
||||
$mform->setDefault('config_trimlength', 50);
|
||||
$mform->setType('config_trimlength', PARAM_INT);
|
||||
}
|
||||
}
|
@ -33,3 +33,8 @@ $string['enabledockdesc'] = 'Allow the user to dock this block';
|
||||
$string['pluginname'] = 'Navigation';
|
||||
$string['showmyhistorydesc'] = 'Show my history as a branch in the navigation';
|
||||
$string['showmyhistorytitle'] = 'My history';
|
||||
$string['trimmode'] = 'Trim mode';
|
||||
$string['trimmoderight'] = 'Trim characters from the right';
|
||||
$string['trimmodeleft'] = 'Trim characters from the left';
|
||||
$string['trimmodecenter'] = 'Trim characters from the center';
|
||||
$string['trimlength'] = 'How many characters to trim to';
|
||||
|
@ -49,17 +49,30 @@ try {
|
||||
// Get the db record for the block instance
|
||||
$blockrecords = $DB->get_record('block_instances', array('id'=>$instanceid,'blockname'=>'navigation'));
|
||||
if ($blockrecords!=false) {
|
||||
// Instantiate a block_instance object so we can access congif
|
||||
|
||||
// Instantiate a block_instance object so we can access config
|
||||
$block = block_instance('navigation', $blockrecords);
|
||||
// Check if the expansion limit config option has been set and isn't the default [everything]
|
||||
if (empty($block->config->showemptybranches) || $block->config->showemptybranches=='no') {
|
||||
$navigation->showemptybranches = false;
|
||||
|
||||
$trimmode = block_navigation::TRIM_RIGHT;
|
||||
$trimlength = 50;
|
||||
|
||||
// Set the trim mode
|
||||
if (!empty($block->config->trimmode)) {
|
||||
$trimmode = (int)$block->config->trimmode;
|
||||
}
|
||||
// Set the trim length
|
||||
if (!empty($block->config->trimlength)) {
|
||||
$trimlength = (int)$block->config->trimlength;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create a navigation object to use, we can't guarantee PAGE will be complete
|
||||
|
||||
$expandable = $navigation->initialise($branchtype, $branchid);
|
||||
if (isset($block)) {
|
||||
$block->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2));
|
||||
}
|
||||
$converter = new navigation_json();
|
||||
|
||||
// Find the actuall branch we are looking for
|
||||
|
@ -190,15 +190,6 @@ class navigation_node implements renderable {
|
||||
}
|
||||
// Default the title to the text
|
||||
$this->title = $this->text;
|
||||
$textlib = textlib_get_instance();
|
||||
if (strlen($this->text)>50) {
|
||||
// Truncate the text to 50 characters
|
||||
$this->text = $textlib->substr($this->text, 0, 50).'...';
|
||||
}
|
||||
if (is_string($this->shorttext) && strlen($this->shorttext)>25) {
|
||||
// Truncate the shorttext
|
||||
$this->shorttext = $textlib->substr($this->shorttext, 0, 25).'...';
|
||||
}
|
||||
// Instantiate a new navigation node collection for this nodes children
|
||||
$this->children = new navigation_node_collection();
|
||||
}
|
||||
|
@ -1149,7 +1149,7 @@ class theme_config {
|
||||
$regions = array();
|
||||
foreach ($this->layouts as $layoutinfo) {
|
||||
foreach ($layoutinfo['regions'] as $region) {
|
||||
$regions[$region] = $this->get_region_name($region, $layoutinfo['theme']);
|
||||
$regions[$region] = $this->get_region_name($region, $this->name);
|
||||
}
|
||||
}
|
||||
return $regions;
|
||||
|
Loading…
x
Reference in New Issue
Block a user