MDL-30361 - mod/glossary/editcategories.php - fix spelling ($rightalignment)
[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 (right_to_left()) { // RTL table alignment support
87     $rightalignment = 'left';
88     $leftalignment = 'right';
89 } else {
90     $rightalignment = 'right';
91     $leftalignment = 'left';
93 }
95 if ( $hook >0 ) {
97     if ( $action == "edit" ) {
98         if ( $confirm ) {
99             $action = "";
100             $cat = new stdClass();
101             $cat->id = $hook;
102             $cat->name = $name;
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);
108         } else {
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();
115             die;
116         }
118     } elseif ( $action == "delete" ) {
119         if ( $confirm ) {
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");
130         } else {
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");
139             }
140             echo "<p>";
141             print_string("areyousuredelete","glossary");
142             echo "</p>";
143 ?>
145                 <table border="0" width="100" class="confirmbuttons">
146                     <tr>
147                         <td align="$rightalignment" style="width:50%">
148                         <form id="form" method="post" action="editcategories.php">
149                         <div>
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")?> " />
156                         </div>
157                         </form>
158                         </td>
159                         <td align="$leftalignment" style="width:50%">
161 <?php
162             unset($options);
163             $options = array ("id" => $id);
164             echo $OUTPUT->single_button(new moodle_url("editcategories.php", $options), get_string("no"));
165             echo "</td></tr></table>";
166             echo "</div>";
167             echo $OUTPUT->box_end();
168         }
169     }
171 } elseif ( $action == "add" ) {
172     if ( $confirm ) {
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&amp;action=add&amp;name=$name");
183         } else {
184             $action = "";
185             $cat = new stdClass();
186             $cat->name = $name;
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);
192         }
193     } else {
194         echo "<h3 class=\"main\">" . get_string("add"). " " . get_string("category","glossary"). "</h3>";
195         $name="";
196         require "editcategories.html";
197     }
200 if ( $action ) {
201     echo $OUTPUT->footer();
202     die;
205 ?>
207 <form method="post" action="editcategories.php">
208 <table width="40%" class="boxaligncenter generalbox" cellpadding="5">
209         <tr>
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>
214         </tr>
215         <tr><td style="width:100%" colspan="2">
219 <?php
220     $categories = $DB->get_records("glossary_categories", array("glossaryid"=>$glossary->id), "name ASC");
222     if ( $categories ) {
223         echo '<table width="100%">';
224         foreach ($categories as $category) {
225             $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid"=>$category->id));
226 ?>
228              <tr>
229                <td style="width:80%" align="$leftalignment">
230                <?php
231                     echo "<span class=\"bold\">".format_string($category->name, true, $fmtoptions)."</span> <span>($num_entries " . get_string("entries","glossary") . ")</span>";
232                ?>
233                </td>
234                <td style="width:19%" align="center" class="action">
235                <?php
236                 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> ";
237                 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>";
238                ?>
239                </td>
240              </tr>
242              <?php
244           }
245         echo '</table>';
246      }
247 ?>
249         </td></tr>
250         <tr>
251         <td style="width:100%" colspan="2"  align="center">
252             <?php
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"));
264              echo "</td></tr>";
265              echo "</table>";
267             ?>
268         </td>
269         </tr>
270         </table>
273 </form>
275 <?php
276 echo $OUTPUT->footer();