Home Reference Source
public class | source

MoodleContextLevel

A class for representing Context Levels within the Moodle VLE's permissions system.

As well as the various functions and properties described in the documetation below there are also dynamically created properties with each valid context level name which get MoodleContextLevel instances for the matching level. In many instances these accessors will obviate the need to use a contructor.

const sysCtx = MoodleContextLevel.system;
const courseCtx = MoodleContextLevel.CONTEXT_COURSE;

See:

Static Member Summary

Static Public Members
public static get

An alphabetic list of all defined level names, be they full names as they appear in the Moodle source code, base names, or aliases.

public static get

An alphabetic list of all defined base names, including aliases.

public static get

A sorted list of all defined context level numbers.

public static get

A list of all context levels sorted by context level number.

public static get

A list of all existing context level names as they appear in the Moodle source code sorted from lowest context level number to highest.

Static Method Summary

Static Public Methods
public static

compare(val1: *, val2: *): number

Compare two values to see if they represent the same context level, a greater context level, or a lesser context level.

public static

isContextLevelName(val: *, strictCheck: boolean): boolean

Test if a given value is a valid Moodle Context Level Name.

public static

isContextLevelNumber(val: *, strictTypeCheck: boolean): boolean

Test if a given value is a valid Moodle Context Level Number.

public static

numberFromName(name: ContextLevelName, ContextLevelBaseName, ContextLevelAlias): ContextLevelNumber | NaN

Convert any valid name to a context level number.

public static

A factory method to build a MoodleContextLevel object from any parsable value.

public static

parseToName(level: number | string | MoodleContextLevel): MoodleContextLevelName

Try to convert a value to a context level name as used in the Moodle source code.

public static

parseToNumber(level: number | string | MoodleContextLevel): MoodleContextLevelNumber

Try to convert a value to a context level number.

Constructor Summary

Public Constructor
public

The default context is CONTEXT_SYSTEM.

Member Summary

Public Members
public get

All the level's aliases.

public get

The level's base name.

public get

The level's name as it appears in the Moodle sourse code.

public set

The level's name in any valid form.

Any name that can be parsed by the nameFromNumber() static function is acceptable.

public get

All the level's valid names in alphabetical order. This includes the level's name as used in the Moodle source code, the level's base name, and all the level's aliases.

public get

The level's numeric value.

public set

The level's numeric value, must be one of the levels defined in lib/accesslib.php in the Moodle source code.

Method Summary

Public Methods
public

Create a new Moodle context level object representing the same context level.

public

Compare this context level to another.

public

equals(val: *): boolean

Test if a given value is a Moodle context level object representing the same context level.

public

The version as a plain object indexed by:

public

The context level as a string consisting of the name followed by a space then the level number in parentheses, e.g.

Static Public Members

public static get allNames: string[] source

An alphabetic list of all defined level names, be they full names as they appear in the Moodle source code, base names, or aliases.

public static get baseNames: string[] source

An alphabetic list of all defined base names, including aliases.

public static get levelNumbers: number[] source

A sorted list of all defined context level numbers.

public static get levels: MoodleContextLevel[] source

A list of all context levels sorted by context level number.

public static get names: string[] source

A list of all existing context level names as they appear in the Moodle source code sorted from lowest context level number to highest.

Static Public Methods

public static compare(val1: *, val2: *): number source

Compare two values to see if they represent the same context level, a greater context level, or a lesser context level.

Context levels are compared based on their context level number.

Params:

NameTypeAttributeDescription
val1 *
val2 *

Return:

number

Unless both values are context level objects, NaN is returned. If val1 represents lower context level number than val2 -1 is returned, if val1 and val2 represent the same context level 0 is returned, and if val1 represents a greater context level number version than val2 1 is returned.

public static isContextLevelName(val: *, strictCheck: boolean): boolean source

Test if a given value is a valid Moodle Context Level Name.

By default names, base names, and aliases are considered valid, but with strict checking only the full context level names as used in the Moodle source code will be accepted.

Params:

NameTypeAttributeDescription
val *

the value to test.

strictCheck boolean
  • optional
  • default: false

By default any name that can be resolved to a context level number, ignoring case, will be considered valid, but if a truthy value is passed only full context level names in the correct case exactly as used in the Moodle source code will be accepted.

Return:

boolean

See:

public static isContextLevelNumber(val: *, strictTypeCheck: boolean): boolean source

Test if a given value is a valid Moodle Context Level Number.

Params:

NameTypeAttributeDescription
val *

the value to test.

strictTypeCheck boolean
  • optional
  • default: false

whether or not to enable strict type checking. With strict type cheking enabled, string representation of otherwise valid values will return false.

Return:

boolean

See:

public static numberFromName(name: ContextLevelName, ContextLevelBaseName, ContextLevelAlias): ContextLevelNumber | NaN source

Convert any valid name to a context level number. Valid names are context level names as they appear in the Moodle code, context level base names, and context level aliases.

Params:

NameTypeAttributeDescription
name ContextLevelName, ContextLevelBaseName, ContextLevelAlias

Return:

ContextLevelNumber | NaN

If the passed value can't be converted to a context level number NaN is returned.

public static parse(level: number | string | MoodleContextLevel): MoodleContextLevel source

A factory method to build a MoodleContextLevel object from any parsable value. The following are supported:

  • A valid context level number (as a number or string)
  • A valid context level name as used in the Moodle code base (in any case).
  • A valid context level base name (in any case).
  • A valid context level alias (in any case).
  • A context level object.

Params:

NameTypeAttributeDescription
level number | string | MoodleContextLevel

the context level value to parse.

Throw:

TypeError

RangeError

See:

public static parseToName(level: number | string | MoodleContextLevel): MoodleContextLevelName source

Try to convert a value to a context level name as used in the Moodle source code. The following values are supported:

  • A valid context level number (as a number or string)
  • A valid context level name as used in the Moodle code base (in any case).
  • A valid context level base name (in any case).
  • A valid context level alias (in any case).
  • A context level object.

Params:

NameTypeAttributeDescription
level number | string | MoodleContextLevel

the context level value to parse.

Return:

MoodleContextLevelName

Throw:

TypeError

RangeError

See:

public static parseToNumber(level: number | string | MoodleContextLevel): MoodleContextLevelNumber source

Try to convert a value to a context level number. The following values are supported:

  • A valid context level number (as a number or string)
  • A valid context level name as used in the Moodle code base (in any case).
  • A valid context level base name (in any case).
  • A valid context level alias (in any case).
  • A context level object.

Params:

NameTypeAttributeDescription
level number | string | MoodleContextLevel

the context level value to parse.

Return:

MoodleContextLevelNumber

Throw:

TypeError

RangeError

See:

Public Constructors

public constructor(context: ContextLevelNumber | ContextLevelName | ContextLevelBaseName | ContextLevelAlias) source

The default context is CONTEXT_SYSTEM.

Params:

NameTypeAttributeDescription
context ContextLevelNumber | ContextLevelName | ContextLevelBaseName | ContextLevelAlias

Throw:

*

TypeError

*

RangeError

Public Members

public get aliases: ContextLevelAlias[] source

All the level's aliases.

public get baseName: ContextLevelBaseName source

The level's base name.

public get name: ContextLevelName source

The level's name as it appears in the Moodle sourse code.

public set name: ContextLevelName | ContextLevelBaseName | ContextLevelAlias source

The level's name in any valid form.

Any name that can be parsed by the nameFromNumber() static function is acceptable.

Throw:

TypeError

RangeError

See:

public get names: string[] source

All the level's valid names in alphabetical order. This includes the level's name as used in the Moodle source code, the level's base name, and all the level's aliases.

public get number: number source

The level's numeric value.

public set number: ContextLevelNumber source

The level's numeric value, must be one of the levels defined in lib/accesslib.php in the Moodle source code.

Throw:

TypeError

RangeError

Public Methods

public clone(): MoodleContextLevel source

Create a new Moodle context level object representing the same context level.

public compareTo(mv: MoodleContextLevel): number source

Compare this context level to another.

Params:

NameTypeAttributeDescription
mv MoodleContextLevel

Return:

number

-1 returned if passed context level is lesser, 0 if the passed context level is the same, and 1 if the passed context level is greater. If the passed value is not a Moodle context level object, NaN will be returned.

public equals(val: *): boolean source

Test if a given value is a Moodle context level object representing the same context level.

Params:

NameTypeAttributeDescription
val *

Return:

boolean

public toObject(): Object source

The version as a plain object indexed by:

  • name
  • number
  • baseName
  • aliases

Return:

Object

public toString(): string source

The context level as a string consisting of the name followed by a space then the level number in parentheses, e.g. SYSTEM (10).

Return:

string