[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 3 require_once('../../config.php'); 4 require_once ('lib.php'); 5 require_once ('edit_form.php'); 6 7 $cmid = required_param('cmid', PARAM_INT); // Course Module ID 8 $id = optional_param('id', 0, PARAM_INT); // EntryID 9 10 if (!$cm = get_coursemodule_from_id('glossary', $cmid)) { 11 print_error('invalidcoursemodule'); 12 } 13 14 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) { 15 print_error('coursemisconf'); 16 } 17 18 require_login($course, false, $cm); 19 20 $context = context_module::instance($cm->id); 21 22 if (!$glossary = $DB->get_record('glossary', array('id'=>$cm->instance))) { 23 print_error('invalidid', 'glossary'); 24 } 25 26 $url = new moodle_url('/mod/glossary/edit.php', array('cmid'=>$cm->id)); 27 if (!empty($id)) { 28 $url->param('id', $id); 29 } 30 $PAGE->set_url($url); 31 32 if ($id) { // if entry is specified 33 if (isguestuser()) { 34 print_error('guestnoedit', 'glossary', "$CFG->wwwroot/mod/glossary/view.php?id=$cmid"); 35 } 36 37 if (!$entry = $DB->get_record('glossary_entries', array('id'=>$id, 'glossaryid'=>$glossary->id))) { 38 print_error('invalidentry'); 39 } 40 41 $ineditperiod = ((time() - $entry->timecreated < $CFG->maxeditingtime) || $glossary->editalways); 42 if (!has_capability('mod/glossary:manageentries', $context) and !($entry->userid == $USER->id and ($ineditperiod and has_capability('mod/glossary:write', $context)))) { 43 if ($USER->id != $entry->userid) { 44 print_error('errcannoteditothers', 'glossary', "view.php?id=$cm->id&mode=entry&hook=$id"); 45 } elseif (!$ineditperiod) { 46 print_error('erredittimeexpired', 'glossary', "view.php?id=$cm->id&mode=entry&hook=$id"); 47 } 48 } 49 50 //prepare extra data 51 if ($aliases = $DB->get_records_menu("glossary_alias", array("entryid"=>$id), '', 'id, alias')) { 52 $entry->aliases = implode("\n", $aliases) . "\n"; 53 } 54 if ($categoriesarr = $DB->get_records_menu("glossary_entries_categories", array('entryid'=>$id), '', 'id, categoryid')) { 55 // TODO: this fetches cats from both main and secondary glossary :-( 56 $entry->categories = array_values($categoriesarr); 57 } 58 59 } else { // new entry 60 require_capability('mod/glossary:write', $context); 61 // note: guest user does not have any write capability 62 $entry = new stdClass(); 63 $entry->id = null; 64 } 65 66 $maxfiles = 99; // TODO: add some setting 67 $maxbytes = $course->maxbytes; // TODO: add some setting 68 69 $definitionoptions = array('trusttext'=>true, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes, 'context'=>$context, 70 'subdirs'=>file_area_contains_subdirs($context, 'mod_glossary', 'entry', $entry->id)); 71 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes); 72 73 $entry = file_prepare_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry', $entry->id); 74 $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'mod_glossary', 'attachment', $entry->id); 75 76 $entry->cmid = $cm->id; 77 78 // create form and set initial data 79 $mform = new mod_glossary_entry_form(null, array('current'=>$entry, 'cm'=>$cm, 'glossary'=>$glossary, 80 'definitionoptions'=>$definitionoptions, 'attachmentoptions'=>$attachmentoptions)); 81 82 if ($mform->is_cancelled()){ 83 if ($id){ 84 redirect("view.php?id=$cm->id&mode=entry&hook=$id"); 85 } else { 86 redirect("view.php?id=$cm->id"); 87 } 88 89 } else if ($entry = $mform->get_data()) { 90 $timenow = time(); 91 92 $categories = empty($entry->categories) ? array() : $entry->categories; 93 unset($entry->categories); 94 $aliases = trim($entry->aliases); 95 unset($entry->aliases); 96 97 if (empty($entry->id)) { 98 $entry->glossaryid = $glossary->id; 99 $entry->timecreated = $timenow; 100 $entry->userid = $USER->id; 101 $entry->timecreated = $timenow; 102 $entry->sourceglossaryid = 0; 103 $entry->teacherentry = has_capability('mod/glossary:manageentries', $context); 104 $isnewentry = true; 105 } else { 106 $isnewentry = false; 107 } 108 109 $entry->concept = trim($entry->concept); 110 $entry->definition = ''; // updated later 111 $entry->definitionformat = FORMAT_HTML; // updated later 112 $entry->definitiontrust = 0; // updated later 113 $entry->timemodified = $timenow; 114 $entry->approved = 0; 115 $entry->usedynalink = isset($entry->usedynalink) ? $entry->usedynalink : 0; 116 $entry->casesensitive = isset($entry->casesensitive) ? $entry->casesensitive : 0; 117 $entry->fullmatch = isset($entry->fullmatch) ? $entry->fullmatch : 0; 118 119 if ($glossary->defaultapproval or has_capability('mod/glossary:approve', $context)) { 120 $entry->approved = 1; 121 } 122 123 if ($isnewentry) { 124 // Add new entry. 125 $entry->id = $DB->insert_record('glossary_entries', $entry); 126 } else { 127 // Update existing entry. 128 $DB->update_record('glossary_entries', $entry); 129 } 130 131 // save and relink embedded images and save attachments 132 $entry = file_postupdate_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry', $entry->id); 133 $entry = file_postupdate_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'mod_glossary', 'attachment', $entry->id); 134 135 // store the updated value values 136 $DB->update_record('glossary_entries', $entry); 137 138 //refetch complete entry 139 $entry = $DB->get_record('glossary_entries', array('id'=>$entry->id)); 140 141 // update entry categories 142 $DB->delete_records('glossary_entries_categories', array('entryid'=>$entry->id)); 143 // TODO: this deletes cats from both both main and secondary glossary :-( 144 if (!empty($categories) and array_search(0, $categories) === false) { 145 foreach ($categories as $catid) { 146 $newcategory = new stdClass(); 147 $newcategory->entryid = $entry->id; 148 $newcategory->categoryid = $catid; 149 $DB->insert_record('glossary_entries_categories', $newcategory, false); 150 } 151 } 152 153 // update aliases 154 $DB->delete_records('glossary_alias', array('entryid'=>$entry->id)); 155 if ($aliases !== '') { 156 $aliases = explode("\n", $aliases); 157 foreach ($aliases as $alias) { 158 $alias = trim($alias); 159 if ($alias !== '') { 160 $newalias = new stdClass(); 161 $newalias->entryid = $entry->id; 162 $newalias->alias = $alias; 163 $DB->insert_record('glossary_alias', $newalias, false); 164 } 165 } 166 } 167 168 // Trigger event and update completion (if entry was created). 169 $eventparams = array( 170 'context' => $context, 171 'objectid' => $entry->id, 172 'other' => array('concept' => $entry->concept) 173 ); 174 if ($isnewentry) { 175 $event = \mod_glossary\event\entry_created::create($eventparams); 176 } else { 177 $event = \mod_glossary\event\entry_updated::create($eventparams); 178 } 179 $event->add_record_snapshot('glossary_entries', $entry); 180 $event->trigger(); 181 if ($isnewentry) { 182 // Update completion state 183 $completion = new completion_info($course); 184 if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $glossary->completionentries && $entry->approved) { 185 $completion->update_state($cm, COMPLETION_COMPLETE); 186 } 187 } 188 189 // Reset caches. 190 if ($isnewentry) { 191 if ($entry->usedynalink and $entry->approved) { 192 \mod_glossary\local\concept_cache::reset_glossary($glossary); 193 } 194 } else { 195 // So many things may affect the linking, let's just purge the cache always on edit. 196 \mod_glossary\local\concept_cache::reset_glossary($glossary); 197 } 198 199 200 redirect("view.php?id=$cm->id&mode=entry&hook=$entry->id"); 201 } 202 203 if (!empty($id)) { 204 $PAGE->navbar->add(get_string('edit')); 205 } 206 207 $PAGE->set_title($glossary->name); 208 $PAGE->set_heading($course->fullname); 209 echo $OUTPUT->header(); 210 echo $OUTPUT->heading(format_string($glossary->name), 2); 211 if ($glossary->intro) { 212 echo $OUTPUT->box(format_module_intro('glossary', $glossary, $cm->id), 'generalbox', 'intro'); 213 } 214 215 $mform->display(); 216 217 echo $OUTPUT->footer(); 218
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |