-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrenderer.php
More file actions
169 lines (142 loc) · 5.68 KB
/
renderer.php
File metadata and controls
169 lines (142 loc) · 5.68 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?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 hierarchy
*
* @package block_ual_mymoodle
* @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/ual_mymoodle/lib.php');
class block_ual_mymoodle_renderer extends plugin_renderer_base {
private $showcode = 0;
private $trimmode = block_ual_mymoodle::TRIM_RIGHT;
private $trimlength = 50;
/**
* Prints course hierarchy view
* @return string
*/
public function course_hierarchy($showcode, $trimmode, $trimlength) {
$this->showcode = $showcode;
$this->trimmode = $trimmode;
$this->trimlength = $trimlength;
return $this->render(new course_hierarchy);
}
/**
* Provides the html contained in the course hierarchy/'My UAL Moodle' block. The hierarchy is passed as a tree.
*
* @param render_course_hierarchy $tree
* @return string
*/
public function render_course_hierarchy(course_hierarchy $tree) {
if (empty($tree) ) {
$html = $this->output->box(get_string('nocourses', 'block_ual_mymoodle'));
} else {
$htmlid = 'course_hierarchy_'.uniqid();
$html = '<div id="'.$htmlid.'">';
$html .= $this->htmllize_tree($tree->courses);
$html .= '</div>';
}
return $html;
}
/**
* Converts the course hierarchy into something more meaningful.
*
* @param $tree
* @param int $indent
* @return string
*/
protected function htmllize_tree($tree, $indent=0) {
global $CFG;
$result = '<ul>';
if (empty($tree)) {
$result .= html_writer::tag('li', get_string('nothingtodisplay'));
} else {
foreach ($tree as $node) {
$name = $node->get_fullname();
if($this->showcode == 1) {
$name .= ' ('.$node->get_shortname().')';
}
$course_fullname = $this->trim($name);
$node_type = $node->get_type();
$type_id = '';
switch($node_type) {
case ual_course::COURSETYPE_PROGRAMME:
$type_id = 'programme';
case ual_course::COURSETYPE_COURSE:
$type_id = 'course';
case ual_course::COURSETYPE_UNIT:
$type_id = 'unit';
}
$attributes = array('id' => $type_id);
if($node_type == ual_course::COURSETYPE_UNIT) {
// Create a link...
$attributes['title'] = $course_fullname;
$moodle_url = $CFG->wwwroot.'/course/view.php?id='.$node->get_id();
$content = html_writer::link($moodle_url, $course_fullname, $attributes);
} else {
// Don't...
$content = html_writer::tag('strong', $course_fullname, $attributes);
}
$children = $node->get_children();
if ($children == null) {
$result .= html_writer::tag('li', $content, $attributes);
} else {
// If this has parents OR it doesn't have parents or children then we need to display it...???
$result .= html_writer::tag('li', $content.$this->htmllize_tree($children, $indent+1), $attributes);
}
}
}
$result .= '</ul>';
return $result;
}
/**
* Trims the text and shorttext properties of this node and optionally
* all of its children.
*
* @param string $text The text to truncate
* @return string
*/
private function trim($text) {
$result = $text;
switch ($this->trimmode) {
case block_ual_mymoodle::TRIM_RIGHT :
if (textlib::strlen($text)>($this->trimlength+3)) {
// Truncate the text to $long characters.
$result = textlib::substr($text, 0, $this->trimlength).'...';
}
break;
case block_ual_mymoodle::TRIM_LEFT :
if (textlib::strlen($text)>($this->trimlength+3)) {
// Truncate the text to $long characters.
$result = '...'.textlib::substr($text, textlib::strlen($text)-$this->trimlength, $this->trimlength);
}
break;
case block_ual_mymoodle::TRIM_CENTER :
if (textlib::strlen($text)>($this->trimlength+3)) {
// Truncate the text to $long characters.
$length = ceil($this->trimlength/2);
$start = textlib::substr($text, 0, $length);
$end = textlib::substr($text, textlib::strlen($text)-$this->trimlength);
$result = $start.'...'.$end;
}
break;
}
return $result;
}
}