Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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   * Unit tests for course.
  19   *
  20   * @package   core_analytics
  21   * @copyright 2016 David MonllaĆ³ {@link http://www.davidmonllao.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Unit tests for course.
  29   *
  30   * @package   core_analytics
  31   * @copyright 2016 David MonllaĆ³ {@link http://www.davidmonllao.com}
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class core_analytics_course_testcase extends advanced_testcase {
  35  
  36      public function setUp(): void {
  37          global $DB;
  38  
  39          $this->course = $this->getDataGenerator()->create_course(['startdate' => 0]);
  40          $this->stu1 = $this->getDataGenerator()->create_user();
  41          $this->stu2 = $this->getDataGenerator()->create_user();
  42          $this->both = $this->getDataGenerator()->create_user();
  43          $this->editingteacher = $this->getDataGenerator()->create_user();
  44          $this->teacher = $this->getDataGenerator()->create_user();
  45  
  46          $this->studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
  47          $this->editingteacherroleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
  48          $this->teacherroleid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
  49  
  50          $this->getDataGenerator()->enrol_user($this->stu1->id, $this->course->id, $this->studentroleid);
  51          $this->getDataGenerator()->enrol_user($this->stu2->id, $this->course->id, $this->studentroleid);
  52          $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->studentroleid);
  53          $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->editingteacherroleid);
  54          $this->getDataGenerator()->enrol_user($this->editingteacher->id, $this->course->id, $this->editingteacherroleid);
  55          $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherroleid);
  56      }
  57  
  58      /**
  59       * Users tests.
  60       */
  61      public function test_users() {
  62          global $DB;
  63  
  64          $this->resetAfterTest(true);
  65  
  66          $courseman = new \core_analytics\course($this->course->id);
  67          $this->assertCount(3, $courseman->get_user_ids(array($this->studentroleid)));
  68          $this->assertCount(2, $courseman->get_user_ids(array($this->editingteacherroleid)));
  69          $this->assertCount(1, $courseman->get_user_ids(array($this->teacherroleid)));
  70  
  71          // Distinct is applied.
  72          $this->assertCount(3, $courseman->get_user_ids(array($this->editingteacherroleid, $this->teacherroleid)));
  73          $this->assertCount(4, $courseman->get_user_ids(array($this->editingteacherroleid, $this->studentroleid)));
  74      }
  75  
  76      /**
  77       * Course validation tests.
  78       *
  79       * @return void
  80       */
  81      public function test_course_validation() {
  82          global $DB;
  83  
  84          $this->resetAfterTest(true);
  85  
  86          $courseman = new \core_analytics\course($this->course->id);
  87          $this->assertFalse($courseman->was_started());
  88          $this->assertFalse($courseman->is_finished());
  89  
  90          // Nothing should change when assigning as teacher.
  91          for ($i = 0; $i < 10; $i++) {
  92              $user = $this->getDataGenerator()->create_user();
  93              $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->teacherroleid);
  94          }
  95          $courseman = new \core_analytics\course($this->course->id);
  96          $this->assertFalse($courseman->was_started());
  97          $this->assertFalse($courseman->is_finished());
  98  
  99          // More students now.
 100          for ($i = 0; $i < 10; $i++) {
 101              $user = $this->getDataGenerator()->create_user();
 102              $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->studentroleid);
 103          }
 104          $courseman = new \core_analytics\course($this->course->id);
 105          $this->assertFalse($courseman->was_started());
 106          $this->assertFalse($courseman->is_finished());
 107  
 108          // Valid start date unknown end date.
 109          $this->course->startdate = gmmktime('0', '0', '0', 10, 24, 2015);
 110          $DB->update_record('course', $this->course);
 111          $courseman = new \core_analytics\course($this->course->id);
 112          $this->assertTrue($courseman->was_started());
 113          $this->assertFalse($courseman->is_finished());
 114  
 115          // Valid start and end date.
 116          $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2016);
 117          $DB->update_record('course', $this->course);
 118          $courseman = new \core_analytics\course($this->course->id);
 119          $this->assertTrue($courseman->was_started());
 120          $this->assertTrue($courseman->is_finished());
 121  
 122          // Valid start and ongoing course.
 123          $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2286);
 124          $DB->update_record('course', $this->course);
 125          $courseman = new \core_analytics\course($this->course->id);
 126          $this->assertTrue($courseman->was_started());
 127          $this->assertFalse($courseman->is_finished());
 128      }
 129  
 130      /**
 131       * Get the minimum time that is considered valid according to guess_end logic.
 132       *
 133       * @param int $time
 134       * @return int
 135       */
 136      protected function time_greater_than($time) {
 137          return $time - (WEEKSECS * 2);
 138      }
 139  
 140      /**
 141       * Get the maximum time that is considered valid according to guess_end logic.
 142       *
 143       * @param int $time
 144       * @return int
 145       */
 146      protected function time_less_than($time) {
 147          return $time + (WEEKSECS * 2);
 148      }
 149  }