Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  namespace enrol_lti\local\ltiadvantage\table;
  18  
  19  use enrol_lti\helper;
  20  
  21  defined('MOODLE_INTERNAL') || die;
  22  
  23  global $CFG;
  24  
  25  require_once($CFG->libdir . '/tablelib.php');
  26  
  27  /**
  28   * Class which displays a list of resources published over LTI Advantage.
  29   *
  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
  33   */
  34  class published_resources_table extends \table_sql {
  35  
  36      /**
  37       * @var \enrol_plugin $ltiplugin
  38       */
  39      protected $ltiplugin;
  40  
  41      /**
  42       * @var bool $ltienabled
  43       */
  44      protected $ltienabled;
  45  
  46      /**
  47       * @var bool $canconfig
  48       */
  49      protected $canconfig;
  50  
  51      /**
  52       * @var int $courseid The course id.
  53       */
  54      protected $courseid;
  55  
  56      /**
  57       * Sets up the table.
  58       *
  59       * @param string $courseid The id of the course.
  60       */
  61      public function __construct($courseid) {
  62          parent::__construct('enrol_lti_manage_table');
  63  
  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);
  76  
  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;
  82  
  83          // Set help icons.
  84          $launchicon = new \help_icon('lti13launchdetails', 'enrol_lti');
  85          $this->define_help_for_headers(['1' => $launchicon]);
  86      }
  87  
  88      /**
  89       * Generate the name column.
  90       *
  91       * @param \stdClass $tool event data.
  92       * @return string
  93       */
  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);
  97  
  98          return $this->get_display_text($tool, $name);
  99      }
 100  
 101      /**
 102       * Generate the launch column.
 103       *
 104       * @param \stdClass $tool instance data.
 105       * @return string
 106       */
 107      public function col_launch($tool) {
 108          global $OUTPUT;
 109  
 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');
 114  
 115          $data = [
 116                  "rows" => [
 117                      [
 118                          "label" => $launchurllabel,
 119                          "text" => $launchurl->out(false),
 120                          "id" => "launchurl",
 121                          "hidelabel" => false
 122                      ],
 123                      [
 124                          "label" => $customparamslabel,
 125                          "text" => $customparams,
 126                          "id" => "customparams",
 127                          "hidelabel" => false
 128                      ]
 129                  ]
 130              ];
 131  
 132          $return = $OUTPUT->render_from_template("enrol_lti/copy_grid", $data);
 133  
 134          return $return;
 135      }
 136  
 137      /**
 138       * Generate the edit column.
 139       *
 140       * @param \stdClass $tool event data.
 141       * @return string
 142       */
 143      public function col_edit($tool) {
 144          global $OUTPUT;
 145  
 146          $buttons = array();
 147  
 148          $instance = new \stdClass();
 149          $instance->id = $tool->enrolid;
 150          $instance->courseid = $tool->courseid;
 151          $instance->enrol = 'lti';
 152          $instance->status = $tool->status;
 153  
 154          $strdelete = get_string('delete');
 155          $strenable = get_string('enable');
 156          $strdisable = get_string('disable');
 157  
 158          $url = new \moodle_url('/enrol/lti/index.php', array('sesskey' => sesskey(), 'courseid' => $this->courseid));
 159  
 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')));
 164          }
 165  
 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')));
 175              }
 176          }
 177  
 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))
 183              );
 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')));
 187          }
 188  
 189          return implode(' ', $buttons);
 190      }
 191  
 192      /**
 193       * Query the reader. Store results in the object for use by build_table.
 194       *
 195       * @param int $pagesize size of page for paginated displayed table.
 196       * @param bool $useinitialsbar do you want to use the initials bar.
 197       */
 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);
 207          }
 208      }
 209  
 210      /**
 211       * Returns text to display in the columns.
 212       *
 213       * @param \stdClass $tool the tool
 214       * @param string $text the text to alter
 215       * @return string
 216       */
 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'));
 220          }
 221  
 222          return $text;
 223      }
 224  
 225      /**
 226       * Get a warning icon, with tooltip, describing enrolment instances sharing activities which have been deleted.
 227       *
 228       * @param \stdClass $tool the tool instance record.
 229       * @return string the HTML for the name column.
 230       */
 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"
 245              ]
 246          );
 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;
 250          }
 251  
 252          return $name;
 253      }
 254  }