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  /**
  18   * Template cohorts 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 cohorts 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 cohorts.
  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_cohorts_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  
  72          // Define columns in the table.
  73          $this->define_table_columns();
  74  
  75          // Define configs.
  76          $this->define_table_configs();
  77      }
  78  
  79      /**
  80       * Column actions.
  81       *
  82       * @param  object $row
  83       * @return string
  84       */
  85      protected function col_actions($row) {
  86          global $OUTPUT;
  87  
  88          $action = new \confirm_action(get_string('areyousure'));
  89          $url = new moodle_url($this->baseurl);
  90          $url->params(array('removecohort' => $row->id, 'sesskey' => sesskey()));
  91          $actionlink = $OUTPUT->action_link($url, '', $action, null, new \pix_icon('t/delete',
  92              get_string('stopsyncingcohort', 'tool_lp')));
  93  
  94          return $actionlink;
  95  
  96      }
  97  
  98      /**
  99       * Setup the headers for the table.
 100       */
 101      protected function define_table_columns() {
 102          // Define headers and columns.
 103          $cols = array(
 104              'name' => get_string('name', 'cohort'),
 105              'idnumber' => get_string('idnumber', 'cohort'),
 106          );
 107  
 108          if ($this->template->can_manage()) {
 109              $cols['actions'] = get_string('actions');
 110          }
 111  
 112          $this->define_columns(array_keys($cols));
 113          $this->define_headers(array_values($cols));
 114      }
 115  
 116      /**
 117       * Define table configs.
 118       */
 119      protected function define_table_configs() {
 120          $this->collapsible(false);
 121          $this->sortable(true, 'name', SORT_ASC);
 122          $this->pageable(true);
 123          $this->no_sorting('actions');
 124      }
 125  
 126      /**
 127       * Builds the SQL query.
 128       *
 129       * @param bool $count When true, return the count SQL.
 130       * @return array containing sql to use and an array of params.
 131       */
 132      protected function get_sql_and_params($count = false) {
 133          $fields = 'c.id, c.name, c.idnumber';
 134  
 135          if ($count) {
 136              $select = "COUNT(1)";
 137          } else {
 138              $select = "$fields";
 139          }
 140  
 141          $sql = "SELECT $select
 142                    FROM {" . \core_competency\template_cohort::TABLE . "} tc
 143                    JOIN {cohort} c ON c.id = tc.cohortid
 144                   WHERE tc.templateid = :templateid";
 145          $params = array('templateid' => $this->template->get('id'));
 146  
 147          // Add order by if needed.
 148          if (!$count && $sqlsort = $this->get_sql_sort()) {
 149              $sql .= " ORDER BY " . $sqlsort;
 150          }
 151  
 152          return array($sql, $params);
 153      }
 154  
 155      /**
 156       * Override the default implementation to set a decent heading level.
 157       */
 158      public function print_nothing_to_display() {
 159          global $OUTPUT;
 160          echo $this->render_reset_button();
 161          $this->print_initials_bar();
 162          echo $OUTPUT->heading(get_string('nothingtodisplay'), 4);
 163      }
 164  
 165      /**
 166       * Query the DB.
 167       *
 168       * @param int $pagesize size of page for paginated displayed table.
 169       * @param bool $useinitialsbar do you want to use the initials bar.
 170       */
 171      public function query_db($pagesize, $useinitialsbar = true) {
 172          global $DB;
 173  
 174          list($countsql, $countparams) = $this->get_sql_and_params(true);
 175          list($sql, $params) = $this->get_sql_and_params();
 176          $total = $DB->count_records_sql($countsql, $countparams);
 177          $this->pagesize($pagesize, $total);
 178          $this->rawdata = $DB->get_records_sql($sql, $params, $this->get_page_start(), $this->get_page_size());
 179  
 180          // Set initial bars.
 181          if ($useinitialsbar) {
 182              $this->initialbars($total > $pagesize);
 183          }
 184      }
 185  }