local:moodlescript:syntaxspecification
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| local:moodlescript:syntaxspecification [2018/02/26 23:32] – created admin | local:moodlescript:syntaxspecification [2026/01/13 07:58] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 6: | Ligne 6: | ||
| The MoodleScript syntax aims to be simple, so we will avoid excess of formal syntactical forms and will prefer a keyword based language that results in naturally readable statements. | The MoodleScript syntax aims to be simple, so we will avoid excess of formal syntactical forms and will prefer a keyword based language that results in naturally readable statements. | ||
| - | A moodle command is basically composed by keyword, arguments, variables and parameter lists. A command always starts with a verb (ADD, REMOVE, ENROL, BACKUP, etc.) and follows with an alternance of keywords and arguments, forming a readable sentence. Some commands may accept an argument list, f.e. to provide a set of attributes to an object to create, or giving conditional clauses for an object destruction. | + | A moodle command is basically composed by keyword, arguments, variables and parameter lists. All terms must be separated by at least one spacing char. A command always starts with a verb (ADD, REMOVE, ENROL, BACKUP, etc.) and follows with an alternance of keywords and arguments, forming a readable sentence. Some commands may accept an argument list, f.e. to provide a set of attributes to an object to create, or giving conditional clauses for an object destruction. |
| The common form of a MoodleScript command is: | The common form of a MoodleScript command is: | ||
| Ligne 44: | Ligne 44: | ||
| ===Context articulations=== | ===Context articulations=== | ||
| - | context articulations are small words that tells what is the use of an argument, in order to provide naturzal readability of the script command. | + | context articulations are small words that tell what is the use of an argument, in order to provide naturzal readability of the script command. |
| IN, FOR, TO, IF EXISTS, IF NOT EXISTS | IN, FOR, TO, IF EXISTS, IF NOT EXISTS | ||
| - | ==== Arguments and variables ==== | + | ==== Arguments, identifiers |
| Arguments are usually moodle object identifiers, | Arguments are usually moodle object identifiers, | ||
| Ligne 60: | Ligne 60: | ||
| In case the expression have some possible ambiguity, additional keywords will be used to discriminate possible cases. | In case the expression have some possible ambiguity, additional keywords will be used to discriminate possible cases. | ||
| + | === Identifiers === | ||
| + | |||
| + | when the syntax requires to identify an existing object, and this object may be identified by several information, | ||
| + | |||
| + | Thus the following identifiers are usable when searching for a user : | ||
| + | |||
| + | id:33 | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | === Special identifier case : identifier given by a function === | ||
| + | |||
| + | In some applications, | ||
| + | |||
| + | Example: | ||
| + | |||
| + | | ||
| + | |||
| + | will invoke the function '' | ||
| + | |||
| + | You cannot pass any parameters to this call, so the identifier must be fully determined using current environment globals such as $USER, $COURSE, etc. to compute the expected identifier. | ||
| + | |||
| + | Here is a sample of an application function that computes the current user's owned category to move a course in: | ||
| + | |||
| + | /** | ||
| + | * Provides an uniform scheme for a teacher category identifier. | ||
| + | * @param object $user a user object. If user is not given will return the cat identifier of | ||
| + | * the current user. | ||
| + | * @return string | ||
| + | */ | ||
| + | function local_ent_installer_get_teacher_cat_idnumber($user = null) { | ||
| + | global $USER; | ||
| + | |||
| + | if (is_null($user)) { | ||
| + | $user = $USER; | ||
| + | } | ||
| + | |||
| + | $teachercatidnum = strtoupper($user-> | ||
| + | $teachercatidnum .= ' | ||
| + | |||
| + | return $teachercatidnum; | ||
| + | } | ||
| + | |||
| + | Called in a moodlescript stack context, it will compute the category idnumber of the current user, so we can write a moodlescript move instruction as follows, moving the current course to the adequate destination: | ||
| + | |||
| + | MOVE COURSE current TO idnumber: | ||
| + | |||
| + | === Literal Argument === | ||
| + | |||
| + | Literal arguments are simple words or strings. There is at the moment a restriction on syntax as strings are not specifically delimited (or only delimited by keywords). So strings should not contain keywords expressions. this is likely why we chose keywords in strict uppercase, to minimize syntactic collision with literal usual strings. | ||
| + | |||
| + | === Variables === | ||
| + | |||
| + | We may need to inject some environmental values in the script to replace some non terminal placeholders. An execution stack can be fed at launch time with a global context data stub that will be merged with each instruction local context (adding or overriding values). Global context variables can be placed whereever in script statements or attribute lists using the Moodle common SQL named variable form: | ||
| + | |||
| + | : | ||
| + | |||
| + | To be valid, the placeholder expression MUST have at least one space character before it. | ||
| + | |||
| + | You may obtain a list of the available variables in the stack logger using the following instruction: | ||
| + | |||
| + | LIST GLOBALS | ||
| + | |||
| + | this will output, e.g. in the admin tool console, giving the console environment preset variables: | ||
| + | |||
| + | > GLOBAL CONTEXT | ||
| + | > wwwroot: http:// | ||
| + | > currentuserid: | ||
| + | > currentusername: | ||
| + | > siteshortname: | ||
| + | |||
| + | Any local invocation of a MoodleScript stack may run the stack with his own global environment variable set, to serve some specific component scoped scripting needs. | ||
| + | |||
| + | ==== Special keywords (metas) ==== | ||
| + | |||
| + | ===' | ||
| + | |||
| + | ' | ||
| + | |||
| + | The use of current will simplify scripts run within a known context, by using shorten expressions: | ||
| + | |||
| + | ADD ENROL METHOD guest TO current | ||
| + | |||
| + | For adding an enrolment method to the current course. | ||
| + | |||
| + | ENROL current INTO current AS student | ||
| + | |||
| + | For enrolling the current user (the $USER being executing the script) into the current course. | ||
| + | |||
| + | ===' | ||
| + | |||
| + | this usually addresses the first available or last available item in the current syntax context. this is used f.e. for blocks location in a region, but might also address any object locaton being sorted with a sortorder attribute. | ||
| + | |||
| + | ===' | ||
| + | |||
| + | Usually identifiers and variable can be evaluated at parse time or at check time, because they are litterals in the script, or they come from some input or global context. But this is not true in all cases. Lets take an example: | ||
| + | |||
| + | In the following scriptlet: | ||
| + | |||
| + | ADD CATEGORY "New category" | ||
| + | | ||
| + | |||
| + | MOVE COURSE idnumber: | ||
| + | |||
| + | We run into an issue because at parse time or at check time, NEWCAT category is not yet created. Thus we must tell the engine that in the second statement, we need the engine waiting the latest moment to evaulate the identifier to move the course in. | ||
| + | |||
| + | This can be done by the special keywork runtime: and we'll rewrite the scriplet as follows: | ||
| + | |||
| + | ADD CATEGORY "New category" | ||
| + | | ||
| + | |||
| + | MOVE COURSE idnumber: | ||
| + | |||
| + | Using the runtime: special keyword will prevent the parser to evaluate and resolve the identifier, and will store it's initial definition in the handler class. The handler will also NOT try to resolve it at check time, as check time only checks the conditions of execution of all the statements without executing them. At real execution time, the identifier will be resolved to get it's definitive actual value. | ||
| + | |||
| + | Note that ' | ||
| [[: | [[: | ||
local/moodlescript/syntaxspecification.1519687973.txt.gz · Dernière modification : (modification externe)
