2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Contains class core_tag_manage_table
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');
30 * Class core_tag_manage_table
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;
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 $baseurl = new moodle_url('/tag/manage.php', array('tc' => $tagcollid,
58 'perpage' => $perpage, 'page' => $page));
60 $tablecolumns = array('select', 'name', 'fullname', 'count', 'flag', 'timemodified', 'isstandard', 'controls');
61 $tableheaders = array(get_string('select', 'tag'),
62 get_string('name', 'tag'),
63 get_string('owner', 'tag'),
64 get_string('count', 'tag'),
65 get_string('flag', 'tag'),
66 get_string('timemodified', 'tag'),
67 get_string('standardtag', 'tag'),
70 $this->define_columns($tablecolumns);
71 $this->define_headers($tableheaders);
72 $this->define_baseurl($baseurl);
74 $this->column_class('select', 'mdl-align col-select');
75 $this->column_class('name', 'col-name');
76 $this->column_class('owner', 'col-owner');
77 $this->column_class('count', 'mdl-align col-count');
78 $this->column_class('flag', 'mdl-align col-flag');
79 $this->column_class('timemodified', 'col-timemodified');
80 $this->column_class('isstandard', 'mdl-align col-isstandard');
81 $this->column_class('controls', 'mdl-align col-controls');
83 $this->sortable(true, 'flag', SORT_DESC);
84 $this->no_sorting('select');
85 $this->no_sorting('controls');
87 $this->set_attribute('cellspacing', '0');
88 $this->set_attribute('id', 'tag-management-list');
89 $this->set_attribute('class', 'admintable generaltable tag-management-table');
91 $totalcount = "SELECT COUNT(id)
93 WHERE tagcollid = :tagcollid";
94 $params = array('tagcollid' => $this->tagcollid);
96 $this->set_count_sql($totalcount, $params);
98 $this->set_sql('', '', '', $params);
100 $this->collapsible(true);
102 $PAGE->requires->js_call_amd('core/tag', 'initManagePage', array());
107 * Query the db. Store results in the table object for use by build_table.
109 * @param int $pagesize size of page for paginated displayed table.
110 * @param bool $useinitialsbar do you want to use the initials bar. Bar
111 * will only be used if there is a fullname column defined for the table.
113 public function query_db($pagesize, $useinitialsbar = true) {
116 if (!$this->is_downloading()) {
117 $grandtotal = $DB->count_records_sql($this->countsql, $this->countparams);
119 list($wsql, $wparams) = $this->get_sql_where();
121 $this->countsql .= ' AND '.$wsql;
122 $this->countparams = array_merge($this->countparams, $wparams);
124 $where .= ' AND '.$wsql;
125 $this->sql->params = array_merge($this->sql->params, $wparams);
127 $total = $DB->count_records_sql($this->countsql, $this->countparams);
129 $total = $grandtotal;
132 $this->pagesize(min($pagesize, $total), $total);
133 $this->totalcount = $total;
136 // Fetch the attempts.
137 $sort = $this->get_sql_sort();
139 $sort .= ", tg.name";
144 $allusernames = get_all_user_name_fields(true, 'u');
146 SELECT tg.id, tg.name, tg.rawname, tg.isstandard, tg.flag, tg.timemodified,
147 u.id AS owner, $allusernames,
148 COUNT(ti.id) AS count, tg.tagcollid
150 LEFT JOIN {tag_instance} ti ON ti.tagid = tg.id
151 LEFT JOIN {user} u ON u.id = tg.userid
152 WHERE tagcollid = :tagcollid $where
153 GROUP BY tg.id, tg.name, tg.rawname, tg.isstandard, tg.flag, tg.timemodified,
154 u.id, $allusernames, tg.tagcollid
157 if (!$this->is_downloading()) {
158 $this->rawdata = $DB->get_records_sql($sql, $this->sql->params, $this->get_page_start(), $this->get_page_size());
160 $this->rawdata = $DB->get_records_sql($sql, $this->sql->params);
165 * Get any extra classes names to add to this row in the HTML
167 * @param stdClass $row array the data for this row.
168 * @return string added to the class="" attribute of the tr.
170 public function get_row_class($row) {
171 return $row->flag ? 'flagged-tag' : '';
177 * @param stdClass $tag
180 public function col_name($tag) {
182 $tagoutput = new core_tag\output\tagname($tag);
183 return $tagoutput->render($OUTPUT);
189 * @param stdClass $tag
192 public function col_flag($tag) {
194 $tagoutput = new core_tag\output\tagflag($tag);
195 return $tagoutput->render($OUTPUT);
199 * Column fullname (user name)
201 * @param stdClass $tag
204 public function col_fullname($tag) {
205 $params = array('id' => $tag->owner);
206 $ownerlink = new moodle_url('/user/view.php', $params);
207 $owner = html_writer::link($ownerlink, fullname($tag));
212 * Column time modified
214 * @param stdClass $tag
217 public function col_timemodified($tag) {
218 return format_time(time() - $tag->timemodified);
224 * @param stdClass $tag
227 public function col_isstandard($tag) {
229 $tagoutput = new core_tag\output\tagisstandard($tag);
230 return $tagoutput->render($OUTPUT);
236 * @param stdClass $tag
239 public function col_select($tag) {
240 $id = "tagselect" . $tag->id;
241 return html_writer::label(get_string('selecttag', 'tag', $tag->rawname), $id,
242 false, array('class' => 'accesshide')).
243 html_writer::empty_tag('input', array('type' => 'checkbox',
244 'name' => 'tagschecked[]', 'value' => $tag->id, 'id' => $id));
250 * @param stdClass $tag
253 public function col_controls($tag) {
254 global $OUTPUT, $PAGE;
257 $url = new moodle_url('/tag/edit.php', array('id' => $tag->id, 'returnurl' => $PAGE->url->out_as_local_url()));
258 $o .= $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('edittag', 'tag')));
260 $url = new moodle_url($this->baseurl, array('action' => 'delete',
261 'tagid' => $tag->id, 'sesskey' => sesskey()));
262 $o .= $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete', 'tag')),
263 null, array('class' => 'action-icon tagdelete'));