Skip to content

Commit

Permalink
MDL-59242 mod_workshop: New WS mod_workshop_get_submission
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Oct 2, 2017
1 parent 3f08cfc commit 6e2f486
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 1 deletion.
75 changes: 75 additions & 0 deletions mod/workshop/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -873,4 +873,79 @@ public static function get_submissions_returns() {
)
);
}

/**
* Returns the description of the external function parameters.
*
* @return external_function_parameters
* @since Moodle 3.4
*/
public static function get_submission_parameters() {
return new external_function_parameters(
array(
'submissionid' => new external_value(PARAM_INT, 'Submission id'),
)
);
}


/**
* Retrieves the given submission.
*
* @param int $submissionid the submission id
* @return array containing the submission and warnings.
* @since Moodle 3.4
* @throws moodle_exception
*/
public static function get_submission($submissionid) {
global $USER, $DB, $PAGE;

$params = self::validate_parameters(self::get_submission_parameters(), array('submissionid' => $submissionid));
$warnings = array();

// Get and validate the submission and workshop.
$submission = $DB->get_record('workshop_submissions', array('id' => $params['submissionid']), '*', MUST_EXIST);
list($workshop, $course, $cm, $context) = self::validate_workshop($submission->workshopid);

$workshopclosed = $workshop->phase == workshop::PHASE_CLOSED;
$canviewpublished = has_capability('mod/workshop:viewpublishedsubmissions', $context);

$canview = $submission->authorid == $USER->id; // I did it.
$canview = $canview || !empty($workshop->get_assessment_of_submission_by_user($submission->id, $USER->id)); // I reviewed.
$canview = $canview || has_capability('mod/workshop:viewallsubmissions', $context); // I can view all.
$canview = $canview || ($submission->published && $workshopclosed && $canviewpublished); // It has been published.

if ($canview) {
// Here we should check if the user share group.
if ($submission->authorid != $USER->id && !groups_user_groups_visible($course, $submission->authorid, $cm)) {
throw new moodle_exception('notingroup');
}
} else {
throw new moodle_exception('nopermissions', 'error', '', 'view submission');
}

$submission = self::prepare_submission_for_external($submission, $workshop);

$related = array('context' => $context);
$exporter = new submission_exporter($submission, $related);
return array(
'submission' => $exporter->export($PAGE->get_renderer('core')),
'warnings' => $warnings
);
}

/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 3.4
*/
public static function get_submission_returns() {
return new external_single_structure(
array(
'submission' => submission_exporter::get_read_structure(),
'warnings' => new external_warnings()
)
);
}
}
7 changes: 7 additions & 0 deletions mod/workshop/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,11 @@
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
'mod_workshop_get_submission' => array(
'classname' => 'mod_workshop_external',
'methodname' => 'get_submission',
'description' => 'Retrieves the given submission.',
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
);
135 changes: 135 additions & 0 deletions mod/workshop/tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -930,4 +930,139 @@ public function test_get_submissions_from_students_as_teacher() {
$this->assertEquals(1, $result['totalcount']);
$this->assertEquals($submissionid2, $result['submissions'][0]['id']);
}

/**
* Test test_get_submission_student.
*/
public function test_get_submission_student() {

// Create a couple of submissions with files.
$firstsubmissionid = $this->create_test_submission($this->student); // Create submission with files.

$this->setUser($this->student);
$result = mod_workshop_external::get_submission($firstsubmissionid);
$result = external_api::clean_returnvalue(mod_workshop_external::get_submission_returns(), $result);
$this->assertEquals($firstsubmissionid, $result['submission']['id']);
$this->assertCount(1, $result['submission']['contentfiles']); // Check we retrieve submission text files.
$this->assertCount(1, $result['submission']['attachmentfiles']); // Check we retrieve attachment files.
}

/**
* Test test_get_submission_i_reviewed.
*/
public function test_get_submission_i_reviewed() {

// Create a couple of submissions with files.
$firstsubmissionid = $this->create_test_submission($this->student); // Create submission with files.
$workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
$workshopgenerator->create_assessment($firstsubmissionid, $this->anotherstudentg1->id, array(
'weight' => 3,
'grade' => 95,
));
// Now try to get the submission I just reviewed.
$this->setUser($this->anotherstudentg1);
$result = mod_workshop_external::get_submission($firstsubmissionid);
$result = external_api::clean_returnvalue(mod_workshop_external::get_submission_returns(), $result);
$this->assertEquals($firstsubmissionid, $result['submission']['id']);
$this->assertCount(1, $result['submission']['contentfiles']); // Check we retrieve submission text files.
$this->assertCount(1, $result['submission']['attachmentfiles']); // Check we retrieve attachment files.
}

