From cc117b897820b1117887a286426ef327d8924940 Mon Sep 17 00:00:00 2001 From: Laurent David Date: Tue, 29 Oct 2024 11:34:42 +0100 Subject: [PATCH] MDL-81790 report_log: Log report page per activities * When clicking on the More / Logs menu in an activity we should stay on the activity page and not go on the course report log --- report/log/classes/renderable.php | 6 +- report/log/classes/renderer.php | 67 +++++++++++-------- report/log/index.php | 19 +++++- report/log/lib.php | 2 +- .../tests/behat/activity_report_log.feature | 42 ++++++++++++ 5 files changed, 104 insertions(+), 32 deletions(-) create mode 100644 report/log/tests/behat/activity_report_log.feature diff --git a/report/log/classes/renderable.php b/report/log/classes/renderable.php index 738b5e94b4e46..20fa7b7dbc3e5 100644 --- a/report/log/classes/renderable.php +++ b/report/log/classes/renderable.php @@ -103,6 +103,9 @@ class report_log_renderable implements renderable { */ public $grouplist; + /** @var int current coursemodule Id */ + public $currentcmid; + /** * Constructor. * @@ -126,7 +129,7 @@ class report_log_renderable implements renderable { */ public function __construct($logreader = "", $course = 0, $userid = 0, $modid = 0, $action = "", $groupid = 0, $edulevel = -1, $showcourses = false, $showusers = false, $showreport = true, $showselectorform = true, $url = "", $date = 0, - $logformat='showashtml', $page = 0, $perpage = 100, $order = "timecreated ASC", $origin ='') { + $logformat='showashtml', $page = 0, $perpage = 100, $order = "timecreated ASC", $origin ='', int $currentcmid = 0) { global $PAGE; @@ -171,6 +174,7 @@ public function __construct($logreader = "", $course = 0, $userid = 0, $modid = $this->showselectorform = $showselectorform; $this->logformat = $logformat; $this->origin = $origin; + $this->currentcmid = $currentcmid; } /** diff --git a/report/log/classes/renderer.php b/report/log/classes/renderer.php index b8ff1aea17345..f006bcf8aed43 100644 --- a/report/log/classes/renderer.php +++ b/report/log/classes/renderer.php @@ -95,28 +95,38 @@ public function report_selector_form(report_log_renderable $reportlog) { $selectedcourseid = empty($reportlog->course) ? 0 : $reportlog->course->id; - // Add course selector. - $sitecontext = context_system::instance(); - $courses = $reportlog->get_course_list(); - if (!empty($courses) && $reportlog->showcourses) { - echo html_writer::label(get_string('selectacourse'), 'menuid', false, array('class' => 'accesshide')); - echo html_writer::select($courses, "id", $selectedcourseid, null, ['class' => 'me-2 mb-2']); - } else { - $courses = array(); - $courses[$selectedcourseid] = get_course_display_name_for_list($reportlog->course) . (($selectedcourseid == SITEID) ? - ' (' . get_string('site') . ') ' : ''); - echo html_writer::label(get_string('selectacourse'), 'menuid', false, array('class' => 'accesshide')); - echo html_writer::select($courses, "id", $selectedcourseid, false, ['class' => 'me-2 mb-2']); - // Check if user is admin and this came because of limitation on number of courses to show in dropdown. - if (has_capability('report/log:view', $sitecontext)) { - $a = new stdClass(); - $a->url = new moodle_url('/report/log/index.php', array('chooselog' => 0, - 'group' => $reportlog->get_selected_group(), 'user' => $reportlog->userid, - 'id' => $selectedcourseid, 'date' => $reportlog->date, 'modid' => $reportlog->modid, - 'showcourses' => 1, 'showusers' => $reportlog->showusers)); - $a->url = $a->url->out(false); - print_string('logtoomanycourses', 'moodle', $a); + // Add course selector if current cmid not set. + if (empty($reportlog->currentcmid)) { + $sitecontext = context_system::instance(); + $courses = $reportlog->get_course_list(); + if (!empty($courses) && $reportlog->showcourses) { + echo html_writer::label(get_string('selectacourse'), 'menuid', false, ['class' => 'accesshide']); + echo html_writer::select($courses, "id", $selectedcourseid, null, ['class' => 'me-2 mb-2']); + } else { + $courses = []; + $courses[$selectedcourseid] = + get_course_display_name_for_list($reportlog->course) . (($selectedcourseid == SITEID) ? + ' (' . get_string('site') . ') ' : ''); + echo html_writer::label(get_string('selectacourse'), 'menuid', false, ['class' => 'accesshide']); + echo html_writer::select($courses, "id", $selectedcourseid, false, ['class' => 'me-2 mb-2']); + // Check if user is admin and this came because of limitation on number of courses to show in dropdown. + if (has_capability('report/log:view', $sitecontext)) { + $a = new stdClass(); + $a->url = new moodle_url('/report/log/index.php', [ + 'chooselog' => 0, + 'group' => $reportlog->get_selected_group(), + 'user' => $reportlog->userid, + 'id' => $selectedcourseid, + 'date' => $reportlog->date, + 'modid' => $reportlog->modid, + 'showcourses' => 1, 'showusers' => $reportlog->showusers, + ]); + $a->url = $a->url->out(false); + print_string('logtoomanycourses', 'moodle', $a); + } } + } else { + echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'id', 'value' => $selectedcourseid]); } // Add group selector. @@ -160,12 +170,15 @@ public function report_selector_form(report_log_renderable $reportlog) { echo html_writer::select($dates, "date", $reportlog->date, get_string("alldays"), ['class' => 'me-2 mb-2']); - // Add activity selector. - [$activities, $disabled] = $reportlog->get_activities_list(); - echo html_writer::label(get_string('activities'), 'menumodid', false, array('class' => 'accesshide')); - echo html_writer::select($activities, "modid", $reportlog->modid, get_string("allactivities"), - ['class' => 'me-2 mb-2'], $disabled); - + // Add activity selector if cmid is not set. + if (empty($reportlog->currentcmid)) { + [$activities, $disabled] = $reportlog->get_activities_list(); + echo html_writer::label(get_string('activities'), 'menumodid', false, ['class' => 'accesshide']); + echo html_writer::select($activities, "modid", $reportlog->modid, get_string("allactivities"), + ['class' => 'me-2 mb-2'], $disabled); + } else { + echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'cmid', 'value' => $reportlog->currentcmid]); + } // Add actions selector. echo html_writer::label(get_string('actions'), 'menumodaction', false, array('class' => 'accesshide')); echo html_writer::select($reportlog->get_actions(), 'modaction', $reportlog->action, diff --git a/report/log/index.php b/report/log/index.php index 41519990296c3..e0bcf62290d3b 100644 --- a/report/log/index.php +++ b/report/log/index.php @@ -35,6 +35,7 @@ $user = optional_param('user', 0, PARAM_INT); // User to display. $date = optional_param('date', 0, PARAM_INT); // Date to display. $modid = optional_param('modid', 0, PARAM_ALPHANUMEXT); // Module id or 'site_errors'. +$cmid = optional_param('cmid', 0, PARAM_INT); // Course Module ID. $modaction = optional_param('modaction', '', PARAM_ALPHAEXT); // An action as recorded in the logs. $page = optional_param('page', '0', PARAM_INT); // Which page to show. $perpage = optional_param('perpage', '100', PARAM_INT); // How many per page. @@ -104,6 +105,14 @@ $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST); require_login($course); $context = context_course::instance($course->id); + if ($cmid) { + $modinfo = get_fast_modinfo($course); + if (!isset($modinfo->cms[$cmid])) { + throw new moodle_exception('invalidmoduleid', '', '', $modid); + } + $context = context_module::instance($cmid); + $PAGE->set_cm($modinfo->cms[$cmid]); + } } else { $course = $SITE; require_login(); @@ -146,7 +155,7 @@ } $reportlog = new report_log_renderable($logreader, $course, $user, $modid, $modaction, $group, $edulevel, $showcourses, $showusers, - $chooselog, true, $url, $date, $logformat, $page, $perpage, 'timecreated DESC', $origin); + $chooselog, true, $url, $date, $logformat, $page, $perpage, 'timecreated DESC', $origin, $cmid); $readers = $reportlog->get_readers(); $output = $PAGE->get_renderer('report_log'); @@ -162,7 +171,9 @@ echo $output->header(); // Print selector dropdown. $pluginname = get_string('pluginname', 'report_log'); - report_helper::print_report_selector($pluginname); + if (empty($cmid)) { + report_helper::print_report_selector($pluginname); + } $userinfo = get_string('allparticipants'); $dateinfo = get_string('alldays'); @@ -186,7 +197,9 @@ echo $output->header(); // Print selector dropdown. $pluginname = get_string('pluginname', 'report_log'); - report_helper::print_report_selector($pluginname); + if (empty($cmid)) { + report_helper::print_report_selector($pluginname); + } echo $output->heading(get_string('chooselogs') .':', 3); echo $output->render($reportlog); } diff --git a/report/log/lib.php b/report/log/lib.php index 1c6dcf6966b56..316efda63126b 100644 --- a/report/log/lib.php +++ b/report/log/lib.php @@ -125,7 +125,7 @@ function report_log_can_access_user_report($user, $course) { */ function report_log_extend_navigation_module($navigation, $cm) { if (has_capability('report/log:view', context_course::instance($cm->course))) { - $url = new moodle_url('/report/log/index.php', array('chooselog'=>'1','id'=>$cm->course,'modid'=>$cm->id)); + $url = new moodle_url('/report/log/index.php', ['chooselog' => '1', 'id' => $cm->course, 'cmid' => $cm->id]); $navigation->add(get_string('logs'), $url, navigation_node::TYPE_SETTING, null, 'logreport', new pix_icon('i/report', '')); } } diff --git a/report/log/tests/behat/activity_report_log.feature b/report/log/tests/behat/activity_report_log.feature new file mode 100644 index 0000000000000..e9559cc6fd7d4 --- /dev/null +++ b/report/log/tests/behat/activity_report_log.feature @@ -0,0 +1,42 @@ +@report @report_log +Feature: In a activity page, navigate through the More / Logs menu, test for report log page + In order to navigate through report page + As an admin + Go to the activity page, click on More / Logs menu, and check for the report log page + + Background: + Given the following "courses" exist: + | fullname | shortname | category | groupmode | + | Course 1 | C1 | 0 | 1 | + And the following "activities" exist: + | activity | name | course | section | + | page | Test page 1 | C1 | 1 | + | page | Test page 2 | C1 | 1 | + And the following "users" exist: + | username | firstname | lastname | email | + | student1 | Student | 1 | student1@example.com | + And the following "course enrolments" exist: + | user | course | role | + | admin | C1 | editingteacher | + | student1 | C1 | student | + + @javascript + Scenario: Report selectors should be targeted toward course module + Given I am on the "Test page 1" Activity page logged in as "admin" + When I navigate to "Logs" in current page administration + And I click on "Logs" "link" + Then "menuid" "select" should not exist + And "modid" "select" should not exist + And I should see "All participants" in the "user" "select" + And I should see "All days" in the "date" "select" + And I should see "All sources" in the "origin" "select" + And I should see "All events" in the "edulevel" "select" + And "Page" "link" should exist in current page administration + + @javascript + Scenario: Report submission stays in the same course module page + Given I am on the "Test page 1" Activity page logged in as "admin" + When I navigate to "Logs" in current page administration + And I click on "Logs" "link" + And I click on "Get these logs" "button" + Then "Page" "link" should exist in current page administration