e8c265be561b329c0e7ba0b9780ce516e2b61daa
[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 if ( $hook >0 ) {
84     if ( $action == "edit" ) {
85         if ( $confirm ) {
86             $action = "";
87             $cat = new stdClass();
88             $cat->id = $hook;
89             $cat->name = $name;
90             $cat->usedynalink = $usedynalink;
92             $DB->update_record("glossary_categories", $cat);
93             add_to_log($course->id, "glossary", "edit category", "editcategories.php?id=$cm->id", $hook,$cm->id);
95         } else {
96             echo "<h3 class=\"main\">" . get_string("edit"). " " . get_string("category","glossary") . "</h3>";
98             $name = $category->name;
99             $usedynalink = $category->usedynalink;
100             require "editcategories.html";
101             echo $OUTPUT->footer();
102             die;
103         }
105     } elseif ( $action == "delete" ) {
106         if ( $confirm ) {
107             $DB->delete_records("glossary_entries_categories", array("categoryid"=>$hook));
108             $DB->delete_records("glossary_categories", array("id"=>$hook));
110             echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow');
111             echo "<div>" . get_string("categorydeleted","glossary") ."</div>";
112             echo $OUTPUT->box_end();
113             echo $OUTPUT->footer();
115             add_to_log($course->id, "glossary", "delete category", "editcategories.php?id=$cm->id", $hook,$cm->id);
117             redirect("editcategories.php?id=$cm->id");
118         } else {
119             echo "<p>" . get_string("delete"). " " . get_string("category","glossary"). "</p>";
121             echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow');
122             echo "<div class=\"boxaligncenter deletecatconfirm\">".format_text($category->name, FORMAT_PLAIN)."<br/>";
124             $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid"=>$category->id));
125             if ( $num_entries ) {
126                 print_string("deletingnoneemptycategory","glossary");
127             }
128             echo "<p>";
129             print_string("areyousuredelete","glossary");
130             echo "</p>";
131 ?>
133                 <table border="0" width="100" class="confirmbuttons">
134                     <tr>
135                         <td align="right" style="width:50%">
136                         <form id="form" method="post" action="editcategories.php">
137                         <div>
138                         <input type="hidden" name="id"          value="<?php p($cm->id) ?>" />
139                         <input type="hidden" name="action"      value="delete" />
140                         <input type="hidden" name="confirm"     value="1" />
141                         <input type="hidden" name="mode"         value="<?php echo $mode ?>" />
142                         <input type="hidden" name="hook"         value="<?php echo $hook ?>" />
143                         <input type="submit" value=" <?php print_string("yes")?> " />
144                         </div>
145                         </form>
146                         </td>
147                         <td align="left" style="width:50%">
149 <?php
150             unset($options);
151             $options = array ("id" => $id);
152             echo $OUTPUT->single_button(new moodle_url("editcategories.php", $options), get_string("no"));
153             echo "</td></tr></table>";
154             echo "</div>";
155             echo $OUTPUT->box_end();
156         }
157     }
159 } elseif ( $action == "add" ) {
160     if ( $confirm ) {
161         $dupcategory = $DB->get_records_sql("SELECT * FROM {glossary_categories} WHERE ".$DB->sql_like('name','?', false)." AND glossaryid=?", array($name, $glossary->id));
162         if ( $dupcategory ) {
163         echo "<h3 class=\"main\">" . get_string("add"). " " . get_string("category","glossary"). "</h3>";
165             echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow');
166             echo "<div>" . get_string("duplicatedcategory","glossary") ."</div>";
167             echo $OUTPUT->box_end();
169             redirect("editcategories.php?id=$cm->id&amp;action=add&&amp;name=$name");
171         } else {
172             $action = "";
173             $cat = new stdClass();
174             $cat->name = $name;
175             $cat->usedynalink = $usedynalink;
176             $cat->glossaryid = $glossary->id;
178             $cat->id = $DB->insert_record("glossary_categories", $cat);
179             add_to_log($course->id, "glossary", "add category", "editcategories.php?id=$cm->id", $cat->id,$cm->id);
180         }
181     } else {
182         echo "<h3 class=\"main\">" . get_string("add"). " " . get_string("category","glossary"). "</h3>";
183         $name="";
184         require "editcategories.html";
185     }
188 if ( $action ) {
189     echo $OUTPUT->footer();
190     die;
193 ?>
195 <form method="post" action="editcategories.php">
196 <table width="40%" class="boxaligncenter generalbox" cellpadding="5">
197         <tr>
198           <th style="width:90%" align="center">
199           <?php p(get_string("categories","glossary")) ?></th>
200           <th style="width:10%" align="center">
201           <?php p(get_string("action")) ?></th>
202         </tr>
203         <tr><td style="width:100%" colspan="2">
207 <?php
208     $categories = $DB->get_records("glossary_categories", array("glossaryid"=>$glossary->id), "name ASC");
210     if ( $categories ) {
211         echo '<table width="100%">';
212         foreach ($categories as $category) {
213             $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid"=>$category->id));
214 ?>
216              <tr>
217                <td style="width:80%" align="left">
218                <?php
219                     echo "<span class=\"bold\">".format_text($category->name, FORMAT_PLAIN)."</span> <span>($num_entries " . get_string("entries","glossary") . ")</span>";
220                ?>
221                </td>
222                <td style="width:19%" align="center" class="action">
223                <?php
224                 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> ";
225                 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>";
226                ?>
227                </td>
228              </tr>
230              <?php
232           }
233         echo '</table>';
234      }
235 ?>
237         </td></tr>
238         <tr>
239         <td style="width:100%" colspan="2"  align="center">
240             <?php
242              $options['id'] = $cm->id;
243              $options['action'] = "add";
245              echo "<table class=\"editbuttons\" border=\"0\"><tr><td align=\"right\">";
246              echo $OUTPUT->single_button(new moodle_url("editcategories.php", $options), get_string("add") . " " . get_string("category","glossary"));
247              echo "</td><td align=\"left\">";
248              unset($options['action']);
249              $options['mode'] = 'cat';
250              $options['hook'] = $hook;
251              echo $OUTPUT->single_button(new moodle_url("view.php", $options), get_string("back","glossary"));
252              echo "</td></tr>";
253              echo "</table>";
255             ?>
256         </td>
257         </tr>
258         </table>
261 </form>
263 <?php
264 echo $OUTPUT->footer();