/**
  * Test get_assignments
  */
 public function test_get_assignments()
 {
     global $DB, $USER, $CFG;
     $this->resetAfterTest(true);
     $category = self::getDataGenerator()->create_category(array('name' => 'Test category'));
     // Create a course.
     $course1 = self::getDataGenerator()->create_course(array('idnumber' => 'idnumbercourse1', 'fullname' => '<b>Lightwork Course 1</b>', 'shortname' => '<b>Lightwork Course 1</b>', 'summary' => 'Lightwork Course 1 description', 'summaryformat' => FORMAT_MOODLE, 'category' => $category->id));
     // Create a second course, just for testing.
     $course2 = self::getDataGenerator()->create_course(array('idnumber' => 'idnumbercourse2', 'fullname' => 'Lightwork Course 2', 'summary' => 'Lightwork Course 2 description', 'summaryformat' => FORMAT_MOODLE, 'category' => $category->id));
     // Create the assignment module with links to a filerecord.
     $assign1 = self::getDataGenerator()->create_module('assign', array('course' => $course1->id, 'name' => 'lightwork assignment', 'intro' => 'the assignment intro text here <a href="@@PLUGINFILE@@/intro.txt">link</a>', 'introformat' => FORMAT_HTML, 'markingworkflow' => 1, 'markingallocation' => 1));
     // Add a file as assignment attachment.
     $context = context_module::instance($assign1->cmid);
     $filerecord = array('component' => 'mod_assign', 'filearea' => 'intro', 'contextid' => $context->id, 'itemid' => 0, 'filename' => 'intro.txt', 'filepath' => '/');
     $fs = get_file_storage();
     $fs->create_file_from_string($filerecord, 'Test intro file');
     // Create manual enrolment record.
     $enrolid = $DB->insert_record('enrol', (object) array('enrol' => 'manual', 'status' => 0, 'courseid' => $course1->id));
     // Create the user and give them capabilities.
     $context = context_course::instance($course1->id);
     $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
     $context = context_module::instance($assign1->cmid);
     $this->assignUserCapability('mod/assign:view', $context->id, $roleid);
     // Create the user enrolment record.
     $DB->insert_record('user_enrolments', (object) array('status' => 0, 'enrolid' => $enrolid, 'userid' => $USER->id));
     // Add a file as assignment attachment.
     $filerecord = array('component' => 'mod_assign', 'filearea' => ASSIGN_INTROATTACHMENT_FILEAREA, 'contextid' => $context->id, 'itemid' => 0, 'filename' => 'introattachment.txt', 'filepath' => '/');
     $fs = get_file_storage();
     $fs->create_file_from_string($filerecord, 'Test intro attachment file');
     $result = mod_assign_external::get_assignments();
     // We need to execute the return values cleaning process to simulate the web service server.
     $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
     // Check the course and assignment are returned.
     $this->assertEquals(1, count($result['courses']));
     $course = $result['courses'][0];
     $this->assertEquals('Lightwork Course 1', $course['fullname']);
     $this->assertEquals('Lightwork Course 1', $course['shortname']);
     $this->assertEquals(1, count($course['assignments']));
     $assignment = $course['assignments'][0];
     $this->assertEquals($assign1->id, $assignment['id']);
     $this->assertEquals($course1->id, $assignment['course']);
     $this->assertEquals('lightwork assignment', $assignment['name']);
     $this->assertContains('the assignment intro text here', $assignment['intro']);
     // Check the url of the file attatched.
     $this->assertRegExp('@"' . $CFG->wwwroot . '/webservice/pluginfile.php/\\d+/mod_assign/intro/intro\\.txt"@', $assignment['intro']);
     $this->assertEquals(1, $assignment['markingworkflow']);
     $this->assertEquals(1, $assignment['markingallocation']);
     $this->assertEquals(0, $assignment['preventsubmissionnotingroup']);
     $this->assertCount(1, $assignment['introattachments']);
     $this->assertEquals('introattachment.txt', $assignment['introattachments'][0]['filename']);
     // Now, hide the descritption until the submission from date.
     $DB->set_field('assign', 'alwaysshowdescription', 0, array('id' => $assign1->id));
     $DB->set_field('assign', 'allowsubmissionsfromdate', time() + DAYSECS, array('id' => $assign1->id));
     $result = mod_assign_external::get_assignments(array($course1->id));
     // We need to execute the return values cleaning process to simulate the web service server.
     $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
     $this->assertEquals(1, count($result['courses']));
     $course = $result['courses'][0];
     $this->assertEquals('Lightwork Course 1', $course['fullname']);
     $this->assertEquals(1, count($course['assignments']));
     $assignment = $course['assignments'][0];
     $this->assertEquals($assign1->id, $assignment['id']);
     $this->assertEquals($course1->id, $assignment['course']);
     $this->assertEquals('lightwork assignment', $assignment['name']);
     $this->assertArrayNotHasKey('intro', $assignment);
     $this->assertArrayNotHasKey('introattachments', $assignment);
     $this->assertEquals(1, $assignment['markingworkflow']);
     $this->assertEquals(1, $assignment['markingallocation']);
     $this->assertEquals(0, $assignment['preventsubmissionnotingroup']);
     $result = mod_assign_external::get_assignments(array($course2->id));
     // We need to execute the return values cleaning process to simulate the web service server.
     $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
     $this->assertEquals(0, count($result['courses']));
     $this->assertEquals(1, count($result['warnings']));
     // Test with non-enrolled user, but with view capabilities.
     $this->setAdminUser();
     $result = mod_assign_external::get_assignments();
     $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
     $this->assertEquals(0, count($result['courses']));
     $this->assertEquals(0, count($result['warnings']));
     // Expect no courses, because we are not using the special flag.
     $result = mod_assign_external::get_assignments(array($course1->id));
     $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
     $this->assertCount(0, $result['courses']);
     // Now use the special flag to return courses where you are not enroled in.
     $result = mod_assign_external::get_assignments(array($course1->id), array(), true);
     $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
     $this->assertCount(1, $result['courses']);
     $course = $result['courses'][0];
     $this->assertEquals('Lightwork Course 1', $course['fullname']);
     $this->assertEquals(1, count($course['assignments']));
     $assignment = $course['assignments'][0];
     $this->assertEquals($assign1->id, $assignment['id']);
     $this->assertEquals($course1->id, $assignment['course']);
     $this->assertEquals('lightwork assignment', $assignment['name']);
     $this->assertArrayNotHasKey('intro', $assignment);
     $this->assertArrayNotHasKey('introattachments', $assignment);
     $this->assertEquals(1, $assignment['markingworkflow']);
     $this->assertEquals(1, $assignment['markingallocation']);
     $this->assertEquals(0, $assignment['preventsubmissionnotingroup']);
 }