/**
* Test test_get_submission_other_student.
*/
public function test_get_submission_other_student() {

// Create a couple of submissions with files.
$firstsubmissionid = $this->create_test_submission($this->student); // Create submission with files.
// Expect failure.
$this->setUser($this->anotherstudentg1);
$this->expectException('moodle_exception');
$result = mod_workshop_external::get_submission($firstsubmissionid);
}

/**
* Test test_get_submission_published_student.
*/
public function test_get_submission_published_student() {
global $DB;

$DB->set_field('workshop', 'phase', workshop::PHASE_CLOSED, array('id' => $this->workshop->id));
// Create a couple of submissions with files.
$workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
$submission = array('published' => 1);
$submissionid = $workshopgenerator->create_submission($this->workshop->id, $this->anotherstudentg1->id, $submission);

$this->setUser($this->student);
$result = mod_workshop_external::get_submission($submissionid);
$result = external_api::clean_returnvalue(mod_workshop_external::get_submission_returns(), $result);
$this->assertEquals($submissionid, $result['submission']['id']);
// Check that the student don't see the other student grade/feedback data even if is published.
// We shoul not see the grade or feedback information.
$properties = submission_exporter::properties_definition();
foreach ($properties as $attribute => $settings) {
if (!empty($settings['optional'])) {
if (isset($result['submission'][$attribute])) {
echo "error $attribute";
}
$this->assertFalse(isset($result['submission'][$attribute]));
}
}

// Check with group restrictions.
$this->setUser($this->anotherstudentg2);
$this->expectException('moodle_exception');
mod_workshop_external::get_submission($submissionid);
}

/**
* Test test_get_submission_from_student_with_feedback_from_teacher.
*/
public function test_get_submission_from_student_with_feedback_from_teacher() {
global $DB;

// Create a couple of submissions with files.
$workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
$submissionid = $workshopgenerator->create_submission($this->workshop->id, $this->student->id);
// Create teacher feedback for submission.
$record = new stdclass();
$record->id = $submissionid;
$record->gradeover = 9;
$record->gradeoverby = $this->teacher->id;
$record->feedbackauthor = 'Hey';
$record->feedbackauthorformat = FORMAT_MOODLE;
$record->published = 1;
$DB->update_record('workshop_submissions', $record);

// Remove teacher caps.
assign_capability('mod/workshop:viewallsubmissions', CAP_PROHIBIT, $this->teacher->id, $this->context->id);
// Empty all the caches that may be affected by this change.
accesslib_clear_all_caches_for_unit_testing();
course_modinfo::clear_instance_cache();

$this->setUser($this->teacher);
$result = mod_workshop_external::get_submission($submissionid);
$result = external_api::clean_returnvalue(mod_workshop_external::get_submission_returns(), $result);
$this->assertEquals($submissionid, $result['submission']['id']);
}

/**
* Test test_get_submission_from_students_as_teacher.
*/
public function test_get_submission_from_students_as_teacher() {
// Create a couple of submissions with files.
$workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
$submissionid1 = $workshopgenerator->create_submission($this->workshop->id, $this->student->id);
$submissionid2 = $workshopgenerator->create_submission($this->workshop->id, $this->anotherstudentg1->id);
$submissionid3 = $workshopgenerator->create_submission($this->workshop->id, $this->anotherstudentg2->id);

$this->setUser($this->teacher);
$result = mod_workshop_external::get_submission($submissionid1); // Get all.
$result = external_api::clean_returnvalue(mod_workshop_external::get_submission_returns(), $result);
$this->assertEquals($submissionid1, $result['submission']['id']);

$result = mod_workshop_external::get_submission($submissionid3); // Get group 2.
$result = external_api::clean_returnvalue(mod_workshop_external::get_submission_returns(), $result);
$this->assertEquals($submissionid3, $result['submission']['id']);
}
}
2 changes: 1 addition & 1 deletion mod/workshop/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2017051508; // The current module version (YYYYMMDDXX)
$plugin->version = 2017051509; // The current module version (YYYYMMDDXX)
$plugin->requires = 2017050500; // Requires this Moodle version.
$plugin->component = 'mod_workshop';
$plugin->cron = 60; // Give as a chance every minute.

0 comments on commit 6e2f486

Please sign in to comment.