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_completion\external;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  use renderer_base;
  24  
  25  /**
  26   * Completion info exporter
  27   *
  28   * @package    core_completion
  29   * @copyright  2021 Dongsheng Cai
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class completion_info_exporter extends \core\external\exporter {
  33      /**
  34       * @var object $course moodle course object
  35       */
  36      private $course;
  37      /**
  38       * @var object|cm_info $cm course module info
  39       */
  40      private $cminfo;
  41      /**
  42       * @var int $userid user id
  43       */
  44      private $userid;
  45  
  46      /**
  47       * Constructor for the completion info exporter.
  48       *
  49       * @param object $course course object
  50       * @param object|cm_info $cm course module info
  51       * @param int $userid user id
  52       * @param array $related related values
  53       */
  54      public function __construct(object $course, object $cm, int $userid, array $related = []) {
  55          $this->course = $course;
  56          $this->cminfo = \cm_info::create($cm);
  57          $this->userid = $userid;
  58          parent::__construct([], $related);
  59      }
  60  
  61      /**
  62       * Get the additional values to inject while exporting.
  63       *
  64       * @param renderer_base $output The renderer.
  65       * @return array Keys are the property names, values are their values.
  66       */
  67      protected function get_other_values(renderer_base $output): array {
  68          $cmcompletion = \core_completion\cm_completion_details::get_instance($this->cminfo, $this->userid);
  69          $cmcompletiondetails = $cmcompletion->get_details();
  70  
  71          $details = [];
  72          foreach ($cmcompletiondetails as $rulename => $rulevalue) {
  73              $details[] = [
  74                  'rulename' => $rulename,
  75                  'rulevalue' => (array)$rulevalue,
  76              ];
  77          }
  78          return [
  79              'state'         => $cmcompletion->get_overall_completion(),
  80              'timecompleted' => $cmcompletion->get_timemodified(),
  81              'overrideby'    => $cmcompletion->overridden_by(),
  82              'valueused'     => \core_availability\info::completion_value_used($this->course, $this->cminfo->id),
  83              'hascompletion'    => $cmcompletion->has_completion(),
  84              'isautomatic'      => $cmcompletion->is_automatic(),
  85              'istrackeduser'    => $cmcompletion->is_tracked_user(),
  86              'overallstatus'    => $cmcompletion->get_overall_completion(),
  87              'uservisible'      => $this->cminfo->uservisible,
  88              'details'          => $details,
  89          ];
  90      }
  91  
  92      /**
  93       * Return the list of additional properties used only for display.
  94       *
  95       * @return array Keys are the property names, and value their definition.
  96       */
  97      public static function define_other_properties(): array {
  98          return [
  99              'state' => [
 100                  'type' => PARAM_INT,
 101                  'description' => 'overall completion state of this course module.',
 102              ],
 103              'timecompleted' => [
 104                  'type' => PARAM_INT,
 105                  'description' => 'course completion timestamp.',
 106              ],
 107              'overrideby' => [
 108                  'type' => PARAM_INT,
 109                  'description' => 'user ID that has overridden the completion state of this activity for the user.',
 110                  'null' => NULL_ALLOWED,
 111              ],
 112              'valueused' => [
 113                  'type' => PARAM_BOOL,
 114                  'description' => 'True if module is used in a condition, false otherwise.',
 115              ],
 116              'hascompletion' => [
 117                  'type' => PARAM_BOOL,
 118                  'description' => 'Whether this activity module has completion enabled.'
 119              ],
 120              'isautomatic' => [
 121                  'type' => PARAM_BOOL,
 122                  'description' => 'Whether this activity module instance tracks completion automatically.'
 123              ],
 124              'istrackeduser' => [
 125                  'type' => PARAM_BOOL,
 126                  'description' => 'Checks whether completion is being tracked for this user.'
 127              ],
 128              'uservisible' => [
 129                  'type' => PARAM_BOOL,
 130                  'description' => 'Whether this activity is visible to user.'
 131              ],
 132              'details' => [
 133                  'multiple' => true,
 134                  'description' => 'An array of completion details containing the description and status.',
 135                  'type' => [
 136                      'rulename' => [
 137                          'type' => PARAM_TEXT,
 138                      ],
 139                      'rulevalue' => [
 140                          'type' => [
 141                              'status' => [
 142                                  'type' => PARAM_INT,
 143                              ],
 144                              'description' => [
 145                                  'type' => PARAM_TEXT,
 146                              ]
 147                          ]
 148                      ]
 149                  ]
 150              ],
 151          ];
 152      }
 153  }