/** * Given an ID of an instance of this module, * this function will permanently delete the instance * and any data that depends on it. * * @global object * @param int $id glossary id * @return bool success */ function glossary_delete_instance($id) { global $DB, $CFG; if (!$glossary = $DB->get_record('glossary', array('id'=>$id))) { return false; } if (!$cm = get_coursemodule_from_instance('glossary', $id)) { return false; } if (!$context = context_module::instance($cm->id, IGNORE_MISSING)) { return false; } $fs = get_file_storage(); if ($glossary->mainglossary) { // unexport entries $sql = "SELECT ge.id, ge.sourceglossaryid, cm.id AS sourcecmid FROM {glossary_entries} ge JOIN {modules} m ON m.name = 'glossary' JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = ge.sourceglossaryid) WHERE ge.glossaryid = ? AND ge.sourceglossaryid > 0"; if ($exported = $DB->get_records_sql($sql, array($id))) { foreach ($exported as $entry) { $entry->glossaryid = $entry->sourceglossaryid; $entry->sourceglossaryid = 0; $newcontext = context_module::instance($entry->sourcecmid); if ($oldfiles = $fs->get_area_files($context->id, 'mod_glossary', 'attachment', $entry->id)) { foreach ($oldfiles as $oldfile) { $file_record = new stdClass(); $file_record->contextid = $newcontext->id; $fs->create_file_from_storedfile($file_record, $oldfile); } $fs->delete_area_files($context->id, 'mod_glossary', 'attachment', $entry->id); $entry->attachment = '1'; } else { $entry->attachment = '0'; } $DB->update_record('glossary_entries', $entry); } } } else { // move exported entries to main glossary $sql = "UPDATE {glossary_entries} SET sourceglossaryid = 0 WHERE sourceglossaryid = ?"; $DB->execute($sql, array($id)); } // Delete any dependent records $entry_select = "SELECT id FROM {glossary_entries} WHERE glossaryid = ?"; $DB->delete_records_select('comments', "contextid=? AND commentarea=? AND itemid IN ($entry_select)", array($id, 'glossary_entry', $context->id)); $DB->delete_records_select('glossary_alias', "entryid IN ($entry_select)", array($id)); $category_select = "SELECT id FROM {glossary_categories} WHERE glossaryid = ?"; $DB->delete_records_select('glossary_entries_categories', "categoryid IN ($category_select)", array($id)); $DB->delete_records('glossary_categories', array('glossaryid'=>$id)); $DB->delete_records('glossary_entries', array('glossaryid'=>$id)); // delete all files $fs->delete_area_files($context->id); glossary_grade_item_delete($glossary); $DB->delete_records('glossary', array('id'=>$id)); // Reset caches. \mod_glossary\local\concept_cache::reset_glossary($glossary); return true; }
$context = context_module::instance($cm->id); require_capability('mod/glossary:approve', $context); if ($newstate != $entry->approved && confirm_sesskey()) { $newentry = new stdClass(); $newentry->id = $entry->id; $newentry->approved = $newstate; $newentry->timemodified = time(); // wee need this date here to speed up recent activity, TODO: use timestamp in approved field instead in 2.0 $DB->update_record("glossary_entries", $newentry); // Trigger event about entry approval/disapproval. $params = array('context' => $context, 'objectid' => $entry->id); if ($newstate) { $event = \mod_glossary\event\entry_approved::create($params); } else { $event = \mod_glossary\event\entry_disapproved::create($params); } $entry->approved = $newstate ? 1 : 0; $entry->timemodified = $newentry->timemodified; $event->add_record_snapshot('glossary_entries', $entry); $event->trigger(); // Update completion state $completion = new completion_info($course); if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $glossary->completionentries) { $completion->update_state($cm, COMPLETION_COMPLETE, $entry->userid); } // Reset caches. if ($entry->usedynalink) { \mod_glossary\local\concept_cache::reset_glossary($glossary); } } redirect("view.php?id={$cm->id}&mode={$mode}&hook={$hook}");
/** * 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; }