|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00028 define('AJAX_SCRIPT', true); 00029 00031 require_once(dirname(__FILE__) . '/../../config.php'); 00033 require_once($CFG->dirroot.'/course/lib.php'); 00034 00035 try { 00036 // Start buffer capture so that we can `remove` any errors 00037 ob_start(); 00038 // Require id This is the key for whatever branch we want to get 00039 $branchid = required_param('id', PARAM_INT); 00040 // This identifies the type of the branch we want to get 00041 $branchtype = required_param('type', PARAM_INT); 00042 // This identifies the block instance requesting AJAX extension 00043 $instanceid = optional_param('instance', null, PARAM_INT); 00044 00045 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); 00046 00047 // Create a global nav object 00048 $navigation = new global_navigation_for_ajax($PAGE, $branchtype, $branchid); 00049 00050 $linkcategories = false; 00051 00052 if ($instanceid!==null) { 00053 // Get the db record for the block instance 00054 $blockrecord = $DB->get_record('block_instances', array('id'=>$instanceid,'blockname'=>'navigation')); 00055 if ($blockrecord!=false) { 00056 00057 // Instantiate a block_instance object so we can access config 00058 $block = block_instance('navigation', $blockrecord); 00059 00060 $trimmode = block_navigation::TRIM_RIGHT; 00061 $trimlength = 50; 00062 00063 // Set the trim mode 00064 if (!empty($block->config->trimmode)) { 00065 $trimmode = (int)$block->config->trimmode; 00066 } 00067 // Set the trim length 00068 if (!empty($block->config->trimlength)) { 00069 $trimlength = (int)$block->config->trimlength; 00070 } 00071 if (!empty($block->config->linkcategories) && $block->config->linkcategories == 'yes') { 00072 $linkcategories = true; 00073 } 00074 } 00075 } 00076 00077 // Create a navigation object to use, we can't guarantee PAGE will be complete 00078 if (!isloggedin()) { 00079 $navigation->set_expansion_limit(navigation_node::TYPE_COURSE); 00080 } else { 00081 if (isset($block) && !empty($block->config->expansionlimit)) { 00082 $navigation->set_expansion_limit($block->config->expansionlimit); 00083 } 00084 } 00085 if (isset($block)) { 00086 $block->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2)); 00087 } 00088 $converter = new navigation_json(); 00089 00090 // Find the actuall branch we are looking for 00091 $branch = $navigation->find($branchid, $branchtype); 00092 00093 // Remove links to categories if required. 00094 if (!$linkcategories) { 00095 foreach ($branch->find_all_of_type(navigation_node::TYPE_CATEGORY) as $category) { 00096 $category->action = null; 00097 } 00098 } 00099 00100 // Stop buffering errors at this point 00101 $html = ob_get_contents(); 00102 ob_end_clean(); 00103 } catch (Exception $e) { 00104 die('Error: '.$e->getMessage()); 00105 } 00106 00107 // Check if the buffer contianed anything if it did ERROR! 00108 if (trim($html) !== '') { 00109 die('Errors were encountered while producing the navigation branch'."\n\n\n".$html); 00110 } 00111 // Check that branch isn't empty... if it is ERROR! 00112 if (empty($branch) || $branch->nodetype !== navigation_node::NODETYPE_BRANCH) { 00113 die('No further information available for this branch'); 00114 } 00115 00116 // Prepare an XML converter for the branch 00117 $converter->set_expandable($navigation->get_expandable()); 00118 // Set XML headers 00119 header('Content-type: text/plain; charset=utf-8'); 00120 // Convert and output the branch as XML 00121 echo $converter->convert($branch);