forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
155 lines (143 loc) · 6.23 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
<?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 = (count($category->categories)>0);
$hascourses = (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';
}
}
$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)), $category->name, 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;
}
}