Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Class containing data for timeline block.
  19   *
  20   * @package    block_timeline
  21   * @copyright  2018 Ryan Wyllie <ryan@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace block_timeline\output;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use renderable;
  28  use renderer_base;
  29  use templatable;
  30  use core_course\external\course_summary_exporter;
  31  
  32  require_once($CFG->dirroot . '/course/lib.php');
  33  require_once($CFG->dirroot . '/blocks/timeline/lib.php');
  34  require_once($CFG->libdir . '/completionlib.php');
  35  
  36  /**
  37   * Class containing data for timeline block.
  38   *
  39   * @copyright  2018 Ryan Wyllie <ryan@moodle.com>
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class main implements renderable, templatable {
  43  
  44      /** Number of courses to load per page */
  45      const COURSES_PER_PAGE = 2;
  46  
  47      /**
  48       * @var string The current filter preference
  49       */
  50      public $filter;
  51  
  52      /**
  53       * @var string The current sort/order preference
  54       */
  55      public $order;
  56  
  57      /**
  58       * @var string The current limit preference
  59       */
  60      public $limit;
  61  
  62      /**
  63       * main constructor.
  64       *
  65       * @param string $order Constant sort value from ../timeline/lib.php
  66       * @param string $filter Constant filter value from ../timeline/lib.php
  67       * @param string $limit Constant limit value from ../timeline/lib.php
  68       */
  69      public function __construct($order, $filter, $limit) {
  70          $this->order = $order ? $order : BLOCK_TIMELINE_SORT_BY_DATES;
  71          $this->filter = $filter ? $filter : BLOCK_TIMELINE_FILTER_BY_7_DAYS;
  72          $this->limit = $limit ? $limit : BLOCK_TIMELINE_ACTIVITIES_LIMIT_DEFAULT;
  73      }
  74  
  75      /**
  76       * Test the available filters with the current user preference and return an array with
  77       * bool flags corresponding to which is active
  78       *
  79       * @return array
  80       */
  81      protected function get_filters_as_booleans() {
  82          $filters = [
  83              BLOCK_TIMELINE_FILTER_BY_NONE => false,
  84              BLOCK_TIMELINE_FILTER_BY_OVERDUE => false,
  85              BLOCK_TIMELINE_FILTER_BY_7_DAYS => false,
  86              BLOCK_TIMELINE_FILTER_BY_30_DAYS => false,
  87              BLOCK_TIMELINE_FILTER_BY_3_MONTHS => false,
  88              BLOCK_TIMELINE_FILTER_BY_6_MONTHS => false
  89          ];
  90  
  91          // Set the selected filter to true.
  92          $filters[$this->filter] = true;
  93  
  94          return $filters;
  95      }
  96  
  97      /**
  98       * Get the offset/limit values corresponding to $this->filter
  99       * which are used to send through to the context as default values
 100       *
 101       * @return array
 102       */
 103      private function get_filter_offsets() {
 104  
 105          $limit = '';
 106          if (in_array($this->filter, [BLOCK_TIMELINE_FILTER_BY_NONE, BLOCK_TIMELINE_FILTER_BY_OVERDUE])) {
 107              $offset = -14;
 108              if ($this->filter == BLOCK_TIMELINE_FILTER_BY_OVERDUE) {
 109                  $limit = 0;
 110              }
 111          } else {
 112              $offset = 0;
 113              $limit = 7;
 114  
 115              switch($this->filter) {
 116                  case BLOCK_TIMELINE_FILTER_BY_30_DAYS:
 117                      $limit = 30;
 118                      break;
 119                  case BLOCK_TIMELINE_FILTER_BY_3_MONTHS:
 120                      $limit = 90;
 121                      break;
 122                  case BLOCK_TIMELINE_FILTER_BY_6_MONTHS:
 123                      $limit = 180;
 124                      break;
 125              }
 126          }
 127  
 128          return [
 129              'daysoffset' => $offset,
 130              'dayslimit' => $limit
 131          ];
 132      }
 133  
 134      /**
 135       * Export this data so it can be used as the context for a mustache template.
 136       *
 137       * @param \renderer_base $output
 138       * @return stdClass
 139       */
 140      public function export_for_template(renderer_base $output) {
 141  
 142          $nocoursesurl = $output->image_url('courses', 'block_timeline')->out();
 143          $noeventsurl = $output->image_url('activities', 'block_timeline')->out();
 144  
 145          $requiredproperties = course_summary_exporter::define_properties();
 146          $fields = join(',', array_keys($requiredproperties));
 147          $courses = course_get_enrolled_courses_for_logged_in_user(0, 0, null, $fields);
 148          list($inprogresscourses, $processedcount) = course_filter_courses_by_timeline_classification(
 149              $courses,
 150              COURSE_TIMELINE_INPROGRESS,
 151              self::COURSES_PER_PAGE
 152          );
 153          $formattedcourses = array_map(function($course) use ($output) {
 154              \context_helper::preload_from_record($course);
 155              $context = \context_course::instance($course->id);
 156              $exporter = new course_summary_exporter($course, ['context' => $context]);
 157              return $exporter->export($output);
 158          }, $inprogresscourses);
 159  
 160          $filters = $this->get_filters_as_booleans();
 161          $offsets = $this->get_filter_offsets();
 162          $contextvariables = [
 163              'midnight' => usergetmidnight(time()),
 164              'coursepages' => [$formattedcourses],
 165              'urls' => [
 166                  'nocourses' => $nocoursesurl,
 167                  'noevents' => $noeventsurl
 168              ],
 169              'sorttimelinedates' => $this->order == BLOCK_TIMELINE_SORT_BY_DATES,
 170              'sorttimelinecourses' => $this->order == BLOCK_TIMELINE_SORT_BY_COURSES,
 171              'selectedfilter' => $this->filter,
 172              'hasdaysoffset' => true,
 173              'hasdayslimit' => $offsets['dayslimit'] !== '' ,
 174              'nodayslimit' => $offsets['dayslimit'] === '' ,
 175              'limit' => $this->limit
 176          ];
 177          return array_merge($contextvariables, $filters, $offsets);
 178      }
 179  }