[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/glossary/ -> editcategories.php (source)

   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      print_error('invalidcoursemodule');
  43  }
  44  
  45  if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
  46      print_error('coursemisconf');
  47  }
  48  
  49  if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
  50      print_error('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              print_error('invalidid', 'glossary');
  58          }
  59      } else {
  60          print_error('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($action). " " . core_text::strtolower(get_string("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              $action = "";
  99              $cat = new stdClass();
 100              $cat->id = $hook;
 101              $cat->name = $name;
 102              $cat->usedynalink = $usedynalink;
 103  
 104              $DB->update_record("glossary_categories", $cat);
 105              $event = \mod_glossary\event\category_updated::create(array(
 106                  'context' => $context,
 107                  'objectid' => $hook
 108              ));
 109              $cat->glossaryid = $glossary->id;
 110              $event->add_record_snapshot('glossary_categories', $cat);
 111              $event->add_record_snapshot('glossary', $glossary);
 112              $event->trigger();
 113  
 114              // Reset caches.
 115              \mod_glossary\local\concept_cache::reset_glossary($glossary);
 116  
 117          } else {
 118              echo $OUTPUT->header();
 119              echo $OUTPUT->heading(format_string($glossary->name), 2);
 120              echo $OUTPUT->heading(format_string(get_string("edit"). " " . get_string("category","glossary")), 3);
 121  
 122              $name = $category->name;
 123              $usedynalink = $category->usedynalink;
 124              require  "editcategories.html";
 125              echo $OUTPUT->footer();
 126              die;
 127          }
 128  
 129      } elseif ( $action == "delete" ) {
 130          if ( $confirm ) {
 131              $DB->delete_records("glossary_entries_categories", array("categoryid"=>$hook));
 132              $DB->delete_records("glossary_categories", array("id"=>$hook));
 133  
 134              $event = \mod_glossary\event\category_deleted::create(array(
 135                  'context' => $context,
 136                  'objectid' => $hook
 137              ));
 138              $event->add_record_snapshot('glossary_categories', $category);
 139              $event->add_record_snapshot('glossary', $glossary);
 140              $event->trigger();
 141  
 142              // Reset caches.
 143              \mod_glossary\local\concept_cache::reset_glossary($glossary);
 144  
 145              redirect("editcategories.php?id=$cm->id", get_string("categorydeleted", "glossary"), 2);
 146          } else {
 147              echo $OUTPUT->header();
 148              echo $OUTPUT->heading(format_string($glossary->name), 2);
 149              echo $OUTPUT->heading(format_string(get_string("delete"). " " . get_string("category","glossary")), 3);
 150  
 151              echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow');
 152              echo "<div class=\"boxaligncenter deletecatconfirm\">".format_string($category->name, true, $fmtoptions)."<br/>";
 153  
 154              $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid"=>$category->id));
 155              if ( $num_entries ) {
 156                  print_string("deletingnoneemptycategory","glossary");
 157              }
 158              echo "<p>";
 159              print_string("areyousuredelete","glossary");
 160              echo "</p>";
 161  ?>
 162  
 163                  <table border="0" width="100" class="confirmbuttons">
 164                      <tr>
 165                          <td align="$rightalignment" style="width:50%">
 166                          <form id="form" method="post" action="editcategories.php">
 167                          <div>
 168                          <input type="hidden" name="id"          value="<?php p($cm->id) ?>" />
 169                          <input type="hidden" name="action"      value="delete" />
 170                          <input type="hidden" name="confirm"     value="1" />
 171                          <input type="hidden" name="mode"         value="<?php echo $mode ?>" />
 172                          <input type="hidden" name="hook"         value="<?php echo $hook ?>" />
 173                          <input type="submit" value=" <?php print_string("yes")?> " />
 174                          </div>
 175                          </form>
 176                          </td>
 177                          <td align="$leftalignment" style="width:50%">
 178  
 179  <?php
 180              unset($options);
 181              $options = array ("id" => $id);
 182              echo $OUTPUT->single_button(new moodle_url("editcategories.php", $options), get_string("no"));
 183              echo "</td></tr></table>";
 184              echo "</div>";
 185              echo $OUTPUT->box_end();
 186          }
 187      }
 188  
 189  } elseif ( $action == "add" ) {
 190      if ( $confirm ) {
 191          $dupcategory = $DB->get_records_sql("SELECT * FROM {glossary_categories} WHERE ".$DB->sql_like('name','?', false)." AND glossaryid=?", array($name, $glossary->id));
 192          if ( $dupcategory ) {
 193              redirect("editcategories.php?id=$cm->id&amp;action=add&amp;name=$name", get_string("duplicatecategory", "glossary"), 2);
 194  
 195          } else {
 196              $action = "";
 197              $cat = new stdClass();
 198              $cat->name = $name;
 199              $cat->usedynalink = $usedynalink;
 200              $cat->glossaryid = $glossary->id;
 201  
 202              $cat->id = $DB->insert_record("glossary_categories", $cat);
 203              $event = \mod_glossary\event\category_created::create(array(
 204                  'context' => $context,
 205                  'objectid' => $cat->id
 206              ));
 207              $event->add_record_snapshot('glossary_categories', $cat);
 208              $event->add_record_snapshot('glossary', $glossary);
 209              $event->trigger();
 210  
 211              // Reset caches.
 212              \mod_glossary\local\concept_cache::reset_glossary($glossary);
 213          }
 214      } else {
 215          echo $OUTPUT->header();
 216          echo $OUTPUT->heading(format_string($glossary->name), 2);
 217          echo "<h3 class=\"main\">" . get_string("add"). " " . get_string("category","glossary"). "</h3>";
 218          $name="";
 219          require  "editcategories.html";
 220      }
 221  }
 222  
 223  if ( $action ) {
 224      echo $OUTPUT->footer();
 225      die;
 226  }
 227  
 228  echo $OUTPUT->header();
 229  echo $OUTPUT->heading(format_string($glossary->name), 2);
 230  
 231  ?>
 232  
 233  <form method="post" action="editcategories.php">
 234  <table width="40%" class="boxaligncenter generalbox" cellpadding="5">
 235          <tr>
 236            <th style="width:90%" align="center">
 237            <?php p(get_string("categories","glossary")) ?></th>
 238            <th style="width:10%" align="center">
 239            <?php p(get_string("action")) ?></th>
 240          </tr>
 241          <tr><td style="width:100%" colspan="2">
 242  
 243  
 244  
 245  <?php
 246      $categories = $DB->get_records("glossary_categories", array("glossaryid"=>$glossary->id), "name ASC");
 247  
 248      if ( $categories ) {
 249          echo '<table width="100%">';
 250          foreach ($categories as $category) {
 251              $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid"=>$category->id));
 252  ?>
 253  
 254               <tr>
 255                 <td style="width:80%" align="$leftalignment">
 256                 <?php
 257                      echo "<span class=\"bold\">".format_string($category->name, true, $fmtoptions)."</span> <span>($num_entries " . get_string("entries","glossary") . ")</span>";
 258                 ?>
 259                 </td>
 260                 <td style="width:19%" align="center" class="action">
 261                 <?php
 262                  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> ";
 263                  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>";
 264                 ?>
 265                 </td>
 266               </tr>
 267  
 268               <?php
 269  
 270            }
 271          echo '</table>';
 272       }
 273  ?>
 274  
 275          </td></tr>
 276          <tr>
 277          <td style="width:100%" colspan="2"  align="center">
 278              <?php
 279  
 280               $options['id'] = $cm->id;
 281               $options['action'] = "add";
 282  
 283               echo "<table class=\"editbuttons\" border=\"0\"><tr><td align=\"$rightalignment\">";
 284               echo $OUTPUT->single_button(new moodle_url("editcategories.php", $options), get_string("add") . " " . get_string("category","glossary"));
 285               echo "</td><td align=\"$leftalignment\">";
 286               unset($options['action']);
 287               $options['mode'] = 'cat';
 288               $options['hook'] = $hook;
 289               echo $OUTPUT->single_button(new moodle_url("view.php", $options), get_string("back","glossary"));
 290               echo "</td></tr>";
 291               echo "</table>";
 292  
 293              ?>
 294          </td>
 295          </tr>
 296          </table>
 297  
 298  
 299  </form>
 300  
 301  <?php
 302  echo $OUTPUT->footer();


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1