General developer forum

Get list of a user's courses which they have access to AND honor system permissions/capabilities

 
Picture of Bobby Siegfried
Get list of a user's courses which they have access to AND honor system permissions/capabilities
 

Hi everyone,

I currently have the following code working in a block, but I'm not convinced I'm doing this properly. There appears to be a few functions built in to Moodle core that should be able to accomplish what I'm doing but I was only able to get so far. Here's what I've done.

I started by using the "enrol_get_my_courses" Moodle core function (found in /lib/enrollib.php). This worked perfectly EXCEPT that it ignores whether or not the logged in user has the ability to view hidden categories (moodle/category:viewhiddencategories). As a result, a user who may be enrolled in courses inside a hidden category will still see those courses in the list that is returned by this function.

For obvious reasons, I did not want to modify a core file, so I copied a snippet from the "enrol_get_my_courses" function and added it to my code (the block of code I copied and edited is within my foreach loop beginning with 'context_helper::preload...' and the following 'if' statement. I added a check within that statement to check if the user had the capability of seeing hidden categories, and this works.

But looking at my code, and knowing that the navigation tree and course overview block definitely honor the permission to view hidden categories, I thought there must be a more efficient way to do what I'm doing, or a core function that I'm missing. I would appreciate any feedback or advice anyone can offer. Thanks in advance!

My code:

public function get_content() {
        global $CFG, $USER, $DB, $OUTPUT, $PAGE;

        if ($this->content !== null) {
            return $this->content;
        }

        $this->content = new stdClass;
        $this->content->text = '';
        $this->content->footer = '';
        //$this->content->text = get_string('userloggedin', 'block_current_courses', $USER->id);


        $courses = enrol_get_my_courses();

        $n = count($courses); // get the number of rows returned

        foreach ($courses as $id=>$c) {

            context_helper::preload_from_record($c);
            if (!$c->visible) {
                if (!$context = context_course::instance($id, IGNORE_MISSING)) {
                    unset($courses[$id]);
                    continue;
                }
                if (!has_capability('moodle/category:viewhiddencategories', $context)) {
                    unset($courses[$id]);
                    continue;
                }
            }

            $cc = $DB->get_record('course_categories', array('id'=>$c->category), $fields='name', $strictness=IGNORE_MISSING);

            if (isset($cat)) {
                $this->content->text .= ($cat !== $c->category) ? '</ul><h3>' . $cc->name . '</h3><ul>' : '';
            } else {
                $this->content->text .= '<h3>' . $cc->name . '</h3><ul>';
            }

            $this->content->text .= '<li><a href="' . $CFG->wwwroot . '/course/view.php?id=' . $c->id . '" ' . (($c->visible == 0) ? 'class="dimmed"' : '') . '>' . $c->fullname . '</a></li>';

            $cat = $c->category;

            $n--;
            if ($n == 0) { $this->content->text .= '</ul>'; } // close bulleted list when there are no more rows to output
        }


        return $this->content;

    }
 
Average of ratings: -
Picture of Bobby Siegfried
Re: Get list of a user's courses which they have access to AND honor system permissions/capabilities
 

Has anyone seen this behavior before? As I'm digging deeper into this, it appears that the Moodle core functionality for the Course Overview block has the same problem - and I'm wondering if I'm actually discovered a bug. The Course Overview block shows ALL courses, including courses in hidden categories which completely ignores whether the user who is logged in has the ability to view that category.

 
Average of ratings: -
Picture of Darko Miletić
Re: Get list of a user's courses which they have access to AND honor system permissions/capabilities
Core developersParticularly helpful Moodlers

But looking at my code, and knowing that the navigation tree and course overview block definitely honor the permission to view hidden categories, I thought there must be a more efficient way to do what I'm doing, or a core function that I'm missing.
What makes you think that? In fact the reason navigation tree honors this is because they check for the capability in the code that implements it. No big secret there. There is no any secret CORE API for this. enrol_get_my_courses does not do the check that you need so it is up to you to do it.




 
Average of ratings: -
Picture of Bobby Siegfried
Re: Get list of a user's courses which they have access to AND honor system permissions/capabilities
 

Thank you for your response. I assumed as much that there was an additional check for the My courses navigation tree. But I have been unable to find where that check is being done - hence my hesitation about my own solution. I'm always looking for more efficient ways to do something, and it felt like my solution may be duplicating core functionality found elsewhere. If that's not the case, however, then at least I know my solution is working.

 
Average of ratings: -