Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [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  declare(strict_types=1);
  18  
  19  namespace core_course\reportbuilder\local\formatters;
  20  
  21  use core_user\output\status_field;
  22  use lang_string;
  23  use stdClass;
  24  
  25  /**
  26   * Formatters for the course enrolment entity
  27   *
  28   * @package     core_course
  29   * @copyright   2022 David Matamoros <davidmc@moodle.com>
  30   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class enrolment {
  33  
  34      /**
  35       * Return enrolment plugin instance name
  36       *
  37       * @param string|null $value
  38       * @param stdClass $row
  39       * @return string
  40       */
  41      public static function enrolment_name(?string $value, stdClass $row): string {
  42          global $DB;
  43  
  44          if (empty($value)) {
  45              return '';
  46          }
  47  
  48          $instance = $DB->get_record('enrol', ['id' => $row->id, 'enrol' => $row->enrol], '*', MUST_EXIST);
  49          $plugin = enrol_get_plugin($row->enrol);
  50  
  51          return $plugin ? $plugin->get_instance_name($instance) : '-';
  52      }
  53  
  54      /**
  55       * Returns list of enrolment statuses
  56       *
  57       * @return lang_string[]
  58       */
  59      public static function enrolment_values(): array {
  60          return [
  61              status_field::STATUS_ACTIVE => new lang_string('participationactive', 'enrol'),
  62              status_field::STATUS_SUSPENDED => new lang_string('participationsuspended', 'enrol'),
  63              status_field::STATUS_NOT_CURRENT => new lang_string('participationnotcurrent', 'enrol'),
  64          ];
  65      }
  66  
  67      /**
  68       * Return enrolment status for user
  69       *
  70       * @param string|null $value
  71       * @return string|null
  72       */
  73      public static function enrolment_status(?string $value): ?string {
  74          if ($value === null) {
  75              return null;
  76          }
  77  
  78          $statusvalues = self::enrolment_values();
  79  
  80          $value = (int) $value;
  81          if (!array_key_exists($value, $statusvalues)) {
  82              return null;
  83          }
  84  
  85          return (string) $statusvalues[$value];
  86      }
  87  }