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!

  • http://moodlenews.com Joseph Thibault

    perhaps one of the best and worst things about Moodle’s docs at Moodle.org is that any registered user can contribute to forums and edit pages for the documentation. It’s incredibly easy which, perhaps, has an effect on the overall quality of some of the more nuanced development efforts.

    By adding your own findings to the wiki you can help countless other developers down the road (and perhaps even impact the overall codebase with fixes!).

  • http://www.louisvuittonoutlethandbags2011.com louis vuitton

    Hey! I’m at work browsing your blog from my new apple iphone! Just wanted to say I love reading your blog and look forward to all your posts! Keep up the great work