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]

   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   * locallib tests.
  19   *
  20   * @package    mod_lesson
  21   * @category   test
  22   * @copyright  2016 Adrian Greeve <adrian@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace mod_lesson;
  26  
  27  use lesson;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  global $CFG;
  32  
  33  require_once($CFG->dirroot.'/mod/lesson/locallib.php');
  34  
  35  /**
  36   * locallib testcase.
  37   *
  38   * @package    mod_lesson
  39   * @category   test
  40   * @copyright  2016 Adrian Greeve <adrian@moodle.com>
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class locallib_test extends \advanced_testcase {
  44  
  45      /**
  46       * Test duplicating a lesson page element.
  47       */
  48      public function test_duplicate_page() {
  49          global $DB;
  50  
  51          $this->resetAfterTest();
  52          $this->setAdminUser();
  53          $course = $this->getDataGenerator()->create_course();
  54          $lessonmodule = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id));
  55          // Convert to a lesson object.
  56          $lesson = new lesson($lessonmodule);
  57  
  58          // Set up a generator to create content.
  59          $generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
  60          $tfrecord = $generator->create_question_truefalse($lesson);
  61          $lesson->duplicate_page($tfrecord->id);
  62  
  63          // Lesson pages.
  64          $records = $DB->get_records('lesson_pages', array('qtype' => 2));
  65          $sameelements = array('lessonid', 'qtype', 'qoption', 'layout', 'display', 'title', 'contents', 'contentsformat');
  66          $baserecord = array_shift($records);
  67          $secondrecord = array_shift($records);
  68          foreach ($sameelements as $element) {
  69              $this->assertEquals($baserecord->$element, $secondrecord->$element);
  70          }
  71          // Need lesson answers as well.
  72          $baserecordanswers = array_values($DB->get_records('lesson_answers', array('pageid' => $baserecord->id)));
  73          $secondrecordanswers = array_values($DB->get_records('lesson_answers', array('pageid' => $secondrecord->id)));
  74          $sameanswerelements = array('lessonid', 'jumpto', 'grade', 'score', 'flags', 'answer', 'answerformat', 'response',
  75                  'responseformat');
  76          foreach ($baserecordanswers as $key => $baseanswer) {
  77              foreach ($sameanswerelements as $element) {
  78                  $this->assertEquals($baseanswer->$element, $secondrecordanswers[$key]->$element);
  79              }
  80          }
  81      }
  82  
  83      /**
  84       * Test test_lesson_get_user_deadline().
  85       */
  86      public function test_lesson_get_user_deadline() {
  87          global $DB;
  88  
  89          $this->resetAfterTest();
  90          $this->setAdminUser();
  91  
  92          $basetimestamp = time(); // The timestamp we will base the enddates on.
  93  
  94          // Create generator, course and lessons.
  95          $student1 = $this->getDataGenerator()->create_user();
  96          $student2 = $this->getDataGenerator()->create_user();
  97          $student3 = $this->getDataGenerator()->create_user();
  98          $teacher = $this->getDataGenerator()->create_user();
  99          $course = $this->getDataGenerator()->create_course();
 100          $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
 101  
 102          // Both lessons close in two hours.
 103          $lesson1 = $lessongenerator->create_instance(array('course' => $course->id, 'deadline' => $basetimestamp + 7200));
 104          $lesson2 = $lessongenerator->create_instance(array('course' => $course->id, 'deadline' => $basetimestamp + 7200));
 105          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 106          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 107  
 108          $student1id = $student1->id;
 109          $student2id = $student2->id;
 110          $student3id = $student3->id;
 111          $teacherid = $teacher->id;
 112  
 113          // Users enrolments.
 114          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 115          $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
 116          $this->getDataGenerator()->enrol_user($student1id, $course->id, $studentrole->id, 'manual');
 117          $this->getDataGenerator()->enrol_user($student2id, $course->id, $studentrole->id, 'manual');
 118          $this->getDataGenerator()->enrol_user($student3id, $course->id, $studentrole->id, 'manual');
 119          $this->getDataGenerator()->enrol_user($teacherid, $course->id, $teacherrole->id, 'manual');
 120  
 121          // Create groups.
 122          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 123          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 124          $group1id = $group1->id;
 125          $group2id = $group2->id;
 126          $this->getDataGenerator()->create_group_member(array('userid' => $student1id, 'groupid' => $group1id));
 127          $this->getDataGenerator()->create_group_member(array('userid' => $student2id, 'groupid' => $group2id));
 128  
 129          // Group 1 gets an group override for lesson 1 to close in three hours.
 130          $record1 = (object) [
 131              'lessonid' => $lesson1->id,
 132              'groupid' => $group1id,
 133              'deadline' => $basetimestamp + 10800 // In three hours.
 134          ];
 135          $DB->insert_record('lesson_overrides', $record1);
 136  
 137          // Let's test lesson 1 closes in three hours for user student 1 since member of group 1.
 138          // lesson 2 closes in two hours.
 139          $this->setUser($student1id);
 140          $params = new \stdClass();
 141  
 142          $comparearray = array();
 143          $object = new \stdClass();
 144          $object->id = $lesson1->id;
 145          $object->userdeadline = $basetimestamp + 10800; // The overriden deadline for lesson 1.
 146  
 147          $comparearray[$lesson1->id] = $object;
 148  
 149          $object = new \stdClass();
 150          $object->id = $lesson2->id;
 151          $object->userdeadline = $basetimestamp + 7200; // The unchanged deadline for lesson 2.
 152  
 153          $comparearray[$lesson2->id] = $object;
 154  
 155          $this->assertEquals($comparearray, lesson_get_user_deadline($course->id));
 156  
 157          // Let's test lesson 1 closes in two hours (the original value) for user student 3 since member of no group.
 158          $this->setUser($student3id);
 159          $params = new \stdClass();
 160  
 161          $comparearray = array();
 162          $object = new \stdClass();
 163          $object->id = $lesson1->id;
 164          $object->userdeadline = $basetimestamp + 7200; // The original deadline for lesson 1.
 165  
 166          $comparearray[$lesson1->id] = $object;
 167  
 168          $object = new \stdClass();
 169          $object->id = $lesson2->id;
 170          $object->userdeadline = $basetimestamp + 7200; // The original deadline for lesson 2.
 171  
 172          $comparearray[$lesson2->id] = $object;
 173  
 174          $this->assertEquals($comparearray, lesson_get_user_deadline($course->id));
 175  
 176          // User 2 gets an user override for lesson 1 to close in four hours.
 177          $record2 = (object) [
 178              'lessonid' => $lesson1->id,
 179              'userid' => $student2id,
 180              'deadline' => $basetimestamp + 14400 // In four hours.
 181          ];
 182          $DB->insert_record('lesson_overrides', $record2);
 183  
 184          // Let's test lesson 1 closes in four hours for user student 2 since personally overriden.
 185          // lesson 2 closes in two hours.
 186          $this->setUser($student2id);
 187  
 188          $comparearray = array();
 189          $object = new \stdClass();
 190          $object->id = $lesson1->id;
 191          $object->userdeadline = $basetimestamp + 14400; // The overriden deadline for lesson 1.
 192  
 193          $comparearray[$lesson1->id] = $object;
 194  
 195          $object = new \stdClass();
 196          $object->id = $lesson2->id;
 197          $object->userdeadline = $basetimestamp + 7200; // The unchanged deadline for lesson 2.
 198  
 199          $comparearray[$lesson2->id] = $object;
 200  
 201          $this->assertEquals($comparearray, lesson_get_user_deadline($course->id));
 202  
 203          // Let's test a teacher sees the original times.
 204          // lesson 1 and lesson 2 close in two hours.
 205          $this->setUser($teacherid);
 206  
 207          $comparearray = array();
 208          $object = new \stdClass();
 209          $object->id = $lesson1->id;
 210          $object->userdeadline = $basetimestamp + 7200; // The unchanged deadline for lesson 1.
 211  
 212          $comparearray[$lesson1->id] = $object;
 213  
 214          $object = new \stdClass();
 215          $object->id = $lesson2->id;
 216          $object->userdeadline = $basetimestamp + 7200; // The unchanged deadline for lesson 2.
 217  
 218          $comparearray[$lesson2->id] = $object;
 219  
 220          $this->assertEquals($comparearray, lesson_get_user_deadline($course->id));
 221      }
 222  
 223      public function test_is_participant() {
 224          global $USER, $DB;
 225          $this->resetAfterTest();
 226          $this->setAdminUser();
 227          $course = $this->getDataGenerator()->create_course();
 228          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 229          $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student', [], 'manual', 0, 0, ENROL_USER_SUSPENDED);
 230          $lessonmodule = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id));
 231  
 232          // Login as student.
 233          $this->setUser($student);
 234          // Convert to a lesson object.
 235          $lesson = new lesson($lessonmodule);
 236          $this->assertEquals(true, $lesson->is_participant($student->id),
 237              'Student is enrolled, active and can participate');
 238  
 239          // Login as student2.
 240          $this->setUser($student2);
 241          $this->assertEquals(false, $lesson->is_participant($student2->id),
 242              'Student is enrolled, suspended and can NOT participate');
 243  
 244          // Login as an admin.
 245          $this->setAdminUser();
 246          $this->assertEquals(false, $lesson->is_participant($USER->id),
 247              'Admin is not enrolled and can NOT participate');
 248  
 249          $this->getDataGenerator()->enrol_user(2, $course->id);
 250          $this->assertEquals(true, $lesson->is_participant($USER->id),
 251              'Admin is enrolled and can participate');
 252  
 253          $this->getDataGenerator()->enrol_user(2, $course->id, [], 'manual', 0, 0, ENROL_USER_SUSPENDED);
 254          $this->assertEquals(true, $lesson->is_participant($USER->id),
 255              'Admin is enrolled, suspended and can participate');
 256      }
 257  
 258      /**
 259       * Data provider for test_get_last_attempt.
 260       *
 261       * @return array
 262       */
 263      public function get_last_attempt_dataprovider() {
 264          return [
 265              [0, [(object)['id' => 1], (object)['id' => 2], (object)['id' => 3]], (object)['id' => 3]],
 266              [1, [(object)['id' => 1], (object)['id' => 2], (object)['id' => 3]], (object)['id' => 1]],
 267              [2, [(object)['id' => 1], (object)['id' => 2], (object)['id' => 3]], (object)['id' => 2]],
 268              [3, [(object)['id' => 1], (object)['id' => 2], (object)['id' => 3]], (object)['id' => 3]],
 269              [4, [(object)['id' => 1], (object)['id' => 2], (object)['id' => 3]], (object)['id' => 3]],
 270          ];
 271      }
 272  
 273      /**
 274       * Test the get_last_attempt() method.
 275       *
 276       * @dataProvider get_last_attempt_dataprovider
 277       * @param int $maxattempts Lesson setting.
 278       * @param array $attempts The list of student attempts.
 279       * @param object $expected Expected result.
 280       */
 281      public function test_get_last_attempt($maxattempts, $attempts, $expected) {
 282          $this->resetAfterTest();
 283          $this->setAdminUser();
 284          $course = $this->getDataGenerator()->create_course();
 285          $lesson = $this->getDataGenerator()->create_module('lesson', ['course' => $course, 'maxattempts' => $maxattempts]);
 286          $lesson = new lesson($lesson);
 287          $this->assertEquals($expected, $lesson->get_last_attempt($attempts));
 288      }
 289  }