MDL-11515:
[moodle-linuxchix.git] / blocks / admin_tree / block_admin_tree.php
blob5bf4bbf16c680c7256168494ebd345a7e42d7966
1 <?php // $Id$
3 class block_admin_tree extends block_base {
5 var $currentdepth;
6 var $spancounter;
7 var $tempcontent;
8 var $pathtosection;
9 var $expandjavascript;
10 var $destination;
12 function init() {
13 global $PAGE;
14 $this->title = get_string('administrationsite');
15 $this->version = 2006090300;
16 $this->currentdepth = 0;
17 $this->spancounter = 1;
18 $this->tempcontent = '';
19 $this->section = (isset($PAGE->section) ? $PAGE->section : '');
20 $this->pathtosection = array();
21 $this->expandjavascript = '';
24 function applicable_formats() {
25 if (block_admin_tree::has_admin_caps()) {
26 return array('site' => true, 'admin' => true, 'my' => true);
27 } else {
28 return array('site' => true);
32 function preferred_width() {
33 return 210;
36 function open_folder($visiblename) {
37 global $CFG;
38 $strfolderopened = s(get_string('folderopened'));
40 for ($i = 0; $i < $this->currentdepth; $i++) {
41 $this->tempcontent .= "&nbsp; &nbsp;";
43 $this->tempcontent .= '<a href="#" onclick="toggle(\'vh_span' . $this->spancounter . '\');return false">';
44 $this->tempcontent .= '<span id="vh_span' . $this->spancounter . 'indicator"><img src="' . $CFG->wwwroot . '/blocks/admin_tree/open.gif" alt="'.$strfolderopened.'" /></span> ';
45 $this->tempcontent .= $visiblename . '</a><br /><span id="vh_span' . $this->spancounter . '">' . "\n";
46 $this->currentdepth++;
47 $this->spancounter++;
50 function close_folder() {
51 $this->currentdepth--;
52 $this->tempcontent .= "</span>\n";
55 function create_item($visiblename,$link,$icon,$class) {
56 global $CFG;
57 for ($i = 0; $i < $this->currentdepth; $i++) {
58 $this->tempcontent .= "&nbsp; &nbsp;";
60 $this->tempcontent .= '<a class="'.$class.'" href="'.$link.'"><img src="'.$icon.'" alt="" />'.
61 $visiblename.'</a><br />'."\n";
64 function build_tree (&$content) {
65 global $CFG;
66 if (is_a($content, 'admin_settingpage')) {
67 if ($content->check_access() and !$content->is_hidden()) {
68 $class = ($content->name == $this->section) ? 'link current' : 'link';
69 $this->create_item($content->visiblename,$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=' . $content->name,$CFG->wwwroot .'/blocks/admin_tree/item.gif', $class);
71 } else if (is_a($content, 'admin_externalpage')) {
72 if ($content->check_access() and !$content->is_hidden()) {
73 $class = ($content->name == $this->section) ? 'link current' : 'link';
74 $this->create_item($content->visiblename, $content->url, $CFG->wwwroot . '/blocks/admin_tree/item.gif', $class);
76 } else if (is_a($content, 'admin_category')) {
77 if ($content->check_access() and !$content->is_hidden()) {
79 // check if the category we're currently printing is a parent category for the current page; if it is, we
80 // make a note (in the javascript) that it has to be expanded after the page has loaded
81 if ($this->pathtosection[count($this->pathtosection) - 1] == $content->name) {
82 $this->expandjavascript .= 'expand("vh_span' . ($this->spancounter) . '");' . "\n";
83 array_pop($this->pathtosection);
86 $this->open_folder($content->visiblename);
88 $entries = array_keys($content->children);
90 foreach ($entries as $entry) {
91 $this->build_tree($content->children[$entry]);
94 $this->close_folder();
99 function get_content() {
101 global $CFG;
103 if ($this->content !== NULL) {
104 return $this->content;
107 if (!($this->has_admin_caps())) {
108 $this->content = '';
109 return '';
112 require_once($CFG->libdir.'/adminlib.php');
113 $adminroot = admin_get_root();
115 if ($this->pathtosection = $adminroot->path($this->section)) {
116 $this->pathtosection = array_reverse($this->pathtosection);
117 array_pop($this->pathtosection);
120 // we need to do this instead of $this->build_tree($adminroot) because the top-level folder
121 // is redundant (and ideally ignored). (the top-level folder is "administration".)
123 $entries = array_keys($adminroot->children);
125 asort($entries);
127 foreach ($entries as $entry) {
128 $this->build_tree($adminroot->children[$entry]);
131 if ($this->tempcontent !== '') {
132 $strfolderopened = s(get_string('folderopened'));
133 $strfolderclosed = s(get_string('folderclosed'));
135 $this->content = new stdClass;
136 $this->content->text = '<script type="text/javascript">'."\n";
137 $this->content->text .= '//<![CDATA[' . "\n";
138 $this->content->text .= 'var vh_numspans = ' . ($this->spancounter - 1) . ';' . "\n";
139 $this->content->text .= 'var vh_content = new Array();' . "\n";
140 $this->content->text .= 'function getspan(spanid) {' . "\n";
141 $this->content->text .= ' if (document.getElementById) {' . "\n";
142 $this->content->text .= ' return document.getElementById(spanid);' . "\n";
143 $this->content->text .= ' } else if (window[spanid]) {' . "\n";
144 $this->content->text .= ' return window[spanid];' . "\n";
145 $this->content->text .= ' }' . "\n";
146 $this->content->text .= ' return null;' . "\n";
147 $this->content->text .= '}' . "\n";
149 $this->content->text .= 'function toggle(spanid) {' . "\n";
150 $this->content->text .= ' if (getspan(spanid).innerHTML == "") {' . "\n";
151 $this->content->text .= ' getspan(spanid).innerHTML = vh_content[spanid];' . "\n";
152 $this->content->text .= ' getspan(spanid + "indicator").innerHTML = \'<img src="' . $CFG->wwwroot . '/blocks/admin_tree/open.gif" alt="'.$strfolderopened.'" />\';' . "\n";
153 $this->content->text .= ' } else {' . "\n";
154 $this->content->text .= ' vh_content[spanid] = getspan(spanid).innerHTML;' . "\n";
155 $this->content->text .= ' getspan(spanid).innerHTML = "";' . "\n";
156 $this->content->text .= ' getspan(spanid + "indicator").innerHTML = \'<img src="' . $CFG->wwwroot . '/blocks/admin_tree/closed.gif" alt="'.$strfolderclosed.'" />\';' . "\n";
157 $this->content->text .= ' }' . "\n";
158 $this->content->text .= '}' . "\n";
160 $this->content->text .= 'function collapse(spanid) {' . "\n";
161 $this->content->text .= ' if (getspan(spanid).innerHTML !== "") {' . "\n";
162 $this->content->text .= ' vh_content[spanid] = getspan(spanid).innerHTML;' . "\n";
163 $this->content->text .= ' getspan(spanid).innerHTML = "";' . "\n";
164 $this->content->text .= ' getspan(spanid + "indicator").innerHTML = \'<img src="' . $CFG->wwwroot . '/blocks/admin_tree/closed.gif" alt="'.$strfolderclosed.'" />\';' . "\n";
165 $this->content->text .= ' }' . "\n";
166 $this->content->text .= '}' . "\n";
168 $this->content->text .= 'function expand(spanid) {' . "\n";
169 $this->content->text .= ' getspan(spanid).innerHTML = vh_content[spanid];' . "\n";
170 $this->content->text .= ' getspan(spanid + "indicator").innerHTML = \'<img src="' . $CFG->wwwroot . '/blocks/admin_tree/open.gif" alt="'.$strfolderopened.'" />\';' . "\n";
171 $this->content->text .= '}' . "\n";
173 $this->content->text .= 'function expandall() {' . "\n";
174 $this->content->text .= ' for (i = 1; i <= vh_numspans; i++) {' . "\n";
175 $this->content->text .= ' expand("vh_span" + String(i));' . "\n";
176 $this->content->text .= ' }' . "\n";
177 $this->content->text .= '}' . "\n";
179 $this->content->text .= 'function collapseall() {' . "\n";
180 $this->content->text .= ' for (i = vh_numspans; i > 0; i--) {' . "\n";
181 $this->content->text .= ' collapse("vh_span" + String(i));' . "\n";
182 $this->content->text .= ' }' . "\n";
183 $this->content->text .= '}' . "\n";
185 $this->content->text .= '//]]>' . "\n";
186 $this->content->text .= '</script>' . "\n";
187 $this->content->text .= '<div class="admintree">' . "\n";
189 $this->content->text .= $this->tempcontent;
191 $this->content->text .= '</div>' . "\n";
192 $this->content->text .= '<script type="text/javascript">' . "\n";
193 $this->content->text .= '//<![CDATA[' . "\n";
194 $this->content->text .= 'collapseall();' . "\n";
195 $this->content->text .= $this->expandjavascript;
197 $this->content->text .= '//]]>' . "\n";
198 $this->content->text .= '</script>' . "\n";
200 $searchcontent = isset($CFG->adminsearchquery) ? $CFG->adminsearchquery : '';
202 $this->content->footer = '<div class="adminsearchform">'.
203 '<form action="'.$CFG->wwwroot.'/admin/search.php" method="get"><div>'.
204 '<label for="query" class="accesshide">'.get_string('searchinsettings', 'admin').'</label>'.
205 '<input type="text" name="query" id="query" size="8" value="'.s($searchcontent).'" />'.
206 '<input type="submit" value="'.get_string('search').'" /></div>'.
207 '</form></div>';
208 } else {
209 $this->content = new stdClass;
210 $this->content->text = '';
213 return $this->content;
218 * Return true
219 * if $USER has any caps that mean we should
220 * display this block...
222 function has_admin_caps() {
224 $sysctx = get_context_instance(CONTEXT_SYSTEM);
226 return (has_capability('moodle/site:config', $sysctx)
227 || has_capability('moodle/site:langeditmaster', $sysctx)
228 || has_capability('moodle/site:langeditlocal', $sysctx)
229 || has_capability('moodle/site:manageblocks', $sysctx)
230 || has_capability('moodle/user:delete', $sysctx)
231 || has_capability('moodle/user:update', $sysctx)
232 || has_capability('moodle/user:create', $sysctx)
233 || has_capability('moodle/grade:manage', $sysctx)
234 || has_capability('moodle/grade:manageletters', $sysctx)
235 || has_capability('moodle/grade:managescales', $sysctx)
236 || has_capability('moodle/site:readallmessages', $sysctx));