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   * Course completion target.
  19   *
  20   * @package   core_course
  21   * @copyright 2019 Victor Deniz <victor@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_course\analytics\target;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->dirroot . '/course/lib.php');
  30  require_once($CFG->dirroot . '/lib/completionlib.php');
  31  require_once($CFG->dirroot . '/completion/completion_completion.php');
  32  
  33  /**
  34   * Course completion target.
  35   *
  36   * @package   core_course
  37   * @copyright 2019 Victor Deniz <victor@moodle.com>
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class course_completion extends course_enrolments {
  41  
  42      /**
  43       * Returns the name.
  44       *
  45       * If there is a corresponding '_help' string this will be shown as well.
  46       *
  47       * @return \lang_string
  48       */
  49      public static function get_name() : \lang_string {
  50          return new \lang_string('target:coursecompletion', 'course');
  51      }
  52  
  53      /**
  54       * Returns descriptions for each of the values the target calculation can return.
  55       *
  56       * @return string[]
  57       */
  58      protected static function classes_description() {
  59          return array(
  60              get_string('targetlabelstudentcompletionno', 'course'),
  61              get_string('targetlabelstudentcompletionyes', 'course')
  62          );
  63      }
  64  
  65      /**
  66       * Discards courses that are not yet ready to be used for training or prediction.
  67       *
  68       * @param \core_analytics\analysable $course
  69       * @param bool $fortraining
  70       * @return true|string
  71       */
  72      public function is_valid_analysable(\core_analytics\analysable $course, $fortraining = true) {
  73          $isvalid = parent::is_valid_analysable($course, $fortraining);
  74  
  75          if (is_string($isvalid)) {
  76              return $isvalid;
  77          }
  78  
  79          // Not a valid target if completion is not enabled or there are not completion criteria defined.
  80          $completion = new \completion_info($course->get_course_data());
  81          if (!$completion->is_enabled() || !$completion->has_criteria()) {
  82              return get_string('completionnotenabledforcourse', 'completion');
  83          }
  84  
  85          return true;
  86      }
  87  
  88      /**
  89       * Course completion sets the target value.
  90       *
  91       * @param int $sampleid
  92       * @param \core_analytics\analysable $course
  93       * @param int $starttime
  94       * @param int $endtime
  95       * @return float|null 0 -> course not completed, 1 -> course completed
  96       */
  97      protected function calculate_sample($sampleid, \core_analytics\analysable $course, $starttime = false, $endtime = false) {
  98  
  99          if (!$this->enrolment_active_during_analysis_time($sampleid, $starttime, $endtime)) {
 100              // We should not use this sample as the analysis results could be misleading.
 101              return null;
 102          }
 103  
 104          $userenrol = $this->retrieve('user_enrolments', $sampleid);
 105  
 106          // We use completion as a success metric.
 107          $ccompletion = new \completion_completion(array('userid' => $userenrol->userid, 'course' => $course->get_id()));
 108          if ($ccompletion->is_complete()) {
 109              return 0;
 110          } else {
 111              return 1;
 112          }
 113      }
 114  }