MDL-32662 Added assign_grouping method
authorJuan Leyva <juanleyvadelgado@gmail.com>
Mon, 30 Apr 2012 14:55:28 +0000 (16:55 +0200)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Thu, 10 May 2012 09:40:51 +0000 (11:40 +0200)
group/externallib.php

index 5ca5935..ba34672 100644 (file)
@@ -926,6 +926,76 @@ class core_group_external extends external_api {
         return null;
     }
 
+    /**
+     * Returns description of method parameters
+     * @return external_function_parameters
+     */
+    public static function assign_grouping_parameters() {
+        return new external_function_parameters(
+            array(
+                'assignments'=> new external_multiple_structure(
+                    new external_single_structure(
+                        array(
+                            'groupingid' => new external_value(PARAM_INT, 'grouping record id'),
+                            'groupid' => new external_value(PARAM_INT, 'group record id'),
+                        )
+                    )
+                )
+            )
+        );
+    }
+
+    /**
+     * Assign a group to a grouping
+     * @param array $assignments of arrays with keys groupid, groupingid
+     * @return void
+     */
+    public static function assign_grouping($assignments) {
+        global $CFG, $DB;
+        require_once("$CFG->dirroot/group/lib.php");
+
+        $params = self::validate_parameters(self::assign_grouping_parameters(), array('assignments'=>$assignments));
+
+        $transaction = $DB->start_delegated_transaction();
+        foreach ($params['assignments'] as $assignment) {
+            // Validate params.
+            $groupingid = $assignment['groupingid'];
+            $groupid = $assignment['groupid'];
+
+            $grouping = groups_get_grouping($groupingid, 'id, courseid', MUST_EXIST);
+            $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST);
+
+            if ($DB->record_exists('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid))) {
+                // Continue silently if the group is yet assigned to the grouping.
+                continue;
+            }
+
+            // now security checks
+            $context = context_course::instance($grouping->courseid);
+            try {
+                self::validate_context($context);
+            } catch (Exception $e) {
+                $exceptionparam = new stdClass();
+                $exceptionparam->message = $e->getMessage();
+                $exceptionparam->courseid = $group->courseid;
+                throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
+            }
+            require_capability('moodle/course:managegroups', $context);
+
+            groups_assign_grouping($groupingid, $groupid);
+        }
+
+        $transaction->allow_commit();
+    }
+
+   /**
+     * Returns description of method result value
+     * @return null
+     */
+    public static function assign_grouping_returns() {
+        return null;
+    }
+
 }
 
 /**