public function set_sources(stdClass $course, array $courses, stdClass $category = null) {
        global $USER;

        // A cousre must always be specified.
        $this->course = $course;
        $this->courseid = $course->id;

        list($courseids, $group, $user) = calendar_set_filters($courses);
        $this->courses = $courseids;
        $this->groups = $group;
        $this->users = $user;

        // Do not show category events by default.
        $this->categoryid = null;
        $this->categories = null;

        // Determine the correct category information to show.
        // When called with a course, the category of that course is usually included too.
        // When a category was specifically requested, it should be requested with the site id.
        if (SITEID !== $this->courseid) {
            // A specific course was requested.
            // Fetch the category that this course is in, along with all parents.
            // Do not include child categories of this category, as the user many not have enrolments in those siblings or children.
            $category = \core_course_category::get($course->category, MUST_EXIST, true);
            $this->categoryid = $category->id;

            $this->categories = $category->get_parents();
            $this->categories[] = $category->id;
        } else if (null !== $category && $category->id > 0) {
            // A specific category was requested.
            // Fetch all parents of this category, along with all children too.
            $category = \core_course_category::get($category->id);
            $this->categoryid = $category->id;

            // Build the category list.
            // This includes the current category.
            $this->categories = $category->get_parents();
            $this->categories[] = $category->id;
            $this->categories = array_merge($this->categories, $category->get_all_children_ids());
        } else if (SITEID === $this->courseid) {
            // The site was requested.
            // Fetch all categories where this user has any enrolment, and all categories that this user can manage.

            // Grab the list of categories that this user has courses in.
            $coursecategories = array_flip(array_map(function($course) {
                return $course->category;
            }, $courses));

            $calcatcache = cache::make('core', 'calendar_categories');
            $this->categories = $calcatcache->get('site');
            if ($this->categories === false) {
                // Use the category id as the key in the following array. That way we do not have to remove duplicates.
                $categories = [];
                foreach (\core_course_category::get_all() as $category) {
                    if (isset($coursecategories[$category->id]) ||
                            has_capability('moodle/category:manage', $category->get_context(), $USER, false)) {
                        // If the user has access to a course in this category or can manage the category,
                        // then they can see all parent categories too.
                        $categories[$category->id] = true;
                        foreach ($category->get_parents() as $catid) {
                            $categories[$catid] = true;
                        }
                    }
                }
                $this->categories = array_keys($categories);
                $calcatcache->set('site', $this->categories);
            }
        }
    }