/** * Test copy_previous_attempt */ public function test_copy_previous_attempt() { global $DB, $USER; $this->resetAfterTest(true); // Create a course and assignment and users. $course = self::getDataGenerator()->create_course(); $teacher = self::getDataGenerator()->create_user(); $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); $this->setUser($teacher); $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); $params['course'] = $course->id; $params['assignsubmission_onlinetext_enabled'] = 1; $params['assignsubmission_file_enabled'] = 0; $params['assignfeedback_file_enabled'] = 0; $params['attemptreopenmethod'] = 'manual'; $params['maxattempts'] = 5; $instance = $generator->create_instance($params); $cm = get_coursemodule_from_instance('assign', $instance->id); $context = context_module::instance($cm->id); $assign = new assign($context, $cm, $course); $student1 = self::getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id); // Now try a submission. $this->setUser($student1); $draftidonlinetext = file_get_unused_draft_itemid(); $submissionpluginparams = array(); $onlinetexteditorparams = array('text' => 'Yeeha!', 'format' => 1, 'itemid' => $draftidonlinetext); $submissionpluginparams['onlinetext_editor'] = $onlinetexteditorparams; $submissionpluginparams['files_filemanager'] = file_get_unused_draft_itemid(); $result = mod_assign_external::save_submission($instance->id, $submissionpluginparams); $result = external_api::clean_returnvalue(mod_assign_external::save_submission_returns(), $result); $this->setUser($teacher); // Add a grade and reopen the attempt. // Now try a grade. $feedbackpluginparams = array(); $feedbackpluginparams['files_filemanager'] = file_get_unused_draft_itemid(); $feedbackeditorparams = array('text' => 'Yeeha!', 'format' => 1); $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams; $result = mod_assign_external::save_grade($instance->id, $student1->id, 50.0, -1, true, 'released', false, $feedbackpluginparams); $this->assertNull($result); $this->setUser($student1); // Now copy the previous attempt. $result = mod_assign_external::copy_previous_attempt($instance->id); $result = external_api::clean_returnvalue(mod_assign_external::copy_previous_attempt_returns(), $result); // No warnings. $this->assertEquals(0, count($result)); $this->setUser($teacher); $result = mod_assign_external::get_submissions(array($instance->id)); $result = external_api::clean_returnvalue(mod_assign_external::get_submissions_returns(), $result); // Check we are now on the second attempt. $this->assertEquals($result['assignments'][0]['submissions'][0]['attemptnumber'], 1); // Check the plugins data is not empty. $this->assertNotEmpty($result['assignments'][0]['submissions'][0]['plugins']); }