Skip to content

Commit

Permalink
MDL-32941 Implement core_course_update_categories, add webform and un…
Browse files Browse the repository at this point in the history
…ittests

Conflicts:

	admin/webservice/testclient_forms.php
	course/externallib.php
	lib/db/services.php
  • Loading branch information
fabiomsouto authored and mouneyrac committed May 17, 2012
1 parent 6c6ec1d commit f2229c6
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 0 deletions.
89 changes: 89 additions & 0 deletions admin/webservice/testclient_forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,3 +878,92 @@ public function get_params() {
return $params;
}
}

/**
* Form class for create_categories() web service function test.
*/
class core_course_update_categories_form extends moodleform {
/**
* The form definition.
*/
public function definition() {
global $CFG;

$mform = $this->_form;

$mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));

// Note: these values are intentionally PARAM_RAW - we want users to test any rubbish as parameters.
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->addElement('text', 'wspassword', 'wspassword');
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
}

$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_SAFEDIR);
$mform->addElement('text', 'id[0]', 'id[0]');
$mform->addElement('text', 'name[0]', 'name[0]');
$mform->addElement('text', 'parent[0]', 'parent[0]');
$mform->addElement('text', 'idnumber[0]', 'idnumber[0]');
$mform->addElement('text', 'description[0]', 'description[0]');
$mform->addElement('text', 'id[1]', 'id[1]');
$mform->addElement('text', 'name[1]', 'name[1]');
$mform->addElement('text', 'parent[1]', 'parent[1]');
$mform->addElement('text', 'idnumber[1]', 'idnumber[1]');
$mform->addElement('text', 'description[1]', 'description[1]');

$mform->addElement('hidden', 'function');
$mform->setType('function', PARAM_SAFEDIR);

$mform->addElement('hidden', 'protocol');
$mform->setType('protocol', PARAM_SAFEDIR);

$this->add_action_buttons(true, get_string('execute', 'webservice'));
}

/**
* Get the parameters that the user submitted using the form.
* @return array|null
*/
public function get_params() {
if (!$data = $this->get_data()) {
return null;
}
// Remove unused from form data.
unset($data->submitbutton);
unset($data->protocol);
unset($data->function);
unset($data->wsusername);
unset($data->wspassword);
unset($data->token);
unset($data->authmethod);

$params = array();
$params['categories'] = array();
for ($i=0; $i<10; $i++) {

if (empty($data->id[$i])) {
continue;
}
$attrs = array();
$attrs['id'] = $data->id[$i];
if (!empty($data->name[$i])) {
$attrs['name'] = $data->name[$i];
}
if (!empty($data->parent[$i])) {
$attrs['parent'] = $data->parent[$i];
}
if (!empty($data->idnumber[$i])) {
$attrs['idnumber'] = $data->idnumber[$i];
}
if (!empty($data->description[$i])) {
$attrs['description'] = $data->description[$i];
}
$params['categories'][] = $attrs;
}
return $params;
}
}
94 changes: 94 additions & 0 deletions course/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,100 @@ public static function delete_categories($categories) {
public static function delete_categories_returns() {
return null;
}

/**
* Returns description of method parameters
* @return external_function_parameters
* @since Moodle 2.3
*/
public static function update_categories_parameters() {
return new external_function_parameters(
array(
'categories' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'course id'),
'name' => new external_value(PARAM_TEXT, 'category name', VALUE_OPTIONAL),
'idnumber' => new external_value(PARAM_RAW, 'category id number', VALUE_OPTIONAL),
'parent' => new external_value(PARAM_INT, 'parent category id', VALUE_OPTIONAL),
'description' => new external_value(PARAM_RAW, 'category description', VALUE_OPTIONAL),
'theme' => new external_value(PARAM_THEME,
'the category theme. This option must be enabled on moodle', VALUE_OPTIONAL),
)
)
)
)
);
}

/**
* Update categories
* @param array $categories The list of categories to update
* @return null
* @since Moodle 2.3
*/
public static function update_categories($categories) {
global $CFG, $DB;
require_once($CFG->dirroot . "/course/lib.php");

// Validate parameters.
$params = self::validate_parameters(self::update_categories_parameters(), array('categories' => $categories));

$transaction = $DB->start_delegated_transaction();

foreach ($params['categories'] as $cat) {
if (!$category = $DB->get_record('course_categories', array('id' => $cat['id']))) {
throw new moodle_exception('unknowcategory');
}

$categorycontext = context_coursecat::instance($cat['id']);
self::validate_context($categorycontext);
require_capability('moodle/category:manage', $categorycontext);

if (!empty($cat['name'])) {
if (textlib::strlen($cat['name'])>30) {
throw new moodle_exception('categorytoolong');
}
$category->name = $cat['name'];
}
if (!empty($cat['idnumber'])) {
if (textlib::strlen($cat['idnumber'])>100) {
throw new moodle_exception('idnumbertoolong');
}
$category->idnumber = $cat['idnumber'];
}
if (!empty($cat['description'])) {
$category->description = $cat['description'];
$category->descriptionformat = FORMAT_HTML;
}
if (!empty($cat['theme'])) {
$category->theme = $cat['theme'];
}
if (!empty($cat['parent']) && ($category->parent != $cat['parent'])) {
// First check if parent exists.
if (!$parent_cat = $DB->get_record('course_categories', array('id' => $cat['parent']))) {
throw new moodle_exception('unknowcategory');
}
// Then check if we have capability.
self::validate_context(get_category_or_system_context((int)$cat['parent']));
require_capability('moodle/category:manage', get_category_or_system_context((int)$cat['parent']));
// Finally move the category.
move_category($category, $parent_cat);
$category->parent = $cat['parent'];
}
$DB->update_record('course_categories', $category);
}

$transaction->allow_commit();
}

/**
* Returns description of method result value
* @return external_description
*/
public static function update_categories_returns() {
return null;
}
}

/**
Expand Down
9 changes: 9 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,15 @@
'capabilities'=> 'moodle/category:viewhiddencategories',
),

'core_course_update_categories' => array(
'classname' => 'core_course_external',
'methodname' => 'update_categories',
'classpath' => 'course/externallib.php',
'description' => 'Update categories',
'type' => 'write',
'capabilities'=> 'moodle:category/manage',
),

'moodle_course_get_courses' => array(
'classname' => 'core_course_external',
'methodname' => 'get_courses',
Expand Down

0 comments on commit f2229c6

Please sign in to comment.