diff --git a/report/loglive/index.php b/report/loglive/index.php index ffe45342e80..c7ac0b4c74b 100644 --- a/report/loglive/index.php +++ b/report/loglive/index.php @@ -29,6 +29,7 @@ use core\report_helper; require('../../config.php'); require_once($CFG->libdir.'/adminlib.php'); require_once($CFG->dirroot.'/course/lib.php'); +global $SITE, $PAGE; $id = optional_param('id', 0, PARAM_INT); $page = optional_param('page', 0, PARAM_INT); @@ -60,23 +61,23 @@ $url = new moodle_url("/report/loglive/index.php", $params); $PAGE->set_url($url); $PAGE->set_pagelayout('report'); +$PAGE->set_context($context); +$strlivelogs = get_string('livelogs', 'report_loglive'); +$PAGE->set_title("$coursename: $strlivelogs"); +$output = $PAGE->get_renderer('report_loglive'); +echo $output->header(); +if (!report_helper::has_valid_group($context)) { + echo $output->notification(get_string('notingroup')); + echo $output->footer(); + exit(); +} $renderable = new report_loglive_renderable($logreader, $id, $url, 0, $page); $refresh = $renderable->get_refresh_rate(); $logreader = $renderable->selectedlogreader; - -$strlivelogs = get_string('livelogs', 'report_loglive'); $strupdatesevery = get_string('updatesevery', 'moodle', $refresh); - - -$PAGE->set_url($url); -$PAGE->set_context($context); -$PAGE->set_title("$coursename: $strlivelogs"); $PAGE->set_heading($coursename); -$output = $PAGE->get_renderer('report_loglive'); -echo $output->header(); - // Print selector dropdown. $pluginname = get_string('pluginname', 'report_loglive'); report_helper::print_report_selector($pluginname); diff --git a/report/loglive/lib.php b/report/loglive/lib.php index c74f4b39dfe..4e21e5a69bb 100644 --- a/report/loglive/lib.php +++ b/report/loglive/lib.php @@ -24,6 +24,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use core\report_helper; + defined('MOODLE_INTERNAL') || die; /** @@ -36,7 +38,10 @@ defined('MOODLE_INTERNAL') || die; * @param context $context The context of the course */ function report_loglive_extend_navigation_course($navigation, $course, $context) { - if (has_capability('report/loglive:view', $context)) { + if ( + has_capability('report/log:view', $context) + && report_helper::has_valid_group($context) + ) { $url = new moodle_url('/report/loglive/index.php', array('id' => $course->id)); $navigation->add(get_string('pluginname', 'report_loglive'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', '')); diff --git a/report/loglive/tests/behat/behat_report_loglive.php b/report/loglive/tests/behat/behat_report_loglive.php new file mode 100644 index 00000000000..b41a5fefaa7 --- /dev/null +++ b/report/loglive/tests/behat/behat_report_loglive.php @@ -0,0 +1,50 @@ +. + +require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); +/** + * Step definition for report_loglive behat tests. + * + * @package report_loglive + * @category test + * @copyright 2025 Laurent David + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_report_loglive extends behat_base { + /** + * Convert page names to URLs for steps like 'When I am on the "[identifier]" "[page type]" page'. + * + * Recognised page names are: + * | pagetype | name meaning | description | + * | Logs | Course name | The course report loglive page | + * + * @param string $page identifies which type of page this is, e.g. 'Logs'. + * @param string $identifier identifies the particular page, e.g. 'C1'. + * @return moodle_url the corresponding URL. + * @throws Exception with a meaningful error message if the specified page cannot be found. + */ + protected function resolve_page_instance_url(string $page, string $identifier): moodle_url { + switch (strtolower($page)) { + case 'logs': + $courseid = $this->get_course_id($identifier); + return new moodle_url('/report/loglive/index.php', [ + 'id' => $courseid, + ]); + default: + throw new Exception("Unrecognised page type '{$page}'"); + } + } +} diff --git a/report/loglive/tests/behat/group_report_loglive.feature b/report/loglive/tests/behat/group_report_loglive.feature new file mode 100644 index 00000000000..ca636963222 --- /dev/null +++ b/report/loglive/tests/behat/group_report_loglive.feature @@ -0,0 +1,95 @@ +@report @report_loglive +Feature: In a course with group mode, I can view the group report loglive page or not + depending on the group I am in. + + Background: + Given the following "courses" exist: + | fullname | shortname | category | groupmode | + | Course separate group | C1 | 0 | 1 | + | Course visible group | C2 | 0 | 2 | + | Course no group | C3 | 0 | 0 | + And the following "users" exist: + | username | firstname | lastname | email | + | student1 | Student | 1 | student1@example.com | + | student2 | Student | 2 | student2@example.com | + | teacher1 | Teacher | 1 | teacher1@example.com | + | teacher2 | Teacher | 2 | teacher2@example.com | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | teacher | + | teacher2 | C1 | teacher | + | student1 | C1 | student | + | student2 | C1 | student | + | teacher1 | C2 | teacher | + | teacher2 | C2 | teacher | + | student1 | C2 | student | + | student2 | C2 | student | + | teacher1 | C3 | teacher | + | teacher2 | C3 | teacher | + | student1 | C3 | student | + | student2 | C3 | student | + And the following "groups" exist: + | course | name | idnumber | + | C1 | Group C1.1 | group11 | + | C2 | Group C2.1 | group21 | + | C3 | Group C3.1 | group31 | + And the following "group members" exist: + | group | user | + | group11 | student1 | + | group11 | teacher1 | + | group21 | student1 | + | group21 | teacher1 | + | group31 | student1 | + | group31 | teacher1 | + And the following "activities" exist: + | activity | name | intro | course | idnumber | + | page | Page11 | Page11 | C1 | page1 | + | page | Page21 | Page21 | C2 | page1 | + | page | Page31 | Page31 | C3 | page1 | + # Generate logs for the pages. + And I am on the "Page11" "page activity" page logged in as student1 + And I am on "Course separate group" course homepage + And I log out + And I am on the "Page11" "page activity" page logged in as student2 + And I am on "Course separate group" course homepage + And I log out + And I am on the "Page21" "page activity" page logged in as student1 + And I am on "Course visible group" course homepage + And I log out + And I am on the "Page21" "page activity" page logged in as student2 + And I am on "Course visible group" course homepage + And I log out + And I am on the "Page31" "page activity" page logged in as student1 + And I am on "Course no group" course homepage + And I log out + And I am on the "Page31" "page activity" page logged in as student2 + And I am on "Course no group" course homepage + And I log out + + Scenario Outline: As a user in a course, I can view a link to the report live logs if I am in the right group. + Given I log in as "" + And I am on "" course homepage + When I navigate to "Reports" in current page administration + And "Live logs" "link" + And I log out + Examples: + | course | user | shouldexist | + | Course separate group | teacher1 | should exist | + | Course separate group | teacher2 | should not exist | + | Course visible group | teacher1 | should exist | + | Course visible group | teacher2 | should exist | + | Course no group | teacher1 | should exist | + | Course no group | teacher2 | should exist | + + Scenario Outline: As a non editing teacher not in a group, I can not view the live report logs. + Given I log in as "" + When I am on the "" "report_loglive > Logs" page + Then I "you need to be part of a group to see this page." + Examples: + | course | user | shouldsee | + | Course separate group | teacher1 | should not see | + | Course separate group | teacher2 | should see | + | Course visible group | teacher1 | should not see | + | Course visible group | teacher2 | should not see | + | Course no group | teacher1 | should not see | + | Course no group | teacher2 | should not see |