Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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   * Contains class used to return completion progress information.
  19   *
  20   * @package    core_completion
  21   * @copyright  2017 Mark Nelson <markn@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_completion;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->libdir . '/completionlib.php');
  30  
  31  /**
  32   * Class used to return completion progress information.
  33   *
  34   * @package    core_completion
  35   * @copyright  2017 Mark Nelson <markn@moodle.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class progress {
  39  
  40      /**
  41       * Returns the course percentage completed by a certain user, returns null if no completion data is available.
  42       *
  43       * @param \stdClass $course Moodle course object
  44       * @param int $userid The id of the user, 0 for the current user
  45       * @return null|float The percentage, or null if completion is not supported in the course,
  46       *         or there are no activities that support completion.
  47       */
  48      public static function get_course_progress_percentage($course, $userid = 0) {
  49          global $USER;
  50  
  51          // Make sure we continue with a valid userid.
  52          if (empty($userid)) {
  53              $userid = $USER->id;
  54          }
  55  
  56          $completion = new \completion_info($course);
  57  
  58          // First, let's make sure completion is enabled.
  59          if (!$completion->is_enabled()) {
  60              return null;
  61          }
  62  
  63          if (!$completion->is_tracked_user($userid)) {
  64              return null;
  65          }
  66  
  67          // Before we check how many modules have been completed see if the course has.
  68          if ($completion->is_course_complete($userid)) {
  69              return 100;
  70          }
  71  
  72          // Get the number of modules that support completion.
  73          $modules = $completion->get_activities();
  74          $count = count($modules);
  75          if (!$count) {
  76              return null;
  77          }
  78  
  79          // Get the number of modules that have been completed.
  80          $completed = 0;
  81          foreach ($modules as $module) {
  82              $data = $completion->get_data($module, true, $userid);
  83              $completed += $data->completionstate == COMPLETION_INCOMPLETE ? 0 : 1;
  84          }
  85  
  86          return ($completed / $count) * 100;
  87      }
  88  }