Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-41738 added web service function mod_assign_set_user_flags

  • Loading branch information...
commit e8b443df331c54c96ca37502e20ce10f8b69cf51 1 parent d45e65c
@pcharsle pcharsle authored
View
9 mod/assign/db/services.php
@@ -57,6 +57,15 @@
'type' => 'read'
),
+ 'mod_assign_set_user_flags' => array(
+ 'classname' => 'mod_assign_external',
+ 'methodname' => 'set_user_flags',
+ 'classpath' => 'mod/assign/externallib.php',
+ 'description' => 'Creates or updates user flags',
+ 'type' => 'write',
+ 'capabilities'=> 'mod/assign:grade'
+ ),
+
'mod_assign_get_user_mappings' => array(
'classname' => 'mod_assign_external',
'methodname' => 'get_user_mappings',
View
143 mod/assign/externallib.php
@@ -737,6 +737,149 @@ public static function get_submissions_returns() {
}
/**
+ * Describes the parameters for set_user_flags
+ * @return external_function_parameters
+ * @since Moodle 2.6
+ */
+ public static function set_user_flags_parameters() {
+ return new external_function_parameters(
+ array(
+ 'assignmentid' => new external_value(PARAM_INT, 'assignment id'),
+ 'userflags' => new external_multiple_structure(
+ new external_single_structure(
+ array(
+ 'userid' => new external_value(PARAM_INT, 'student id'),
+ 'locked' => new external_value(PARAM_INT, 'locked', VALUE_OPTIONAL),
+ 'mailed' => new external_value(PARAM_INT, 'mailed', VALUE_OPTIONAL),
+ 'extensionduedate' => new external_value(PARAM_INT, 'extension due date', VALUE_OPTIONAL),
+ 'workflowstate' => new external_value(PARAM_TEXT, 'marking workflow state', VALUE_OPTIONAL),
+ 'allocatedmarker' => new external_value(PARAM_INT, 'allocated marker', VALUE_OPTIONAL)
+ )
+ )
+ )
+ )
+ );
+ }
+
+ /**
+ * Create or update user_flags records
+ *
+ * @param int $assignmentid the assignment for which the userflags are created or updated
+ * @param array $userflags An array of userflags to create or update
+ * @return array containing success or failure information for each record
+ * @since Moodle 2.6
+ */
+ public static function set_user_flags($assignmentid, $userflags = array()) {
+ global $CFG, $DB;
+ require_once($CFG->dirroot . "/mod/assign/locallib.php");
+
+ $params = self::validate_parameters(self::set_user_flags_parameters(),
+ array('assignmentid' => $assignmentid,
+ 'userflags' => $userflags));
+
+ // Load assignment if it exists and if the user has the capability.
+ $cm = get_coursemodule_from_instance('assign', $params['assignmentid'], 0, false, MUST_EXIST);
+ $context = context_module::instance($cm->id);
+ self::validate_context($context);
+ require_capability('mod/assign:grade', $context);
+ $assign = new assign($context, null, null);
+
+ $results = array();
+ foreach ($params['userflags'] as $userflag) {
+ $success = true;
+ $result = array();
+
+ $record = $assign->get_user_flags($userflag['userid'], false);
+ if ($record) {
+ if (isset($userflag['locked'])) {
+ $record->locked = $userflag['locked'];
+ }
+ if (isset($userflag['mailed'])) {
+ $record->mailed = $userflag['mailed'];
+ }
+ if (isset($userflag['extensionduedate'])) {
+ $record->extensionduedate = $userflag['extensionduedate'];
+ }
+ if (isset($userflag['workflowstate'])) {
+ $record->workflowstate = $userflag['workflowstate'];
+ }
+ if (isset($userflag['allocatedmarker'])) {
+ $record->allocatedmarker = $userflag['allocatedmarker'];
+ }
+ if ($assign->update_user_flags($record)) {
+ $result['id'] = $record->id;
+ $result['userid'] = $userflag['userid'];
+ } else {
+ $result['id'] = $record->id;
+ $result['userid'] = $userflag['userid'];
+ $result['errormessage'] = 'Record created but values could not be set';
+ }
+ } else {
+ $record = $assign->get_user_flags($userflag['userid'], true);
+ $setfields = isset($userflag['locked'])
+ || isset($userflag['mailed'])
+ || isset($userflag['extensionduedate'])
+ || isset($userflag['workflowstate'])
+ || isset($userflag['allocatedmarker']);
+ if ($record) {
+ if ($setfields) {
+ if (isset($userflag['locked'])) {
+ $record->locked = $userflag['locked'];
+ }
+ if (isset($userflag['mailed'])) {
+ $record->mailed = $userflag['mailed'];
+ }
+ if (isset($userflag['extensionduedate'])) {
+ $record->extensionduedate = $userflag['extensionduedate'];
+ }
+ if (isset($userflag['workflowstate'])) {
+ $record->workflowstate = $userflag['workflowstate'];
+ }
+ if (isset($userflag['allocatedmarker'])) {
+ $record->allocatedmarker = $userflag['allocatedmarker'];
+ }
+ if ($assign->update_user_flags($record)) {
+ $result['id'] = $record->id;
+ $result['userid'] = $userflag['userid'];
+ } else {
+ $result['id'] = $record->id;
+ $result['userid'] = $userflag['userid'];
+ $result['errormessage'] = 'Record created but values could not be set';
+ }
+ } else {
+ $result['id'] = $record->id;
+ $result['userid'] = $userflag['userid'];
+ }
+ } else {
+ $result['id'] = -1;
+ $result['userid'] = $userflag['userid'];
+ $result['errormessage'] = 'Record could not be created';
+ }
+ }
+
+ $results[] = $result;
+ }
+ return $results;
+ }
+
+ /**
+ * Describes the set_user_flags return value
+ * @return external_multiple_structure
+ * @since Moodle 2.6
+ */
+ public static function set_user_flags_returns() {
+ return new external_multiple_structure(
+ new external_single_structure(
+ array(
+ 'id' => new external_value(PARAM_INT, 'id of record if successful, -1 for failure'),
+ 'userid' => new external_value(PARAM_INT, 'userid of record'),
+ 'errormessage' => new external_value(PARAM_TEXT, 'Failure error message', VALUE_OPTIONAL)
+ )
+ )
+ );
+ }
+
+ /**
* Describes the parameters for get_user_flags
* @return external_function_parameters
* @since Moodle 2.6
View
87 mod/assign/tests/externallib_test.php
@@ -1043,4 +1043,91 @@ public function test_copy_previous_attempt() {
$this->assertNotEmpty($result['assignments'][0]['submissions'][0]['plugins']);
}
+
+ /**
+ * Test set_user_flags
+ */
+ public function test_set_user_flags() {
+ global $DB, $USER;
+
+ $this->resetAfterTest(true);
+ // Create a course and assignment.
+ $coursedata['idnumber'] = 'idnumbercourse';
+ $coursedata['fullname'] = 'Lightwork Course';
+ $coursedata['summary'] = 'Lightwork Course description';
+ $coursedata['summaryformat'] = FORMAT_MOODLE;
+ $course = self::getDataGenerator()->create_course($coursedata);
+
+ $assigndata['course'] = $course->id;
+ $assigndata['name'] = 'lightwork assignment';
+
+ $assign = self::getDataGenerator()->create_module('assign', $assigndata);
+
+ // Create a manual enrolment record.
+ $manualenroldata['enrol'] = 'manual';
+ $manualenroldata['status'] = 0;
+ $manualenroldata['courseid'] = $course->id;
+ $enrolid = $DB->insert_record('enrol', $manualenroldata);
+
+ // Create a teacher and give them capabilities.
+ $context = context_course::instance($course->id);
+ $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
+ $context = context_module::instance($assign->id);
+ $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
+
+ // Create the teacher's enrolment record.
+ $userenrolmentdata['status'] = 0;
+ $userenrolmentdata['enrolid'] = $enrolid;
+ $userenrolmentdata['userid'] = $USER->id;
+ $DB->insert_record('user_enrolments', $userenrolmentdata);
+
+ // Create a student.
+ $student = self::getDataGenerator()->create_user();
+
+ // Create test user flags record.
+ $userflags = array();
+ $userflag['userid'] = $student->id;
+ $userflag['workflowstate'] = 'inmarking';
+ $userflag['allocatedmarker'] = $USER->id;
+ $userflags = array($userflag);
+
+ $createduserflags = mod_assign_external::set_user_flags($assign->id, $userflags);
+ // We need to execute the return values cleaning process to simulate the web service server.
+ $createduserflags = external_api::clean_returnvalue(mod_assign_external::set_user_flags_returns(), $createduserflags);
+
+ $this->assertEquals($student->id, $createduserflags[0]['userid']);
+ $createduserflag = $DB->get_record('assign_user_flags', array('id' => $createduserflags[0]['id']));
+
+ // Confirm that all data was inserted correctly.
+ $this->assertEquals($student->id, $createduserflag->userid);
+ $this->assertEquals($assign->id, $createduserflag->assignment);
+ $this->assertEquals(0, $createduserflag->locked);
+ $this->assertEquals(2, $createduserflag->mailed);
+ $this->assertEquals(0, $createduserflag->extensionduedate);
+ $this->assertEquals('inmarking', $createduserflag->workflowstate);
+ $this->assertEquals($USER->id, $createduserflag->allocatedmarker);
+
+ // Create update data.
+ $userflags = array();
+ $userflag['userid'] = $createduserflag->userid;
+ $userflag['workflowstate'] = 'readyforreview';
+ $userflags = array($userflag);
+
+ $updateduserflags = mod_assign_external::set_user_flags($assign->id, $userflags);
+ // We need to execute the return values cleaning process to simulate the web service server.
+ $updateduserflags = external_api::clean_returnvalue(mod_assign_external::set_user_flags_returns(), $updateduserflags);
+
+ $this->assertEquals($student->id, $updateduserflags[0]['userid']);
+ $updateduserflag = $DB->get_record('assign_user_flags', array('id' => $updateduserflags[0]['id']));
+
+ // Confirm that all data was updated correctly.
+ $this->assertEquals($student->id, $updateduserflag->userid);
+ $this->assertEquals($assign->id, $updateduserflag->assignment);
+ $this->assertEquals(0, $updateduserflag->locked);
+ $this->assertEquals(2, $updateduserflag->mailed);
+ $this->assertEquals(0, $updateduserflag->extensionduedate);
+ $this->assertEquals('readyforreview', $updateduserflag->workflowstate);
+ $this->assertEquals($USER->id, $updateduserflag->allocatedmarker);
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.