diff --git a/course/lib.php b/course/lib.php
index 6a75714..2a99a2e 100644
--- a/course/lib.php
+++ b/course/lib.php
@@ -2403,7 +2403,7 @@ function print_courses($category) {
         echo html_writer::start_tag('ul', array('class'=>'unlist'));
         foreach ($courses as $course) {
             $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
-            if ($course->visible == 1 || has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
+            if (($course->visible == 1 && has_capability('moodle/course:view', $coursecontext)) || has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
                 echo html_writer::start_tag('li');
                 print_course($course);
                 echo html_writer::end_tag('li');
@@ -2623,46 +2623,48 @@ function print_my_moodle() {
 
 
 function print_course_search($value="", $return=false, $format="plain") {
-    global $CFG;
+    global $CFG, $PAGE, $USER;
     static $count = 0;
 
     $count++;
 
-    $id = 'coursesearch';
-
-    if ($count > 1) {
-        $id .= $count;
-    }
-
-    $strsearchcourses= get_string("searchcourses");
-
-    if ($format == 'plain') {
-        $output  = '<form id="'.$id.'" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
-        $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
-        $output .= '<label for="coursesearchbox">'.$strsearchcourses.': </label>';
-        $output .= '<input type="text" id="coursesearchbox" size="30" name="search" value="'.s($value).'" />';
-        $output .= '<input type="submit" value="'.get_string('go').'" />';
-        $output .= '</fieldset></form>';
-    } else if ($format == 'short') {
-        $output  = '<form id="'.$id.'" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
-        $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
-        $output .= '<label for="shortsearchbox">'.$strsearchcourses.': </label>';
-        $output .= '<input type="text" id="shortsearchbox" size="12" name="search" alt="'.s($strsearchcourses).'" value="'.s($value).'" />';
-        $output .= '<input type="submit" value="'.get_string('go').'" />';
-        $output .= '</fieldset></form>';
-    } else if ($format == 'navbar') {
-        $output  = '<form id="coursesearchnavbar" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
-        $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
-        $output .= '<label for="navsearchbox">'.$strsearchcourses.': </label>';
-        $output .= '<input type="text" id="navsearchbox" size="20" name="search" alt="'.s($strsearchcourses).'" value="'.s($value).'" />';
-        $output .= '<input type="submit" value="'.get_string('go').'" />';
-        $output .= '</fieldset></form>';
-    }
+    if (has_capability('moodle/course:view', $PAGE->context)) {
+        $id = 'coursesearch';
 
-    if ($return) {
-        return $output;
+        if ($count > 1) {
+            $id .= $count;
+        }
+
+        $strsearchcourses= get_string("searchcourses");
+
+        if ($format == 'plain') {
+            $output  = '<form id="'.$id.'" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
+            $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
+            $output .= '<label for="coursesearchbox">'.$strsearchcourses.': </label>';
+            $output .= '<input type="text" id="coursesearchbox" size="30" name="search" value="'.s($value).'" />';
+            $output .= '<input type="submit" value="'.get_string('go').'" />';
+            $output .= '</fieldset></form>';
+        } else if ($format == 'short') {
+            $output  = '<form id="'.$id.'" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
+            $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
+            $output .= '<label for="shortsearchbox">'.$strsearchcourses.': </label>';
+            $output .= '<input type="text" id="shortsearchbox" size="12" name="search" alt="'.s($strsearchcourses).'" value="'.s($value).'" />';
+            $output .= '<input type="submit" value="'.get_string('go').'" />';
+            $output .= '</fieldset></form>';
+        } else if ($format == 'navbar') {
+            $output  = '<form id="coursesearchnavbar" action="'.$CFG->wwwroot.'/course/search.php" method="get">';
+            $output .= '<fieldset class="coursesearchbox invisiblefieldset">';
+            $output .= '<label for="navsearchbox">'.$strsearchcourses.': </label>';
+            $output .= '<input type="text" id="navsearchbox" size="20" name="search" alt="'.s($strsearchcourses).'" value="'.s($value).'" />';
+            $output .= '<input type="submit" value="'.get_string('go').'" />';
+            $output .= '</fieldset></form>';
+        }
+
+        if ($return) {
+            return $output;
+        }
+        echo $output;
     }
-    echo $output;
 }
 
 function print_remote_course($course, $width="100%") {
diff --git a/lib/navigationlib.php b/lib/navigationlib.php
index bc68711..e56d73d 100644
--- a/lib/navigationlib.php
+++ b/lib/navigationlib.php
@@ -1060,7 +1060,7 @@ class global_navigation extends navigation_node {
         }
 
         $mycourses = enrol_get_my_courses(NULL, 'visible DESC,sortorder ASC', $limit);
-        $showallcourses = (count($mycourses) == 0 || !empty($CFG->navshowallcourses));
+        $showallcourses = ((count($mycourses) == 0 && has_capability('moodle/course:view', $this->page->context)) || !empty($CFG->navshowallcourses));
         // When checking if we are to show categories there is an additional override.
         // If the user is viewing a category then we will load it regardless of settings.
         // to ensure that the navigation is consistent.
@@ -1094,14 +1094,18 @@ class global_navigation extends navigation_node {
             case CONTEXT_SYSTEM :
                 // This has already been loaded we just need to map the variable
                 $coursenode = $frontpagecourse;
-                $this->load_all_categories(null, $showcategories);
+                if (has_capability('moodle/course:view', $this->page->context) || !empty($CFG->navshowallcourses)) {
+                    $this->load_all_categories(null, $showcategories);
+                }
                 break;
             case CONTEXT_COURSECAT :
                 // This has already been loaded we just need to map the variable
                 $coursenode = $frontpagecourse;
-                $this->load_all_categories($this->page->context->instanceid, $showcategories);
-                if (array_key_exists($this->page->context->instanceid, $this->addedcategories)) {
-                    $this->addedcategories[$this->page->context->instanceid]->make_active();
+                if (has_capability('moodle/course:view', $this->page->context) || !empty($CFG->navshowallcourses)) {
+                    $this->load_all_categories($this->page->context->instanceid, $showcategories);
+                    if (array_key_exists($this->page->context->instanceid, $this->addedcategories)) {
+                        $this->addedcategories[$this->page->context->instanceid]->make_active();
+                    }
                 }
                 break;
             case CONTEXT_BLOCK :
@@ -1114,7 +1118,9 @@ class global_navigation extends navigation_node {
                 // Load the course associated with the page into the navigation
                 $course = $this->page->course;
                 if ($showcategories && !$ismycourse) {
-                    $this->load_all_categories($course->category, $showcategories);
+                    if (has_capability('moodle/course:view', $this->page->context) || !empty($CFG->navshowallcourses)) {
+                        $this->load_all_categories($course->category, $showcategories);
+                    }
                 }
                 $coursenode = $this->load_course($course);
 
@@ -1175,7 +1181,9 @@ class global_navigation extends navigation_node {
                 $cm = $this->page->cm;
 
                 if ($showcategories && !$ismycourse) {
-                    $this->load_all_categories($course->category, $showcategories);
+                    if (has_capability('moodle/course:view', $this->page->context) || !empty($CFG->navshowallcourses)) {
+                        $this->load_all_categories($course->category, $showcategories);
+                    }
                 }
 
                 // Load the course associated with the page into the navigation
@@ -1263,7 +1271,9 @@ class global_navigation extends navigation_node {
                 }
                 $course = $this->page->course;
                 if ($showcategories && !$ismycourse) {
-                    $this->load_all_categories($course->category, $showcategories);
+                    if (has_capability('moodle/course:view', $this->page->context) || !empty($CFG->navshowallcourses)) {
+                        $this->load_all_categories($course->category, $showcategories);
+                    }
                 }
                 // Load the course associated with the user into the navigation
                 $coursenode = $this->load_course($course);
@@ -1539,7 +1549,9 @@ class global_navigation extends navigation_node {
         }
         // Check if there are any categories to load.
         if (count($coursestoload) > 0) {
-            $this->load_all_courses($coursestoload);
+            if (has_capability('moodle/course:view', $this->page->context) || !empty($CFG->navshowallcourses)) {
+                $this->load_all_courses($coursestoload);
+            }
         }
     }
 
