Commit | Line | Data |
---|---|---|
36dc3b71 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 | * This file contains the renderers for the calendar within Moodle | |
20 | * | |
21 | * @copyright 2010 Sam Hemelryk | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | * @package calendar | |
24 | */ | |
25 | ||
26 | /** | |
27 | * The primary renderer for the calendar. | |
28 | */ | |
29 | class core_calendar_renderer extends plugin_renderer_base { | |
30 | ||
31 | /** | |
32 | * Creates a basic export form | |
33 | * | |
34 | * @param bool $allowthisweek | |
35 | * @param bool $allownextweek | |
36 | * @param bool $allownextmonth | |
37 | * @param string $username | |
38 | * @param string $authtoken | |
39 | * @return string | |
40 | */ | |
41 | public function basic_export_form($allowthisweek, $allownextweek, $allownextmonth, $username, $authtoken) { | |
42 | ||
43 | $output = html_writer::tag('div', get_string('export', 'calendar'), array('class'=>'header')); | |
44 | $output .= html_writer::start_tag('fieldset'); | |
45 | $output .= html_writer::tag('legend', get_string('commontasks', 'calendar')); | |
46 | $output .= html_writer::start_tag('form', array('action'=>new moodle_url('/calendar/export_execute.php'), 'method'=>'get')); | |
47 | ||
48 | $output .= html_writer::tag('div', get_string('iwanttoexport', 'calendar')); | |
6be8c6b7 | 49 | |
36dc3b71 SH |
50 | $output .= html_writer::start_tag('div', array('class'=>'indent')); |
51 | $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_what', 'id'=>'pw_all', 'value'=>'all', 'checked'=>'checked')); | |
52 | $output .= html_writer::tag('label', get_string('eventsall', 'calendar'), array('for'=>'pw_all')); | |
53 | $output .= html_writer::empty_tag('br'); | |
54 | $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_what', 'id'=>'pw_course', 'value'=>'courses')); | |
55 | $output .= html_writer::tag('label', get_string('eventsrelatedtocourses', 'calendar'), array('for'=>'pw_course')); | |
56 | $output .= html_writer::empty_tag('br'); | |
57 | $output .= html_writer::end_tag('div'); | |
6be8c6b7 | 58 | |
36dc3b71 SH |
59 | $output .= html_writer::tag('div', get_string('for', 'calendar').':'); |
60 | ||
61 | $output .= html_writer::start_tag('div', array('class'=>'indent')); | |
62 | if ($allowthisweek) { | |
63 | $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_wknow', 'value'=>'weeknow', 'checked'=>'checked')); | |
64 | $output .= html_writer::tag('label', get_string('weekthis', 'calendar'), array('for'=>'pt_wknow')); | |
65 | $output .= html_writer::empty_tag('br'); | |
66 | } | |
67 | if ($allownextweek) { | |
68 | $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_wknext', 'value'=>'weeknext')); | |
69 | $output .= html_writer::tag('label', get_string('weeknext', 'calendar'), array('for'=>'pt_wknext')); | |
70 | $output .= html_writer::empty_tag('br'); | |
71 | } | |
72 | $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_monnow', 'value'=>'monthnow')); | |
73 | $output .= html_writer::tag('label', get_string('monththis', 'calendar'), array('for'=>'pt_monnow')); | |
74 | $output .= html_writer::empty_tag('br'); | |
75 | if ($allownextmonth) { | |
76 | $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_monnext', 'value'=>'monthnext')); | |
77 | $output .= html_writer::tag('label', get_string('monthnext', 'calendar'), array('for'=>'pt_monnext')); | |
78 | $output .= html_writer::empty_tag('br'); | |
79 | } | |
80 | $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_recupc', 'value'=>'recentupcoming')); | |
81 | $output .= html_writer::tag('label', get_string('recentupcoming', 'calendar'), array('for'=>'pt_recupc')); | |
82 | $output .= html_writer::empty_tag('br'); | |
83 | $output .= html_writer::end_tag('div'); | |
84 | ||
85 | $output .= html_writer::start_tag('div', array('class'=>'rightalign')); | |
86 | $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_d', 'value'=>'')); | |
87 | $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_m', 'value'=>'')); | |
88 | $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_y', 'value'=>'')); | |
89 | $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'username', 'value'=>$username)); | |
90 | $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'authtoken', 'value'=>$authtoken)); | |
91 | ||
92 | $output .= html_writer::empty_tag('input', array('type'=>'button', 'id'=>'generateurl', 'value'=>get_string('generateurlbutton', 'calendar'))); | |
93 | $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('exportbutton', 'calendar'))); | |
94 | ||
95 | $output .= html_writer::end_tag('div'); | |
96 | ||
97 | $output .= html_writer::end_tag('form'); | |
98 | $output .= html_writer::end_tag('fieldset'); | |
99 | ||
100 | $output .= html_writer::start_tag('div', array('id'=>'urlbox', 'style'=>'display:none;')); | |
101 | $output .= html_writer::tag('p', get_string('urlforical', 'calendar')); | |
102 | $output .= html_writer::tag('div', '', array('id'=>'url', 'style'=>'overflow:scroll;width:650px;')); | |
103 | $output .= html_writer::end_tag('div'); | |
104 | ||
33e48a1a | 105 | $this->page->requires->yui_module('moodle-calendar-eventmanager', 'M.core_calendar.init_basic_export', array($allowthisweek, $allownextweek, $allownextmonth, $username, $authtoken)); |
36dc3b71 SH |
106 | |
107 | return $output; | |
108 | } | |
109 | ||
110 | /** | |
111 | * Starts the standard layout for the page | |
6be8c6b7 | 112 | * |
36dc3b71 SH |
113 | * @return string |
114 | */ | |
115 | public function start_layout() { | |
116 | return html_writer::start_tag('div', array('class'=>'maincalendar')); | |
117 | } | |
118 | ||
119 | /** | |
120 | * Creates the remainder of the layout | |
121 | * | |
122 | * @return string | |
123 | */ | |
124 | public function complete_layout() { | |
125 | return html_writer::end_tag('div'); | |
126 | } | |
127 | ||
128 | /** | |
129 | * Produces the content for the filters block (pretend block) | |
130 | * | |
131 | * @param int $courseid | |
132 | * @param int $day | |
133 | * @param int $month | |
134 | * @param int $year | |
135 | * @param int $view | |
136 | * @param int $courses | |
137 | * @return string | |
138 | */ | |
139 | public function fake_block_filters($courseid, $day, $month, $year, $view, $courses) { | |
797cedc7 | 140 | return html_writer::tag('div', calendar_filter_controls($this->page->url), array('class'=>'calendar_filters filters')); |
36dc3b71 SH |
141 | } |
142 | ||
143 | /** | |
144 | * Produces the content for the three months block (pretend block) | |
145 | * | |
146 | * This includes the previous month, the current month, and the next month | |
147 | * | |
148 | * @param calendar_information $calendar | |
149 | * @return string | |
150 | */ | |
151 | public function fake_block_threemonths(calendar_information $calendar) { | |
152 | ||
153 | list($prevmon, $prevyr) = calendar_sub_month($calendar->month, $calendar->year); | |
154 | list($nextmon, $nextyr) = calendar_add_month($calendar->month, $calendar->year); | |
155 | ||
156 | $content = html_writer::start_tag('div', array('class'=>'minicalendarblock')); | |
157 | $content .= calendar_top_controls('display', array('id' => $calendar->courseid, 'm' => $prevmon, 'y' => $prevyr)); | |
158 | $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, $prevmon, $prevyr); | |
159 | $content .= html_writer::end_tag('div'); | |
160 | $content .= html_writer::start_tag('div', array('class'=>'minicalendarblock')); | |
161 | $content .= calendar_top_controls('display', array('id' => $calendar->courseid, 'm' => $calendar->month, 'y' => $calendar->year)); | |
162 | $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, $calendar->month, $calendar->year); | |
163 | $content .= html_writer::end_tag('div'); | |
164 | $content .= html_writer::start_tag('div', array('class'=>'minicalendarblock')); | |
165 | $content .= calendar_top_controls('display', array('id' => $calendar->courseid, 'm' => $nextmon, 'y' => $nextyr)); | |
166 | $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, $nextmon, $nextyr); | |
167 | $content .= html_writer::end_tag('div'); | |
168 | return $content; | |
169 | } | |
170 | ||
171 | /** | |
172 | * Adds a pretent calendar block | |
173 | * | |
174 | * @param block_contents $bc | |
175 | * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT | |
176 | */ | |
177 | public function add_pretend_calendar_block(block_contents $bc, $pos=BLOCK_POS_RIGHT) { | |
d9c26e21 | 178 | $this->page->blocks->add_fake_block($bc, $pos); |
36dc3b71 SH |
179 | } |
180 | ||
181 | /** | |
182 | * Creates a button to add a new event | |
183 | * | |
184 | * @param int $courseid | |
185 | * @param int $day | |
186 | * @param int $month | |
187 | * @param int $year | |
188 | * @return string | |
189 | */ | |
190 | protected function add_event_button($courseid, $day=null, $month=null, $year=null) { | |
191 | $output = html_writer::start_tag('div', array('class'=>'buttons')); | |
192 | $output .= html_writer::start_tag('form', array('action'=>CALENDAR_URL.'event.php', 'method'=>'get')); | |
193 | $output .= html_writer::start_tag('div'); | |
194 | $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'action', 'value'=>'new')); | |
195 | $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'course', 'value'=>$courseid)); | |
196 | if ($day !== null) { | |
197 | $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_d', 'value'=>$day)); | |
198 | } | |
199 | if ($month !== null) { | |
200 | $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_m', 'value'=>$month)); | |
201 | } | |
202 | if ($year !== null) { | |
203 | $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_y', 'value'=>$year)); | |
204 | } | |
205 | $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('newevent', 'calendar'))); | |
206 | $output .= html_writer::end_tag('div'); | |
207 | $output .= html_writer::end_tag('form'); | |
208 | $output .= html_writer::end_tag('div'); | |
209 | return $output; | |
210 | } | |
211 | ||
212 | /** | |
213 | * Displays the calendar for a single day | |
214 | * | |
215 | * @param calendar_information $calendar | |
216 | * @return string | |
217 | */ | |
797cedc7 SH |
218 | public function show_day(calendar_information $calendar, moodle_url $returnurl = null) { |
219 | ||
220 | if ($returnurl === null) { | |
221 | $returnurl = $this->page->url; | |
222 | } | |
223 | ||
36dc3b71 SH |
224 | $calendar->checkdate(); |
225 | $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, 1, 100, $calendar->timestamp_today()); | |
226 | ||
227 | $output = html_writer::start_tag('div', array('class'=>'header')); | |
797cedc7 SH |
228 | if (calendar_user_can_add_event($calendar->course)) { |
229 | $output .= $this->add_event_button($calendar->course->id, $calendar->day, $calendar->month, $calendar->year); | |
36dc3b71 | 230 | } |
91a774e2 | 231 | //$output .= html_writer::tag('label', get_string('dayview', 'calendar'), array('for'=>'cal_course_flt_jump')); |
797cedc7 | 232 | $output .= $this->course_filter_selector($returnurl, get_string('dayview', 'calendar')); |
36dc3b71 SH |
233 | $output .= html_writer::end_tag('div'); |
234 | // Controls | |
235 | $output .= html_writer::tag('div', calendar_top_controls('day', array('id' => $calendar->courseid, 'd' => $calendar->day, 'm' => $calendar->month, 'y' => $calendar->year)), array('class'=>'controls')); | |
236 | ||
237 | if (empty($events)) { | |
238 | // There is nothing to display today. | |
239 | $output .= $this->output->heading(get_string('daywithnoevents', 'calendar'), 3); | |
240 | } else { | |
241 | $output .= html_writer::start_tag('div', array('class'=>'eventlist')); | |
242 | $underway = array(); | |
243 | // First, print details about events that start today | |
244 | foreach ($events as $event) { | |
245 | $event = new calendar_event($event); | |
246 | $event->calendarcourseid = $calendar->courseid; | |
247 | if ($event->timestart >= $calendar->timestamp_today() && $event->timestart <= $calendar->timestamp_tomorrow()-1) { // Print it now | |
0f927f1e | 248 | $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today()); |
36dc3b71 SH |
249 | $output .= $this->event($event); |
250 | } else { // Save this for later | |
251 | $underway[] = $event; | |
252 | } | |
253 | } | |
254 | ||
255 | // Then, show a list of all events that just span this day | |
256 | if (!empty($underway)) { | |
257 | $output .= $this->output->heading(get_string('spanningevents', 'calendar'), 3); | |
258 | foreach ($underway as $event) { | |
0f927f1e | 259 | $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today()); |
36dc3b71 SH |
260 | $output .= $this->event($event); |
261 | } | |
262 | } | |
263 | ||
264 | $output .= html_writer::end_tag('div'); | |
265 | } | |
266 | ||
267 | return $output; | |
268 | } | |
269 | ||
270 | /** | |
271 | * Displays an event | |
272 | * | |
273 | * @param calendar_event $event | |
274 | * @param bool $showactions | |
275 | * @return string | |
276 | */ | |
277 | public function event(calendar_event $event, $showactions=true) { | |
278 | $event = calendar_add_event_metadata($event); | |
279 | ||
0f927f1e | 280 | $anchor = html_writer::tag('a', '', array('name'=>'event_'.$event->id)); |
36dc3b71 SH |
281 | |
282 | $table = new html_table(); | |
283 | $table->attributes = array('class'=>'event', 'cellspacing'=>'0'); | |
284 | $table->data = array( | |
285 | 0 => new html_table_row(), | |
286 | 1 => new html_table_row(), | |
287 | ); | |
288 | ||
289 | if (!empty($event->icon)) { | |
0f927f1e | 290 | $table->data[0]->cells[0] = new html_table_cell($anchor.$event->icon); |
36dc3b71 | 291 | } else { |
0f927f1e | 292 | $table->data[0]->cells[0] = new html_table_cell($anchor.$this->output->spacer(array('height'=>16, 'width'=>16, 'br'=>true))); |
36dc3b71 SH |
293 | } |
294 | $table->data[0]->cells[0]->attributes['class'] .= ' picture'; | |
295 | ||
296 | $table->data[0]->cells[1] = new html_table_cell(); | |
297 | $table->data[0]->cells[1]->attributes['class'] .= ' topic'; | |
298 | if (!empty($event->referer)) { | |
299 | $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->referer, array('class'=>'referer')); | |
300 | } else { | |
301 | $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->name, array('class'=>'name')); | |
302 | } | |
303 | if (!empty($event->courselink)) { | |
304 | $table->data[0]->cells[1]->text .= html_writer::tag('div', $event->courselink, array('class'=>'course')); | |
305 | } | |
306 | if (!empty($event->time)) { | |
307 | $table->data[0]->cells[1]->text .= html_writer::tag('span', $event->time, array('class'=>'date')); | |
308 | } else { | |
309 | $table->data[0]->cells[1]->text .= html_writer::tag('span', calendar_time_representation($event->timestart), array('class'=>'date')); | |
310 | } | |
311 | ||
312 | $table->data[1]->cells[0] = new html_table_cell(' '); | |
313 | $table->data[1]->cells[0]->attributes['class'] .= 'side'; | |
314 | ||
315 | $table->data[1]->cells[1] = new html_table_cell($event->description); | |
316 | $table->data[1]->cells[1]->attributes['class'] .= ' description'; | |
317 | if (isset($event->cssclass)) { | |
318 | $table->data[1]->cells[1]->attributes['class'] .= ' '.$event->cssclass; | |
319 | } | |
320 | ||
321 | if (calendar_edit_event_allowed($event) && $showactions) { | |
322 | if (empty($event->cmid)) { | |
323 | $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action'=>'edit', 'id'=>$event->id)); | |
324 | $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->id)); | |
325 | if (!empty($event->calendarcourseid)) { | |
326 | $editlink->param('course', $event->calendarcourseid); | |
327 | $deletelink->param('course', $event->calendarcourseid); | |
328 | } | |
329 | } else { | |
330 | $editlink = new moodle_url('/course/mod.php', array('update'=>$event->cmid, 'return'=>true, 'sesskey'=>sesskey())); | |
331 | $deletelink = null; | |
332 | } | |
333 | ||
334 | $commands = html_writer::start_tag('div', array('class'=>'commands')); | |
335 | $commands .= html_writer::start_tag('a', array('href'=>$editlink)); | |
336 | $commands .= html_writer::empty_tag('img', array('src'=>$this->output->pix_url('t/edit'), 'alt'=>get_string('tt_editevent', 'calendar'), 'title'=>get_string('tt_editevent', 'calendar'))); | |
337 | $commands .= html_writer::end_tag('a'); | |
338 | if ($deletelink != null) { | |
339 | $commands .= html_writer::start_tag('a', array('href'=>$deletelink)); | |
340 | $commands .= html_writer::empty_tag('img', array('src'=>$this->output->pix_url('t/delete'), 'alt'=>get_string('tt_deleteevent', 'calendar'), 'title'=>get_string('tt_deleteevent', 'calendar'))); | |
341 | $commands .= html_writer::end_tag('a'); | |
342 | } | |
343 | $commands .= html_writer::end_tag('div'); | |
344 | $table->data[1]->cells[1]->text .= $commands; | |
345 | } | |
346 | return html_writer::table($table); | |
347 | } | |
348 | ||
349 | /** | |
350 | * Displays a month in detail | |
351 | * | |
36dc3b71 SH |
352 | * @param calendar_information $calendar |
353 | * @return string | |
354 | */ | |
797cedc7 SH |
355 | public function show_month_detailed(calendar_information $calendar, moodle_url $returnurl = null) { |
356 | global $CFG; | |
357 | ||
358 | if (empty($returnurl)) { | |
359 | $returnurl = $this->page->url; | |
360 | } | |
6be8c6b7 | 361 | |
36dc3b71 | 362 | $date = usergetdate(time()); |
36dc3b71 SH |
363 | |
364 | $display = new stdClass; | |
365 | $display->minwday = get_user_preferences('calendar_startwday', calendar_get_starting_weekday()); | |
366 | $display->maxwday = $display->minwday + 6; | |
ac5e0418 SH |
367 | $display->thismonth = ($date['mon'] == $calendar->month); |
368 | $display->maxdays = calendar_days_in_month($calendar->month, $calendar->year); | |
36dc3b71 SH |
369 | |
370 | $startwday = 0; | |
371 | if (get_user_timezone_offset() < 99) { | |
372 | // We 'll keep these values as GMT here, and offset them when the time comes to query the db | |
ac5e0418 SH |
373 | $display->tstart = gmmktime(0, 0, 0, $calendar->month, 1, $calendar->year); // This is GMT |
374 | $display->tend = gmmktime(23, 59, 59, $calendar->month, $display->maxdays, $calendar->year); // GMT | |
36dc3b71 SH |
375 | $startwday = gmdate('w', $display->tstart); // $display->tstart is already GMT, so don't use date(): messes with server's TZ |
376 | } else { | |
377 | // no timezone info specified | |
ac5e0418 SH |
378 | $display->tstart = mktime(0, 0, 0, $calendar->month, 1, $calendar->year); |
379 | $display->tend = mktime(23, 59, 59, $calendar->month, $display->maxdays, $calendar->year); | |
36dc3b71 SH |
380 | $startwday = date('w', $display->tstart); // $display->tstart not necessarily GMT, so use date() |
381 | } | |
382 | ||
383 | // Align the starting weekday to fall in our display range | |
384 | if ($startwday < $display->minwday) { | |
385 | $startwday += 7; | |
386 | } | |
387 | ||
388 | // Get events from database | |
389 | $events = calendar_get_events(usertime($display->tstart), usertime($display->tend), $calendar->users, $calendar->groups, $calendar->courses); | |
390 | if (!empty($events)) { | |
391 | foreach($events as $eventid => $event) { | |
392 | $event = new calendar_event($event); | |
393 | if (!empty($event->modulename)) { | |
394 | $cm = get_coursemodule_from_instance($event->modulename, $event->instance); | |
395 | if (!groups_course_module_visible($cm)) { | |
396 | unset($events[$eventid]); | |
397 | } | |
398 | } | |
399 | } | |
400 | } | |
401 | ||
402 | // Extract information: events vs. time | |
ac5e0418 | 403 | calendar_events_by_day($events, $calendar->month, $calendar->year, $eventsbyday, $durationbyday, $typesbyday, $calendar->courses); |
36dc3b71 SH |
404 | |
405 | $output = html_writer::start_tag('div', array('class'=>'header')); | |
797cedc7 SH |
406 | if (calendar_user_can_add_event($calendar->course)) { |
407 | $output .= $this->add_event_button($calendar->course->id, null, $calendar->month, $calendar->year); | |
36dc3b71 | 408 | } |
797cedc7 | 409 | $output .= get_string('detailedmonthview', 'calendar').': '.$this->course_filter_selector($returnurl); |
36dc3b71 SH |
410 | $output .= html_writer::end_tag('div', array('class'=>'header')); |
411 | // Controls | |
2bf448ca | 412 | $output .= html_writer::tag('div', calendar_top_controls('month', array('id' => $calendar->courseid, 'm' => $calendar->month, 'y' => $calendar->year)), array('class'=>'controls')); |
36dc3b71 | 413 | |
797cedc7 SH |
414 | $days = calendar_get_days(); |
415 | ||
36dc3b71 SH |
416 | $table = new html_table(); |
417 | $table->attributes = array('class'=>'calendarmonth calendartable'); | |
418 | $table->data = array(); | |
419 | ||
420 | $header = new html_table_row(); | |
421 | $header->attributes = array('class'=>'weekdays'); | |
422 | $header->cells = array(); | |
423 | for($i = $display->minwday; $i <= $display->maxwday; ++$i) { | |
424 | // This uses the % operator to get the correct weekday no matter what shift we have | |
425 | // applied to the $display->minwday : $display->maxwday range from the default 0 : 6 | |
797cedc7 | 426 | $cell = new html_table_cell(get_string($days[$i % 7], 'calendar')); |
36dc3b71 SH |
427 | $cell->header = true; |
428 | $header->cells[] = $cell; | |
429 | } | |
430 | ||
431 | // For the table display. $week is the row; $dayweek is the column. | |
432 | $week = 1; | |
433 | $dayweek = $startwday; | |
434 | ||
435 | $row = new html_table_row(array()); | |
2953ad13 JI |
436 | |
437 | // Create an array of all the week days. | |
438 | $wdays = array(0 => '<strong>'. get_string('sunday', 'calendar'). '</strong>', | |
439 | 1 => '<strong>'. get_string('monday', 'calendar'). '</strong>', | |
440 | 2 => '<strong>'. get_string('tuesday', 'calendar'). '</strong>', | |
441 | 3 => '<strong>'. get_string('wednesday', 'calendar'). '</strong>', | |
442 | 4 => '<strong>'. get_string('thursday', 'calendar'). '</strong>', | |
443 | 5 => '<strong>'. get_string('friday', 'calendar'). '</strong>', | |
444 | 6 => '<strong>'. get_string('saturday', 'calendar'). '</strong>'); | |
445 | ||
446 | // Loop only if the day offset is greater than 0. | |
447 | // This loop involves shifting the days around until the desired start day | |
448 | // is at the start of the array. | |
449 | $daycount = 0; | |
450 | while ($display->minwday > $daycount++) { | |
451 | $wdays_end = array_shift($wdays); | |
452 | array_push($wdays, $wdays_end); | |
453 | } | |
454 | ||
455 | // Now we set the (modified) array to the table cells to be displayed. | |
456 | $row->cells = $wdays; | |
457 | $table->data[] = $row; | |
458 | ||
459 | $row = new html_table_row(array()); | |
460 | ||
36dc3b71 SH |
461 | // Paddding (the first week may have blank days in the beginning) |
462 | for($i = $display->minwday; $i < $startwday; ++$i) { | |
463 | $cell = new html_table_cell(' '); | |
464 | $cell->attributes = array('class'=>'nottoday'); | |
465 | $row->cells[] = $cell; | |
466 | } | |
467 | ||
468 | // Now display all the calendar | |
797cedc7 SH |
469 | $weekend = CALENDAR_DEFAULT_WEEKEND; |
470 | if (isset($CFG->calendar_weekend)) { | |
471 | $weekend = intval($CFG->calendar_weekend); | |
472 | } | |
473 | ||
36dc3b71 SH |
474 | for ($calendar->day = 1; $calendar->day <= $display->maxdays; ++$calendar->day, ++$dayweek) { |
475 | if($dayweek > $display->maxwday) { | |
476 | // We need to change week (table row) | |
477 | $table->data[] = $row; | |
478 | $row = new html_table_row(array()); | |
479 | $dayweek = $display->minwday; | |
480 | ++$week; | |
481 | } | |
482 | ||
483 | // Reset vars | |
484 | $cell = new html_table_cell(); | |
0f927f1e | 485 | $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'day', 'course'=>$calendar->courseid)), $calendar->day, $calendar->month, $calendar->year); |
36dc3b71 SH |
486 | |
487 | $cellclasses = array(); | |
488 | ||
797cedc7 | 489 | if ($weekend & (1 << ($dayweek % 7))) { |
36dc3b71 SH |
490 | // Weekend. This is true no matter what the exact range is. |
491 | $cellclasses[] = 'weekend'; | |
492 | } | |
493 | ||
494 | // Special visual fx if an event is defined | |
495 | if (isset($eventsbyday[$calendar->day])) { | |
496 | if(count($eventsbyday[$calendar->day]) == 1) { | |
497 | $title = get_string('oneevent', 'calendar'); | |
498 | } else { | |
499 | $title = get_string('manyevents', 'calendar', count($eventsbyday[$calendar->day])); | |
500 | } | |
501 | $cell->text = html_writer::tag('div', html_writer::link($dayhref, $calendar->day, array('title'=>$title)), array('class'=>'day')); | |
502 | } else { | |
503 | $cell->text = html_writer::tag('div', $calendar->day, array('class'=>'day')); | |
504 | } | |
505 | ||
506 | // Special visual fx if an event spans many days | |
507 | $durationclass = false; | |
508 | if (isset($typesbyday[$calendar->day]['durationglobal'])) { | |
509 | $durationclass = 'duration_global'; | |
510 | } else if (isset($typesbyday[$calendar->day]['durationcourse'])) { | |
511 | $durationclass = 'duration_course'; | |
512 | } else if (isset($typesbyday[$calendar->day]['durationgroup'])) { | |
513 | $durationclass = 'duration_group'; | |
514 | } else if (isset($typesbyday[$calendar->day]['durationuser'])) { | |
515 | $durationclass = 'duration_user'; | |
516 | } | |
517 | if ($durationclass) { | |
518 | $cellclasses[] = 'duration'; | |
519 | $cellclasses[] = $durationclass; | |
520 | } | |
521 | ||
522 | // Special visual fx for today | |
ac5e0418 | 523 | if($display->thismonth && $calendar->day == $calendar->day) { |
36dc3b71 SH |
524 | $cellclasses[] = 'today'; |
525 | } else { | |
526 | $cellclasses[] = 'nottoday'; | |
527 | } | |
528 | $cell->attributes = array('class'=>join(' ',$cellclasses)); | |
529 | ||
530 | if (isset($eventsbyday[$calendar->day])) { | |
531 | $cell->text .= html_writer::start_tag('ul', array('class'=>'events-new')); | |
532 | foreach($eventsbyday[$calendar->day] as $eventindex) { | |
533 | // If event has a class set then add it to the event <li> tag | |
534 | $attributes = array(); | |
535 | if (!empty($events[$eventindex]->class)) { | |
536 | $attributes['class'] = $events[$eventindex]->class; | |
537 | } | |
0f927f1e SH |
538 | $dayhref->set_anchor('event_'.$events[$eventindex]->id); |
539 | $link = html_writer::link($dayhref, format_string($events[$eventindex]->name, true)); | |
36dc3b71 SH |
540 | $cell->text .= html_writer::tag('li', $link, $attributes); |
541 | } | |
542 | $cell->text .= html_writer::end_tag('ul'); | |
543 | } | |
544 | if (isset($durationbyday[$calendar->day])) { | |
545 | $cell->text .= html_writer::start_tag('ul', array('class'=>'events-underway')); | |
546 | foreach($durationbyday[$calendar->day] as $eventindex) { | |
547 | $cell->text .= html_writer::tag('li', '['.format_string($events[$eventindex]->name,true).']', array('class'=>'events-underway')); | |
548 | } | |
549 | $cell->text .= html_writer::end_tag('ul'); | |
550 | } | |
551 | $row->cells[] = $cell; | |
552 | } | |
553 | ||
554 | // Paddding (the last week may have blank days at the end) | |
555 | for($i = $dayweek; $i <= $display->maxwday; ++$i) { | |
556 | $cell = new html_table_cell(' '); | |
557 | $cell->attributes = array('class'=>'nottoday'); | |
558 | $row->cells[] = $cell; | |
559 | } | |
560 | $table->data[] = $row; | |
561 | $output .= html_writer::table($table); | |
562 | ||
563 | // OK, now for the filtering display | |
564 | $output .= $this->filter_selection_table($calendar); | |
565 | return $output; | |
566 | } | |
567 | ||
568 | /** | |
569 | * Displays a filter selection table | |
570 | * | |
571 | * @param calendar_information $calendar | |
572 | * @return string | |
573 | */ | |
797cedc7 | 574 | protected function filter_selection_table(calendar_information $calendar, moodle_url $returnurl = null) { |
36dc3b71 | 575 | global $SESSION; |
6be8c6b7 | 576 | |
797cedc7 SH |
577 | if ($returnurl === null) { |
578 | $returnurl = $this->page->url; | |
579 | } | |
580 | ||
36dc3b71 SH |
581 | $output = html_writer::start_tag('div', array('class'=>'filters')); |
582 | $output .= html_writer::start_tag('table'); | |
583 | $output .= html_writer::start_tag('tr'); | |
584 | ||
585 | // Global events | |
797cedc7 SH |
586 | $link = new moodle_url(CALENDAR_URL.'set.php', array('var' => 'showglobal', 'return' => $returnurl)); |
587 | if (calendar_show_event_type(CALENDAR_EVENT_GLOBAL)) { | |
36dc3b71 SH |
588 | $output .= html_writer::tag('td', '', array('class'=>'calendar_event_global', 'style'=>'width:8px;')); |
589 | $output .= html_writer::tag('td', html_writer::tag('strong', get_string('globalevents', 'calendar')).' '.get_string('shown', 'calendar').' ('.html_writer::link($link, get_string('clickhide', 'calendar')).')'); | |
590 | } else { | |
591 | $output .= html_writer::tag('td', '', array('style'=>'width:8px;')); | |
592 | $output .= html_writer::tag('td', html_writer::tag('strong', get_string('globalevents', 'calendar')).' '.get_string('hidden', 'calendar').' ('.html_writer::link($link, get_string('clickshow', 'calendar')).')'); | |
593 | } | |
594 | ||
595 | // Course events | |
797cedc7 SH |
596 | $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showcourses', 'return' => $returnurl)); |
597 | if (calendar_show_event_type(CALENDAR_EVENT_COURSE)) { | |
36dc3b71 SH |
598 | $output .= html_writer::tag('td', '', array('class'=>'calendar_event_course', 'style'=>'width:8px;')); |
599 | $output .= html_writer::tag('td', html_writer::tag('strong', get_string('courseevents', 'calendar')).' '.get_string('shown', 'calendar').' ('.html_writer::link($link, get_string('clickhide', 'calendar')).')'); | |
600 | } else { | |
601 | $output .= html_writer::tag('td', '', array('style'=>'width:8px;')); | |
602 | $output .= html_writer::tag('td', html_writer::tag('strong', get_string('courseevents', 'calendar')).' '.get_string('hidden', 'calendar').' ('.html_writer::link($link, get_string('clickshow', 'calendar')).')'); | |
603 | } | |
604 | $output .= html_writer::end_tag('tr'); | |
605 | ||
606 | if(isloggedin() && !isguestuser()) { | |
607 | $output .= html_writer::start_tag('tr'); | |
608 | // Group events | |
797cedc7 SH |
609 | $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showgroups', 'return' => $returnurl)); |
610 | if (calendar_show_event_type(CALENDAR_EVENT_GROUP)) { | |
36dc3b71 SH |
611 | $output .= html_writer::tag('td', '', array('class'=>'calendar_event_group', 'style'=>'width:8px;')); |
612 | $output .= html_writer::tag('td', html_writer::tag('strong', get_string('groupevents', 'calendar')).' '.get_string('shown', 'calendar').' ('.html_writer::link($link, get_string('clickhide', 'calendar')).')'); | |
613 | } else { | |
614 | $output .= html_writer::tag('td', '', array('style'=>'width:8px;')); | |
615 | $output .= html_writer::tag('td', html_writer::tag('strong', get_string('groupevents', 'calendar')).' '.get_string('hidden', 'calendar').' ('.html_writer::link($link, get_string('clickshow', 'calendar')).')'); | |
616 | } | |
617 | // User events | |
797cedc7 SH |
618 | $link = new moodle_url(CALENDAR_URL.'set.php', array('var'=>'showuser', 'return' => $returnurl)); |
619 | if (calendar_show_event_type(CALENDAR_EVENT_USER)) { | |
36dc3b71 SH |
620 | $output .= html_writer::tag('td', '', array('class'=>'calendar_event_user', 'style'=>'width:8px;')); |
621 | $output .= html_writer::tag('td', html_writer::tag('strong', get_string('userevents', 'calendar')).' '.get_string('shown', 'calendar').' ('.html_writer::link($link, get_string('clickhide', 'calendar')).')'); | |
622 | } else { | |
623 | $output .= html_writer::tag('td', '', array('style'=>'width:8px;')); | |
624 | $output .= html_writer::tag('td', html_writer::tag('strong', get_string('userevents', 'calendar')).' '.get_string('hidden', 'calendar').' ('.html_writer::link($link, get_string('clickshow', 'calendar')).')'); | |
625 | } | |
626 | $output .= html_writer::end_tag('tr'); | |
627 | } | |
628 | $output .= html_writer::end_tag('table'); | |
629 | $output .= html_writer::end_tag('div'); | |
630 | return $output; | |
631 | } | |
632 | ||
633 | /** | |
634 | * Displays upcoming events | |
635 | * | |
636 | * @param calendar_information $calendar | |
637 | * @param int $futuredays | |
638 | * @param int $maxevents | |
639 | * @return string | |
640 | */ | |
797cedc7 SH |
641 | public function show_upcoming_events(calendar_information $calendar, $futuredays, $maxevents, moodle_url $returnurl = null) { |
642 | ||
643 | if ($returnurl === null) { | |
644 | $returnurl = $this->page->url; | |
645 | } | |
646 | ||
36dc3b71 SH |
647 | $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, $futuredays, $maxevents); |
648 | ||
649 | $output = html_writer::start_tag('div', array('class'=>'header')); | |
797cedc7 SH |
650 | if (calendar_user_can_add_event($calendar->course)) { |
651 | $output .= $this->add_event_button($calendar->course->id); | |
36dc3b71 SH |
652 | } |
653 | $output .= html_writer::tag('label', get_string('upcomingevents', 'calendar'), array('for'=>'cal_course_flt_jump')); | |
797cedc7 | 654 | $output .= $this->course_filter_selector($returnurl); |
36dc3b71 SH |
655 | $output .= html_writer::end_tag('div'); |
656 | ||
657 | if ($events) { | |
658 | $output .= html_writer::start_tag('div', array('class'=>'eventlist')); | |
659 | foreach ($events as $event) { | |
660 | // Convert to calendar_event object so that we transform description | |
661 | // accordingly | |
662 | $event = new calendar_event($event); | |
484617d2 | 663 | $event->calendarcourseid = $calendar->courseid; |
36dc3b71 SH |
664 | $output .= $this->event($event); |
665 | } | |
666 | $output .= html_writer::end_tag('div'); | |
667 | } else { | |
668 | $output .= $this->output->heading(get_string('noupcomingevents', 'calendar')); | |
669 | } | |
670 | ||
671 | return $output; | |
672 | } | |
673 | ||
674 | /** | |
675 | * Displays a course filter selector | |
676 | * | |
677 | * @param array $getvars | |
678 | * @return string | |
679 | */ | |
797cedc7 | 680 | protected function course_filter_selector(moodle_url $returnurl, $label=null) { |
6be8c6b7 | 681 | global $USER, $SESSION, $CFG; |
36dc3b71 SH |
682 | |
683 | if (!isloggedin() or isguestuser()) { | |
684 | return ''; | |
685 | } | |
686 | ||
687 | if (has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM)) && !empty($CFG->calendar_adminseesall)) { | |
688 | $courses = get_courses('all', 'c.shortname','c.id,c.shortname'); | |
689 | } else { | |
690 | $courses = enrol_get_my_courses(); | |
691 | } | |
692 | ||
693 | unset($courses[SITEID]); | |
694 | ||
695 | $courseoptions = array(); | |
696 | $courseoptions[SITEID] = get_string('fulllistofcourses'); | |
697 | foreach ($courses as $course) { | |
698 | $courseoptions[$course->id] = format_string($course->shortname); | |
699 | } | |
700 | ||
797cedc7 SH |
701 | if ($this->page->course->id !== SITEID) { |
702 | $selected = $this->page->course->id; | |
36dc3b71 SH |
703 | } else { |
704 | $selected = ''; | |
705 | } | |
797cedc7 | 706 | $select = new single_select(new moodle_url(CALENDAR_URL.'set.php', array('return' => $returnurl, 'var' => 'setcourse')), 'id', $courseoptions, $selected, null); |
36dc3b71 | 707 | $select->class = 'cal_courses_flt'; |
91a774e2 SH |
708 | if ($label !== null) { |
709 | $select->label = $label; | |
710 | } | |
711 | return $this->output->render($select); | |
36dc3b71 SH |
712 | } |
713 | } |