From 53a493030c572a601467c5da78e00ddbd2d7d210 Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Thu, 12 Aug 2021 09:18:46 +0800 Subject: [PATCH] MDL-73672 reports: fix navigation when clicking on parent Reports link --- course/tests/behat/report_navigation.feature | 49 +++++++++++++++++++ .../log/tests/behat/course_report_log.feature | 16 +++--- report/view.php | 34 +++++++++++-- 3 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 course/tests/behat/report_navigation.feature diff --git a/course/tests/behat/report_navigation.feature b/course/tests/behat/report_navigation.feature new file mode 100644 index 00000000000..fb4c7853c06 --- /dev/null +++ b/course/tests/behat/report_navigation.feature @@ -0,0 +1,49 @@ +@core @core_course +Feature: Report navigation + As a teacher + I will be redirected to the first report page on the navigation if I can't access to other reports. + + Background: + Given the following "users" exist: + | username | + | teacher | + And the following "courses" exist: + | fullname | shortname | + | Course 1 | C1 | + And the following "course enrolments" exist: + | user | course | role | + | teacher | C1 | editingteacher | + + Scenario: The teacher will be redirected to the first report page if they can't access to most recently report + Given I am on the "C1" "Course" page logged in as "teacher" + When I navigate to "Logs" in current page administration + Then I should see "Choose which logs you want to see" + When I click on "Reports" "link" + Then I should see "Choose which logs you want to see" + When the following "permission overrides" exist: + | capability | permission | role | contextlevel | reference | + | report/log:view | Prohibit | editingteacher | System | | + And I click on "Reports" "link" + Then I should see "Competency breakdown" + And I should not see "Sorry, but you do not currently have permissions to do that" + + Scenario: If capability Log view is unset, the teacher will be redirected to the first valid report page + Given the following "permission overrides" exist: + | capability | permission | role | contextlevel | reference | + | report/log:view | Prohibit | editingteacher | System | | + When I am on the "C1" "Course" page logged in as "teacher" + And I navigate to "Reports" in current page administration + Then I should see "Competency breakdown" + And I should not see "Sorry, but you do not currently have permissions to do that" + + Scenario: A warning message will be shown if the user cannot access any report page + Given the following "permission overrides" exist: + | capability | permission | role | contextlevel | reference | + | report/log:view | Prohibit | editingteacher | System | | + | report/loglive:view | Prohibit | editingteacher | System | | + | report/outline:view | Prohibit | editingteacher | System | | + | report/participation:view | Prohibit | editingteacher | System | | + | moodle/competency:coursecompetencyview | Prohibit | editingteacher | System | | + When I am on the "C1" "Course" page logged in as "teacher" + And I navigate to "Reports" in current page administration + Then I should see "No reports accessible" diff --git a/report/log/tests/behat/course_report_log.feature b/report/log/tests/behat/course_report_log.feature index 74edcf0a01b..77c3157a010 100644 --- a/report/log/tests/behat/course_report_log.feature +++ b/report/log/tests/behat/course_report_log.feature @@ -1,9 +1,9 @@ @report @report_log -Feature: In a course administration page, navigate through report page, test for report log page +Feature: In a course administration page, navigate through report page, test for first report page In order to navigate through report page As an admin Go to course administration -> reports - The reports page by default points to logs page + The reports page by default points to first report page Background: Given the following "courses" exist: @@ -24,13 +24,13 @@ Feature: In a course administration page, navigate through report page, test for | student1 | C3 | student | @javascript - Scenario: Default page accessed for Report is log page + Scenario: Default page accessed for Report is competency breakdown page Given I log in as "admin" And I am on "Course 1" course homepage When I navigate to "Reports" in current page administration Then "Report" "field" should exist - And the "Report" select box should contain "Logs" - And the field "Report" matches value "Logs" + And the "Report" select box should contain "Competency breakdown" + And the field "Report" matches value "Competency breakdown" @javascript Scenario: Verify the session setting is saved for different courses @@ -38,15 +38,15 @@ Feature: In a course administration page, navigate through report page, test for And I am on "Course 1" course homepage And I navigate to "Reports" in current page administration And "Report" "field" should exist - And the "Report" select box should contain "Logs" - And the field "Report" matches value "Logs" + And the "Report" select box should contain "Competency breakdown" + And the field "Report" matches value "Competency breakdown" # Now select the Live logs for Course 2 And I am on "Course 2" course homepage And I navigate to "Reports > Live logs" in current page administration # now come back to course 1 and see if the default is logs page or not And I am on "Course 1" course homepage And I navigate to "Reports" in current page administration - And the "Report" select box should contain "Logs" + And the "Report" select box should contain "Competency breakdown" # Now come back again to Course 2 And I am on "Course 2" course homepage When I navigate to "Reports" in current page administration diff --git a/report/view.php b/report/view.php index bdaec23e9ed..1de6e4e3b0f 100644 --- a/report/view.php +++ b/report/view.php @@ -37,10 +37,34 @@ if (!$course = $DB->get_record('course', array('id' => $courseid))) { require_login($course); // Get the last viewed Page. -if (!isset($USER->course_last_report[$courseid])) { - $lasturl = new moodle_url('/report/log/index.php', ['id' => $courseid]); -} else { - $lasturl = $USER->course_last_report[$courseid]; +$lasturl = null; +$reportsnode = $PAGE->settingsnav->find('coursereports', \navigation_node::TYPE_CONTAINER); +$reportchildrennode = $reportsnode->children; +// If there are available course reports to the user. +if ($reportsnode && $reportchildrennode->count() > 0) { + // By default, set the first valid report URL for the redirect URL. + $lasturl = $reportchildrennode->getIterator()[0]->action()->out(false); + // If exist the last viewed report page. + if (isset($USER->course_last_report[$courseid])) { + // Check if the most recently report link is existed on report navigation node. + foreach ($reportchildrennode as $node) { + if ($node->action()->get_path() === $USER->course_last_report[$courseid]->get_path()) { + $lasturl = $USER->course_last_report[$courseid]; + break; + } + } + } } +if ($lasturl) { + redirect($lasturl); +} +// Otherwise, output the page with a notification stating that there are no available course reports. +$PAGE->set_title(get_string('reports')); +$PAGE->set_pagelayout('incourse'); +$PAGE->set_heading($course->fullname); +$PAGE->set_pagetype('course-view-' . $course->format); -redirect($lasturl); +echo $OUTPUT->header(); +echo $OUTPUT->heading(get_string('reports')); +echo html_writer::div($OUTPUT->notification(get_string('noreports', 'debug'), 'error'), 'mt-3'); +echo $OUTPUT->footer();