Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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   * Output rendering for the plugin.
  19   *
  20   * @package     tool_oauth2
  21   * @copyright   2017 Damyon Wiese
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_oauth2\output;
  25  
  26  use plugin_renderer_base;
  27  use html_table;
  28  use html_table_cell;
  29  use html_table_row;
  30  use html_writer;
  31  use core\oauth2\issuer;
  32  use core\oauth2\api;
  33  use moodle_url;
  34  
  35  defined('MOODLE_INTERNAL') || die();
  36  
  37  /**
  38   * Implements the plugin renderer
  39   *
  40   * @copyright 2017 Damyon Wiese
  41   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class renderer extends plugin_renderer_base {
  44      /**
  45       * This function will render one beautiful table with all the issuers.
  46       *
  47       * @param \core\oauth2\issuer[] $issuers - list of all issuers.
  48       * @return string HTML to output.
  49       */
  50      public function issuers_table($issuers) {
  51          global $CFG;
  52  
  53          $table = new html_table();
  54          $table->head  = [
  55              get_string('name'),
  56              get_string('configuredstatus', 'tool_oauth2'),
  57              get_string('loginissuer', 'tool_oauth2'),
  58              get_string('discoverystatus', 'tool_oauth2') . ' ' . $this->help_icon('discovered', 'tool_oauth2'),
  59              get_string('systemauthstatus', 'tool_oauth2') . ' ' . $this->help_icon('systemaccountconnected', 'tool_oauth2'),
  60              get_string('edit'),
  61          ];
  62          $table->attributes['class'] = 'admintable generaltable';
  63          $data = [];
  64  
  65          $index = 0;
  66  
  67          foreach ($issuers as $issuer) {
  68              // We need to handle the first and last ones specially.
  69              $first = false;
  70              if ($index == 0) {
  71                  $first = true;
  72              }
  73              $last = false;
  74              if ($index == count($issuers) - 1) {
  75                  $last = true;
  76              }
  77  
  78              // Name.
  79              $name = $issuer->get('name');
  80              $image = $issuer->get('image');
  81              if ($image) {
  82                  $name = '<img width="24" height="24" alt="" src="' . s($image) . '"> ' . s($name);
  83              }
  84              $namecell = new html_table_cell($name);
  85              $namecell->header = true;
  86  
  87              // Configured.
  88              if ($issuer->is_configured()) {
  89                  $configured = $this->pix_icon('yes', get_string('configured', 'tool_oauth2'), 'tool_oauth2');
  90              } else {
  91                  $configured = $this->pix_icon('no', get_string('notconfigured', 'tool_oauth2'), 'tool_oauth2');
  92              }
  93              $configuredstatuscell = new html_table_cell($configured);
  94  
  95              // Login issuer.
  96              if (!empty($issuer->get('showonloginpage'))) {
  97                  $loginissuer = $this->pix_icon('yes', get_string('loginissuer', 'tool_oauth2'), 'tool_oauth2');
  98              } else {
  99                  $loginissuer = $this->pix_icon('no', get_string('notloginissuer', 'tool_oauth2'), 'tool_oauth2');
 100              }
 101              $loginissuerstatuscell = new html_table_cell($loginissuer);
 102  
 103              // Discovered.
 104              if (!empty($issuer->get('scopessupported'))) {
 105                  $discovered = $this->pix_icon('yes', get_string('discovered', 'tool_oauth2'), 'tool_oauth2');
 106              } else {
 107                  if (!empty($issuer->get_endpoint_url('discovery'))) {
 108                      $discovered = $this->pix_icon('no', get_string('notdiscovered', 'tool_oauth2'), 'tool_oauth2');
 109                  } else {
 110                      $discovered = '-';
 111                  }
 112              }
 113  
 114              $discoverystatuscell = new html_table_cell($discovered);
 115  
 116              // Connected.
 117              if ($issuer->is_system_account_connected()) {
 118                  $systemaccount = \core\oauth2\api::get_system_account($issuer);
 119                  $systemauth = s($systemaccount->get('email')) . ' (' . s($systemaccount->get('username')). ') ';
 120                  $systemauth .= $this->pix_icon('yes', get_string('systemaccountconnected', 'tool_oauth2'), 'tool_oauth2');
 121              } else {
 122                  $systemauth = $this->pix_icon('no', get_string('systemaccountnotconnected', 'tool_oauth2'), 'tool_oauth2');
 123              }
 124  
 125              $params = ['id' => $issuer->get('id'), 'action' => 'auth'];
 126              $authurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 127              $icon = $this->pix_icon('auth', get_string('connectsystemaccount', 'tool_oauth2'), 'tool_oauth2');
 128              $authlink = html_writer::link($authurl, $icon);
 129              $systemauth .= ' ' . $authlink;
 130  
 131              $systemauthstatuscell = new html_table_cell($systemauth);
 132  
 133              $links = '';
 134              // Action links.
 135              $editurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['id' => $issuer->get('id'), 'action' => 'edit']);
 136              $editlink = html_writer::link($editurl, $this->pix_icon('t/edit', get_string('edit')));
 137              $links .= ' ' . $editlink;
 138  
 139              // Endpoints.
 140              $editendpointsurl = new moodle_url('/admin/tool/oauth2/endpoints.php', ['issuerid' => $issuer->get('id')]);
 141              $str = get_string('editendpoints', 'tool_oauth2');
 142              $editendpointlink = html_writer::link($editendpointsurl, $this->pix_icon('t/viewdetails', $str));
 143              $links .= ' ' . $editendpointlink;
 144  
 145              // User field mapping.
 146              $params = ['issuerid' => $issuer->get('id')];
 147              $edituserfieldmappingsurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', $params);
 148              $str = get_string('edituserfieldmappings', 'tool_oauth2');
 149              $edituserfieldmappinglink = html_writer::link($edituserfieldmappingsurl, $this->pix_icon('t/user', $str));
 150              $links .= ' ' . $edituserfieldmappinglink;
 151  
 152              // Delete.
 153              $deleteurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['id' => $issuer->get('id'), 'action' => 'delete']);
 154              $deletelink = html_writer::link($deleteurl, $this->pix_icon('t/delete', get_string('delete')));
 155              $links .= ' ' . $deletelink;
 156              // Enable / Disable.
 157              if ($issuer->get('enabled')) {
 158                  // Disable.
 159                  $disableparams = ['id' => $issuer->get('id'), 'sesskey' => sesskey(), 'action' => 'disable'];
 160                  $disableurl = new moodle_url('/admin/tool/oauth2/issuers.php', $disableparams);
 161                  $disablelink = html_writer::link($disableurl, $this->pix_icon('t/hide', get_string('disable')));
 162                  $links .= ' ' . $disablelink;
 163              } else {
 164                  // Enable.
 165                  $enableparams = ['id' => $issuer->get('id'), 'sesskey' => sesskey(), 'action' => 'enable'];
 166                  $enableurl = new moodle_url('/admin/tool/oauth2/issuers.php', $enableparams);
 167                  $enablelink = html_writer::link($enableurl, $this->pix_icon('t/show', get_string('enable')));
 168                  $links .= ' ' . $enablelink;
 169              }
 170              if (!$last) {
 171                  // Move down.
 172                  $params = ['id' => $issuer->get('id'), 'action' => 'movedown', 'sesskey' => sesskey()];
 173                  $movedownurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 174                  $movedownlink = html_writer::link($movedownurl, $this->pix_icon('t/down', get_string('movedown')));
 175                  $links .= ' ' . $movedownlink;
 176              }
 177              if (!$first) {
 178                  // Move up.
 179                  $params = ['id' => $issuer->get('id'), 'action' => 'moveup', 'sesskey' => sesskey()];
 180                  $moveupurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 181                  $moveuplink = html_writer::link($moveupurl, $this->pix_icon('t/up', get_string('moveup')));
 182                  $links .= ' ' . $moveuplink;
 183              }
 184  
 185              $editcell = new html_table_cell($links);
 186  
 187              $row = new html_table_row([
 188                  $namecell,
 189                  $configuredstatuscell,
 190                  $loginissuerstatuscell,
 191                  $discoverystatuscell,
 192                  $systemauthstatuscell,
 193                  $editcell,
 194              ]);
 195  
 196              $data[] = $row;
 197              $index++;
 198          }
 199          $table->data = $data;
 200          return html_writer::table($table);
 201      }
 202  
 203      /**
 204       * This function will render one beautiful table with all the endpoints.
 205       *
 206       * @param \core\oauth2\endpoint[] $endpoints - list of all endpoints.
 207       * @param int $issuerid
 208       * @return string HTML to output.
 209       */
 210      public function endpoints_table($endpoints, $issuerid) {
 211          global $CFG;
 212  
 213          $table = new html_table();
 214          $table->head  = [
 215              get_string('name'),
 216              get_string('url'),
 217              get_string('edit'),
 218          ];
 219          $table->attributes['class'] = 'admintable generaltable';
 220          $data = [];
 221  
 222          $index = 0;
 223  
 224          foreach ($endpoints as $endpoint) {
 225              // Name.
 226              $name = $endpoint->get('name');
 227              $namecell = new html_table_cell(s($name));
 228              $namecell->header = true;
 229  
 230              // Url.
 231              $url = $endpoint->get('url');
 232              $urlcell = new html_table_cell(s($url));
 233  
 234              $links = '';
 235              // Action links.
 236              $editparams = ['issuerid' => $issuerid, 'endpointid' => $endpoint->get('id'), 'action' => 'edit'];
 237              $editurl = new moodle_url('/admin/tool/oauth2/endpoints.php', $editparams);
 238              $editlink = html_writer::link($editurl, $this->pix_icon('t/edit', get_string('edit')));
 239              $links .= ' ' . $editlink;
 240  
 241              // Delete.
 242              $deleteparams = ['issuerid' => $issuerid, 'endpointid' => $endpoint->get('id'), 'action' => 'delete'];
 243              $deleteurl = new moodle_url('/admin/tool/oauth2/endpoints.php', $deleteparams);
 244              $deletelink = html_writer::link($deleteurl, $this->pix_icon('t/delete', get_string('delete')));
 245              $links .= ' ' . $deletelink;
 246  
 247              $editcell = new html_table_cell($links);
 248  
 249              $row = new html_table_row([
 250                  $namecell,
 251                  $urlcell,
 252                  $editcell,
 253              ]);
 254  
 255              $data[] = $row;
 256              $index++;
 257          }
 258          $table->data = $data;
 259          return html_writer::table($table);
 260      }
 261  
 262      /**
 263       * This function will render one beautiful table with all the user_field_mappings.
 264       *
 265       * @param \core\oauth2\user_field_mapping[] $userfieldmappings - list of all user_field_mappings.
 266       * @param int $issuerid
 267       * @return string HTML to output.
 268       */
 269      public function user_field_mappings_table($userfieldmappings, $issuerid) {
 270          global $CFG;
 271  
 272          $table = new html_table();
 273          $table->head  = [
 274              get_string('userfieldexternalfield', 'tool_oauth2'),
 275              get_string('userfieldinternalfield', 'tool_oauth2'),
 276              get_string('edit'),
 277          ];
 278          $table->attributes['class'] = 'admintable generaltable';
 279          $data = [];
 280  
 281          $index = 0;
 282  
 283          foreach ($userfieldmappings as $userfieldmapping) {
 284              // External field.
 285              $externalfield = $userfieldmapping->get('externalfield');
 286              $externalfieldcell = new html_table_cell(s($externalfield));
 287  
 288              // Internal field.
 289              $internalfield = $userfieldmapping->get('internalfield');
 290              $internalfieldcell = new html_table_cell(s($internalfield));
 291  
 292              $links = '';
 293              // Action links.
 294              $editparams = ['issuerid' => $issuerid, 'userfieldmappingid' => $userfieldmapping->get('id'), 'action' => 'edit'];
 295              $editurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', $editparams);
 296              $editlink = html_writer::link($editurl, $this->pix_icon('t/edit', get_string('edit')));
 297              $links .= ' ' . $editlink;
 298  
 299              // Delete.
 300              $deleteparams = ['issuerid' => $issuerid, 'userfieldmappingid' => $userfieldmapping->get('id'), 'action' => 'delete'];
 301              $deleteurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', $deleteparams);
 302              $deletelink = html_writer::link($deleteurl, $this->pix_icon('t/delete', get_string('delete')));
 303              $links .= ' ' . $deletelink;
 304  
 305              $editcell = new html_table_cell($links);
 306  
 307              $row = new html_table_row([
 308                  $externalfieldcell,
 309                  $internalfieldcell,
 310                  $editcell,
 311              ]);
 312  
 313              $data[] = $row;
 314              $index++;
 315          }
 316          $table->data = $data;
 317          return html_writer::table($table);
 318      }
 319  }