/** * 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); }
function validation($data, $files) { global $CFG, $USER, $DB; $errors = parent::validation($data, $files); $glossary = $this->_customdata['glossary']; $cm = $this->_customdata['cm']; $context = context_module::instance($cm->id); $id = (int) $data['id']; $data['concept'] = trim($data['concept']); if ($id) { //We are updating an entry, so we compare current session user with //existing entry user to avoid some potential problems if secureforms=off //Perhaps too much security? Anyway thanks to skodak (Bug 1823) $old = $DB->get_record('glossary_entries', array('id' => $id)); $ineditperiod = time() - $old->timecreated < $CFG->maxeditingtime || $glossary->editalways; if (!$ineditperiod || $USER->id != $old->userid and !has_capability('mod/glossary:manageentries', $context)) { if ($USER->id != $old->userid) { $errors['concept'] = get_string('errcannoteditothers', 'glossary'); } elseif (!$ineditperiod) { $errors['concept'] = get_string('erredittimeexpired', 'glossary'); } } if (!$glossary->allowduplicatedentries) { if ($DB->record_exists_select('glossary_entries', 'glossaryid = :glossaryid AND LOWER(concept) = :concept AND id != :id', array('glossaryid' => $glossary->id, 'concept' => core_text::strtolower($data['concept']), 'id' => $id))) { $errors['concept'] = get_string('errconceptalreadyexists', 'glossary'); } } } else { if (!$glossary->allowduplicatedentries) { if (glossary_concept_exists($glossary, $data['concept'])) { $errors['concept'] = get_string('errconceptalreadyexists', 'glossary'); } } } return $errors; }