1 |
efrain |
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 Moodle 2.0
|
|
|
23 |
* @package core
|
|
|
24 |
* @copyright 2009 Sam Hemelryk
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
define('AJAX_SCRIPT', true);
|
|
|
29 |
define('READ_ONLY_SESSION', true);
|
|
|
30 |
|
|
|
31 |
/** Include config */
|
|
|
32 |
require_once(__DIR__ . '/../../config.php');
|
|
|
33 |
/** Include course lib for its functions */
|
|
|
34 |
require_once($CFG->dirroot.'/course/lib.php');
|
|
|
35 |
|
|
|
36 |
if (!empty($CFG->forcelogin)) {
|
|
|
37 |
require_login();
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
try {
|
|
|
41 |
// Start buffer capture so that we can `remove` any errors
|
|
|
42 |
ob_start();
|
|
|
43 |
// Require id This is the key for whatever branch we want to get.
|
|
|
44 |
// This accepts alphanum because the courses and my courses branches don't have numerical keys.
|
|
|
45 |
// For those branches we return the alphanum key, courses and mycourses.
|
|
|
46 |
$branchid = required_param('id', PARAM_ALPHANUM);
|
|
|
47 |
// This identifies the type of the branch we want to get
|
|
|
48 |
$branchtype = required_param('type', PARAM_INT);
|
|
|
49 |
// This identifies the block instance requesting AJAX extension
|
|
|
50 |
$instanceid = optional_param('instance', null, PARAM_INT);
|
|
|
51 |
|
|
|
52 |
$PAGE->set_context(context_system::instance());
|
|
|
53 |
|
|
|
54 |
// Create a global nav object
|
|
|
55 |
$navigation = new global_navigation_for_ajax($PAGE, $branchtype, $branchid);
|
|
|
56 |
|
|
|
57 |
$linkcategories = false;
|
|
|
58 |
|
|
|
59 |
if ($instanceid!==null) {
|
|
|
60 |
// Get the db record for the block instance
|
|
|
61 |
$blockrecord = $DB->get_record('block_instances', array('id'=>$instanceid,'blockname'=>'navigation'));
|
|
|
62 |
if ($blockrecord!=false) {
|
|
|
63 |
|
|
|
64 |
// Instantiate a block_instance object so we can access config
|
|
|
65 |
$block = block_instance('navigation', $blockrecord);
|
|
|
66 |
|
|
|
67 |
$trimmode = block_navigation::TRIM_RIGHT;
|
|
|
68 |
$trimlength = 50;
|
|
|
69 |
|
|
|
70 |
// Set the trim mode
|
|
|
71 |
if (!empty($block->config->trimmode)) {
|
|
|
72 |
$trimmode = (int)$block->config->trimmode;
|
|
|
73 |
}
|
|
|
74 |
// Set the trim length
|
|
|
75 |
if (!empty($block->config->trimlength)) {
|
|
|
76 |
$trimlength = (int)$block->config->trimlength;
|
|
|
77 |
}
|
|
|
78 |
if (!empty($block->config->linkcategories) && $block->config->linkcategories == 'yes') {
|
|
|
79 |
$linkcategories = true;
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
// Create a navigation object to use, we can't guarantee PAGE will be complete
|
|
|
85 |
if (!isloggedin()) {
|
|
|
86 |
$navigation->set_expansion_limit(navigation_node::TYPE_COURSE);
|
|
|
87 |
} else {
|
|
|
88 |
if (isset($block) && !empty($block->config->expansionlimit)) {
|
|
|
89 |
$navigation->set_expansion_limit($block->config->expansionlimit);
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
if (isset($block)) {
|
|
|
93 |
$block->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2));
|
|
|
94 |
}
|
|
|
95 |
$converter = new navigation_json();
|
|
|
96 |
|
|
|
97 |
// Find the actual branch we are looking for
|
|
|
98 |
if ($branchtype != 0) {
|
|
|
99 |
$branch = $navigation->find($branchid, $branchtype);
|
|
|
100 |
} else if ($branchid === 'mycourses' || $branchid === 'courses') {
|
|
|
101 |
$branch = $navigation->find($branchid, navigation_node::TYPE_ROOTNODE);
|
|
|
102 |
} else {
|
|
|
103 |
throw new coding_exception('Invalid branch type/id passed to AJAX call to load branches.');
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
// Remove links to categories if required.
|
|
|
107 |
if (!$linkcategories) {
|
|
|
108 |
foreach ($branch->find_all_of_type(navigation_node::TYPE_CATEGORY) as $category) {
|
|
|
109 |
$category->action = null;
|
|
|
110 |
}
|
|
|
111 |
foreach ($branch->find_all_of_type(navigation_node::TYPE_MY_CATEGORY) as $category) {
|
|
|
112 |
$category->action = null;
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
// Stop buffering errors at this point
|
|
|
117 |
$html = ob_get_contents();
|
|
|
118 |
ob_end_clean();
|
|
|
119 |
} catch (Exception $e) {
|
|
|
120 |
throw new coding_exception('Error: '.$e->getMessage());
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
// Check if the buffer contianed anything if it did ERROR!
|
|
|
124 |
if (trim($html) !== '') {
|
|
|
125 |
throw new coding_exception('Errors were encountered while producing the navigation branch'."\n\n\n".$html);
|
|
|
126 |
}
|
|
|
127 |
// Check that branch isn't empty... if it is ERROR!
|
|
|
128 |
if (empty($branch) || ($branch->nodetype !== navigation_node::NODETYPE_BRANCH && !$branch->isexpandable)) {
|
|
|
129 |
throw new coding_exception('No further information available for this branch');
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
// Prepare an XML converter for the branch
|
|
|
133 |
$converter->set_expandable($navigation->get_expandable());
|
|
|
134 |
// Set XML headers
|
|
|
135 |
header('Content-type: text/plain; charset=utf-8');
|
|
|
136 |
// Convert and output the branch as XML
|
|
|
137 |
echo $converter->convert($branch);
|