Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / glossary / editcategories.php
blobca1b584b9ff9eddf0363a17ba6a486ee6654f0e1
1 <?php // $Id$
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 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
20 error("Course Module ID was incorrect");
23 if (! $course = get_record("course", "id", $cm->course)) {
24 error("Course is misconfigured");
27 if (! $glossary = get_record("glossary", "id", $cm->instance)) {
28 error("Course module is incorrect");
31 if ($hook > 0) {
32 if ($category = get_record("glossary_categories","id",$hook)) {
33 //Check it belongs to the same glossary
34 if ($category->glossaryid != $glossary->id) {
35 error("Glossary is incorrect");
37 } else {
38 error("Category is incorrect");
42 require_login($course->id, false, $cm);
44 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
45 require_capability('mod/glossary:managecategories', $context);
47 $strglossaries = get_string("modulenameplural", "glossary");
48 $strglossary = get_string("modulename", "glossary");
50 $navlinks = array();
51 $navlinks[] = array('name' => $strglossaries, 'link' => "index.php?id=$course->id", 'type' => 'activity');
52 $navlinks[] = array('name' => format_string($glossary->name), 'link' => "view.php?id=$cm->id&amp;tab=GLOSSARY_CATEGORY_VIEW", 'type' => 'activityinstance');
53 $navlinks[] = array('name' => get_string("categories","glossary"), 'link' => '', 'type' => 'title');
55 $navigation = build_navigation($navlinks);
57 print_header_simple(format_string($glossary->name), "", $navigation,
58 "", "", true, update_module_button($cm->id, $course->id, $strglossary),
59 navmenu($course, $cm));
61 if ( $hook >0 ) {
63 if ( $action == "edit" ) {
64 if ( $confirm ) {
65 $action = "";
66 $cat->id = $hook;
67 $cat->name = $name;
68 $cat->usedynalink = $usedynalink;
70 if ( !update_record("glossary_categories", $cat) ) {
71 error("Weird error. The category was not updated.");
72 redirect("editcategories.php?id=$cm->id");
73 } else {
74 add_to_log($course->id, "glossary", "edit category", "editcategories.php?id=$cm->id", $hook,$cm->id);
76 } else {
77 echo "<p style=\"text-align:center\">" . get_string("edit"). " " . get_string("category","glossary") . "<span style=\"font-size:1.5em\">";
79 $name = $category->name;
80 $usedynalink = $category->usedynalink;
81 require "editcategories.html";
82 print_footer();
83 die;
86 } elseif ( $action == "delete" ) {
87 if ( $confirm ) {
88 delete_records("glossary_entries_categories","categoryid", $hook);
89 delete_records("glossary_categories","id", $hook);
91 print_simple_box_start("center","40%", "#FFBBBB");
92 echo "<div style=\"text-align:center\">" . get_string("categorydeleted","glossary") ."</div>";
93 echo "</center>";
94 print_simple_box_end();
95 print_footer($course);
97 add_to_log($course->id, "glossary", "delete category", "editcategories.php?id=$cm->id", $hook,$cm->id);
99 redirect("editcategories.php?id=$cm->id");
100 } else {
101 echo "<p style=\"text-align:center\">" . get_string("delete"). " " . get_string("category","glossary"). "</p>";
103 print_simple_box_start("center","40%", "#FFBBBB");
104 echo "<div class=\"boxaligncenter\"><b>".format_text($category->name, FORMAT_PLAIN)."</b><br/>";
106 $num_entries = count_records("glossary_entries_categories","categoryid",$category->id);
107 if ( $num_entries ) {
108 print_string("deletingnoneemptycategory","glossary");
110 echo "<p>";
111 print_string("areyousuredelete","glossary");
112 echo "</p>";
115 <table border="0" width="100">
116 <tr>
117 <td align="right" style="width:50%">
118 <form id="form" method="post" action="editcategories.php">
119 <div>
120 <input type="hidden" name="id" value="<?php p($cm->id) ?>" />
121 <input type="hidden" name="action" value="delete" />
122 <input type="hidden" name="confirm" value="1" />
123 <input type="hidden" name="mode" value="<?php echo $mode ?>" />
124 <input type="hidden" name="hook" value="<?php echo $hook ?>" />
125 <input type="submit" value=" <?php print_string("yes")?> " />
126 </div>
127 </form>
128 </td>
129 <td align="left" style="width:50%">
131 <?php
132 unset($options);
133 $options = array ("id" => $id);
134 print_single_button("editcategories.php", $options, get_string("no") );
135 echo "</td></tr></table>";
136 echo "</div>";
137 print_simple_box_end();
141 } elseif ( $action == "add" ) {
142 if ( $confirm ) {
143 $ILIKE = sql_ilike();
144 $dupcategory = get_records_sql("SELECT * FROM {$CFG->prefix}glossary_categories WHERE name $ILIKE '$name' AND glossaryid=$glossary->id");
145 if ( $dupcategory ) {
146 echo "<p style=\"text-align:center\">" . get_string("add"). " " . get_string("category","glossary");
148 print_simple_box_start("center","40%", "#FFBBBB");
149 echo "<div style=\"text-align:center\">" . get_string("duplicatedcategory","glossary") ."</div>";
150 print_simple_box_end();
152 redirect("editcategories.php?id=$cm->id&amp;action=add&&amp;name=$name");
154 } else {
155 $action = "";
156 $cat->name = $name;
157 $cat->usedynalink = $usedynalink;
158 $cat->glossaryid = $glossary->id;
160 if ( ! $cat->id = insert_record("glossary_categories", $cat) ) {
161 error("Weird error. The category was not inserted.");
163 redirect("editcategories.php?id=$cm->id");
164 } else {
165 add_to_log($course->id, "glossary", "add category", "editcategories.php?id=$cm->id", $cat->id,$cm->id);
168 } else {
169 echo "<p style=\"text-align:center\">" . get_string("add"). " " . get_string("category","glossary"). "</p>";
170 $name="";
171 require "editcategories.html";
175 if ( $action ) {
176 print_footer();
177 die;
182 <form method="post" action="editcategories.php">
183 <table width="40%" class="boxaligncenter generalbox" cellpadding="5">
184 <tr>
185 <td style="width:90%" align="center"><b>
186 <?php p(get_string("categories","glossary")) ?></b></td>
187 <td style="width:10%" align="center"><b>
188 <?php p(get_string("action")) ?></b></td>
189 </tr>
190 <tr><td style="width:100%" colspan="2">
194 <?php
195 $categories = get_records("glossary_categories","glossaryid",$glossary->id,"name ASC");
197 if ( $categories ) {
198 echo '<table width="100%">';
199 foreach ($categories as $category) {
200 $num_entries = count_records("glossary_entries_categories","categoryid",$category->id);
203 <tr>
204 <td style="width:90%" align="left">
205 <?php
206 echo "<b>".format_text($category->name, FORMAT_PLAIN)."</b> <span style=\"font-size:0.75em\">($num_entries " . get_string("entries","glossary") . ")</span>";
208 </td>
209 <td style="width:10%" align="center"><b>
210 <?php
211 echo "<a href=\"editcategories.php?id=$cm->id&amp;action=delete&amp;mode=cat&amp;hook=$category->id\"><img alt=\"" . get_string("delete") . "\"src=\"{$CFG->pixpath}/t/delete.gif\" class=\"iconsmall\" /></a> ";
212 echo "<a href=\"editcategories.php?id=$cm->id&amp;action=edit&amp;mode=cat&amp;hook=$category->id\"><img alt=\"" . get_string("edit") . "\" src=\"{$CFG->pixpath}/t/edit.gif\" class=\"iconsmall\" /></a>";
214 </b></td>
215 </tr>
217 <?php
220 echo '</table>';
224 </td></tr>
225 <tr>
226 <td style="width:100%" colspan="2" align="center">
227 <?php
229 $options['id'] = $cm->id;
230 $options['action'] = "add";
232 echo "<table border=\"0\"><tr><td align=\"right\">";
233 echo print_single_button("editcategories.php", $options, get_string("add") . " " . get_string("category","glossary"), "get");
234 echo "</td><td align=\"left\">";
235 unset($options['action']);
236 $options['mode'] = 'cat';
237 $options['hook'] = $hook;
238 echo print_single_button("view.php", $options, get_string("back","glossary") );
239 echo "</td></tr>";
240 echo "</table>";
243 </td>
244 </tr>
245 </table>
248 </form>
250 <?php print_footer() ?>