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 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   * Class exposing the api for the cohortroles tool.
  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  namespace tool_cohortroles;
  25  
  26  use stdClass;
  27  use context_system;
  28  use core_competency\invalid_persistent_exception;
  29  
  30  /**
  31   * Class for doing things with cohort roles.
  32   *
  33   * @copyright  2015 Damyon Wiese
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class api {
  37  
  38      /**
  39       * Create a cohort role assignment from a record containing all the data for the class.
  40       *
  41       * Requires moodle/role:manage capability at the system context.
  42       *
  43       * @param stdClass $record Record containing all the data for an instance of the class.
  44       * @return competency
  45       */
  46      public static function create_cohort_role_assignment(stdClass $record) {
  47          $cohortroleassignment = new cohort_role_assignment(0, $record);
  48          $context = context_system::instance();
  49  
  50          // First we do a permissions check.
  51          require_capability('moodle/role:manage', $context);
  52  
  53          // Validate before we check for existing records.
  54          if (!$cohortroleassignment->is_valid()) {
  55              throw new invalid_persistent_exception($cohortroleassignment->get_errors());
  56          }
  57  
  58          $existing = cohort_role_assignment::get_record((array) $record);
  59          if (!empty($existing)) {
  60              return false;
  61          } else {
  62              // OK - all set.
  63              $cohortroleassignment->create();
  64          }
  65          return $cohortroleassignment;
  66      }
  67  
  68      /**
  69       * Delete a cohort role assignment by id.
  70       *
  71       * Requires moodle/role:manage capability at the system context.
  72       *
  73       * @param int $id The record to delete. This will also remove this role from the user for all users in the system.
  74       * @return boolean
  75       */
  76      public static function delete_cohort_role_assignment($id) {
  77          $cohortroleassignment = new cohort_role_assignment($id);
  78          $context = context_system::instance();
  79  
  80          // First we do a permissions check.
  81          require_capability('moodle/role:manage', $context);
  82  
  83          // OK - all set.
  84          return $cohortroleassignment->delete();
  85      }
  86  
  87      /**
  88       * Perform a search based on the provided filters and return a paginated list of records.
  89       *
  90       * Requires moodle/role:manage capability at the system context.
  91       *
  92       * @param string $sort The column to sort on
  93       * @param string $order ('ASC' or 'DESC')
  94       * @param int $skip Number of records to skip (pagination)
  95       * @param int $limit Max of records to return (pagination)
  96       * @return array of cohort_role_assignment
  97       */
  98      public static function list_cohort_role_assignments($sort = '', $order = 'ASC', $skip = 0, $limit = 0) {
  99          $context = context_system::instance();
 100  
 101          // First we do a permissions check.
 102          require_capability('moodle/role:manage', $context);
 103  
 104          // OK - all set.
 105          return cohort_role_assignment::get_records(array(), $sort, $order, $skip, $limit);
 106      }
 107  
 108      /**
 109       * Perform a search based on the provided filters and return a paginated list of records.
 110       *
 111       * Requires moodle/role:manage capability at system context.
 112       *
 113       * @return int
 114       */
 115      public static function count_cohort_role_assignments() {
 116          $context = context_system::instance();
 117  
 118          // First we do a permissions check.
 119          require_capability('moodle/role:manage', $context);
 120  
 121          // OK - all set.
 122          return cohort_role_assignment::count_records();
 123      }
 124  
 125      /**
 126       * Sync all roles - adding and deleting role assignments as required.
 127       *
 128       * Slow. Should only be called from a background task.
 129       *
 130       * Requires moodle/role:manage capability at the system context.
 131       *
 132       * @return array('rolesadded' => array of (useridassignedto, useridassignedover, roleid),
 133       *               'rolesremoved' => array of (useridassignedto, useridassignedover, roleid))
 134       */
 135      public static function sync_all_cohort_roles() {
 136          global $DB;
 137  
 138          $context = context_system::instance();
 139  
 140          // First we do a permissions check.
 141          require_capability('moodle/role:manage', $context);
 142  
 143          // Ok ready to go.
 144          $rolesadded = array();
 145          $rolesremoved = array();
 146  
 147          // Remove any cohort role mappings for roles which have been deleted.
 148          // The role assignments are not a consideration because these will have been removed when the role was.
 149          $DB->delete_records_select('tool_cohortroles', "roleid NOT IN (SELECT id FROM {role})");
 150  
 151          // Get all cohort role assignments and group them by user and role.
 152          $all = cohort_role_assignment::get_records(array(), 'userid, roleid');
 153          // We build an better structure to loop on.
 154          $info = array();
 155          foreach ($all as $cra) {
 156              if (!isset($info[$cra->get('userid')])) {
 157                  $info[$cra->get('userid')] = array();
 158              }
 159              if (!isset($info[$cra->get('userid')][$cra->get('roleid')])) {
 160                  $info[$cra->get('userid')][$cra->get('roleid')] = array();
 161              }
 162              array_push($info[$cra->get('userid')][$cra->get('roleid')], $cra->get('cohortid'));
 163          }
 164          // Then for each user+role combo - find user context in the cohort without a role assigned.
 165  
 166          foreach ($info as $userid => $roles) {
 167              foreach ($roles as $roleid => $cohorts) {
 168                  list($cohortsql, $params) = $DB->get_in_or_equal($cohorts, SQL_PARAMS_NAMED);
 169  
 170                  $params['usercontext'] = CONTEXT_USER;
 171                  $params['roleid'] = $roleid;
 172                  $params['userid'] = $userid;
 173  
 174                  $sql = 'SELECT DISTINCT u.id AS userid, ra.id, ctx.id AS contextid
 175                            FROM {user} u
 176                            JOIN {cohort_members} cm ON u.id = cm.userid
 177                            JOIN {context} ctx ON u.id = ctx.instanceid AND ctx.contextlevel = :usercontext
 178                            LEFT JOIN {role_assignments} ra ON ra.contextid = ctx.id
 179                             AND ra.roleid = :roleid
 180                             AND ra.userid = :userid
 181                           WHERE cm.cohortid ' . $cohortsql . '
 182                             AND ra.id IS NULL';
 183  
 184                  $toadd = $DB->get_records_sql($sql, $params);
 185  
 186                  foreach ($toadd as $add) {
 187                      role_assign($roleid, $userid, $add->contextid, 'tool_cohortroles');
 188                      $rolesadded[] = array(
 189                          'useridassignedto' => $userid,
 190                          'useridassignedover' => $add->userid,
 191                          'roleid' => $roleid
 192                      );
 193                  }
 194              }
 195          }
 196  
 197          // And for each user+role combo - find user context not in the cohort with a role assigned.
 198          // If the role was assigned by this component, unassign the role.
 199          foreach ($info as $userid => $roles) {
 200              foreach ($roles as $roleid => $cohorts) {
 201                  // Now we are looking for entries NOT in the cohort.
 202                  list($cohortsql, $params) = $DB->get_in_or_equal($cohorts, SQL_PARAMS_NAMED);
 203  
 204                  $params['usercontext'] = CONTEXT_USER;
 205                  $params['roleid'] = $roleid;
 206                  $params['userid'] = $userid;
 207                  $params['component'] = 'tool_cohortroles';
 208  
 209                  $sql = 'SELECT u.id as userid, ra.id, ctx.id AS contextid
 210                            FROM {user} u
 211                            JOIN {context} ctx ON u.id = ctx.instanceid AND ctx.contextlevel = :usercontext
 212                            JOIN {role_assignments} ra ON ra.contextid = ctx.id AND ra.roleid = :roleid AND ra.userid = :userid
 213                       LEFT JOIN {cohort_members} cm ON u.id = cm.userid
 214                             AND cm.cohortid ' . $cohortsql . '
 215                           WHERE ra.component = :component AND cm.cohortid IS NULL';
 216  
 217                  $toremove = $DB->get_records_sql($sql, $params);
 218                  foreach ($toremove as $remove) {
 219                      role_unassign($roleid, $userid, $remove->contextid, 'tool_cohortroles');
 220                      $rolesremoved[] = array(
 221                          'useridassignedto' => $userid,
 222                          'useridassignedover' => $remove->userid,
 223                          'roleid' => $roleid
 224                      );
 225                  }
 226              }
 227          }
 228  
 229          // Clean the legacy role assignments which are stale.
 230          $paramsclean['usercontext'] = CONTEXT_USER;
 231          $paramsclean['component'] = 'tool_cohortroles';
 232          $sql = 'SELECT DISTINCT(ra.id), ra.roleid, ra.userid, ra.contextid, ctx.instanceid
 233                    FROM {role_assignments} ra
 234                    JOIN {context} ctx ON ctx.id = ra.contextid AND ctx.contextlevel = :usercontext
 235                    JOIN {cohort_members} cm ON cm.userid = ctx.instanceid
 236                    LEFT JOIN {tool_cohortroles} tc ON tc.cohortid = cm.cohortid
 237                      AND tc.userid = ra.userid
 238                      AND tc.roleid = ra.roleid
 239                   WHERE ra.component = :component
 240                     AND tc.id is null';
 241          if ($candidatelegacyassignments = $DB->get_records_sql($sql, $paramsclean)) {
 242              $sql = 'SELECT DISTINCT(ra.id)
 243                    FROM {role_assignments} ra
 244                    JOIN {context} ctx ON ctx.id = ra.contextid AND ctx.contextlevel = :usercontext
 245                    JOIN {cohort_members} cm ON cm.userid = ctx.instanceid
 246                    JOIN {tool_cohortroles} tc ON tc.cohortid = cm.cohortid AND tc.userid = ra.userid
 247                   WHERE ra.component = :component';
 248              if ($currentvalidroleassignments = $DB->get_records_sql($sql, $paramsclean)) {
 249                  foreach ($candidatelegacyassignments as $candidate) {
 250                      if (!array_key_exists($candidate->id, $currentvalidroleassignments)) {
 251                          role_unassign($candidate->roleid, $candidate->userid, $candidate->contextid, 'tool_cohortroles');
 252                          $rolesremoved[] = array(
 253                              'useridassignedto' => $candidate->userid,
 254                              'useridassignedover' => $candidate->instanceid,
 255                              'roleid' => $candidate->roleid
 256                          );
 257                      }
 258                  }
 259              } else {
 260                  foreach ($candidatelegacyassignments as $candidate) {
 261                      role_unassign($candidate->roleid, $candidate->userid, $candidate->contextid, 'tool_cohortroles');
 262                      $rolesremoved[] = array(
 263                          'useridassignedto' => $candidate->userid,
 264                          'useridassignedover' => $candidate->instanceid,
 265                          'roleid' => $candidate->roleid
 266                      );
 267                  }
 268              }
 269          }
 270  
 271          return array('rolesadded' => $rolesadded, 'rolesremoved' => $rolesremoved);
 272      }
 273  
 274  }