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   * No teaching target.
  19   *
  20   * @package   core_course
  21   * @copyright 2016 David Monllao {@link http://www.davidmonllao.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  /**
  30   * No teaching target.
  31   *
  32   * @package   core_course
  33   * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class no_teaching extends \core_analytics\local\target\binary {
  37  
  38      /**
  39       * Machine learning backends are not required to predict.
  40       *
  41       * @return bool
  42       */
  43      public static function based_on_assumptions() {
  44          return true;
  45      }
  46  
  47      /**
  48       * It requires a specific time-splitting method.
  49       *
  50       * @param  \core_analytics\local\time_splitting\base $timesplitting
  51       * @return bool
  52       */
  53      public function can_use_timesplitting(\core_analytics\local\time_splitting\base $timesplitting): bool {
  54          return (get_class($timesplitting) === \core\analytics\time_splitting\single_range::class);
  55      }
  56  
  57      /**
  58       * Returns the name.
  59       *
  60       * If there is a corresponding '_help' string this will be shown as well.
  61       *
  62       * @return \lang_string
  63       */
  64      public static function get_name() : \lang_string {
  65          return new \lang_string('target:noteachingactivity', 'course');
  66      }
  67  
  68      /**
  69       * Overwritten to show a simpler language string.
  70       *
  71       * @param  int $modelid
  72       * @param  \context $context
  73       * @return string
  74       */
  75      public function get_insight_subject(int $modelid, \context $context) {
  76          return get_string('noteachingupcomingcourses');
  77      }
  78  
  79      /**
  80       * Returns the body message for the insight.
  81       *
  82       * @param  \context     $context
  83       * @param  string       $contextname
  84       * @param  \stdClass    $user
  85       * @param  \moodle_url  $insighturl
  86       * @return string[]                     The plain text message and the HTML message
  87       */
  88      public function get_insight_body(\context $context, string $contextname, \stdClass $user, \moodle_url $insighturl): array {
  89          global $OUTPUT;
  90  
  91          $a = (object)['userfirstname' => $user->firstname];
  92          $fullmessage = get_string('noteachinginfomessage', 'course', $a) . PHP_EOL . PHP_EOL . $insighturl->out(false);
  93          $fullmessagehtml = $OUTPUT->render_from_template('core_analytics/insight_info_message',
  94              ['url' => $insighturl->out(false), 'insightinfomessage' => get_string('noteachinginfomessage', 'course', $a)]
  95          );
  96  
  97          return [$fullmessage, $fullmessagehtml];
  98      }
  99  
 100      /**
 101       * prediction_actions
 102       *
 103       * @param \core_analytics\prediction $prediction
 104       * @param mixed $includedetailsaction
 105       * @param bool $isinsightuser
 106       * @return \core_analytics\prediction_action[]
 107       */
 108      public function prediction_actions(\core_analytics\prediction $prediction, $includedetailsaction = false,
 109              $isinsightuser = false) {
 110          global $CFG;
 111  
 112          require_once($CFG->dirroot . '/course/lib.php');
 113  
 114          $sampledata = $prediction->get_sample_data();
 115          $course = $sampledata['course'];
 116  
 117          $actions = array();
 118  
 119          $url = new \moodle_url('/course/view.php', array('id' => $course->id));
 120          $pix = new \pix_icon('i/course', get_string('course'));
 121          $actions[] = new \core_analytics\prediction_action('viewcourse', $prediction,
 122              $url, $pix, get_string('view'));
 123  
 124          if (course_can_view_participants($sampledata['context'])) {
 125              $url = new \moodle_url('/user/index.php', array('id' => $course->id));
 126              $pix = new \pix_icon('i/cohort', get_string('participants'));
 127              $actions[] = new \core_analytics\prediction_action('viewparticipants', $prediction,
 128                  $url, $pix, get_string('participants'));
 129          }
 130  
 131          $parentactions = parent::prediction_actions($prediction, $includedetailsaction, $isinsightuser);
 132  
 133          return array_merge($actions, $parentactions);
 134      }
 135  
 136      /**
 137       * classes_description
 138       *
 139       * @return string[]
 140       */
 141      protected static function classes_description() {
 142          return array(
 143              get_string('targetlabelteachingyes', 'course'),
 144              get_string('targetlabelteachingno', 'course'),
 145          );
 146      }
 147  
 148      /**
 149       * get_analyser_class
 150       *
 151       * @return string
 152       */
 153      public function get_analyser_class() {
 154          return '\core\analytics\analyser\site_courses';
 155      }
 156  
 157      /**
 158       * is_valid_analysable
 159       *
 160       * @param \core_analytics\analysable $analysable
 161       * @param mixed $fortraining
 162       * @return true|string
 163       */
 164      public function is_valid_analysable(\core_analytics\analysable $analysable, $fortraining = true) {
 165          // The analysable is the site, so yes, it is always valid.
 166          return true;
 167      }
 168  
 169      /**
 170       * Only process samples which start date is getting close.
 171       *
 172       * @param int $sampleid
 173       * @param \core_analytics\analysable $analysable
 174       * @param bool $fortraining
 175       * @return bool
 176       */
 177      public function is_valid_sample($sampleid, \core_analytics\analysable $analysable, $fortraining = true) {
 178  
 179          $course = $this->retrieve('course', $sampleid);
 180  
 181          $now = time();
 182  
 183          // No courses without start date, no finished courses, no predictions before start - 1 week nor
 184          // predictions for courses that started more than 1 week ago.
 185          if (!$course->startdate || (!empty($course->enddate) && $course->enddate < $now) ||
 186                  $course->startdate - WEEKSECS > $now || $course->startdate + WEEKSECS < $now) {
 187              return false;
 188          }
 189          return true;
 190      }
 191  
 192      /**
 193       * calculate_sample
 194       *
 195       * @param int $sampleid
 196       * @param \core_analytics\analysable $analysable
 197       * @param int $starttime
 198       * @param int $endtime
 199       * @return float
 200       */
 201      protected function calculate_sample($sampleid, \core_analytics\analysable $analysable, $starttime = false, $endtime = false) {
 202  
 203          $noteachersindicator = $this->retrieve('\core_course\analytics\indicator\no_teacher', $sampleid);
 204          $nostudentsindicator = $this->retrieve('\core_course\analytics\indicator\no_student', $sampleid);
 205          if ($noteachersindicator == \core_course\analytics\indicator\no_teacher::get_min_value() ||
 206                  $nostudentsindicator == \core_course\analytics\indicator\no_student::get_min_value()) {
 207              // No teachers or no students :(.
 208              return 1;
 209          }
 210          return 0;
 211      }
 212  }