3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Renderer for use with the course section and all the goodness that falls
22 * This renderer should contain methods useful to courses, and categories.
25 * @copyright 2010 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 * The core course renderer
32 * Can be retrieved with the following:
33 * $renderer = $PAGE->get_renderer('core','course');
35 class core_course_renderer
extends plugin_renderer_base
{
44 * Override the constructor so that we can initialise the string cache
46 * @param moodle_page $page
47 * @param string $target
49 public function __construct(moodle_page
$page, $target) {
50 $this->strings
= new stdClass
;
51 parent
::__construct($page, $target);
55 * Renderers a structured array of courses and categories into a nice
56 * XHTML tree structure.
58 * This method was designed initially to display the front page course/category
59 * combo view. The structure can be retrieved by get_course_category_tree()
61 * @param array $structure
64 public function course_category_tree(array $structure) {
65 $this->strings
->summary
= get_string('summary');
67 // Generate an id and the required JS call to make this a nice widget
68 $id = html_writer
::random_id('course_category_tree');
69 $this->page
->requires
->js_init_call('M.util.init_toggle_class_on_click', array($id, '.category.with_children .category_label', 'collapsed', '.category.with_children'));
71 // Start content generation
72 $content = html_writer
::start_tag('div', array('class'=>'course_category_tree', 'id'=>$id));
73 foreach ($structure as $category) {
74 $content .= $this->course_category_tree_category($category);
76 $content .= html_writer
::start_tag('div', array('class'=>'controls'));
77 $content .= html_writer
::tag('div', get_string('collapseall'), array('class'=>'addtoall expandall'));
78 $content .= html_writer
::tag('div', get_string('expandall'), array('class'=>'removefromall collapseall'));
79 $content .= html_writer
::end_tag('div');
80 $content .= html_writer
::end_tag('div');
82 // Return the course category tree HTML
87 * Renderers a category for use with course_category_tree
89 * @param array $category
93 protected function course_category_tree_category(stdClass
$category, $depth=1) {
95 $hassubcategories = (isset($category->categories
) && count($category->categories
)>0);
96 $hascourses = (isset($category->courses
) && count($category->courses
)>0);
97 $classes = array('category');
98 if ($category->parent
!= 0) {
99 $classes[] = 'subcategory';
101 if (empty($category->visible
)) {
102 $classes[] = 'dimmed_category';
104 if ($hassubcategories ||
$hascourses) {
105 $classes[] = 'with_children';
107 $classes[] = 'collapsed';
110 $categoryname = format_string($category->name
, true, array('context' => get_context_instance(CONTEXT_COURSECAT
, $category->id
)));
112 $content .= html_writer
::start_tag('div', array('class'=>join(' ', $classes)));
113 $content .= html_writer
::start_tag('div', array('class'=>'category_label'));
114 $content .= html_writer
::link(new moodle_url('/course/category.php', array('id'=>$category->id
)), $categoryname, array('class'=>'category_link'));
115 $content .= html_writer
::end_tag('div');
116 if ($hassubcategories) {
117 $content .= html_writer
::start_tag('div', array('class'=>'subcategories'));
118 foreach ($category->categories
as $subcategory) {
119 $content .= $this->course_category_tree_category($subcategory, $depth+
1);
121 $content .= html_writer
::end_tag('div');
124 $content .= html_writer
::start_tag('div', array('class'=>'courses'));
126 foreach ($category->courses
as $course) {
127 $classes = array('course');
128 $linkclass = 'course_link';
129 if (!$course->visible
) {
130 $linkclass .= ' dimmed';
133 $classes[] = ($coursecount%2
)?
'odd':'even';
134 $content .= html_writer
::start_tag('div', array('class'=>join(' ', $classes)));
135 $content .= html_writer
::link(new moodle_url('/course/view.php', array('id'=>$course->id
)), format_string($course->fullname
), array('class'=>$linkclass));
136 $content .= html_writer
::start_tag('div', array('class'=>'course_info clearfix'));
139 if ($icons = enrol_get_course_info_icons($course)) {
140 foreach ($icons as $pix_icon) {
141 $content .= $this->render($pix_icon);
145 if ($course->summary
) {
146 $image = html_writer
::empty_tag('img', array('src'=>$this->output
->pix_url('i/info'), 'alt'=>$this->strings
->summary
));
147 $content .= html_writer
::link(new moodle_url('/course/info.php', array('id'=>$course->id
)), $image, array('title'=>$this->strings
->summary
));
149 $content .= html_writer
::end_tag('div');
150 $content .= html_writer
::end_tag('div');
152 $content .= html_writer
::end_tag('div');
154 $content .= html_writer
::end_tag('div');