/** * Creates or updates a glossary entry * * @param stdClass $entry entry data * @param stdClass $course course object * @param stdClass $cm course module object * @param stdClass $glossary glossary object * @param stdClass $context context object * @return stdClass the complete new or updated entry * @since Moodle 3.2 */ function glossary_edit_entry($entry, $course, $cm, $glossary, $context) { global $DB, $USER; list($definitionoptions, $attachmentoptions) = glossary_get_editor_and_attachment_options($course, $context, $entry); $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. if (!empty($entry->definition_editor)) { $entry = file_postupdate_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry', $entry->id); } if (!empty($entry->attachment_filemanager)) { $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); } return $entry; }
//prepare extra data if ($aliases = $DB->get_records_menu("glossary_alias", array("entryid" => $id), '', 'id, alias')) { $entry->aliases = implode("\n", $aliases) . "\n"; } if ($categoriesarr = $DB->get_records_menu("glossary_entries_categories", array('entryid' => $id), '', 'id, categoryid')) { // TODO: this fetches cats from both main and secondary glossary :-( $entry->categories = array_values($categoriesarr); } } else { // new entry require_capability('mod/glossary:write', $context); // note: guest user does not have any write capability $entry = new stdClass(); $entry->id = null; } 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}");