Methodology : Replace deprecated functions isteacher(), get_course_teachers(), isstudent(), get_course_students()
These methods were all stored in the deprecatedlib.php because they rely on an old rigid role mapping prior to 1.7. They still work but should be abandonned, so how to get rid off them ?
A solution is to have an adequate use of the capacity model :
I posted an old methodology (in french) for developers that wanted to know the process to use capability model in a new project http://moodle.org/mod/forum/discuss.php?d=83835.
I will have it soon translated in english.
Step 1 : Create capabilities that are characteristical of a particular legacy role.
The only thing to do is creating a capability which semantic clearly discriminates the teacher, non editing teacher or student or even guest.
These capabilities are added to the db/access.php as said in the developer doc on docs.moodle.org or in the above post.
Let setup a very emblematic capability :
'mod/module:isteacher' so defined :
'mod/module:isteacher' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'legacy' => array(
'student' => CAP_INHIBIT,
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'coursemanager' => CAP_INHIBIT,
'admin' => CAP_INHIBIT
)
),
and another one 'mod/module:isstudent' defined as :
'mod/module:isteacher' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'legacy' => array(
'student' => CAP_ALLOW,
'teacher' => CAP_INHIBIT,
'editingteacher' => CAP_INHIBIT,
'coursemanager' => CAP_INHIBIT,
'admin' => CAP_INHIBIT
)
),
Step 2 : review the code and replace oldway functions
The function isteacher() will be changed in :
$context = get_context_intance(CONTEXT_MODULE, $idmodule);
has_capability('mod/module:isteacher', $context);
for the current context (what copes within a module copes for a block or a course).
In the same way, function isstudent() should be rewritten as :
$context = get_context_intance(CONTEXT_MODULE, $idmodule);
has_capability('mod/module:isstudent', $context);
also in the current context.
To replace get_course_teachers() or get_course_students(), you may ask for the users who have this capability in the current context :
$teachers = get_users_by_capability($context, 'mod/module:isteacher', 'u.id,firstname,lastname,picture,email', 'lastname');
This would be done either with the 'mod/module/isstudent' capability. (fields which were asked for are usable for a fullname or a print_user).
There is of course absolutely no need to add role discriminating explicit capabilities, and this selection could be done with capabilities that have more functional signification.