Search moodle.org's
Developer Documentation

  • Bug fixes for general core bugs in 3.7.x will end 11 May 2020 (12 months).
  • Bug fixes for security issues in 3.7.x will end 9 November 2020 (18 months) - Support has ended.
  • minimum PHP 7.1.0 Note: minimum PHP version has increased since Moodle 3.6. PHP 7.2.x and 7.3.x are supported too. PHP 7.x could have some engine limitations.
  • Differences Between: [Versions 35 and 37] [Versions 36 and 37] [Versions 37 and 310] [Versions 37 and 311] [Versions 37 and 38] [Versions 37 and 39]

       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  if (!defined('MOODLE_INTERNAL')) {
      27      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
      28  }
      29  
      30  /**
      31   * The primary renderer for the calendar.
      32   */
      33  class core_calendar_renderer extends plugin_renderer_base {
      34  
      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      }
      43  
      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      }
      52  
      53      /**
      54       * Produces the content for the three months block (pretend block)
      55       *
      56       * This includes the previous month, the current month, and the next month
      57       *
      58       * @param calendar_information $calendar
      59       * @return string
      60       */
      61      public function fake_block_threemonths(calendar_information $calendar) {
      62          // Get the calendar type we are using.
      63          $calendartype = \core_calendar\type_factory::get_calendar_instance();
      64          $time = $calendartype->timestamp_to_date_array($calendar->time);
      65  
      66          $current = $calendar->time;
      67          $prevmonthyear = $calendartype->get_prev_month($time['year'], $time['mon']);
      68          $prev = $calendartype->convert_to_timestamp(
      69                  $prevmonthyear[1],
      70                  $prevmonthyear[0],
      71                  1
      72              );
      73          $nextmonthyear = $calendartype->get_next_month($time['year'], $time['mon']);
      74          $next = $calendartype->convert_to_timestamp(
      75                  $nextmonthyear[1],
      76                  $nextmonthyear[0],
      77                  1
      78              );
      79  
      80          $content = '';
      81  
      82          // Previous.
      83          $calendar->set_time($prev);
      84          list($previousmonth, ) = calendar_get_view($calendar, 'minithree', false, true);
      85  
      86          // Current month.
      87          $calendar->set_time($current);
      88          list($currentmonth, ) = calendar_get_view($calendar, 'minithree', false, true);
      89  
      90          // Next month.
      91          $calendar->set_time($next);
      92          list($nextmonth, ) = calendar_get_view($calendar, 'minithree', false, true);
      93  
      94          // Reset the time back.
      95          $calendar->set_time($current);
      96  
      97          $data = (object) [
      98              'previousmonth' => $previousmonth,
      99              'currentmonth' => $currentmonth,
     100              'nextmonth' => $nextmonth,
     101          ];
     102  
     103          $template = 'core_calendar/calendar_threemonth';
     104          $content .= $this->render_from_template($template, $data);
     105          return $content;
     106      }
     107  
     108      /**
     109       * Adds a pretent calendar block
     110       *
     111       * @param block_contents $bc
     112       * @param mixed $pos BLOCK_POS_RIGHT | BLOCK_POS_LEFT
     113       */
     114      public function add_pretend_calendar_block(block_contents $bc, $pos=BLOCK_POS_RIGHT) {
     115          $this->page->blocks->add_fake_block($bc, $pos);
     116      }
     117  
     118      /**
     119       * Creates a button to add a new event.
     120       *
     121       * @param int $courseid
     122       * @param int $unused1
     123       * @param int $unused2
     124       * @param int $unused3
     125       * @param int $unused4
     126       * @return string
     127       */
     128      public function add_event_button($courseid, $unused1 = null, $unused2 = null, $unused3 = null, $unused4 = null) {
     129          $data = [
     130              'contextid' => (\context_course::instance($courseid))->id,
     131          ];
     132          return $this->render_from_template('core_calendar/add_event_button', $data);
     133      }
     134  
     135      /**
     136       * Displays an event
     137       *
     138       * @param calendar_event $event
     139       * @param bool $showactions
     140       * @return string
     141       */
     142      public function event(calendar_event $event, $showactions=true) {
     143          global $CFG;
     144  
     145          $event = calendar_add_event_metadata($event);
     146          $context = $event->context;
     147          $output = '';
     148  
     149          $output .= $this->output->box_start('card-header clearfix');
     150          if (calendar_edit_event_allowed($event) && $showactions) {
     151              if (calendar_delete_event_allowed($event)) {
     152                  $editlink = new moodle_url(CALENDAR_URL.'event.php', array('action' => 'edit', 'id' => $event->id));
     153                  $deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id' => $event->id));
     154                  if (!empty($event->calendarcourseid)) {
     155                      $editlink->param('course', $event->calendarcourseid);
     156                      $deletelink->param('course', $event->calendarcourseid);
     157                  }
     158              } else {
     159                  $params = array('update' => $event->cmid, 'return' => true, 'sesskey' => sesskey());
     160                  $editlink = new moodle_url('/course/mod.php', $params);
     161                  $deletelink = null;
     162              }
     163  
     164              $commands  = html_writer::start_tag('div', array('class' => 'commands float-sm-right'));
     165              $commands .= html_writer::start_tag('a', array('href' => $editlink));
     166              $str = get_string('tt_editevent', 'calendar');
     167              $commands .= $this->output->pix_icon('t/edit', $str);
     168              $commands .= html_writer::end_tag('a');
     169              if ($deletelink != null) {
     170                  $commands .= html_writer::start_tag('a', array('href' => $deletelink));
     171                  $str = get_string('tt_deleteevent', 'calendar');
     172                  $commands .= $this->output->pix_icon('t/delete', $str);
     173                  $commands .= html_writer::end_tag('a');
     174              }
     175              $commands .= html_writer::end_tag('div');
     176              $output .= $commands;
     177          }
     178          if (!empty($event->icon)) {
     179              $output .= $event->icon;
     180          } else {
     181              $output .= $this->output->spacer(array('height' => 16, 'width' => 16));
     182          }
     183  
     184          if (!empty($event->referer)) {
     185              $output .= $this->output->heading($event->referer, 3, array('class' => 'referer'));
     186          } else {
     187              $output .= $this->output->heading(
     188                  format_string($event->name, false, array('context' => $context)),
     189                  3,
     190                  array('class' => 'name d-inline-block')
     191              );
     192          }
     193          // Show subscription source if needed.
     194          if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
     195              if (!empty($event->subscription->url)) {
     196                  $source = html_writer::link($event->subscription->url,
     197                          get_string('subscriptionsource', 'calendar', $event->subscription->name));
     198              } else {
     199                  // File based ical.
     200                  $source = get_string('subscriptionsource', 'calendar', $event->subscription->name);
     201              }
     202              $output .= html_writer::tag('div', $source, array('class' => 'subscription'));
     203          }
     204          if (!empty($event->courselink)) {
     205              $output .= html_writer::tag('div', $event->courselink);
     206          }
     207          if (!empty($event->time)) {
     208              $output .= html_writer::tag('span', $event->time, array('class' => 'date float-sm-right mr-1'));
     209          } else {
     210              $attrs = array('class' => 'date float-sm-right mr-1');
     211              $output .= html_writer::tag('span', calendar_time_representation($event->timestart), $attrs);
     212          }
     213  
     214          if (!empty($event->actionurl)) {
     215              $actionlink = html_writer::link(new moodle_url($event->actionurl), $event->actionname);
     216              $output .= html_writer::tag('div', $actionlink, ['class' => 'action']);
     217          }
     218  
     219          $output .= $this->output->box_end();
     220          $eventdetailshtml = '';
     221          $eventdetailsclasses = '';
     222  
     223          $eventdetailshtml .= format_text($event->description, $event->format, array('context' => $context));
     224          $eventdetailsclasses .= 'description card-block';
     225          if (isset($event->cssclass)) {
     226              $eventdetailsclasses .= ' '.$event->cssclass;
     227          }
     228  
     229          if (!empty($eventdetailshtml)) {
     230              $output .= html_writer::tag('div', $eventdetailshtml, array('class' => $eventdetailsclasses));
     231          }
     232  
     233          $eventhtml = html_writer::tag('div', $output, array('class' => 'card'));
     234          return html_writer::tag('div', $eventhtml, array('class' => 'event', 'id' => 'event_' . $event->id));
     235      }
     236  
     237      /**
     238       * Displays a course filter selector
     239       *
     240       * @param moodle_url $returnurl The URL that the user should be taken too upon selecting a course.
     241       * @param string $label The label to use for the course select.
     242       * @param int $courseid The id of the course to be selected.
     243       * @return string
     244       */
     245      public function course_filter_selector(moodle_url $returnurl, $label = null, $courseid = null) {
     246          global $CFG, $DB;
     247  
     248          if (!isloggedin() or isguestuser()) {
     249              return '';
     250          }
     251  
     252          $contextrecords = [];
     253          $courses = calendar_get_default_courses($courseid, 'id, shortname');
     254  
     255          if (!empty($courses) && count($courses) > CONTEXT_CACHE_MAX_SIZE) {
     256              // We need to pull the context records from the DB to preload them
     257              // below. The calendar_get_default_courses code will actually preload
     258              // the contexts itself however the context cache is capped to a certain
     259              // amount before it starts recycling. Unfortunately that starts to happen
     260              // quite a bit if a user has access to a large number of courses (e.g. admin).
     261              // So in order to avoid hitting the DB for each context as we loop below we
     262              // can load all of the context records and add them to the cache just in time.
     263              $courseids = array_map(function($c) {
     264                  return $c->id;
     265              }, $courses);
     266              list($insql, $params) = $DB->get_in_or_equal($courseids);
     267              $contextsql = "SELECT ctx.instanceid, " . context_helper::get_preload_record_columns_sql('ctx') .
     268                            " FROM {context} ctx WHERE ctx.contextlevel = ? AND ctx.instanceid $insql";
     269              array_unshift($params, CONTEXT_COURSE);
     270              $contextrecords = $DB->get_records_sql($contextsql, $params);
     271          }
     272  
     273          unset($courses[SITEID]);
     274  
     275          $courseoptions = array();
     276          $courseoptions[SITEID] = get_string('fulllistofcourses');
     277          foreach ($courses as $course) {
     278              if (isset($contextrecords[$course->id])) {
     279                  context_helper::preload_from_record($contextrecords[$course->id]);
     280              }
     281              $coursecontext = context_course::instance($course->id);
     282              $courseoptions[$course->id] = format_string($course->shortname, true, array('context' => $coursecontext));
     283          }
     284  
     285          if ($courseid) {
     286              $selected = $courseid;
     287          } else if ($this->page->course->id !== SITEID) {
     288              $selected = $this->page->course->id;
     289          } else {
     290              $selected = '';
     291          }
     292          $courseurl = new moodle_url($returnurl);
     293          $courseurl->remove_params('course');
     294  
     295          if (empty($label)) {
     296              $label = get_string('listofcourses');
     297              $labelclass = 'sr-only';
     298          } else {
     299              $labelclass = 'mr-1';
     300          }
     301  
     302          $select = html_writer::label($label, 'course', false, ['class' => $labelclass]);
     303          $select .= html_writer::select($courseoptions, 'course', $selected, false,
     304                  ['class' => 'cal_courses_flt', 'id' => 'course']);
     305  
     306          return $select;
     307      }
     308  
     309      /**
     310       * Renders a table containing information about calendar subscriptions.
     311       *
     312       * @param int $unused
     313       * @param array $subscriptions
     314       * @param string $importresults
     315       * @return string
     316       */
     317      public function subscription_details($unused = null, $subscriptions, $importresults = '') {
     318          $table = new html_table();
     319          $table->head  = array(
     320              get_string('colcalendar', 'calendar'),
     321              get_string('collastupdated', 'calendar'),
     322              get_string('eventkind', 'calendar'),
     323              get_string('colpoll', 'calendar'),
     324              get_string('colactions', 'calendar')
     325          );
     326          $table->align = array('left', 'left', 'left', 'center');
     327          $table->width = '100%';
     328          $table->data  = array();
     329  
     330          if (empty($subscriptions)) {
     331              $cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
     332              $cell->colspan = 5;
     333              $table->data[] = new html_table_row(array($cell));
     334          }
     335          $strnever = new lang_string('never', 'calendar');
     336          foreach ($subscriptions as $sub) {
     337              $label = $sub->name;
     338              if (!empty($sub->url)) {
     339                  $label = html_writer::link($sub->url, $label);
     340              }
     341              if (empty($sub->lastupdated)) {
     342                  $lastupdated = $strnever->out();
     343              } else {
     344                  $lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
     345              }
     346  
     347              $cell = new html_table_cell($this->subscription_action_form($sub));
     348              $cell->colspan = 2;
     349              $type = $sub->eventtype . 'events';
     350  
     351              $table->data[] = new html_table_row(array(
     352                  new html_table_cell($label),
     353                  new html_table_cell($lastupdated),
     354                  new html_table_cell(get_string($type, 'calendar')),
     355                  $cell
     356              ));
     357          }
     358  
     359          $out  = $this->output->box_start('generalbox calendarsubs');
     360  
     361          $out .= $importresults;
     362          $out .= html_writer::table($table);
     363          $out .= $this->output->box_end();
     364          return $out;
     365      }
     366  
     367      /**
     368       * Creates a form to perform actions on a given subscription.
     369       *
     370       * @param stdClass $subscription
     371       * @return string
     372       */
     373      protected function subscription_action_form($subscription) {
     374          // Assemble form for the subscription row.
     375          $html = html_writer::start_tag('form', array('action' => new moodle_url('/calendar/managesubscriptions.php'), 'method' => 'post'));
     376          if (empty($subscription->url)) {
     377              // Don't update an iCal file, which has no URL.
     378              $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pollinterval', 'value' => '0'));
     379          } else {
     380              // Assemble pollinterval control.
     381              $html .= html_writer::start_tag('div', array('style' => 'float:left;'));
     382              $html .= html_writer::start_tag('select', array('name' => 'pollinterval', 'class' => 'custom-select'));
     383              foreach (calendar_get_pollinterval_choices() as $k => $v) {
     384                  $attributes = array();
     385                  if ($k == $subscription->pollinterval) {
     386                      $attributes['selected'] = 'selected';
     387                  }
     388                  $attributes['value'] = $k;
     389                  $html .= html_writer::tag('option', $v, $attributes);
     390              }
     391              $html .= html_writer::end_tag('select');
     392              $html .= html_writer::end_tag('div');
     393          }
     394          $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
     395          $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
     396          $html .= html_writer::start_tag('div', array('class' => 'btn-group float-right'));
     397          if (!empty($subscription->url)) {
     398              $html .= html_writer::tag('button', get_string('update'), array('type'  => 'submit', 'name' => 'action',
     399                                                                              'class' => 'btn btn-secondary',
     400                                                                              'value' => CALENDAR_SUBSCRIPTION_UPDATE));
     401          }
     402          $html .= html_writer::tag('button', get_string('remove'), array('type'  => 'submit', 'name' => 'action',
     403                                                                          'class' => 'btn btn-secondary',
     404                                                                          'value' => CALENDAR_SUBSCRIPTION_REMOVE));
     405          $html .= html_writer::end_tag('div');
     406          $html .= html_writer::end_tag('form');
     407          return $html;
     408      }
     409  
     410      /**
     411       * Render the event filter region.
     412       *
     413       * @return  string
     414       */
     415      public function event_filter() {
     416          $data = [
     417              'eventtypes' => calendar_get_filter_types(),
     418          ];
     419          return $this->render_from_template('core_calendar/event_filter', $data);
     420      }
     421  }