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: -