original_string: public static function get_related_contexts($context, $includes, array $hasanycapability = null) {
          global $DB;
          static::require_enabled();
  
          if (!in_array($includes, array('children', 'parents', 'self'))) {
              throw new coding_exception('Invalid parameter value for \'includes\'.');
          }
  
          // If context user swap it for the context_system.
          if ($context->contextlevel == CONTEXT_USER) {
              $context = context_system::instance();
          }
  
          $contexts = array($context->id => $context);
  
          if ($includes == 'children') {
              $params = array('coursecatlevel' => CONTEXT_COURSECAT, 'path' => $context->path . '/%');
              $pathlike = $DB->sql_like('path', ':path');
              $sql = "contextlevel = :coursecatlevel AND $pathlike";
              $rs = $DB->get_recordset_select('context', $sql, $params);
              foreach ($rs as $record) {
                  $ctxid = $record->id;
                  context_helper::preload_from_record($record);
                  $contexts[$ctxid] = context::instance_by_id($ctxid);
              }
              $rs->close();
  
          } else if ($includes == 'parents') {
              $children = $context->get_parent_contexts();
              foreach ($children as $ctx) {
                  $contexts[$ctx->id] = $ctx;
              }
          }
  
          // Filter according to the capabilities required.
          if (!empty($hasanycapability)) {
              foreach ($contexts as $key => $ctx) {
                  if (!has_any_capability($hasanycapability, $ctx)) {
                      unset($contexts[$key]);
                  }
              }
          }
  
          return $contexts;
      }
code: public static function get_related_contexts($context, $includes, array $hasanycapability = null) {
          global $DB;
          static::require_enabled();
  
          if (!in_array($includes, array('children', 'parents', 'self'))) {
              throw new coding_exception('Invalid parameter value for \'includes\'.');
          }
  
          // If context user swap it for the context_system.
          if ($context->contextlevel == CONTEXT_USER) {
              $context = context_system::instance();
          }
  
          $contexts = array($context->id => $context);
  
          if ($includes == 'children') {
              $params = array('coursecatlevel' => CONTEXT_COURSECAT, 'path' => $context->path . '/%');
              $pathlike = $DB->sql_like('path', ':path');
              $sql = "contextlevel = :coursecatlevel AND $pathlike";
              $rs = $DB->get_recordset_select('context', $sql, $params);
              foreach ($rs as $record) {
                  $ctxid = $record->id;
                  context_helper::preload_from_record($record);
                  $contexts[$ctxid] = context::instance_by_id($ctxid);
              }
              $rs->close();
  
          } else if ($includes == 'parents') {
              $children = $context->get_parent_contexts();
              foreach ($children as $ctx) {
                  $contexts[$ctx->id] = $ctx;
              }
          }
  
          // Filter according to the capabilities required.
          if (!empty($hasanycapability)) {
              foreach ($contexts as $key => $ctx) {
                  if (!has_any_capability($hasanycapability, $ctx)) {
                      unset($contexts[$key]);
                  }
              }
          }
  
          return $contexts;
      }
code_tokens: public static function get_related_contexts ( $ context , $ includes , array $ hasanycapability = null ) { global $ DB ; static :: require_enabled ( ) ; if ( ! in_array ( $ includes , array ( 'children' , 'parents' , 'self' ) ) ) { throw new coding_exception ( 'Invalid parameter value for \'includes\'.' ) ; } // If context user swap it for the context_system. if ( $ context -> contextlevel == CONTEXT_USER ) { $ context = context_system :: instance ( ) ; } $ contexts = array ( $ context -> id => $ context ) ; if ( $ includes == 'children' ) { $ params = array ( 'coursecatlevel' => CONTEXT_COURSECAT , 'path' => $ context -> path . '/%' ) ; $ pathlike = $ DB -> sql_like ( 'path' , ':path' ) ; $ sql = "contextlevel = :coursecatlevel AND $pathlike" ; $ rs = $ DB -> get_recordset_select ( 'context' , $ sql , $ params ) ; foreach ( $ rs as $ record ) { $ ctxid = $ record -> id ; context_helper :: preload_from_record ( $ record ) ; $ contexts [ $ ctxid ] = context :: instance_by_id ( $ ctxid ) ; } $ rs -> close ( ) ; } else if ( $ includes == 'parents' ) { $ children = $ context -> get_parent_contexts ( ) ; foreach ( $ children as $ ctx ) { $ contexts [ $ ctx -> id ] = $ ctx ; } } // Filter according to the capabilities required. if ( ! empty ( $ hasanycapability ) ) { foreach ( $ contexts as $ key => $ ctx ) { if ( ! has_any_capability ( $ hasanycapability , $ ctx ) ) { unset ( $ contexts [ $ key ] ) ; } } } return $ contexts ; }
docstring: Fetches all the relevant contexts.
  
  Note: This currently only supports system, category and user contexts. However user contexts
  behave a bit differently and will fallback on the system context. This is what makes the most
  sense because a user context does not have descendants, and only has system as a parent.
  
  @param context $context The context to start from.
  @param string $includes Defines what other contexts to find.
  Accepted values are:
  - children: All descendants
  - parents: All parents, grand parents, etc...
  - self: Context passed only.
  @param array $hasanycapability Array of capabilities passed to {@link has_any_capability()} in each context.
  @return context[] An array of contexts where keys are context IDs.
docstring_tokens: Fetches all the relevant contexts .