Easily build a great vocabulary without studying! Now on the App Store!
Click here for more info about the app

How to Check if the Current Logged In User can Edit a Course in MOODLE Using the Has_capability Function


After an hour of fruitless searching through the docs and forums, I finally found an answer to my simple question, of course buried in the depths of the shark infested, murky water that is the Moodle code base .

How do I check if a user is a course creator for a given course? I'm still not 100% sure, the access control policy seems needlessly confusing on first glance and even after reading the heady and mostly pointless, expose on how roles/perms are calculated (Available Here: http://docs.moodle.org/en/How_permissions_are_calculated ) If you notice the section titled "Some Practical Examples" is 100% empty! (I've added my code to the wiki so not 100% empty any longer) The most useful part of the docs are completely empty! Instead boatloads of time went into a treatise on the internals and lovely graphs like this one

But you could always hop into the forum dedicated just to this chunk of the code base (Available here: http://moodle.org/mod/forum/view.php?f=941&page=0) and search through the 500 topics w/ 5000+(i didn't actually count) nested discussions (including version 1.7 of moodle as well).

No, instead, ACK came to the rescue and I found a snippet that performed the task.

Without further ado

$context = get_context_instance(CONTEXT_COURSE, $course->id);
if (has_capability('moodle/course:update', $coursecontext)) {
  /* do stuff here /
There it is. That could have saved me an hour if mentioned in the docs!

Tagged w/ #context #course #has_capbility #moodle #php #teachermoodle #php #programming