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);
88 if ( $action == "edit" ) {
91 $cat = new stdClass();
94 $cat->usedynalink = $usedynalink;
96 $DB->update_record("glossary_categories", $cat);
97 add_to_log($course->id, "glossary", "edit category", "editcategories.php?id=$cm->id", $hook,$cm->id);
100 echo "<h3 class=\"main\">" . get_string("edit"). " " . get_string("category","glossary") . "</h3>";
102 $name = $category->name;
103 $usedynalink = $category->usedynalink;
104 require "editcategories.html";
105 echo $OUTPUT->footer();
109 } elseif ( $action == "delete" ) {
111 $DB->delete_records("glossary_entries_categories", array("categoryid"=>$hook));
112 $DB->delete_records("glossary_categories", array("id"=>$hook));
114 echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow');
115 echo "<div>" . get_string("categorydeleted","glossary") ."</div>";
116 echo $OUTPUT->box_end();
118 add_to_log($course->id, "glossary", "delete category", "editcategories.php?id=$cm->id", $hook,$cm->id);
120 redirect("editcategories.php?id=$cm->id");
122 echo "<p>" . get_string("delete"). " " . get_string("category","glossary"). "</p>";
124 echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow');
125 echo "<div class=\"boxaligncenter deletecatconfirm\">".format_string($category->name, true, $fmtoptions)."<br/>";
127 $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid"=>$category->id));
128 if ( $num_entries ) {
129 print_string("deletingnoneemptycategory","glossary");
132 print_string("areyousuredelete","glossary");
136 <table border="0" width="100" class="confirmbuttons">
138 <td align="right" style="width:50%">
139 <form id="form" method="post" action="editcategories.php">
141 <input type="hidden" name="id" value="<?php p($cm->id) ?>" />
142 <input type="hidden" name="action" value="delete" />
143 <input type="hidden" name="confirm" value="1" />
144 <input type="hidden" name="mode" value="<?php echo $mode ?>" />
145 <input type="hidden" name="hook" value="<?php echo $hook ?>" />
146 <input type="submit" value=" <?php print_string("yes")?> " />
150 <td align="left" style="width:50%">
154 $options = array ("id" => $id);
155 echo $OUTPUT->single_button(new moodle_url("editcategories.php", $options), get_string("no"));
156 echo "</td></tr></table>";
158 echo $OUTPUT->box_end();
162 } elseif ( $action == "add" ) {
164 $dupcategory = $DB->get_records_sql("SELECT * FROM {glossary_categories} WHERE ".$DB->sql_like('name','?', false)." AND glossaryid=?", array($name, $glossary->id));
165 if ( $dupcategory ) {
166 echo "<h3 class=\"main\">" . get_string("add"). " " . get_string("category","glossary"). "</h3>";
168 echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow');
169 echo "<div>" . get_string("duplicatecategory","glossary") ."</div>";
170 echo $OUTPUT->box_end();
172 redirect("editcategories.php?id=$cm->id&action=add&name=$name");
176 $cat = new stdClass();
178 $cat->usedynalink = $usedynalink;
179 $cat->glossaryid = $glossary->id;
181 $cat->id = $DB->insert_record("glossary_categories", $cat);
182 add_to_log($course->id, "glossary", "add category", "editcategories.php?id=$cm->id", $cat->id,$cm->id);
185 echo "<h3 class=\"main\">" . get_string("add"). " " . get_string("category","glossary"). "</h3>";
187 require "editcategories.html";
192 echo $OUTPUT->footer();
198 <form method="post" action="editcategories.php">
199 <table width="40%" class="boxaligncenter generalbox" cellpadding="5">
201 <th style="width:90%" align="center">
202 <?php p(get_string("categories","glossary")) ?></th>
203 <th style="width:10%" align="center">
204 <?php p(get_string("action")) ?></th>
206 <tr><td style="width:100%" colspan="2">
211 $categories = $DB->get_records("glossary_categories", array("glossaryid"=>$glossary->id), "name ASC");
214 echo '<table width="100%">';
215 foreach ($categories as $category) {
216 $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid"=>$category->id));
220 <td style="width:80%" align="left">
222 echo "<span class=\"bold\">".format_string($category->name, true, $fmtoptions)."</span> <span>($num_entries " . get_string("entries","glossary") . ")</span>";
225 <td style="width:19%" align="center" class="action">
227 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> ";
228 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>";
242 <td style="width:100%" colspan="2" align="center">
245 $options['id'] = $cm->id;
246 $options['action'] = "add";
248 echo "<table class=\"editbuttons\" border=\"0\"><tr><td align=\"right\">";
249 echo $OUTPUT->single_button(new moodle_url("editcategories.php", $options), get_string("add") . " " . get_string("category","glossary"));
250 echo "</td><td align=\"left\">";
251 unset($options['action']);
252 $options['mode'] = 'cat';
253 $options['hook'] = $hook;
254 echo $OUTPUT->single_button(new moodle_url("view.php", $options), get_string("back","glossary"));
267 echo $OUTPUT->footer();