Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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   * Cohort role assignments table
  19   *
  20   * @package    tool_cohortroles
  21   * @copyright  2015 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_cohortroles\output;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir . '/tablelib.php');
  29  
  30  use context_helper;
  31  use context_system;
  32  use html_writer;
  33  use moodle_url;
  34  use table_sql;
  35  
  36  /**
  37   * Cohort role assignments table.
  38   *
  39   * @package    tool_cohortroles
  40   * @copyright  2015 Damyon Wiese
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class cohort_role_assignments_table extends table_sql {
  44  
  45      /**
  46       * Sets up the table.
  47       *
  48       * @param string $uniqueid Unique id of table.
  49       * @param moodle_url $url The base URL.
  50       */
  51      public function __construct($uniqueid, $url) {
  52          global $CFG;
  53          parent::__construct($uniqueid);
  54          $context = context_system::instance();
  55  
  56          $this->context = $context;
  57  
  58          $this->rolenames = role_get_names();
  59  
  60          // This object should not be used without the right permissions.
  61          require_capability('moodle/role:manage', $context);
  62  
  63          $this->useridfield = 'userid';
  64  
  65          // Define columns in the table.
  66          $this->define_table_columns();
  67  
  68          $this->define_baseurl($url);
  69          // Define configs.
  70          $this->define_table_configs();
  71      }
  72  
  73      /**
  74       * Role name column.
  75       *
  76       * @param array $data Row data.
  77       * @return string
  78       */
  79      protected function col_rolename($data) {
  80          return $this->rolenames[$data->roleid]->localname;
  81      }
  82  
  83      /**
  84       * Cohort name column.
  85       *
  86       * @param array $data Row data.
  87       * @return string
  88       */
  89      protected function col_cohortname($data) {
  90          global $OUTPUT;
  91  
  92          $record = (object) array(
  93              'id' => $data->cohortid,
  94              'idnumber' => $data->cohortidnumber,
  95              'description' => $data->cohortdescription,
  96              'visible' => $data->cohortvisible,
  97              'name' => $data->cohortname,
  98              'theme' => $data->cohorttheme
  99          );
 100          $context = context_helper::instance_by_id($data->cohortcontextid);
 101  
 102          $exporter = new \core_cohort\external\cohort_summary_exporter($record, array('context' => $context));
 103          $cohort = $exporter->export($OUTPUT);
 104  
 105          $html = $OUTPUT->render_from_template('tool_cohortroles/cohort-in-list', $cohort);
 106          return $html;
 107      }
 108  
 109      /**
 110       * Actions column.
 111       *
 112       * @param array $data Row data.
 113       * @return string
 114       */
 115      protected function col_actions($data) {
 116          global $OUTPUT;
 117  
 118          $action = new \confirm_action(get_string('removecohortroleassignmentconfirm', 'tool_cohortroles'));
 119          $url = new moodle_url($this->baseurl);
 120          $url->params(array('removecohortroleassignment' => $data->id, 'sesskey' => sesskey()));
 121          $pix = new \pix_icon('t/delete', get_string('removecohortroleassignment', 'tool_cohortroles'));
 122          return $OUTPUT->action_link($url, '', $action, null, $pix);
 123      }
 124  
 125      /**
 126       * Setup the headers for the table.
 127       */
 128      protected function define_table_columns() {
 129          $extrafields = get_extra_user_fields($this->context);
 130  
 131          // Define headers and columns.
 132          $cols = array(
 133              'cohortname' => get_string('cohort', 'cohort'),
 134              'rolename' => get_string('role'),
 135              'fullname' => get_string('name'),
 136          );
 137  
 138          // Add headers for extra user fields.
 139          foreach ($extrafields as $field) {
 140              if (get_string_manager()->string_exists($field, 'moodle')) {
 141                  $cols[$field] = get_string($field);
 142              } else {
 143                  $cols[$field] = $field;
 144              }
 145          }
 146  
 147          // Add remaining headers.
 148          $cols = array_merge($cols, array('actions' => get_string('actions')));
 149  
 150          $this->define_columns(array_keys($cols));
 151          $this->define_headers(array_values($cols));
 152      }
 153  
 154      /**
 155       * Define table configs.
 156       */
 157      protected function define_table_configs() {
 158          $this->collapsible(false);
 159          $this->sortable(true, 'lastname', SORT_ASC);
 160          $this->pageable(true);
 161          $this->no_sorting('actions');
 162      }
 163  
 164      /**
 165       * Builds the SQL query.
 166       *
 167       * @param bool $count When true, return the count SQL.
 168       * @return array containing sql to use and an array of params.
 169       */
 170      protected function get_sql_and_params($count = false) {
 171          $fields = 'uca.id, uca.cohortid, uca.userid, uca.roleid, ';
 172          $fields .= 'c.name as cohortname, c.idnumber as cohortidnumber, c.contextid as cohortcontextid, ';
 173          $fields .= 'c.visible as cohortvisible, c.description as cohortdescription, c.theme as cohorttheme, ';
 174  
 175          // Add extra user fields that we need for the graded user.
 176          $extrafields = get_extra_user_fields($this->context);
 177          foreach ($extrafields as $field) {
 178              $fields .= 'u.' . $field . ', ';
 179          }
 180          $fields .= get_all_user_name_fields(true, 'u');
 181  
 182          if ($count) {
 183              $select = "COUNT(1)";
 184          } else {
 185              $select = "$fields";
 186          }
 187  
 188          $sql = "SELECT $select
 189                     FROM {tool_cohortroles} uca
 190                     JOIN {user} u ON u.id = uca.userid
 191                     JOIN {cohort} c ON c.id = uca.cohortid";
 192  
 193          // Check if any additional filtering is required.
 194          [$sqlwhere, $params] = $this->get_sql_where();
 195          if ($sqlwhere) {
 196              $sql .= " WHERE {$sqlwhere}";
 197          }
 198  
 199          // Add order by if needed.
 200          if (!$count && $sqlsort = $this->get_sql_sort()) {
 201              $sql .= " ORDER BY " . $sqlsort;
 202          }
 203  
 204          return array($sql, $params);
 205      }
 206  
 207      /**
 208       * Override the default implementation to set a decent heading level.
 209       */
 210      public function print_nothing_to_display() {
 211          global $OUTPUT;
 212          echo $this->render_reset_button();
 213          $this->print_initials_bar();
 214          echo $OUTPUT->heading(get_string('nothingtodisplay'), 4);
 215      }
 216  
 217      /**
 218       * Query the DB.
 219       *
 220       * @param int $pagesize size of page for paginated displayed table.
 221       * @param bool $useinitialsbar do you want to use the initials bar.
 222       */
 223      public function query_db($pagesize, $useinitialsbar = true) {
 224          global $DB;
 225  
 226          list($countsql, $countparams) = $this->get_sql_and_params(true);
 227          list($sql, $params) = $this->get_sql_and_params();
 228          $total = $DB->count_records_sql($countsql, $countparams);
 229          $this->pagesize($pagesize, $total);
 230          $this->rawdata = $DB->get_records_sql($sql, $params, $this->get_page_start(), $this->get_page_size());
 231  
 232          // Set initial bars.
 233          if ($useinitialsbar) {
 234              $this->initialbars($total > $pagesize);
 235          }
 236      }
 237  }