Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/tag/ -> edit.php (source)

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

   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  /**
  19   * @package    core_tag
  20   * @category   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 ('lib.php');
  27  require_once ('edit_form.php');
  28  
  29  $tagid = optional_param('id', 0, PARAM_INT);
  30  $tagname = optional_param('tag', '', PARAM_TAG);
  31  $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
  32  
  33  require_login();
  34  
  35  if (empty($CFG->usetags)) {
  36      throw new \moodle_exception('tagsaredisabled', 'tag');
  37  }
  38  
  39  //Editing a tag requires moodle/tag:edit capability
  40  $systemcontext   = context_system::instance();
  41  require_capability('moodle/tag:edit', $systemcontext);
  42  
  43  if ($tagname) {
  44      $tagcollid = optional_param('tc', 0, PARAM_INT);
  45      if (!$tagcollid) {
  46          // Tag name specified but tag collection was not. Try to guess it.
  47          $tags = core_tag_tag::guess_by_name($tagname, '*');
  48          if (count($tags) > 1) {
  49              // This tag was found in more than one collection, redirect to search.
  50              redirect(new moodle_url('/tag/search.php', array('tag' => $tagname)));
  51          } else if (count($tags) == 1) {
  52              $tag = reset($tags);
  53          }
  54      } else {
  55          if (!$tag = core_tag_tag::get_by_name($tagcollid, $tagname, '*')) {
  56              redirect(new moodle_url('/tag/search.php', array('tagcollid' => $tagcollid)));
  57          }
  58      }
  59  } else if ($tagid) {
  60      $tag = core_tag_tag::get($tagid, '*');
  61  }
  62  
  63  if (empty($tag)) {
  64      redirect(new moodle_url('/tag/search.php'));
  65  }
  66  
  67  $PAGE->set_url($tag->get_view_url());
  68  $PAGE->set_subpage($tag->id);
  69  $PAGE->set_context($systemcontext);
  70  $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
  71  $PAGE->set_pagelayout('standard');
  72  
  73  $tagname = $tag->get_display_name();
  74  $tagcollid = $tag->tagcollid;
  75  
  76  // set the relatedtags field of the $tag object that will be passed to the form
  77  $data = $tag->to_object();
  78  $data->relatedtags = core_tag_tag::get_item_tags_array('core', 'tag', $tag->id);
  79  
  80  $options = new stdClass();
  81  $options->smiley = false;
  82  $options->filter = false;
  83  
  84  // convert and remove any XSS
  85  $data->description       = format_text($tag->description, $tag->descriptionformat, $options);
  86  $data->descriptionformat = FORMAT_HTML;
  87  
  88  $errorstring = '';
  89  
  90  $editoroptions = array(
  91      'maxfiles'  => EDITOR_UNLIMITED_FILES,
  92      'maxbytes'  => $CFG->maxbytes,
  93      'trusttext' => false,
  94      'context'   => $systemcontext,
  95      'subdirs'   => file_area_contains_subdirs($systemcontext, 'tag', 'description', $tag->id),
  96  );
  97  $data = file_prepare_standard_editor($data, 'description', $editoroptions, $systemcontext, 'tag', 'description', $data->id);
  98  
  99  $tagform = new tag_edit_form(null, array('editoroptions' => $editoroptions, 'tag' => $tag));
 100  $data->returnurl = $returnurl;
 101  
 102  $tagform->set_data($data);
 103  
 104  if ($tagform->is_cancelled()) {
 105      redirect($returnurl ? new moodle_url($returnurl) : $tag->get_view_url());
 106  } else if ($tagnew = $tagform->get_data()) {
 107      // If new data has been sent, update the tag record.
 108      $updatedata = array();
 109  
 110      if (has_capability('moodle/tag:manage', $systemcontext)) {
 111          $updatedata['isstandard'] = empty($tagnew->isstandard) ? 0 : 1;
 112          $updatedata['rawname'] = $tagnew->rawname;
 113      }
 114  
 115      $tagnew = file_postupdate_standard_editor($tagnew, 'description', $editoroptions,
 116              $systemcontext, 'tag', 'description', $tag->id);
 117      $updatedata['description'] = $tagnew->description;
 118      $updatedata['descriptionformat'] = $tagnew->descriptionformat;
 119  
 120      // Update name, description and whether it is a standard tag.
 121      $tag->update($updatedata);
 122  
 123      // Updated related tags.
 124      $tag->set_related_tags($tagnew->relatedtags);
 125  
 126      redirect($returnurl ? new moodle_url($returnurl) : $tag->get_view_url());
 127  }
 128  
 129  navigation_node::override_active_url(new moodle_url('/tag/search.php'));
 130  $PAGE->navbar->add($tagname);
 131  $PAGE->navbar->add(get_string('edit'));
 132  $PAGE->set_title(get_string('tag', 'tag') . ' - '. $tagname);
 133  $PAGE->set_heading($COURSE->fullname);
 134  echo $OUTPUT->header();
 135  echo $OUTPUT->heading($tagname, 2);
 136  
 137  if (!empty($errorstring)) {
 138      echo $OUTPUT->notification($errorstring);
 139  }
 140  
 141  $tagform->display();
 142  
 143  echo $OUTPUT->footer();