mirror of
https://github.com/moodle/moodle.git
synced 2025-04-17 22:45:54 +02:00
MDL-55785 mod_glossary: New Web Service mod_glossary_add_entry
This commit is contained in:
parent
e6de11d7b7
commit
f0e1808fc4
@ -1395,4 +1395,136 @@ class mod_glossary_external extends external_api {
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the description of the external function parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
public static function add_entry_parameters() {
|
||||
return new external_function_parameters(array(
|
||||
'glossaryid' => new external_value(PARAM_INT, 'Glossary id'),
|
||||
'concept' => new external_value(PARAM_TEXT, 'Glossary concept'),
|
||||
'definition' => new external_value(PARAM_TEXT, 'Glossary concept definition'),
|
||||
'definitionformat' => new external_format_value('definition'),
|
||||
'options' => new external_multiple_structure (
|
||||
new external_single_structure(
|
||||
array(
|
||||
'name' => new external_value(PARAM_ALPHANUM,
|
||||
'The allowed keys (value format) are:
|
||||
inlineattachmentsid (int); the draft file area id for inline attachments
|
||||
attachmentsid (int); the draft file area id for attachments
|
||||
categories (comma separated int); comma separated category ids
|
||||
aliases (comma separated str); comma separated aliases
|
||||
usedynalink (bool); whether the entry should be automatically linked.
|
||||
casesensitive (bool); whether the entry is case sensitive.
|
||||
fullmatch (bool); whether to match whole words only.'),
|
||||
'value' => new external_value(PARAM_RAW, 'the value of the option (validated inside the function)')
|
||||
)
|
||||
), 'Optional settings', VALUE_DEFAULT, array()
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a new entry to a given glossary.
|
||||
*
|
||||
* @param int $glossaryid the glosary id
|
||||
* @param string $concept the glossary concept
|
||||
* @param string $definition the concept definition
|
||||
* @param int $definitionformat the concept definition format
|
||||
* @param array $options additional settings
|
||||
* @return array Containing entry and warnings.
|
||||
* @since Moodle 3.2
|
||||
* @throws moodle_exception
|
||||
* @throws invalid_parameter_exception
|
||||
*/
|
||||
public static function add_entry($glossaryid, $concept, $definition, $definitionformat, $options = array()) {
|
||||
global $CFG;
|
||||
|
||||
$params = self::validate_parameters(self::add_entry_parameters(), array(
|
||||
'glossaryid' => $glossaryid,
|
||||
'concept' => $concept,
|
||||
'definition' => $definition,
|
||||
'definitionformat' => $definitionformat,
|
||||
'options' => $options,
|
||||
));
|
||||
$warnings = array();
|
||||
|
||||
// Get and validate the glossary.
|
||||
list($glossary, $context, $course, $cm) = self::validate_glossary($params['glossaryid']);
|
||||
require_capability('mod/glossary:write', $context);
|
||||
|
||||
if (!$glossary->allowduplicatedentries) {
|
||||
if (glossary_concept_exists($glossary, $params['concept'])) {
|
||||
throw new moodle_exception('errconceptalreadyexists', 'glossary');
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare the entry object.
|
||||
$entry = new stdClass;
|
||||
$entry->id = null;
|
||||
$entry->aliases = '';
|
||||
$entry->usedynalink = $CFG->glossary_linkentries;
|
||||
$entry->casesensitive = $CFG->glossary_casesensitive;
|
||||
$entry->fullmatch = $CFG->glossary_fullmatch;
|
||||
$entry->concept = $params['concept'];
|
||||
$entry->definition_editor = array(
|
||||
'text' => $params['definition'],
|
||||
'format' => $params['definitionformat'],
|
||||
);
|
||||
// Options.
|
||||
foreach ($params['options'] as $option) {
|
||||
$name = trim($option['name']);
|
||||
switch ($name) {
|
||||
case 'inlineattachmentsid':
|
||||
$entry->definition_editor['itemid'] = clean_param($option['value'], PARAM_INT);
|
||||
break;
|
||||
case 'attachmentsid':
|
||||
$entry->attachment_filemanager = clean_param($option['value'], PARAM_INT);
|
||||
break;
|
||||
case 'categories':
|
||||
$entry->categories = clean_param($option['value'], PARAM_SEQUENCE);
|
||||
$entry->categories = explode(',', $entry->categories);
|
||||
break;
|
||||
case 'aliases':
|
||||
$entry->aliases = clean_param($option['value'], PARAM_NOTAGS);
|
||||
// Convert to the expected format.
|
||||
$entry->aliases = str_replace(",", "\n", $entry->aliases);
|
||||
break;
|
||||
case 'usedynalink':
|
||||
case 'casesensitive':
|
||||
case 'fullmatch':
|
||||
// Only allow if linking is enabled.
|
||||
if ($glossary->usedynalink) {
|
||||
$entry->{$name} = clean_param($option['value'], PARAM_BOOL);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
|
||||
}
|
||||
}
|
||||
|
||||
$entry = glossary_edit_entry($entry, $course, $cm, $glossary, $context);
|
||||
|
||||
return array(
|
||||
'entryid' => $entry->id,
|
||||
'warnings' => $warnings
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the description of the external function return value.
|
||||
*
|
||||
* @return external_description
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
public static function add_entry_returns() {
|
||||
return new external_single_structure(array(
|
||||
'entryid' => new external_value(PARAM_INT, 'New glossary entry ID'),
|
||||
'warnings' => new external_warnings()
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -153,4 +153,13 @@ $functions = array(
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
|
||||
),
|
||||
|
||||
'mod_glossary_add_entry' => array(
|
||||
'classname' => 'mod_glossary_external',
|
||||
'methodname' => 'add_entry',
|
||||
'description' => 'Add a new entry to a given glossary',
|
||||
'type' => 'write',
|
||||
'capabilities' => 'mod/glossary:write',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
|
||||
),
|
||||
|
||||
);
|
||||
|
@ -1094,4 +1094,142 @@ class mod_glossary_external_testcase extends externallib_advanced_testcase {
|
||||
$this->assertEquals($e3->id, $return['entry']['id']);
|
||||
}
|
||||
|
||||
public function test_add_entry_without_optional_settings() {
|
||||
global $CFG, $DB;
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
|
||||
|
||||
$this->setAdminUser();
|
||||
$concept = 'A concept';
|
||||
$definition = 'A definition';
|
||||
$return = mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML);
|
||||
$return = external_api::clean_returnvalue(mod_glossary_external::add_entry_returns(), $return);
|
||||
|
||||
// Get entry from DB.
|
||||
$entry = $DB->get_record('glossary_entries', array('id' => $return['entryid']));
|
||||
|
||||
$this->assertEquals($concept, $entry->concept);
|
||||
$this->assertEquals($definition, $entry->definition);
|
||||
$this->assertEquals($CFG->glossary_linkentries, $entry->usedynalink);
|
||||
$this->assertEquals($CFG->glossary_casesensitive, $entry->casesensitive);
|
||||
$this->assertEquals($CFG->glossary_fullmatch, $entry->fullmatch);
|
||||
$this->assertEmpty($DB->get_records('glossary_alias', array('entryid' => $return['entryid'])));
|
||||
$this->assertEmpty($DB->get_records('glossary_entries_categories', array('entryid' => $return['entryid'])));
|
||||
}
|
||||
|
||||
public function test_add_entry_with_aliases() {
|
||||
global $DB;
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
|
||||
|
||||
$this->setAdminUser();
|
||||
$concept = 'A concept';
|
||||
$definition = 'A definition';
|
||||
$paramaliases = 'abc, def, gez';
|
||||
$options = array(
|
||||
array(
|
||||
'name' => 'aliases',
|
||||
'value' => $paramaliases,
|
||||
)
|
||||
);
|
||||
$return = mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML, $options);
|
||||
$return = external_api::clean_returnvalue(mod_glossary_external::add_entry_returns(), $return);
|
||||
|
||||
$aliases = $DB->get_records('glossary_alias', array('entryid' => $return['entryid']));
|
||||
$this->assertCount(3, $aliases);
|
||||
foreach ($aliases as $alias) {
|
||||
$this->assertContains($alias->alias, $paramaliases);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_add_entry_in_categories() {
|
||||
global $DB;
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
|
||||
$gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
|
||||
$cat1 = $gg->create_category($glossary);
|
||||
$cat2 = $gg->create_category($glossary);
|
||||
|
||||
$this->setAdminUser();
|
||||
$concept = 'A concept';
|
||||
$definition = 'A definition';
|
||||
$paramcategories = "$cat1->id, $cat2->id";
|
||||
$options = array(
|
||||
array(
|
||||
'name' => 'categories',
|
||||
'value' => $paramcategories,
|
||||
)
|
||||
);
|
||||
$return = mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML, $options);
|
||||
$return = external_api::clean_returnvalue(mod_glossary_external::add_entry_returns(), $return);
|
||||
|
||||
$categories = $DB->get_records('glossary_entries_categories', array('entryid' => $return['entryid']));
|
||||
$this->assertCount(2, $categories);
|
||||
foreach ($categories as $category) {
|
||||
$this->assertContains($category->categoryid, $paramcategories);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_add_entry_with_attachments() {
|
||||
global $DB, $USER;
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
|
||||
$context = context_module::instance($glossary->cmid);
|
||||
|
||||
$this->setAdminUser();
|
||||
$concept = 'A concept';
|
||||
$definition = 'A definition';
|
||||
|
||||
// Draft files.
|
||||
$draftidinlineattach = file_get_unused_draft_itemid();
|
||||
$draftidattach = file_get_unused_draft_itemid();
|
||||
$usercontext = context_user::instance($USER->id);
|
||||
$filerecordinline = array(
|
||||
'contextid' => $usercontext->id,
|
||||
'component' => 'user',
|
||||
'filearea' => 'draft',
|
||||
'itemid' => $draftidinlineattach,
|
||||
'filepath' => '/',
|
||||
'filename' => 'shouldbeanimage.txt',
|
||||
);
|
||||
$fs = get_file_storage();
|
||||
|
||||
// Create a file in a draft area for regular attachments.
|
||||
$filerecordattach = $filerecordinline;
|
||||
$attachfilename = 'attachment.txt';
|
||||
$filerecordattach['filename'] = $attachfilename;
|
||||
$filerecordattach['itemid'] = $draftidattach;
|
||||
$fs->create_file_from_string($filerecordinline, 'image contents (not really)');
|
||||
$fs->create_file_from_string($filerecordattach, 'simple text attachment');
|
||||
|
||||
$options = array(
|
||||
array(
|
||||
'name' => 'inlineattachmentsid',
|
||||
'value' => $draftidinlineattach,
|
||||
),
|
||||
array(
|
||||
'name' => 'attachmentsid',
|
||||
'value' => $draftidattach,
|
||||
)
|
||||
);
|
||||
$return = mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML, $options);
|
||||
$return = external_api::clean_returnvalue(mod_glossary_external::add_entry_returns(), $return);
|
||||
|
||||
$editorfiles = external_util::get_area_files($context->id, 'mod_glossary', 'entry', $return['entryid']);
|
||||
$attachmentfiles = external_util::get_area_files($context->id, 'mod_glossary', 'attachment', $return['entryid']);
|
||||
|
||||
$this->assertCount(1, $editorfiles);
|
||||
$this->assertCount(1, $attachmentfiles);
|
||||
|
||||
$this->assertEquals('shouldbeanimage.txt', $editorfiles[0]['filename']);
|
||||
$this->assertEquals('attachment.txt', $attachmentfiles[0]['filename']);
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$plugin->version = 2016052300; // The current module version (Date: YYYYMMDDXX)
|
||||
$plugin->version = 2016052301; // The current module version (Date: YYYYMMDDXX)
|
||||
$plugin->requires = 2016051900; // Requires this Moodle version
|
||||
$plugin->component = 'mod_glossary'; // Full name of the plugin (used for diagnostics)
|
||||
$plugin->cron = 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user