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 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   * Course completion critieria - marked by role
  19   *
  20   * @package core_completion
  21   * @category completion
  22   * @copyright 2009 Catalyst IT Ltd
  23   * @author Aaron Barnes <aaronb@catalyst.net.nz>
  24   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Course completion critieria - marked by role
  31   *
  32   * @package core_completion
  33   * @category completion
  34   * @copyright 2009 Catalyst IT Ltd
  35   * @author Aaron Barnes <aaronb@catalyst.net.nz>
  36   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class completion_criteria_role extends completion_criteria {
  39  
  40      /* @var int Criteria type constant [COMPLETION_CRITERIA_TYPE_ROLE] */
  41      public $criteriatype = COMPLETION_CRITERIA_TYPE_ROLE;
  42  
  43      /**
  44       * Finds and returns a data_object instance based on params.
  45       *
  46       * @param array $params associative arrays varname=>value
  47       * @return data_object data_object instance or false if none found.
  48       */
  49      public static function fetch($params) {
  50          $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_ROLE;
  51          return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
  52      }
  53  
  54     /**
  55      * Add appropriate form elements to the critieria form
  56      *
  57      * @param moodleform $mform Moodle forms object
  58      * @param stdClass $data used to set default values of the form
  59      */
  60      public function config_form_display(&$mform, $data = null) {
  61  
  62          $mform->addElement('checkbox', 'criteria_role['.$data->id.']', $this->get_title($data));
  63  
  64          if ($this->id) {
  65              $mform->setDefault('criteria_role['.$data->id.']', 1);
  66          }
  67      }
  68  
  69      /**
  70       * Update the criteria information stored in the database
  71       *
  72       * @param stdClass $data Form data
  73       */
  74      public function update_config(&$data) {
  75  
  76          if (!empty($data->criteria_role) && is_array($data->criteria_role)) {
  77  
  78              $this->course = $data->id;
  79  
  80              foreach (array_keys($data->criteria_role) as $role) {
  81  
  82                  $this->role = $role;
  83                  $this->id = NULL;
  84                  $this->insert();
  85              }
  86          }
  87      }
  88  
  89      /**
  90       * Mark this criteria as complete
  91       *
  92       * @param completion_completion $completion The user's completion record
  93       */
  94      public function complete($completion) {
  95          $this->review($completion, true, true);
  96      }
  97  
  98      /**
  99       * Review this criteria and decide if the user has completed
 100       *
 101       * @param completion_completion $completion The user's completion record
 102       * @param bool $mark Optionally set false to not save changes to database
 103       * @param bool $is_complete Set to false if the criteria has been completed just now.
 104       * @return bool
 105       */
 106      public function review($completion, $mark = true, $is_complete = false)  {
 107          // If we are marking this as complete
 108          if ($is_complete && $mark) {
 109              $completion->completedself = 1;
 110              $completion->mark_complete();
 111  
 112              return true;
 113          }
 114  
 115          return $completion->is_complete();
 116      }
 117  
 118      /**
 119       * Return criteria title for display in reports
 120       *
 121       * @return string
 122       */
 123      public function get_title() {
 124          global $DB;
 125          $role = $DB->get_record('role', array('id' => $this->role));
 126          if (!$role) {
 127              return '['.get_string('roleidnotfound', 'completion', $this->role).']';
 128          }
 129          return role_get_name($role, context_course::instance($this->course));
 130      }
 131  
 132      /**
 133       * Return a more detailed criteria title for display in reports
 134       *
 135       * @return string
 136       */
 137      public function get_title_detailed() {
 138          return $this->get_title();
 139      }
 140  
 141      /**
 142       * Return criteria type title for display in reports
 143       *
 144       * @return string
 145       */
 146      public function get_type_title() {
 147          return get_string('approval', 'completion');
 148      }
 149  
 150      /**
 151       * Return criteria progress details for display in reports
 152       *
 153       * @param completion_completion $completion The user's completion record
 154       * @return array An array with the following keys:
 155       *     type, criteria, requirement, status
 156       */
 157      public function get_details($completion) {
 158          $details = array();
 159          $details['type'] = get_string('manualcompletionby', 'completion');
 160          $details['criteria'] = $this->get_title();
 161          $details['requirement'] = get_string('markedcompleteby', 'completion', $details['criteria']);
 162          $details['status'] = '';
 163  
 164          return $details;
 165      }
 166  }