Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

   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      /** @var int Number of timeline instances displayed. */
  63      protected static $timelineinstances = 0;
  64  
  65      /** @var int This timeline instance's ID. */
  66      protected $timelineinstanceid = 0;
  67  
  68      /**
  69       * main constructor.
  70       *
  71       * @param string $order Constant sort value from ../timeline/lib.php
  72       * @param string $filter Constant filter value from ../timeline/lib.php
  73       * @param string $limit Constant limit value from ../timeline/lib.php
  74       */
  75      public function __construct($order, $filter, $limit) {
  76          $this->order = $order ? $order : BLOCK_TIMELINE_SORT_BY_DATES;
  77          $this->filter = $filter ? $filter : BLOCK_TIMELINE_FILTER_BY_7_DAYS;
  78          $this->limit = $limit ? $limit : BLOCK_TIMELINE_ACTIVITIES_LIMIT_DEFAULT;
  79          // Increment the timeline instances count on initialisation.
  80          self::$timelineinstances++;
  81          // Assign this instance an ID based on the latest timeline instances count.
  82          $this->timelineinstanceid = self::$timelineinstances;
  83      }
  84  
  85      /**
  86       * Test the available filters with the current user preference and return an array with
  87       * bool flags corresponding to which is active
  88       *
  89       * @return array
  90       */
  91      protected function get_filters_as_booleans() {
  92          $filters = [
  93              BLOCK_TIMELINE_FILTER_BY_NONE => false,
  94              BLOCK_TIMELINE_FILTER_BY_OVERDUE => false,
  95              BLOCK_TIMELINE_FILTER_BY_7_DAYS => false,
  96              BLOCK_TIMELINE_FILTER_BY_30_DAYS => false,
  97              BLOCK_TIMELINE_FILTER_BY_3_MONTHS => false,
  98              BLOCK_TIMELINE_FILTER_BY_6_MONTHS => false
  99          ];
 100  
 101          // Set the selected filter to true.
 102          $filters[$this->filter] = true;
 103  
 104          return $filters;
 105      }
 106  
 107      /**
 108       * Get the offset/limit values corresponding to $this->filter
 109       * which are used to send through to the context as default values
 110       *
 111       * @return array
 112       */
 113      private function get_filter_offsets() {
 114  
 115          $limit = '';
 116          if (in_array($this->filter, [BLOCK_TIMELINE_FILTER_BY_NONE, BLOCK_TIMELINE_FILTER_BY_OVERDUE])) {
 117              $offset = -14;
 118              if ($this->filter == BLOCK_TIMELINE_FILTER_BY_OVERDUE) {
 119                  $limit = 1;
 120              }
 121          } else {
 122              $offset = 0;
 123              $limit = 7;
 124  
 125              switch($this->filter) {
 126                  case BLOCK_TIMELINE_FILTER_BY_30_DAYS:
 127                      $limit = 30;
 128                      break;
 129                  case BLOCK_TIMELINE_FILTER_BY_3_MONTHS:
 130                      $limit = 90;
 131                      break;
 132                  case BLOCK_TIMELINE_FILTER_BY_6_MONTHS:
 133                      $limit = 180;
 134                      break;
 135              }
 136          }
 137  
 138          return [
 139              'daysoffset' => $offset,
 140              'dayslimit' => $limit
 141          ];
 142      }
 143  
 144      /**
 145       * Export this data so it can be used as the context for a mustache template.
 146       *
 147       * @param \renderer_base $output
 148       * @return array
 149       */
 150      public function export_for_template(renderer_base $output) {
 151  
 152          $nocoursesurl = $output->image_url('courses', 'block_timeline')->out();
 153          $noeventsurl = $output->image_url('activities', 'block_timeline')->out();
 154  
 155          $requiredproperties = course_summary_exporter::define_properties();
 156          $fields = join(',', array_keys($requiredproperties));
 157          $courses = course_get_enrolled_courses_for_logged_in_user(0, 0, null, $fields);
 158          list($inprogresscourses, $processedcount) = course_filter_courses_by_timeline_classification(
 159              $courses,
 160              COURSE_TIMELINE_INPROGRESS,
 161              self::COURSES_PER_PAGE
 162          );
 163          $formattedcourses = array_map(function($course) use ($output) {
 164              \context_helper::preload_from_record($course);
 165              $context = \context_course::instance($course->id);
 166              $exporter = new course_summary_exporter($course, ['context' => $context]);
 167              return $exporter->export($output);
 168          }, $inprogresscourses);
 169  
 170          $filters = $this->get_filters_as_booleans();
 171          $offsets = $this->get_filter_offsets();
 172          $contextvariables = [
 173              'timelineinstanceid' => $this->timelineinstanceid,
 174              'midnight' => usergetmidnight(time()),
 175              'coursepages' => [$formattedcourses],
 176              'urls' => [
 177                  'nocourses' => $nocoursesurl,
 178                  'noevents' => $noeventsurl
 179              ],
 180              'sorttimelinedates' => $this->order == BLOCK_TIMELINE_SORT_BY_DATES,
 181              'sorttimelinecourses' => $this->order == BLOCK_TIMELINE_SORT_BY_COURSES,
 182              'selectedfilter' => $this->filter,
 183              'hasdaysoffset' => true,
 184              'hasdayslimit' => $offsets['dayslimit'] !== '' ,
 185              'nodayslimit' => $offsets['dayslimit'] === '' ,
 186              'limit' => $this->limit,
 187              'hascourses' => !empty($formattedcourses),
 188          ];
 189          return array_merge($contextvariables, $filters, $offsets);
 190      }
 191  }