MDL-68843 enrol_lti: add deleted activity warning to published tools
[moodle.git] / enrol / lti / classes / local / ltiadvantage / table / published_resources_table.php
blob8367b634da220e8107b441af16c4251c0446a32e
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 namespace enrol_lti\local\ltiadvantage\table;
19 use enrol_lti\helper;
21 defined('MOODLE_INTERNAL') || die;
23 global $CFG;
25 require_once($CFG->libdir . '/tablelib.php');
27 /**
28 * Class which displays a list of resources published over LTI Advantage.
30 * @package enrol_lti
31 * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class published_resources_table extends \table_sql {
36 /**
37 * @var \enrol_plugin $ltiplugin
39 protected $ltiplugin;
41 /**
42 * @var bool $ltienabled
44 protected $ltienabled;
46 /**
47 * @var bool $canconfig
49 protected $canconfig;
51 /**
52 * @var int $courseid The course id.
54 protected $courseid;
56 /**
57 * Sets up the table.
59 * @param string $courseid The id of the course.
61 public function __construct($courseid) {
62 parent::__construct('enrol_lti_manage_table');
64 $this->define_columns(array(
65 'name',
66 'launch',
67 'edit'
68 ));
69 $this->define_headers(array(
70 get_string('name'),
71 get_string('lti13launchdetails', 'enrol_lti'),
72 get_string('edit')
73 ));
74 $this->collapsible(false);
75 $this->sortable(false);
77 // Set the variables we need access to.
78 $this->ltiplugin = enrol_get_plugin('lti');
79 $this->ltienabled = enrol_is_enabled('lti');
80 $this->canconfig = has_capability('moodle/course:enrolconfig', \context_course::instance($courseid));
81 $this->courseid = $courseid;
83 // Set help icons.
84 $launchicon = new \help_icon('lti13launchdetails', 'enrol_lti');
85 $this->define_help_for_headers(['1' => $launchicon]);
88 /**
89 * Generate the name column.
91 * @param \stdClass $tool event data.
92 * @return string
94 public function col_name($tool) {
95 $toolcontext = \context::instance_by_id($tool->contextid, IGNORE_MISSING);
96 $name = $toolcontext ? helper::get_name($tool) : $this->get_deleted_activity_name_html($tool);
98 return $this->get_display_text($tool, $name);
102 * Generate the launch column.
104 * @param \stdClass $tool instance data.
105 * @return string
107 public function col_launch($tool) {
108 global $OUTPUT;
110 $customparamslabel = get_string('customproperties', 'enrol_lti');
111 $customparams = "id={$tool->uuid}";
112 $launchurl = new \moodle_url('/enrol/lti/launch.php');
113 $launchurllabel = get_string('launchurl', 'enrol_lti');
115 $data = [
116 "rows" => [
118 "label" => $launchurllabel,
119 "text" => $launchurl->out(false),
120 "id" => "launchurl",
121 "hidelabel" => false
124 "label" => $customparamslabel,
125 "text" => $customparams,
126 "id" => "customparams",
127 "hidelabel" => false
132 $return = $OUTPUT->render_from_template("enrol_lti/copy_grid", $data);
134 return $return;
138 * Generate the edit column.
140 * @param \stdClass $tool event data.
141 * @return string
143 public function col_edit($tool) {
144 global $OUTPUT;
146 $buttons = array();
148 $instance = new \stdClass();
149 $instance->id = $tool->enrolid;
150 $instance->courseid = $tool->courseid;
151 $instance->enrol = 'lti';
152 $instance->status = $tool->status;
154 $strdelete = get_string('delete');
155 $strenable = get_string('enable');
156 $strdisable = get_string('disable');
158 $url = new \moodle_url('/enrol/lti/index.php', array('sesskey' => sesskey(), 'courseid' => $this->courseid));
160 if ($this->ltiplugin->can_delete_instance($instance)) {
161 $aurl = new \moodle_url($url, array('action' => 'delete', 'instanceid' => $instance->id));
162 $buttons[] = $OUTPUT->action_icon($aurl, new \pix_icon('t/delete', $strdelete, 'core',
163 array('class' => 'iconsmall')));
166 if ($this->ltienabled && $this->ltiplugin->can_hide_show_instance($instance)) {
167 if ($instance->status == ENROL_INSTANCE_ENABLED) {
168 $aurl = new \moodle_url($url, array('action' => 'disable', 'instanceid' => $instance->id));
169 $buttons[] = $OUTPUT->action_icon($aurl, new \pix_icon('t/hide', $strdisable, 'core',
170 array('class' => 'iconsmall')));
171 } else if ($instance->status == ENROL_INSTANCE_DISABLED) {
172 $aurl = new \moodle_url($url, array('action' => 'enable', 'instanceid' => $instance->id));
173 $buttons[] = $OUTPUT->action_icon($aurl, new \pix_icon('t/show', $strenable, 'core',
174 array('class' => 'iconsmall')));
178 if ($this->ltienabled && $this->canconfig) {
179 $linkparams = array(
180 'courseid' => $instance->courseid,
181 'id' => $instance->id, 'type' => $instance->enrol,
182 'returnurl' => new \moodle_url('/enrol/lti/index.php', array('courseid' => $this->courseid))
184 $editlink = new \moodle_url("/enrol/editinstance.php", $linkparams);
185 $buttons[] = $OUTPUT->action_icon($editlink, new \pix_icon('t/edit', get_string('edit'), 'core',
186 array('class' => 'iconsmall')));
189 return implode(' ', $buttons);
193 * Query the reader. Store results in the object for use by build_table.
195 * @param int $pagesize size of page for paginated displayed table.
196 * @param bool $useinitialsbar do you want to use the initials bar.
198 public function query_db($pagesize, $useinitialsbar = true) {
199 $total = helper::count_lti_tools(['courseid' => $this->courseid, 'ltiversion' => 'LTI-1p3']);
200 $this->pagesize($pagesize, $total);
201 $tools = helper::get_lti_tools(['courseid' => $this->courseid, 'ltiversion' => 'LTI-1p3'],
202 $this->get_page_start(), $this->get_page_size());
203 $this->rawdata = $tools;
204 // Set initial bars.
205 if ($useinitialsbar) {
206 $this->initialbars($total > $pagesize);
211 * Returns text to display in the columns.
213 * @param \stdClass $tool the tool
214 * @param string $text the text to alter
215 * @return string
217 protected function get_display_text($tool, $text) {
218 if ($tool->status != ENROL_INSTANCE_ENABLED) {
219 return \html_writer::tag('div', $text, array('class' => 'dimmed_text'));
222 return $text;
226 * Get a warning icon, with tooltip, describing enrolment instances sharing activities which have been deleted.
228 * @param \stdClass $tool the tool instance record.
229 * @return string the HTML for the name column.
231 protected function get_deleted_activity_name_html(\stdClass $tool): string {
232 global $OUTPUT;
233 $icon = \html_writer::tag(
234 'a',
235 $OUTPUT->pix_icon('enrolinstancewarning', get_string('deletedactivityalt' , 'enrol_lti'), 'enrol_lti'), [
236 "class" => "btn btn-link p-0",
237 "role" => "button",
238 "data-container" => "body",
239 "data-toggle" => "popover",
240 "data-placement" => right_to_left() ? "left" : "right",
241 "data-content" => get_string('deletedactivitydescription', 'enrol_lti'),
242 "data-html" => "true",
243 "tabindex" => "0",
244 "data-trigger" => "focus"
247 $name = \html_writer::span($icon . get_string('deletedactivity', 'enrol_lti'));
248 if ($tool->name) {
249 $name .= \html_writer::empty_tag('br') . \html_writer::empty_tag('br') . $tool->name;
252 return $name;