Hi, I'm trying to figure out how many courses a given user has the Course Creator role for. I thought while I was at it I'd find or write a function that would do the same for other roles too. What I'm looking for is something like get_my_courses() but with one big difference: I'd like to feed it a $roleid. I thought to substitute the has_capability() section with something like user_has_role_assignment(). The problem is that user_has_role_assignment() only works for assignments directly on that course, not if the user has the role assigned, say, on the category which the course is contained in.
Is there a built in function that does what I want to do?
Here is what I tried (based on get_my_courses). Note the line in red which is what I think I need to change:
function get_my_courses_by_role($userid, $sort='visible DESC,sortorder ASC', $fields='*', $roleid, $countonly='0') {
if ($countonly == 0 ) {
$mycourses = array();
} else {
$mycourses = 0;
}
$rs = get_recordset('course', '', '', $sort, $fields);
if ($rs && $rs->RecordCount() > 0) {
while (!$rs->EOF) {
$course = (object)$rs->fields;
if ($course->id != SITEID) {
$context = get_context_instance(CONTEXT_COURSE, $course->id);
// now we want to use user_has_role_assignment() instead.
if ( user_has_role_assignment($userid,$roleid,$context->id)) {
if ($countonly == 0) {
$mycourses[$course->id] = $course;
} else { //or just do a count
$mycourses++;
}
}
}
$rs->MoveNext();
}
}
return $mycourses;
}
Thanks, Greg.
P.S. there must be a way to insert code, preserve indenting, and format the text...but how?
General developer forum
which function to get a coursecount in 1.7...
This discussion has been locked so you can no longer reply to it.