Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [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      /** @var \stdClass Course record. */
  29      protected $course;
  30  
  31      /** @var \stdClass Student 1 user record. */
  32      protected $stu1;
  33  
  34      /** @var \stdClass Student 2 user record. */
  35      protected $stu2;
  36  
  37      /** @var \stdClass Student both user record. */
  38      protected $both;
  39  
  40      /** @var \stdClass Editing teacher user record. */
  41      protected $editingteacher;
  42  
  43      /** @var \stdClass Teacher user record. */
  44      protected $teacher;
  45  
  46      /** @var int Student role ID record. */
  47      protected $studentroleid;
  48  
  49      /** @var int Editing teacher role ID record. */
  50      protected $editingteacherroleid;
  51  
  52      /** @var int Teacher role ID record. */
  53      protected $teacherroleid;
  54  
  55      public function setUp(): void {
  56          global $DB;
  57  
  58          $this->course = $this->getDataGenerator()->create_course(['startdate' => 0]);
  59          $this->stu1 = $this->getDataGenerator()->create_user();
  60          $this->stu2 = $this->getDataGenerator()->create_user();
  61          $this->both = $this->getDataGenerator()->create_user();
  62          $this->editingteacher = $this->getDataGenerator()->create_user();
  63          $this->teacher = $this->getDataGenerator()->create_user();
  64  
  65          $this->studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
  66          $this->editingteacherroleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
  67          $this->teacherroleid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
  68  
  69          $this->getDataGenerator()->enrol_user($this->stu1->id, $this->course->id, $this->studentroleid);
  70          $this->getDataGenerator()->enrol_user($this->stu2->id, $this->course->id, $this->studentroleid);
  71          $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->studentroleid);
  72          $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->editingteacherroleid);
  73          $this->getDataGenerator()->enrol_user($this->editingteacher->id, $this->course->id, $this->editingteacherroleid);
  74          $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherroleid);
  75      }
  76  
  77      /**
  78       * Users tests.
  79       */
  80      public function test_users() {
  81          global $DB;
  82  
  83          $this->resetAfterTest(true);
  84  
  85          $courseman = new \core_analytics\course($this->course->id);
  86          $this->assertCount(3, $courseman->get_user_ids(array($this->studentroleid)));
  87          $this->assertCount(2, $courseman->get_user_ids(array($this->editingteacherroleid)));
  88          $this->assertCount(1, $courseman->get_user_ids(array($this->teacherroleid)));
  89  
  90          // Distinct is applied.
  91          $this->assertCount(3, $courseman->get_user_ids(array($this->editingteacherroleid, $this->teacherroleid)));
  92          $this->assertCount(4, $courseman->get_user_ids(array($this->editingteacherroleid, $this->studentroleid)));
  93      }
  94  
  95      /**
  96       * Course validation tests.
  97       *
  98       * @return void
  99       */
 100      public function test_course_validation() {
 101          global $DB;
 102  
 103          $this->resetAfterTest(true);
 104  
 105          $courseman = new \core_analytics\course($this->course->id);
 106          $this->assertFalse($courseman->was_started());
 107          $this->assertFalse($courseman->is_finished());
 108  
 109          // Nothing should change when assigning as teacher.
 110          for ($i = 0; $i < 10; $i++) {
 111              $user = $this->getDataGenerator()->create_user();
 112              $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->teacherroleid);
 113          }
 114          $courseman = new \core_analytics\course($this->course->id);
 115          $this->assertFalse($courseman->was_started());
 116          $this->assertFalse($courseman->is_finished());
 117  
 118          // More students now.
 119          for ($i = 0; $i < 10; $i++) {
 120              $user = $this->getDataGenerator()->create_user();
 121              $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->studentroleid);
 122          }
 123          $courseman = new \core_analytics\course($this->course->id);
 124          $this->assertFalse($courseman->was_started());
 125          $this->assertFalse($courseman->is_finished());
 126  
 127          // Valid start date unknown end date.
 128          $this->course->startdate = gmmktime('0', '0', '0', 10, 24, 2015);
 129          $DB->update_record('course', $this->course);
 130          $courseman = new \core_analytics\course($this->course->id);
 131          $this->assertTrue($courseman->was_started());
 132          $this->assertFalse($courseman->is_finished());
 133  
 134          // Valid start and end date.
 135          $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2016);
 136          $DB->update_record('course', $this->course);
 137          $courseman = new \core_analytics\course($this->course->id);
 138          $this->assertTrue($courseman->was_started());
 139          $this->assertTrue($courseman->is_finished());
 140  
 141          // Valid start and ongoing course.
 142          $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2286);
 143          $DB->update_record('course', $this->course);
 144          $courseman = new \core_analytics\course($this->course->id);
 145          $this->assertTrue($courseman->was_started());
 146          $this->assertFalse($courseman->is_finished());
 147      }
 148  
 149      /**
 150       * Get the minimum time that is considered valid according to guess_end logic.
 151       *
 152       * @param int $time
 153       * @return int
 154       */
 155      protected function time_greater_than($time) {
 156          return $time - (WEEKSECS * 2);
 157      }
 158  
 159      /**
 160       * Get the maximum time that is considered valid according to guess_end logic.
 161       *
 162       * @param int $time
 163       * @return int
 164       */
 165      protected function time_less_than($time) {
 166          return $time + (WEEKSECS * 2);
 167      }
 168  }