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   * 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          $extrafields = get_extra_user_fields($this->context);
  96  
  97          // Define headers and columns.
  98          $cols = array(
  99              'name' => get_string('planname', 'tool_lp'),
 100              'fullname' => get_string('name')
 101          );
 102  
 103          // Add headers for extra user fields.
 104          foreach ($extrafields as $field) {
 105              if (get_string_manager()->string_exists($field, 'moodle')) {
 106                  $cols[$field] = get_string($field);
 107              } else {
 108                  $cols[$field] = $field;
 109              }
 110          }
 111  
 112          // Add remaining headers.
 113          $cols = array_merge($cols, array());
 114  
 115          $this->define_columns(array_keys($cols));
 116          $this->define_headers(array_values($cols));
 117      }
 118  
 119      /**
 120       * Define table configs.
 121       */
 122      protected function define_table_configs() {
 123          $this->collapsible(false);
 124          $this->sortable(true, 'lastname', SORT_ASC);
 125          $this->pageable(true);
 126      }
 127  
 128      /**
 129       * Builds the SQL query.
 130       *
 131       * @param bool $count When true, return the count SQL.
 132       * @return array containing sql to use and an array of params.
 133       */
 134      protected function get_sql_and_params($count = false) {
 135          $fields = 'p.id, p.userid, p.name, ';
 136  
 137          // Add extra user fields that we need for the graded user.
 138          $extrafields = get_extra_user_fields($this->context);
 139          foreach ($extrafields as $field) {
 140              $fields .= 'u.' . $field . ', ';
 141          }
 142          $fields .= get_all_user_name_fields(true, 'u');
 143  
 144          if ($count) {
 145              $select = "COUNT(1)";
 146          } else {
 147              $select = "$fields";
 148          }
 149  
 150          $sql = "SELECT $select
 151                    FROM {" . \core_competency\plan::TABLE . "} p
 152                    JOIN {user} u ON u.id = p.userid
 153                   WHERE p.templateid = :templateid";
 154          $params = array('templateid' => $this->template->get('id'));
 155  
 156          // Add order by if needed.
 157          if (!$count && $sqlsort = $this->get_sql_sort()) {
 158              $sql .= " ORDER BY " . $sqlsort;
 159          }
 160  
 161          return array($sql, $params);
 162      }
 163  
 164      /**
 165       * Override the default implementation to set a decent heading level.
 166       */
 167      public function print_nothing_to_display() {
 168          global $OUTPUT;
 169          echo $this->render_reset_button();
 170          $this->print_initials_bar();
 171          echo $OUTPUT->heading(get_string('nothingtodisplay'), 4);
 172      }
 173  
 174      /**
 175       * Query the DB.
 176       *
 177       * @param int $pagesize size of page for paginated displayed table.
 178       * @param bool $useinitialsbar do you want to use the initials bar.
 179       */
 180      public function query_db($pagesize, $useinitialsbar = true) {
 181          global $DB;
 182  
 183          list($countsql, $countparams) = $this->get_sql_and_params(true);
 184          list($sql, $params) = $this->get_sql_and_params();
 185          $total = $DB->count_records_sql($countsql, $countparams);
 186          $this->pagesize($pagesize, $total);
 187          $this->rawdata = $DB->get_records_sql($sql, $params, $this->get_page_start(), $this->get_page_size());
 188  
 189          // Set initial bars.
 190          if ($useinitialsbar) {
 191              $this->initialbars($total > $pagesize);
 192          }
 193      }
 194  }