Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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   * Displays enrolment LTI instances.
  19   *
  20   * @package    enrol_lti
  21   * @copyright  2016 Mark Nelson <markn@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace enrol_lti;
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  global $CFG;
  30  
  31  require_once($CFG->libdir . '/tablelib.php');
  32  
  33  /**
  34   * Handles displaying enrolment LTI instances.
  35   *
  36   * @package    enrol_lti
  37   * @copyright  2016 Mark Nelson <markn@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class manage_table extends \table_sql {
  41  
  42      /**
  43       * @var \enrol_plugin $ltiplugin
  44       */
  45      protected $ltiplugin;
  46  
  47      /**
  48       * @var bool $ltienabled
  49       */
  50      protected $ltienabled;
  51  
  52      /**
  53       * @var bool $canconfig
  54       */
  55      protected $canconfig;
  56  
  57      /**
  58       * @var int $courseid The course id.
  59       */
  60      protected $courseid;
  61  
  62      /**
  63       * Sets up the table.
  64       *
  65       * @param string $courseid The id of the course.
  66       */
  67      public function __construct($courseid) {
  68          parent::__construct('enrol_lti_manage_table');
  69  
  70          $this->define_columns(array(
  71              'name',
  72              'launch',
  73              'registration',
  74              'edit'
  75          ));
  76          $this->define_headers(array(
  77              get_string('name'),
  78              get_string('launchdetails', 'enrol_lti'),
  79              get_string('registrationurl', 'enrol_lti'),
  80              get_string('edit')
  81          ));
  82          $this->collapsible(false);
  83          $this->sortable(false);
  84  
  85          // Set the variables we need access to.
  86          $this->ltiplugin = enrol_get_plugin('lti');
  87          $this->ltienabled = enrol_is_enabled('lti');
  88          $this->canconfig = has_capability('moodle/course:enrolconfig', \context_course::instance($courseid));
  89          $this->courseid = $courseid;
  90  
  91          // Set help icons.
  92          $launchicon = new \help_icon('launchdetails', 'enrol_lti');
  93          $regicon = new \help_icon('registrationurl', 'enrol_lti');
  94          $this->define_help_for_headers(['1' => $launchicon, '2' => $regicon]);
  95      }
  96  
  97      /**
  98       * Generate the name column.
  99       *
 100       * @param \stdClass $tool event data.
 101       * @return string
 102       */
 103      public function col_name($tool) {
 104          $toolcontext = \context::instance_by_id($tool->contextid, IGNORE_MISSING);
 105          $name = $toolcontext ? helper::get_name($tool) : $this->get_deleted_activity_name_html($tool);
 106  
 107          return $this->get_display_text($tool, $name);
 108      }
 109  
 110      /**
 111       * Generate the launch column.
 112       *
 113       * @param \stdClass $tool instance data.
 114       * @return string
 115       */
 116      public function col_launch($tool) {
 117          global $OUTPUT;
 118  
 119          $url = helper::get_cartridge_url($tool);
 120  
 121          $cartridgeurllabel = get_string('cartridgeurl', 'enrol_lti');
 122          $cartridgeurl = $url;
 123          $secretlabel = get_string('secret', 'enrol_lti');
 124          $secret = $tool->secret;
 125          $launchurl = helper::get_launch_url($tool->id);
 126          $launchurllabel = get_string('launchurl', 'enrol_lti');
 127  
 128          $data = [
 129                  "rows" => [
 130                      [ "label" => $cartridgeurllabel, "text" => $cartridgeurl, "id" => "cartridgeurl", "hidelabel" => false ],
 131                      [ "label" => $secretlabel, "text" => $secret, "id" => "secret", "hidelabel" => false ],
 132                      [ "label" => $launchurllabel, "text" => $launchurl, "id" => "launchurl", "hidelabel" => false ],
 133                  ]
 134              ];
 135  
 136          $return = $OUTPUT->render_from_template("enrol_lti/copy_grid", $data);
 137  
 138          return $return;
 139      }
 140  
 141      /**
 142       * Generate the Registration column.
 143       *
 144       * @param \stdClass $tool instance data.
 145       * @return string
 146       */
 147      public function col_registration($tool) {
 148          global $OUTPUT;
 149  
 150          $url = helper::get_proxy_url($tool);
 151  
 152          $toolurllabel = get_string("registrationurl", "enrol_lti");
 153          $toolurl = $url;
 154  
 155          $data = [
 156                  "rows" => [
 157                      [ "label" => $toolurllabel, "text" => $toolurl, "id" => "toolurl" , "hidelabel" => true],
 158                  ]
 159              ];
 160  
 161          $return = $OUTPUT->render_from_template("enrol_lti/copy_grid", $data);
 162          return $return;
 163      }
 164  
 165      /**
 166       * Generate the edit column.
 167       *
 168       * @param \stdClass $tool event data.
 169       * @return string
 170       */
 171      public function col_edit($tool) {
 172          global $OUTPUT;
 173  
 174          $buttons = array();
 175  
 176          $instance = new \stdClass();
 177          $instance->id = $tool->enrolid;
 178          $instance->courseid = $tool->courseid;
 179          $instance->enrol = 'lti';
 180          $instance->status = $tool->status;
 181  
 182          $strdelete = get_string('delete');
 183          $strenable = get_string('enable');
 184          $strdisable = get_string('disable');
 185  
 186          $url = new \moodle_url('/enrol/lti/index.php', array('sesskey' => sesskey(), 'courseid' => $this->courseid));
 187  
 188          if ($this->ltiplugin->can_delete_instance($instance)) {
 189              $aurl = new \moodle_url($url, array('action' => 'delete', 'instanceid' => $instance->id));
 190              $buttons[] = $OUTPUT->action_icon($aurl, new \pix_icon('t/delete', $strdelete, 'core',
 191                  array('class' => 'iconsmall')));
 192          }
 193  
 194          if ($this->ltienabled && $this->ltiplugin->can_hide_show_instance($instance)) {
 195              if ($instance->status == ENROL_INSTANCE_ENABLED) {
 196                  $aurl = new \moodle_url($url, array('action' => 'disable', 'instanceid' => $instance->id));
 197                  $buttons[] = $OUTPUT->action_icon($aurl, new \pix_icon('t/hide', $strdisable, 'core',
 198                      array('class' => 'iconsmall')));
 199              } else if ($instance->status == ENROL_INSTANCE_DISABLED) {
 200                  $aurl = new \moodle_url($url, array('action' => 'enable', 'instanceid' => $instance->id));
 201                  $buttons[] = $OUTPUT->action_icon($aurl, new \pix_icon('t/show', $strenable, 'core',
 202                      array('class' => 'iconsmall')));
 203              }
 204          }
 205  
 206          if ($this->ltienabled && $this->canconfig) {
 207              $linkparams = array(
 208                  'courseid' => $instance->courseid,
 209                  'id' => $instance->id, 'type' => $instance->enrol,
 210                  'returnurl' => new \moodle_url('/enrol/lti/index.php', array('courseid' => $this->courseid))
 211              );
 212              $editlink = new \moodle_url("/enrol/editinstance.php", $linkparams);
 213              $buttons[] = $OUTPUT->action_icon($editlink, new \pix_icon('t/edit', get_string('edit'), 'core',
 214                  array('class' => 'iconsmall')));
 215          }
 216  
 217          return implode(' ', $buttons);
 218      }
 219  
 220      /**
 221       * Query the reader. Store results in the object for use by build_table.
 222       *
 223       * @param int $pagesize size of page for paginated displayed table.
 224       * @param bool $useinitialsbar do you want to use the initials bar.
 225       */
 226      public function query_db($pagesize, $useinitialsbar = true) {
 227          $total = \enrol_lti\helper::count_lti_tools(array('courseid' => $this->courseid));
 228          $this->pagesize($pagesize, $total);
 229          $tools = \enrol_lti\helper::get_lti_tools(array('courseid' => $this->courseid), $this->get_page_start(),
 230              $this->get_page_size());
 231          $this->rawdata = $tools;
 232          // Set initial bars.
 233          if ($useinitialsbar) {
 234              $this->initialbars($total > $pagesize);
 235          }
 236      }
 237  
 238      /**
 239       * Returns text to display in the columns.
 240       *
 241       * @param \stdClass $tool the tool
 242       * @param string $text the text to alter
 243       * @return string
 244       */
 245      protected function get_display_text($tool, $text) {
 246          if ($tool->status != ENROL_INSTANCE_ENABLED) {
 247              return \html_writer::tag('div', $text, array('class' => 'dimmed_text'));
 248          }
 249  
 250          return $text;
 251      }
 252  
 253      /**
 254       * Get a warning icon, with tooltip, describing enrolment instances sharing activities which have been deleted.
 255       *
 256       * @param \stdClass $tool the tool instance record.
 257       * @return string the HTML for the name column.
 258       */
 259      protected function get_deleted_activity_name_html(\stdClass $tool): string {
 260          global $OUTPUT;
 261          $icon = \html_writer::tag(
 262              'a',
 263              $OUTPUT->pix_icon('enrolinstancewarning', get_string('deletedactivityalt' , 'enrol_lti'), 'enrol_lti'), [
 264                  "class" => "btn btn-link p-0",
 265                  "role" => "button",
 266                  "data-container" => "body",
 267                  "data-toggle" => "popover",
 268                  "data-placement" => right_to_left() ? "left" : "right",
 269                  "data-content" => get_string('deletedactivitydescription', 'enrol_lti'),
 270                  "data-html" => "true",
 271                  "tabindex" => "0",
 272                  "data-trigger" => "focus"
 273              ]
 274          );
 275          $name = \html_writer::span($icon . get_string('deletedactivity', 'enrol_lti'));
 276          if ($tool->name) {
 277              $name .= \html_writer::empty_tag('br') . \html_writer::empty_tag('br') . $tool->name;
 278          }
 279  
 280          return $name;
 281      }
 282  }