MDL-32941 Create service definition and implementation for

core_course_create_categories()
This commit is contained in:
Fábio Souto 2011-12-29 14:07:42 +00:00 committed by Jerome Mouneyrac
parent 71d7bc34a8
commit 479a5db197
2 changed files with 96 additions and 0 deletions

View File

@ -418,6 +418,93 @@ class core_course_external extends external_api {
);
}
/**
* Create categories
* @param array $options
* @return array
*/
public static function create_categories($options) {
global $CFG, $DB;
require_once($CFG->dirroot . "/course/lib.php");
$params = self::validate_parameters(self::create_categories_parameters(),
array('categories' => $options));
$createdcategories = array();
foreach($params['categories'] as $category) {
if ($category['parent']) {
if (!$DB->record_exists('course_categories', array('id' => $category['parent'])))
throw new moodle_exception(get_string('unknowncategory'), 'webservice', null);
$context = context_coursecat::instance($category['parent']);
}
else {
$context = context_system::instance();
}
self::validate_context($context);
$newcategory = new stdClass();
$newcategory->name = $category['name'];
$newcategory->idnumber = $category['idnumber'];
$newcategory->description = $category['description'];
$newcategory->parent = $category['parent'];
$newcategory->sortorder = 999;
//not sure if this is correct, but since text editor was not used, assume defaults
$newcategory->descriptionformat = 1;
if (isset($category['theme']) and !empty($CFG->allowcategorythemes)) {
$newcategory->theme = $category['theme'];
}
$newcategory->id = $DB->insert_record('course_categories', $newcategory);
$newcategory->context = context_coursecat::instance($newcategory->id);
mark_context_dirty($newcategory->context->path);
//populate special fields
fix_course_sortorder();
$createdcategories[] = array('id' => $newcategory->id, 'name' => $newcategory->name);
}
return $createdcategories;
}
/**
* Returns description of method parameters
* @return external_function_parameters
*/
public static function create_categories_parameters() {
return new external_function_parameters(
array(
'categories' => new external_multiple_structure(
new external_single_structure(
array(
'name' => new external_value(PARAM_TEXT, 'new category name'),
'parent' => new external_value(PARAM_INT, 'the parent category inside which the new category will be created'),
'idnumber' => new external_value(PARAM_INT, 'the new category idnumber', VALUE_OPTIONAL),
'description' => new external_value(PARAM_TEXT, 'the new category description', VALUE_OPTIONAL),
'theme' => new external_value(PARAM_THEME,
'the new category theme. This option must be enabled on moodle', VALUE_OPTIONAL),
)
)
)
)
);
}
/**
* Returns description of method parameters
* @return external_function_parameters
*/
public static function create_categories_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'new category id'),
'name' => new external_value(PARAM_TEXT, 'new category name'),
)
)
);
}
/**
* Returns description of method parameters
*

View File

@ -431,6 +431,15 @@ $functions = array(
'capabilities'=> 'moodle/course:delete',
),
'core_course_create_categories' => array(
'classname' => 'core_course_external',
'methodname' => 'create_categories',
'classpath' => 'course/externallib.php',
'description' => 'Create course categories',
'type' => 'write',
'capabilities'=> 'moodle/category:manage',
),
// === message related functions ===
'moodle_message_send_instantmessages' => array(