940be31519d897eb23259d6e91fc70f1ba2aa335
[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', ['data-region' => 'calendar', '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),
68             array('class' => 'calendar_filters filters'));
69     }
71     /**
72      * Produces the content for the three months block (pretend block)
73      *
74      * This includes the previous month, the current month, and the next month
75      *
76      * @param calendar_information $calendar
77      * @return string
78      */
79     public function fake_block_threemonths(calendar_information $calendar) {
80         // Get the calendar type we are using.
81         $calendartype = \core_calendar\type_factory::get_calendar_instance();
83         $date = $calendartype->timestamp_to_date_array($calendar->time);
85         $prevmonth = calendar_sub_month($date['mon'], $date['year']);
86         $prevmonthtime = $calendartype->convert_to_gregorian($prevmonth[1], $prevmonth[0], 1);
87         $prevmonthtime = make_timestamp($prevmonthtime['year'], $prevmonthtime['month'], $prevmonthtime['day'],
88             $prevmonthtime['hour'], $prevmonthtime['minute']);
90         $nextmonth = calendar_add_month($date['mon'], $date['year']);
91         $nextmonthtime = $calendartype->convert_to_gregorian($nextmonth[1], $nextmonth[0], 1);
92         $nextmonthtime = make_timestamp($nextmonthtime['year'], $nextmonthtime['month'], $nextmonthtime['day'],
93             $nextmonthtime['hour'], $nextmonthtime['minute']);
95         $content  = html_writer::start_tag('div', array('class' => 'minicalendarblock'));
96         $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users,
97             false, false, 'display', $calendar->courseid, $prevmonthtime);
98         $content .= html_writer::end_tag('div');
99         $content .= html_writer::start_tag('div', array('class' => 'minicalendarblock'));
100         $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users,
101             false, false, 'display', $calendar->courseid, $calendar->time);
102         $content .= html_writer::end_tag('div');
103         $content .= html_writer::start_tag('div', array('class' => 'minicalendarblock'));
104         $content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users,
105             false, false, 'display', $calendar->courseid, $nextmonthtime);
106         $content .= html_writer::end_tag('div');
107         return $content;
108     }
110     /**
111      * Adds a pretent calendar block
112      *
113      * @param block_contents $bc
114      * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
115      */
116     public function add_pretend_calendar_block(block_contents $bc, $pos=BLOCK_POS_RIGHT) {
117         $this->page->blocks->add_fake_block($bc, $pos);
118     }
120     /**
121      * Creates a button to add a new event
122      *
123      * @param int $courseid
124      * @param int $day
125      * @param int $month
126      * @param int $year
127      * @param int $time the unixtime, used for multiple calendar support. The values $day,
128      *     $month and $year are kept for backwards compatibility.
129      * @return string
130      */
131     protected function add_event_button($courseid, $day = null, $month = null, $year = null, $time = null) {
132         // If a day, month and year were passed then convert it to a timestamp. If these were passed
133         // then we can assume the day, month and year are passed as Gregorian, as no where in core
134         // should we be passing these values rather than the time. This is done for BC.
135         if (!empty($day) && !empty($month) && !empty($year)) {
136             if (checkdate($month, $day, $year)) {
137                 $time = make_timestamp($year, $month, $day);
138             } else {
139                 $time = time();
140             }
141         } else if (empty($time)) {
142             $time = time();
143         }
145         $coursecontext = \context_course::instance($courseid);
146         $attributes = [
147             'class' => 'btn btn-secondary pull-xs-right pull-right',
148             'data-context-id' => $coursecontext->id,
149             'data-action' => 'new-event-button'
150         ];
151         return html_writer::tag('button', get_string('newevent', 'calendar'), $attributes);
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,
167             1, 100, $calendar->timestamp_today());
169         $output  = html_writer::start_tag('div', array('class'=>'header'));
170         $output .= $this->course_filter_selector($returnurl, get_string('dayviewfor', 'calendar'));
171         if (calendar_user_can_add_event($calendar->course)) {
172             $output .= $this->add_event_button($calendar->course->id, 0, 0, 0, $calendar->time);
173         }
174         $output .= html_writer::end_tag('div');
175         // Controls
176         $output .= html_writer::tag('div', calendar_top_controls('day', array('id' => $calendar->courseid,
177             'time' => $calendar->time)), array('class' => 'controls'));
179         if (empty($events)) {
180             // There is nothing to display today.
181             $output .= html_writer::span(get_string('daywithnoevents', 'calendar'), 'calendar-information calendar-no-results');
182         } else {
183             $output .= html_writer::start_tag('div', array('class' => 'eventlist'));
184             $underway = array();
185             // First, print details about events that start today
186             foreach ($events as $event) {
187                 $event = new calendar_event($event);
188                 $event->calendarcourseid = $calendar->courseid;
189                 if ($event->timestart >= $calendar->timestamp_today() && $event->timestart <= $calendar->timestamp_tomorrow()-1) {  // Print it now
190                     $event->time = calendar_format_event_time($event, time(), null, false,
191                         $calendar->timestamp_today());
192                     $output .= $this->event($event);
193                 } else {                                                                 // Save this for later
194                     $underway[] = $event;
195                 }
196             }
198             // Then, show a list of all events that just span this day
199             if (!empty($underway)) {
200                 $output .= html_writer::span(get_string('spanningevents', 'calendar'),
201                     'calendar-information calendar-span-multiple-days');
202                 foreach ($underway as $event) {
203                     $event->time = calendar_format_event_time($event, time(), null, false,
204                         $calendar->timestamp_today());
205                     $output .= $this->event($event);
206                 }
207             }
209             $output .= html_writer::end_tag('div');
210         }
212         return $output;
213     }
215     /**
216      * Displays an event
217      *
218      * @param calendar_event $event
219      * @param bool $showactions
220      * @return string
221      */
222     public function event(calendar_event $event, $showactions=true) {
223         global $CFG;
225         $event = calendar_add_event_metadata($event);
226         $context = $event->context;
227         $output = '';
229         $output .= $this->output->box_start('card-header clearfix');
230         if (calendar_edit_event_allowed($event) && $showactions) {
231             if (empty($event->cmid)) {
232                 $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action' => 'edit', 'id' => $event->id));
233                 $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id' => $event->id));
234                 if (!empty($event->calendarcourseid)) {
235                     $editlink->param('course', $event->calendarcourseid);
236                     $deletelink->param('course', $event->calendarcourseid);
237                 }
238             } else {
239                 $params = array('update' => $event->cmid, 'return' => true, 'sesskey' => sesskey());
240                 $editlink = new moodle_url('/course/mod.php', $params);
241                 $deletelink = null;
242             }
244             $commands  = html_writer::start_tag('div', array('class' => 'commands pull-xs-right'));
245             $commands .= html_writer::start_tag('a', array('href' => $editlink));
246             $str = get_string('tt_editevent', 'calendar');
247             $commands .= $this->output->pix_icon('t/edit', $str);
248             $commands .= html_writer::end_tag('a');
249             if ($deletelink != null) {
250                 $commands .= html_writer::start_tag('a', array('href' => $deletelink));
251                 $str = get_string('tt_deleteevent', 'calendar');
252                 $commands .= $this->output->pix_icon('t/delete', $str);
253                 $commands .= html_writer::end_tag('a');
254             }
255             $commands .= html_writer::end_tag('div');
256             $output .= $commands;
257         }
258         if (!empty($event->icon)) {
259             $output .= $event->icon;
260         } else {
261             $output .= $this->output->spacer(array('height' => 16, 'width' => 16));
262         }
264         if (!empty($event->referer)) {
265             $output .= $this->output->heading($event->referer, 3, array('class' => 'referer'));
266         } else {
267             $output .= $this->output->heading(
268                 format_string($event->name, false, array('context' => $context)),
269                 3,
270                 array('class' => 'name d-inline-block')
271             );
272         }
273         // Show subscription source if needed.
274         if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
275             if (!empty($event->subscription->url)) {
276                 $source = html_writer::link($event->subscription->url, get_string('subsource', 'calendar', $event->subscription));
277             } else {
278                 // File based ical.
279                 $source = get_string('subsource', 'calendar', $event->subscription);
280             }
281             $output .= html_writer::tag('div', $source, array('class' => 'subscription'));
282         }
283         if (!empty($event->courselink)) {
284             $output .= html_writer::tag('div', $event->courselink, array('class' => 'course'));
285         }
286         if (!empty($event->time)) {
287             $output .= html_writer::tag('span', $event->time, array('class' => 'date pull-xs-right m-r-1'));
288         } else {
289             $attrs = array('class' => 'date pull-xs-right m-r-1');
290             $output .= html_writer::tag('span', calendar_time_representation($event->timestart), $attrs);
291         }
293         if (!empty($event->actionurl)) {
294             $output .= html_writer::tag('div', html_writer::link(new moodle_url($event->actionurl), $event->actionname));
295         }
297         $output .= $this->output->box_end();
298         $eventdetailshtml = '';
299         $eventdetailsclasses = '';
301         $eventdetailshtml .= format_text($event->description, $event->format, array('context' => $context));
302         $eventdetailsclasses .= 'description card-block';
303         if (isset($event->cssclass)) {
304             $eventdetailsclasses .= ' '.$event->cssclass;
305         }
307         if (!empty($eventdetailshtml)) {
308             $output .= html_writer::tag('div', $eventdetailshtml, array('class' => $eventdetailsclasses));
309         }
311         $eventhtml = html_writer::tag('div', $output, array('class' => 'card'));
312         return html_writer::tag('div', $eventhtml, array('class' => 'event', 'id' => 'event_' . $event->id));
313     }
315     /**
316      * Displays a month in detail
317      *
318      * @param calendar_information $calendar
319      * @param moodle_url $returnurl the url to return to
320      * @return string
321      */
322     public function show_month_detailed(calendar_information $calendar, moodle_url $returnurl  = null) {
323         global $CFG;
325         if (empty($returnurl)) {
326             $returnurl = $this->page->url;
327         }
329         // Get the calendar type we are using.
330         $calendartype = \core_calendar\type_factory::get_calendar_instance();
332         // Store the display settings.
333         $display = new stdClass;
334         $display->thismonth = false;
336         // Get the specified date in the calendar type being used.
337         $date = $calendartype->timestamp_to_date_array($calendar->time);
338         $thisdate = $calendartype->timestamp_to_date_array(time());
339         if ($date['mon'] == $thisdate['mon'] && $date['year'] == $thisdate['year']) {
340             $display->thismonth = true;
341             $date = $thisdate;
342             $calendar->time = time();
343         }
345         // Get Gregorian date for the start of the month.
346         $gregoriandate = $calendartype->convert_to_gregorian($date['year'], $date['mon'], 1);
347         // Store the gregorian date values to be used later.
348         list($gy, $gm, $gd, $gh, $gmin) = array($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
349             $gregoriandate['hour'], $gregoriandate['minute']);
351         // Get the starting week day for this month.
352         $startwday = dayofweek(1, $date['mon'], $date['year']);
353         // Get the days in a week.
354         $daynames = calendar_get_days();
355         // Store the number of days in a week.
356         $numberofdaysinweek = $calendartype->get_num_weekdays();
358         $display->minwday = calendar_get_starting_weekday();
359         $display->maxwday = $display->minwday + ($numberofdaysinweek - 1);
360         $display->maxdays = calendar_days_in_month($date['mon'], $date['year']);
362         // These are used for DB queries, so we want unixtime, so we need to use Gregorian dates.
363         $display->tstart = make_timestamp($gy, $gm, $gd, $gh, $gmin, 0);
364         $display->tend = $display->tstart + ($display->maxdays * DAYSECS) - 1;
366         // Align the starting weekday to fall in our display range
367         // This is simple, not foolproof.
368         if ($startwday < $display->minwday) {
369             $startwday += $numberofdaysinweek;
370         }
372         // Get events from database
373         $events = calendar_get_legacy_events($display->tstart, $display->tend, $calendar->users, $calendar->groups,
374             $calendar->courses);
375         if (!empty($events)) {
376             foreach($events as $eventid => $event) {
377                 $event = new calendar_event($event);
378                 if (!empty($event->modulename)) {
379                     $instances = get_fast_modinfo($event->courseid)->get_instances_of($event->modulename);
380                     if (empty($instances[$event->instance]->uservisible)) {
381                         unset($events[$eventid]);
382                     }
383                 }
384             }
385         }
387         // Extract information: events vs. time
388         calendar_events_by_day($events, $date['mon'], $date['year'], $eventsbyday, $durationbyday,
389             $typesbyday, $calendar->courses);
391         $output  = html_writer::start_tag('div', array('class'=>'header'));
392         $output .= $this->course_filter_selector($returnurl, get_string('detailedmonthviewfor', 'calendar'));
393         if (calendar_user_can_add_event($calendar->course)) {
394             $output .= $this->add_event_button($calendar->course->id, 0, 0, 0, $calendar->time);
395         }
396         $output .= html_writer::end_tag('div', array('class'=>'header'));
397         // Controls
398         $output .= html_writer::tag('div', calendar_top_controls('month', array('id' => $calendar->courseid,
399             'time' => $calendar->time)), array('class' => 'controls'));
401         $table = new html_table();
402         $table->attributes = array('class'=>'calendarmonth calendartable');
403         $table->summary = get_string('calendarheading', 'calendar', userdate($calendar->time, get_string('strftimemonthyear')));
404         $table->data = array();
406         // Get the day names as the header.
407         $header = array();
408         for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
409             $header[] = $daynames[$i % $numberofdaysinweek]['shortname'];
410         }
411         $table->head = $header;
413         // For the table display. $week is the row; $dayweek is the column.
414         $week = 1;
415         $dayweek = $startwday;
417         $row = new html_table_row(array());
419         // Paddding (the first week may have blank days in the beginning)
420         for($i = $display->minwday; $i < $startwday; ++$i) {
421             $cell = new html_table_cell('&nbsp;');
422             $cell->attributes = array('class'=>'nottoday dayblank');
423             $row->cells[] = $cell;
424         }
426         // Now display all the calendar
427         $weekend = CALENDAR_DEFAULT_WEEKEND;
428         if (isset($CFG->calendar_weekend)) {
429             $weekend = intval($CFG->calendar_weekend);
430         }
432         $daytime = strtotime('-1 day', $display->tstart);
433         for ($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
434             $daytime = strtotime('+1 day', $daytime);
435             if($dayweek > $display->maxwday) {
436                 // We need to change week (table row)
437                 $table->data[] = $row;
438                 $row = new html_table_row(array());
439                 $dayweek = $display->minwday;
440                 ++$week;
441             }
443             // Reset vars
444             $cell = new html_table_cell();
445             $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL . 'view.php',
446                 array('view' => 'day', 'course' => $calendar->courseid)), 0, 0, 0, $daytime);
448             $cellclasses = array();
450             if ($weekend & (1 << ($dayweek % $numberofdaysinweek))) {
451                 // Weekend. This is true no matter what the exact range is.
452                 $cellclasses[] = 'weekend';
453             }
455             // Special visual fx if an event is defined
456             if (isset($eventsbyday[$day])) {
457                 if(count($eventsbyday[$day]) == 1) {
458                     $title = get_string('oneevent', 'calendar');
459                 } else {
460                     $title = get_string('manyevents', 'calendar', count($eventsbyday[$day]));
461                 }
462                 $cell->text = html_writer::tag('div', html_writer::link($dayhref, $day, array('title'=>$title)), array('class'=>'day'));
463             } else {
464                 $cell->text = html_writer::tag('div', $day, array('class'=>'day'));
465             }
467             // Special visual fx if an event spans many days
468             $durationclass = false;
469             if (isset($typesbyday[$day]['durationglobal'])) {
470                 $durationclass = 'duration_global';
471             } else if (isset($typesbyday[$day]['durationcourse'])) {
472                 $durationclass = 'duration_course';
473             } else if (isset($typesbyday[$day]['durationgroup'])) {
474                 $durationclass = 'duration_group';
475             } else if (isset($typesbyday[$day]['durationuser'])) {
476                 $durationclass = 'duration_user';
477             }
478             if ($durationclass) {
479                 $cellclasses[] = 'duration';
480                 $cellclasses[] = $durationclass;
481             }
483             // Special visual fx for today
484             if ($display->thismonth && $day == $date['mday']) {
485                 $cellclasses[] = 'day today';
486             } else {
487                 $cellclasses[] = 'day nottoday';
488             }
489             $cell->attributes = array('class'=>join(' ',$cellclasses));
491             if (isset($eventsbyday[$day])) {
492                 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-new'));
493                 foreach($eventsbyday[$day] as $eventindex) {
494                     // If event has a class set then add it to the event <li> tag
495                     $attributes = array();
496                     if (!empty($events[$eventindex]->class)) {
497                         $attributes['class'] = $events[$eventindex]->class;
498                     }
499                     $dayhref->set_anchor('event_'.$events[$eventindex]->id);
501                     $eventcontext = $events[$eventindex]->context;
502                     $eventformatopts = array('context' => $eventcontext);
503                     // Get event name.
504                     $eventname = format_string($events[$eventindex]->name, true, $eventformatopts);
505                     // Include course's shortname into the event name, if applicable.
506                     $courseid = $events[$eventindex]->courseid;
507                     if (!empty($courseid) && $courseid !== SITEID) {
508                         $course = get_course($courseid);
509                         $eventnameparams = (object)[
510                             'name' => $eventname,
511                             'course' => format_string($course->shortname, true, $eventformatopts)
512                         ];
513                         $eventname = get_string('eventnameandcourse', 'calendar', $eventnameparams);
514                     }
515                     $link = html_writer::link($dayhref, $eventname, ['data-action' => 'view-event',
516                             'data-event-id' => $events[$eventindex]->id]);
517                     $cell->text .= html_writer::tag('li', $link, $attributes);
518                 }
519                 $cell->text .= html_writer::end_tag('ul');
520             }
521             if (isset($durationbyday[$day])) {
522                 $cell->text .= html_writer::start_tag('ul', array('class'=>'events-underway'));
523                 foreach($durationbyday[$day] as $eventindex) {
524                     $cell->text .= html_writer::tag('li', '['.format_string($events[$eventindex]->name,true).']', array('class'=>'events-underway'));
525                 }
526                 $cell->text .= html_writer::end_tag('ul');
527             }
528             $row->cells[] = $cell;
529         }
531         // Paddding (the last week may have blank days at the end)
532         for($i = $dayweek; $i <= $display->maxwday; ++$i) {
533             $cell = new html_table_cell('&nbsp;');
534             $cell->attributes = array('class'=>'nottoday dayblank');
535             $row->cells[] = $cell;
536         }
537         $table->data[] = $row;
538         $output .= html_writer::table($table);
540         return $output;
541     }
543     /**
544      * Displays upcoming events
545      *
546      * @param calendar_information $calendar
547      * @param int $futuredays
548      * @param int $maxevents
549      * @return string
550      */
551     public function show_upcoming_events(calendar_information $calendar, $futuredays, $maxevents, moodle_url $returnurl = null) {
553         if ($returnurl === null) {
554             $returnurl = $this->page->url;
555         }
557         $events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users,
558             $futuredays, $maxevents);
560         $output  = html_writer::start_tag('div', array('class'=>'header'));
561         $output .= $this->course_filter_selector($returnurl, get_string('upcomingeventsfor', 'calendar'));
562         if (calendar_user_can_add_event($calendar->course)) {
563             $output .= $this->add_event_button($calendar->course->id);
564         }
565         $output .= html_writer::end_tag('div');
567         if ($events) {
568             $output .= html_writer::start_tag('div', array('class' => 'eventlist'));
569             foreach ($events as $event) {
570                 // Convert to calendar_event object so that we transform description accordingly.
571                 $event = new calendar_event($event);
572                 $event->calendarcourseid = $calendar->courseid;
573                 $output .= $this->event($event);
574             }
575             $output .= html_writer::end_tag('div');
576         } else {
577             $output .= html_writer::span(get_string('noupcomingevents', 'calendar'), 'calendar-information calendar-no-results');
578         }
580         return $output;
581     }
583     /**
584      * Displays a course filter selector
585      *
586      * @param moodle_url $returnurl The URL that the user should be taken too upon selecting a course.
587      * @param string $label The label to use for the course select.
588      * @return string
589      */
590     protected function course_filter_selector(moodle_url $returnurl, $label=null) {
591         global $USER, $SESSION, $CFG;
593         if (!isloggedin() or isguestuser()) {
594             return '';
595         }
597         if (has_capability('moodle/calendar:manageentries', context_system::instance()) && !empty($CFG->calendar_adminseesall)) {
598             $courses = get_courses('all', 'c.shortname','c.id,c.shortname');
599         } else {
600             $courses = enrol_get_my_courses();
601         }
603         unset($courses[SITEID]);
605         $courseoptions = array();
606         $courseoptions[SITEID] = get_string('fulllistofcourses');
607         foreach ($courses as $course) {
608             $coursecontext = context_course::instance($course->id);
609             $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
610         }
612         if ($this->page->course->id !== SITEID) {
613             $selected = $this->page->course->id;
614         } else {
615             $selected = '';
616         }
617         $courseurl = new moodle_url($returnurl);
618         $courseurl->remove_params('course');
619         $select = new single_select($courseurl, 'course', $courseoptions, $selected, null);
620         $select->class = 'cal_courses_flt m-r-1';
621         if ($label !== null) {
622             $select->set_label($label);
623         } else {
624             $select->set_label(get_string('listofcourses'), array('class' => 'accesshide'));
625         }
626         return $this->output->render($select);
627     }
629     /**
630      * Renders a table containing information about calendar subscriptions.
631      *
632      * @param int $courseid
633      * @param array $subscriptions
634      * @param string $importresults
635      * @return string
636      */
637     public function subscription_details($courseid, $subscriptions, $importresults = '') {
638         $table = new html_table();
639         $table->head  = array(
640             get_string('colcalendar', 'calendar'),
641             get_string('collastupdated', 'calendar'),
642             get_string('eventkind', 'calendar'),
643             get_string('colpoll', 'calendar'),
644             get_string('colactions', 'calendar')
645         );
646         $table->align = array('left', 'left', 'left', 'center');
647         $table->width = '100%';
648         $table->data  = array();
650         if (empty($subscriptions)) {
651             $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
652             $cell->colspan = 5;
653             $table->data[] = new html_table_row(array($cell));
654         }
655         $strnever = new lang_string('never', 'calendar');
656         foreach ($subscriptions as $sub) {
657             $label = $sub->name;
658             if (!empty($sub->url)) {
659                 $label = html_writer::link($sub->url, $label);
660             }
661             if (empty($sub->lastupdated)) {
662                 $lastupdated = $strnever->out();
663             } else {
664                 $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
665             }
667             $cell = new html_table_cell($this->subscription_action_form($sub, $courseid));
668             $cell->colspan = 2;
669             $type = $sub->eventtype . 'events';
671             $table->data[] = new html_table_row(array(
672                 new html_table_cell($label),
673                 new html_table_cell($lastupdated),
674                 new html_table_cell(get_string($type, 'calendar')),
675                 $cell
676             ));
677         }
679         $out  = $this->output->box_start('generalbox calendarsubs');
681         $out .= $importresults;
682         $out .= html_writer::table($table);
683         $out .= $this->output->box_end();
684         return $out;
685     }
687     /**
688      * Creates a form to perform actions on a given subscription.
689      *
690      * @param stdClass $subscription
691      * @param int $courseid
692      * @return string
693      */
694     protected function subscription_action_form($subscription, $courseid) {
695         // Assemble form for the subscription row.
696         $html = html_writer::start_tag('form', array('action' => new moodle_url('/calendar/managesubscriptions.php'), 'method' => 'post'));
697         if (empty($subscription->url)) {
698             // Don't update an iCal file, which has no URL.
699             $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pollinterval', 'value' => '0'));
700         } else {
701             // Assemble pollinterval control.
702             $html .= html_writer::start_tag('div', array('style' => 'float:left;'));
703             $html .= html_writer::start_tag('select', array('name' => 'pollinterval', 'class' => 'custom-select'));
704             foreach (calendar_get_pollinterval_choices() as $k => $v) {
705                 $attributes = array();
706                 if ($k == $subscription->pollinterval) {
707                     $attributes['selected'] = 'selected';
708                 }
709                 $attributes['value'] = $k;
710                 $html .= html_writer::tag('option', $v, $attributes);
711             }
712             $html .= html_writer::end_tag('select');
713             $html .= html_writer::end_tag('div');
714         }
715         $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
716         $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'course', 'value' => $courseid));
717         $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
718         $html .= html_writer::start_tag('div', array('class' => 'btn-group pull-right'));
719         if (!empty($subscription->url)) {
720             $html .= html_writer::tag('button', get_string('update'), array('type'  => 'submit', 'name' => 'action',
721                                                                             'class' => 'btn btn-secondary',
722                                                                             'value' => CALENDAR_SUBSCRIPTION_UPDATE));
723         }
724         $html .= html_writer::tag('button', get_string('remove'), array('type'  => 'submit', 'name' => 'action',
725                                                                         'class' => 'btn btn-secondary',
726                                                                         'value' => CALENDAR_SUBSCRIPTION_REMOVE));
727         $html .= html_writer::end_tag('div');
728         $html .= html_writer::end_tag('form');
729         return $html;
730     }