Skip to content

Commit

Permalink
Merge branch 'MDL-55785-master' of git://github.com/jleyva/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Sep 28, 2016
2 parents cf1d2cb + fad980b commit 7bc32eb
Show file tree
Hide file tree
Showing 7 changed files with 461 additions and 122 deletions.
136 changes: 135 additions & 1 deletion mod/glossary/classes/external.php
Expand Up @@ -226,6 +226,7 @@ public static function get_glossaries_by_courses($courseids = array()) {
$modes[$glossary->displayformat] = self::get_browse_modes_from_display_format($glossary->displayformat);
}
$glossary->browsemodes = $modes[$glossary->displayformat];
$glossary->canaddentry = has_capability('mod/glossary:write', $context) ? 1 : 0;
}
}

Expand Down Expand Up @@ -292,7 +293,8 @@ public static function get_glossaries_by_courses_returns() {
'groupingid' => new external_value(PARAM_INT, 'Grouping ID'),
'browsemodes' => new external_multiple_structure(
new external_value(PARAM_ALPHA, 'Modes of browsing allowed')
)
),
'canaddentry' => new external_value(PARAM_INT, 'Whether the user can add a new entry', VALUE_OPTIONAL),
), 'Glossaries')
),
'warnings' => new external_warnings())
Expand Down Expand Up @@ -1395,4 +1397,136 @@ public static function get_entry_by_id_returns() {
));
}

/**
* 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()
));
}

}
9 changes: 9 additions & 0 deletions mod/glossary/db/services.php
Expand Up @@ -153,4 +153,13 @@
'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)
),

);
118 changes: 2 additions & 116 deletions mod/glossary/edit.php
Expand Up @@ -63,12 +63,7 @@
$entry->id = null;
}

$maxfiles = 99; // TODO: add some setting
$maxbytes = $course->maxbytes; // TODO: add some setting

$definitionoptions = array('trusttext'=>true, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes, 'context'=>$context,
'subdirs'=>file_area_contains_subdirs($context, 'mod_glossary', 'entry', $entry->id));
$attachmentoptions = array('subdirs'=>false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes);
list($definitionoptions, $attachmentoptions) = glossary_get_editor_and_attachment_options($course, $context, $entry);

$entry = file_prepare_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry', $entry->id);
$entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'mod_glossary', 'attachment', $entry->id);
Expand All @@ -87,116 +82,7 @@
}

} else if ($entry = $mform->get_data()) {
$timenow = time();

$categories = empty($entry->categories) ? array() : $entry->categories;
unset($entry->categories);
$aliases = trim($entry->aliases);
unset($entry->aliases);

if (empty($entry->id)) {
$entry->glossaryid = $glossary->id;
$entry->timecreated = $timenow;
$entry->userid = $USER->id;
$entry->timecreated = $timenow;
$entry->sourceglossaryid = 0;
$entry->teacherentry = has_capability('mod/glossary:manageentries', $context);
$isnewentry = true;
} else {
$isnewentry = false;
}

$entry->concept = trim($entry->concept);
$entry->definition = ''; // updated later
$entry->definitionformat = FORMAT_HTML; // updated later
$entry->definitiontrust = 0; // updated later
$entry->timemodified = $timenow;
$entry->approved = 0;
$entry->usedynalink = isset($entry->usedynalink) ? $entry->usedynalink : 0;
$entry->casesensitive = isset($entry->casesensitive) ? $entry->casesensitive : 0;
$entry->fullmatch = isset($entry->fullmatch) ? $entry->fullmatch : 0;

if ($glossary->defaultapproval or has_capability('mod/glossary:approve', $context)) {
$entry->approved = 1;
}

if ($isnewentry) {
// Add new entry.
$entry->id = $DB->insert_record('glossary_entries', $entry);
} else {
// Update existing entry.
$DB->update_record('glossary_entries', $entry);
}

// save and relink embedded images and save attachments
$entry = file_postupdate_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry', $entry->id);
$entry = file_postupdate_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'mod_glossary', 'attachment', $entry->id);

// store the updated value values
$DB->update_record('glossary_entries', $entry);

//refetch complete entry
$entry = $DB->get_record('glossary_entries', array('id'=>$entry->id));

// update entry categories
$DB->delete_records('glossary_entries_categories', array('entryid'=>$entry->id));
// TODO: this deletes cats from both both main and secondary glossary :-(
if (!empty($categories) and array_search(0, $categories) === false) {
foreach ($categories as $catid) {
$newcategory = new stdClass();
$newcategory->entryid = $entry->id;
$newcategory->categoryid = $catid;
$DB->insert_record('glossary_entries_categories', $newcategory, false);
}
}

// update aliases
$DB->delete_records('glossary_alias', array('entryid'=>$entry->id));
if ($aliases !== '') {
$aliases = explode("\n", $aliases);
foreach ($aliases as $alias) {
$alias = trim($alias);
if ($alias !== '') {
$newalias = new stdClass();
$newalias->entryid = $entry->id;
$newalias->alias = $alias;
$DB->insert_record('glossary_alias', $newalias, false);
}
}
}

// Trigger event and update completion (if entry was created).
$eventparams = array(
'context' => $context,
'objectid' => $entry->id,
'other' => array('concept' => $entry->concept)
);
if ($isnewentry) {
$event = \mod_glossary\event\entry_created::create($eventparams);
} else {
$event = \mod_glossary\event\entry_updated::create($eventparams);
}
$event->add_record_snapshot('glossary_entries', $entry);
$event->trigger();
if ($isnewentry) {
// Update completion state
$completion = new completion_info($course);
if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $glossary->completionentries && $entry->approved) {
$completion->update_state($cm, COMPLETION_COMPLETE);
}
}

// Reset caches.
if ($isnewentry) {
if ($entry->usedynalink and $entry->approved) {
\mod_glossary\local\concept_cache::reset_glossary($glossary);
}
} else {
// So many things may affect the linking, let's just purge the cache always on edit.
\mod_glossary\local\concept_cache::reset_glossary($glossary);
}


$entry = glossary_edit_entry($entry, $course, $cm, $glossary, $context);
redirect("view.php?id=$cm->id&mode=entry&hook=$entry->id");
}

Expand Down
5 changes: 1 addition & 4 deletions mod/glossary/edit_form.php
Expand Up @@ -126,10 +126,7 @@ function validation($data, $files) {

} else {
if (!$glossary->allowduplicatedentries) {
if ($DB->record_exists_select('glossary_entries',
'glossaryid = :glossaryid AND LOWER(concept) = :concept', array(
'glossaryid' => $glossary->id,
'concept' => core_text::strtolower($data['concept'])))) {
if (glossary_concept_exists($glossary, $data['concept'])) {
$errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
}
}
Expand Down

0 comments on commit 7bc32eb

Please sign in to comment.