Moodle

Function 'role_assign' does not return the id of the assignment

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.9.2, 2.0
  • Fix Version/s: 2.0
  • Component/s: Enrolments
  • Labels:
    None
  • Environment:
    Solaris 10, Oracle 10.2.0.2, version 1.9.2+ build 20080716
  • Database:
    Oracle
  • Affected Branches:
    MOODLE_19_STABLE, MOODLE_20_STABLE
  • Fixed Branches:
    MOODLE_20_STABLE

Description

The documentation of the function 'role_assign' says that the function returns the new id of the assignment, but actually the function returns 'true' if successfull, false otherwise. I would suggest to change the implementation to match the documentation. When unsuccessfull, the function should still return false. This should not break existing code, as the id is > 0, and therefore will evaluate to true. I can provide a patch. This is a problem for us, as we need the id to integrate with our other backend systems.

The issue is not visible. In order to experience it, write code that calls the function 'role_assign' with valid arguments, then print out the return value. But it is probably a lot easier just reading the code.
The actual result is true, the expected result is $ra->id

Activity

Hide
Dan Poltawski added a comment -

Hmm, this is a pretty simple fix worth considering..

diff --git a/lib/accesslib.php b/lib/accesslib.php
index 84a731d..847897e 100755
— a/lib/accesslib.php
+++ b/lib/accesslib.php
@@ -2803,7 +2803,7 @@ function role_assign($roleid, $userid, $groupid, $contexti

events_trigger('role_assigned', $ra);

  • return true;
    + return $ra->id;
    }
Show
Dan Poltawski added a comment - Hmm, this is a pretty simple fix worth considering.. diff --git a/lib/accesslib.php b/lib/accesslib.php index 84a731d..847897e 100755 — a/lib/accesslib.php +++ b/lib/accesslib.php @@ -2803,7 +2803,7 @@ function role_assign($roleid, $userid, $groupid, $contexti events_trigger('role_assigned', $ra);
  • return true; + return $ra->id; }
Hide
Dan Poltawski added a comment -

Hi Gerd,

Thanks for the report - I'm going to fix this in HEAD and we can then leave it a while to see if it breaks anything major before considering applying to 1.9.

I suspect it was an oversight in the 1.9 accesslib overhaul.

Show
Dan Poltawski added a comment - Hi Gerd, Thanks for the report - I'm going to fix this in HEAD and we can then leave it a while to see if it breaks anything major before considering applying to 1.9. I suspect it was an oversight in the 1.9 accesslib overhaul.
Hide
Dan Poltawski added a comment -

I've fixed this in HEAD - I would not like to attempt this fix in 1.9 at the moment

Show
Dan Poltawski added a comment - I've fixed this in HEAD - I would not like to attempt this fix in 1.9 at the moment

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: