Merge branch 'MDL-74636_39' of https://github.com/timhunt/moodle into MOODLE_39_STABLE
[moodle.git] / mod / glossary / editcategories.php
blob079c86f1d3649e68d07f1c70bf6e2002c5f8ef50
1 <?php
3 /// This page allows to edit entries categories for a particular instance of glossary
5 require_once("../../config.php");
6 require_once("lib.php");
8 $id = required_param('id', PARAM_INT); // Course Module ID, or
9 $usedynalink = optional_param('usedynalink', 0, PARAM_INT); // category ID
10 $confirm = optional_param('confirm', 0, PARAM_INT); // confirm the action
11 $name = optional_param('name', '', PARAM_CLEAN); // confirm the name
13 $action = optional_param('action', '', PARAM_ALPHA ); // what to do
14 $hook = optional_param('hook', '', PARAM_ALPHANUM); // category ID
15 $mode = optional_param('mode', '', PARAM_ALPHA); // cat
17 $action = strtolower($action);
19 $url = new moodle_url('/mod/glossary/editcategories.php', array('id'=>$id));
20 if ($usedynalink !== 0) {
21 $url->param('usedynalink', $usedynalink);
23 if ($confirm !== 0) {
24 $url->param('confirm', $confirm);
26 if ($name !== 'name') {
27 $url->param('name', $name);
29 if ($action !== 'action') {
30 $url->param('action', $action);
32 if ($hook !== 'hook') {
33 $url->param('hook', $hook);
35 if ($mode !== 'mode') {
36 $url->param('mode', $mode);
39 $PAGE->set_url($url);
41 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
42 print_error('invalidcoursemodule');
45 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
46 print_error('coursemisconf');
49 if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
50 print_error('invalidcoursemodule');
53 if ($hook > 0) {
54 if ($category = $DB->get_record("glossary_categories", array("id"=>$hook))) {
55 //Check it belongs to the same glossary
56 if ($category->glossaryid != $glossary->id) {
57 print_error('invalidid', 'glossary');
59 } else {
60 print_error('invalidcategoryid');
64 require_login($course, false, $cm);
66 $context = context_module::instance($cm->id);
67 require_capability('mod/glossary:managecategories', $context);
69 $strglossaries = get_string("modulenameplural", "glossary");
70 $strglossary = get_string("modulename", "glossary");
72 $PAGE->navbar->add(get_string("categories","glossary"),
73 new moodle_url('/mod/glossary/editcategories.php', array('id' => $cm->id,'mode' => 'cat')));
74 if (!empty($action)) {
75 $navaction = get_string(core_text::strtolower($action."category"), 'glossary');
76 $PAGE->navbar->add($navaction);
78 $PAGE->set_title($glossary->name);
79 $PAGE->set_heading($course->fullname);
81 // Prepare format_string/text options
82 $fmtoptions = array(
83 'context' => $context);
85 if (right_to_left()) { // RTL table alignment support
86 $rightalignment = 'left';
87 $leftalignment = 'right';
88 } else {
89 $rightalignment = 'right';
90 $leftalignment = 'left';
94 if ( $hook >0 ) {
96 if ( $action == "edit" ) {
97 if ( $confirm ) {
98 require_sesskey();
99 $action = "";
100 $cat = new stdClass();
101 $cat->id = $hook;
102 $cat->name = $name;
103 $cat->usedynalink = $usedynalink;
105 $DB->update_record("glossary_categories", $cat);
106 $event = \mod_glossary\event\category_updated::create(array(
107 'context' => $context,
108 'objectid' => $hook
110 $cat->glossaryid = $glossary->id;
111 $event->add_record_snapshot('glossary_categories', $cat);
112 $event->add_record_snapshot('glossary', $glossary);
113 $event->trigger();
115 // Reset caches.
116 \mod_glossary\local\concept_cache::reset_glossary($glossary);
118 } else {
119 echo $OUTPUT->header();
120 echo $OUTPUT->heading(format_string($glossary->name), 2);
121 echo $OUTPUT->heading(format_string(get_string("editcategory", "glossary")), 3);
123 $name = $category->name;
124 $usedynalink = $category->usedynalink;
125 require "editcategories.html";
126 echo $OUTPUT->footer();
127 die;
130 } elseif ( $action == "delete" ) {
131 if ( $confirm ) {
132 require_sesskey();
133 $DB->delete_records("glossary_entries_categories", array("categoryid"=>$hook));
134 $DB->delete_records("glossary_categories", array("id"=>$hook));
136 $event = \mod_glossary\event\category_deleted::create(array(
137 'context' => $context,
138 'objectid' => $hook
140 $event->add_record_snapshot('glossary_categories', $category);
141 $event->add_record_snapshot('glossary', $glossary);
142 $event->trigger();
144 // Reset caches.
145 \mod_glossary\local\concept_cache::reset_glossary($glossary);
147 redirect("editcategories.php?id=$cm->id", get_string("categorydeleted", "glossary"), 2);
148 } else {
149 echo $OUTPUT->header();
150 echo $OUTPUT->heading(format_string($glossary->name), 2);
151 echo $OUTPUT->heading(format_string(get_string("deletecategory", "glossary")), 3);
153 echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow');
154 echo "<div class=\"boxaligncenter deletecatconfirm\">".format_string($category->name, true, $fmtoptions)."<br/>";
156 $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid"=>$category->id));
157 if ( $num_entries ) {
158 print_string("deletingnoneemptycategory","glossary");
160 echo "<p>";
161 print_string("areyousuredelete","glossary");
162 echo "</p>";
165 <table border="0" width="100" class="confirmbuttons">
166 <tr>
167 <td align="$rightalignment" style="width:50%">
168 <form id="form" method="post" action="editcategories.php">
169 <div>
170 <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
171 <input type="hidden" name="id" value="<?php p($cm->id) ?>" />
172 <input type="hidden" name="action" value="delete" />
173 <input type="hidden" name="confirm" value="1" />
174 <input type="hidden" name="mode" value="<?php echo $mode ?>" />
175 <input type="hidden" name="hook" value="<?php echo $hook ?>" />
176 <input type="submit" class="btn btn-primary" value=" <?php print_string("yes")?> " />
177 </div>
178 </form>
179 </td>
180 <td align="$leftalignment" style="width:50%">
182 <?php
183 unset($options);
184 $options = array ("id" => $id);
185 echo $OUTPUT->single_button(new moodle_url("editcategories.php", $options), get_string("no"));
186 echo "</td></tr></table>";
187 echo "</div>";
188 echo $OUTPUT->box_end();
192 } elseif ( $action == "add" ) {
193 if ( $confirm ) {
194 require_sesskey();
195 $dupcategory = $DB->get_records_sql("SELECT * FROM {glossary_categories} WHERE ".$DB->sql_like('name','?', false)." AND glossaryid=?", array($name, $glossary->id));
196 if ( $dupcategory ) {
197 redirect("editcategories.php?id=$cm->id&amp;action=add&amp;name=$name", get_string("duplicatecategory", "glossary"), 2);
199 } else {
200 $action = "";
201 $cat = new stdClass();
202 $cat->name = $name;
203 $cat->usedynalink = $usedynalink;
204 $cat->glossaryid = $glossary->id;
206 $cat->id = $DB->insert_record("glossary_categories", $cat);
207 $event = \mod_glossary\event\category_created::create(array(
208 'context' => $context,
209 'objectid' => $cat->id
211 $event->add_record_snapshot('glossary_categories', $cat);
212 $event->add_record_snapshot('glossary', $glossary);
213 $event->trigger();
215 // Reset caches.
216 \mod_glossary\local\concept_cache::reset_glossary($glossary);
218 } else {
219 echo $OUTPUT->header();
220 echo $OUTPUT->heading(format_string($glossary->name), 2);
221 echo "<h3 class=\"main\">" . get_string("addcategory", "glossary"). "</h3>";
222 $name="";
223 require "editcategories.html";
227 if ( $action ) {
228 echo $OUTPUT->footer();
229 die;
232 echo $OUTPUT->header();
233 echo $OUTPUT->heading(format_string($glossary->name), 2);
237 <form method="post" action="editcategories.php">
238 <table width="40%" class="boxaligncenter generalbox" cellpadding="5">
239 <tr>
240 <th style="width:90%" align="center">
241 <?php p(get_string("categories","glossary")) ?></th>
242 <th style="width:10%" align="center">
243 <?php p(get_string("action")) ?></th>
244 </tr>
245 <tr><td style="width:100%" colspan="2">
249 <?php
250 $categories = $DB->get_records("glossary_categories", array("glossaryid"=>$glossary->id), "name ASC");
252 if ( $categories ) {
253 echo '<table width="100%">';
254 foreach ($categories as $category) {
255 $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid"=>$category->id));
258 <tr>
259 <td style="width:80%" align="$leftalignment">
260 <?php
261 echo "<span class=\"bold\">".format_string($category->name, true, $fmtoptions)."</span> <span>($num_entries " . get_string("entries","glossary") . ")</span>";
263 </td>
264 <td style="width:19%" align="center" class="action">
265 <?php
266 echo "<a href=\"editcategories.php?id=$cm->id&amp;action=delete&amp;mode=cat&amp;hook=$category->id\">" .
267 $OUTPUT->pix_icon('t/delete', get_string('delete')). "</a> ";
268 echo "<a href=\"editcategories.php?id=$cm->id&amp;action=edit&amp;mode=cat&amp;hook=$category->id\">" .
269 $OUTPUT->pix_icon('t/edit', get_string('edit')). "</a> ";
271 </td>
272 </tr>
274 <?php
277 echo '</table>';
281 </td></tr>
282 <tr>
283 <td style="width:100%" colspan="2" align="center">
284 <?php
286 $options['id'] = $cm->id;
287 $options['action'] = "add";
289 echo "<table class=\"editbuttons\" border=\"0\"><tr><td align=\"$rightalignment\">";
290 echo $OUTPUT->single_button(new moodle_url("editcategories.php", $options), get_string("addcategory", "glossary"));
291 echo "</td><td align=\"$leftalignment\">";
292 unset($options['action']);
293 $options['mode'] = 'cat';
294 $options['hook'] = $hook;
295 echo $OUTPUT->single_button(new moodle_url("view.php", $options), get_string("back","glossary"));
296 echo "</td></tr>";
297 echo "</table>";
300 </td>
301 </tr>
302 </table>
305 </form>
307 <?php
308 echo $OUTPUT->footer();