MDL-28048 glossary - fix double ampersand & missing string
[moodle.git] / mod / glossary / editcategories.php
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);
22 }
23 if ($confirm !== 0) {
24     $url->param('confirm', $confirm);
25 }
26 if ($name !== 'name') {
27     $url->param('name', $name);
28 }
29 if ($action !== 'action') {
30     $url->param('action', $action);
31 }
32 if ($hook !== 'hook') {
33     $url->param('hook', $hook);
34 }
35 if ($mode !== 'mode') {
36     $url->param('mode', $mode);
37 }
39 $PAGE->set_url($url);
41 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
42     print_error('invalidcoursemodule');
43 }
45 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
46     print_error('coursemisconf');
47 }
49 if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
50     print_error('invalidcoursemodule');
51 }
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');
58         }
59     } else {
60         print_error('invalidcategoryid');
61     }
62 }
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);
77 }
78 $PAGE->set_title(format_string($glossary->name));
79 $PAGE->set_heading($course->fullname);
80 echo $OUTPUT->header();
82 // Prepare format_string/text options
83 $fmtoptions = array(
84     'context' => $context);
86 if ( $hook >0 ) {
88     if ( $action == "edit" ) {
89         if ( $confirm ) {
90             $action = "";
91             $cat = new stdClass();
92             $cat->id = $hook;
93             $cat->name = $name;
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);
99         } else {
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();
106             die;
107         }
109     } elseif ( $action == "delete" ) {
110         if ( $confirm ) {
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");
121         } else {
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");
130             }
131             echo "<p>";
132             print_string("areyousuredelete","glossary");
133             echo "</p>";
134 ?>
136                 <table border="0" width="100" class="confirmbuttons">
137                     <tr>
138                         <td align="right" style="width:50%">
139                         <form id="form" method="post" action="editcategories.php">
140                         <div>
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")?> " />
147                         </div>
148                         </form>
149                         </td>
150                         <td align="left" style="width:50%">
152 <?php
153             unset($options);
154             $options = array ("id" => $id);
155             echo $OUTPUT->single_button(new moodle_url("editcategories.php", $options), get_string("no"));
156             echo "</td></tr></table>";
157             echo "</div>";
158             echo $OUTPUT->box_end();
159         }
160     }
162 } elseif ( $action == "add" ) {
163     if ( $confirm ) {
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&amp;action=add&amp;name=$name");
174         } else {
175             $action = "";
176             $cat = new stdClass();
177             $cat->name = $name;
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);
183         }
184     } else {
185         echo "<h3 class=\"main\">" . get_string("add"). " " . get_string("category","glossary"). "</h3>";
186         $name="";
187         require "editcategories.html";
188     }
191 if ( $action ) {
192     echo $OUTPUT->footer();
193     die;
196 ?>
198 <form method="post" action="editcategories.php">
199 <table width="40%" class="boxaligncenter generalbox" cellpadding="5">
200         <tr>
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>
205         </tr>
206         <tr><td style="width:100%" colspan="2">
210 <?php
211     $categories = $DB->get_records("glossary_categories", array("glossaryid"=>$glossary->id), "name ASC");
213     if ( $categories ) {
214         echo '<table width="100%">';
215         foreach ($categories as $category) {
216             $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid"=>$category->id));
217 ?>
219              <tr>
220                <td style="width:80%" align="left">
221                <?php
222                     echo "<span class=\"bold\">".format_string($category->name, true, $fmtoptions)."</span> <span>($num_entries " . get_string("entries","glossary") . ")</span>";
223                ?>
224                </td>
225                <td style="width:19%" align="center" class="action">
226                <?php
227                 echo "<a href=\"editcategories.php?id=$cm->id&amp;action=delete&amp;mode=cat&amp;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&amp;action=edit&amp;mode=cat&amp;hook=$category->id\"><img  alt=\"" . get_string("edit") . "\" src=\"" . $OUTPUT->pix_url('t/edit') . "\" class=\"iconsmall\" /></a>";
229                ?>
230                </td>
231              </tr>
233              <?php
235           }
236         echo '</table>';
237      }
238 ?>
240         </td></tr>
241         <tr>
242         <td style="width:100%" colspan="2"  align="center">
243             <?php
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"));
255              echo "</td></tr>";
256              echo "</table>";
258             ?>
259         </td>
260         </tr>
261         </table>
264 </form>
266 <?php
267 echo $OUTPUT->footer();