Search moodle.org's
Developer Documentation


/tag/ -> manage.php (source)
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package    core
  20   * @subpackage 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  $newnames    = optional_param_array('newname', array(), PARAM_TAG);
  35  $tagtypes    = optional_param_array('tagtypes', array(), PARAM_ALPHA);
  36  $action      = optional_param('action', '', PARAM_ALPHA);
  37  $perpage     = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);
  38  
  39  require_login();
  40  
  41  if (empty($CFG->usetags)) {
  42      print_error('tagsaredisabled', 'tag');
  43  }
  44  
  45  $params = array();
  46  if ($perpage != DEFAULT_PAGE_SIZE) {
  47      $params['perpage'] = $perpage;
  48  }
  49  admin_externalpage_setup('managetags', '', $params, '', array('pagelayout' => 'standard'));
  50  
  51  $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
  52  
  53  echo $OUTPUT->header();
  54  
  55  $err_notice = '';
  56  $notice = '';
  57  
  58  // get all the possible tag types from db
  59  $existing_tagtypes = array();
  60  if ($ptypes = $DB->get_records_sql("SELECT DISTINCT(tagtype) FROM {tag}")) {
  61      foreach ($ptypes as $ptype) {
  62          $existing_tagtypes[$ptype->tagtype] = $ptype->tagtype;
  63      }
  64  }
  65  $existing_tagtypes['official'] = get_string('tagtype_official', 'tag');
  66  $existing_tagtypes['default'] = get_string('tagtype_default', 'tag');
  67  
  68  switch($action) {
  69  
  70      case 'delete':
  71          if (!data_submitted() or !confirm_sesskey()) {
  72              break;
  73          }
  74  
  75          $str_tagschecked = implode(', ', tag_get_name($tagschecked));
  76          tag_delete($tagschecked);
  77          $notice = $str_tagschecked.' --  '.get_string('deleted','tag');
  78          break;
  79  
  80      case 'reset':
  81          if (!data_submitted() or !confirm_sesskey()) {
  82              break;
  83          }
  84          $str_tagschecked = implode(', ', tag_get_name($tagschecked));
  85          tag_unset_flag($tagschecked);
  86          $notice = $str_tagschecked .' -- '. get_string('reset', 'tag');
  87          break;
  88  
  89      case 'changetype':
  90          if (!data_submitted() or !confirm_sesskey()) {
  91              break;
  92          }
  93  
  94          $changed = array();
  95          foreach ($tagschecked as $tag_id) {
  96              if (!array_key_exists($tagtypes[$tag_id], $existing_tagtypes)) {
  97                  //can not add new types here!!
  98                  continue;
  99              }
 100  
 101              // update tag type;
 102              if (tag_type_set($tag_id, $tagtypes[$tag_id])) {
 103                  $changed[] = $tag_id;
 104              }
 105          }
 106  
 107          if (!empty($changed)) {
 108              $str_changed = implode(', ', tag_get_name($changed));
 109              $notice = $str_changed .' --  '. get_string('typechanged','tag');
 110          }
 111          break;
 112  
 113      case 'changename':
 114          if (!data_submitted() or !confirm_sesskey()) {
 115              break;
 116          }
 117  
 118          $tags_names_changed = array();
 119          foreach ($tagschecked as $tag_id) {
 120              if ($newnames[$tag_id] != '') {
 121                  if (! $tags_names_updated[] = tag_rename($tag_id, $newnames[$tag_id]) ) {
 122                      // if tag already exists, or is not a valid tag name, etc.
 123                      $err_notice .= $newnames[$tag_id]. '-- ' . get_string('namesalreadybeeingused','tag').'<br />';
 124                  } else {
 125                      $tags_names_changed[$tag_id] = $newnames[$tag_id];
 126                  }
 127              }
 128          }
 129  
 130          //notice to inform what tags had their names effectively updated
 131          if ($tags_names_changed){
 132              $notice = implode($tags_names_changed, ', ');
 133              $notice .= ' -- ' . get_string('updated','tag');
 134          }
 135          break;
 136      case 'addofficialtag':
 137          if (!data_submitted() or !confirm_sesskey()) {
 138              break;
 139          }
 140  
 141          $new_otags = explode(',', optional_param('otagsadd', '', PARAM_TAG));
 142          $notice = '';
 143          foreach ( $new_otags as $new_otag ) {
 144              if ( $new_otag_id = tag_get_id($new_otag) ) {
 145                  // tag exists, change the type
 146                  tag_type_set($new_otag_id, 'official');
 147              } else {
 148                  require_capability('moodle/tag:create', context_system::instance());
 149                  tag_add($new_otag, 'official');
 150              }
 151              $notice .= get_string('addedotag', 'tag', $new_otag) .' ';
 152          }
 153          break;
 154  }
 155  
 156  if ($err_notice) {
 157      echo $OUTPUT->notification($err_notice, 'notifyproblem');
 158  }
 159  if ($notice) {
 160      echo $OUTPUT->notification($notice, 'notifysuccess');
 161  }
 162  
 163  // small form to add an official tag
 164  print('<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">');
 165  print('<input type="hidden" name="action" value="addofficialtag" />');
 166  print('<div class="tag-management-form generalbox"><label class="accesshide" for="id_otagsadd">'. get_string('addotags', 'tag') .'</label>'.
 167      '<input name="otagsadd" id="id_otagsadd" type="text" />'.
 168      '<input type="hidden" name="sesskey" value="'.sesskey().'">'.
 169      '<input name="addotags" value="'. get_string('addotags', 'tag') .'" onclick="skipClientValidation = true;" id="id_addotags" type="submit" />'.
 170      '</div>');
 171  print('</form>');
 172  
 173  //setup table
 174  
 175  $tablecolumns = array('id', 'name', 'fullname', 'count', 'flag', 'timemodified', 'rawname', 'tagtype', '');
 176  $tableheaders = array(get_string('id', 'tag'),
 177                        get_string('name', 'tag'),
 178                        get_string('owner', 'tag'),
 179                        get_string('count', 'tag'),
 180                        get_string('flag', 'tag'),
 181                        get_string('timemodified', 'tag'),
 182                        get_string('newname', 'tag'),
 183                        get_string('tagtype', 'tag'),
 184                        get_string('select', 'tag'));
 185  
 186  $table = new flexible_table('tag-management-list-'.$USER->id);
 187  
 188  $baseurl = $CFG->wwwroot.'/tag/manage.php?perpage='.$perpage;
 189  
 190  $table->define_columns($tablecolumns);
 191  $table->define_headers($tableheaders);
 192  $table->define_baseurl($baseurl);
 193  
 194  $table->sortable(true, 'flag', SORT_DESC);
 195  
 196  $table->set_attribute('cellspacing', '0');
 197  $table->set_attribute('id', 'tag-management-list');
 198  $table->set_attribute('class', 'admintable generaltable');
 199  
 200  $table->set_control_variables(array(
 201  TABLE_VAR_SORT    => 'ssort',
 202  TABLE_VAR_HIDE    => 'shide',
 203  TABLE_VAR_SHOW    => 'sshow',
 204  TABLE_VAR_IFIRST  => 'sifirst',
 205  TABLE_VAR_ILAST   => 'silast',
 206  TABLE_VAR_PAGE    => 'spage'
 207  ));
 208  
 209  $table->setup();
 210  
 211  if ($table->get_sql_sort()) {
 212      $sort = 'ORDER BY '. $table->get_sql_sort();
 213  } else {
 214      $sort = '';
 215  }
 216  
 217  list($where, $params) = $table->get_sql_where();
 218  if ($where) {
 219      $where = 'WHERE '. $where;
 220  }
 221  
 222  $allusernames = get_all_user_name_fields(true, 'u');
 223  $query = "
 224          SELECT tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
 225                 u.id AS owner, $allusernames,
 226                 COUNT(ti.id) AS count
 227            FROM {tag} tg
 228       LEFT JOIN {tag_instance} ti ON ti.tagid = tg.id
 229       LEFT JOIN {user} u ON u.id = tg.userid
 230                 $where
 231        GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
 232                 u.id, $allusernames
 233           $sort";
 234  
 235  $totalcount = $DB->count_records_sql("
 236          SELECT COUNT(DISTINCT(tg.id))
 237            FROM {tag} tg
 238       LEFT JOIN {user} u ON u.id = tg.userid
 239          $where", $params);
 240  
 241  $table->initialbars(true); // always initial bars
 242  $table->pagesize($perpage, $totalcount);
 243  
 244  //@todo MDL-35474 convert to mform
 245  echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php"><div>';
 246  
 247  //retrieve tags from DB
 248  if ($tagrecords = $DB->get_records_sql($query, $params, $table->get_page_start(),  $table->get_page_size())) {
 249  
 250      //populate table with data
 251      foreach ($tagrecords as $tag) {
 252          $id             = $tag->id;
 253          $params         = array('id' => $tag->id);
 254          $taglink        = new moodle_url($CFG->wwwroot . '/tag/index.php', $params);
 255          $name           = html_writer::link($taglink, tag_display_name($tag));
 256          $params         = array('id' => $tag->owner);
 257          $ownerlink      = new moodle_url($CFG->wwwroot . '/user/view.php', $params);
 258          $owner          = html_writer::link($ownerlink, fullname($tag));
 259          $count          = $tag->count;
 260          $flag           = $tag->flag;
 261          $timemodified   = format_time(time() - $tag->timemodified);
 262          $checkbox       = html_writer::tag('input', '', array('type' => 'checkbox', 'name' => 'tagschecked[]', 'value' => $tag->id));
 263          $attrs          = array('type' => 'text', 'id' => 'newname_' . $tag->id, 'name' => 'newname['.$tag->id.']');
 264          $text           = html_writer::label(get_string('newname', 'tag'), 'newname_' . $tag->id, false, array('class' => 'accesshide'));
 265          $text          .= html_writer::empty_tag('input', $attrs);
 266          $tagtype        = html_writer::label(get_string('tagtype', 'tag'), 'menutagtypes'. $tag->id, false, array('class' => 'accesshide'));
 267          $tagtype       .= html_writer::select($existing_tagtypes, 'tagtypes['.$tag->id.']', $tag->tagtype, false, array('id' => 'menutagtypes'. $tag->id));
 268  
 269          //if the tag if flagged, highlight it
 270          if ($tag->flag > 0) {
 271              $id = html_writer::tag('span', $id, array('class' => 'flagged-tag'));
 272              $name = html_writer::tag('span', $name, array('class' => 'flagged-tag'));
 273              $owner = html_writer::tag('span', $owner, array('class' => 'flagged-tag'));
 274              $count = html_writer::tag('span', $count, array('class' => 'flagged-tag'));
 275              $flag = html_writer::tag('span', $flag, array('class' => 'flagged-tag'));
 276              $timemodified = html_writer::tag('span', $timemodified, array('class' => 'flagged-tag'));
 277              $tagtype = html_writer::tag('span', $tagtype, array('class' => 'flagged-tag'));
 278          }
 279  
 280          $data = array($id, $name, $owner, $count, $flag, $timemodified, $text, $tagtype, $checkbox);
 281  
 282          $table->add_data($data);
 283      }
 284  
 285      echo html_writer::empty_tag('input', array('type' => 'button', 'onclick' => 'checkall()', 'value' => get_string('selectall')));
 286      echo html_writer::empty_tag('input', array('type' => 'button', 'onclick' => 'checknone()', 'value' => get_string('deselectall')));
 287      echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
 288      echo html_writer::empty_tag('br');
 289      echo html_writer::empty_tag('br');
 290  
 291      echo html_writer::label(get_string('withselectedtags', 'tag'), 'menuformaction', false, array('class' => 'accesshide'));
 292      $options = array('' => get_string('withselectedtags', 'tag'),
 293                       'reset' => get_string('resetflag', 'tag'),
 294                       'delete' => get_string('delete', 'tag'),
 295                       'changetype' => get_string('changetype', 'tag'),
 296                       'changename' => get_string('changename', 'tag'));
 297      echo html_writer::select($options, 'action', '', array(), array('id' => 'menuformaction'));
 298  
 299      echo html_writer::tag('button', get_string('ok'), array('id' => 'tag-management-submit', 'type' => 'submit'));
 300  }
 301  
 302  $table->print_html();
 303  
 304  //@todo MDL-35474 convert to mform
 305  echo '</div></form>';
 306  
 307  if ($perpage == SHOW_ALL_PAGE_SIZE) {
 308      echo html_writer::start_tag('div', array('id' => 'showall'));
 309      $params = array('perpage' => DEFAULT_PAGE_SIZE);
 310      $url = new moodle_url($baseurl, $params);
 311      echo html_writer::link($url, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
 312      echo html_writer::end_tag('div');
 313  
 314  } else if ($totalcount > 0 and $perpage < $totalcount) {
 315      echo html_writer::start_tag('div', array('id' => 'showall'));
 316      $params = array('perpage' => SHOW_ALL_PAGE_SIZE);
 317      $url = new moodle_url($baseurl, $params);
 318      echo html_writer::link($url, get_string('showall', '', $totalcount));
 319      echo html_writer::end_tag('div');
 320  }
 321  
 322  echo html_writer::empty_tag('br');
 323  
 324  echo $OUTPUT->footer();

Search This Site: