forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
308 lines (277 loc) · 13.2 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?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/>.
/**
* Renderer for use with the course section and all the goodness that falls
* within it.
*
* This renderer should contain methods useful to courses, and categories.
*
* @package moodlecore
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* The core course renderer
*
* Can be retrieved with the following:
* $renderer = $PAGE->get_renderer('core','course');
*/
class core_course_renderer extends plugin_renderer_base {
/**
* A cache of strings
* @var stdClass
*/
protected $strings;
/**
* Override the constructor so that we can initialise the string cache
*
* @param moodle_page $page
* @param string $target
*/
public function __construct(moodle_page $page, $target) {
$this->strings = new stdClass;
parent::__construct($page, $target);
}
/**
* Renderers a structured array of courses and categories into a nice
* XHTML tree structure.
*
* This method was designed initially to display the front page course/category
* combo view. The structure can be retrieved by get_course_category_tree()
*
* @param array $structure
* @return string
*/
public function course_category_tree(array $structure) {
$this->strings->summary = get_string('summary');
// Generate an id and the required JS call to make this a nice widget
$id = html_writer::random_id('course_category_tree');
$this->page->requires->js_init_call('M.util.init_toggle_class_on_click', array($id, '.category.with_children .category_label', 'collapsed', '.category.with_children'));
// Start content generation
$content = html_writer::start_tag('div', array('class'=>'course_category_tree', 'id'=>$id));
foreach ($structure as $category) {
$content .= $this->course_category_tree_category($category);
}
$content .= html_writer::start_tag('div', array('class'=>'controls'));
$content .= html_writer::tag('div', get_string('collapseall'), array('class'=>'addtoall expandall'));
$content .= html_writer::tag('div', get_string('expandall'), array('class'=>'removefromall collapseall'));
$content .= html_writer::end_tag('div');
$content .= html_writer::end_tag('div');
// Return the course category tree HTML
return $content;
}
/**
* Renderers a category for use with course_category_tree
*
* @param array $category
* @param int $depth
* @return string
*/
protected function course_category_tree_category(stdClass $category, $depth=1) {
$content = '';
$hassubcategories = (isset($category->categories) && count($category->categories)>0);
$hascourses = (isset($category->courses) && count($category->courses)>0);
$classes = array('category');
if ($category->parent != 0) {
$classes[] = 'subcategory';
}
if (empty($category->visible)) {
$classes[] = 'dimmed_category';
}
if ($hassubcategories || $hascourses) {
$classes[] = 'with_children';
if ($depth > 1) {
$classes[] = 'collapsed';
}
}
$categoryname = format_string($category->name, true, array('context' => get_context_instance(CONTEXT_COURSECAT, $category->id)));
$content .= html_writer::start_tag('div', array('class'=>join(' ', $classes)));
$content .= html_writer::start_tag('div', array('class'=>'category_label'));
$content .= html_writer::link(new moodle_url('/course/category.php', array('id'=>$category->id)), $categoryname, array('class'=>'category_link'));
$content .= html_writer::end_tag('div');
if ($hassubcategories) {
$content .= html_writer::start_tag('div', array('class'=>'subcategories'));
foreach ($category->categories as $subcategory) {
$content .= $this->course_category_tree_category($subcategory, $depth+1);
}
$content .= html_writer::end_tag('div');
}
if ($hascourses) {
$content .= html_writer::start_tag('div', array('class'=>'courses'));
$coursecount = 0;
foreach ($category->courses as $course) {
$classes = array('course');
$linkclass = 'course_link';
if (!$course->visible) {
$linkclass .= ' dimmed';
}
$coursecount ++;
$classes[] = ($coursecount%2)?'odd':'even';
$content .= html_writer::start_tag('div', array('class'=>join(' ', $classes)));
$content .= html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), format_string($course->fullname), array('class'=>$linkclass));
$content .= html_writer::start_tag('div', array('class'=>'course_info clearfix'));
// print enrol info
if ($icons = enrol_get_course_info_icons($course)) {
foreach ($icons as $pix_icon) {
$content .= $this->render($pix_icon);
}
}
if ($course->summary) {
$image = html_writer::empty_tag('img', array('src'=>$this->output->pix_url('i/info'), 'alt'=>$this->strings->summary));
$content .= html_writer::link(new moodle_url('/course/info.php', array('id'=>$course->id)), $image, array('title'=>$this->strings->summary));
}
$content .= html_writer::end_tag('div');
$content .= html_writer::end_tag('div');
}
$content .= html_writer::end_tag('div');
}
$content .= html_writer::end_tag('div');
return $content;
}
/**
* Build the HTML for the module chooser javascript popup
*
* @param array $modules A set of modules as returned form @see
* get_module_metadata
* @param object $course The course that will be displayed
* @return string The composed HTML for the module
*/
public function course_modchooser($modules, $course) {
global $OUTPUT;
// Add the header
$header = html_writer::tag('div', get_string('addresourceoractivity', 'moodle'),
array('id' => 'choosertitle', 'class' => 'hd'));
$formcontent = html_writer::start_tag('form', array('action' => new moodle_url('/course/jumpto.php'),
'id' => 'chooserform', 'method' => 'post'));
$formcontent .= html_writer::start_tag('div', array('id' => 'typeformdiv'));
$formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'id' => 'course',
'name' => 'course', 'value' => $course->id));
$formcontent .= html_writer::tag('input', '',
array('type' => 'hidden', 'id' => 'jump', 'name' => 'jump', 'value' => ''));
$formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => 'sesskey',
'value' => sesskey()));
$formcontent .= html_writer::end_tag('div');
// Put everything into one tag 'options'
$formcontent .= html_writer::start_tag('div', array('class' => 'options'));
$formcontent .= html_writer::tag('div', get_string('selectmoduletoviewhelp', 'moodle'),
array('class' => 'instruction'));
// Put all options into one tag 'alloptions' to allow us to handle scrolling
$formcontent .= html_writer::start_tag('div', array('class' => 'alloptions'));
// First display Resources
$resources = array_filter($modules,
create_function('$mod', 'return ($mod->archetype === MOD_CLASS_RESOURCE);'));
if (count($resources)) {
$formcontent .= $this->course_modchooser_title('resources');
$formcontent .= $this->course_modchooser_module_types($resources);
}
// Then activities
$activities = array_filter($modules,
create_function('$mod', 'return ($mod->archetype !== MOD_CLASS_RESOURCE);'));
if (count($activities)) {
$formcontent .= $this->course_modchooser_title('activities');
$formcontent .= $this->course_modchooser_module_types($activities);
}
$formcontent .= html_writer::end_tag('div'); // modoptions
$formcontent .= html_writer::end_tag('div'); // types
$formcontent .= html_writer::start_tag('div', array('class' => 'submitbuttons'));
$formcontent .= html_writer::tag('input', '',
array('type' => 'submit', 'name' => 'addcancel', 'id' => 'addcancel', 'value' => get_string('cancel')));
$formcontent .= html_writer::tag('input', '',
array('type' => 'submit', 'name' => 'submitbutton', 'id' => 'submitbutton', 'value' => get_string('add')));
$formcontent .= html_writer::end_tag('div');
$formcontent .= html_writer::end_tag('form');
// Wrap the whole form in a div
$formcontent = html_writer::tag('div', $formcontent, array('id' => 'chooseform'));
// Put all of the content together
$content = $formcontent;
$content = html_writer::tag('div', $content, array('id' => 'choosercontainer'));
return $header . html_writer::tag('div', $content, array('id' => 'chooserdialogue'));
}
/**
* Build the HTML for a specified set of modules
*
* @param array $modules A set of modules as used by the
* course_modchooser_module function
* @return string The composed HTML for the module
*/
protected function course_modchooser_module_types($modules) {
$return = '';
foreach ($modules as $module) {
if (!isset($module->types)) {
$return .= $this->course_modchooser_module($module);
} else {
$return .= $this->course_modchooser_module($module, array('nonoption'));
foreach ($module->types as $type) {
$return .= $this->course_modchooser_module($type, array('option', 'subtype'));
}
}
}
return $return;
}
/**
* Return the HTML for the specified module adding any required classes
*
* @param object $module An object containing the title, and link. An
* icon, and help text may optionally be specified. If the module
* contains subtypes in the types option, then these will also be
* displayed.
* @param array $classes Additional classes to add to the encompassing
* div element
* @return string The composed HTML for the module
*/
protected function course_modchooser_module($module, $classes = array('option')) {
$output = '';
$output .= html_writer::start_tag('div', array('class' => implode(' ', $classes)));
$output .= html_writer::start_tag('label', array('for' => 'module_' . $module->name));
if (!isset($module->types)) {
$output .= html_writer::tag('input', '', array('type' => 'radio',
'name' => 'jumplink', 'id' => 'module_' . $module->name, 'value' => $module->link));
}
$output .= html_writer::start_tag('span', array('class' => 'modicon'));
if (isset($module->icon)) {
// Add an icon if we have one
$output .= $module->icon;
}
$output .= html_writer::end_tag('span');
$output .= html_writer::tag('span', $module->title, array('class' => 'typename'));
if (!isset($module->help)) {
// Add help if found
$module->help = get_string('nohelpforactivityorresource', 'moodle');
}
// Format the help text using markdown with the following options
$options = new stdClass();
$options->trusted = false;
$options->noclean = false;
$options->smiley = false;
$options->filter = false;
$options->para = true;
$options->newlines = false;
$options->overflowdiv = false;
$module->help = format_text($module->help, FORMAT_MARKDOWN, $options);
$output .= html_writer::tag('span', $module->help, array('class' => 'typesummary'));
$output .= html_writer::end_tag('label');
$output .= html_writer::end_tag('div');
return $output;
}
protected function course_modchooser_title($title, $identifier = null) {
$module = new stdClass();
$module->name = $title;
$module->types = array();
$module->title = get_string($title, $identifier);
$module->help = '';
return $this->course_modchooser_module($module, array('moduletypetitle'));
}
}