Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   1  <?php
   2  
   3  /// This page allows to edit entries categories for a particular instance of glossary
   4  
   5  require_once("../../config.php");
   6  require_once ("lib.php");
   7  
   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
  12  
  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
  16  
  17  $action = strtolower($action);
  18  
  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  }
  38  
  39  $PAGE->set_url($url);
  40  
  41  if (! $cm = get_coursemodule_from_id('glossary', $id)) {
  42      throw new \moodle_exception('invalidcoursemodule');
  43  }
  44  
  45  if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
  46      throw new \moodle_exception('coursemisconf');
  47  }
  48  
  49  if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
  50      throw new \moodle_exception('invalidcoursemodule');
  51  }
  52  
  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              throw new \moodle_exception('invalidid', 'glossary');
  58          }
  59      } else {
  60          throw new \moodle_exception('invalidcategoryid');
  61      }
  62  }
  63  
  64  require_login($course, false, $cm);
  65  
  66  $context = context_module::instance($cm->id);
  67  require_capability('mod/glossary:managecategories', $context);
  68  
  69  $strglossaries   = get_string("modulenameplural", "glossary");
  70  $strglossary     = get_string("modulename", "glossary");
  71  
  72  $PAGE->navbar->add(get_string("categories","glossary"),
  73          new moodle_url('/mod/glossary/editcategories.php', array('id' => $cm->id,'mode' => 'cat')));
  74  if (!empty($action)) {
  75      $navaction = get_string(core_text::strtolower($action."category"), 'glossary');
  76      $PAGE->navbar->add($navaction);
  77  }
  78  $PAGE->set_title($glossary->name);
  79  $PAGE->set_heading($course->fullname);
  80  
  81  // Prepare format_string/text options
  82  $fmtoptions = array(
  83      'context' => $context);
  84  
  85  if (right_to_left()) { // RTL table alignment support
  86      $rightalignment = 'left';
  87      $leftalignment = 'right';
  88  } else {
  89      $rightalignment = 'right';
  90      $leftalignment = 'left';
  91  
  92  }
  93  
  94  if ( $hook >0 ) {
  95  
  96      if ( $action == "edit" ) {
  97          if ( $confirm ) {
  98              require_sesskey();
  99              $action = "";
 100              $cat = new stdClass();
 101              $cat->id = $hook;
 102              $cat->name = $name;
 103              $cat->usedynalink = $usedynalink;
 104  
 105              $DB->update_record("glossary_categories", $cat);
 106              $event = \mod_glossary\event\category_updated::create(array(
 107                  'context' => $context,
 108                  'objectid' => $hook
 109              ));
 110              $cat->glossaryid = $glossary->id;
 111              $event->add_record_snapshot('glossary_categories', $cat);
 112              $event->add_record_snapshot('glossary', $glossary);
 113              $event->trigger();
 114  
 115              // Reset caches.
 116              \mod_glossary\local\concept_cache::reset_glossary($glossary);
 117  
 118          } else {
 119              echo $OUTPUT->header();
 120              echo $OUTPUT->heading(format_string($glossary->name), 2);
 121              echo $OUTPUT->heading(format_string(get_string("editcategory", "glossary")), 3);
 122  
 123              $name = $category->name;
 124              $usedynalink = $category->usedynalink;
 125              require "editcategories.html";
 126              echo $OUTPUT->footer();
 127              die;
 128          }
 129  
 130      } elseif ( $action == "delete" ) {
 131          if ( $confirm ) {
 132              require_sesskey();
 133              $DB->delete_records("glossary_entries_categories", array("categoryid"=>$hook));
 134              $DB->delete_records("glossary_categories", array("id"=>$hook));
 135  
 136              $event = \mod_glossary\event\category_deleted::create(array(
 137                  'context' => $context,
 138                  'objectid' => $hook
 139              ));
 140              $event->add_record_snapshot('glossary_categories', $category);
 141              $event->add_record_snapshot('glossary', $glossary);
 142              $event->trigger();
 143  
 144              // Reset caches.
 145              \mod_glossary\local\concept_cache::reset_glossary($glossary);
 146  
 147              redirect("editcategories.php?id=$cm->id", get_string("categorydeleted", "glossary"), 2);
 148          } else {
 149              echo $OUTPUT->header();
 150              echo $OUTPUT->heading(format_string($glossary->name), 2);
 151              echo $OUTPUT->heading(format_string(get_string("deletecategory", "glossary")), 3);
 152  
 153              echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow');
 154              echo "<div class=\"boxaligncenter deletecatconfirm\">".format_string($category->name, true, $fmtoptions)."<br/>";
 155  
 156              $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid"=>$category->id));
 157              if ( $num_entries ) {
 158                  print_string("deletingnoneemptycategory","glossary");
 159              }
 160              echo "<p>";
 161              print_string("areyousuredelete","glossary");
 162              echo "</p>";
 163  ?>
 164  
 165                  <table border="0" width="100" class="confirmbuttons">
 166                      <tr>
 167                          <td align="$rightalignment" style="width:50%">
 168                          <form id="form" method="post" action="editcategories.php">
 169                          <div>
 170                          <input type="hidden" name="sesskey"     value="<?php echo sesskey(); ?>" />
 171                          <input type="hidden" name="id"          value="<?php p($cm->id) ?>" />
 172                          <input type="hidden" name="action"      value="delete" />
 173                          <input type="hidden" name="confirm"     value="1" />
 174                          <input type="hidden" name="mode"         value="<?php echo $mode ?>" />
 175                          <input type="hidden" name="hook"         value="<?php echo $hook ?>" />
 176                          <input type="submit" class="btn btn-primary" value=" <?php print_string("yes")?> " />
 177                          </div>
 178                          </form>
 179                          </td>
 180                          <td align="$leftalignment" style="width:50%">
 181  
 182  <?php
 183              unset($options);
 184              $options = array ("id" => $id);
 185              echo $OUTPUT->single_button(new moodle_url("editcategories.php", $options), get_string("no"));
 186              echo "</td></tr></table>";
 187              echo "</div>";
 188              echo $OUTPUT->box_end();
 189          }
 190      }
 191  
 192  } elseif ( $action == "add" ) {
 193      if ( $confirm ) {
 194          require_sesskey();
 195          $dupcategory = $DB->get_records_sql("SELECT * FROM {glossary_categories} WHERE ".$DB->sql_like('name','?', false)." AND glossaryid=?", array($name, $glossary->id));
 196          if ( $dupcategory ) {
 197              redirect("editcategories.php?id=$cm->id&amp;action=add&amp;name=$name", get_string("duplicatecategory", "glossary"), 2);
 198  
 199          } else {
 200              $action = "";
 201              $cat = new stdClass();
 202              $cat->name = $name;
 203              $cat->usedynalink = $usedynalink;
 204              $cat->glossaryid = $glossary->id;
 205  
 206              $cat->id = $DB->insert_record("glossary_categories", $cat);
 207              $event = \mod_glossary\event\category_created::create(array(
 208                  'context' => $context,
 209                  'objectid' => $cat->id
 210              ));
 211              $event->add_record_snapshot('glossary_categories', $cat);
 212              $event->add_record_snapshot('glossary', $glossary);
 213              $event->trigger();
 214  
 215              // Reset caches.
 216              \mod_glossary\local\concept_cache::reset_glossary($glossary);
 217          }
 218      } else {
 219          echo $OUTPUT->header();
 220          echo $OUTPUT->heading(format_string($glossary->name), 2);
 221          echo "<h3 class=\"main\">" . get_string("addcategory", "glossary"). "</h3>";
 222          $name="";
 223          require "editcategories.html";
 224      }
 225  }
 226  
 227  if ( $action ) {
 228      echo $OUTPUT->footer();
 229      die;
 230  }
 231  
 232  echo $OUTPUT->header();
 233  echo $OUTPUT->heading(format_string($glossary->name), 2);
 234  
 235  ?>
 236  
 237  <form method="post" action="editcategories.php">
 238  <table width="40%" class="boxaligncenter generalbox" cellpadding="5">
 239          <tr>
 240            <th style="width:90%" align="center">
 241            <?php p(get_string("categories","glossary")) ?></th>
 242            <th style="width:10%" align="center">
 243            <?php p(get_string("action")) ?></th>
 244          </tr>
 245          <tr><td style="width:100%" colspan="2">
 246  
 247  
 248  
 249  <?php
 250      $categories = $DB->get_records("glossary_categories", array("glossaryid"=>$glossary->id), "name ASC");
 251  
 252      if ( $categories ) {
 253          echo '<table width="100%">';
 254          foreach ($categories as $category) {
 255              $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid"=>$category->id));
 256  ?>
 257  
 258               <tr>
 259                 <td style="width:80%" align="$leftalignment">
 260                 <?php
 261                      echo "<span class=\"bold\">".format_string($category->name, true, $fmtoptions)."</span> <span>($num_entries " . get_string("entries","glossary") . ")</span>";
 262                 ?>
 263                 </td>
 264                 <td style="width:19%" align="center" class="action">
 265                 <?php
 266                  echo "<a href=\"editcategories.php?id=$cm->id&amp;action=delete&amp;mode=cat&amp;hook=$category->id\">" .
 267                       $OUTPUT->pix_icon('t/delete', get_string('delete')). "</a> ";
 268                  echo "<a href=\"editcategories.php?id=$cm->id&amp;action=edit&amp;mode=cat&amp;hook=$category->id\">" .
 269                       $OUTPUT->pix_icon('t/edit', get_string('edit')). "</a> ";
 270                 ?>
 271                 </td>
 272               </tr>
 273  
 274               <?php
 275  
 276            }
 277          echo '</table>';
 278       }
 279  ?>
 280  
 281          </td></tr>
 282          <tr>
 283          <td style="width:100%" colspan="2"  align="center">
 284              <?php
 285  
 286               $options['id'] = $cm->id;
 287               $options['action'] = "add";
 288  
 289               echo "<table class=\"editbuttons\" border=\"0\"><tr><td align=\"$rightalignment\">";
 290               echo $OUTPUT->single_button(new moodle_url("editcategories.php", $options), get_string("addcategory", "glossary"));
 291               echo "</td><td align=\"$leftalignment\">";
 292               unset($options['action']);
 293               $options['mode'] = 'cat';
 294               $options['hook'] = $hook;
 295               echo $OUTPUT->single_button(new moodle_url("view.php", $options), get_string("back","glossary"));
 296               echo "</td></tr>";
 297               echo "</table>";
 298  
 299              ?>
 300          </td>
 301          </tr>
 302          </table>
 303  
 304  
 305  </form>
 306  
 307  <?php
 308  echo $OUTPUT->footer();