Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400]

   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   * Completion data for a specific user, course and critieria
  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  require_once($CFG->dirroot.'/completion/data_object.php');
  29  
  30  /**
  31   * Completion data for a specific user, course and critieria
  32   *
  33   * @package core_completion
  34   * @category completion
  35   * @copyright 2009 Catalyst IT Ltd
  36   * @author Aaron Barnes <aaronb@catalyst.net.nz>
  37   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class completion_criteria_completion extends data_object {
  40  
  41      /* @var string Database table that stores completion type criteria */
  42      public $table = 'course_completion_crit_compl';
  43  
  44      /* @var array Array of required table fields, must start with 'id'. */
  45      public $required_fields = array('id', 'userid', 'course', 'criteriaid', 'gradefinal', 'rpl', 'unenroled', 'timecompleted');
  46  
  47      /* @var array Array of unique fields, used in where clauses */
  48      public $unique_fields = array('userid', 'course', 'criteriaid');
  49  
  50      /* @var int User ID */
  51      public $userid;
  52  
  53      /* @var int course ID */
  54      public $course;
  55  
  56      /* @var int The id of the course completion criteria this completion references */
  57      public $criteriaid;
  58  
  59      /* @var float The final grade for the user in the course (if completing a grade criteria) */
  60      public $gradefinal;
  61  
  62      /* @var string Record of prior learning, leave blank if none */
  63      public $rpl;
  64  
  65      /* @var int Timestamp of user unenrolment (if completing a unenrol criteria) */
  66      public $unenroled;
  67  
  68      /* @var int Timestamp of course criteria completion {@link completion_criteria_completion::mark_complete()} */
  69      public $timecompleted;
  70  
  71      /* @var completion_criterria Associated criteria object */
  72      private $_criteria;
  73  
  74      /**
  75       * Finds and returns a data_object instance based on params.
  76       *
  77       * @param array $params associative arrays varname=>value
  78       * @return data_object instance of data_object or false if none found.
  79       */
  80      public static function fetch($params) {
  81          return self::fetch_helper('course_completion_crit_compl', __CLASS__, $params);
  82      }
  83  
  84      /**
  85       * Finds and returns all data_object instances based on params.
  86       *
  87       * @param array $params associative arrays varname=>value
  88       * @return array array of data_object insatnces or false if none found.
  89       */
  90      public static function fetch_all($params) {}
  91  
  92      /**
  93       * Return status of this criteria completion
  94       *
  95       * @return bool
  96       */
  97      public function is_complete() {
  98          return (bool) $this->timecompleted;
  99      }
 100  
 101      /**
 102       * Mark this criteria complete for the associated user
 103       *
 104       * This method creates a course_completion_crit_compl record
 105       *
 106       * @param   int $timecompleted Time completed (optional).
 107       * @return  int id of completion record.
 108       */
 109      public function mark_complete($timecompleted = null) {
 110          if (empty($timecompleted)) {
 111              $timecompleted = time();
 112          }
 113          // Create record
 114          $this->timecompleted = $timecompleted;
 115  
 116          // Save record
 117          if ($this->id) {
 118              $this->update();
 119          } else {
 120              $this->insert();
 121          }
 122  
 123          // Mark course completion record as started (if not already)
 124          $cc = array(
 125              'course'    => $this->course,
 126              'userid'    => $this->userid
 127          );
 128          $ccompletion = new completion_completion($cc);
 129          $result = $ccompletion->mark_inprogress($this->timecompleted);
 130          return $result;
 131      }
 132  
 133      /**
 134       * Attach a preloaded criteria object to this object
 135       *
 136       * @param   $criteria   object  completion_criteria
 137       */
 138      public function attach_criteria(completion_criteria $criteria) {
 139          $this->_criteria = $criteria;
 140      }
 141  
 142      /**
 143       * Return the associated criteria with this completion
 144       * If nothing attached, load from the db
 145       *
 146       * @return completion_criteria
 147       */
 148      public function get_criteria() {
 149  
 150          if (!$this->_criteria) {
 151              global $DB;
 152  
 153              $params = array(
 154                  'id'    => $this->criteriaid
 155              );
 156  
 157              $record = $DB->get_record('course_completion_criteria', $params);
 158  
 159              $this->attach_criteria(completion_criteria::factory((array) $record));
 160          }
 161  
 162          return $this->_criteria;
 163      }
 164  
 165      /**
 166       * Return criteria status text for display in reports {@link completion_criteria::get_status()}
 167       *
 168       * @return string
 169       */
 170      public function get_status() {
 171          return $this->_criteria->get_status($this);
 172      }
 173  }