Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / glossary / edit.php
blobc3ee5d5673784ab6a5ccede34741917a1da0a6ea
1 <?php // $Id$
3 require_once('../../config.php');
4 require_once('lib.php');
5 require_once('edit_form.php');
7 global $CFG, $USER;
9 $id = required_param('id', PARAM_INT); // Course Module ID
10 $e = optional_param('e', 0, PARAM_INT); // EntryID
11 $confirm = optional_param('confirm',0, PARAM_INT); // proceed. Edit the edtry
13 $mode = optional_param('mode', '', PARAM_ALPHA); // categories if by category?
14 $hook = optional_param('hook', '', PARAM_ALPHANUM); // CategoryID
16 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
17 error("Course Module ID was incorrect");
20 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
22 if (! $course = get_record("course", "id", $cm->course)) {
23 error("Course is misconfigured");
26 require_login($course->id, false, $cm);
28 if ( isguest() ) {
29 error("Guests are not allowed to edit glossaries", $_SERVER["HTTP_REFERER"]);
32 if (! $glossary = get_record("glossary", "id", $cm->instance)) {
33 error("Course module is incorrect");
37 if ($e) { // if entry is specified
38 if (!$entry = get_record("glossary_entries", "id", $e)) {
39 error("Incorrect entry id");
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 //expired edit time is the most probable cause here
44 print_error('erredittimeexpired', 'glossary', "view.php?id=$cm->id&amp;mode=entry&amp;hook=$e");
46 } else { // new entry
47 require_capability('mod/glossary:write', $context);
50 $mform =& new mod_glossary_entry_form(null, compact('cm', 'glossary', 'hook', 'mode', 'e', 'context'));
51 if ($mform->is_cancelled()){
52 if ($e){
53 redirect("view.php?id=$cm->id&amp;mode=entry&amp;hook=$e");
54 } else {
55 redirect("view.php?id=$cm->id");
58 } elseif ($fromform = $mform->get_data()) {
59 trusttext_after_edit($fromform->definition, $context);
61 if ( !isset($fromform->usedynalink) ) {
62 $fromform->usedynalink = 0;
64 if ( !isset($fromform->casesensitive) ) {
65 $fromform->casesensitive = 0;
67 if ( !isset($fromform->fullmatch) ) {
68 $fromform->fullmatch = 0;
70 $timenow = time();
72 $todb = new object();
73 $todb->course = $glossary->course;
74 $todb->glossaryid = $glossary->id;
76 $todb->concept = trim($fromform->concept);
77 $todb->definition = $fromform->definition;
78 $todb->format = $fromform->format;
79 $todb->usedynalink = $fromform->usedynalink;
80 $todb->casesensitive = $fromform->casesensitive;
81 $todb->fullmatch = $fromform->fullmatch;
82 $todb->timemodified = $timenow;
83 $todb->approved = 0;
84 $todb->aliases = "";
85 if ( $glossary->defaultapproval or has_capability('mod/glossary:approve', $context) ) {
86 $todb->approved = 1;
89 if ($e) {
90 $todb->id = $e;
91 $dir = glossary_file_area_name($todb);
92 if ($mform->save_files($dir) and $newfilename = $mform->get_new_filename()) {
93 $todb->attachment = $newfilename;
96 if (update_record('glossary_entries', $todb)) {
97 add_to_log($course->id, "glossary", "update entry",
98 "view.php?id=$cm->id&amp;mode=entry&amp;hook=$todb->id",
99 $todb->id, $cm->id);
100 } else {
101 error("Could not update your glossary");
103 } else {
105 $todb->userid = $USER->id;
106 $todb->timecreated = $timenow;
107 $todb->sourceglossaryid = 0;
108 $todb->teacherentry = has_capability('mod/glossary:manageentries', $context);
111 if ($todb->id = insert_record("glossary_entries", $todb)) {
112 $e = $todb->id;
113 $dir = glossary_file_area_name($todb);
114 if ($mform->save_files($dir) and $newfilename = $mform->get_new_filename()) {
115 set_field("glossary_entries", "attachment", $newfilename, "id", $todb->id);
117 add_to_log($course->id, "glossary", "add entry",
118 "view.php?id=$cm->id&amp;mode=entry&amp;hook=$todb->id", $todb->id,$cm->id);
119 } else {
120 error("Could not insert this new entry");
125 delete_records("glossary_entries_categories", "entryid", $e);
126 delete_records("glossary_alias", "entryid", $e);
128 if (empty($fromform->notcategorised) && isset($fromform->categories)) {
129 $newcategory->entryid = $e;
130 foreach ($fromform->categories as $category) {
131 if ( $category > 0 ) {
132 $newcategory->categoryid = $category;
133 insert_record("glossary_entries_categories", $newcategory, false);
134 } else {
135 break;
139 if ( isset($fromform->aliases) ) {
140 if ( $aliases = explode("\n", $fromform->aliases) ) {
141 foreach ($aliases as $alias) {
142 $alias = trim($alias);
143 if ($alias) {
144 unset($newalias);
145 $newalias->entryid = $e;
146 $newalias->alias = $alias;
147 insert_record("glossary_alias", $newalias, false);
152 redirect("view.php?id=$cm->id&amp;mode=entry&amp;hook=$todb->id");
154 } else {
155 if ($e) {
156 $fromdb = get_record("glossary_entries", "id", $e);
158 $toform = new object();
160 if ($categoriesarr = get_records_menu("glossary_entries_categories", "entryid", $e, '', 'id, categoryid')){
161 $toform->categories = array_values($categoriesarr);
162 } else {
163 $toform->categories = array(0);
165 $toform->concept = $fromdb->concept;
166 $toform->definition = $fromdb->definition;
167 $toform->format = $fromdb->format;
168 trusttext_prepare_edit($toform->definition, $toform->format, can_use_html_editor(), $context);
169 $toform->approved = $glossary->defaultapproval or has_capability('mod/glossary:approve', $context);
170 $toform->usedynalink = $fromdb->usedynalink;
171 $toform->casesensitive = $fromdb->casesensitive;
172 $toform->fullmatch = $fromdb->fullmatch;
173 $toform->aliases = '';
174 $ineditperiod = ((time() - $fromdb->timecreated < $CFG->maxeditingtime) || $glossary->editalways);
175 if ((!$ineditperiod || $USER->id != $fromdb->userid) and !has_capability('mod/glossary:manageentries', $context)) {
176 if ( $USER->id != $fromdb->userid ) {
177 print_error('errcannoteditothers', 'glossary');
178 } elseif (!$ineditperiod) {
179 print_error('erredittimeexpired', 'glossary');
181 die;
184 if ( $aliases = get_records_menu("glossary_alias", "entryid", $e, '', 'id, alias') ) {
185 $toform->aliases = implode("\n", $aliases) . "\n";
187 $mform->set_data($toform);
191 $stredit = empty($e) ? get_string('addentry', 'glossary') : get_string("edit");
192 $navigation = build_navigation($stredit, $cm);
193 print_header_simple(format_string($glossary->name), "", $navigation, "",
194 "", true, "", navmenu($course, $cm));
196 print_heading(format_string($glossary->name));
198 /// Info box
200 ///if ( $glossary->intro ) {
201 /// print_simple_box(format_text($glossary->intro), 'center', '70%', '', 5, 'generalbox', 'intro');
202 ///}
204 /// Tabbed browsing sections
205 ///$tab = GLOSSARY_ADDENTRY_VIEW;
206 ///include("tabs.php");
208 if (!$e) {
209 require_capability('mod/glossary:write', $context);
212 $mform->display();
214 ///glossary_print_tabbed_table_end();
217 print_footer($course);