weekly release 3.9.2+
[moodle.git] / tag / classes / manage_table.php
blobed2e88abd850547a93d40b40b8b5a761f17526b7
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.
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/>.
17 /**
18 * Contains class core_tag_manage_table
20 * @package core_tag
21 * @copyright 2015 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->libdir . '/tablelib.php');
29 /**
30 * Class core_tag_manage_table
32 * @package core
33 * @copyright 2015 Marina Glancy
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_tag_manage_table extends table_sql {
38 /** @var int stores the total number of found tags */
39 public $totalcount = null;
41 /** @var int */
42 protected $tagcollid;
44 /**
45 * Constructor
47 * @param int $tagcollid
49 public function __construct($tagcollid) {
50 global $USER, $CFG, $PAGE;
51 parent::__construct('tag-management-list-'.$USER->id);
53 $this->tagcollid = $tagcollid;
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));
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 '');
71 $this->define_columns($tablecolumns);
72 $this->define_headers($tableheaders);
73 $this->define_baseurl($baseurl);
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');
84 $this->sortable(true, 'flag', SORT_DESC);
85 $this->no_sorting('select');
86 $this->no_sorting('controls');
88 $this->set_attribute('cellspacing', '0');
89 $this->set_attribute('id', 'tag-management-list');
90 $this->set_attribute('class', 'admintable generaltable tag-management-table');
92 $totalcount = "SELECT COUNT(tg.id)
93 FROM {tag} tg
94 WHERE tg.tagcollid = :tagcollid";
95 $params = array('tagcollid' => $this->tagcollid);
97 $this->set_count_sql($totalcount, $params);
99 $this->set_sql('', '', '', $params);
101 $this->collapsible(true);
103 $PAGE->requires->js_call_amd('core/tag', 'initManagePage', array());
108 * @return string sql to add to where statement.
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 . '%';
117 return array($wsql, $wparams);
121 * Query the db. Store results in the table object for use by build_table.
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.
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);
133 list($wsql, $wparams) = $this->get_sql_where();
134 if ($wsql) {
135 $this->countsql .= ' AND '.$wsql;
136 $this->countparams = array_merge($this->countparams, $wparams);
138 $where .= ' AND '.$wsql;
139 $this->sql->params = array_merge($this->sql->params, $wparams);
141 $total = $DB->count_records_sql($this->countsql, $this->countparams);
142 } else {
143 $total = $grandtotal;
146 $this->pagesize(min($pagesize, $total), $total);
147 $this->totalcount = $total;
150 // Fetch the attempts.
151 $sort = $this->get_sql_sort();
152 if ($sort) {
153 $sort .= ", tg.name";
154 } else {
155 $sort = "tg.name";
158 $allusernames = get_all_user_name_fields(true, 'u');
159 $sql = "
160 SELECT tg.id, tg.name, tg.rawname, tg.isstandard, tg.flag, tg.timemodified,
161 u.id AS owner, $allusernames,
162 COUNT(ti.id) AS count, tg.tagcollid
163 FROM {tag} tg
164 LEFT JOIN {tag_instance} ti ON ti.tagid = tg.id
165 LEFT JOIN {user} u ON u.id = tg.userid
166 WHERE tagcollid = :tagcollid $where
167 GROUP BY tg.id, tg.name, tg.rawname, tg.isstandard, tg.flag, tg.timemodified,
168 u.id, $allusernames, tg.tagcollid
169 ORDER BY $sort";
171 if (!$this->is_downloading()) {
172 $this->rawdata = $DB->get_records_sql($sql, $this->sql->params, $this->get_page_start(), $this->get_page_size());
173 } else {
174 $this->rawdata = $DB->get_records_sql($sql, $this->sql->params);
179 * Get any extra classes names to add to this row in the HTML
181 * @param stdClass $row array the data for this row.
182 * @return string added to the class="" attribute of the tr.
184 public function get_row_class($row) {
185 return $row->flag ? 'flagged-tag' : '';
189 * Column name
191 * @param stdClass $tag
192 * @return string
194 public function col_name($tag) {
195 global $OUTPUT;
196 $tagoutput = new core_tag\output\tagname($tag);
197 return $tagoutput->render($OUTPUT);
201 * Column flag
203 * @param stdClass $tag
204 * @return string
206 public function col_flag($tag) {
207 global $OUTPUT;
208 $tagoutput = new core_tag\output\tagflag($tag);
209 return $tagoutput->render($OUTPUT);
213 * Column fullname (user name)
215 * @param stdClass $tag
216 * @return string
218 public function col_fullname($tag) {
219 $params = array('id' => $tag->owner);
220 $ownerlink = new moodle_url('/user/view.php', $params);
221 $owner = html_writer::link($ownerlink, fullname($tag));
222 return $owner;
226 * Column time modified
228 * @param stdClass $tag
229 * @return string
231 public function col_timemodified($tag) {
232 return format_time(time() - $tag->timemodified);
236 * Column tag type
238 * @param stdClass $tag
239 * @return string
241 public function col_isstandard($tag) {
242 global $OUTPUT;
243 $tagoutput = new core_tag\output\tagisstandard($tag);
244 return $tagoutput->render($OUTPUT);
248 * Column select
250 * @param stdClass $tag
251 * @return string
253 public function col_select($tag) {
254 $id = "tagselect" . $tag->id;
255 return html_writer::label(get_string('selecttag', 'tag', $tag->rawname), $id,
256 false, array('class' => 'accesshide')).
257 html_writer::empty_tag('input', array('type' => 'checkbox',
258 'name' => 'tagschecked[]', 'value' => $tag->id, 'id' => $id));
262 * Column controls
264 * @param stdClass $tag
265 * @return string
267 public function col_controls($tag) {
268 global $OUTPUT, $PAGE;
269 $o = '';
270 // Edit.
271 $url = new moodle_url('/tag/edit.php', array('id' => $tag->id, 'returnurl' => $PAGE->url->out_as_local_url()));
272 $o .= $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('edittag', 'tag')));
273 // Delete.
274 $url = new moodle_url($this->baseurl, array('action' => 'delete',
275 'tagid' => $tag->id, 'sesskey' => sesskey()));
276 $o .= $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete', 'tag')),
277 null, array('class' => 'action-icon tagdelete'));
278 return $o;