Example #2
0
 /**
  * Test get_assignments with submissionstatement.
  */
 public function test_get_assignments_with_submissionstatement()
 {
     global $DB, $USER, $CFG;
     $this->resetAfterTest(true);
     // Setup test data. Create 2 assigns, one with requiresubmissionstatement and the other without it.
     $course = $this->getDataGenerator()->create_course();
     $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id, 'requiresubmissionstatement' => 1));
     $assign2 = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
     // Create student.
     $student = self::getDataGenerator()->create_user();
     // Users enrolments.
     $studentrole = $DB->get_record('role', array('shortname' => 'student'));
     $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
     // Update the submissionstatement.
     $submissionstatement = 'This is a fake submission statement.';
     set_config('submissionstatement', $submissionstatement, 'assign');
     $this->setUser($student);
     $result = mod_assign_external::get_assignments();
     // We need to execute the return values cleaning process to simulate the web service server.
     $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
     // Check that the amount of courses and assignments is right.
     $this->assertCount(1, $result['courses']);
     $assignmentsret = $result['courses'][0]['assignments'];
     $this->assertCount(2, $assignmentsret);
     // Order the returned assignments by ID.
     usort($assignmentsret, function ($a, $b) {
         return strcmp($a['id'], $b['id']);
     });
     // Check that the first assign contains the submission statement.
     $assignmentret = $assignmentsret[0];
     $this->assertEquals($assign->id, $assignmentret['id']);
     $this->assertEquals(1, $assignmentret['requiresubmissionstatement']);
     $this->assertEquals($submissionstatement, $assignmentret['submissionstatement']);
     // Check that the second assign does NOT contain the submission statement.
     $assignmentret = $assignmentsret[1];
     $this->assertEquals($assign2->id, $assignmentret['id']);
     $this->assertEquals(0, $assignmentret['requiresubmissionstatement']);
     $this->assertArrayNotHasKey('submissionstatement', $assignmentret);
 }
