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.
   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_completion\progress;
  22  use core_reportbuilder\local\helpers\format;
  23  use stdClass;
  24  
  25  /**
  26   * Formatters for the course completion 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 completion {
  33  
  34      /**
  35       * Return completion progress as a percentage
  36       *
  37       * @param string|null $value
  38       * @param stdClass $row
  39       * @return string
  40       */
  41      public static function completion_progress(?string $value, stdClass $row): string {
  42          global $CFG;
  43          require_once($CFG->libdir . '/completionlib.php');
  44  
  45          // Do not show progress if there is no userid.
  46          if (!$row->userid) {
  47              return '';
  48          }
  49  
  50          // Make sure courseid and userid have a value, specially userid because get_course_progress_percentage() defaults
  51          // to the current user if this is null and the result would be wrong.
  52          $courseid = (int) $row->courseid;
  53          $userid = (int) $row->userid;
  54          if ($courseid === 0 || $userid === 0) {
  55              return format::percent(0);
  56          }
  57  
  58          $course = get_course($courseid);
  59          $progress = (float) progress::get_course_progress_percentage($course, $userid);
  60          return format::percent($progress);
  61      }
  62  
  63      /**
  64       * Return number of days for methods daystakingcourse and daysuntilcompletion
  65       *
  66       * @param int|null $value
  67       * @param stdClass $row
  68       * @return int|null
  69       */
  70      public static function get_days(?int $value, stdClass $row): ?int {
  71          // Do not show anything if there is no userid.
  72          if (!$row->userid) {
  73              return null;
  74          }
  75          return $value;
  76      }
  77  }