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  /**
  18   * This file contains the course 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 course completion
  31   *
  32   * This course completion criteria depends on another course with
  33   * completion enabled to be marked as complete for this user
  34   *
  35   * @package core_completion
  36   * @category completion
  37   * @copyright 2009 Catalyst IT Ltd
  38   * @author Aaron Barnes <aaronb@catalyst.net.nz>
  39   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class completion_criteria_course extends completion_criteria {
  42  
  43      /* @var int Criteria type constant */
  44      public $criteriatype = COMPLETION_CRITERIA_TYPE_COURSE;
  45  
  46      /**
  47       * Finds and returns a data_object instance based on params.
  48       *
  49       * @param array $params associative arrays varname=>value
  50       * @return data_object instance of data_object or false if none found.
  51       */
  52      public static function fetch($params) {
  53          $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_COURSE;
  54          return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
  55      }
  56  
  57      /**
  58       * Add appropriate form elements to the critieria form
  59       *
  60       * @param moodle_form $mform Moodle forms object
  61       * @param stdClass $data data used to define default value of the form
  62       */
  63      public function config_form_display(&$mform, $data = null) {
  64          global $CFG;
  65  
  66          $link = "<a href=\"{$CFG->wwwroot}/course/view.php?id={$data->id}\">".s($data->fullname).'</a>';
  67          $mform->addElement('checkbox', 'criteria_course['.$data->id.']', $link);
  68  
  69          if ($this->id) {
  70              $mform->setDefault('criteria_course['.$data->id.']', 1);
  71          }
  72      }
  73  
  74      /**
  75       * Update the criteria information stored in the database
  76       *
  77       * @param array $data Form data
  78       */
  79      public function update_config(&$data) {
  80  
  81          if (!empty($data->criteria_course) && is_array($data->criteria_course)) {
  82  
  83              $this->course = $data->id;
  84  
  85              foreach ($data->criteria_course as $course) {
  86  
  87                  $this->courseinstance = $course;
  88                  $this->id = NULL;
  89                  $this->insert();
  90              }
  91          }
  92      }
  93  
  94      /**
  95       * Review this criteria and decide if the user has completed
  96       *
  97       * @param completion_completion $completion The user's completion record
  98       * @param bool $mark Optionally set false to not save changes to database
  99       * @return bool
 100       */
 101      public function review($completion, $mark = true) {
 102          global $DB;
 103  
 104          $course = $DB->get_record('course', array('id' => $this->courseinstance));
 105          $info = new completion_info($course);
 106  
 107          // If the course is complete
 108          if ($info->is_course_complete($completion->userid)) {
 109  
 110              if ($mark) {
 111                  $completion->mark_complete();
 112              }
 113  
 114              return true;
 115          }
 116  
 117          return false;
 118      }
 119  
 120      /**
 121       * Return criteria title for display in reports
 122       *
 123       * @return string
 124       */
 125      public function get_title() {
 126          return get_string('dependenciescompleted', 'completion');
 127      }
 128  
 129      /**
 130       * Return a more detailed criteria title for display in reports
 131       *
 132       * @return string
 133       */
 134      public function get_title_detailed() {
 135          global $DB;
 136  
 137          $prereq = $DB->get_record('course', array('id' => $this->courseinstance));
 138          $coursecontext = context_course::instance($prereq->id, MUST_EXIST);
 139          $fullname = format_string($prereq->fullname, true, array('context' => $coursecontext));
 140          return shorten_text(urldecode($fullname));
 141      }
 142  
 143      /**
 144       * Return criteria type title for display in reports
 145       *
 146       * @return string
 147       */
 148      public function get_type_title() {
 149          return get_string('dependencies', 'completion');
 150      }
 151  
 152      /**
 153       * Find user's who have completed this criteria
 154       */
 155      public function cron() {
 156  
 157          global $DB;
 158  
 159          // Get all users who meet this criteria
 160          $sql = "
 161              SELECT DISTINCT
 162                  c.id AS course,
 163                  cr.id AS criteriaid,
 164                  ra.userid AS userid,
 165                  cc.timecompleted AS timecompleted
 166              FROM
 167                  {course_completion_criteria} cr
 168              INNER JOIN
 169                  {course} c
 170               ON cr.course = c.id
 171              INNER JOIN
 172                  {context} con
 173               ON con.instanceid = c.id
 174              INNER JOIN
 175                  {role_assignments} ra
 176                ON ra.contextid = con.id
 177              INNER JOIN
 178                  {course_completions} cc
 179               ON cc.course = cr.courseinstance
 180              AND cc.userid = ra.userid
 181              LEFT JOIN
 182                  {course_completion_crit_compl} ccc
 183               ON ccc.criteriaid = cr.id
 184              AND ccc.userid = ra.userid
 185              WHERE
 186                  cr.criteriatype = ".COMPLETION_CRITERIA_TYPE_COURSE."
 187              AND con.contextlevel = ".CONTEXT_COURSE."
 188              AND c.enablecompletion = 1
 189              AND ccc.id IS NULL
 190              AND cc.timecompleted IS NOT NULL
 191          ";
 192  
 193          // Loop through completions, and mark as complete
 194          $rs = $DB->get_recordset_sql($sql);
 195          foreach ($rs as $record) {
 196              $completion = new completion_criteria_completion((array) $record, DATA_OBJECT_FETCH_BY_KEY);
 197              $completion->mark_complete($record->timecompleted);
 198          }
 199          $rs->close();
 200      }
 201  
 202      /**
 203       * Return criteria progress details for display in reports
 204       *
 205       * @param completion_completion $completion The user's completion record
 206       * @return array An array with the following keys:
 207       *     type, criteria, requirement, status
 208       */
 209      public function get_details($completion) {
 210          global $CFG, $DB;
 211  
 212          // Get completion info
 213          $course = new stdClass();
 214          $course->id = $completion->course;
 215          $info = new completion_info($course);
 216  
 217          $prereq = $DB->get_record('course', array('id' => $this->courseinstance));
 218          $coursecontext = context_course::instance($prereq->id, MUST_EXIST);
 219          $fullname = format_string($prereq->fullname, true, array('context' => $coursecontext));
 220  
 221          $prereq_info = new completion_info($prereq);
 222  
 223          $details = array();
 224          $details['type'] = $this->get_title();
 225          $details['criteria'] = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$this->courseinstance.'">'.s($fullname).'</a>';
 226          $details['requirement'] = get_string('coursecompleted', 'completion');
 227          $details['status'] = '<a href="'.$CFG->wwwroot.'/blocks/completionstatus/details.php?course='.$this->courseinstance.'">'.get_string('seedetails', 'completion').'</a>';
 228  
 229          return $details;
 230      }
 231  }