Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  
   3  /**
   4   * Aids in capability assignment and alteration of the assigned capability.
   5   *
   6   * @package    core_course
   7   * @copyright  2013 Sam Hemelryk
   8   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
   9   */
  10  class course_capability_assignment {
  11      /**
  12       * @var array The capability that has been assigned.
  13       */
  14      protected $capability;
  15      /**
  16       * @var int The role ID that the assignment was made for.
  17       */
  18      protected $roleid;
  19      /**
  20       * @var int The context ID against which the assignment was made.
  21       */
  22      protected $contextid;
  23  
  24      /**
  25       * Assigns a capability to a role at the given context giving it permission.
  26       *
  27       * @param string|array $capability The capability to assign.
  28       * @param int $roleid The roleID to assign to.
  29       * @param int $contextid The contextID for where to make the assignment.
  30       * @return course_capability_assignment
  31       */
  32      public static function allow($capability, $roleid, $contextid) {
  33          return new course_capability_assignment($capability, $roleid, $contextid, CAP_ALLOW);
  34      }
  35  
  36      /**
  37       * Assigns a capability to a role at the given context prohibiting it.
  38       *
  39       * @param string|array $capability The capability to assign.
  40       * @param int $roleid The roleID to assign to.
  41       * @param int $contextid The contextID for where to make the assignment.
  42       * @return course_capability_assignment
  43       */
  44      public static function prohibit($capability, $roleid, $contextid) {
  45          return new course_capability_assignment($capability, $roleid, $contextid, CAP_PROHIBIT);
  46      }
  47  
  48      /**
  49       * Assigns a capability to a role at the given context preventing it.
  50       *
  51       * @param string|array $capability The capability to assign.
  52       * @param int $roleid The roleID to assign to.
  53       * @param int $contextid The contextID for where to make the assignment.
  54       * @return course_capability_assignment
  55       */
  56      public static function prevent($capability, $roleid, $contextid) {
  57          return new course_capability_assignment($capability, $roleid, $contextid, CAP_PREVENT);
  58      }
  59  
  60      /**
  61       * Creates a new course_capability_assignment object
  62       *
  63       * @param string|array $capability The capability to assign.
  64       * @param int $roleid The roleID to assign to.
  65       * @param int $contextid The contextID for where to make the assignment.
  66       * @param int $permission The permission to apply. One of CAP_ALLOW, CAP_PROHIBIT, CAP_PREVENT.
  67       * @return course_capability_assignment
  68       */
  69      protected function __construct($capability, $roleid, $contextid, $permission) {
  70          if (is_string($capability)) {
  71              $capability = array($capability);
  72          }
  73          $this->capability = $capability;
  74          $this->roleid = $roleid;
  75          $this->contextid = $contextid;
  76          $this->assign($permission);
  77      }
  78  
  79      /**
  80       * Assign a new permission.
  81       * @param int $permission One of CAP_ALLOW, CAP_PROHIBIT, CAP_PREVENT
  82       */
  83      public function assign($permission) {
  84          foreach ($this->capability as $capability) {
  85              assign_capability($capability, $permission, $this->roleid, $this->contextid, true);
  86          }
  87          accesslib_clear_all_caches_for_unit_testing();
  88      }
  89  
  90      /**
  91       * Revokes the capability assignment.
  92       */
  93      public function revoke() {
  94          foreach ($this->capability as $capability) {
  95              unassign_capability($capability, $this->roleid, $this->contextid);
  96          }
  97          accesslib_clear_all_caches_for_unit_testing();
  98      }
  99  }