Merge branch 'MDL-30198-m22' of git://github.com/ankitagarwal/moodle into MOODLE_22_S...
[moodle.git] / mod / glossary / edit.php
blobd69ae54e37ccda05b016dbcb63d60a1c924dd957
1 <?php
3 require_once('../../config.php');
4 require_once('lib.php');
5 require_once('edit_form.php');
7 $cmid = required_param('cmid', PARAM_INT); // Course Module ID
8 $id = optional_param('id', 0, PARAM_INT); // EntryID
10 if (!$cm = get_coursemodule_from_id('glossary', $cmid)) {
11 print_error('invalidcoursemodule');
14 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
15 print_error('coursemisconf');
18 require_login($course, false, $cm);
20 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
22 if (!$glossary = $DB->get_record('glossary', array('id'=>$cm->instance))) {
23 print_error('invalidid', 'glossary');
26 $url = new moodle_url('/mod/glossary/edit.php', array('cmid'=>$cm->id));
27 if (!empty($id)) {
28 $url->param('id', $id);
30 $PAGE->set_url($url);
32 if ($id) { // if entry is specified
33 if (isguestuser()) {
34 print_error('guestnoedit', 'glossary', "$CFG->wwwroot/mod/glossary/view.php?id=$cmid");
37 if (!$entry = $DB->get_record('glossary_entries', array('id'=>$id, 'glossaryid'=>$glossary->id))) {
38 print_error('invalidentry');
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 != $fromdb->userid) {
44 print_error('errcannoteditothers', 'glossary', "view.php?id=$cm->id&amp;mode=entry&amp;hook=$id");
45 } elseif (!$ineditperiod) {
46 print_error('erredittimeexpired', 'glossary', "view.php?id=$cm->id&amp;mode=entry&amp;hook=$id");
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";
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);
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;
66 $maxfiles = 99; // TODO: add some setting
67 $maxbytes = $course->maxbytes; // TODO: add some setting
69 $definitionoptions = array('trusttext'=>true, 'subdirs'=>false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes, 'context'=>$context);
70 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes);
72 $entry = file_prepare_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry', $entry->id);
73 $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'mod_glossary', 'attachment', $entry->id);
75 $entry->cmid = $cm->id;
77 // create form and set initial data
78 $mform = new mod_glossary_entry_form(null, array('current'=>$entry, 'cm'=>$cm, 'glossary'=>$glossary,
79 'definitionoptions'=>$definitionoptions, 'attachmentoptions'=>$attachmentoptions));
81 if ($mform->is_cancelled()){
82 if ($id){
83 redirect("view.php?id=$cm->id&mode=entry&hook=$id");
84 } else {
85 redirect("view.php?id=$cm->id");
88 } else if ($entry = $mform->get_data()) {
89 $timenow = time();
91 $categories = empty($entry->categories) ? array() : $entry->categories;
92 unset($entry->categories);
93 $aliases = trim($entry->aliases);
94 unset($entry->aliases);
96 if (empty($entry->id)) {
97 $entry->glossaryid = $glossary->id;
98 $entry->timecreated = $timenow;
99 $entry->userid = $USER->id;
100 $entry->timecreated = $timenow;
101 $entry->sourceglossaryid = 0;
102 $entry->teacherentry = has_capability('mod/glossary:manageentries', $context);
105 $entry->concept = trim($entry->concept);
106 $entry->definition = ''; // updated later
107 $entry->definitionformat = FORMAT_HTML; // updated later
108 $entry->definitiontrust = 0; // updated later
109 $entry->timemodified = $timenow;
110 $entry->approved = 0;
111 $entry->usedynalink = isset($entry->usedynalink) ? $entry->usedynalink : 0;
112 $entry->casesensitive = isset($entry->casesensitive) ? $entry->casesensitive : 0;
113 $entry->fullmatch = isset($entry->fullmatch) ? $entry->fullmatch : 0;
115 if ($glossary->defaultapproval or has_capability('mod/glossary:approve', $context)) {
116 $entry->approved = 1;
119 if (empty($entry->id)) {
120 //new entry
121 $entry->id = $DB->insert_record('glossary_entries', $entry);
123 // Update completion state
124 $completion = new completion_info($course);
125 if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $glossary->completionentries && $entry->approved) {
126 $completion->update_state($cm, COMPLETION_COMPLETE);
129 add_to_log($course->id, "glossary", "add entry",
130 "view.php?id=$cm->id&amp;mode=entry&amp;hook=$entry->id", $entry->id, $cm->id);
132 } else {
133 //existing entry
134 $DB->update_record('glossary_entries', $entry);
135 add_to_log($course->id, "glossary", "update entry",
136 "view.php?id=$cm->id&amp;mode=entry&amp;hook=$entry->id",
137 $entry->id, $cm->id);
140 // save and relink embedded images and save attachments
141 $entry = file_postupdate_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry', $entry->id);
142 $entry = file_postupdate_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'mod_glossary', 'attachment', $entry->id);
144 // store the updated value values
145 $DB->update_record('glossary_entries', $entry);
147 //refetch complete entry
148 $entry = $DB->get_record('glossary_entries', array('id'=>$entry->id));
150 // update entry categories
151 $DB->delete_records('glossary_entries_categories', array('entryid'=>$entry->id));
152 // TODO: this deletes cats from both both main and secondary glossary :-(
153 if (!empty($categories) and array_search(0, $categories) === false) {
154 foreach ($categories as $catid) {
155 $newcategory = new stdClass();
156 $newcategory->entryid = $entry->id;
157 $newcategory->categoryid = $catid;
158 $DB->insert_record('glossary_entries_categories', $newcategory, false);
162 // update aliases
163 $DB->delete_records('glossary_alias', array('entryid'=>$entry->id));
164 if ($aliases !== '') {
165 $aliases = explode("\n", $aliases);
166 foreach ($aliases as $alias) {
167 $alias = trim($alias);
168 if ($alias !== '') {
169 $newalias = new stdClass();
170 $newalias->entryid = $entry->id;
171 $newalias->alias = $alias;
172 $DB->insert_record('glossary_alias', $newalias, false);
177 redirect("view.php?id=$cm->id&mode=entry&hook=$entry->id");
180 if (!empty($id)) {
181 $PAGE->navbar->add(get_string('edit'));
184 $PAGE->set_title(format_string($glossary->name));
185 $PAGE->set_heading($course->fullname);
186 echo $OUTPUT->header();
187 echo $OUTPUT->heading(format_string($glossary->name));
189 $mform->display();
191 echo $OUTPUT->footer();