Example #3
0
 /**
  * Test get_assignments
  */
 public function test_get_assignments()
 {
     global $DB, $USER;
     $this->resetAfterTest(true);
     $category = self::getDataGenerator()->create_category(array('name' => 'Test category'));
     // Create a course.
     $course1 = self::getDataGenerator()->create_course(array('idnumber' => 'idnumbercourse1', 'fullname' => 'Lightwork Course 1', 'summary' => 'Lightwork Course 1 description', 'summaryformat' => FORMAT_MOODLE, 'category' => $category->id));
     // Create a second course, just for testing.
     $course2 = self::getDataGenerator()->create_course(array('idnumber' => 'idnumbercourse2', 'fullname' => 'Lightwork Course 2', 'summary' => 'Lightwork Course 2 description', 'summaryformat' => FORMAT_MOODLE, 'category' => $category->id));
     // Create the assignment module.
     $assign1 = self::getDataGenerator()->create_module('assign', array('course' => $course1->id, 'name' => 'lightwork assignment'));
     // Create manual enrolment record.
     $enrolid = $DB->insert_record('enrol', (object) array('enrol' => 'manual', 'status' => 0, 'courseid' => $course1->id));
     // Create the user and give them capabilities.
     $context = context_course::instance($course1->id);
     $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
     $context = context_module::instance($assign1->id);
     $this->assignUserCapability('mod/assign:view', $context->id, $roleid);
     // Create the user enrolment record.
     $DB->insert_record('user_enrolments', (object) array('status' => 0, 'enrolid' => $enrolid, 'userid' => $USER->id));
     $result = mod_assign_external::get_assignments();
     // Check the course and assignment are returned.
     $this->assertEquals(1, count($result['courses']));
     $course = $result['courses'][0];
     $this->assertEquals('Lightwork Course 1', $course['fullname']);
     $this->assertEquals(1, count($course['assignments']));
     $assignment = $course['assignments'][0];
     $this->assertEquals($assign1->id, $assignment['id']);
     $this->assertEquals($course1->id, $assignment['course']);
     $this->assertEquals('lightwork assignment', $assignment['name']);
     $result = mod_assign_external::get_assignments(array($course1->id));
     $this->assertEquals(1, count($result['courses']));
     $course = $result['courses'][0];
     $this->assertEquals('Lightwork Course 1', $course['fullname']);
     $this->assertEquals(1, count($course['assignments']));
     $assignment = $course['assignments'][0];
     $this->assertEquals($assign1->id, $assignment['id']);
     $this->assertEquals($course1->id, $assignment['course']);
     $this->assertEquals('lightwork assignment', $assignment['name']);
     $result = mod_assign_external::get_assignments(array($course2->id));
     $this->assertEquals(0, count($result['courses']));
     $this->assertEquals(1, count($result['warnings']));
 }
Example #4
0
 /**
  * Test get_assignments
  */
 public function test_get_assignments()
 {
     global $DB, $USER;
     $this->resetAfterTest(true);
     $category = self::getDataGenerator()->create_category(array('name' => 'Test category'));
     // Create a course.
     $course1 = self::getDataGenerator()->create_course(array('idnumber' => 'idnumbercourse1', 'fullname' => 'Lightwork Course 1', 'summary' => 'Lightwork Course 1 description', 'summaryformat' => FORMAT_MOODLE, 'category' => $category->id));
     // Create a second course, just for testing.
     $course2 = self::getDataGenerator()->create_course(array('idnumber' => 'idnumbercourse2', 'fullname' => 'Lightwork Course 2', 'summary' => 'Lightwork Course 2 description', 'summaryformat' => FORMAT_MOODLE, 'category' => $category->id));
     // Create the assignment module.
     $assign1 = self::getDataGenerator()->create_module('assign', array('course' => $course1->id, 'name' => 'lightwork assignment', 'intro' => 'the assignment intro text here', 'markingworkflow' => 1, 'markingallocation' => 1));
     // Create manual enrolment record.
     $enrolid = $DB->insert_record('enrol', (object) array('enrol' => 'manual', 'status' => 0, 'courseid' => $course1->id));
     // Create the user and give them capabilities.
     $context = context_course::instance($course1->id);
     $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
     $context = context_module::instance($assign1->cmid);
     $this->assignUserCapability('mod/assign:view', $context->id, $roleid);
     // Create the user enrolment record.
     $DB->insert_record('user_enrolments', (object) array('status' => 0, 'enrolid' => $enrolid, 'userid' => $USER->id));
     // Add a file as assignment attachment.
     $filerecord = array('component' => 'mod_assign', 'filearea' => ASSIGN_INTROATTACHMENT_FILEAREA, 'contextid' => $context->id, 'itemid' => 0, 'filename' => 'introattachment.txt', 'filepath' => '/');
     $fs = get_file_storage();
     $fs->create_file_from_string($filerecord, 'Test intro attachment file');
     $result = mod_assign_external::get_assignments();
     // We need to execute the return values cleaning process to simulate the web service server.
     $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
     // Check the course and assignment are returned.
     $this->assertEquals(1, count($result['courses']));
     $course = $result['courses'][0];
     $this->assertEquals('Lightwork Course 1', $course['fullname']);
     $this->assertEquals(1, count($course['assignments']));
     $assignment = $course['assignments'][0];
     $this->assertEquals($assign1->id, $assignment['id']);
     $this->assertEquals($course1->id, $assignment['course']);
     $this->assertEquals('lightwork assignment', $assignment['name']);
     $this->assertEquals('the assignment intro text here', $assignment['intro']);
     $this->assertEquals(1, $assignment['markingworkflow']);
     $this->assertEquals(1, $assignment['markingallocation']);
     $this->assertCount(1, $assignment['introattachments']);
     $this->assertEquals('introattachment.txt', $assignment['introattachments'][0]['filename']);
     // Now, hide the descritption until the submission from date.
     $DB->set_field('assign', 'alwaysshowdescription', 0, array('id' => $assign1->id));
     $DB->set_field('assign', 'allowsubmissionsfromdate', time() + DAYSECS, array('id' => $assign1->id));
     $result = mod_assign_external::get_assignments(array($course1->id));
     // We need to execute the return values cleaning process to simulate the web service server.
     $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
     $this->assertEquals(1, count($result['courses']));
     $course = $result['courses'][0];
     $this->assertEquals('Lightwork Course 1', $course['fullname']);
     $this->assertEquals(1, count($course['assignments']));
     $assignment = $course['assignments'][0];
     $this->assertEquals($assign1->id, $assignment['id']);
     $this->assertEquals($course1->id, $assignment['course']);
     $this->assertEquals('lightwork assignment', $assignment['name']);
     $this->assertArrayNotHasKey('intro', $assignment);
     $this->assertArrayNotHasKey('introattachments', $assignment);
     $this->assertEquals(1, $assignment['markingworkflow']);
     $this->assertEquals(1, $assignment['markingallocation']);
     $result = mod_assign_external::get_assignments(array($course2->id));
     // We need to execute the return values cleaning process to simulate the web service server.
     $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
     $this->assertEquals(0, count($result['courses']));
     $this->assertEquals(1, count($result['warnings']));
 }