Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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  /**
  18   * Unit tests for (some of) mod/assign/locallib.php.
  19   *
  20   * @package    mod_assign
  21   * @category   phpunit
  22   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once (__DIR__ . '/../locallib.php');
  31  require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
  32  
  33  class mod_assign_locallib_participants extends advanced_testcase {
  34      use mod_assign_test_generator;
  35  
  36      public function test_list_participants_blind_marking() {
  37          global $DB;
  38          $this->resetAfterTest(true);
  39  
  40          $course = $this->getDataGenerator()->create_course();
  41  
  42          $roles = $DB->get_records('role', null, '', 'shortname, id');
  43          $teacher = $this->getDataGenerator()->create_user();
  44  
  45          $this->getDataGenerator()->enrol_user($teacher->id,
  46                  $course->id,
  47                  $roles['teacher']->id);
  48  
  49          $this->setUser($teacher);
  50  
  51          // Enrol two students.
  52          $students = [];
  53          for ($i = 0; $i < 2; $i++) {
  54              $student = $this->getDataGenerator()->create_user();
  55              $this->getDataGenerator()->enrol_user($student->id,
  56                      $course->id,
  57                      $roles['student']->id);
  58              $students[$student->id] = $student;
  59          }
  60  
  61          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  62          $instance = $generator->create_instance(['course' => $course->id, 'blindmarking' => 1]);
  63          $cm = get_coursemodule_from_instance('assign', $instance->id);
  64          $context = context_module::instance($cm->id);
  65          $assign = new assign($context, $cm, $course);
  66  
  67          // Allocate IDs now.
  68          // We're testing whether the IDs are correct after allocation.
  69          assign::allocate_unique_ids($assign->get_instance()->id);
  70  
  71          $participants = $assign->list_participants(null, false);
  72  
  73          // There should be exactly two participants and they should be the students.
  74          $this->assertCount(2, $participants);
  75          foreach ($participants as $participant) {
  76              $this->assertArrayHasKey($participant->id, $students);
  77          }
  78  
  79          $keys = array_keys($participants);
  80  
  81          // Create a grading table, and query the DB This should have the same order.
  82          $table = new assign_grading_table($assign, 10, '', 0, false);
  83          $table->setup();
  84          $table->query_db(10);
  85          $this->assertEquals($keys, array_keys($table->rawdata));
  86  
  87          // Submit a file for the second student.
  88          $data = new stdClass();
  89          $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
  90                                           'text'=>'Submission text',
  91                                           'format'=>FORMAT_MOODLE);
  92          static::helper_add_submission($assign, $participants[$keys[1]], $data, 'onlinetext');
  93  
  94          // Assign has a private cache. The easiest way to clear this is to create a new instance.
  95          $assign = new assign($context, $cm, $course);
  96  
  97          $newparticipants = $assign->list_participants(null, false);
  98  
  99          // There should be exactly two participants and they should be the students.
 100          $this->assertCount(2, $newparticipants);
 101          foreach ($newparticipants as $participant) {
 102              $this->assertArrayHasKey($participant->id, $students);
 103          }
 104          $newkeys = array_keys($newparticipants);
 105  
 106          // The user who submitted first should now be listed first.
 107          $this->assertEquals($participants[$keys[1]]->id, $newparticipants[$newkeys[0]]->id);
 108          $this->assertEquals($participants[$keys[0]]->id, $newparticipants[$newkeys[1]]->id);
 109  
 110          // Submit for the other student.
 111          static::helper_add_submission($assign, $participants[$keys[0]], $data, 'onlinetext');
 112          $assign = new assign($context, $cm, $course);
 113          $newparticipants = $assign->list_participants(null, false);
 114  
 115          // The users should still be listed in order of the first submission
 116          $this->assertEquals($participants[$keys[1]]->id, $newparticipants[$newkeys[0]]->id);
 117          $this->assertEquals($participants[$keys[0]]->id, $newparticipants[$newkeys[1]]->id);
 118  
 119          // The updated grading table should have the same order as the updated participant list.
 120          $table->query_db(10);
 121          $this->assertEquals($newkeys, array_keys($table->rawdata));
 122      }
 123  
 124      /**
 125       * Tests that users who have a submission, but can no longer submit are listed.
 126       */
 127      public function test_list_participants_can_no_longer_submit() {
 128          global $DB;
 129          $this->resetAfterTest(true);
 130          // Create a role that will prevent users submitting.
 131          $role = self::getDataGenerator()->create_role();
 132          assign_capability('mod/assign:submit', CAP_PROHIBIT, $role, context_system::instance());
 133          // Create the test data.
 134          $course = self::getDataGenerator()->create_course();
 135          $coursecontext = context_course::instance($course->id);
 136          $assign = $this->create_instance($course);
 137          self::getDataGenerator()->create_and_enrol($course, 'teacher');
 138          $student1 = self::getDataGenerator()->create_and_enrol($course, 'student');
 139          $student2 = self::getDataGenerator()->create_and_enrol($course, 'student');
 140          $cannotsubmit1 = self::getDataGenerator()->create_and_enrol($course, 'student');
 141          $cannotsubmit2 = self::getDataGenerator()->create_and_enrol($course, 'student');
 142          // Create submissions for some users.
 143          $this->add_submission($student1, $assign);
 144          $this->submit_for_grading($student1, $assign);
 145          $this->add_submission($cannotsubmit1, $assign);
 146          $this->submit_for_grading($cannotsubmit1, $assign);
 147          // Remove the capability to submit from some users.
 148          role_assign($role, $cannotsubmit1->id, $coursecontext);
 149          role_assign($role, $cannotsubmit2->id, $coursecontext);
 150          // Everything is setup for the test now.
 151          $participants = $assign->list_participants(null, true);
 152          self::assertCount(3, $participants);
 153          self::assertArrayHasKey($student1->id, $participants);
 154          self::assertArrayHasKey($student2->id, $participants);
 155          self::assertArrayHasKey($cannotsubmit1->id, $participants);
 156      }
 157  
 158      public function helper_add_submission($assign, $user, $data, $type) {
 159          global $USER;
 160  
 161          $previoususer = $USER;
 162  
 163          $this->setUser($user);
 164          $submission = $assign->get_user_submission($user->id, true);
 165          $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
 166  
 167          $rc = new ReflectionClass('assign');
 168          $rcm = $rc->getMethod('update_submission');
 169          $rcm->setAccessible(true);
 170          $rcm->invokeArgs($assign, [$submission, $user->id, true, false]);
 171  
 172          $plugin = $assign->get_submission_plugin_by_type($type);
 173          $plugin->save($submission, $data);
 174  
 175          $this->setUser($previoususer);
 176      }
 177  }