46e6c569444d20e682b19365f6de0541b5d5e828
[moodle.git] / calendar / renderer.php
1 <?php
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/>.
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  */
26 if (!defined('MOODLE_INTERNAL')) {
27     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
28 }
30 /**
31  * The primary renderer for the calendar.
32  */
33 class core_calendar_renderer extends plugin_renderer_base {
35     /**
36      * Starts the standard layout for the page
37      *
38      * @return string
39      */
40     public function start_layout() {
41         return html_writer::start_tag('div', array('class'=>'maincalendar'));
42     }
44     /**
45      * Creates the remainder of the layout
46      *
47      * @return string
48      */
49     public function complete_layout() {
50         return html_writer::end_tag('div');
51     }
53     /**
54      * Produces the content for the filters block (pretend block)
55      *
56      * @param int $courseid
57      * @param int $day
58      * @param int $month
59      * @param int $year
60      * @param int $view
61      * @param int $courses
62      * @return string
63      */
64     public function fake_block_filters($courseid, $day, $month, $year, $view, $courses) {
65         $returnurl = $this->page->url;
66         $returnurl->param('course', $courseid);
67         return html_writer::tag('div', calendar_filter_controls($returnurl), array('class'=>'calendar_filters filters'));
68     }
70     /**
71      * Produces the content for the three months block (pretend block)
72      *
73      * This includes the previous month, the current month, and the next month
74      *
75      * @param calendar_information $calendar
76      * @return string
77      */
78     public function fake_block_threemonths(calendar_information $calendar) {
79         // Get the calendar type we are using.
80         $calendartype = \core_calendar\type_factory::get_calendar_instance();
82         $date = $calendartype->timestamp_to_date_array($calendar->time);
84         $prevmonth = calendar_sub_month($date['mon'], $date['year']);
85         $prevmonthtime = $calendartype->convert_to_gregorian($prevmonth[1], $prevmonth[0], 1);
86         $prevmonthtime = make_timestamp($prevmonthtime['year'], $prevmonthtime['month'], $prevmonthtime['day'],
87             $prevmonthtime['hour'], $prevmonthtime['minute']);
89         $nextmonth = calendar_add_month($date['mon'], $date['year']);
90         $nextmonthtime = $calendartype->convert_to_gregorian($nextmonth[1], $nextmonth[0], 1);
91         $nextmonthtime = make_timestamp($nextmonthtime['year'], $nextmonthtime['month'], $nextmonthtime['day'],
92             $nextmonthtime['hour'], $nextmonthtime['minute']);
94         $content  = html_writer::start_tag('div', array('class' => 'minicalendarblock'));
95         $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, false, false, 'display', $calendar->courseid, $prevmonthtime);
96         $content .= html_writer::end_tag('div');
97         $content .= html_writer::start_tag('div', array('class' => 'minicalendarblock'));
98         $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, false, false, 'display', $calendar->courseid, $calendar->time);
99         $content .= html_writer::end_tag('div');
100         $content .= html_writer::start_tag('div', array('class' => 'minicalendarblock'));
101         $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users, false, false, 'display', $calendar->courseid, $nextmonthtime);
102         $content .= html_writer::end_tag('div');
103         return $content;
104     }
106     /**
107      * Adds a pretent calendar block
108      *
109      * @param block_contents $bc
110      * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
111      */
112     public function add_pretend_calendar_block(block_contents $bc, $pos=BLOCK_POS_RIGHT) {
113         $this->page->blocks->add_fake_block($bc, $pos);
114     }
116     /**
117      * Creates a button to add a new event
118      *
119      * @param int $courseid
120      * @param int $day
121      * @param int $month
122      * @param int $year
123      * @param int $time the unixtime, used for multiple calendar support. The values $day,
124      *     $month and $year are kept for backwards compatibility.
125      * @return string
126      */
127     protected function add_event_button($courseid, $day = null, $month = null, $year = null, $time = null) {
128         // If a day, month and year were passed then convert it to a timestamp. If these were passed
129         // then we can assume the day, month and year are passed as Gregorian, as no where in core
130         // should we be passing these values rather than the time. This is done for BC.
131         if (!empty($day) && !empty($month) && !empty($year)) {
132             if (checkdate($month, $day, $year)) {
133                 $time = make_timestamp($year, $month, $day);
134             } else {
135                 $time = time();
136             }
137         } else if (empty($time)) {
138             $time = time();
139         }
141         $output = html_writer::start_tag('div', array('class'=>'buttons'));
142         $output .= html_writer::start_tag('form', array('action' => CALENDAR_URL . 'event.php', 'method' => 'get'));
143         $output .= html_writer::start_tag('div');
144         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name' => 'action', 'value' => 'new'));
145         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name' => 'course', 'value' => $courseid));
146         $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name' => 'time', 'value' => $time));
147         $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value' => get_string('newevent', 'calendar')));
148         $output .= html_writer::end_tag('div');
149         $output .= html_writer::end_tag('form');
150         $output .= html_writer::end_tag('div');
151         return $output;
152     }
154     /**
155      * Displays the calendar for a single day
156      *
157      * @param calendar_information $calendar
158      * @return string
159      */
160     public function show_day(calendar_information $calendar, moodle_url $returnurl = null) {
162         if ($returnurl === null) {
163             $returnurl = $this->page->url;
164         }
166         $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, 1, 100, $calendar->timestamp_today());
168         $output  = html_writer::start_tag('div', array('class'=>'header'));
169         $output .= $this->course_filter_selector($returnurl, get_string('dayviewfor', 'calendar'));
170         if (calendar_user_can_add_event($calendar->course)) {
171             $output .= $this->add_event_button($calendar->course->id, 0, 0, 0, $calendar->time);
172         }
173         $output .= html_writer::end_tag('div');
174         // Controls
175         $output .= html_writer::tag('div', calendar_top_controls('day', array('id' => $calendar->courseid, 'time' => $calendar->time)), array('class'=>'controls'));
177         if (empty($events)) {
178             // There is nothing to display today.
179             $output .= html_writer::span(get_string('daywithnoevents', 'calendar'), 'calendar-information calendar-no-results');
180         } else {
181             $output .= html_writer::start_tag('div', array('class' => 'eventlist'));
182             $underway = array();
183             // First, print details about events that start today
184             foreach ($events as $event) {
185                 $event = new calendar_event($event);
186                 $event->calendarcourseid = $calendar->courseid;
187                 if ($event->timestart >= $calendar->timestamp_today() && $event->timestart <= $calendar->timestamp_tomorrow()-1) {  // Print it now
188                     $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
189                     $output .= $this->event($event);
190                 } else {                                                                 // Save this for later
191                     $underway[] = $event;
192                 }
193             }
195             // Then, show a list of all events that just span this day
196             if (!empty($underway)) {
197                 $output .= html_writer::span(get_string('spanningevents', 'calendar'),
198                     'calendar-information calendar-span-multiple-days');
199                 foreach ($underway as $event) {
200                     $event->time = calendar_format_event_time($event, time(), null, false, $calendar->timestamp_today());
201                     $output .= $this->event($event);
202                 }
203             }
205             $output .= html_writer::end_tag('div');
206         }
208         return $output;
209     }
211     /**
212      * Displays an event
213      *
214      * @param calendar_event $event
215      * @param bool $showactions
216      * @return string
217      */
218     public function event(calendar_event $event, $showactions=true) {
219         global $CFG;
221         $event = calendar_add_event_metadata($event);
222         $context = $event->context;
223         $output = '';
225         if (!empty($event->icon)) {
226             $output .= $event->icon;
227         } else {
228             $output .= $this->output->spacer(array('height' => 16, 'width' => 16));
229         }
231         if (!empty($event->referer)) {
232             $output .= $this->output->heading($event->referer, 3, array('class' => 'referer'));
233         } else {
234             $output .= $this->output->heading(
235                 format_string($event->name, false, array('context' => $context)),
236                 3,
237                 array('class' => 'name')
238             );
239         }
240         if (!empty($event->courselink)) {
241             $output .= html_writer::tag('div', $event->courselink, array('class' => 'course'));
242         }
243         // Show subscription source if needed.
244         if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
245             if (!empty($event->subscription->url)) {
246                 $source = html_writer::link($event->subscription->url, get_string('subsource', 'calendar', $event->subscription));
247             } else {
248                 // File based ical.
249                 $source = get_string('subsource', 'calendar', $event->subscription);
250             }
251             $output .= html_writer::tag('div', $source, array('class' => 'subscription'));
252         }
253         if (!empty($event->time)) {
254             $output .= html_writer::tag('span', $event->time, array('class' => 'date'));
255         } else {
256             $output .= html_writer::tag('span', calendar_time_representation($event->timestart), array('class' => 'date'));
257         }
259         $eventdetailshtml = '';
260         $eventdetailsclasses = '';
262         $eventdetailshtml .= format_text($event->description, $event->format, array('context' => $context));
263         $eventdetailsclasses .= 'description';
264         if (isset($event->cssclass)) {
265             $eventdetailsclasses .= ' '.$event->cssclass;
266         }
268         $output .= html_writer::tag('div', $eventdetailshtml, array('class' => $eventdetailsclasses));
270         if (calendar_edit_event_allowed($event) && $showactions) {
271             if (empty($event->cmid)) {
272                 $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action'=>'edit', 'id'=>$event->id));
273                 $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->id));
274                 if (!empty($event->calendarcourseid)) {
275                     $editlink->param('course', $event->calendarcourseid);
276                     $deletelink->param('course', $event->calendarcourseid);
277                 }
278             } else {
279                 $editlink = new moodle_url('/course/mod.php', array('update'=>$event->cmid, 'return'=>true, 'sesskey'=>sesskey()));
280                 $deletelink = null;
281             }
283             $commands  = html_writer::start_tag('div', array('class'=>'commands'));
284             $commands .= html_writer::start_tag('a', array('href'=>$editlink));
285             $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')));
286             $commands .= html_writer::end_tag('a');
287             if ($deletelink != null) {
288                 $commands .= html_writer::start_tag('a', array('href'=>$deletelink));
289                 $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')));
290                 $commands .= html_writer::end_tag('a');
291             }
292             $commands .= html_writer::end_tag('div');
293             $output .= $commands;
294         }
295         return html_writer::tag('div', $output , array('class' => 'event', 'id' => 'event_' . $event->id));
296     }
298     /**
299      * Displays a month in detail
300      *
301      * @param calendar_information $calendar
302      * @param moodle_url $returnurl the url to return to
303      * @return string
304      */
305     public function show_month_detailed(calendar_information $calendar, moodle_url $returnurl  = null) {
306         global $CFG;
308         if (empty($returnurl)) {
309             $returnurl = $this->page->url;
310         }
312         // Get the calendar type we are using.
313         $calendartype = \core_calendar\type_factory::get_calendar_instance();
315         // Store the display settings.
316         $display = new stdClass;
317         $display->thismonth = false;
319         // Get the specified date in the calendar type being used.
320         $date = $calendartype->timestamp_to_date_array($calendar->time);
321         $thisdate = $calendartype->timestamp_to_date_array(time());
322         if ($date['mon'] == $thisdate['mon'] && $date['year'] == $thisdate['year']) {
323             $display->thismonth = true;
324             $date = $thisdate;
325             $calendar->time = time();
326         }
328         // Get Gregorian date for the start of the month.
329         $gregoriandate = $calendartype->convert_to_gregorian($date['year'], $date['mon'], 1);
330         // Store the gregorian date values to be used later.
331         list($gy, $gm, $gd, $gh, $gmin) = array($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
332             $gregoriandate['hour'], $gregoriandate['minute']);
334         // Get the starting week day for this month.
335         $startwday = dayofweek(1, $date['mon'], $date['year']);
336         // Get the days in a week.
337         $daynames = calendar_get_days();
338         // Store the number of days in a week.
339         $numberofdaysinweek = $calendartype->get_num_weekdays();
341         $display->minwday = calendar_get_starting_weekday();
342         $display->maxwday = $display->minwday + ($numberofdaysinweek - 1);
343         $display->maxdays = calendar_days_in_month($date['mon'], $date['year']);
345         // These are used for DB queries, so we want unixtime, so we need to use Gregorian dates.
346         $display->tstart = make_timestamp($gy, $gm, $gd, $gh, $gmin, 0);
347         $display->tend = $display->tstart + ($display->maxdays * DAYSECS) - 1;
349         // Align the starting weekday to fall in our display range
350         // This is simple, not foolproof.
351         if ($startwday < $display->minwday) {
352             $startwday += $numberofdaysinweek;
353         }
355         // Get events from database
356         $events = calendar_get_events($display->tstart, $display->tend, $calendar->users, $calendar->groups, $calendar->courses);
357         if (!empty($events)) {
358             foreach($events as $eventid => $event) {
359                 $event = new calendar_event($event);
360                 if (!empty($event->modulename)) {
361                     $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
362                     if (!\core_availability\info_module::is_user_visible($cm, 0, false)) {
363                         unset($events[$eventid]);
364                     }
365                 }
366             }
367         }
369         // Extract information: events vs. time
370         calendar_events_by_day($events, $date['mon'], $date['year'], $eventsbyday, $durationbyday, $typesbyday, $calendar->courses);
372         $output  = html_writer::start_tag('div', array('class'=>'header'));
373         $output .= $this->course_filter_selector($returnurl, get_string('detailedmonthviewfor', 'calendar'));
374         if (calendar_user_can_add_event($calendar->course)) {
375             $output .= $this->add_event_button($calendar->course->id, 0, 0, 0, $calendar->time);
376         }
377         $output .= html_writer::end_tag('div', array('class'=>'header'));
378         // Controls
379         $output .= html_writer::tag('div', calendar_top_controls('month', array('id' => $calendar->courseid, 'time' => $calendar->time)), array('class' => 'controls'));
381         $table = new html_table();
382         $table->attributes = array('class'=>'calendarmonth calendartable');
383         $table->summary = get_string('calendarheading', 'calendar', userdate($calendar->time, get_string('strftimemonthyear')));
384         $table->data = array();
386         // Get the day names as the header.
387         $header = array();
388         for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
389             $header[] = $daynames[$i % $numberofdaysinweek]['shortname'];
390         }
391         $table->head = $header;
393         // For the table display. $week is the row; $dayweek is the column.
394         $week = 1;
395         $dayweek = $startwday;
397         $row = new html_table_row(array());
399         // Paddding (the first week may have blank days in the beginning)
400         for($i = $display->minwday; $i < $startwday; ++$i) {
401             $cell = new html_table_cell('&nbsp;');
402             $cell->attributes = array('class'=>'nottoday dayblank');
403             $row->cells[] = $cell;
404         }
406         // Now display all the calendar
407         $weekend = CALENDAR_DEFAULT_WEEKEND;
408         if (isset($CFG->calendar_weekend)) {
409             $weekend = intval($CFG->calendar_weekend);
410         }
412         $daytime = strtotime('-1 day', $display->tstart);
413         for ($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
414             $daytime = strtotime('+1 day', $daytime);
415             if($dayweek > $display->maxwday) {
416                 // We need to change week (table row)
417                 $table->data[] = $row;
418                 $row = new html_table_row(array());
419                 $dayweek = $display->minwday;
420                 ++$week;
421             }
423             // Reset vars
424             $cell = new html_table_cell();
425             $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view' => 'day', 'course' => $calendar->courseid)), 0, 0, 0, $daytime);
427             $cellclasses = array();
429             if ($weekend & (1 << ($dayweek % $numberofdaysinweek))) {
430                 // Weekend. This is true no matter what the exact range is.
431                 $cellclasses[] = 'weekend';
432             }
434             // Special visual fx if an event is defined
435             if (isset($eventsbyday[$day])) {
436                 if(count($eventsbyday[$day]) == 1) {
437                     $title = get_string('oneevent', 'calendar');
438                 } else {
439                     $title = get_string('manyevents', 'calendar', count($eventsbyday[$day]));
440                 }
441                 $cell->text = html_writer::tag('div', html_writer::link($dayhref, $day, array('title'=>$title)), array('class'=>'day'));
442             } else {
443                 $cell->text = html_writer::tag('div', $day, array('class'=>'day'));
444             }
446             // Special visual fx if an event spans many days
447             $durationclass = false;
448             if (isset($typesbyday[$day]['durationglobal'])) {
449                 $durationclass = 'duration_global';
450             } else if (isset($typesbyday[$day]['durationcourse'])) {
451                 $durationclass = 'duration_course';
452             } else if (isset($typesbyday[$day]['durationgroup'])) {
453                 $durationclass = 'duration_group';
454             } else if (isset($typesbyday[$day]['durationuser'])) {
455                 $durationclass = 'duration_user';
456             }
457             if ($durationclass) {
458                 $cellclasses[] = 'duration';
459                 $cellclasses[] = $durationclass;
460             }
462             // Special visual fx for today
463             if ($display->thismonth && $day == $date['mday']) {
464                 $cellclasses[] = 'day today';
465             } else {
466                 $cellclasses[] = 'day nottoday';
467             }
468             $cell->attributes = array('class'=>join(' ',$cellclasses));
470             if (isset($eventsbyday[$day])) {
471                 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-new'));
472                 foreach($eventsbyday[$day] as $eventindex) {
473                     // If event has a class set then add it to the event <li> tag
474                     $attributes = array();
475                     if (!empty($events[$eventindex]->class)) {
476                         $attributes['class'] = $events[$eventindex]->class;
477                     }
478                     $dayhref->set_anchor('event_'.$events[$eventindex]->id);
479                     $link = html_writer::link($dayhref, format_string($events[$eventindex]->name, true));
480                     $cell->text .= html_writer::tag('li', $link, $attributes);
481                 }
482                 $cell->text .= html_writer::end_tag('ul');
483             }
484             if (isset($durationbyday[$day])) {
485                 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-underway'));
486                 foreach($durationbyday[$day] as $eventindex) {
487                     $cell->text .= html_writer::tag('li', '['.format_string($events[$eventindex]->name,true).']', array('class'=>'events-underway'));
488                 }
489                 $cell->text .= html_writer::end_tag('ul');
490             }
491             $row->cells[] = $cell;
492         }
494         // Paddding (the last week may have blank days at the end)
495         for($i = $dayweek; $i <= $display->maxwday; ++$i) {
496             $cell = new html_table_cell('&nbsp;');
497             $cell->attributes = array('class'=>'nottoday dayblank');
498             $row->cells[] = $cell;
499         }
500         $table->data[] = $row;
501         $output .= html_writer::table($table);
503         return $output;
504     }
506     /**
507      * Displays upcoming events
508      *
509      * @param calendar_information $calendar
510      * @param int $futuredays
511      * @param int $maxevents
512      * @return string
513      */
514     public function show_upcoming_events(calendar_information $calendar, $futuredays, $maxevents, moodle_url $returnurl = null) {
516         if ($returnurl === null) {
517             $returnurl = $this->page->url;
518         }
520         $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users, $futuredays, $maxevents);
522         $output  = html_writer::start_tag('div', array('class'=>'header'));
523         $output .= $this->course_filter_selector($returnurl, get_string('upcomingeventsfor', 'calendar'));
524         if (calendar_user_can_add_event($calendar->course)) {
525             $output .= $this->add_event_button($calendar->course->id);
526         }
527         $output .= html_writer::end_tag('div');
529         if ($events) {
530             $output .= html_writer::start_tag('div', array('class' => 'eventlist'));
531             foreach ($events as $event) {
532                 // Convert to calendar_event object so that we transform description
533                 // accordingly
534                 $event = new calendar_event($event);
535                 $event->calendarcourseid = $calendar->courseid;
536                 $output .= $this->event($event);
537             }
538             $output .= html_writer::end_tag('div');
539         } else {
540             $output .= html_writer::span(get_string('noupcomingevents', 'calendar'), 'calendar-information calendar-no-results');
541         }
543         return $output;
544     }
546     /**
547      * Displays a course filter selector
548      *
549      * @param moodle_url $returnurl The URL that the user should be taken too upon selecting a course.
550      * @param string $label The label to use for the course select.
551      * @return string
552      */
553     protected function course_filter_selector(moodle_url $returnurl, $label=null) {
554         global $USER, $SESSION, $CFG;
556         if (!isloggedin() or isguestuser()) {
557             return '';
558         }
560         if (has_capability('moodle/calendar:manageentries', context_system::instance()) && !empty($CFG->calendar_adminseesall)) {
561             $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
562         } else {
563             $courses = enrol_get_my_courses();
564         }
566         unset($courses[SITEID]);
568         $courseoptions = array();
569         $courseoptions[SITEID] = get_string('fulllistofcourses');
570         foreach ($courses as $course) {
571             $coursecontext = context_course::instance($course->id);
572             $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
573         }
575         if ($this->page->course->id !== SITEID) {
576             $selected = $this->page->course->id;
577         } else {
578             $selected = '';
579         }
580         $courseurl = new moodle_url($returnurl);
581         $courseurl->remove_params('course');
582         $select = new single_select($courseurl, 'course', $courseoptions, $selected, null);
583         $select->class = 'cal_courses_flt';
584         if ($label !== null) {
585             $select->set_label($label);
586         } else {
587             $select->set_label(get_string('listofcourses'), array('class' => 'accesshide'));
588         }
589         return $this->output->render($select);
590     }
592     /**
593      * Renders a table containing information about calendar subscriptions.
594      *
595      * @param int $courseid
596      * @param array $subscriptions
597      * @param string $importresults
598      * @return string
599      */
600     public function subscription_details($courseid, $subscriptions, $importresults = '') {
601         $table = new html_table();
602         $table->head  = array(
603             get_string('colcalendar', 'calendar'),
604             get_string('collastupdated', 'calendar'),
605             get_string('eventkind', 'calendar'),
606             get_string('colpoll', 'calendar'),
607             get_string('colactions', 'calendar')
608         );
609         $table->align = array('left', 'left', 'left', 'center');
610         $table->width = '100%';
611         $table->data  = array();
613         if (empty($subscriptions)) {
614             $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
615             $cell->colspan = 4;
616             $table->data[] = new html_table_row(array($cell));
617         }
618         $strnever = new lang_string('never', 'calendar');
619         foreach ($subscriptions as $sub) {
620             $label = $sub->name;
621             if (!empty($sub->url)) {
622                 $label = html_writer::link($sub->url, $label);
623             }
624             if (empty($sub->lastupdated)) {
625                 $lastupdated = $strnever->out();
626             } else {
627                 $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
628             }
630             $cell = new html_table_cell($this->subscription_action_form($sub, $courseid));
631             $cell->colspan = 2;
632             $type = $sub->eventtype . 'events';
634             $table->data[] = new html_table_row(array(
635                 new html_table_cell($label),
636                 new html_table_cell($lastupdated),
637                 new html_table_cell(get_string($type, 'calendar')),
638                 $cell
639             ));
640         }
642         $out  = $this->output->box_start('generalbox calendarsubs');
644         $out .= $importresults;
645         $out .= html_writer::table($table);
646         $out .= $this->output->box_end();
647         return $out;
648     }
650     /**
651      * Creates a form to perform actions on a given subscription.
652      *
653      * @param stdClass $subscription
654      * @param int $courseid
655      * @return string
656      */
657     protected function subscription_action_form($subscription, $courseid) {
658         // Assemble form for the subscription row.
659         $html = html_writer::start_tag('form', array('action' => new moodle_url('/calendar/managesubscriptions.php'), 'method' => 'post'));
660         if (empty($subscription->url)) {
661             // Don't update an iCal file, which has no URL.
662             $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pollinterval', 'value' => '0'));
663         } else {
664             // Assemble pollinterval control.
665             $html .= html_writer::start_tag('div', array('style' => 'float:left;'));
666             $html .= html_writer::start_tag('select', array('name' => 'pollinterval'));
667             foreach (calendar_get_pollinterval_choices() as $k => $v) {
668                 $attributes = array();
669                 if ($k == $subscription->pollinterval) {
670                     $attributes['selected'] = 'selected';
671                 }
672                 $attributes['value'] = $k;
673                 $html .= html_writer::tag('option', $v, $attributes);
674             }
675             $html .= html_writer::end_tag('select');
676             $html .= html_writer::end_tag('div');
677         }
678         $html .= html_writer::start_tag('div', array('style' => 'float:right;'));
679         $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
680         $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'course', 'value' => $courseid));
681         $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
682         if (!empty($subscription->url)) {
683             $html .= html_writer::tag('button', get_string('update'), array('type'  => 'submit', 'name' => 'action',
684                                                                             'value' => CALENDAR_SUBSCRIPTION_UPDATE));
685         }
686         $html .= html_writer::tag('button', get_string('remove'), array('type'  => 'submit', 'name' => 'action',
687                                                                         'value' => CALENDAR_SUBSCRIPTION_REMOVE));
688         $html .= html_writer::end_tag('div');
689         $html .= html_writer::end_tag('form');
690         return $html;
691     }