/** * 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); }
} 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); $entry->cmid = $cm->id; // create form and set initial data $mform = new mod_glossary_entry_form(null, array('current' => $entry, 'cm' => $cm, 'glossary' => $glossary, 'definitionoptions' => $definitionoptions, 'attachmentoptions' => $attachmentoptions)); if ($mform->is_cancelled()) { if ($id) { redirect("view.php?id={$cm->id}&mode=entry&hook={$id}"); } else { redirect("view.php?id={$cm->id}"); } } else { if ($entry = $mform->get_data()) { $entry = glossary_edit_entry($entry, $course, $cm, $glossary, $context); redirect("view.php?id={$cm->id}&mode=entry&hook={$entry->id}"); } } if (!empty($id)) { $PAGE->navbar->add(get_string('edit')); } $PAGE->set_title($glossary->name); $PAGE->set_heading($course->fullname); echo $OUTPUT->header(); echo $OUTPUT->heading(format_string($glossary->name), 2); if ($glossary->intro) { echo $OUTPUT->box(format_module_intro('glossary', $glossary, $cm->id), 'generalbox', 'intro'); } $mform->display(); echo $OUTPUT->footer();