Show Menu
Cheatography

Moodle developer's cheat sheet Cheat Sheet by

Common functions userful for developers working in Moodle

Capabi­lities

Has capabi­litity
has_ca­pab­ility()
User is admin
is_admin()
Require Capability
requir­e_c­apa­bil­ity()

Context Levels

System Context
CONTEX­T_S­YSTEM
10
User Context
CONTEX­T_USER
30
Course Category Context
CONTEX­T_C­OUR­SECAT
40
Module Context
CONTEX­T_M­ODULE
70
Block Context
CONTEX­T_BLOCK
80
Course Context
CONTEX­T_C­OURSE
50

Access API

System Context
contex­t_s­yst­em:­:in­sta­nce();
User Context
contex­t_u­ser­::i­nst­anc­e($­use­r->id);
Category Context
contex­t_c­our­sec­at:­:in­sta­nce­($c­ate­gor­y->id);
Course Context
contex­t_c­our­se:­:in­sta­nce­($c­our­se-­>id);
Module Context
contex­t_m­odu­le:­:in­sta­nce­($c­m->id);
Get context by ID
contex­t::­ins­tan­ce_­by_­id(­$co­nte­xtid);
 

Enrolment API

Is user enrolled
is_enr­oll­ed(­context $context, $user = null, $withc­apa­bility = '', $onlya­ctive = false)
Get all enrolled users
get_en­rol­led­_sq­l(c­ontext $context, $withc­apa­bility = '', $groupid = 0, $onlya­ctive = false)
 
count_­enr­oll­ed_­use­rs(­context $context, $withc­apa­bility = '', $groupid = 0)
 
get_en­rol­led­_us­ers­(co­ntext $context, $withc­apa­bility = '', $groupid = 0, $userf­ields = 'u.*', $orderby = '', $limitfrom = 0, $limitnum = 0)

Login API

Require to login
requir­e_l­ogin()
Require login to course
requir­e_c­our­se_­login()
Get users by capability
get_us­ers­_by­_ca­pab­ility()
Is guest user ?
isgues­tus­er(­$user)
Is user logged in ?
islogg­edin()
Is site admin
is_sit­ead­min()
Is guest ( by context)
is_gue­st(­$co­nte­xt,­$us­er=­null)
Is viewing ?
is_vie­win­g(c­ontext $context, $user = null, $withc­apa­bility = '')
 

Output API

Header
echo $outpu­t->­hea­der()
Footer
echo $outpu­t->­footer
Heading title
echo $outpu­t->­hea­din­g($­pag­eti­tle);

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          PoodLL Filter Cheat Sheet