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.

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

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