Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
/tag/ -> manage.php (source)

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Managing tags, tag areas and tags collections
  19   *
  20   * @package    core_tag
  21   * @copyright  2007 Luiz Cruz <luiz.laydner@gmail.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once('../config.php');
  26  require_once($CFG->libdir.'/tablelib.php');
  27  require_once ('lib.php');
  28  require_once($CFG->libdir.'/adminlib.php');
  29  
  30  define('SHOW_ALL_PAGE_SIZE', 50000);
  31  define('DEFAULT_PAGE_SIZE', 30);
  32  
  33  $tagschecked = optional_param_array('tagschecked', array(), PARAM_INT);
  34  $tagid       = optional_param('tagid', null, PARAM_INT);
  35  $isstandard  = optional_param('isstandard', null, PARAM_INT);
  36  $action      = optional_param('action', '', PARAM_ALPHA);
  37  $perpage     = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);
  38  $page        = optional_param('page', 0, PARAM_INT);
  39  $tagcollid   = optional_param('tc', 0, PARAM_INT);
  40  $tagareaid   = optional_param('ta', null, PARAM_INT);
  41  $filter      = optional_param('filter', '', PARAM_NOTAGS);
  42  
  43  $params = array();
  44  if ($perpage != DEFAULT_PAGE_SIZE) {
  45      $params['perpage'] = $perpage;
  46  }
  47  if ($page > 0) {
  48      $params['page'] = $page;
  49  }
  50  if ($tagcollid) {
  51      $params['tc'] = $tagcollid;
  52  }
  53  if ($filter !== '') {
  54      $params['filter'] = $filter;
  55  }
  56  
  57  admin_externalpage_setup('managetags', '', $params, '', array('pagelayout' => 'report'));
  58  
  59  if (empty($CFG->usetags)) {
  60      print_error('tagsaredisabled', 'tag');
  61  }
  62  
  63  $tagobject = null;
  64  if ($tagid) {
  65      $tagobject = core_tag_tag::get($tagid, '*', MUST_EXIST);
  66      $tagcollid = $tagobject->tagcollid;
  67  }
  68  $tagcoll = core_tag_collection::get_by_id($tagcollid);
  69  $tagarea = core_tag_area::get_by_id($tagareaid);
  70  $manageurl = new moodle_url('/tag/manage.php');
  71  if ($tagcoll) {
  72      // We are inside a tag collection - add it to the breadcrumb.
  73      $PAGE->navbar->add(core_tag_collection::display_name($tagcoll),
  74              new moodle_url($manageurl, array('tc' => $tagcoll->id)));
  75  }
  76  
  77  $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
  78  
  79  switch($action) {
  80  
  81      case 'colladd':
  82          require_sesskey();
  83          $name = required_param('name', PARAM_NOTAGS);
  84          $searchable = optional_param('searchable', false, PARAM_BOOL);
  85          core_tag_collection::create(array('name' => $name, 'searchable' => $searchable));
  86          redirect($manageurl);
  87          break;
  88  
  89      case 'colldelete':
  90          if ($tagcoll && !$tagcoll->component) {
  91              require_sesskey();
  92              core_tag_collection::delete($tagcoll);
  93              \core\notification::success(get_string('changessaved', 'core_tag'));
  94          }
  95          redirect($manageurl);
  96          break;
  97  
  98      case 'collmoveup':
  99          if ($tagcoll) {
 100              require_sesskey();
 101              core_tag_collection::change_sortorder($tagcoll, -1);
 102              redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
 103          }
 104          redirect($manageurl);
 105          break;
 106  
 107      case 'collmovedown':
 108          if ($tagcoll) {
 109              require_sesskey();
 110              core_tag_collection::change_sortorder($tagcoll, 1);
 111              redirect($manageurl, get_string('changessaved', 'core_tag'), null, \core\output\notification::NOTIFY_SUCCESS);
 112          }
 113          redirect($manageurl);
 114          break;
 115  
 116      case 'delete':
 117          if ($tagid) {
 118              require_sesskey();
 119              core_tag_tag::delete_tags(array($tagid));
 120              \core\notification::success(get_string('deleted', 'core_tag'));
 121          }
 122          redirect($PAGE->url);
 123          break;
 124  
 125      case 'bulk':
 126          if (optional_param('bulkdelete', null, PARAM_RAW) !== null) {
 127              if ($tagschecked) {
 128                  require_sesskey();
 129                  core_tag_tag::delete_tags($tagschecked);
 130                  \core\notification::success(get_string('deleted', 'core_tag'));
 131              }
 132              redirect($PAGE->url);
 133          } else if (optional_param('bulkcombine', null, PARAM_RAW) !== null) {
 134              $tags = core_tag_tag::get_bulk($tagschecked, '*');
 135              if (count($tags) > 1) {
 136                  require_sesskey();
 137                  if (($maintag = optional_param('maintag', 0, PARAM_INT)) && array_key_exists($maintag, $tags)) {
 138                      $tag = $tags[$maintag];
 139                  } else {
 140                      $tag = array_shift($tags);
 141                  }
 142                  $tag->combine_tags($tags);
 143                  \core\notification::success(get_string('combined', 'core_tag'));
 144              }
 145              redirect($PAGE->url);
 146          }
 147          break;
 148  
 149      case 'renamecombine':
 150          // Allows to rename the tag and if the tag with the new name already exists these tags will be combined.
 151          if ($tagid && ($newname = required_param('newname', PARAM_TAG))) {
 152              require_sesskey();
 153              $tag = core_tag_tag::get($tagid, '*', MUST_EXIST);
 154              $targettag = core_tag_tag::get_by_name($tag->tagcollid, $newname, '*');
 155              if ($targettag) {
 156                  $targettag->combine_tags(array($tag));
 157                  \core\notification::success(get_string('combined', 'core_tag'));
 158              } else {
 159                  $tag->update(array('rawname' => $newname));
 160                  \core\notification::success(get_string('changessaved', 'core_tag'));
 161              }
 162          }
 163          redirect($PAGE->url);
 164          break;
 165  
 166      case 'addstandardtag':
 167          require_sesskey();
 168          $tagobjects = array();
 169          if ($tagcoll) {
 170              $tagslist = optional_param('tagslist', '', PARAM_RAW);
 171              $newtags = preg_split('/\s*,\s*/', trim($tagslist), -1, PREG_SPLIT_NO_EMPTY);
 172              $tagobjects = core_tag_tag::create_if_missing($tagcoll->id, $newtags, true);
 173          }
 174          foreach ($tagobjects as $tagobject) {
 175              if (!$tagobject->isstandard) {
 176                  $tagobject->update(array('isstandard' => 1));
 177              }
 178          }
 179          redirect($PAGE->url, $tagobjects ? get_string('added', 'core_tag') : null,
 180                  null, \core\output\notification::NOTIFY_SUCCESS);
 181          break;
 182  }
 183  
 184  echo $OUTPUT->header();
 185  
 186  if (!$tagcoll) {
 187      // Tag collection is not specified. Display the overview of tag collections and tag areas.
 188      $tagareastable = new core_tag_areas_table($manageurl);
 189      $colltable = new core_tag_collections_table($manageurl);
 190  
 191      echo $OUTPUT->heading(get_string('tagcollections', 'core_tag') . $OUTPUT->help_icon('tagcollection', 'tag'), 3);
 192      echo html_writer::table($colltable);
 193      $url = new moodle_url($manageurl, array('action' => 'colladd'));
 194      echo html_writer::div(html_writer::link('#', get_string('addtagcoll', 'tag'), array('data-url' => $url)),
 195              'mdl-right addtagcoll');
 196  
 197      echo $OUTPUT->heading(get_string('tagareas', 'core_tag'), 3);
 198      echo html_writer::table($tagareastable);
 199  
 200      $PAGE->requires->js_call_amd('core/tag', 'initManageCollectionsPage', array());
 201  
 202      echo $OUTPUT->footer();
 203      exit;
 204  }
 205  
 206  // Tag collection is specified. Manage tags in this collection.
 207  echo $OUTPUT->heading(core_tag_collection::display_name($tagcoll));
 208  
 209  $hiddenfields = [
 210      (object) ['type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid],
 211      (object) ['type' => 'hidden', 'name' => 'perpage', 'value' => $perpage]
 212  ];
 213  
 214  $otherfields = '';
 215  if ($filter !== '') {
 216      $otherfields = html_writer::link(new moodle_url($PAGE->url, ['filter' => null]),
 217          get_string('resetfilter', 'tag'));
 218  }
 219  
 220  $data = [
 221      'action' => new moodle_url('/tag/manage.php'),
 222      'hiddenfields' => $hiddenfields,
 223      'inputname' => 'filter',
 224      'searchstring' => get_string('search'),
 225      'query' => s($filter),
 226      'extraclasses' => 'mb-2',
 227      'otherfields' => $otherfields
 228  ];
 229  echo $OUTPUT->render_from_template('core/search_input', $data);
 230  
 231  // Link to add an standard tags.
 232  $img = $OUTPUT->pix_icon('t/add', '');
 233  echo '<div class="addstandardtags visibleifjs">' .
 234      html_writer::link('#', $img . get_string('addotags', 'tag'), array('data-action' => 'addstandardtag')) .
 235      '</div>';
 236  
 237  $table = new core_tag_manage_table($tagcollid);
 238  echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">';
 239  echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'tc', 'value' => $tagcollid));
 240  echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
 241  echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'bulk'));
 242  echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'perpage', 'value' => $perpage));
 243  echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'page', 'value' => $page));
 244  echo $table->out($perpage, true);
 245  
 246  if ($table->rawdata) {
 247      echo html_writer::start_tag('p');
 248      echo html_writer::tag('button', get_string('deleteselected', 'tag'),
 249              array('id' => 'tag-management-delete', 'type' => 'submit',
 250                    'class' => 'tagdeleteselected btn btn-secondary', 'name' => 'bulkdelete'));
 251      echo html_writer::tag('button', get_string('combineselected', 'tag'),
 252          array('id' => 'tag-management-combine', 'type' => 'submit',
 253                'class' => 'tagcombineselected btn btn-secondary', 'name' => 'bulkcombine'));
 254      echo html_writer::end_tag('p');
 255  }
 256  echo '</form>';
 257  
 258  $totalcount = $table->totalcount;
 259  if ($perpage == SHOW_ALL_PAGE_SIZE) {
 260      echo html_writer::start_tag('div', array('id' => 'showall'));
 261      $params = array('perpage' => DEFAULT_PAGE_SIZE, 'page' => 0);
 262      $url = new moodle_url($PAGE->url, $params);
 263      echo html_writer::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
 264      echo html_writer::end_tag('div');
 265  } else if ($totalcount > 0 and $perpage < $totalcount) {
 266      echo html_writer::start_tag('div', array('id' => 'showall'));
 267      $params = array('perpage' => SHOW_ALL_PAGE_SIZE, 'page' => 0);
 268      $url = new moodle_url($PAGE->url, $params);
 269      echo html_writer::link($url, get_string('showall', '', $totalcount));
 270      echo html_writer::end_tag('div');
 271  }
 272  
 273  echo $OUTPUT->footer();