MDL-25973 add missing charset
[moodle.git] / lib / ajax / getnavbranch.php
CommitLineData
7d2a0492 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
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
21 *
22 * @since 2.0
23 * @package moodlecore
24 * @copyright 2009 Sam Hemelryk
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
a70eb30f
SH
28define('AJAX_SCRIPT', true);
29
7d2a0492 30/** Include config */
31require_once(dirname(__FILE__) . '/../../config.php');
32/** Include course lib for its functions */
33require_once($CFG->dirroot.'/course/lib.php');
7d2a0492 34
507a7a9a
SH
35try {
36 // Start buffer capture so that we can `remove` any errors
37 ob_start();
38 // Require id This is the key for whatever branch we want to get
39 $branchid = required_param('id', PARAM_INT);
40 // This identifies the type of the branch we want to get
41 $branchtype = required_param('type', PARAM_INT);
42 // This identifies the block instance requesting AJAX extension
43 $instanceid = optional_param('instance', null, PARAM_INT);
7d2a0492 44
4766a50c
SH
45 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
46
507a7a9a 47 // Create a global nav object
246a9b05 48 $navigation = new global_navigation_for_ajax($PAGE, $branchtype, $branchid);
507a7a9a 49
480f906e 50 $linkcategories = false;
8a7703ce 51
507a7a9a
SH
52 if ($instanceid!==null) {
53 // Get the db record for the block instance
88f77c3c
SH
54 $blockrecord = $DB->get_record('block_instances', array('id'=>$instanceid,'blockname'=>'navigation'));
55 if ($blockrecord!=false) {
a9535f79
SH
56
57 // Instantiate a block_instance object so we can access config
88f77c3c 58 $block = block_instance('navigation', $blockrecord);
a9535f79
SH
59
60 $trimmode = block_navigation::TRIM_RIGHT;
61 $trimlength = 50;
62
63 // Set the trim mode
64 if (!empty($block->config->trimmode)) {
65 $trimmode = (int)$block->config->trimmode;
66 }
67 // Set the trim length
68 if (!empty($block->config->trimlength)) {
69 $trimlength = (int)$block->config->trimlength;
507a7a9a 70 }
480f906e
SH
71 if (!empty($block->config->linkcategories) && $block->config->linkcategories == 'yes') {
72 $linkcategories = true;
73 }
7d2a0492 74 }
75 }
7d2a0492 76
507a7a9a 77 // Create a navigation object to use, we can't guarantee PAGE will be complete
ca787a92 78 if (!isloggedin()) {
4766a50c
SH
79 $navigation->set_expansion_limit(navigation_node::TYPE_COURSE);
80 } else {
81 if (isset($block) && !empty($block->config->expansionlimit)) {
82 $navigation->set_expansion_limit($block->config->expansionlimit);
83 }
88f77c3c 84 }
a9535f79
SH
85 if (isset($block)) {
86 $block->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2));
87 }
507a7a9a 88 $converter = new navigation_json();
8a7703ce 89
507a7a9a 90 // Find the actuall branch we are looking for
3406acde 91 $branch = $navigation->find($branchid, $branchtype);
8a7703ce 92
480f906e
SH
93 // Remove links to categories if required.
94 if (!$linkcategories) {
95 foreach ($branch->find_all_of_type(navigation_node::TYPE_CATEGORY) as $category) {
96 $category->action = null;
97 }
98 }
8a7703ce 99
507a7a9a
SH
100 // Stop buffering errors at this point
101 $html = ob_get_contents();
102 ob_end_clean();
103} catch (Exception $e) {
4d84db27 104 die('Error: '.$e->getMessage());
507a7a9a 105}
7d2a0492 106
107// Check if the buffer contianed anything if it did ERROR!
507a7a9a 108if (trim($html) !== '') {
7d2a0492 109 die('Errors were encountered while producing the navigation branch'."\n\n\n".$html);
110}
111// Check that branch isn't empty... if it is ERROR!
112if (empty($branch) || $branch->nodetype !== navigation_node::NODETYPE_BRANCH) {
113 die('No further information available for this branch');
114}
115
116// Prepare an XML converter for the branch
246a9b05 117$converter->set_expandable($navigation->get_expandable());
7d2a0492 118// Set XML headers
8a7703ce 119header('Content-type: text/plain; charset=utf-8');
7d2a0492 120// Convert and output the branch as XML
117bd748 121echo $converter->convert($branch);