forked from iandavidwild/moodle-block_course_level
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
89 lines (70 loc) · 2.75 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Print course level tree
*
* @package block_course_level
* @copyright 2012 University of London Computer Centre
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/blocks/course_level/lib.php');
class block_course_level_renderer extends plugin_renderer_base {
/**
* Prints course level tree view
* @return string
*/
public function course_level_tree() {
return $this->render(new course_level_tree);
}
public function render_course_level_tree(course_level_tree $tree) {
$module = array('name'=>'block_course_level', 'fullpath'=>'/blocks/course_level/module.js', 'requires'=>array('yui2-treeview'));
if (empty($tree) ) {
$html = $this->output->box(get_string('nocourses', 'block_course_level'));
} else {
$htmlid = 'course_level_tree_'.uniqid();
$this->page->requires->js_init_call('M.block_course_level.init_tree', array(false, $htmlid));
$html = '<div id="'.$htmlid.'">';
$html .= $this->htmllize_tree($tree->courses);
$html .= '</div>';
}
return $html;
}
/**
* Converts the course tree into something more meaningful.
*
* @param $tree
* @param int $indent
* @return string
*/
protected function htmllize_tree($tree, $indent=0) {
$yuiconfig = array();
$yuiconfig['type'] = 'html';
$result = '<ul>';
foreach ($tree as $node) {
$children = $node->get_children();
// TODO this needs to use html_writer to output
if($children != null) {
$result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.s($node->get_shortname()).'</div> '.$this->htmllize_tree($children, $indent+1).'</li>';
} else {
$result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.s($node->get_shortname()).'</div></li>';
}
}
$result .= '</ul>';
return $result;
}
}