Merge branch 'MDL-87283-main' of https://github.com/aanabit/moodle

This commit is contained in:
Mihail Geshoski
2026-01-20 10:43:01 +08:00
10 changed files with 457 additions and 27 deletions
@@ -0,0 +1,5 @@
issueNumber: MDL-87283
notes:
core:
- message: A new path_module parameter type for routing has been created
type: improved
@@ -0,0 +1,18 @@
issueNumber: MDL-87283
notes:
core:
- message: >-
Public require_login() function in moodlelib.php has been change to
redirect users to a restricted page when the activity restrictions are
visible.
type: improved
core_courseformat:
- message: >-
A new restricted page has been created using routing for users to access
the activity information. Only the activities with visible restrictions
will be available.
type: improved
- message: >-
Public course_section_cm_unavailable_error_message() function has been
changed to return the same message for all restricted activities.
type: improved
@@ -0,0 +1,94 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_course\route\controller;
use core\router\route;
use core\router\require_login;
use core_course\modinfo;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* Restricted modules rediretion.
*
* @package core_course
* @copyright 2026 Amaia Anabitarte <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restricted_module {
use \core\router\route_controller;
/**
* Restricted module.
*
* @param ResponseInterface $response
* @param modinfo $cm
* @return ResponseInterface
*/
#[route(
path: '/{course}/restricted/{cm}',
pathtypes: [
new \core\router\parameters\path_course(),
new \core\router\parameters\path_module(),
],
requirelogin: new require_login(
requirelogin: true,
courseattributename: 'course',
),
)]
public function restricted_module_page(
ServerRequestInterface $request,
ResponseInterface $response,
\stdClass $course,
\stdClass $cm,
): ResponseInterface {
global $OUTPUT, $PAGE;
$context = \context_module::instance($cm->id);
$modinfo = get_fast_modinfo($course);
$cminfo = $modinfo->get_cm($cm->id);
$sectioninfo = $modinfo->get_section_info_by_id($cm->section);
$format = course_get_format($course);
$course->format = $format->get_format();
$PAGE->set_url('/restricted.php', ['id' => $cm->id]);
$PAGE->add_body_class('limitedwidth');
$PAGE->set_context($context);
$PAGE->set_pagetype('mod-' . $cm->modname . '-restricted');
$strtitle = get_string('restrictedtitle', 'course', $cminfo->get_name());
$PAGE->set_title($strtitle . \moodle_page::TITLE_SEPARATOR . $course->shortname);
$PAGE->set_heading($course->fullname);
$PAGE->set_cm($cminfo);
$PAGE->set_secondary_navigation(false);
$restrictedclass = $format->get_output_classname('content\\cm\\restricted');
$cmoutput = new $restrictedclass(
format: $format,
section: $sectioninfo,
mod: $cminfo,
);
$renderer = $format->get_renderer($PAGE);
$response->getBody()->write($OUTPUT->header());
$response->getBody()->write($renderer->render($cmoutput));
$response->getBody()->write($OUTPUT->footer());
return $response;
}
}
@@ -0,0 +1,99 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains the default activity list from a section.
*
* @package core_courseformat
* @copyright 2020 Ferran Recio <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_courseformat\output\local\content\cm;
use cm_info;
use context_course;
use core\output\named_templatable;
use core_availability\info_module;
use core_courseformat\base as course_format;
use core_courseformat\output\local\content\cm;
use core_courseformat\output\local\courseformat_named_templatable;
use core_courseformat\output\section_renderer;
use renderable;
use renderer_base;
use section_info;
use stdClass;
/**
* Base class to render a restricted course module.
*
* @package core_courseformat
* @copyright 2026 Amaia Anabitarte <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restricted extends cm {
/** @var string the activity name output class name */
protected $restrictedclass;
/**
* Constructor.
*
* @param course_format $format the course format
* @param section_info $section the section info
* @param cm_info $mod the course module ionfo
* @param array $displayoptions optional extra display options
*/
public function __construct(course_format $format, section_info $section, cm_info $mod, array $displayoptions = []) {
// Get the necessary classes.
$this->restrictedclass = $format->get_output_classname('content\\cm\\restricted');
parent::__construct($format, $section, $mod, $displayoptions);
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return stdClass data context for a mustache template
*/
public function export_for_template(renderer_base $output): stdClass {
$mod = $this->mod;
$displayoptions = $this->displayoptions;
$data = (object)[
'grouping' => $mod->get_grouping_label($displayoptions['textclasses']),
'textclasses' => $displayoptions['textclasses'],
'classlist' => [],
'cmid' => $mod->id,
'hasurl' => false,
'hasname' => false,
];
// Add partial data segments.
$haspartials = [];
$haspartials['availability'] = $this->add_availability_data($data, $output);
$haspartials['alternative'] = $this->add_alternative_content_data($data, $output);
$haspartials['completion'] = $this->add_completion_data($data, $output);
$haspartials['dates'] = $this->add_dates_data($data, $output);
$haspartials['groupmode'] = $this->add_groupmode_data($data, $output);
$haspartials['visibility'] = $this->add_visibility_data($data, $output);
$this->add_format_data($data, $haspartials, $output);
return $data;
}
}
@@ -0,0 +1,67 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template core_courseformat/local/content/cm/restricted
Displays a restricted course module information.
Example context (json):
{
"cmname": {
"displayvalue" : "<a class=\"aalink\" href=\"#\"><span class=\"instancename\">Activity example</span></a>"
},
"hasname": "true",
"afterlink": "<span class=\"badge bg-primary\">30 unread messages</span>",
"hasextras": true,
"extras": ["<span class=\"badge bg-secondary\">[extras]</span>"],
"activityinfo": {
"hasmodavailability": true,
"activityname": "Activity example",
"hascompletion": true,
"uservisible": true,
"hasdates": true,
"isautomatic": true,
"istrackeduser": true,
"activitydates": [
{
"label": "Opens:",
"datestring": "6 April 2021, 6:46 PM"
}
],
"completiondetails": [
{
"statuscomplete": 1,
"description": "Viewed"
},
{
"statusincomplete": 1,
"description": "Receive a grade"
}
]
},
"modstealth": true,
"indent": 1
}
}}
<div class="course-section">
{{! Availability }}
{{#modavailability}}
{{$ core_courseformat/local/content/cm/availability }}
{{> core_courseformat/local/content/cm/availability }}
{{/ core_courseformat/local/content/cm/availability }}
{{/modavailability}}
</div>
+1 -11
View File
@@ -323,17 +323,7 @@ class core_course_renderer extends plugin_renderer_base {
if ($cm->uservisible) {
return null;
}
if (!$cm->availableinfo) {
return get_string('activityiscurrentlyhidden');
}
$altname = get_accesshide(' ' . $cm->modfullname);
$name = html_writer::empty_tag('img', ['src' => $cm->get_icon_url(),
'class' => 'activityicon', 'alt' => '']) .
html_writer::tag('span', ' '.$cm->get_formatted_name() . $altname, array('class' => 'instancename'));
$formattedinfo = \core_availability\info::format_info($cm->availableinfo, $cm->get_course());
return html_writer::div($name, 'activityinstance-error') .
html_writer::div($formattedinfo, 'availabilityinfo-error');
return get_string('activityiscurrentlyhidden');
}
/**
+1
View File
@@ -149,6 +149,7 @@ $string['recommendcheckbox'] = 'Recommend activity: {$a}';
$string['recommended_help'] = 'Activities recommended by your organisation.';
$string['relativedatessubmissionduedateafter'] = '{$a->datediffstr} after course start';
$string['relativedatessubmissionduedatebefore'] = '{$a->datediffstr} before course start';
$string['restrictedtitle'] = 'Not available: {$a}';
$string['searchactivitiesbyname'] = 'Search for activities by name';
$string['searchresults'] = 'Search results: {$a}';
$string['sectionlink'] = 'Permalink';
@@ -0,0 +1,77 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\router\parameters;
use core\exception\not_found_exception;
use core\param;
use core\router\schema\example;
use core\router\schema\parameters\mapped_property_parameter;
use core\router\schema\referenced_object;
use Psr\Http\Message\ServerRequestInterface;
/**
* A Moodle parameter referenced in the path.
*
* @package core
* @copyright 2026 Amaia Anabitarte <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class path_module extends \core\router\schema\parameters\path_parameter implements
mapped_property_parameter,
referenced_object
{
/**
* Create a new path_module parameter.
*
* @param string $name The name of the parameter to use for the module identifier
* @param mixed ...$extra Additional arguments
*/
public function __construct(
string $name = 'cm',
...$extra,
) {
$extra['name'] = $name;
$extra['type'] = param::RAW;
$extra['description'] = <<<EOF
The module identifier.
This can be the id of the module.
EOF;
$extra['examples'] = [
new example(
name: 'A module id',
value: 54,
),
];
parent::__construct(...$extra);
}
#[\Override]
public function add_attributes_for_parameter_value(
ServerRequestInterface $request,
string $value,
): ServerRequestInterface {
if (!$cm = get_coursemodule_from_id('', $value)) {
throw new not_found_exception('course_module', $value);
}
return $request
->withAttribute($this->name, $cm)
->withAttribute("{$this->name}context", \core\context\module::instance($cm->id));
}
}
+27 -16
View File
@@ -2645,7 +2645,7 @@ function require_login($courseorid = null, $autologinguest = true, $cm = null, $
}
// Check visibility of activity to current user; includes visible flag, conditional availability, etc.
if ($cm && !$cm->uservisible) {
if ($cm && !$cm->uservisible && !$cm->is_visible_on_course_page()) {
if ($preventredirect) {
throw new require_login_exception('Activity is hidden');
}
@@ -2656,24 +2656,35 @@ function require_login($courseorid = null, $autologinguest = true, $cm = null, $
redirect(course_get_url($course), $message, null, \core\output\notification::NOTIFY_ERROR);
}
// Set the global $COURSE.
if ($cm) {
$PAGE->set_cm($cm, $course);
$PAGE->set_pagelayout('incourse');
} else if (!empty($courseorid)) {
$PAGE->set_course($course);
}
if ($cm && !$cm->uservisible) {
if ($cm->is_visible_on_course_page()) {
$url = \core\router\util::get_path_for_callable(
[\core_course\route\controller\restricted_module::class, 'restricted_module_page'],
['course' => $course->id, 'cm' => $cm->id],
);
redirect($url, '', null);
}
} else {
// Set the global $COURSE.
if ($cm) {
$PAGE->set_cm($cm, $course);
$PAGE->set_pagelayout('incourse');
} else if (!empty($courseorid)) {
$PAGE->set_course($course);
}
foreach ($afterlogins as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginfunction($courseorid, $autologinguest, $cm, $setwantsurltome, $preventredirect);
foreach ($afterlogins as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginfunction($courseorid, $autologinguest, $cm, $setwantsurltome, $preventredirect);
}
}
// Finally access granted, update lastaccess times.
// Do not update access time for webservice or ajax requests.
if (!WS_SERVER && !AJAX_SCRIPT) {
user_accesstime_log($course->id);
}
}
// Finally access granted, update lastaccess times.
// Do not update access time for webservice or ajax requests.
if (!WS_SERVER && !AJAX_SCRIPT) {
user_accesstime_log($course->id);
}
}
@@ -0,0 +1,68 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\router\parameters;
use core\exception\not_found_exception;
use core\router\parameters\path_module;
use core\tests\router\route_testcase;
use GuzzleHttp\Psr7\ServerRequest;
use stdClass;
/**
* Tests for the Module Path paraemter.
*
* @package core
* @copyright Amaia Anabitarte <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core\router\parameters\path_module
*/
final class path_module_test extends route_testcase {
public function test_module_id(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$mod = $this->getDataGenerator()->create_module('page', ['course' => $course->id]);
$modcontext = \context_module::instance($mod->cmid);
$param = new path_module();
$request = new ServerRequest('GET', '/course/' . $course->id . '/restricted/' . $mod->cmid);
$newrequest = $param->add_attributes_for_parameter_value($request, $mod->cmid);
$this->assertInstanceOf(stdClass::class, $newrequest->getAttribute('cm'));
$this->assertInstanceOf(\core\context\module::class, $newrequest->getAttribute('cmcontext'));
$this->assertEquals($mod->cmid, $newrequest->getAttribute('cm')->id);
$this->assertEquals($modcontext->id, $newrequest->getAttribute('cmcontext')->id);
}
/**
* Tests for when a module was not found.
*/
public function test_module_not_found(): void {
$this->resetAfterTest();
$param = new path_module();
$course = $this->getDataGenerator()->create_course();
$modid = 9999;
$request = new ServerRequest('GET', '/course/' . $course->id . '/restricted/' . $modid);
$this->expectException(not_found_exception::class);
$param->add_attributes_for_parameter_value($request, $modid);
}
}