Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

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