Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 29] [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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  defined('MOODLE_INTERNAL') || die();
      18  
      19  global $CFG;
      20  
      21  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
      22  
      23  /**
      24   * External mod assign functions unit tests
      25   *
      26   * @package mod_assign
      27   * @category external
      28   * @copyright 2012 Paul Charsley
      29   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      30   */
      31  class mod_assign_external_testcase extends externallib_advanced_testcase {
      32  
      33      /**
      34       * Tests set up
      35       */
      36      protected function setUp() {
      37          global $CFG;
      38          require_once($CFG->dirroot . '/mod/assign/externallib.php');
      39      }
      40  
      41      /**
      42       * Test get_grades
      43       */
      44      public function test_get_grades() {
      45          global $DB, $USER;
      46  
      47          $this->resetAfterTest(true);
      48          // Create a course and assignment.
      49          $coursedata['idnumber'] = 'idnumbercourse';
      50          $coursedata['fullname'] = 'Lightwork Course';
      51          $coursedata['summary'] = 'Lightwork Course description';
      52          $coursedata['summaryformat'] = FORMAT_MOODLE;
      53          $course = self::getDataGenerator()->create_course($coursedata);
      54  
      55          $assigndata['course'] = $course->id;
      56          $assigndata['name'] = 'lightwork assignment';
      57  
      58          $assign = self::getDataGenerator()->create_module('assign', $assigndata);
      59  
      60          // Create a manual enrolment record.
      61          $manualenroldata['enrol'] = 'manual';
      62          $manualenroldata['status'] = 0;
      63          $manualenroldata['courseid'] = $course->id;
      64          $enrolid = $DB->insert_record('enrol', $manualenroldata);
      65  
      66          // Create a teacher and give them capabilities.
      67          $context = context_course::instance($course->id);
      68          $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
      69          $context = context_module::instance($assign->cmid);
      70          $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
      71  
      72          // Create the teacher's enrolment record.
      73          $userenrolmentdata['status'] = 0;
      74          $userenrolmentdata['enrolid'] = $enrolid;
      75          $userenrolmentdata['userid'] = $USER->id;
      76          $DB->insert_record('user_enrolments', $userenrolmentdata);
      77  
      78          // Create a student and give them 2 grades (for 2 attempts).
      79          $student = self::getDataGenerator()->create_user();
      80  
      81          $submission = new stdClass();
      82          $submission->assignment = $assign->id;
      83          $submission->userid = $student->id;
      84          $submission->status = ASSIGN_SUBMISSION_STATUS_NEW;
      85          $submission->latest = 0;
      86          $submission->attemptnumber = 0;
      87          $submission->groupid = 0;
      88          $submission->timecreated = time();
      89          $submission->timemodified = time();
      90          $DB->insert_record('assign_submission', $submission);
      91  
      92          $grade = new stdClass();
      93          $grade->assignment = $assign->id;
      94          $grade->userid = $student->id;
      95          $grade->timecreated = time();
      96          $grade->timemodified = $grade->timecreated;
      97          $grade->grader = $USER->id;
      98          $grade->grade = 50;
      99          $grade->attemptnumber = 0;
     100          $DB->insert_record('assign_grades', $grade);
     101  
     102          $submission = new stdClass();
     103          $submission->assignment = $assign->id;
     104          $submission->userid = $student->id;
     105          $submission->status = ASSIGN_SUBMISSION_STATUS_NEW;
     106          $submission->latest = 1;
     107          $submission->attemptnumber = 1;
     108          $submission->groupid = 0;
     109          $submission->timecreated = time();
     110          $submission->timemodified = time();
     111          $DB->insert_record('assign_submission', $submission);
     112  
     113          $grade = new stdClass();
     114          $grade->assignment = $assign->id;
     115          $grade->userid = $student->id;
     116          $grade->timecreated = time();
     117          $grade->timemodified = $grade->timecreated;
     118          $grade->grader = $USER->id;
     119          $grade->grade = 75;
     120          $grade->attemptnumber = 1;
     121          $DB->insert_record('assign_grades', $grade);
     122  
     123          $assignmentids[] = $assign->id;
     124          $result = mod_assign_external::get_grades($assignmentids);
     125  
     126          // We need to execute the return values cleaning process to simulate the web service server.
     127          $result = external_api::clean_returnvalue(mod_assign_external::get_grades_returns(), $result);
     128  
     129          // Check that the correct grade information for the student is returned.
     130          $this->assertEquals(1, count($result['assignments']));
     131          $assignment = $result['assignments'][0];
     132          $this->assertEquals($assign->id, $assignment['assignmentid']);
     133          // Should only get the last grade for this student.
     134          $this->assertEquals(1, count($assignment['grades']));
     135          $grade = $assignment['grades'][0];
     136          $this->assertEquals($student->id, $grade['userid']);
     137          // Should be the last grade (not the first).
     138          $this->assertEquals(75, $grade['grade']);
     139      }
     140  
     141      /**
     142       * Test get_assignments
     143       */
     144      public function test_get_assignments() {
     145          global $DB, $USER;
     146  
     147          $this->resetAfterTest(true);
     148  
     149          $category = self::getDataGenerator()->create_category(array(
     150              'name' => 'Test category'
     151          ));
     152  
     153          // Create a course.
     154          $course1 = self::getDataGenerator()->create_course(array(
     155              'idnumber' => 'idnumbercourse1',
     156              'fullname' => 'Lightwork Course 1',
     157              'summary' => 'Lightwork Course 1 description',
     158              'summaryformat' => FORMAT_MOODLE,
     159              'category' => $category->id
     160          ));
     161  
     162          // Create a second course, just for testing.
     163          $course2 = self::getDataGenerator()->create_course(array(
     164              'idnumber' => 'idnumbercourse2',
     165              'fullname' => 'Lightwork Course 2',
     166              'summary' => 'Lightwork Course 2 description',
     167              'summaryformat' => FORMAT_MOODLE,
     168              'category' => $category->id
     169          ));
     170  
     171          // Create the assignment module.
     172          $assign1 = self::getDataGenerator()->create_module('assign', array(
     173              'course' => $course1->id,
     174              'name' => 'lightwork assignment',
     175              'markingworkflow' => 1,
     176              'markingallocation' => 1
     177          ));
     178  
     179          // Create manual enrolment record.
     180          $enrolid = $DB->insert_record('enrol', (object)array(
     181              'enrol' => 'manual',
     182              'status' => 0,
     183              'courseid' => $course1->id
     184          ));
     185  
     186          // Create the user and give them capabilities.
     187          $context = context_course::instance($course1->id);
     188          $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
     189          $context = context_module::instance($assign1->cmid);
     190          $this->assignUserCapability('mod/assign:view', $context->id, $roleid);
     191  
     192          // Create the user enrolment record.
     193          $DB->insert_record('user_enrolments', (object)array(
     194              'status' => 0,
     195              'enrolid' => $enrolid,
     196              'userid' => $USER->id
     197          ));
     198  
     199          $result = mod_assign_external::get_assignments();
     200  
     201          // We need to execute the return values cleaning process to simulate the web service server.
     202          $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
     203  
     204          // Check the course and assignment are returned.
     205          $this->assertEquals(1, count($result['courses']));
     206          $course = $result['courses'][0];
     207          $this->assertEquals('Lightwork Course 1', $course['fullname']);
     208          $this->assertEquals(1, count($course['assignments']));
     209          $assignment = $course['assignments'][0];
     210          $this->assertEquals($assign1->id, $assignment['id']);
     211          $this->assertEquals($course1->id, $assignment['course']);
     212          $this->assertEquals('lightwork assignment', $assignment['name']);
     213          $this->assertEquals(1, $assignment['markingworkflow']);
     214          $this->assertEquals(1, $assignment['markingallocation']);
     215  
     216          $result = mod_assign_external::get_assignments(array($course1->id));
     217  
     218          // We need to execute the return values cleaning process to simulate the web service server.
     219          $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
     220  
     221          $this->assertEquals(1, count($result['courses']));
     222          $course = $result['courses'][0];
     223          $this->assertEquals('Lightwork Course 1', $course['fullname']);
     224          $this->assertEquals(1, count($course['assignments']));
     225          $assignment = $course['assignments'][0];
     226          $this->assertEquals($assign1->id, $assignment['id']);
     227          $this->assertEquals($course1->id, $assignment['course']);
     228          $this->assertEquals('lightwork assignment', $assignment['name']);
     229          $this->assertEquals(1, $assignment['markingworkflow']);
     230          $this->assertEquals(1, $assignment['markingallocation']);
     231  
     232          $result = mod_assign_external::get_assignments(array($course2->id));
     233  
     234          // We need to execute the return values cleaning process to simulate the web service server.
     235          $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
     236  
     237          $this->assertEquals(0, count($result['courses']));
     238          $this->assertEquals(1, count($result['warnings']));
     239      }
     240  
     241      /**
     242       * Test get_submissions
     243       */
     244      public function test_get_submissions() {
     245          global $DB, $USER;
     246  
     247          $this->resetAfterTest(true);
     248          // Create a course and assignment.
     249          $coursedata['idnumber'] = 'idnumbercourse1';
     250          $coursedata['fullname'] = 'Lightwork Course 1';
     251          $coursedata['summary'] = 'Lightwork Course 1 description';
     252          $coursedata['summaryformat'] = FORMAT_MOODLE;
     253          $course1 = self::getDataGenerator()->create_course($coursedata);
     254  
     255          $assigndata['course'] = $course1->id;
     256          $assigndata['name'] = 'lightwork assignment';
     257  
     258          $assign1 = self::getDataGenerator()->create_module('assign', $assigndata);
     259  
     260          // Create a student with an online text submission.
     261          // First attempt.
     262          $student = self::getDataGenerator()->create_user();
     263          $submission = new stdClass();
     264          $submission->assignment = $assign1->id;
     265          $submission->userid = $student->id;
     266          $submission->timecreated = time();
     267          $submission->timemodified = $submission->timecreated;
     268          $submission->status = 'draft';
     269          $submission->attemptnumber = 0;
     270          $submission->latest = 0;
     271          $sid = $DB->insert_record('assign_submission', $submission);
     272  
     273          // Second attempt.
     274          $submission = new stdClass();
     275          $submission->assignment = $assign1->id;
     276          $submission->userid = $student->id;
     277          $submission->timecreated = time();
     278          $submission->timemodified = $submission->timecreated;
     279          $submission->status = 'submitted';
     280          $submission->attemptnumber = 1;
     281          $submission->latest = 1;
     282          $sid = $DB->insert_record('assign_submission', $submission);
     283          $submission->id = $sid;
     284  
     285          $onlinetextsubmission = new stdClass();
     286          $onlinetextsubmission->onlinetext = "<p>online test text</p>";
     287          $onlinetextsubmission->onlineformat = 1;
     288          $onlinetextsubmission->submission = $submission->id;
     289          $onlinetextsubmission->assignment = $assign1->id;
     290          $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
     291  
     292          // Create manual enrolment record.
     293          $manualenroldata['enrol'] = 'manual';
     294          $manualenroldata['status'] = 0;
     295          $manualenroldata['courseid'] = $course1->id;
     296          $enrolid = $DB->insert_record('enrol', $manualenroldata);
     297  
     298          // Create a teacher and give them capabilities.
     299          $context = context_course::instance($course1->id);
     300          $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
     301          $context = context_module::instance($assign1->cmid);
     302          $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
     303  
     304          // Create the teacher's enrolment record.
     305          $userenrolmentdata['status'] = 0;
     306          $userenrolmentdata['enrolid'] = $enrolid;
     307          $userenrolmentdata['userid'] = $USER->id;
     308          $DB->insert_record('user_enrolments', $userenrolmentdata);
     309  
     310          $assignmentids[] = $assign1->id;
     311          $result = mod_assign_external::get_submissions($assignmentids);
     312          $result = external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result);
     313  
     314          // Check the online text submission is returned.
     315          $this->assertEquals(1, count($result['assignments']));
     316          $assignment = $result['assignments'][0];
     317          $this->assertEquals($assign1->id, $assignment['assignmentid']);
     318          $this->assertEquals(1, count($assignment['submissions']));
     319          $submission = $assignment['submissions'][0];
     320          $this->assertEquals($sid, $submission['id']);
     321          $this->assertGreaterThanOrEqual(3, count($submission['plugins']));
     322          $plugins = $submission['plugins'];
     323          foreach ($plugins as $plugin) {
     324              $foundonlinetext = false;
     325              if ($plugin['type'] == 'onlinetext') {
     326                  $foundonlinetext = true;
     327                  break;
     328              }
     329          }
     330          $this->assertTrue($foundonlinetext);
     331      }
     332  
     333      /**
     334       * Test get_user_flags
     335       */
     336      public function test_get_user_flags() {
     337          global $DB, $USER;
     338  
     339          $this->resetAfterTest(true);
     340          // Create a course and assignment.
     341          $coursedata['idnumber'] = 'idnumbercourse';
     342          $coursedata['fullname'] = 'Lightwork Course';
     343          $coursedata['summary'] = 'Lightwork Course description';
     344          $coursedata['summaryformat'] = FORMAT_MOODLE;
     345          $course = self::getDataGenerator()->create_course($coursedata);
     346  
     347          $assigndata['course'] = $course->id;
     348          $assigndata['name'] = 'lightwork assignment';
     349  
     350          $assign = self::getDataGenerator()->create_module('assign', $assigndata);
     351  
     352          // Create a manual enrolment record.
     353          $manualenroldata['enrol'] = 'manual';
     354          $manualenroldata['status'] = 0;
     355          $manualenroldata['courseid'] = $course->id;
     356          $enrolid = $DB->insert_record('enrol', $manualenroldata);
     357  
     358          // Create a teacher and give them capabilities.
     359          $context = context_course::instance($course->id);
     360          $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
     361          $context = context_module::instance($assign->cmid);
     362          $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
     363  
     364          // Create the teacher's enrolment record.
     365          $userenrolmentdata['status'] = 0;
     366          $userenrolmentdata['enrolid'] = $enrolid;
     367          $userenrolmentdata['userid'] = $USER->id;
     368          $DB->insert_record('user_enrolments', $userenrolmentdata);
     369  
     370          // Create a student and give them a user flag record.
     371          $student = self::getDataGenerator()->create_user();
     372          $userflag = new stdClass();
     373          $userflag->assignment = $assign->id;
     374          $userflag->userid = $student->id;
     375          $userflag->locked = 0;
     376          $userflag->mailed = 0;
     377          $userflag->extensionduedate = 0;
     378          $userflag->workflowstate = 'inmarking';
     379          $userflag->allocatedmarker = $USER->id;
     380  
     381          $DB->insert_record('assign_user_flags', $userflag);
     382  
     383          $assignmentids[] = $assign->id;
     384          $result = mod_assign_external::get_user_flags($assignmentids);
     385  
     386          // We need to execute the return values cleaning process to simulate the web service server.
     387          $result = external_api::clean_returnvalue(mod_assign_external::get_user_flags_returns(), $result);
     388  
     389          // Check that the correct user flag information for the student is returned.
     390          $this->assertEquals(1, count($result['assignments']));
     391          $assignment = $result['assignments'][0];
     392          $this->assertEquals($assign->id, $assignment['assignmentid']);
     393          // Should be one user flag record.
     394          $this->assertEquals(1, count($assignment['userflags']));
     395          $userflag = $assignment['userflags'][0];
     396          $this->assertEquals($student->id, $userflag['userid']);
     397          $this->assertEquals(0, $userflag['locked']);
     398          $this->assertEquals(0, $userflag['mailed']);
     399          $this->assertEquals(0, $userflag['extensionduedate']);
     400          $this->assertEquals('inmarking', $userflag['workflowstate']);
     401          $this->assertEquals($USER->id, $userflag['allocatedmarker']);
     402      }
     403  
     404      /**
     405       * Test get_user_mappings
     406       */
     407      public function test_get_user_mappings() {
     408          global $DB, $USER;
     409  
     410          $this->resetAfterTest(true);
     411          // Create a course and assignment.
     412          $coursedata['idnumber'] = 'idnumbercourse';
     413          $coursedata['fullname'] = 'Lightwork Course';
     414          $coursedata['summary'] = 'Lightwork Course description';
     415          $coursedata['summaryformat'] = FORMAT_MOODLE;
     416          $course = self::getDataGenerator()->create_course($coursedata);
     417  
     418          $assigndata['course'] = $course->id;
     419          $assigndata['name'] = 'lightwork assignment';
     420  
     421          $assign = self::getDataGenerator()->create_module('assign', $assigndata);
     422  
     423          // Create a manual enrolment record.
     424          $manualenroldata['enrol'] = 'manual';
     425          $manualenroldata['status'] = 0;
     426          $manualenroldata['courseid'] = $course->id;
     427          $enrolid = $DB->insert_record('enrol', $manualenroldata);
     428  
     429          // Create a teacher and give them capabilities.
     430          $context = context_course::instance($course->id);
     431          $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
     432          $context = context_module::instance($assign->cmid);
     433          $this->assignUserCapability('mod/assign:revealidentities', $context->id, $roleid);
     434  
     435          // Create the teacher's enrolment record.
     436          $userenrolmentdata['status'] = 0;
     437          $userenrolmentdata['enrolid'] = $enrolid;
     438          $userenrolmentdata['userid'] = $USER->id;
     439          $DB->insert_record('user_enrolments', $userenrolmentdata);
     440  
     441          // Create a student and give them a user mapping record.
     442          $student = self::getDataGenerator()->create_user();
     443          $mapping = new stdClass();
     444          $mapping->assignment = $assign->id;
     445          $mapping->userid = $student->id;
     446  
     447          $DB->insert_record('assign_user_mapping', $mapping);
     448  
     449          $assignmentids[] = $assign->id;
     450          $result = mod_assign_external::get_user_mappings($assignmentids);
     451  
     452          // We need to execute the return values cleaning process to simulate the web service server.
     453          $result = external_api::clean_returnvalue(mod_assign_external::get_user_mappings_returns(), $result);
     454  
     455          // Check that the correct user mapping information for the student is returned.
     456          $this->assertEquals(1, count($result['assignments']));
     457          $assignment = $result['assignments'][0];
     458          $this->assertEquals($assign->id, $assignment['assignmentid']);
     459          // Should be one user mapping record.
     460          $this->assertEquals(1, count($assignment['mappings']));
     461          $mapping = $assignment['mappings'][0];
     462          $this->assertEquals($student->id, $mapping['userid']);
     463      }
     464  
     465      /**
     466       * Test lock_submissions
     467       */
     468      public function test_lock_submissions() {
     469          global $DB, $USER;
     470  
     471          $this->resetAfterTest(true);
     472          // Create a course and assignment and users.
     473          $course = self::getDataGenerator()->create_course();
     474  
     475          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
     476          $params['course'] = $course->id;
     477          $params['assignsubmission_onlinetext_enabled'] = 1;
     478          $instance = $generator->create_instance($params);
     479          $cm = get_coursemodule_from_instance('assign', $instance->id);
     480          $context = context_module::instance($cm->id);
     481  
     482          $assign = new assign($context, $cm, $course);
     483  
     484          $student1 = self::getDataGenerator()->create_user();
     485          $student2 = self::getDataGenerator()->create_user();
     486          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
     487          $this->getDataGenerator()->enrol_user($student1->id,
     488                                                $course->id,
     489                                                $studentrole->id);
     490          $this->getDataGenerator()->enrol_user($student2->id,
     491                                                $course->id,
     492                                                $studentrole->id);
     493          $teacher = self::getDataGenerator()->create_user();
     494          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
     495          $this->getDataGenerator()->enrol_user($teacher->id,
     496                                                $course->id,
     497                                                $teacherrole->id);
     498  
     499          // Create a student1 with an online text submission.
     500          // Simulate a submission.
     501          $this->setUser($student1);
     502          $submission = $assign->get_user_submission($student1->id, true);
     503          $data = new stdClass();
     504          $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
     505                                           'text'=>'Submission text',
     506                                           'format'=>FORMAT_MOODLE);
     507          $plugin = $assign->get_submission_plugin_by_type('onlinetext');
     508          $plugin->save($submission, $data);
     509  
     510          // Ready to test.
     511          $this->setUser($teacher);
     512          $students = array($student1->id, $student2->id);
     513          $result = mod_assign_external::lock_submissions($instance->id, $students);
     514          $result = external_api::clean_returnvalue(mod_assign_external::lock_submissions_returns(), $result);
     515  
     516          // Check for 0 warnings.
     517          $this->assertEquals(0, count($result));
     518  
     519          $this->setUser($student2);
     520          $submission = $assign->get_user_submission($student2->id, true);
     521          $data = new stdClass();
     522          $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
     523                                           'text'=>'Submission text',
     524                                           'format'=>FORMAT_MOODLE);
     525          $notices = array();
     526          $this->setExpectedException('moodle_exception');
     527          $assign->save_submission($data, $notices);
     528      }
     529  
     530      /**
     531       * Test unlock_submissions
     532       */
     533      public function test_unlock_submissions() {
     534          global $DB, $USER;
     535  
     536          $this->resetAfterTest(true);
     537          // Create a course and assignment and users.
     538          $course = self::getDataGenerator()->create_course();
     539  
     540          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
     541          $params['course'] = $course->id;
     542          $params['assignsubmission_onlinetext_enabled'] = 1;
     543          $instance = $generator->create_instance($params);
     544          $cm = get_coursemodule_from_instance('assign', $instance->id);
     545          $context = context_module::instance($cm->id);
     546  
     547          $assign = new assign($context, $cm, $course);
     548  
     549          $student1 = self::getDataGenerator()->create_user();
     550          $student2 = self::getDataGenerator()->create_user();
     551          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
     552          $this->getDataGenerator()->enrol_user($student1->id,
     553                                                $course->id,
     554                                                $studentrole->id);
     555          $this->getDataGenerator()->enrol_user($student2->id,
     556                                                $course->id,
     557                                                $studentrole->id);
     558          $teacher = self::getDataGenerator()->create_user();
     559          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
     560          $this->getDataGenerator()->enrol_user($teacher->id,
     561                                                $course->id,
     562                                                $teacherrole->id);
     563  
     564          // Create a student1 with an online text submission.
     565          // Simulate a submission.
     566          $this->setUser($student1);
     567          $submission = $assign->get_user_submission($student1->id, true);
     568          $data = new stdClass();
     569          $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
     570                                           'text'=>'Submission text',
     571                                           'format'=>FORMAT_MOODLE);
     572          $plugin = $assign->get_submission_plugin_by_type('onlinetext');
     573          $plugin->save($submission, $data);
     574  
     575          // Ready to test.
     576          $this->setUser($teacher);
     577          $students = array($student1->id, $student2->id);
     578          $result = mod_assign_external::lock_submissions($instance->id, $students);
     579          $result = external_api::clean_returnvalue(mod_assign_external::lock_submissions_returns(), $result);
     580  
     581          // Check for 0 warnings.
     582          $this->assertEquals(0, count($result));
     583  
     584          $result = mod_assign_external::unlock_submissions($instance->id, $students);
     585          $result = external_api::clean_returnvalue(mod_assign_external::unlock_submissions_returns(), $result);
     586  
     587          // Check for 0 warnings.
     588          $this->assertEquals(0, count($result));
     589  
     590          $this->setUser($student2);
     591          $submission = $assign->get_user_submission($student2->id, true);
     592          $data = new stdClass();
     593          $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
     594                                           'text'=>'Submission text',
     595                                           'format'=>FORMAT_MOODLE);
     596          $notices = array();
     597          $assign->save_submission($data, $notices);
     598      }
     599  
     600      /**
     601       * Test submit_for_grading
     602       */
     603      public function test_submit_for_grading() {
     604          global $DB, $USER;
     605  
     606          $this->resetAfterTest(true);
     607          // Create a course and assignment and users.
     608          $course = self::getDataGenerator()->create_course();
     609  
     610          set_config('submissionreceipts', 0, 'assign');
     611          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
     612          $params['course'] = $course->id;
     613          $params['assignsubmission_onlinetext_enabled'] = 1;
     614          $params['submissiondrafts'] = 1;
     615          $params['sendnotifications'] = 0;
     616          $params['requiresubmissionstatement'] = 1;
     617          $instance = $generator->create_instance($params);
     618          $cm = get_coursemodule_from_instance('assign', $instance->id);
     619          $context = context_module::instance($cm->id);
     620  
     621          $assign = new assign($context, $cm, $course);
     622  
     623          $student1 = self::getDataGenerator()->create_user();
     624          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
     625          $this->getDataGenerator()->enrol_user($student1->id,
     626                                                $course->id,
     627                                                $studentrole->id);
     628  
     629          // Create a student1 with an online text submission.
     630          // Simulate a submission.
     631          $this->setUser($student1);
     632          $submission = $assign->get_user_submission($student1->id, true);
     633          $data = new stdClass();
     634          $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
     635                                           'text'=>'Submission text',
     636                                           'format'=>FORMAT_MOODLE);
     637          $plugin = $assign->get_submission_plugin_by_type('onlinetext');
     638          $plugin->save($submission, $data);
     639  
     640          $result = mod_assign_external::submit_for_grading($instance->id, false);
     641          $result = external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result);
     642  
     643          // Should be 1 fail because the submission statement was not aceptted.
     644          $this->assertEquals(1, count($result));
     645  
     646          $result = mod_assign_external::submit_for_grading($instance->id, true);
     647          $result = external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result);
     648  
     649          // Check for 0 warnings.
     650          $this->assertEquals(0, count($result));
     651  
     652          $submission = $assign->get_user_submission($student1->id, false);
     653  
     654          $this->assertEquals(ASSIGN_SUBMISSION_STATUS_SUBMITTED, $submission->status);
     655      }
     656  
     657      /**
     658       * Test save_user_extensions
     659       */
     660      public function test_save_user_extensions() {
     661          global $DB, $USER;
     662  
     663          $this->resetAfterTest(true);
     664          // Create a course and assignment and users.
     665          $course = self::getDataGenerator()->create_course();
     666  
     667          $teacher = self::getDataGenerator()->create_user();
     668          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
     669          $this->getDataGenerator()->enrol_user($teacher->id,
     670                                                $course->id,
     671                                                $teacherrole->id);
     672          $this->setUser($teacher);
     673  
     674          $now = time();
     675          $yesterday = $now - 24*60*60;
     676          $tomorrow = $now + 24*60*60;
     677          set_config('submissionreceipts', 0, 'assign');
     678          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
     679          $params['course'] = $course->id;
     680          $params['submissiondrafts'] = 1;
     681          $params['sendnotifications'] = 0;
     682          $params['duedate'] = $yesterday;
     683          $params['cutoffdate'] = $now - 10;
     684          $instance = $generator->create_instance($params);
     685          $cm = get_coursemodule_from_instance('assign', $instance->id);
     686          $context = context_module::instance($cm->id);
     687  
     688          $assign = new assign($context, $cm, $course);
     689  
     690          $student1 = self::getDataGenerator()->create_user();
     691          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
     692          $this->getDataGenerator()->enrol_user($student1->id,
     693                                                $course->id,
     694                                                $studentrole->id);
     695  
     696          $this->setUser($student1);
     697          $result = mod_assign_external::submit_for_grading($instance->id, true);
     698          $result = external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result);
     699  
     700          // Check for 0 warnings.
     701          $this->assertEquals(1, count($result));
     702  
     703          $this->setUser($teacher);
     704          $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($now, $tomorrow));
     705          $result = external_api::clean_returnvalue(mod_assign_external::save_user_extensions_returns(), $result);
     706          $this->assertEquals(1, count($result));
     707  
     708          $this->setUser($teacher);
     709          $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($yesterday - 10));
     710          $result = external_api::clean_returnvalue(mod_assign_external::save_user_extensions_returns(), $result);
     711          $this->assertEquals(1, count($result));
     712  
     713          $this->setUser($teacher);
     714          $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($tomorrow));
     715          $result = external_api::clean_returnvalue(mod_assign_external::save_user_extensions_returns(), $result);
     716          $this->assertEquals(0, count($result));
     717  
     718          $this->setUser($student1);
     719          $result = mod_assign_external::submit_for_grading($instance->id, true);
     720          $result = external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result);
     721          $this->assertEquals(0, count($result));
     722  
     723          $this->setUser($student1);
     724          $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($now, $tomorrow));
     725          $result = external_api::clean_returnvalue(mod_assign_external::save_user_extensions_returns(), $result);
     726  
     727      }
     728  
     729      /**
     730       * Test reveal_identities
     731       */
     732      public function test_reveal_identities() {
     733          global $DB, $USER;
     734  
     735          $this->resetAfterTest(true);
     736          // Create a course and assignment and users.
     737          $course = self::getDataGenerator()->create_course();
     738  
     739          $teacher = self::getDataGenerator()->create_user();
     740          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
     741          $this->getDataGenerator()->enrol_user($teacher->id,
     742                                                $course->id,
     743                                                $teacherrole->id);
     744          $this->setUser($teacher);
     745  
     746          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
     747          $params['course'] = $course->id;
     748          $params['submissiondrafts'] = 1;
     749          $params['sendnotifications'] = 0;
     750          $params['blindmarking'] = 1;
     751          $instance = $generator->create_instance($params);
     752          $cm = get_coursemodule_from_instance('assign', $instance->id);
     753          $context = context_module::instance($cm->id);
     754  
     755          $assign = new assign($context, $cm, $course);
     756  
     757          $student1 = self::getDataGenerator()->create_user();
     758          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
     759          $this->getDataGenerator()->enrol_user($student1->id,
     760                                                $course->id,
     761                                                $studentrole->id);
     762  
     763          $this->setUser($student1);
     764          $this->setExpectedException('required_capability_exception');
     765          $result = mod_assign_external::reveal_identities($instance->id);
     766          $result = external_api::clean_returnvalue(mod_assign_external::reveal_identities_returns(), $result);
     767          $this->assertEquals(1, count($result));
     768          $this->assertEquals(true, $assign->is_blind_marking());
     769  
     770          $this->setUser($teacher);
     771          $result = mod_assign_external::reveal_identities($instance->id);
     772          $result = external_api::clean_returnvalue(mod_assign_external::reveal_identities_returns(), $result);
     773          $this->assertEquals(0, count($result));
     774          $this->assertEquals(false, $assign->is_blind_marking());
     775  
     776          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
     777          $params['course'] = $course->id;
     778          $params['submissiondrafts'] = 1;
     779          $params['sendnotifications'] = 0;
     780          $params['blindmarking'] = 0;
     781          $instance = $generator->create_instance($params);
     782          $cm = get_coursemodule_from_instance('assign', $instance->id);
     783          $context = context_module::instance($cm->id);
     784  
     785          $assign = new assign($context, $cm, $course);
     786          $result = mod_assign_external::reveal_identities($instance->id);
     787          $result = external_api::clean_returnvalue(mod_assign_external::reveal_identities_returns(), $result);
     788          $this->assertEquals(1, count($result));
     789          $this->assertEquals(false, $assign->is_blind_marking());
     790  
     791      }
     792  
     793      /**
     794       * Test revert_submissions_to_draft
     795       */
     796      public function test_revert_submissions_to_draft() {
     797          global $DB, $USER;
     798  
     799          $this->resetAfterTest(true);
     800          set_config('submissionreceipts', 0, 'assign');
     801          // Create a course and assignment and users.
     802          $course = self::getDataGenerator()->create_course();
     803  
     804          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
     805          $params['course'] = $course->id;
     806          $params['sendnotifications'] = 0;
     807          $params['submissiondrafts'] = 1;
     808          $instance = $generator->create_instance($params);
     809          $cm = get_coursemodule_from_instance('assign', $instance->id);
     810          $context = context_module::instance($cm->id);
     811  
     812          $assign = new assign($context, $cm, $course);
     813  
     814          $student1 = self::getDataGenerator()->create_user();
     815          $student2 = self::getDataGenerator()->create_user();
     816          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
     817          $this->getDataGenerator()->enrol_user($student1->id,
     818                                                $course->id,
     819                                                $studentrole->id);
     820          $this->getDataGenerator()->enrol_user($student2->id,
     821                                                $course->id,
     822                                                $studentrole->id);
     823          $teacher = self::getDataGenerator()->create_user();
     824          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
     825          $this->getDataGenerator()->enrol_user($teacher->id,
     826                                                $course->id,
     827                                                $teacherrole->id);
     828  
     829          // Create a student1 with an online text submission.
     830          // Simulate a submission.
     831          $this->setUser($student1);
     832          $result = mod_assign_external::submit_for_grading($instance->id, true);
     833          $result = external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result);
     834          $this->assertEquals(0, count($result));
     835  
     836          // Ready to test.
     837          $this->setUser($teacher);
     838          $students = array($student1->id, $student2->id);
     839          $result = mod_assign_external::revert_submissions_to_draft($instance->id, array($student1->id));
     840          $result = external_api::clean_returnvalue(mod_assign_external::submit_for_grading_returns(), $result);
     841  
     842          // Check for 0 warnings.
     843          $this->assertEquals(0, count($result));
     844  
     845      }
     846  
     847      /**
     848       * Test save_submission
     849       */
     850      public function test_save_submission() {
     851          global $DB, $USER;
     852  
     853          $this->resetAfterTest(true);
     854          // Create a course and assignment and users.
     855          $course = self::getDataGenerator()->create_course();
     856  
     857          $teacher = self::getDataGenerator()->create_user();
     858          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
     859          $this->getDataGenerator()->enrol_user($teacher->id,
     860                                                $course->id,
     861                                                $teacherrole->id);
     862          $this->setUser($teacher);
     863  
     864          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
     865          $params['course'] = $course->id;
     866          $params['assignsubmission_onlinetext_enabled'] = 1;
     867          $params['assignsubmission_file_enabled'] = 1;
     868          $params['assignsubmission_file_maxfiles'] = 5;
     869          $params['assignsubmission_file_maxsizebytes'] = 1024*1024;
     870          $instance = $generator->create_instance($params);
     871          $cm = get_coursemodule_from_instance('assign', $instance->id);
     872          $context = context_module::instance($cm->id);
     873  
     874          $assign = new assign($context, $cm, $course);
     875  
     876          $student1 = self::getDataGenerator()->create_user();
     877          $student2 = self::getDataGenerator()->create_user();
     878          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
     879          $this->getDataGenerator()->enrol_user($student1->id,
     880                                                $course->id,
     881                                                $studentrole->id);
     882          $this->getDataGenerator()->enrol_user($student2->id,
     883                                                $course->id,
     884                                                $studentrole->id);
     885          // Create a student1 with an online text submission.
     886          // Simulate a submission.
     887          $this->setUser($student1);
     888  
     889          // Create a file in a draft area.
     890          $draftidfile = file_get_unused_draft_itemid();
     891  
     892          $usercontext = context_user::instance($student1->id);
     893          $filerecord = array(
     894              'contextid' => $usercontext->id,
     895              'component' => 'user',
     896              'filearea'  => 'draft',
     897              'itemid'    => $draftidfile,
     898              'filepath'  => '/',
     899              'filename'  => 'testtext.txt',
     900          );
     901  
     902          $fs = get_file_storage();
     903          $fs->create_file_from_string($filerecord, 'text contents');
     904  
     905          // Create another file in a different draft area.
     906          $draftidonlinetext = file_get_unused_draft_itemid();
     907  
     908          $filerecord = array(
     909              'contextid' => $usercontext->id,
     910              'component' => 'user',
     911              'filearea'  => 'draft',
     912              'itemid'    => $draftidonlinetext,
     913              'filepath'  => '/',
     914              'filename'  => 'shouldbeanimage.txt',
     915          );
     916  
     917          $fs->create_file_from_string($filerecord, 'image contents (not really)');
     918  
     919          // Now try a submission.
     920          $submissionpluginparams = array();
     921          $submissionpluginparams['files_filemanager'] = $draftidfile;
     922          $onlinetexteditorparams = array('text'=>'Yeeha!',
     923                                          'format'=>1,
     924                                          'itemid'=>$draftidonlinetext);
     925          $submissionpluginparams['onlinetext_editor'] = $onlinetexteditorparams;
     926          $result = mod_assign_external::save_submission($instance->id, $submissionpluginparams);
     927          $result = external_api::clean_returnvalue(mod_assign_external::save_submission_returns(), $result);
     928  
     929          $this->assertEquals(0, count($result));
     930  
     931          // Set up a due and cutoff passed date.
     932          $instance->duedate = time() - WEEKSECS;
     933          $instance->cutoffdate = time() - WEEKSECS;
     934          $DB->update_record('assign', $instance);
     935  
     936          $result = mod_assign_external::save_submission($instance->id, $submissionpluginparams);
     937          $result = external_api::clean_returnvalue(mod_assign_external::save_submission_returns(), $result);
     938  
     939          $this->assertCount(1, $result);
     940          $this->assertEquals(get_string('duedatereached', 'assign'), $result[0]['item']);
     941      }
     942  
     943      /**
     944       * Test save_grade
     945       */
     946      public function test_save_grade() {
     947          global $DB, $USER;
     948  
     949          $this->resetAfterTest(true);
     950          // Create a course and assignment and users.
     951          $course = self::getDataGenerator()->create_course();
     952  
     953          $teacher = self::getDataGenerator()->create_user();
     954          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
     955          $this->getDataGenerator()->enrol_user($teacher->id,
     956                                                $course->id,
     957                                                $teacherrole->id);
     958          $this->setUser($teacher);
     959  
     960          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
     961          $params['course'] = $course->id;
     962          $params['assignfeedback_file_enabled'] = 1;
     963          $params['assignfeedback_comments_enabled'] = 1;
     964          $instance = $generator->create_instance($params);
     965          $cm = get_coursemodule_from_instance('assign', $instance->id);
     966          $context = context_module::instance($cm->id);
     967  
     968          $assign = new assign($context, $cm, $course);
     969  
     970          $student1 = self::getDataGenerator()->create_user();
     971          $student2 = self::getDataGenerator()->create_user();
     972          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
     973          $this->getDataGenerator()->enrol_user($student1->id,
     974                                                $course->id,
     975                                                $studentrole->id);
     976          $this->getDataGenerator()->enrol_user($student2->id,
     977                                                $course->id,
     978                                                $studentrole->id);
     979          // Simulate a grade.
     980          $this->setUser($teacher);
     981  
     982          // Create a file in a draft area.
     983          $draftidfile = file_get_unused_draft_itemid();
     984  
     985          $usercontext = context_user::instance($teacher->id);
     986          $filerecord = array(
     987              'contextid' => $usercontext->id,
     988              'component' => 'user',
     989              'filearea'  => 'draft',
     990              'itemid'    => $draftidfile,
     991              'filepath'  => '/',
     992              'filename'  => 'testtext.txt',
     993          );
     994  
     995          $fs = get_file_storage();
     996          $fs->create_file_from_string($filerecord, 'text contents');
     997  
     998          // Now try a grade.
     999          $feedbackpluginparams = array();
    1000          $feedbackpluginparams['files_filemanager'] = $draftidfile;
    1001          $feedbackeditorparams = array('text' => 'Yeeha!',
    1002                                          'format' => 1);
    1003          $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams;
    1004          $result = mod_assign_external::save_grade($instance->id,
    1005                                                    $student1->id,
    1006                                                    50.0,
    1007                                                    -1,
    1008                                                    true,
    1009                                                    'released',
    1010                                                    false,
    1011                                                    $feedbackpluginparams);
    1012          // No warnings.
    1013          $this->assertNull($result);
    1014  
    1015          $result = mod_assign_external::get_grades(array($instance->id));
    1016          $result = external_api::clean_returnvalue(mod_assign_external::get_grades_returns(), $result);
    1017  
    1018          $this->assertEquals($result['assignments'][0]['grades'][0]['grade'], '50.0');
    1019      }
    1020  
    1021      /**
    1022       * Test save grades with advanced grading data
    1023       */
    1024      public function test_save_grades_with_advanced_grading() {
    1025          global $DB, $USER;
    1026  
    1027          $this->resetAfterTest(true);
    1028          // Create a course and assignment and users.
    1029          $course = self::getDataGenerator()->create_course();
    1030  
    1031          $teacher = self::getDataGenerator()->create_user();
    1032          $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
    1033          $this->getDataGenerator()->enrol_user($teacher->id,
    1034                                                $course->id,
    1035                                                $teacherrole->id);
    1036  
    1037          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
    1038          $params['course'] = $course->id;
    1039          $params['assignfeedback_file_enabled'] = 0;
    1040          $params['assignfeedback_comments_enabled'] = 0;
    1041          $instance = $generator->create_instance($params);
    1042          $cm = get_coursemodule_from_instance('assign', $instance->id);
    1043          $context = context_module::instance($cm->id);
    1044  
    1045          $assign = new assign($context, $cm, $course);
    1046  
    1047          $student1 = self::getDataGenerator()->create_user();
    1048          $student2 = self::getDataGenerator()->create_user();
    1049          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
    1050          $this->getDataGenerator()->enrol_user($student1->id,
    1051                                                $course->id,
    1052                                                $studentrole->id);
    1053          $this->getDataGenerator()->enrol_user($student2->id,
    1054                                                $course->id,
    1055                                                $studentrole->id);
    1056  
    1057          $this->setUser($teacher);
    1058  
    1059          $feedbackpluginparams = array();
    1060          $feedbackpluginparams['files_filemanager'] = 0;
    1061          $feedbackeditorparams = array('text' => '', 'format' => 1);
    1062          $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams;
    1063  
    1064          // Create advanced grading data.
    1065          // Create grading area.
    1066          $gradingarea = array(
    1067              'contextid' => $context->id,
    1068              'component' => 'mod_assign',
    1069              'areaname' => 'submissions',
    1070              'activemethod' => 'rubric'
    1071          );
    1072          $areaid = $DB->insert_record('grading_areas', $gradingarea);
    1073  
    1074          // Create a rubric grading definition.
    1075          $rubricdefinition = array (
    1076              'areaid' => $areaid,
    1077              'method' => 'rubric',
    1078              'name' => 'test',
    1079              'status' => 20,
    1080              'copiedfromid' => 1,
    1081              'timecreated' => 1,
    1082              'usercreated' => $teacher->id,
    1083              'timemodified' => 1,
    1084              'usermodified' => $teacher->id,
    1085              'timecopied' => 0
    1086          );
    1087          $definitionid = $DB->insert_record('grading_definitions', $rubricdefinition);
    1088  
    1089          // Create a criterion with a level.
    1090          $rubriccriteria = array (
    1091               'definitionid' => $definitionid,
    1092               'sortorder' => 1,
    1093               'description' => 'Demonstrate an understanding of disease control',
    1094               'descriptionformat' => 0
    1095          );
    1096          $criterionid = $DB->insert_record('gradingform_rubric_criteria', $rubriccriteria);
    1097          $rubriclevel1 = array (
    1098              'criterionid' => $criterionid,
    1099              'score' => 50,
    1100              'definition' => 'pass',
    1101              'definitionformat' => 0
    1102          );
    1103          $rubriclevel2 = array (
    1104              'criterionid' => $criterionid,
    1105              'score' => 100,
    1106              'definition' => 'excellent',
    1107              'definitionformat' => 0
    1108          );
    1109          $rubriclevel3 = array (
    1110              'criterionid' => $criterionid,
    1111              'score' => 0,
    1112              'definition' => 'fail',
    1113              'definitionformat' => 0
    1114          );
    1115          $levelid1 = $DB->insert_record('gradingform_rubric_levels', $rubriclevel1);
    1116          $levelid2 = $DB->insert_record('gradingform_rubric_levels', $rubriclevel2);
    1117          $levelid3 = $DB->insert_record('gradingform_rubric_levels', $rubriclevel3);
    1118  
    1119          // Create the filling.
    1120          $student1filling = array (
    1121              'criterionid' => $criterionid,
    1122              'levelid' => $levelid1,
    1123              'remark' => 'well done you passed',
    1124              'remarkformat' => 0
    1125          );
    1126  
    1127          $student2filling = array (
    1128              'criterionid' => $criterionid,
    1129              'levelid' => $levelid2,
    1130              'remark' => 'Excellent work',
    1131              'remarkformat' => 0
    1132          );
    1133  
    1134          $student1criteria = array(array('criterionid' => $criterionid, 'fillings' => array($student1filling)));
    1135          $student1advancedgradingdata = array('rubric' => array('criteria' => $student1criteria));
    1136  
    1137          $student2criteria = array(array('criterionid' => $criterionid, 'fillings' => array($student2filling)));
    1138          $student2advancedgradingdata = array('rubric' => array('criteria' => $student2criteria));
    1139  
    1140          $grades = array();
    1141          $student1gradeinfo = array();
    1142          $student1gradeinfo['userid'] = $student1->id;
    1143          $student1gradeinfo['grade'] = 0; // Ignored since advanced grading is being used.
    1144          $student1gradeinfo['attemptnumber'] = -1;
    1145          $student1gradeinfo['addattempt'] = true;
    1146          $student1gradeinfo['workflowstate'] = 'released';
    1147          $student1gradeinfo['plugindata'] = $feedbackpluginparams;
    1148          $student1gradeinfo['advancedgradingdata'] = $student1advancedgradingdata;
    1149          $grades[] = $student1gradeinfo;
    1150  
    1151          $student2gradeinfo = array();
    1152          $student2gradeinfo['userid'] = $student2->id;
    1153          $student2gradeinfo['grade'] = 0; // Ignored since advanced grading is being used.
    1154          $student2gradeinfo['attemptnumber'] = -1;
    1155          $student2gradeinfo['addattempt'] = true;
    1156          $student2gradeinfo['workflowstate'] = 'released';
    1157          $student2gradeinfo['plugindata'] = $feedbackpluginparams;
    1158          $student2gradeinfo['advancedgradingdata'] = $student2advancedgradingdata;
    1159          $grades[] = $student2gradeinfo;
    1160  
    1161          $result = mod_assign_external::save_grades($instance->id, false, $grades);
    1162          $this->assertNull($result);
    1163  
    1164          $student1grade = $DB->get_record('assign_grades',
    1165                                           array('userid' => $student1->id, 'assignment' => $instance->id),
    1166                                           '*',
    1167                                           MUST_EXIST);
    1168          $this->assertEquals($student1grade->grade, '50.0');
    1169  
    1170          $student2grade = $DB->get_record('assign_grades',
    1171                                           array('userid' => $student2->id, 'assignment' => $instance->id),
    1172                                           '*',
    1173                                           MUST_EXIST);
    1174          $this->assertEquals($student2grade->grade, '100.0');
    1175      }
    1176  
    1177      /**
    1178       * Test save grades for a team submission
    1179       */
    1180      public function test_save_grades_with_group_submission() {
    1181          global $DB, $USER, $CFG;
    1182          require_once($CFG->dirroot . '/group/lib.php');
    1183  
    1184          $this->resetAfterTest(true);
    1185          // Create a course and assignment and users.
    1186          $course = self::getDataGenerator()->create_course();
    1187  
    1188          $teacher = self::getDataGenerator()->create_user();
    1189          $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
    1190          $this->getDataGenerator()->enrol_user($teacher->id,
    1191                                                $course->id,
    1192                                                $teacherrole->id);
    1193  
    1194          $groupingdata = array();
    1195          $groupingdata['courseid'] = $course->id;
    1196          $groupingdata['name'] = 'Group assignment grouping';
    1197  
    1198          $grouping = self::getDataGenerator()->create_grouping($groupingdata);
    1199  
    1200          $group1data = array();
    1201          $group1data['courseid'] = $course->id;
    1202          $group1data['name'] = 'Team 1';
    1203          $group2data = array();
    1204          $group2data['courseid'] = $course->id;
    1205          $group2data['name'] = 'Team 2';
    1206  
    1207          $group1 = self::getDataGenerator()->create_group($group1data);
    1208          $group2 = self::getDataGenerator()->create_group($group2data);
    1209  
    1210          groups_assign_grouping($grouping->id, $group1->id);
    1211          groups_assign_grouping($grouping->id, $group2->id);
    1212  
    1213          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
    1214          $params['course'] = $course->id;
    1215          $params['teamsubmission'] = 1;
    1216          $params['teamsubmissiongroupingid'] = $grouping->id;
    1217          $instance = $generator->create_instance($params);
    1218          $cm = get_coursemodule_from_instance('assign', $instance->id);
    1219          $context = context_module::instance($cm->id);
    1220  
    1221          $assign = new assign($context, $cm, $course);
    1222  
    1223          $student1 = self::getDataGenerator()->create_user();
    1224          $student2 = self::getDataGenerator()->create_user();
    1225          $student3 = self::getDataGenerator()->create_user();
    1226          $student4 = self::getDataGenerator()->create_user();
    1227          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
    1228          $this->getDataGenerator()->enrol_user($student1->id,
    1229                                                $course->id,
    1230                                                $studentrole->id);
    1231          $this->getDataGenerator()->enrol_user($student2->id,
    1232                                                $course->id,
    1233                                                $studentrole->id);
    1234          $this->getDataGenerator()->enrol_user($student3->id,
    1235                                                $course->id,
    1236                                                $studentrole->id);
    1237          $this->getDataGenerator()->enrol_user($student4->id,
    1238                                                $course->id,
    1239                                                $studentrole->id);
    1240  
    1241          groups_add_member($group1->id, $student1->id);
    1242          groups_add_member($group1->id, $student2->id);
    1243          groups_add_member($group1->id, $student3->id);
    1244          groups_add_member($group2->id, $student4->id);
    1245          $this->setUser($teacher);
    1246  
    1247          $feedbackpluginparams = array();
    1248          $feedbackpluginparams['files_filemanager'] = 0;
    1249          $feedbackeditorparams = array('text' => '', 'format' => 1);
    1250          $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams;
    1251  
    1252          $grades1 = array();
    1253          $student1gradeinfo = array();
    1254          $student1gradeinfo['userid'] = $student1->id;
    1255          $student1gradeinfo['grade'] = 50;
    1256          $student1gradeinfo['attemptnumber'] = -1;
    1257          $student1gradeinfo['addattempt'] = true;
    1258          $student1gradeinfo['workflowstate'] = 'released';
    1259          $student1gradeinfo['plugindata'] = $feedbackpluginparams;
    1260          $grades1[] = $student1gradeinfo;
    1261  
    1262          $student2gradeinfo = array();
    1263          $student2gradeinfo['userid'] = $student2->id;
    1264          $student2gradeinfo['grade'] = 75;
    1265          $student2gradeinfo['attemptnumber'] = -1;
    1266          $student2gradeinfo['addattempt'] = true;
    1267          $student2gradeinfo['workflowstate'] = 'released';
    1268          $student2gradeinfo['plugindata'] = $feedbackpluginparams;
    1269          $grades1[] = $student2gradeinfo;
    1270  
    1271          $this->setExpectedException('invalid_parameter_exception');
    1272          // Expect an exception since 2 grades have been submitted for the same team.
    1273          $result = mod_assign_external::save_grades($instance->id, true, $grades1);
    1274          $result = external_api::clean_returnvalue(mod_assign_external::save_grades_returns(), $result);
    1275  
    1276          $grades2 = array();
    1277          $student3gradeinfo = array();
    1278          $student3gradeinfo['userid'] = $student3->id;
    1279          $student3gradeinfo['grade'] = 50;
    1280          $student3gradeinfo['attemptnumber'] = -1;
    1281          $student3gradeinfo['addattempt'] = true;
    1282          $student3gradeinfo['workflowstate'] = 'released';
    1283          $student3gradeinfo['plugindata'] = $feedbackpluginparams;
    1284          $grades2[] = $student3gradeinfo;
    1285  
    1286          $student4gradeinfo = array();
    1287          $student4gradeinfo['userid'] = $student4->id;
    1288          $student4gradeinfo['grade'] = 75;
    1289          $student4gradeinfo['attemptnumber'] = -1;
    1290          $student4gradeinfo['addattempt'] = true;
    1291          $student4gradeinfo['workflowstate'] = 'released';
    1292          $student4gradeinfo['plugindata'] = $feedbackpluginparams;
    1293          $grades2[] = $student4gradeinfo;
    1294          $result = mod_assign_external::save_grades($instance->id, true, $grades2);
    1295          $result = external_api::clean_returnvalue(mod_assign_external::save_grades_returns(), $result);
    1296          // There should be no warnings.
    1297          $this->assertEquals(0, count($result));
    1298  
    1299          $student3grade = $DB->get_record('assign_grades',
    1300                                           array('userid' => $student3->id, 'assignment' => $instance->id),
    1301                                           '*',
    1302                                           MUST_EXIST);
    1303          $this->assertEquals($student3grade->grade, '50.0');
    1304  
    1305          $student4grade = $DB->get_record('assign_grades',
    1306                                           array('userid' => $student4->id, 'assignment' => $instance->id),
    1307                                           '*',
    1308                                           MUST_EXIST);
    1309          $this->assertEquals($student4grade->grade, '75.0');
    1310      }
    1311  
    1312      /**
    1313       * Test copy_previous_attempt
    1314       */
    1315      public function test_copy_previous_attempt() {
    1316          global $DB, $USER;
    1317  
    1318          $this->resetAfterTest(true);
    1319          // Create a course and assignment and users.
    1320          $course = self::getDataGenerator()->create_course();
    1321  
    1322          $teacher = self::getDataGenerator()->create_user();
    1323          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
    1324          $this->getDataGenerator()->enrol_user($teacher->id,
    1325                                                $course->id,
    1326                                                $teacherrole->id);
    1327          $this->setUser($teacher);
    1328  
    1329          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
    1330          $params['course'] = $course->id;
    1331          $params['assignsubmission_onlinetext_enabled'] = 1;
    1332          $params['assignsubmission_file_enabled'] = 0;
    1333          $params['assignfeedback_file_enabled'] = 0;
    1334          $params['attemptreopenmethod'] = 'manual';
    1335          $params['maxattempts'] = 5;
    1336          $instance = $generator->create_instance($params);
    1337          $cm = get_coursemodule_from_instance('assign', $instance->id);
    1338          $context = context_module::instance($cm->id);
    1339  
    1340          $assign = new assign($context, $cm, $course);
    1341  
    1342          $student1 = self::getDataGenerator()->create_user();
    1343          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
    1344          $this->getDataGenerator()->enrol_user($student1->id,
    1345                                                $course->id,
    1346                                                $studentrole->id);
    1347          // Now try a submission.
    1348          $this->setUser($student1);
    1349          $draftidonlinetext = file_get_unused_draft_itemid();
    1350          $submissionpluginparams = array();
    1351          $onlinetexteditorparams = array('text'=>'Yeeha!',
    1352                                          'format'=>1,
    1353                                          'itemid'=>$draftidonlinetext);
    1354          $submissionpluginparams['onlinetext_editor'] = $onlinetexteditorparams;
    1355          $submissionpluginparams['files_filemanager'] = file_get_unused_draft_itemid();
    1356          $result = mod_assign_external::save_submission($instance->id, $submissionpluginparams);
    1357          $result = external_api::clean_returnvalue(mod_assign_external::save_submission_returns(), $result);
    1358  
    1359          $this->setUser($teacher);
    1360          // Add a grade and reopen the attempt.
    1361          // Now try a grade.
    1362          $feedbackpluginparams = array();
    1363          $feedbackpluginparams['files_filemanager'] = file_get_unused_draft_itemid();
    1364          $feedbackeditorparams = array('text'=>'Yeeha!',
    1365                                          'format'=>1);
    1366          $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams;
    1367          $result = mod_assign_external::save_grade($instance->id,
    1368                                                    $student1->id,
    1369                                                    50.0,
    1370                                                    -1,
    1371                                                    true,
    1372                                                    'released',
    1373                                                    false,
    1374                                                    $feedbackpluginparams);
    1375          $this->assertNull($result);
    1376  
    1377          $this->setUser($student1);
    1378          // Now copy the previous attempt.
    1379          $result = mod_assign_external::copy_previous_attempt($instance->id);
    1380          $result = external_api::clean_returnvalue(mod_assign_external::copy_previous_attempt_returns(), $result);
    1381          // No warnings.
    1382          $this->assertEquals(0, count($result));
    1383  
    1384          $this->setUser($teacher);
    1385          $result = mod_assign_external::get_submissions(array($instance->id));
    1386          $result = external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result);
    1387  
    1388          // Check we are now on the second attempt.
    1389          $this->assertEquals($result['assignments'][0]['submissions'][0]['attemptnumber'], 1);
    1390          // Check the plugins data is not empty.
    1391          $this->assertNotEmpty($result['assignments'][0]['submissions'][0]['plugins']);
    1392  
    1393      }
    1394  
    1395      /**
    1396       * Test set_user_flags
    1397       */
    1398      public function test_set_user_flags() {
    1399          global $DB, $USER;
    1400  
    1401          $this->resetAfterTest(true);
    1402          // Create a course and assignment.
    1403          $coursedata['idnumber'] = 'idnumbercourse';
    1404          $coursedata['fullname'] = 'Lightwork Course';
    1405          $coursedata['summary'] = 'Lightwork Course description';
    1406          $coursedata['summaryformat'] = FORMAT_MOODLE;
    1407          $course = self::getDataGenerator()->create_course($coursedata);
    1408  
    1409          $assigndata['course'] = $course->id;
    1410          $assigndata['name'] = 'lightwork assignment';
    1411  
    1412          $assign = self::getDataGenerator()->create_module('assign', $assigndata);
    1413  
    1414          // Create a manual enrolment record.
    1415          $manualenroldata['enrol'] = 'manual';
    1416          $manualenroldata['status'] = 0;
    1417          $manualenroldata['courseid'] = $course->id;
    1418          $enrolid = $DB->insert_record('enrol', $manualenroldata);
    1419  
    1420          // Create a teacher and give them capabilities.
    1421          $context = context_course::instance($course->id);
    1422          $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
    1423          $context = context_module::instance($assign->cmid);
    1424          $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
    1425  
    1426          // Create the teacher's enrolment record.
    1427          $userenrolmentdata['status'] = 0;
    1428          $userenrolmentdata['enrolid'] = $enrolid;
    1429          $userenrolmentdata['userid'] = $USER->id;
    1430          $DB->insert_record('user_enrolments', $userenrolmentdata);
    1431  
    1432          // Create a student.
    1433          $student = self::getDataGenerator()->create_user();
    1434  
    1435          // Create test user flags record.
    1436          $userflags = array();
    1437          $userflag['userid'] = $student->id;
    1438          $userflag['workflowstate'] = 'inmarking';
    1439          $userflag['allocatedmarker'] = $USER->id;
    1440          $userflags = array($userflag);
    1441  
    1442          $createduserflags = mod_assign_external::set_user_flags($assign->id, $userflags);
    1443          // We need to execute the return values cleaning process to simulate the web service server.
    1444          $createduserflags = external_api::clean_returnvalue(mod_assign_external::set_user_flags_returns(), $createduserflags);
    1445  
    1446          $this->assertEquals($student->id, $createduserflags[0]['userid']);
    1447          $createduserflag = $DB->get_record('assign_user_flags', array('id' => $createduserflags[0]['id']));
    1448  
    1449          // Confirm that all data was inserted correctly.
    1450          $this->assertEquals($student->id,  $createduserflag->userid);
    1451          $this->assertEquals($assign->id, $createduserflag->assignment);
    1452          $this->assertEquals(0, $createduserflag->locked);
    1453          $this->assertEquals(2, $createduserflag->mailed);
    1454          $this->assertEquals(0, $createduserflag->extensionduedate);
    1455          $this->assertEquals('inmarking', $createduserflag->workflowstate);
    1456          $this->assertEquals($USER->id, $createduserflag->allocatedmarker);
    1457  
    1458          // Create update data.
    1459          $userflags = array();
    1460          $userflag['userid'] = $createduserflag->userid;
    1461          $userflag['workflowstate'] = 'readyforreview';
    1462          $userflags = array($userflag);
    1463  
    1464          $updateduserflags = mod_assign_external::set_user_flags($assign->id, $userflags);
    1465          // We need to execute the return values cleaning process to simulate the web service server.
    1466          $updateduserflags = external_api::clean_returnvalue(mod_assign_external::set_user_flags_returns(), $updateduserflags);
    1467  
    1468          $this->assertEquals($student->id, $updateduserflags[0]['userid']);
    1469          $updateduserflag = $DB->get_record('assign_user_flags', array('id' => $updateduserflags[0]['id']));
    1470  
    1471          // Confirm that all data was updated correctly.
    1472          $this->assertEquals($student->id,  $updateduserflag->userid);
    1473          $this->assertEquals($assign->id, $updateduserflag->assignment);
    1474          $this->assertEquals(0, $updateduserflag->locked);
    1475          $this->assertEquals(2, $updateduserflag->mailed);
    1476          $this->assertEquals(0, $updateduserflag->extensionduedate);
    1477          $this->assertEquals('readyforreview', $updateduserflag->workflowstate);
    1478          $this->assertEquals($USER->id, $updateduserflag->allocatedmarker);
    1479      }
    1480  
    1481  }
    

    Search This Site: