MDL-13130 webservices: Implementation of core_course_delete_courses
authorJuan Leyva <juanleyvadelgado@gmail.com>
Wed, 18 Apr 2012 08:08:24 +0000 (10:08 +0200)
committerDan Poltawski <dan@moodle.com>
Tue, 1 May 2012 07:23:50 +0000 (15:23 +0800)
course/externallib.php
lib/db/services.php

index 8fd2e40..fef3647 100644 (file)
@@ -593,6 +593,59 @@ class core_course_external extends external_api {
         );
     }
 
+    /**
+     * Returns description of method parameters
+     * @return external_function_parameters
+     */
+    public static function delete_courses_parameters() {
+        return new external_function_parameters(
+            array(
+                'courseids' => new external_multiple_structure(new external_value(PARAM_INT, 'course ID')),
+            )
+        );
+    }
+
+    /**
+     * Delete courses
+     * @param array $courseids A list of course ids
+     */
+    public static function delete_courses($courseids) {
+        global $CFG, $DB;
+        require_once($CFG->dirroot."/course/lib.php");
+
+        // Parameter validation.
+        $params = self::validate_parameters(self::delete_courses_parameters(), array('courseids'=>$courseids));
+
+        $transaction = $DB->start_delegated_transaction();
+
+        foreach ($params['courseids'] as $courseid) {
+            $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
+
+            // Check if the context is valid.
+            $coursecontext = context_course::instance($course->id);
+            self::validate_context($coursecontext);
+
+            // Check if the current user has enought permissions.
+            if (!can_delete_course($courseid)) {
+                throw new moodle_exception('cannotdeletecategorycourse', 'error', '', format_string($course->fullname)." (id: $courseid)");
+            }
+
+            delete_course($course, false);
+        }
+
+        $transaction->allow_commit();
+
+        return null;
+    }
+
+    /**
+     * Returns description of method result value
+     * @return external_description
+     */
+    public static function delete_courses_returns() {
+        return null;
+    }
+
 }
 
 /**
index cdb6a57..476b4bb 100644 (file)
@@ -422,6 +422,15 @@ $functions = array(
         'capabilities'=> 'moodle/course:update,moodle/course:viewhiddencourses',
     ),
 
+    'core_course_delete_courses' => array(
+        'classname'   => 'core_course_external',
+        'methodname'  => 'delete_courses',
+        'classpath'   => 'course/externallib.php',
+        'description' => 'Deletes all specified courses',
+        'type'        => 'write',
+        'capabilities'=> 'moodle/course:delete',
+    ),
+
     // === message related functions ===
 
     'moodle_message_send_instantmessages' => array(