[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/cohortroles/classes/output/ -> cohort_role_assignments_table.php (source)

   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          );
  99          $context = context_helper::instance_by_id($data->cohortcontextid);
 100  
 101          $exporter = new \tool_lp\external\cohort_summary_exporter($record, array('context' => $context));
 102          $cohort = $exporter->export($OUTPUT);
 103  
 104          $html = $OUTPUT->render_from_template('tool_cohortroles/cohort-in-list', $cohort);
 105          return $html;
 106      }
 107  
 108      /**
 109       * Actions column.
 110       *
 111       * @param array $data Row data.
 112       * @return string
 113       */
 114      protected function col_actions($data) {
 115          global $OUTPUT;
 116  
 117          $action = new \confirm_action(get_string('removecohortroleassignmentconfirm', 'tool_cohortroles'));
 118          $url = new moodle_url($this->baseurl);
 119          $url->params(array('removecohortroleassignment' => $data->id, 'sesskey' => sesskey()));
 120          $pix = new \pix_icon('t/delete', get_string('removecohortroleassignment', 'tool_cohortroles'));
 121          return $OUTPUT->action_link($url, '', $action, null, $pix);
 122      }
 123  
 124      /**
 125       * Setup the headers for the table.
 126       */
 127      protected function define_table_columns() {
 128          $extrafields = get_extra_user_fields($this->context);
 129  
 130          // Define headers and columns.
 131          $cols = array(
 132              'cohortname' => get_string('cohort', 'cohort'),
 133              'rolename' => get_string('role'),
 134              'fullname' => get_string('name'),
 135          );
 136  
 137          // Add headers for extra user fields.
 138          foreach ($extrafields as $field) {
 139              if (get_string_manager()->string_exists($field, 'moodle')) {
 140                  $cols[$field] = get_string($field);
 141              } else {
 142                  $cols[$field] = $field;
 143              }
 144          }
 145  
 146          // Add remaining headers.
 147          $cols = array_merge($cols, array('actions' => get_string('actions')));
 148  
 149          $this->define_columns(array_keys($cols));
 150          $this->define_headers(array_values($cols));
 151      }
 152  
 153      /**
 154       * Define table configs.
 155       */
 156      protected function define_table_configs() {
 157          $this->collapsible(false);
 158          $this->sortable(true, 'lastname', SORT_ASC);
 159          $this->pageable(true);
 160          $this->no_sorting('actions');
 161      }
 162  
 163      /**
 164       * Builds the SQL query.
 165       *
 166       * @param bool $count When true, return the count SQL.
 167       * @return array containing sql to use and an array of params.
 168       */
 169      protected function get_sql_and_params($count = false) {
 170          $fields = 'uca.id, uca.cohortid, uca.userid, uca.roleid, ';
 171          $fields .= 'c.name as cohortname, c.idnumber as cohortidnumber, c.contextid as cohortcontextid, ';
 172          $fields .= 'c.visible as cohortvisible, c.description as cohortdescription, ';
 173  
 174          // Add extra user fields that we need for the graded user.
 175          $extrafields = get_extra_user_fields($this->context);
 176          foreach ($extrafields as $field) {
 177              $fields .= 'u.' . $field . ', ';
 178          }
 179          $fields .= get_all_user_name_fields(true, 'u');
 180  
 181          if ($count) {
 182              $select = "COUNT(1)";
 183          } else {
 184              $select = "$fields";
 185          }
 186  
 187          $sql = "SELECT $select
 188                     FROM {tool_cohortroles} uca
 189                     JOIN {user} u ON u.id = uca.userid
 190                     JOIN {cohort} c ON c.id = uca.cohortid";
 191          $params = array();
 192  
 193          // Add order by if needed.
 194          if (!$count && $sqlsort = $this->get_sql_sort()) {
 195              $sql .= " ORDER BY " . $sqlsort;
 196          }
 197  
 198          return array($sql, $params);
 199      }
 200  
 201      /**
 202       * Override the default implementation to set a decent heading level.
 203       */
 204      public function print_nothing_to_display() {
 205          global $OUTPUT;
 206          echo $this->render_reset_button();
 207          $this->print_initials_bar();
 208          echo $OUTPUT->heading(get_string('nothingtodisplay'), 4);
 209      }
 210  
 211      /**
 212       * Query the DB.
 213       *
 214       * @param int $pagesize size of page for paginated displayed table.
 215       * @param bool $useinitialsbar do you want to use the initials bar.
 216       */
 217      public function query_db($pagesize, $useinitialsbar = true) {
 218          global $DB;
 219  
 220          list($countsql, $countparams) = $this->get_sql_and_params(true);
 221          list($sql, $params) = $this->get_sql_and_params();
 222          $total = $DB->count_records_sql($countsql, $countparams);
 223          $this->pagesize($pagesize, $total);
 224          $this->rawdata = $DB->get_records_sql($sql, $params, $this->get_page_start(), $this->get_page_size());
 225  
 226          // Set initial bars.
 227          if ($useinitialsbar) {
 228              $this->initialbars($total > $pagesize);
 229          }
 230      }
 231  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1