MDL-50182 core: add 3.0 environmental requirements
[moodle.git] / tag / classes / manage_table.php
blob61d3b19375d26052e11e91ea677f985960da408f
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 /**
42 * Constructor
44 public function __construct() {
45 global $USER, $CFG, $PAGE;
46 parent::__construct('tag-management-list-'.$USER->id);
48 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);
49 $page = optional_param('page', 0, PARAM_INT);
50 $baseurl = new moodle_url('/tag/manage.php', array('perpage' => $perpage, 'page' => $page));
52 $tablecolumns = array('select', 'name', 'fullname', 'count', 'flag', 'timemodified', 'tagtype', 'controls');
53 $tableheaders = array(get_string('select', 'tag'),
54 get_string('name', 'tag'),
55 get_string('owner', 'tag'),
56 get_string('count', 'tag'),
57 get_string('flag', 'tag'),
58 get_string('timemodified', 'tag'),
59 get_string('officialtag', 'tag'),
60 '');
62 $this->define_columns($tablecolumns);
63 $this->define_headers($tableheaders);
64 $this->define_baseurl($baseurl);
66 $this->column_class('select', 'mdl-align col-select');
67 $this->column_class('name', 'col-name');
68 $this->column_class('owner', 'col-owner');
69 $this->column_class('count', 'mdl-align col-count');
70 $this->column_class('flag', 'mdl-align col-flag');
71 $this->column_class('timemodified', 'col-timemodified');
72 $this->column_class('tagtype', 'mdl-align col-tagtype');
73 $this->column_class('controls', 'mdl-align col-controls');
75 $this->sortable(true, 'flag', SORT_DESC);
76 $this->no_sorting('select');
77 $this->no_sorting('controls');
79 $this->set_attribute('cellspacing', '0');
80 $this->set_attribute('id', 'tag-management-list');
81 $this->set_attribute('class', 'admintable generaltable tag-management-table');
83 $totalcount = "SELECT COUNT(id) FROM {tag}";
84 $params = array();
86 $this->set_count_sql($totalcount, $params);
88 $this->set_sql('', '', '', $params);
90 $this->collapsible(true);
92 $PAGE->requires->js_call_amd('core/tag', 'init_manage_page', array());
96 /**
97 * Query the db. Store results in the table object for use by build_table.
99 * @param int $pagesize size of page for paginated displayed table.
100 * @param bool $useinitialsbar do you want to use the initials bar. Bar
101 * will only be used if there is a fullname column defined for the table.
103 public function query_db($pagesize, $useinitialsbar = true) {
104 global $DB;
105 $where = '';
106 if (!$this->is_downloading()) {
107 $grandtotal = $DB->count_records_sql($this->countsql, $this->countparams);
109 list($wsql, $wparams) = $this->get_sql_where();
110 if ($wsql) {
111 $this->countsql .= ' AND '.$wsql;
112 $this->countparams = array_merge($this->countparams, $wparams);
114 $where .= ' AND '.$wsql;
115 $this->sql->params = array_merge($this->sql->params, $wparams);
117 $total = $DB->count_records_sql($this->countsql, $this->countparams);
118 } else {
119 $total = $grandtotal;
122 $this->pagesize(min($pagesize, $total), $total);
123 $this->totalcount = $total;
126 // Fetch the attempts.
127 $sort = $this->get_sql_sort();
128 if ($sort) {
129 $sort .= ", tg.name";
130 } else {
131 $sort = "tg.name";
134 $allusernames = get_all_user_name_fields(true, 'u');
135 $sql = "
136 SELECT tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
137 u.id AS owner, $allusernames,
138 COUNT(ti.id) AS count
139 FROM {tag} tg
140 LEFT JOIN {tag_instance} ti ON ti.tagid = tg.id
141 LEFT JOIN {user} u ON u.id = tg.userid
142 WHERE 1 = 1 $where
143 GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
144 u.id, $allusernames
145 ORDER BY $sort";
147 if (!$this->is_downloading()) {
148 $this->rawdata = $DB->get_records_sql($sql, $this->sql->params, $this->get_page_start(), $this->get_page_size());
149 } else {
150 $this->rawdata = $DB->get_records_sql($sql, $this->sql->params);
155 * Get any extra classes names to add to this row in the HTML
157 * @param stdClass $row array the data for this row.
158 * @return string added to the class="" attribute of the tr.
160 public function get_row_class($row) {
161 return $row->flag ? 'flagged-tag' : '';
165 * Column name
167 * @param stdClass $tag
168 * @return string
170 public function col_name($tag) {
171 global $OUTPUT;
172 $tagoutput = new core_tag\output\tag($tag);
173 return $OUTPUT->render_from_template('core_tag/tagname', $tagoutput->export_for_template($OUTPUT));
177 * Column flag
179 * @param stdClass $tag
180 * @return string
182 public function col_flag($tag) {
183 global $OUTPUT;
184 $tagoutput = new core_tag\output\tag($tag);
185 return $OUTPUT->render_from_template('core_tag/tagflag', $tagoutput->export_for_template($OUTPUT));
189 * Column fullname (user name)
191 * @param stdClass $tag
192 * @return string
194 public function col_fullname($tag) {
195 $params = array('id' => $tag->owner);
196 $ownerlink = new moodle_url('/user/view.php', $params);
197 $owner = html_writer::link($ownerlink, fullname($tag));
198 return $owner;
202 * Column time modified
204 * @param stdClass $tag
205 * @return string
207 public function col_timemodified($tag) {
208 return format_time(time() - $tag->timemodified);
212 * Column tag type
214 * @param stdClass $tag
215 * @return string
217 public function col_tagtype($tag) {
218 global $OUTPUT;
219 $tagoutput = new core_tag\output\tag($tag);
220 return $OUTPUT->render_from_template('core_tag/tagtype', $tagoutput->export_for_template($OUTPUT));
224 * Column select
226 * @param stdClass $tag
227 * @return string
229 public function col_select($tag) {
230 $id = "tagselect" . $tag->id;
231 return html_writer::label(get_string('selecttag', 'tag', $tag->rawname), $id,
232 false, array('class' => 'accesshide')).
233 html_writer::empty_tag('input', array('type' => 'checkbox',
234 'name' => 'tagschecked[]', 'value' => $tag->id, 'id' => $id));
238 * Column controls
240 * @param stdClass $tag
241 * @return string
243 public function col_controls($tag) {
244 global $OUTPUT, $PAGE;
245 $o = '';
246 // Edit.
247 $url = new moodle_url('/tag/edit.php', array('id' => $tag->id, 'returnurl' => $PAGE->url->out_as_local_url()));
248 $o .= $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('edittag', 'tag')));
249 // Delete.
250 $url = new moodle_url($this->baseurl, array('action' => 'delete',
251 'tagid' => $tag->id, 'sesskey' => sesskey()));
252 $o .= $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete', 'tag')),
253 null, array('class' => 'action-icon tagdelete'));
254 return $o;