From e6de11d7b73669d818ee22c42713e9365967e7dc Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Tue, 6 Sep 2016 17:35:43 +0100 Subject: [PATCH] MDL-55785 mod_glossary: Move edit.php code to new lib function Most of the code that was in mod/glossary/edit.php has been moved to mod/glossary/lib.php:glossary_edit_entry --- mod/glossary/edit.php | 118 +------------------------ mod/glossary/edit_form.php | 5 +- mod/glossary/lib.php | 171 +++++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+), 120 deletions(-) diff --git a/mod/glossary/edit.php b/mod/glossary/edit.php index ab5b179f93e..6a7129cdd33 100644 --- a/mod/glossary/edit.php +++ b/mod/glossary/edit.php @@ -63,12 +63,7 @@ if ($id) { // if entry is specified $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); @@ -87,116 +82,7 @@ if ($mform->is_cancelled()){ } } 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"); } diff --git a/mod/glossary/edit_form.php b/mod/glossary/edit_form.php index a2206910dd3..04ee8f52315 100644 --- a/mod/glossary/edit_form.php +++ b/mod/glossary/edit_form.php @@ -126,10 +126,7 @@ class mod_glossary_entry_form extends moodleform { } 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'); } } diff --git a/mod/glossary/lib.php b/mod/glossary/lib.php index 7b5a2b96658..562449d03a2 100644 --- a/mod/glossary/lib.php +++ b/mod/glossary/lib.php @@ -3929,3 +3929,174 @@ function glossary_can_view_entry($entry, $cminfo) { return true; } + +/** + * Check if a concept exists in a glossary. + * + * @param stdClass $glossary glossary object + * @param string $concept the concept to check + * @return bool true if exists + * @since Moodle 3.2 + */ +function glossary_concept_exists($glossary, $concept) { + global $DB; + + return $DB->record_exists_select('glossary_entries', 'glossaryid = :glossaryid AND LOWER(concept) = :concept', + array( + 'glossaryid' => $glossary->id, + 'concept' => core_text::strtolower($concept) + ) + ); +} + +/** + * Return the editor and attachment options when editing a glossary entry + * + * @param stdClass $course course object + * @param stdClass $context context object + * @param stdClass $entry entry object + * @return array array containing the editor and attachment options + * @since Moodle 3.2 + */ +function glossary_get_editor_and_attachment_options($course, $context, $entry) { + $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); + return array($definitionoptions, $attachmentoptions); +} + +/** + * 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; +}