Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-31053

"programmer error" in get_user_capability_course


    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.2, 2.3
    • Fix Version/s: 2.2.2
    • Component/s: Libraries
    • Labels:
    • Environment:
      PHP 5.3.6
    • Database:
    • Testing Instructions:

      for developers: create script that uses get_user_capability_course() and execute it

      for developers: create script that uses get_user_capability_course() and execute it
    • Affected Branches:
    • Fixed Branches:
    • Pull from Repository:


      Perhaps this is untested, because it seems that core Moodle never calls get_user_capability_course(). It is seen here because it gets called from a third-party plugin, but the source of the error is in lib/accesslib.php, in get_user_capability_course(). in get_user_capability_course a query is made and the resulting stdClass objects are passed to has_capability(), but has_capability is expecting an object of type context.

      Error seen in Apache error log when the Lightwork client attempts to connect and get some information from Moodle:

      [Fri Jan 06 14:23:13 2012] [error] [client] Default exception handler: Coding error detected, it must be fixed by a programmer: PHP catchable fatal error Debug: Argument 2 passed to has_capability() must be an instance of context, instance of stdClass given, called in /private/var/www/postmoodle/lib/accesslib.php on line 3870 and defined\n* line 365 of /lib/setuplib.php: coding_exception thrown\n* line 348 of /lib/accesslib.php: call to default_error_handler()\n* line 3870 of /lib/accesslib.php: call to has_capability()\n* line 398 of /local/lightwork/lib/lw_marker.php: call to get_user_capability_course()\n* line 81 of /local/lightwork/lib/lw_marker.php: call to LW_Marker->load_courses()\n* line 362 of /local/lightwork/lib/lw_marker.php: call to LW_Marker->get_courses()\n* line 286 of /local/lightwork/ws/mdl_soapserver.class.php: call to LW_Marker->courses_modified_since()\n* line ? of unknownfile: call to mdl_soapserver->getCourses()\n* line 4087 of /local/lightwork/ws/lib/nusoap.php: call to call_user_func_array()\n* line 3718 of /local/lightwork/ws/lib/nusoap.php: call to nusoap_server->invoke_method()\n* line 51 of /local/lightwork/ws/service.php: call to nusoap_server->service()\n

      Here is a fix:

      diff --git a/lib/accesslib.php b/lib/accesslib.php
      index b7c0c94..91e4c28 100644
      --- a/lib/accesslib.php
      +++ b/lib/accesslib.php
      @@ -3866,15 +3866,16 @@ function get_user_capability_course($capability, $userid = null, $doanything = t
                                                ON (c.id=x.instanceid AND x.contextlevel=".CONTEXT_COURSE.")
           // Check capability for each course in turn
      -    foreach ($rs as $coursecontext) {
      +    foreach ($rs as $courseinfo) {
      +        $coursecontext = context_course::instance($courseinfo->courseid);
               if (has_capability($capability, $coursecontext, $userid, $doanything)) {
                   // We've got the capability. Make the record look like a course record
                   // and store it
      -            $coursecontext->id = $coursecontext->courseid;
      -            unset($coursecontext->courseid);
      -            unset($coursecontext->contextlevel);
      -            unset($coursecontext->instanceid);
      -            $courses[] = $coursecontext;
      +            $courseinfo->id = $courseinfo->courseid;
      +            unset($courseinfo->courseid);
      +            unset($courseinfo->contextlevel);
      +            unset($courseinfo->instanceid);
      +            $courses[] = $courseinfo;

        Gliffy Diagrams


            Issue Links



                • Votes:
                  0 Vote for this issue
                  2 Start watching this issue


                  • Created:
                    Fix Release Date: