Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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   * Potential cognitive depth indicator.
  19   *
  20   * @package   core_course
  21   * @copyright 2017 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\indicator;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use \core_analytics\local\indicator\community_of_inquiry_activity;
  30  
  31  /**
  32   * Potential cognitive depth indicator.
  33   *
  34   * It extends linear instead of discrete as there is a linear relation between
  35   * the different cognitive levels activities can reach.
  36   *
  37   * @package   core_course
  38   * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class potential_cognitive_depth extends \core_analytics\local\indicator\linear {
  42  
  43      /**
  44       * get_name
  45       *
  46       * @return \lang_string
  47       */
  48      public static function get_name() : \lang_string {
  49          return new \lang_string('indicator:potentialcognitive', 'moodle');
  50      }
  51  
  52      /**
  53       * Specify the required data to process this indicator.
  54       *
  55       * @return string[]
  56       */
  57      public static function required_sample_data() {
  58          // We require course because, although this indicator can also work with course_modules we can't
  59          // calculate anything without the course.
  60          return array('course');
  61      }
  62  
  63      /**
  64       * calculate_sample
  65       *
  66       * @throws \coding_exception
  67       * @param int $sampleid
  68       * @param string $sampleorigin
  69       * @param int|false $notusedstarttime
  70       * @param int|false $notusedendtime
  71       * @return float
  72       */
  73      public function calculate_sample($sampleid, $sampleorigin, $notusedstarttime = false, $notusedendtime = false) {
  74  
  75          if ($sampleorigin === 'course_modules') {
  76              $cm = $this->retrieve('course_modules', $sampleid);
  77              $cminfo = \cm_info::create($cm);
  78  
  79              $cognitivedepthindicator = $this->get_cognitive_indicator($cminfo->modname);
  80              $potentiallevel = $cognitivedepthindicator->get_cognitive_depth_level($cminfo);
  81              if ($potentiallevel > community_of_inquiry_activity::MAX_COGNITIVE_LEVEL) {
  82                  throw new \coding_exception('Maximum cognitive depth level is ' .
  83                      community_of_inquiry_activity::MAX_COGNITIVE_LEVEL . ', ' . $potentiallevel . ' provided by ' .
  84                          get_class($this));
  85              }
  86  
  87          } else {
  88              $course = $this->retrieve('course', $sampleid);
  89              $modinfo = get_fast_modinfo($course);
  90  
  91              $cms = $modinfo->get_cms();
  92              if (!$cms) {
  93                  return self::get_min_value();
  94              }
  95  
  96              $potentiallevel = 0;
  97              foreach ($cms as $cm) {
  98                  if (!$cognitivedepthindicator = $this->get_cognitive_indicator($cm->modname)) {
  99                      continue;
 100                  }
 101                  $level = $cognitivedepthindicator->get_cognitive_depth_level($cm);
 102                  if ($level > community_of_inquiry_activity::MAX_COGNITIVE_LEVEL) {
 103                      throw new \coding_exception('Maximum cognitive depth level is ' .
 104                          community_of_inquiry_activity::MAX_COGNITIVE_LEVEL . ', ' . $level . ' provided by ' . get_class($this));
 105                  }
 106                  if ($level > $potentiallevel) {
 107                      $potentiallevel = $level;
 108                  }
 109              }
 110          }
 111  
 112          // Values from -1 to 1 range split in 5 parts (the max cognitive depth level).
 113          // Note that we divide by 4 because we start from -1.
 114          $levelscore = round((self::get_max_value() - self::get_min_value()) / 4, 2);
 115          // We substract $levelscore because we want to start from the lower score and there is no cognitive depth level 0.
 116          return self::get_min_value() + ($levelscore * $potentiallevel) - $levelscore;
 117      }
 118  
 119      /**
 120       * Returns the cognitive depth class of this indicator.
 121       *
 122       * @param string $modname
 123       * @return \core_analytics\local\indicator\base|false
 124       */
 125      protected function get_cognitive_indicator($modname) {
 126          $indicators = \core_analytics\manager::get_all_indicators();
 127          foreach ($indicators as $indicator) {
 128              if ($indicator instanceof community_of_inquiry_activity &&
 129                      $indicator->get_indicator_type() === community_of_inquiry_activity::INDICATOR_COGNITIVE &&
 130                      $indicator->get_activity_type() === $modname) {
 131                  return $indicator;
 132              }
 133          }
 134          return false;
 135      }
 136  }