Testing and QA

 
 
TBC
A BUG FOUND in "has_capability" function
 

Hi All,

I am using Moodle 2.7.I am creating a block that could display some information for teachers and stuff only, and display another text for students. After searching in Google, I found this code that could do the job:

            $context = get_context_instance(CONTEXT_COURSE, $courseid);

            if(has_capability('moodle/legacy:student', $context, $sel_user_id))
            {
               $UserType[$recNo] = "Student";
            }
            if(has_capability('moodle/legacy:teacher', $context, $sel_user_id))
            {
               $UserType[$recNo] = "Staff"; //"Assitent Teacher";
            }
            if(has_capability('moodle/legacy:editingteacher', $context, $sel_user_id))
            {
               $UserType[$recNo] = "Staff"; //"Teacher";
            }
            if(has_capability('moodle/legacy:admin', $context, $sel_user_id))
            {
               $UserType[$recNo] = "Staff"; //"ADMIN";
            }

 

Where $sel_user_id is the selected user id been fetched from the database.

 

After running the code, I got this error message:

Legacy capabilities can not be used any more!

 

After searching in Moodle source code for what generating this error, I found in the "\moodle\lib\accesslib.php" at line 367 this code:

    if (strpos($capability, 'moodle/legacy:') === 0) {
        throw new coding_exception('Legacy capabilities can not be used any more!');
    }

 

After commenting this code, the code works fine.

 

Is anyone have any idea about this, and why Moodle developers stopped legacy capabilities?

 

Thanks in advance ;)

 

Best regards,

 

Ahmed

 

 

 
Average of ratings: -
Gmads
Re: A BUG FOUND in "has_capability" function
Group Particularly helpful MoodlersGroup Testers

Hi Ahmed,

Please avoid posting the same subject more than once. This thread is a duplicate of:

https://moodle.org/mod/forum/discuss.php?d=253754#p1100617

 
Average of ratings: -
TBC
Re: A BUG FOUND in "has_capability" function
 

I am very sorry... As I am desperate for a solution.

Thanks.

 
Average of ratings: -
Picture of Valery Fremaux
Re: A BUG FOUND in "has_capability" function
Group Particularly helpful Moodlers

Moreover, you cannot qualify your post as something related to a bug. This is a natural evolution of Moodle that is progressing to a cleaner implementatiion structure (always ? evil

The way in the moodle 2.6 code a role is attached to its archetype has changed. So there was no reason these capabilities still be continued for a long time. 

Of course this raises a problem of code maintenance for all contributors in some cases, although outside of very major architecture changes (such the 2.0 introduction), the shift effort often keeps being small.

 
Average of ratings:Useful (1)
TBC
Re: A BUG FOUND in "has_capability" function
 

Hi Valery,

Thanks for your reply; If it's not a bug, could you please tell me (in code example) how could I find the role of each user? could you please update my code and make it works?

I am really desperate for a solution.

 

Thanks.

 

Ahmed

 
Average of ratings: -
C'est moi :-)
Re: A BUG FOUND in "has_capability" function
Group Documentation writersGroup Particularly helpful MoodlersGroup TestersGroup Translators

Hi Ahmed,

I'm not a Moodle programmer, and won't give you a direct technical solution.

But you should take time to understand the way Moodle handles "rights" for people. It's all based on capabilities!

And you should test and rely on these rights : the rights the user has (or not) on a precise capability, in a specific context (your block).

Roles are only a way to group rights on capabilities, and affect them on specific contexts...

Hope this helps,
Séverin

 

 

 
Average of ratings:Useful (1)
Tim at Lone Pine Koala Sanctuary
Re: A BUG FOUND in "has_capability" function
Group DevelopersGroup Documentation writersGroup Particularly helpful Moodlers

That is good advice from Séverin. Section 13.2 of http://www.aosabook.org/en/moodle.html tries to explain this.

 
Average of ratings: -
TBC
Re: A BUG FOUND in "has_capability" function
 

DEAR ADMIN,

 

COULD YOU PLEASE DELETE THIS THREAD?

 

THANKS.

AHMED

 
Average of ratings: -
Picture of Helen Foster
Re: A BUG FOUND in "has_capability" function
Group DevelopersGroup Documentation writersGroup Moodle HQGroup Particularly helpful MoodlersGroup Testers

Dear Ahmed,

This thread contains a number of useful replies; thus it is not necessary for it to be deleted.

 
Average of ratings:Useful (1)
TBC
Re: A BUG FOUND in "has_capability" function
 

Dear Admin,

 

There are a duplicated thread at this URL: A BUG FOUND in "has_capability" function.

 

Thanks.

 

Ahmed

 
Average of ratings: -
Picture of Helen Foster
Re: A BUG FOUND in "has_capability" function
Group DevelopersGroup Documentation writersGroup Moodle HQGroup Particularly helpful MoodlersGroup Testers

That thread also contains a number of useful replies; thus it is not necessary for it to be deleted.

If duplicate posts are noticed soon, before they have any replies, it's fine for them to be deleted. However, it's not always possible for moderators to notice them soon, since they may be in a different time zone.

 
Average of ratings: -