[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/cohortroles/classes/ -> api.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   * 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          // Get all cohort role assignments and group them by user and role.
 148          $all = cohort_role_assignment::get_records(array(), 'userid, roleid');
 149          // We build an better structure to loop on.
 150          $info = array();
 151          foreach ($all as $cra) {
 152              if (!isset($info[$cra->get_userid()])) {
 153                  $info[$cra->get_userid()] = array();
 154              }
 155              if (!isset($info[$cra->get_userid()][$cra->get_roleid()])) {
 156                  $info[$cra->get_userid()][$cra->get_roleid()] = array();
 157              }
 158              array_push($info[$cra->get_userid()][$cra->get_roleid()], $cra->get_cohortid());
 159          }
 160          // Then for each user+role combo - find user context in the cohort without a role assigned.
 161  
 162          foreach ($info as $userid => $roles) {
 163              foreach ($roles as $roleid => $cohorts) {
 164                  list($cohortsql, $params) = $DB->get_in_or_equal($cohorts, SQL_PARAMS_NAMED);
 165  
 166                  $params['usercontext'] = CONTEXT_USER;
 167                  $params['roleid'] = $roleid;
 168                  $params['userid'] = $userid;
 169  
 170                  $sql = 'SELECT u.id AS userid, ra.id, ctx.id AS contextid
 171                            FROM {user} u
 172                            JOIN {cohort_members} cm ON u.id = cm.userid
 173                            JOIN {context} ctx ON u.id = ctx.instanceid AND ctx.contextlevel = :usercontext
 174                            LEFT JOIN {role_assignments} ra ON ra.contextid = ctx.id
 175                             AND ra.roleid = :roleid
 176                             AND ra.userid = :userid
 177                           WHERE cm.cohortid ' . $cohortsql . '
 178                             AND ra.id IS NULL';
 179  
 180                  $toadd = $DB->get_records_sql($sql, $params);
 181  
 182                  foreach ($toadd as $add) {
 183                      role_assign($roleid, $userid, $add->contextid, 'tool_cohortroles');
 184                      $rolesadded[] = array(
 185                          'useridassignedto' => $userid,
 186                          'useridassignedover' => $add->userid,
 187                          'roleid' => $roleid
 188                      );
 189                  }
 190              }
 191          }
 192  
 193          // And for each user+role combo - find user context not in the cohort with a role assigned.
 194          // If the role was assigned by this component, unassign the role.
 195          foreach ($info as $userid => $roles) {
 196              foreach ($roles as $roleid => $cohorts) {
 197                  // Now we are looking for entries NOT in the cohort.
 198                  list($cohortsql, $params) = $DB->get_in_or_equal($cohorts, SQL_PARAMS_NAMED);
 199  
 200                  $params['usercontext'] = CONTEXT_USER;
 201                  $params['roleid'] = $roleid;
 202                  $params['userid'] = $userid;
 203                  $params['component'] = 'tool_cohortroles';
 204  
 205                  $sql = 'SELECT u.id as userid, ra.id, ctx.id AS contextid
 206                            FROM {user} u
 207                            JOIN {context} ctx ON u.id = ctx.instanceid AND ctx.contextlevel = :usercontext
 208                            JOIN {role_assignments} ra ON ra.contextid = ctx.id AND ra.roleid = :roleid AND ra.userid = :userid
 209                       LEFT JOIN {cohort_members} cm ON u.id = cm.userid
 210                             AND cm.cohortid ' . $cohortsql . '
 211                           WHERE ra.component = :component AND cm.cohortid IS NULL';
 212  
 213                  $toremove = $DB->get_records_sql($sql, $params);
 214                  foreach ($toremove as $remove) {
 215                      role_unassign($roleid, $userid, $remove->contextid, 'tool_cohortroles');
 216                      $rolesremoved[] = array(
 217                          'useridassignedto' => $userid,
 218                          'useridassignedover' => $remove->userid,
 219                          'roleid' => $roleid
 220                      );
 221                  }
 222              }
 223          }
 224  
 225          return array('rolesadded' => $rolesadded, 'rolesremoved' => $rolesremoved);
 226      }
 227  
 228  }


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