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.

Differences Between: [Versions 401 and 402] [Versions 401 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   * This file contains the date criteria type
  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 - completion on specified date
  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_date extends completion_criteria {
  39  
  40      /* @var int Criteria type constant [COMPLETION_CRITERIA_TYPE_DATE]  */
  41      public $criteriatype = COMPLETION_CRITERIA_TYPE_DATE;
  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_DATE;
  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 not used
  59       */
  60      public function config_form_display(&$mform, $data = null) {
  61          $mform->addElement('checkbox', 'criteria_date', get_string('enable'));
  62          $mform->addElement('date_selector', 'criteria_date_value', get_string('completionondatevalue', 'core_completion'));
  63          $mform->disabledIf('criteria_date_value', 'criteria_date');
  64  
  65          // If instance of criteria exists
  66          if ($this->id) {
  67              $mform->setDefault('criteria_date', 1);
  68              $mform->setDefault('criteria_date_value', $this->timeend);
  69          } else {
  70              $mform->setDefault('criteria_date_value', time() + 3600 * 24);
  71          }
  72      }
  73  
  74      /**
  75       * Update the criteria information stored in the database
  76       *
  77       * @param stdClass $data Form data
  78       */
  79      public function update_config(&$data) {
  80          if (!empty($data->criteria_date)) {
  81              $this->course = $data->id;
  82              $this->timeend = $data->criteria_date_value;
  83              $this->insert();
  84          }
  85      }
  86  
  87      /**
  88       * Review this criteria and decide if the user has completed
  89       *
  90       * @param completion_completion $completion The user's completion record
  91       * @param bool $mark Optionally set false to not save changes to database
  92       * @return bool
  93       */
  94      public function review($completion, $mark = true) {
  95          // If current time is past timeend
  96          if ($this->timeend && $this->timeend < time()) {
  97              if ($mark) {
  98                  $completion->mark_complete();
  99              }
 100  
 101              return true;
 102          }
 103          return false;
 104      }
 105  
 106      /**
 107       * Return criteria title for display in reports
 108       *
 109       * @return string
 110       */
 111      public function get_title() {
 112          return get_string('date');
 113      }
 114  
 115      /**
 116       * Return a more detailed criteria title for display in reports
 117       *
 118       * @return string
 119       */
 120      public function get_title_detailed() {
 121          return userdate($this->timeend, '%d-%h-%y');
 122      }
 123  
 124      /**
 125       * Return criteria type title for display in reports
 126       *
 127       * @return string
 128       */
 129      public function get_type_title() {
 130          return get_string('date');
 131      }
 132  
 133  
 134      /**
 135       * Return criteria status text for display in reports
 136       *
 137       * @param completion_completion $completion The user's completion record
 138       * @return string
 139       */
 140      public function get_status($completion) {
 141          return $completion->is_complete() ? get_string('yes') : userdate($this->timeend, '%d-%h-%y');
 142      }
 143  
 144      /**
 145       * Find user's who have completed this criteria
 146       */
 147      public function cron() {
 148          global $DB;
 149  
 150          // Get all users who match meet this criteria
 151          $sql = '
 152              SELECT DISTINCT
 153                  c.id AS course,
 154                  cr.timeend AS timeend,
 155                  cr.id AS criteriaid,
 156                  ra.userid AS userid
 157              FROM
 158                  {course_completion_criteria} cr
 159              INNER JOIN
 160                  {course} c
 161               ON cr.course = c.id
 162              INNER JOIN
 163                  {context} con
 164               ON con.instanceid = c.id
 165              INNER JOIN
 166                  {role_assignments} ra
 167               ON ra.contextid = con.id
 168              LEFT JOIN
 169                  {course_completion_crit_compl} cc
 170               ON cc.criteriaid = cr.id
 171              AND cc.userid = ra.userid
 172              WHERE
 173                  cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_DATE.'
 174              AND con.contextlevel = '.CONTEXT_COURSE.'
 175              AND c.enablecompletion = 1
 176              AND cc.id IS NULL
 177              AND cr.timeend < ?
 178          ';
 179  
 180          // Loop through completions, and mark as complete
 181          $rs = $DB->get_recordset_sql($sql, array(time()));
 182          foreach ($rs as $record) {
 183              $completion = new completion_criteria_completion((array) $record, DATA_OBJECT_FETCH_BY_KEY);
 184              $completion->mark_complete($record->timeend);
 185          }
 186          $rs->close();
 187      }
 188  
 189      /**
 190       * Return criteria progress details for display in reports
 191       *
 192       * @param completion_completion $completion The user's completion record
 193       * @return array An array with the following keys:
 194       *     type, criteria, requirement, status
 195       */
 196      public function get_details($completion) {
 197          $details = array();
 198          $details['type'] = get_string('datepassed', 'completion');
 199          $details['criteria'] = get_string('remainingenroleduntildate', 'completion');
 200          $details['requirement'] = userdate($this->timeend, '%d %B %Y');
 201          $details['status'] = '';
 202  
 203          return $details;
 204      }
 205  
 206      /**
 207       * Return pix_icon for display in reports.
 208       *
 209       * @param string $alt The alt text to use for the icon
 210       * @param array $attributes html attributes
 211       * @return pix_icon
 212       */
 213      public function get_icon($alt, array $attributes = null) {
 214          return new pix_icon('i/calendar', $alt, 'moodle', $attributes);
 215      }
 216  
 217      /**
 218       * Shift the date when resetting course.
 219       *
 220       * @param int $courseid the course id
 221       * @param int $timeshift number of seconds to shift date
 222       * @return boolean was the operation successful?
 223       */
 224      public static function update_date($courseid, $timeshift) {
 225          if ($criteria = self::fetch(array('course' => $courseid))) {
 226              $criteria->timeend = $criteria->timeend + $timeshift;
 227              $criteria->update();
 228          }
 229      }
 230  }