Permalink
Browse files

MDL-40548 added mod_assign_get_user_flags and

mod_assign_get_user_mapping
  • Loading branch information...
1 parent bdd045c commit 07df8c38d9044cdd02c6f008fc0c93c1d2679762 @pcharsle pcharsle committed Jul 28, 2013
Showing with 464 additions and 17 deletions.
  1. +16 −0 mod/assign/db/services.php
  2. +298 −0 mod/assign/externallib.php
  3. +149 −16 mod/assign/tests/externallib_test.php
  4. +1 −1 mod/assign/version.php
View
@@ -47,5 +47,21 @@
'classpath' => 'mod/assign/externallib.php',
'description' => 'Returns the submissions for assignments',
'type' => 'read'
+ ),
+
+ 'mod_assign_get_user_flags' => array(
+ 'classname' => 'mod_assign_external',
+ 'methodname' => 'get_user_flags',
+ 'classpath' => 'mod/assign/externallib.php',
+ 'description' => 'Returns the user flags for assignments',
+ 'type' => 'read'
+ ),
+
+ 'mod_assign_get_user_mappings' => array(
+ 'classname' => 'mod_assign_external',
+ 'methodname' => 'get_user_mappings',
+ 'classpath' => 'mod/assign/externallib.php',
+ 'description' => 'Returns the blind marking mappings for assignments',
+ 'type' => 'read'
)
);
View
@@ -687,4 +687,302 @@ public static function get_submissions_returns() {
)
);
}
+
+ /**
+ * Describes the parameters for get_user_flags
+ * @return external_function_parameters
+ * @since Moodle 2.6
+ */
+ public static function get_user_flags_parameters() {
+ return new external_function_parameters(
+ array(
+ 'assignmentids' => new external_multiple_structure(
+ new external_value(PARAM_INT, 'assignment id'),
+ '1 or more assignment ids',
+ VALUE_REQUIRED)
+ )
+ );
+ }
+
+ /**
+ * Returns user flag information from assign_user_flags for the requested assignment ids
+ * @param array of ints $assignmentids
+ * @return array of user flag records for each requested assignment
+ * @since Moodle 2.6
+ */
+ public static function get_user_flags($assignmentids) {
+ global $DB;
+ $params = self::validate_parameters(self::get_user_flags_parameters(),
+ array('assignmentids' => $assignmentids));
+
+ $assignments = array();
+ $warnings = array();
+ $requestedassignmentids = $params['assignmentids'];
+
+ // Check the user is allowed to get the user flags for the assignments requested.
+ $placeholders = array();
+ list($sqlassignmentids, $placeholders) = $DB->get_in_or_equal($requestedassignmentids, SQL_PARAMS_NAMED);
+ $sql = "SELECT cm.id, cm.instance FROM {course_modules} cm JOIN {modules} md ON md.id = cm.module ".
+ "WHERE md.name = :modname AND cm.instance ".$sqlassignmentids;
+ $placeholders['modname'] = 'assign';
+ $cms = $DB->get_records_sql($sql, $placeholders);
+ foreach ($cms as $cm) {
+ try {
+ $context = context_module::instance($cm->id);
+ self::validate_context($context);
+ require_capability('mod/assign:grade', $context);
+ } catch (Exception $e) {
+ $requestedassignmentids = array_diff($requestedassignmentids, array($cm->instance));
+ $warning = array();
+ $warning['item'] = 'assignment';
+ $warning['itemid'] = $cm->instance;
+ $warning['warningcode'] = '1';
+ $warning['message'] = 'No access rights in module context';
+ $warnings[] = $warning;
+ }
+ }
+
+ // Create the query and populate an array of assign_user_flags records from the recordset results.
+ if (count ($requestedassignmentids) > 0) {
+ $placeholders = array();
+ list($inorequalsql, $placeholders) = $DB->get_in_or_equal($requestedassignmentids, SQL_PARAMS_NAMED);
+
+ $sql = "SELECT auf.id,auf.assignment,auf.userid,auf.locked,auf.mailed,".
+ "auf.extensionduedate,auf.workflowstate,auf.allocatedmarker ".
+ "FROM {assign_user_flags} auf ".
+ "WHERE auf.assignment ".$inorequalsql.
+ " ORDER BY auf.assignment, auf.id";
+
+ $rs = $DB->get_recordset_sql($sql, $placeholders);
+ $currentassignmentid = null;
+ $assignment = null;
+ foreach ($rs as $rd) {
+ $userflag = array();
+ $userflag['id'] = $rd->id;
+ $userflag['userid'] = $rd->userid;
+ $userflag['locked'] = $rd->locked;
+ $userflag['mailed'] = $rd->mailed;
+ $userflag['extensionduedate'] = $rd->extensionduedate;
+ $userflag['workflowstate'] = $rd->workflowstate;
+ $userflag['allocatedmarker'] = $rd->allocatedmarker;
+
+ if (is_null($currentassignmentid) || ($rd->assignment != $currentassignmentid )) {
+ if (!is_null($assignment)) {
+ $assignments[] = $assignment;
+ }
+ $assignment = array();
+ $assignment['assignmentid'] = $rd->assignment;
+ $assignment['userflags'] = array();
+ $requestedassignmentids = array_diff($requestedassignmentids, array($rd->assignment));
+ }
+ $assignment['userflags'][] = $userflag;
+
+ $currentassignmentid = $rd->assignment;
+ }
+ if (!is_null($assignment)) {
+ $assignments[] = $assignment;
+ }
+ $rs->close();
+
+ }
+
+ foreach ($requestedassignmentids as $assignmentid) {
+ $warning = array();
+ $warning['item'] = 'assignment';
+ $warning['itemid'] = $assignmentid;
+ $warning['warningcode'] = '3';
+ $warning['message'] = 'No user flags found';
+ $warnings[] = $warning;
+ }
+
+ $result = array();
+ $result['assignments'] = $assignments;
+ $result['warnings'] = $warnings;
+ return $result;
+ }
+
+ /**
+ * Creates an assign_user_flags external_single_structure
+ * @return external_single_structure
+ * @since Moodle 2.6
+ */
+ private static function assign_user_flags() {
+ return new external_single_structure(
+ array (
+ 'assignmentid' => new external_value(PARAM_INT, 'assignment id'),
+ 'userflags' => new external_multiple_structure(new external_single_structure(
+ array(
+ 'id' => new external_value(PARAM_INT, 'user flag id'),
+ 'userid' => new external_value(PARAM_INT, 'student id'),
+ 'locked' => new external_value(PARAM_INT, 'locked'),
+ 'mailed' => new external_value(PARAM_INT, 'mailed'),
+ 'extensionduedate' => new external_value(PARAM_INT, 'extension due date'),
+ 'workflowstate' => new external_value(PARAM_TEXT, 'marking workflow state', VALUE_OPTIONAL),
+ 'allocatedmarker' => new external_value(PARAM_INT, 'allocated marker')
+ )
+ )
+ )
+ )
+ );
+ }
+
+ /**
+ * Describes the get_user_flags return value
+ * @return external_single_structure
+ * @since Moodle 2.6
+ */
+ public static function get_user_flags_returns() {
+ return new external_single_structure(
+ array(
+ 'assignments' => new external_multiple_structure(self::assign_user_flags(), 'list of assign user flag information'),
+ 'warnings' => new external_warnings('item is always \'assignment\'',
+ 'when errorcode is 3 then itemid is an assignment id. When errorcode is 1, itemid is a course module id',
+ 'errorcode can be 3 (no user flags found) or 1 (no permission to get user flags)')
+ )
+ );
+ }
+
+ /**
+ * Describes the parameters for get_user_mappings
+ * @return external_function_parameters
+ * @since Moodle 2.6
+ */
+ public static function get_user_mappings_parameters() {
+ return new external_function_parameters(
+ array(
+ 'assignmentids' => new external_multiple_structure(
+ new external_value(PARAM_INT, 'assignment id'),
+ '1 or more assignment ids',
+ VALUE_REQUIRED)
+ )
+ );
+ }
+
+ /**
+ * Returns user mapping information from assign_user_mapping for the requested assignment ids
+ * @param array of ints $assignmentids
+ * @return array of user mapping records for each requested assignment
+ * @since Moodle 2.6
+ */
+ public static function get_user_mappings($assignmentids) {
+ global $DB;
+ $params = self::validate_parameters(self::get_user_mappings_parameters(),
+ array('assignmentids' => $assignmentids));
+
+ $assignments = array();
+ $warnings = array();
+ $requestedassignmentids = $params['assignmentids'];
+
+ // Check the user is allowed to get the mappings for the assignments requested.
+ $placeholders = array();
+ list($sqlassignmentids, $placeholders) = $DB->get_in_or_equal($requestedassignmentids, SQL_PARAMS_NAMED);
+ $sql = "SELECT cm.id, cm.instance FROM {course_modules} cm JOIN {modules} md ON md.id = cm.module ".
+ "WHERE md.name = :modname AND cm.instance ".$sqlassignmentids;
+ $placeholders['modname'] = 'assign';
+ $cms = $DB->get_records_sql($sql, $placeholders);
+ foreach ($cms as $cm) {
+ try {
+ $context = context_module::instance($cm->id);
+ self::validate_context($context);
+ require_capability('mod/assign:revealidentities', $context);
+ } catch (Exception $e) {
+ $requestedassignmentids = array_diff($requestedassignmentids, array($cm->instance));
+ $warning = array();
+ $warning['item'] = 'assignment';
+ $warning['itemid'] = $cm->instance;
+ $warning['warningcode'] = '1';
+ $warning['message'] = 'No access rights in module context';
+ $warnings[] = $warning;
+ }
+ }
+
+ // Create the query and populate an array of assign_user_mapping records from the recordset results.
+ if (count ($requestedassignmentids) > 0) {
+ $placeholders = array();
+ list($inorequalsql, $placeholders) = $DB->get_in_or_equal($requestedassignmentids, SQL_PARAMS_NAMED);
+
+ $sql = "SELECT aum.id,aum.assignment,aum.userid ".
+ "FROM {assign_user_mapping} aum ".
+ "WHERE aum.assignment ".$inorequalsql.
+ " ORDER BY aum.assignment, aum.id";
+
+ $rs = $DB->get_recordset_sql($sql, $placeholders);
+ $currentassignmentid = null;
+ $assignment = null;
+ foreach ($rs as $rd) {
+ $mapping = array();
+ $mapping['id'] = $rd->id;
+ $mapping['userid'] = $rd->userid;
+
+ if (is_null($currentassignmentid) || ($rd->assignment != $currentassignmentid )) {
+ if (!is_null($assignment)) {
+ $assignments[] = $assignment;
+ }
+ $assignment = array();
+ $assignment['assignmentid'] = $rd->assignment;
+ $assignment['mappings'] = array();
+ $requestedassignmentids = array_diff($requestedassignmentids, array($rd->assignment));
+ }
+ $assignment['mappings'][] = $mapping;
+
+ $currentassignmentid = $rd->assignment;
+ }
+ if (!is_null($assignment)) {
+ $assignments[] = $assignment;
+ }
+ $rs->close();
+
+ }
+
+ foreach ($requestedassignmentids as $assignmentid) {
+ $warning = array();
+ $warning['item'] = 'assignment';
+ $warning['itemid'] = $assignmentid;
+ $warning['warningcode'] = '3';
+ $warning['message'] = 'No mappings found';
+ $warnings[] = $warning;
+ }
+
+ $result = array();
+ $result['assignments'] = $assignments;
+ $result['warnings'] = $warnings;
+ return $result;
+ }
+
+ /**
+ * Creates an assign_user_mappings external_single_structure
+ * @return external_single_structure
+ * @since Moodle 2.6
+ */
+ private static function assign_user_mappings() {
+ return new external_single_structure(
+ array (
+ 'assignmentid' => new external_value(PARAM_INT, 'assignment id'),
+ 'mappings' => new external_multiple_structure(new external_single_structure(
+ array(
+ 'id' => new external_value(PARAM_INT, 'user mapping id'),
+ 'userid' => new external_value(PARAM_INT, 'student id')
+ )
+ )
+ )
+ )
+ );
+ }
+
+ /**
+ * Describes the get_user_mappings return value
+ * @return external_single_structure
+ * @since Moodle 2.6
+ */
+ public static function get_user_mappings_returns() {
+ return new external_single_structure(
+ array(
+ 'assignments' => new external_multiple_structure(self::assign_user_mappings(), 'list of assign user mapping data'),
+ 'warnings' => new external_warnings('item is always \'assignment\'',
+ 'when errorcode is 3 then itemid is an assignment id. When errorcode is 1, itemid is a course module id',
+ 'errorcode can be 3 (no user mappings found) or 1 (no permission to get user mappings)')
+ )
+ );
+ }
+
}
Oops, something went wrong.

0 comments on commit 07df8c3

Please sign in to comment.