Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

   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  use core\output\checkbox_toggleall;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  require_once($CFG->libdir . '/tablelib.php');
  22  
  23  /**
  24   * Class core_tag_manage_table
  25   *
  26   * @package   core_tag
  27   * @copyright 2015 Marina Glancy
  28   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class core_tag_manage_table extends table_sql {
  31  
  32      /** @var int stores the total number of found tags */
  33      public $totalcount = null;
  34  
  35      /** @var int */
  36      protected $tagcollid;
  37  
  38      /**
  39       * Constructor
  40       *
  41       * @param int $tagcollid
  42       */
  43      public function __construct($tagcollid) {
  44          global $USER, $PAGE, $OUTPUT;
  45  
  46          parent::__construct('tag-management-list-'.$USER->id);
  47  
  48          $this->tagcollid = $tagcollid;
  49  
  50          $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);
  51          $page = optional_param('page', 0, PARAM_INT);
  52          $filter = optional_param('filter', '', PARAM_NOTAGS);
  53          $baseurl = new moodle_url('/tag/manage.php', array('tc' => $tagcollid,
  54              'perpage' => $perpage, 'page' => $page, 'filter' => $filter));
  55  
  56          $checkboxall = new checkbox_toggleall('tags-manage', true, [
  57              'id' => 'select-all-tags',
  58              'name' => 'select-all-tags',
  59              'checked' => false,
  60              'label' => get_string('selectall'),
  61              'labelclasses' => 'accesshide',
  62          ]);
  63  
  64          $tablecolumns = array('select', 'name', 'fullname', 'count', 'flag', 'timemodified', 'isstandard', 'controls');
  65          $tableheaders = array($OUTPUT->render($checkboxall),
  66                                get_string('name', 'tag'),
  67                                get_string('owner', 'tag'),
  68                                get_string('count', 'tag'),
  69                                get_string('flag', 'tag'),
  70                                get_string('timemodified', 'tag'),
  71                                get_string('standardtag', 'tag'),
  72                                '');
  73  
  74          $this->define_columns($tablecolumns);
  75          $this->define_headers($tableheaders);
  76          $this->define_baseurl($baseurl);
  77  
  78          $this->column_class('select', 'mdl-align col-select');
  79          $this->column_class('name', 'col-name');
  80          $this->column_class('owner', 'col-owner');
  81          $this->column_class('count', 'mdl-align col-count');
  82          $this->column_class('flag', 'mdl-align col-flag');
  83          $this->column_class('timemodified', 'col-timemodified');
  84          $this->column_class('isstandard', 'mdl-align col-isstandard');
  85          $this->column_class('controls', 'mdl-align col-controls');
  86  
  87          $this->sortable(true, 'flag', SORT_DESC);
  88          $this->no_sorting('select');
  89          $this->no_sorting('controls');
  90  
  91          $this->set_attribute('cellspacing', '0');
  92          $this->set_attribute('id', 'tag-management-list');
  93          $this->set_attribute('class', 'admintable generaltable tag-management-table');
  94  
  95          $totalcount = "SELECT COUNT(tg.id)
  96              FROM {tag} tg
  97              WHERE tg.tagcollid = :tagcollid";
  98          $params = array('tagcollid' => $this->tagcollid);
  99  
 100          $this->set_count_sql($totalcount, $params);
 101  
 102          $this->set_sql('', '', '', $params);
 103  
 104          $this->collapsible(true);
 105  
 106          $PAGE->requires->js_call_amd('core/tag', 'initManagePage', array());
 107  
 108      }
 109  
 110      /**
 111       * @return string sql to add to where statement.
 112       */
 113      function get_sql_where() {
 114          $filter = optional_param('filter', '', PARAM_NOTAGS);
 115          list($wsql, $wparams) = parent::get_sql_where();
 116          if ($filter !== '') {
 117              $wsql .= ($wsql ? ' AND ' : '') . 'tg.name LIKE :tagfilter';
 118              $wparams['tagfilter'] = '%' . $filter . '%';
 119          }
 120          return array($wsql, $wparams);
 121      }
 122  
 123      /**
 124       * Query the db. Store results in the table object for use by build_table.
 125       *
 126       * @param int $pagesize size of page for paginated displayed table.
 127       * @param bool $useinitialsbar do you want to use the initials bar. Bar
 128       * will only be used if there is a fullname column defined for the table.
 129       */
 130      public function query_db($pagesize, $useinitialsbar = true) {
 131          global $DB;
 132          $where = '';
 133          if (!$this->is_downloading()) {
 134              $grandtotal = $DB->count_records_sql($this->countsql, $this->countparams);
 135  
 136              list($wsql, $wparams) = $this->get_sql_where();
 137              if ($wsql) {
 138                  $this->countsql .= ' AND '.$wsql;
 139                  $this->countparams = array_merge($this->countparams, $wparams);
 140  
 141                  $where .= ' AND '.$wsql;
 142                  $this->sql->params = array_merge($this->sql->params, $wparams);
 143  
 144                  $total  = $DB->count_records_sql($this->countsql, $this->countparams);
 145              } else {
 146                  $total = $grandtotal;
 147              }
 148  
 149              $this->pagesize(min($pagesize, $total), $total);
 150              $this->totalcount = $total;
 151          }
 152  
 153          // Fetch the attempts.
 154          $sort = $this->get_sql_sort();
 155          if ($sort) {
 156              $sort .= ", tg.name";
 157          } else {
 158              $sort = "tg.name";
 159          }
 160  
 161          $userfieldsapi = \core_user\fields::for_name();
 162          $allusernames = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
 163          $sql = "
 164              SELECT tg.id, tg.name, tg.rawname, tg.isstandard, tg.flag, tg.timemodified,
 165                         u.id AS owner, $allusernames,
 166                         COUNT(ti.id) AS count, tg.tagcollid
 167              FROM {tag} tg
 168              LEFT JOIN {tag_instance} ti ON ti.tagid = tg.id
 169              LEFT JOIN {user} u ON u.id = tg.userid
 170                         WHERE tagcollid = :tagcollid $where
 171              GROUP BY tg.id, tg.name, tg.rawname, tg.isstandard, tg.flag, tg.timemodified,
 172                         u.id, $allusernames, tg.tagcollid
 173              ORDER BY $sort";
 174  
 175          if (!$this->is_downloading()) {
 176              $this->rawdata = $DB->get_records_sql($sql, $this->sql->params, $this->get_page_start(), $this->get_page_size());
 177          } else {
 178              $this->rawdata = $DB->get_records_sql($sql, $this->sql->params);
 179          }
 180      }
 181  
 182      /**
 183       * Override the table show_hide_link to not show for select column.
 184       *
 185       * @param string $column the column name, index into various names
 186       * @param int $index numerical index of the column
 187       * @return string HTML fragment
 188       */
 189      protected function show_hide_link($column, $index) {
 190          if ($index > 0) {
 191              return parent::show_hide_link($column, $index);
 192          }
 193          return '';
 194      }
 195  
 196      /**
 197       * Get any extra classes names to add to this row in the HTML
 198       *
 199       * @param stdClass $row array the data for this row.
 200       * @return string added to the class="" attribute of the tr.
 201       */
 202      public function get_row_class($row) {
 203          return $row->flag ? 'table-warning' : '';
 204      }
 205  
 206      /**
 207       * Column name
 208       *
 209       * @param stdClass $tag
 210       * @return string
 211       */
 212      public function col_name($tag) {
 213          global $OUTPUT;
 214          $tagoutput = new core_tag\output\tagname($tag);
 215          return $tagoutput->render($OUTPUT);
 216      }
 217  
 218      /**
 219       * Column flag
 220       *
 221       * @param stdClass $tag
 222       * @return string
 223       */
 224      public function col_flag($tag) {
 225          global $OUTPUT;
 226          $tagoutput = new core_tag\output\tagflag($tag);
 227          return $tagoutput->render($OUTPUT);
 228      }
 229  
 230      /**
 231       * Column fullname (user name)
 232       *
 233       * @param stdClass $tag
 234       * @return string
 235       */
 236      public function col_fullname($tag) {
 237          $params         = array('id' => $tag->owner);
 238          $ownerlink      = new moodle_url('/user/view.php', $params);
 239          $owner          = html_writer::link($ownerlink, fullname($tag));
 240          return $owner;
 241      }
 242  
 243      /**
 244       * Column time modified
 245       *
 246       * @param stdClass $tag
 247       * @return string
 248       */
 249      public function col_timemodified($tag) {
 250          return format_time(time() - $tag->timemodified);
 251      }
 252  
 253      /**
 254       * Column tag type
 255       *
 256       * @param stdClass $tag
 257       * @return string
 258       */
 259      public function col_isstandard($tag) {
 260          global $OUTPUT;
 261          $tagoutput = new core_tag\output\tagisstandard($tag);
 262          return $tagoutput->render($OUTPUT);
 263      }
 264  
 265      /**
 266       * Column select
 267       *
 268       * @param stdClass $tag
 269       * @return string
 270       */
 271      public function col_select($tag) {
 272          global $OUTPUT;
 273  
 274          $checkbox = new checkbox_toggleall('tags-manage', false, [
 275              'id' => 'tagselect' . $tag->id,
 276              'name' => 'tagschecked[]',
 277              'value' => $tag->id,
 278              'checked' => false,
 279              'label' => get_string('selecttag', 'tag', $tag->rawname),
 280              'labelclasses' => 'accesshide',
 281          ]);
 282  
 283          return $OUTPUT->render($checkbox);
 284      }
 285  
 286      /**
 287       * Column controls
 288       *
 289       * @param stdClass $tag
 290       * @return string
 291       */
 292      public function col_controls($tag) {
 293          global $OUTPUT, $PAGE;
 294          $o = '';
 295          // Edit.
 296          $url = new moodle_url('/tag/edit.php', array('id' => $tag->id, 'returnurl' => $PAGE->url->out_as_local_url()));
 297          $o .= $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('edittag', 'tag')));
 298          // Delete.
 299          $url = new moodle_url($this->baseurl, array('action' => 'delete',
 300              'tagid' => $tag->id, 'sesskey' => sesskey()));
 301          $o .= $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete', 'tag')),
 302                  null, array('class' => 'action-icon tagdelete'));
 303          return $o;
 304      }
 305  }