on-demand release 4.0dev+
[moodle.git] / admin / tool / licensemanager / classes / output / table.php
CommitLineData
4399e475
TD
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 * Renderable for display of license manager table.
19 *
20 * @package tool_licensemanager
21 * @copyright 2020 Tom Dickman <tomdickman@catalyst-au.net>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace tool_licensemanager\output;
25
26use html_table;
27use html_table_cell;
28use html_table_row;
29use html_writer;
30use license_manager;
31
32defined('MOODLE_INTERNAL') || die();
33
34/**
35 * Renderable for display of license manager table.
36 *
37 * @package tool_licensemanager
38 * @copyright 2020 Tom Dickman <tomdickman@catalyst-au.net>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class table implements \renderable {
42
43 /**
44 * 'Create License' link.
45 *
46 * @return string HTML string.
47 */
48 public function create_license_link() {
49 $link = html_writer::link(\tool_licensemanager\helper::get_create_license_url(),
50 get_string('createlicensebuttontext', 'tool_licensemanager'),
51 ['class' => 'btn btn-secondary mb-3']);
52
53 return $link;
54 }
55
56 /**
57 * Create the HTML table for license management.
58 *
59 * @param array $licenses
60 * @param \renderer_base $output
61 *
62 * @return string HTML for license manager table.
63 */
64 public function create_license_manager_table(array $licenses, \renderer_base $output) {
65 $table = new html_table();
66 $table->head = [
67 get_string('enable'),
68 get_string('license', 'tool_licensemanager'),
69 get_string('version'),
70 get_string('order'),
71 get_string('edit'),
72 get_string('delete'),
73 ];
74 $table->colclasses = [
75 'text-center',
76 'text-left',
77 'text-left',
78 'text-center',
79 'text-center',
80 'text-center',
81 ];
82 $table->id = 'manage-licenses';
83 $table->attributes['class'] = 'admintable generaltable';
84 $table->data = [];
85
86 $rownumber = 0;
87 $rowcount = count($licenses);
88
89 foreach ($licenses as $key => $value) {
90 $canmoveup = $rownumber > 0;
91 $canmovedown = $rownumber < $rowcount - 1;
92 $table->data[] = $this->get_license_table_row_data($value, $canmoveup, $canmovedown, $output);
93 $rownumber++;
94 }
95
96 $html = html_writer::table($table);
97
98 return $html;
99 }
100
101 /**
102 * Get table row data for a license.
103 *
104 * @param object $license the license to populate row data for.
105 * @param bool $canmoveup can this row move up.
106 * @param bool $canmovedown can this row move down.
107 * @param \renderer_base $output the renderer
108 *
109 * @return \html_table_row of columns values for row.
110 */
111 protected function get_license_table_row_data($license, bool $canmoveup, bool $canmovedown, \renderer_base $output) {
112 global $CFG;
113
114 $summary = $license->fullname . ' ('. $license->shortname . ')';
115 if (!empty($license->source)) {
116 $summary .= html_writer::empty_tag('br');
117 $summary .= html_writer::link($license->source, $license->source, ['target' => '_blank']);
118 }
119 $summarycell = new html_table_cell($summary);
120 $summarycell->attributes['class'] = 'license-summary';
121 $versioncell = new html_table_cell($license->version);
122 $versioncell->attributes['class'] = 'license-version';
123
cd4ec9b3 124 $deletelicense = '';
4399e475
TD
125 if ($license->shortname == $CFG->sitedefaultlicense) {
126 $hideshow = $output->pix_icon('t/locked', get_string('sitedefaultlicenselock', 'tool_licensemanager'));
127 } else {
128 if ($license->enabled == license_manager::LICENSE_ENABLED) {
129 $hideshow = html_writer::link(\tool_licensemanager\helper::get_disable_license_url($license->shortname),
130 $output->pix_icon('t/hide', get_string('disablelicensename', 'tool_licensemanager', $license->fullname)));
131 } else {
132 $hideshow = html_writer::link(\tool_licensemanager\helper::get_enable_license_url($license->shortname),
133 $output->pix_icon('t/show', get_string('enablelicensename', 'tool_licensemanager', $license->fullname)));
134 }
135
136 if ($license->custom == license_manager::CUSTOM_LICENSE) {
137 // Link url is added by the JS `delete_license` modal used for confirmation of deletion, to avoid
138 // link being usable before JavaScript loads on page.
139 $deletelicense = html_writer::link('#', $output->pix_icon('i/trash',
140 get_string('deletelicensename', 'tool_licensemanager', $license->fullname)),
141 ['class' => 'delete-license', 'data-license' => $license->shortname]);
4399e475
TD
142 }
143 }
144 $hideshowcell = new html_table_cell($hideshow);
145 $hideshowcell->attributes['class'] = 'license-status';
146
147 if ($license->custom == license_manager::CUSTOM_LICENSE) {
148 $editlicense = html_writer::link(\tool_licensemanager\helper::get_update_license_url($license->shortname),
149 $output->pix_icon('t/editinline', get_string('editlicensename', 'tool_licensemanager', $license->fullname)),
150 ['class' => 'edit-license']);
151 } else {
152 $editlicense = '';
153 }
154 $editlicensecell = new html_table_cell($editlicense);
155 $editlicensecell->attributes['class'] = 'edit-license';
156
157 $spacer = $output->pix_icon('spacer', '', 'moodle', ['class' => 'iconsmall']);
158 $updown = '';
159 if ($canmoveup) {
160 $updown .= html_writer::link(\tool_licensemanager\helper::get_moveup_license_url($license->shortname),
161 $output->pix_icon('t/up', get_string('movelicenseupname', 'tool_licensemanager', $license->fullname),
162 'moodle', ['class' => 'iconsmall']),
163 ['class' => 'move-up']) . '';
164 } else {
165 $updown .= $spacer;
166 }
167
168 if ($canmovedown) {
169 $updown .= '&nbsp;'.html_writer::link(\tool_licensemanager\helper::get_movedown_license_url($license->shortname),
170 $output->pix_icon('t/down', get_string('movelicensedownname', 'tool_licensemanager', $license->fullname),
171 'moodle', ['class' => 'iconsmall']),
172 ['class' => 'move-down']);
173 } else {
174 $updown .= $spacer;
175 }
176 $updowncell = new html_table_cell($updown);
177 $updowncell->attributes['class'] = 'license-order';
178
179 $row = new html_table_row([$hideshowcell, $summarycell, $versioncell, $updowncell, $editlicensecell, $deletelicense]);
180 $row->attributes['data-license'] = $license->shortname;
181 $row->attributes['class'] = strtolower(get_string('license', 'tool_licensemanager'));
182
183 return $row;
184 }
185}