+ // Let's decide the context where viewhidden cap checks will happen.
+ if ($category->parent == '0') {
+ $contexttocheck = context_system::instance();
+ } else {
+ $contexttocheck = context_coursecat::instance($category->parent);
+ }
+ if (!has_capability('moodle/category:viewhiddencategories', $contexttocheck)) {