Commit | Line | Data |
---|---|---|
24e27ac0 SH |
1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
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. | |
9 | // | |
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. | |
14 | // | |
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/>. | |
17 | ||
18 | /** | |
19 | * Renderer for use with the course section and all the goodness that falls | |
20 | * within it. | |
21 | * | |
22 | * This renderer should contain methods useful to courses, and categories. | |
23 | * | |
24 | * @package moodlecore | |
25 | * @copyright 2010 Sam Hemelryk | |
26 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
27 | */ | |
28 | ||
29 | /** | |
30 | * The core course renderer | |
31 | * | |
32 | * Can be retrieved with the following: | |
33 | * $renderer = $PAGE->get_renderer('core','course'); | |
34 | */ | |
35 | class core_course_renderer extends plugin_renderer_base { | |
36 | ||
37 | /** | |
38 | * A cache of strings | |
39 | * @var stdClass | |
40 | */ | |
41 | protected $strings; | |
42 | ||
43 | /** | |
44 | * Override the constructor so that we can initialise the string cache | |
45 | * | |
46 | * @param moodle_page $page | |
47 | * @param string $target | |
48 | */ | |
49 | public function __construct(moodle_page $page, $target) { | |
50 | $this->strings = new stdClass; | |
51 | parent::__construct($page, $target); | |
52 | } | |
53 | ||
54 | /** | |
55 | * Renderers a structured array of courses and categories into a nice | |
56 | * XHTML tree structure. | |
57 | * | |
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() | |
60 | * | |
61 | * @param array $structure | |
62 | * @return string | |
63 | */ | |
64 | public function course_category_tree(array $structure) { | |
65 | $this->strings->allowguests = get_string('allowguests'); | |
66 | $this->strings->requireskey = get_string('requireskey'); | |
67 | $this->strings->summary = get_string('summary'); | |
68 | ||
69 | // Generate an id and the required JS call to make this a nice widget | |
70 | $id = html_writer::random_id('course_category_tree'); | |
71 | $this->page->requires->js_init_call('M.util.init_toggle_class_on_click', array($id, '.category.with_children', 'collapsed')); | |
72 | ||
73 | // Start content generation | |
74 | $content = html_writer::start_tag('div', array('class'=>'course_category_tree', 'id'=>$id)); | |
75 | foreach ($structure as $category) { | |
76 | $content .= $this->course_category_tree_category($category); | |
77 | } | |
78 | $content .= html_writer::start_tag('div', array('class'=>'controls')); | |
79 | $content .= html_writer::tag('div', get_string('collapseall'), array('class'=>'addtoall expandall')); | |
80 | $content .= html_writer::tag('div', get_string('expandall'), array('class'=>'removefromall collapseall')); | |
81 | $content .= html_writer::end_tag('div'); | |
82 | $content .= html_writer::end_tag('div'); | |
df997f84 | 83 | |
24e27ac0 SH |
84 | // Return the course category tree HTML |
85 | return $content; | |
86 | } | |
87 | ||
88 | /** | |
89 | * Renderers a category for use with course_category_tree | |
90 | * | |
91 | * @param array $category | |
92 | * @param int $depth | |
93 | * @return string | |
94 | */ | |
95 | protected function course_category_tree_category(stdClass $category, $depth=1) { | |
96 | $content = ''; | |
97 | $hassubcategories = (count($category->categories)>0); | |
98 | $hascourses = (count($category->courses)>0); | |
99 | $classes = array('category'); | |
100 | if ($category->parent != 0) { | |
101 | $classes[] = 'subcategory'; | |
102 | } | |
103 | if ($hassubcategories || $hascourses) { | |
104 | $classes[] = 'with_children'; | |
105 | if ($depth > 1) { | |
106 | $classes[] = 'collapsed'; | |
107 | } | |
108 | } | |
109 | $content .= html_writer::start_tag('div', array('class'=>join(' ', $classes))); | |
110 | $content .= html_writer::start_tag('div', array('class'=>'category_label')); | |
111 | $content .= html_writer::link(new moodle_url('/course/category.php', array('id'=>$category->id)), $category->name, array('class'=>'category_link')); | |
112 | $content .= html_writer::end_tag('div'); | |
113 | if ($hassubcategories) { | |
114 | $content .= html_writer::start_tag('div', array('class'=>'subcategories')); | |
115 | foreach ($category->categories as $subcategory) { | |
116 | $content .= $this->course_category_tree_category($subcategory, $depth+1); | |
117 | } | |
118 | $content .= html_writer::end_tag('div'); | |
119 | } | |
120 | if ($hascourses) { | |
121 | $content .= html_writer::start_tag('div', array('class'=>'courses')); | |
122 | $coursecount = 0; | |
123 | foreach ($category->courses as $course) { | |
124 | $classes = array('course'); | |
125 | $coursecount ++; | |
126 | $classes[] = ($coursecount%2)?'odd':'even'; | |
127 | $content .= html_writer::start_tag('div', array('class'=>join(' ', $classes))); | |
7d2b6e8c | 128 | $content .= html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), s($course->fullname), array('class'=>'course_link')); |
24e27ac0 SH |
129 | $content .= html_writer::start_tag('div', array('class'=>'course_info clearfix')); |
130 | ||
df997f84 PS |
131 | //TODO: add enrol info |
132 | $content .= html_writer::tag('div', '', array('class'=>'course_info_spacer')); | |
133 | $content .= html_writer::tag('div', '', array('class'=>'course_info_spacer')); | |
134 | ||
24e27ac0 SH |
135 | if ($course->summary) { |
136 | $image = html_writer::empty_tag('img', array('src'=>$this->output->pix_url('i/info'), 'alt'=>$this->strings->summary)); | |
137 | $content .= html_writer::link(new moodle_url('/course/info.php', array('id'=>$course->id)), $image, array('title'=>$this->strings->summary)); | |
138 | } else { | |
139 | $content .= html_writer::tag('div', '', array('class'=>'course_info_spacer')); | |
140 | } | |
141 | $content .= html_writer::end_tag('div'); | |
142 | $content .= html_writer::end_tag('div'); | |
143 | } | |
144 | $content .= html_writer::end_tag('div'); | |
145 | } | |
146 | $content .= html_writer::end_tag('div'); | |
147 | return $content; | |
148 | } | |
149 | } |