/** * 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']); }
/** * Test get_submissions */ public function test_get_submissions () { global $DB, $USER; $this->resetAfterTest(true); // Create a course and assignment. $coursedata['idnumber'] = 'idnumbercourse1'; $coursedata['fullname'] = 'Lightwork Course 1'; $coursedata['summary'] = 'Lightwork Course 1 description'; $coursedata['summaryformat'] = FORMAT_MOODLE; $course1 = self::getDataGenerator()->create_course($coursedata); $assigndata['course'] = $course1->id; $assigndata['name'] = 'lightwork assignment'; $assign1 = self::getDataGenerator()->create_module('assign', $assigndata); // Create a student with an online text submission. // First attempt. $student = self::getDataGenerator()->create_user(); $submission = new stdClass(); $submission->assignment = $assign1->id; $submission->userid = $student->id; $submission->timecreated = time(); $submission->timemodified = $submission->timecreated; $submission->status = 'draft'; $submission->attemptnumber = 0; $sid = $DB->insert_record('assign_submission', $submission); // Second attempt. $submission = new stdClass(); $submission->assignment = $assign1->id; $submission->userid = $student->id; $submission->timecreated = time(); $submission->timemodified = $submission->timecreated; $submission->status = 'submitted'; $submission->attemptnumber = 1; $sid = $DB->insert_record('assign_submission', $submission); $submission->id = $sid; $onlinetextsubmission = new stdClass(); $onlinetextsubmission->onlinetext = "<p>online test text</p>"; $onlinetextsubmission->onlineformat = 1; $onlinetextsubmission->submission = $submission->id; $onlinetextsubmission->assignment = $assign1->id; $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission); // Create manual enrolment record. $manual_enrol_data['enrol'] = 'manual'; $manual_enrol_data['status'] = 0; $manual_enrol_data['courseid'] = $course1->id; $enrolid = $DB->insert_record('enrol', $manual_enrol_data); // Create a teacher and give them capabilities. $context = context_course::instance($course1->id); $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3); $context = context_module::instance($assign1->id); $this->assignUserCapability('mod/assign:grade', $context->id, $roleid); // Create the teacher's enrolment record. $user_enrolment_data['status'] = 0; $user_enrolment_data['enrolid'] = $enrolid; $user_enrolment_data['userid'] = $USER->id; $DB->insert_record('user_enrolments', $user_enrolment_data); $assignmentids[] = $assign1->id; $result = mod_assign_external::get_submissions($assignmentids); // Check the online text submission is returned. $this->assertEquals(1, count($result['assignments'])); $assignment = $result['assignments'][0]; $this->assertEquals($assign1->id, $assignment['assignmentid']); $this->assertEquals(1, count($assignment['submissions'])); $submission = $assignment['submissions'][0]; $this->assertEquals($sid, $submission['id']); $this->assertGreaterThanOrEqual(3, count($submission['plugins'])); $plugins = $submission['plugins']; foreach ($plugins as $plugin) { $foundonlinetext = false; if ($plugin['type'] == 'onlinetext') { $foundonlinetext = true; break; } } $this->assertTrue($foundonlinetext); }