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.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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   * Template plans table.
  19   *
  20   * @package    tool_lp
  21   * @copyright  2015 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_lp\output;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir . '/tablelib.php');
  29  
  30  use html_writer;
  31  use moodle_url;
  32  use table_sql;
  33  use core_competency\template;
  34  
  35  /**
  36   * Template plans table class.
  37   *
  38   * Note that presently this table may display some rows although the current user
  39   * does not have permission to view those plans.
  40   *
  41   * @package    tool_lp
  42   * @copyright  2015 Frédéric Massart - FMCorz.net
  43   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class template_plans_table extends table_sql {
  46  
  47      /** @var context The context. */
  48      protected $context;
  49  
  50      /** @var \core_competency\template The template. */
  51      protected $template;
  52  
  53      /**
  54       * Sets up the table.
  55       *
  56       * @param string $uniqueid Unique id of table.
  57       * @param \core_competency\template $template The template.
  58       */
  59      public function __construct($uniqueid, \core_competency\template $template) {
  60          parent::__construct($uniqueid);
  61  
  62          // This object should not be used without the right permissions.
  63          if (!$template->can_read()) {
  64              throw new \required_capability_exception($template->get_context(), 'moodle/competency:templateview',
  65                  'nopermissions', '');
  66          }
  67  
  68          // Set protected properties.
  69          $this->template = $template;
  70          $this->context = $this->template->get_context();
  71          $this->useridfield = 'userid';
  72  
  73          // Define columns in the table.
  74          $this->define_table_columns();
  75  
  76          // Define configs.
  77          $this->define_table_configs();
  78      }
  79  
  80      /**
  81       * Format column name.
  82       *
  83       * @param  stdClass $row
  84       * @return string
  85       */
  86      protected function col_name($row) {
  87          return html_writer::link(new moodle_url('/admin/tool/lp/plan.php', array('id' => $row->id)),
  88              format_string($row->name, true, array('context' => $this->context)));
  89      }
  90  
  91      /**
  92       * Setup the headers for the table.
  93       */
  94      protected function define_table_columns() {
  95          // TODO Does not support custom user profile fields (MDL-70456).
  96          $extrafields = \core_user\fields::get_identity_fields($this->context, false);
  97  
  98          // Define headers and columns.
  99          $cols = array(
 100              'name' => get_string('planname', 'tool_lp'),
 101              'fullname' => get_string('name')
 102          );
 103  
 104          // Add headers for extra user fields.
 105          foreach ($extrafields as $field) {
 106              if (get_string_manager()->string_exists($field, 'moodle')) {
 107                  $cols[$field] = get_string($field);
 108              } else {
 109                  $cols[$field] = $field;
 110              }
 111          }
 112  
 113          // Add remaining headers.
 114          $cols = array_merge($cols, array());
 115  
 116          $this->define_columns(array_keys($cols));
 117          $this->define_headers(array_values($cols));
 118      }
 119  
 120      /**
 121       * Define table configs.
 122       */
 123      protected function define_table_configs() {
 124          $this->collapsible(false);
 125          $this->sortable(true, 'lastname', SORT_ASC);
 126          $this->pageable(true);
 127      }
 128  
 129      /**
 130       * Builds the SQL query.
 131       *
 132       * @param bool $count When true, return the count SQL.
 133       * @return array containing sql to use and an array of params.
 134       */
 135      protected function get_sql_and_params($count = false) {
 136          $fields = 'p.id, p.userid, p.name';
 137  
 138          // Add extra user fields that we need for the graded user.
 139          // TODO Does not support custom user profile fields (MDL-70456).
 140          $userfieldsapi = \core_user\fields::for_identity($this->context, false)->with_name();
 141          $fields .= $userfieldsapi->get_sql('u')->selects;
 142  
 143          if ($count) {
 144              $select = "COUNT(1)";
 145          } else {
 146              $select = "$fields";
 147          }
 148  
 149          $sql = "SELECT $select
 150                    FROM {" . \core_competency\plan::TABLE . "} p
 151                    JOIN {user} u ON u.id = p.userid
 152                   WHERE p.templateid = :templateid";
 153          $params = array('templateid' => $this->template->get('id'));
 154  
 155          // Add order by if needed.
 156          if (!$count && $sqlsort = $this->get_sql_sort()) {
 157              $sql .= " ORDER BY " . $sqlsort;
 158          }
 159  
 160          return array($sql, $params);
 161      }
 162  
 163      /**
 164       * Override the default implementation to set a decent heading level.
 165       */
 166      public function print_nothing_to_display() {
 167          global $OUTPUT;
 168          echo $this->render_reset_button();
 169          $this->print_initials_bar();
 170          echo $OUTPUT->heading(get_string('nothingtodisplay'), 4);
 171      }
 172  
 173      /**
 174       * Query the DB.
 175       *
 176       * @param int $pagesize size of page for paginated displayed table.
 177       * @param bool $useinitialsbar do you want to use the initials bar.
 178       */
 179      public function query_db($pagesize, $useinitialsbar = true) {
 180          global $DB;
 181  
 182          list($countsql, $countparams) = $this->get_sql_and_params(true);
 183          list($sql, $params) = $this->get_sql_and_params();
 184          $total = $DB->count_records_sql($countsql, $countparams);
 185          $this->pagesize($pagesize, $total);
 186          $this->rawdata = $DB->get_records_sql($sql, $params, $this->get_page_start(), $this->get_page_size());
 187  
 188          // Set initial bars.
 189          if ($useinitialsbar) {
 190              $this->initialbars($total > $pagesize);
 191          }
 192      }
 193  }