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.
   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 the data necessary for rendering the status field in the course participants page.
  19   *
  20   * @package    core_user
  21   * @copyright  2017 Jun Pataleta
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_user\output;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use renderable;
  30  use renderer_base;
  31  use stdClass;
  32  use templatable;
  33  use user_enrolment_action;
  34  
  35  /**
  36   * Class containing the data for the status field.
  37   *
  38   * @package    core_user
  39   * @copyright  2017 Jun Pataleta
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class status_field implements renderable, templatable {
  43  
  44      /** Active user enrolment status constant. */
  45      const STATUS_ACTIVE = 0;
  46  
  47      /** Suspended user enrolment status constant. */
  48      const STATUS_SUSPENDED = 1;
  49  
  50      /** Not current user enrolment status constant. */
  51      const STATUS_NOT_CURRENT = 2;
  52  
  53      /** @var string $enrolinstancename The enrolment instance name. */
  54      protected $enrolinstancename;
  55  
  56      /** @var string $coursename The course's full name. */
  57      protected $coursename;
  58  
  59      /** @var string $fullname The user's full name. */
  60      protected $fullname;
  61  
  62      /** @var string $status The user enrolment status. */
  63      protected $status;
  64  
  65      /** @var int $timestart The timestamp when the user's enrolment starts. */
  66      protected $timestart;
  67  
  68      /** @var int $timeend The timestamp when the user's enrolment ends. */
  69      protected $timeend;
  70  
  71      /** @var int $timeenrolled The timestamp when the user was enrolled. */
  72      protected $timeenrolled;
  73  
  74      /** @var user_enrolment_action[] $enrolactions Array of enrol action objects for the given enrolment method. */
  75      protected $enrolactions;
  76  
  77      /** @var bool $statusactive Indicates whether a user enrolment status should be rendered as active. */
  78      protected $statusactive = false;
  79  
  80      /** @var bool $statusactive Indicates whether a user enrolment status should be rendered as suspended. */
  81      protected $statussuspended = false;
  82  
  83      /** @var bool $statusactive Indicates whether a user enrolment status should be rendered as not current. */
  84      protected $statusnotcurrent = false;
  85  
  86      /**
  87       * status_field constructor.
  88       *
  89       * @param string $enrolinstancename The enrolment instance name.
  90       * @param string $coursename The course's full name.
  91       * @param string $fullname The user's full name.
  92       * @param string $status The user enrolment status.
  93       * @param int|null $timestart The timestamp when the user's enrolment starts.
  94       * @param int|null $timeend The timestamp when the user's enrolment ends.
  95       * @param user_enrolment_action[] $enrolactions Array of enrol action objects for the given enrolment method.
  96       * @param int|null $timeenrolled The timestamp when the user was enrolled.
  97       */
  98      public function __construct($enrolinstancename, $coursename, $fullname, $status, $timestart = null, $timeend = null,
  99                                  $enrolactions = [], $timeenrolled = null) {
 100          $this->enrolinstancename = $enrolinstancename;
 101          $this->coursename = $coursename;
 102          $this->fullname = $fullname;
 103          $this->status = $status;
 104          $this->timestart = $timestart;
 105          $this->timeend = $timeend;
 106          $this->enrolactions = $enrolactions;
 107          $this->timeenrolled = $timeenrolled;
 108      }
 109  
 110      /**
 111       * Function to export the renderer data in a format that is suitable for a
 112       * mustache template. This means:
 113       * 1. No complex types - only stdClass, array, int, string, float, bool
 114       * 2. Any additional info that is required for the template is pre-calculated (e.g. capability checks).
 115       *
 116       * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
 117       * @return stdClass|array
 118       */
 119      public function export_for_template(renderer_base $output) {
 120          $data = new stdClass();
 121          $data->enrolinstancename = $this->enrolinstancename;
 122          $data->coursename = $this->coursename;
 123          $data->fullname = $this->fullname;
 124          $data->status = $this->status;
 125          $data->active = $this->statusactive;
 126          $data->suspended = $this->statussuspended;
 127          $data->notcurrent = $this->statusnotcurrent;
 128  
 129          if ($this->timestart) {
 130              $data->timestart = userdate($this->timestart);
 131          }
 132          if ($this->timeend) {
 133              $data->timeend = userdate($this->timeend);
 134          }
 135          if ($this->timeenrolled) {
 136              $data->timeenrolled = userdate($this->timeenrolled);
 137          }
 138          $data->enrolactions = [];
 139  
 140          foreach ($this->enrolactions as $enrolaction) {
 141              $action = new stdClass();
 142              $action->url = $enrolaction->get_url()->out(false);
 143              $action->icon = $output->render($enrolaction->get_icon());
 144              $action->attributes = [];
 145              foreach ($enrolaction->get_attributes() as $name => $value) {
 146                  $attribute = (object) [
 147                      'name' => $name,
 148                      'value' => $value
 149                  ];
 150                  $action->attributes[] = $attribute;
 151              }
 152              $data->enrolactions[] = $action;
 153          }
 154  
 155          return $data;
 156      }
 157  
 158      /**
 159       * Status setter.
 160       *
 161       * @param int $status The user enrolment status representing one of this class' STATUS_* constants.
 162       * @return status_field This class' instance. Useful for chaining.
 163       */
 164      public function set_status($status = self::STATUS_ACTIVE) {
 165          $this->statusactive = $status == static::STATUS_ACTIVE;
 166          $this->statussuspended = $status == static::STATUS_SUSPENDED;
 167          $this->statusnotcurrent = $status == static::STATUS_NOT_CURRENT;
 168  
 169          return $this;
 170      }
 171  }