Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/blocks/navigation/block_navigation.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00037 class block_navigation extends block_base {
00038 
00040     public static $navcount;
00042     public $blockname = null;
00044     protected $contentgenerated = false;
00046     protected $docked = null;
00047 
00049     const TRIM_RIGHT = 1;
00051     const TRIM_LEFT = 2;
00053     const TRIM_CENTER = 3;
00054 
00058     function init() {
00059         global $CFG;
00060         $this->blockname = get_class($this);
00061         $this->title = get_string('pluginname', $this->blockname);
00062     }
00063 
00068     function instance_allow_multiple() {
00069         return false;
00070     }
00071 
00076     function applicable_formats() {
00077         return array('all' => true);
00078     }
00079 
00084     function instance_allow_config() {
00085         return true;
00086     }
00087 
00094     function  instance_can_be_hidden() {
00095         return false;
00096     }
00097 
00098     function instance_can_be_docked() {
00099         return (parent::instance_can_be_docked() && (empty($this->config->enabledock) || $this->config->enabledock=='yes'));
00100     }
00101 
00102     function get_required_javascript() {
00103         global $CFG;
00104         user_preference_allow_ajax_update('docked_block_instance_'.$this->instance->id, PARAM_INT);
00105         $this->page->requires->js_module('core_dock');
00106         $limit = 20;
00107         if (!empty($CFG->navcourselimit)) {
00108             $limit = $CFG->navcourselimit;
00109         }
00110         $expansionlimit = 0;
00111         if (!empty($this->config->expansionlimit)) {
00112             $expansionlimit = $this->config->expansionlimit;
00113         }
00114         $arguments = array(
00115             'id'             => $this->instance->id,
00116             'instance'       => $this->instance->id,
00117             'candock'        => $this->instance_can_be_docked(),
00118             'courselimit'    => $limit,
00119             'expansionlimit' => $expansionlimit
00120         );
00121         $this->page->requires->string_for_js('viewallcourses', 'moodle');
00122         $this->page->requires->yui_module(array('core_dock', 'moodle-block_navigation-navigation'), 'M.block_navigation.init_add_tree', array($arguments));
00123     }
00124 
00128     function get_content() {
00129         global $CFG, $OUTPUT;
00130         // First check if we have already generated, don't waste cycles
00131         if ($this->contentgenerated === true) {
00132             return $this->content;
00133         }
00134         // JS for navigation moved to the standard theme, the code will probably have to depend on the actual page structure
00135         // $this->page->requires->js('/lib/javascript-navigation.js');
00136         // Navcount is used to allow us to have multiple trees although I dont' know why
00137         // you would want to trees the same
00138 
00139         block_navigation::$navcount++;
00140 
00141         // Check if this block has been docked
00142         if ($this->docked === null) {
00143             $this->docked = get_user_preferences('nav_in_tab_panel_globalnav'.block_navigation::$navcount, 0);
00144         }
00145 
00146         // Check if there is a param to change the docked state
00147         if ($this->docked && optional_param('undock', null, PARAM_INT)==$this->instance->id) {
00148             unset_user_preference('nav_in_tab_panel_globalnav'.block_navigation::$navcount);
00149             $url = $this->page->url;
00150             $url->remove_params(array('undock'));
00151             redirect($url);
00152         } else if (!$this->docked && optional_param('dock', null, PARAM_INT)==$this->instance->id) {
00153             set_user_preferences(array('nav_in_tab_panel_globalnav'.block_navigation::$navcount=>1));
00154             $url = $this->page->url;
00155             $url->remove_params(array('dock'));
00156             redirect($url);
00157         }
00158 
00159         $trimmode = self::TRIM_LEFT;
00160         $trimlength = 50;
00161 
00162         if (!empty($this->config->trimmode)) {
00163             $trimmode = (int)$this->config->trimmode;
00164         }
00165 
00166         if (!empty($this->config->trimlength)) {
00167             $trimlength = (int)$this->config->trimlength;
00168         }
00169 
00170         // Initialise (only actually happens if it hasn't already been done yet
00171         $this->page->navigation->initialise();
00172         $navigation = clone($this->page->navigation);
00173         $expansionlimit = null;
00174         if (!empty($this->config->expansionlimit)) {
00175             $expansionlimit = $this->config->expansionlimit;
00176             $navigation->set_expansion_limit($this->config->expansionlimit);
00177         }
00178         $this->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2));
00179 
00180         // Get the expandable items so we can pass them to JS
00181         $expandable = array();
00182         $navigation->find_expandable($expandable);
00183         if ($expansionlimit) {
00184             foreach ($expandable as $key=>$node) {
00185                 if ($node['type'] > $expansionlimit && !($expansionlimit == navigation_node::TYPE_COURSE && $node['type'] == $expansionlimit && $node['branchid'] == SITEID)) {
00186                     unset($expandable[$key]);
00187                 }
00188             }
00189         }
00190 
00191         $this->page->requires->data_for_js('navtreeexpansions'.$this->instance->id, $expandable);
00192 
00193         $options = array();
00194         $options['linkcategories'] = (!empty($this->config->linkcategories) && $this->config->linkcategories == 'yes');
00195         
00196         // Grab the items to display
00197         $renderer = $this->page->get_renderer('block_navigation');
00198         $this->content = new stdClass();
00199         $this->content->text = $renderer->navigation_tree($navigation, $expansionlimit, $options);
00200 
00201         // Set content generated to true so that we know it has been done
00202         $this->contentgenerated = true;
00203 
00204         return $this->content;
00205     }
00206 
00218     public function html_attributes() {
00219         $attributes = parent::html_attributes();
00220         if (!empty($this->config->enablehoverexpansion) && $this->config->enablehoverexpansion == 'yes') {
00221             $attributes['class'] .= ' block_js_expansion';
00222         }
00223         return $attributes;
00224     }
00225 
00236     public function trim(navigation_node $node, $mode=1, $long=50, $short=25, $recurse=true, $textlib=null) {
00237         if ($textlib == null) {
00238             $textlib = textlib_get_instance();
00239         }
00240         switch ($mode) {
00241             case self::TRIM_RIGHT :
00242                 if ($textlib->strlen($node->text)>($long+3)) {
00243                     // Truncate the text to $long characters
00244                     $node->text = $this->trim_right($textlib, $node->text, $long);
00245                 }
00246                 if (is_string($node->shorttext) && $textlib->strlen($node->shorttext)>($short+3)) {
00247                     // Truncate the shorttext
00248                     $node->shorttext = $this->trim_right($textlib, $node->shorttext, $short);
00249                 }
00250                 break;
00251             case self::TRIM_LEFT :
00252                 if ($textlib->strlen($node->text)>($long+3)) {
00253                     // Truncate the text to $long characters
00254                     $node->text = $this->trim_left($textlib, $node->text, $long);
00255                 }
00256                 if (is_string($node->shorttext) && $textlib->strlen($node->shorttext)>($short+3)) {
00257                     // Truncate the shorttext
00258                     $node->shorttext = $this->trim_left($textlib, $node->shorttext, $short);
00259                 }
00260                 break;
00261             case self::TRIM_CENTER :
00262                 if ($textlib->strlen($node->text)>($long+3)) {
00263                     // Truncate the text to $long characters
00264                     $node->text = $this->trim_center($textlib, $node->text, $long);
00265                 }
00266                 if (is_string($node->shorttext) && $textlib->strlen($node->shorttext)>($short+3)) {
00267                     // Truncate the shorttext
00268                     $node->shorttext = $this->trim_center($textlib, $node->shorttext, $short);
00269                 }
00270                 break;
00271         }
00272         if ($recurse && $node->children->count()) {
00273             foreach ($node->children as &$child) {
00274                 $this->trim($child, $mode, $long, $short, true, $textlib);
00275             }
00276         }
00277     }
00285     protected function trim_left($textlib, $string, $length) {
00286         return '...'.$textlib->substr($string, $textlib->strlen($string)-$length, $length);
00287     }
00295     protected function trim_right($textlib, $string, $length) {
00296         return $textlib->substr($string, 0, $length).'...';
00297     }
00305     protected function trim_center($textlib, $string, $length) {
00306         $trimlength = ceil($length/2);
00307         $start = $textlib->substr($string, 0, $trimlength);
00308         $end = $textlib->substr($string, $textlib->strlen($string)-$trimlength);
00309         $string = $start.'...'.$end;
00310         return $string;
00311     }
00312 }
 All Data Structures Namespaces Files Functions Variables Enumerations