Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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_completion;
  18  
  19  use completion_completion;
  20  
  21  /**
  22   * Test completion progress API.
  23   *
  24   * @package core_completion
  25   * @category test
  26   * @copyright 2017 Mark Nelson <markn@moodle.com>
  27   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class progress_test extends \advanced_testcase {
  30  
  31      /**
  32       * Test setup.
  33       */
  34      public function setUp(): void {
  35          global $CFG;
  36  
  37          $CFG->enablecompletion = true;
  38          $this->resetAfterTest();
  39      }
  40  
  41      /**
  42       * Tests that the course progress percentage is returned correctly when we have only activity completion.
  43       */
  44      public function test_course_progress_percentage_with_just_activities() {
  45          global $DB;
  46  
  47          // Add a course that supports completion.
  48          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  49  
  50          // Enrol a user in the course.
  51          $user = $this->getDataGenerator()->create_user();
  52          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  53          $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
  54  
  55          // Add four activities that use completion.
  56          $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id),
  57              array('completion' => 1));
  58          $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
  59              array('completion' => 1));
  60          $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
  61              array('completion' => 1));
  62          $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
  63              array('completion' => 1));
  64  
  65          // Add an activity that does *not* use completion.
  66          $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
  67  
  68          // Mark two of them as completed for a user.
  69          $cmassign = get_coursemodule_from_id('assign', $assign->cmid);
  70          $cmdata = get_coursemodule_from_id('data', $data->cmid);
  71          $completion = new \completion_info($course);
  72          $completion->update_state($cmassign, COMPLETION_COMPLETE, $user->id);
  73          $completion->update_state($cmdata, COMPLETION_COMPLETE, $user->id);
  74  
  75          // Check we have received valid data.
  76          // Note - only 4 out of the 5 activities support completion, and the user has completed 2 of those.
  77          $this->assertEquals('50', \core_completion\progress::get_course_progress_percentage($course, $user->id));
  78      }
  79  
  80      /**
  81       * Tests that the course progress percentage is returned correctly when we have a course and activity completion.
  82       */
  83      public function test_course_progress_percentage_with_activities_and_course() {
  84          global $DB;
  85  
  86          // Add a course that supports completion.
  87          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  88  
  89          // Enrol a user in the course.
  90          $user = $this->getDataGenerator()->create_user();
  91          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  92          $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
  93  
  94          // Add four activities that use completion.
  95          $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id),
  96              array('completion' => 1));
  97          $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
  98              array('completion' => 1));
  99          $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
 100              array('completion' => 1));
 101          $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
 102              array('completion' => 1));
 103  
 104          // Add an activity that does *not* use completion.
 105          $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
 106  
 107          // Mark two of them as completed for a user.
 108          $cmassign = get_coursemodule_from_id('assign', $assign->cmid);
 109          $cmdata = get_coursemodule_from_id('data', $data->cmid);
 110          $completion = new \completion_info($course);
 111          $completion->update_state($cmassign, COMPLETION_COMPLETE, $user->id);
 112          $completion->update_state($cmdata, COMPLETION_COMPLETE, $user->id);
 113  
 114          // Now, mark the course as completed.
 115          $ccompletion = new completion_completion(array('course' => $course->id, 'userid' => $user->id));
 116          $ccompletion->mark_complete();
 117  
 118          // Check we have received valid data.
 119          // The course completion takes priority, so should return 100.
 120          $this->assertEquals('100', \core_completion\progress::get_course_progress_percentage($course, $user->id));
 121      }
 122  
 123      /**
 124       * Tests that the course progress returns null when the course does not support it.
 125       */
 126      public function test_course_progress_course_not_using_completion() {
 127          // Create a course that does not use completion.
 128          $course = $this->getDataGenerator()->create_course();
 129  
 130          // Check that the result was null.
 131          $this->assertNull(\core_completion\progress::get_course_progress_percentage($course));
 132      }
 133  
 134      /**
 135       * Tests that the course progress returns null when there are no activities that support it.
 136       */
 137      public function test_course_progress_no_activities_using_completion() {
 138          // Create a course that does support completion.
 139          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 140  
 141          // Add an activity that does *not* support completion.
 142          $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
 143  
 144          // Check that the result was null.
 145          $this->assertNull(\core_completion\progress::get_course_progress_percentage($course));
 146      }
 147  
 148      /**
 149       * Tests that the course progress returns null for a not tracked for completion user in a course.
 150       */
 151      public function test_course_progress_not_tracked_user() {
 152          global $DB;
 153  
 154          // Add a course that supports completion.
 155          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 156  
 157          // Enrol a user in the course.
 158          $user = $this->getDataGenerator()->create_user();
 159          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 160  
 161          $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
 162  
 163          // Now, mark the course as completed.
 164          $ccompletion = new completion_completion(array('course' => $course->id, 'userid' => $user->id));
 165          $ccompletion->mark_complete();
 166  
 167          // The course completion should return 100.
 168          $this->assertEquals('100', \core_completion\progress::get_course_progress_percentage($course, $user->id));
 169  
 170          // Now make the user's role to be not tracked for completion.
 171          unassign_capability('moodle/course:isincompletionreports', $studentrole->id);
 172  
 173          // Check that the result is null now.
 174          $this->assertNull(\core_completion\progress::get_course_progress_percentage($course, $user->id));
 175      }
 176  }