[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/tag/ -> edit.php (source)

   1  <?php // $Id: edit.php,v 1.5.2.19 2008/07/07 07:40:35 scyrma Exp $
   2  
   3  require_once ('../config.php');
   4  require_once ('lib.php');
   5  require_once ('edit_form.php');
   6  
   7  require_js(array('yui_dom-event', 'yui_connection', 'yui_animation', 'yui_autocomplete'));
   8  
   9  require_login();
  10  
  11  if (empty($CFG->usetags)) {
  12      print_error('tagsaredisabled', 'tag');
  13  }
  14  
  15  $tag_id = optional_param('id', 0, PARAM_INT);
  16  $tag_name = optional_param('tag', '', PARAM_TAG);
  17  
  18  if ($tag_name) {
  19      $tag = tag_get('name', $tag_name, '*');
  20  } else if ($tag_id) {
  21      $tag = tag_get('id', $tag_id, '*');
  22  }
  23  
  24  if (empty($tag)) {
  25      redirect($CFG->wwwroot.'/tag/search.php');
  26  }
  27  
  28  $tagname = tag_display_name($tag);
  29  
  30  //Editing a tag requires moodle/tag:edit capability
  31  $systemcontext   = get_context_instance(CONTEXT_SYSTEM);
  32  require_capability('moodle/tag:edit', $systemcontext);
  33  
  34  // set the relatedtags field of the $tag object that will be passed to the form
  35  $tag->relatedtags = tag_get_related_tags_csv(tag_get_related_tags($tag->id, TAG_RELATED_MANUAL), TAG_RETURN_TEXT);
  36  
  37  if (can_use_html_editor()) {
  38      $options = new object();
  39      $options->smiley = false;
  40      $options->filter = false;
  41  
  42      // convert and remove any XSS
  43      $tag->description       = format_text($tag->description, $tag->descriptionformat, $options);
  44      $tag->descriptionformat = FORMAT_HTML;
  45  }
  46  
  47  $errorstring = '';
  48  
  49  $tagform = new tag_edit_form();
  50  if ( $tag->tagtype == 'official' ) {
  51      $tag->tagtype = '1';
  52  } else {
  53      $tag->tagtype = '0';
  54  }
  55  $tagform->set_data($tag);
  56  
  57  // If new data has been sent, update the tag record
  58  if ($tagnew = $tagform->get_data()) {
  59  
  60      tag_description_set($tag_id, stripslashes($tagnew->description), $tagnew->descriptionformat);
  61  
  62      if (has_capability('moodle/tag:manage', $systemcontext)) {
  63          if (($tag->tagtype != 'default') && (!isset($tagnew->tagtype) || ($tagnew->tagtype != '1'))) {
  64              tag_type_set($tag->id, 'default');
  65  
  66          } elseif (($tag->tagtype != 'official') && ($tagnew->tagtype == '1')) {
  67              tag_type_set($tag->id, 'official');
  68          }
  69      }
  70  
  71      if (!has_capability('moodle/tag:manage', $systemcontext) && !has_capability('moodle/tag:edit', $systemcontext)) {
  72          unset($tagnew->name);
  73          unset($tagnew->rawname);
  74  
  75      } else {  // They might be trying to change the rawname, make sure it's a change that doesn't affect name
  76          $tagnew->name = array_shift(tag_normalize($tagnew->rawname, TAG_CASE_LOWER));
  77  
  78          if ($tag->name != $tagnew->name) {  // The name has changed, let's make sure it's not another existing tag
  79              if (tag_get_id($tagnew->name)) {   // Something exists already, so flag an error
  80                  $errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag');
  81              }
  82          }
  83      }
  84  
  85      if (empty($errorstring)) {    // All is OK, let's save it
  86  
  87          $tagnew->timemodified = time();
  88  
  89          if (has_capability('moodle/tag:manage', $systemcontext)) {
  90              // rename tag
  91              if(!tag_rename($tag->id, $tagnew->rawname)) {
  92                  error('Error updating tag record');
  93              }
  94          }
  95      
  96          //updated related tags
  97          tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)));
  98          //print_object($tagnew); die();
  99      
 100          redirect($CFG->wwwroot.'/tag/index.php?tag='.rawurlencode($tag->name)); // must use $tag here, as the name isn't in the edit form
 101      }
 102  }
 103  
 104  
 105  $navlinks = array();
 106  $navlinks[] = array('name' => get_string('tags', 'tag'), 'link' => "{$CFG->wwwroot}/tag/search.php", 'type' => '');
 107  $navlinks[] = array('name' => $tagname, 'link' => '', 'type' => '');
 108  
 109  $navigation = build_navigation($navlinks);
 110  print_header_simple(get_string('tag', 'tag') . ' - '. $tagname, '', $navigation);
 111  
 112  print_heading($tagname, '', 2);
 113  
 114  if (!empty($errorstring)) {
 115      notify($errorstring);
 116  }
 117  
 118  $tagform->display();
 119  
 120  if (ajaxenabled()) {
 121  ?>
 122  
 123  <script type="text/javascript">
 124  
 125  // An XHR DataSource
 126  var myServer = "./tag_autocomplete.php";
 127  var myDataSource = new YAHOO.widget.DS_XHR(myServer, ["\n", "\t"]);
 128  myDataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
 129  myDataSource.maxCacheEntries = 60;
 130  myDataSource.queryMatchSubset = true;
 131  
 132  var myAutoComp = new YAHOO.widget.AutoComplete("id_relatedtags","relatedtags-autocomplete", myDataSource);
 133  myAutoComp.delimChar = ",";
 134  myAutoComp.maxResultsDisplayed = 20;
 135  myAutoComp.minQueryLength = 2;
 136  myAutoComp.allowBrowserAutocomplete = false;
 137  myAutoComp.formatResult = function(aResultItem, sQuery) {
 138      return aResultItem[1];
 139  }
 140  </script>
 141  
 142  <?php
 143  }
 144  print_footer();
 145  
 146  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7