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);
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);
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');
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');
60 print_error('invalidcategoryid');
64 require_login($course->id, false, $cm);
66 $context = get_context_instance(CONTEXT_MODULE, $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($strglossaries, new moodle_url('/mod/glossary/index.php', array('id'=>$course->id)));
73 $PAGE->navbar->add(get_string("categories","glossary"));
74 if (!empty($action)) {
75 $navaction = get_string($action). " " . moodle_strtolower(get_string("category","glossary"));
76 $PAGE->navbar->add($navaction);
78 $PAGE->set_title(format_string($glossary->name));
79 $PAGE->set_heading($course->fullname);
80 echo $OUTPUT->header();
82 // Prepare format_string/text options
84 'context' => $context);
86 if (right_to_left()) { // RTL table alignment support
87 $rightalignment = 'left';
88 $leftalignment = 'right';
90 $rightalignment = 'right';
91 $leftalignment = 'left';
97 if ( $action == "edit" ) {
100 $cat = new stdClass();
103 $cat->usedynalink = $usedynalink;
105 $DB->update_record("glossary_categories", $cat);
106 add_to_log($course->id, "glossary", "edit category", "editcategories.php?id=$cm->id", $hook,$cm->id);
109 echo "<h3 class=\"main\">" . get_string("edit"). " " . get_string("category","glossary") . "</h3>";
111 $name = $category->name;
112 $usedynalink = $category->usedynalink;
113 require "editcategories.html";
114 echo $OUTPUT->footer();
118 } elseif ( $action == "delete" ) {
120 $DB->delete_records("glossary_entries_categories", array("categoryid"=>$hook));
121 $DB->delete_records("glossary_categories", array("id"=>$hook));
123 echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow');
124 echo "<div>" . get_string("categorydeleted","glossary") ."</div>";
125 echo $OUTPUT->box_end();
127 add_to_log($course->id, "glossary", "delete category", "editcategories.php?id=$cm->id", $hook,$cm->id);
129 redirect("editcategories.php?id=$cm->id");
131 echo "<p>" . get_string("delete"). " " . get_string("category","glossary"). "</p>";
133 echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow');
134 echo "<div class=\"boxaligncenter deletecatconfirm\">".format_string($category->name, true, $fmtoptions)."<br/>";
136 $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid"=>$category->id));
137 if ( $num_entries ) {
138 print_string("deletingnoneemptycategory","glossary");
141 print_string("areyousuredelete","glossary");
145 <table border="0" width="100" class="confirmbuttons">
147 <td align="$rightalignment" style="width:50%">
148 <form id="form" method="post" action="editcategories.php">
150 <input type="hidden" name="id" value="<?php p($cm->id) ?>" />
151 <input type="hidden" name="action" value="delete" />
152 <input type="hidden" name="confirm" value="1" />
153 <input type="hidden" name="mode" value="<?php echo $mode ?>" />
154 <input type="hidden" name="hook" value="<?php echo $hook ?>" />
155 <input type="submit" value=" <?php print_string("yes")?> " />
159 <td align="$leftalignment" style="width:50%">
163 $options = array ("id" => $id);
164 echo $OUTPUT->single_button(new moodle_url("editcategories.php", $options), get_string("no"));
165 echo "</td></tr></table>";
167 echo $OUTPUT->box_end();
171 } elseif ( $action == "add" ) {
173 $dupcategory = $DB->get_records_sql("SELECT * FROM {glossary_categories} WHERE ".$DB->sql_like('name','?', false)." AND glossaryid=?", array($name, $glossary->id));
174 if ( $dupcategory ) {
175 echo "<h3 class=\"main\">" . get_string("add"). " " . get_string("category","glossary"). "</h3>";
177 echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow');
178 echo "<div>" . get_string("duplicatecategory","glossary") ."</div>";
179 echo $OUTPUT->box_end();
181 redirect("editcategories.php?id=$cm->id&action=add&name=$name");
185 $cat = new stdClass();
187 $cat->usedynalink = $usedynalink;
188 $cat->glossaryid = $glossary->id;
190 $cat->id = $DB->insert_record("glossary_categories", $cat);
191 add_to_log($course->id, "glossary", "add category", "editcategories.php?id=$cm->id", $cat->id,$cm->id);
194 echo "<h3 class=\"main\">" . get_string("add"). " " . get_string("category","glossary"). "</h3>";
196 require "editcategories.html";
201 echo $OUTPUT->footer();
207 <form method="post" action="editcategories.php">
208 <table width="40%" class="boxaligncenter generalbox" cellpadding="5">
210 <th style="width:90%" align="center">
211 <?php p(get_string("categories","glossary")) ?></th>
212 <th style="width:10%" align="center">
213 <?php p(get_string("action")) ?></th>
215 <tr><td style="width:100%" colspan="2">
220 $categories = $DB->get_records("glossary_categories", array("glossaryid"=>$glossary->id), "name ASC");
223 echo '<table width="100%">';
224 foreach ($categories as $category) {
225 $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid"=>$category->id));
229 <td style="width:80%" align="$leftalignment">
231 echo "<span class=\"bold\">".format_string($category->name, true, $fmtoptions)."</span> <span>($num_entries " . get_string("entries","glossary") . ")</span>";
234 <td style="width:19%" align="center" class="action">
236 echo "<a href=\"editcategories.php?id=$cm->id&action=delete&mode=cat&hook=$category->id\"><img alt=\"" . get_string("delete") . "\"src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" /></a> ";
237 echo "<a href=\"editcategories.php?id=$cm->id&action=edit&mode=cat&hook=$category->id\"><img alt=\"" . get_string("edit") . "\" src=\"" . $OUTPUT->pix_url('t/edit') . "\" class=\"iconsmall\" /></a>";
251 <td style="width:100%" colspan="2" align="center">
254 $options['id'] = $cm->id;
255 $options['action'] = "add";
257 echo "<table class=\"editbuttons\" border=\"0\"><tr><td align=\"$rightalignment\">";
258 echo $OUTPUT->single_button(new moodle_url("editcategories.php", $options), get_string("add") . " " . get_string("category","glossary"));
259 echo "</td><td align=\"$leftalignment\">";
260 unset($options['action']);
261 $options['mode'] = 'cat';
262 $options['hook'] = $hook;
263 echo $OUTPUT->single_button(new moodle_url("view.php", $options), get_string("back","glossary"));
276 echo $OUTPUT->footer();