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