3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This file is used to deliver a branch from the navigation structure
20 * in XML format back to a page from an AJAX call
24 * @copyright 2009 Sam Hemelryk
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 define('AJAX_SCRIPT', true);
31 require_once(dirname(__FILE__) . '/../../config.php');
32 /** Include course lib for its functions */
33 require_once($CFG->dirroot.'/course/lib.php');
36 // Start buffer capture so that we can `remove` any errors
38 // Require id This is the key for whatever branch we want to get.
39 // This accepts alphanum because the courses and my courses branches don't have numerical keys.
40 // For those branches we return the alphanum key, courses and mycourses.
41 $branchid = required_param('id', PARAM_ALPHANUM);
42 // This identifies the type of the branch we want to get
43 $branchtype = required_param('type', PARAM_INT);
44 // This identifies the block instance requesting AJAX extension
45 $instanceid = optional_param('instance', null, PARAM_INT);
47 $PAGE->set_context(context_system::instance());
49 // Create a global nav object
50 $navigation = new global_navigation_for_ajax($PAGE, $branchtype, $branchid);
52 $linkcategories = false;
54 if ($instanceid!==null) {
55 // Get the db record for the block instance
56 $blockrecord = $DB->get_record('block_instances', array('id'=>$instanceid,'blockname'=>'navigation'));
57 if ($blockrecord!=false) {
59 // Instantiate a block_instance object so we can access config
60 $block = block_instance('navigation', $blockrecord);
62 $trimmode = block_navigation::TRIM_RIGHT;
66 if (!empty($block->config->trimmode)) {
67 $trimmode = (int)$block->config->trimmode;
69 // Set the trim length
70 if (!empty($block->config->trimlength)) {
71 $trimlength = (int)$block->config->trimlength;
73 if (!empty($block->config->linkcategories) && $block->config->linkcategories == 'yes') {
74 $linkcategories = true;
79 // Create a navigation object to use, we can't guarantee PAGE will be complete
81 $navigation->set_expansion_limit(navigation_node::TYPE_COURSE);
83 if (isset($block) && !empty($block->config->expansionlimit)) {
84 $navigation->set_expansion_limit($block->config->expansionlimit);
88 $block->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2));
90 $converter = new navigation_json();
92 // Find the actual branch we are looking for
93 if ($branchtype != 0) {
94 $branch = $navigation->find($branchid, $branchtype);
95 } else if ($branchid === 'mycourses' || $branchid === 'courses') {
96 $branch = $navigation->find($branchid, navigation_node::TYPE_ROOTNODE);
98 throw new coding_exception('Invalid branch type/id passed to AJAX call to load branches.');
101 // Remove links to categories if required.
102 if (!$linkcategories) {
103 foreach ($branch->find_all_of_type(navigation_node::TYPE_CATEGORY) as $category) {
104 $category->action = null;
106 foreach ($branch->find_all_of_type(navigation_node::TYPE_MY_CATEGORY) as $category) {
107 $category->action = null;
111 // Stop buffering errors at this point
112 $html = ob_get_contents();
114 } catch (Exception $e) {
115 die('Error: '.$e->getMessage());
118 // Check if the buffer contianed anything if it did ERROR!
119 if (trim($html) !== '') {
120 die('Errors were encountered while producing the navigation branch'."\n\n\n".$html);
122 // Check that branch isn't empty... if it is ERROR!
123 if (empty($branch) || $branch->nodetype !== navigation_node::NODETYPE_BRANCH) {
124 die('No further information available for this branch');
127 // Prepare an XML converter for the branch
128 $converter->set_expandable($navigation->get_expandable());
130 header('Content-type: text/plain; charset=utf-8');
131 // Convert and output the branch as XML
132 echo $converter->convert($branch);