MDL-84143 enrol: Use mustache templates on enrolment options page

This commit is contained in:
Ilya Tregubov
2025-03-12 10:38:51 +00:00
committed by Marina Glancy
parent 2f82f5fe8c
commit 647db3f6e4
4 changed files with 185 additions and 31 deletions
+38
View File
@@ -93,6 +93,44 @@ class core_course_renderer extends plugin_renderer_base {
return $content;
}
/**
* Renders course info box.
*
* @param stdClass $course course object
* @param string[] $widgets array of enrolment widgets
* @param \core\url|null $returnurl return url
* @return string
*/
public function enrolment_options(stdClass $course, array $widgets, ?\core\url $returnurl = null): string {
if (!$widgets) {
if (isguestuser()) {
$message = get_string('noguestaccess', 'enrol');
$continuebutton = $this->output->continue_button(get_login_url());
} else if ($returnurl) {
$message = get_string('notenrollable', 'enrol');
$continuebutton = $this->output->continue_button($returnurl);
} else {
$url = get_local_referer(false);
if (empty($url)) {
$url = new moodle_url('/index.php');
}
$message = get_string('notenrollable', 'enrol');
$continuebutton = $this->output->continue_button($url);
}
}
$courseinfobox = $this->course_info_box($course);
$templatecontext = [
'heading' => get_string('enrolmentoptions', 'enrol'),
'courseinfobox' => $courseinfobox,
'widgets' => array_values($widgets),
'message' => $message ?? '',
'continuebutton' => $continuebutton ?? '',
];
return $this->render_from_template('core_enrol/enrolment_options', $templatecontext);
}
/**
* Renderers a structured array of courses and categories into a nice XHTML tree structure.
*
+92
View File
@@ -0,0 +1,92 @@
<?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;
use context_course;
use moodle_url;
/**
* Tests for the \core_course_renderer class.
*
* @package core_course
* @copyright 2025 Ilya Tregubov <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core_course_renderer
*/
final class course_renderer_test extends \advanced_testcase {
/**
* Data provider for {@see self::test_enrolment_options}.
*
* @return array
*/
public static function enrolment_options_provider(): array {
return [
'with forms' => [
'forms' => ['<form>Form 1</form>', '<form>Form 2</form>'],
'isguestuser' => false,
'expected' => ['<form>Form 1</form>', '<form>Form 2</form>'],
'notexpected' => [get_string('noguestaccess', 'enrol'), get_string('notenrollable', 'enrol')],
],
'no forms guest user' => [
'forms' => [],
'isguestuser' => true,
'expected' => [get_string('noguestaccess', 'enrol')],
'notexpected' => [get_string('notenrollable', 'enrol')],
],
'no forms not guest user' => [
'forms' => [],
'isguestuser' => false,
'expected' => [get_string('notenrollable', 'enrol')],
'notexpected' => [get_string('noguestaccess', 'enrol')],
],
];
}
/**
* Test render_enrolment_options with data provider.
*
* @param array $forms
* @param bool $isguestuser
* @param array $expected
* @param array $notexpected
* @dataProvider enrolment_options_provider
*/
public function test_enrolment_options(array $forms, bool $isguestuser, array $expected, array $notexpected): void {
global $PAGE;
$this->resetAfterTest();
$isguestuser ? $this->setGuestUser() : $this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);
$PAGE->set_context($context);
$PAGE->set_course($course);
/** @var \core_course_renderer $renderer */
$renderer = $PAGE->get_renderer('core', 'course');
$returnurl = new moodle_url('/course/view.php', ['id' => $course->id]);
$output = $renderer->enrolment_options($course, $forms, $returnurl);
foreach ($expected as $expectedstring) {
$this->assertStringContainsString($expectedstring, $output);
}
foreach ($notexpected as $notexpectedstring) {
$this->assertStringNotContainsString($notexpectedstring, $output);
}
}
}
+11 -31
View File
@@ -26,7 +26,7 @@ require('../config.php');
require_once("$CFG->libdir/formslib.php");
$id = required_param('id', PARAM_INT);
$returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
if (!isloggedin()) {
$referer = get_local_referer();
@@ -69,17 +69,17 @@ if (!core_course_category::can_view_course_info($course) && !is_enrolled($contex
throw new \moodle_exception('coursehidden', '', $CFG->wwwroot . '/');
}
// get all enrol forms available in this course
// Get all enrol widgets available in this course.
$enrols = enrol_get_plugins(true);
$enrolinstances = enrol_get_instances($course->id, true);
$forms = array();
$widgets = [];
foreach($enrolinstances as $instance) {
if (!isset($enrols[$instance->enrol])) {
continue;
}
$form = $enrols[$instance->enrol]->enrol_page_hook($instance);
if ($form) {
$forms[$instance->id] = $form;
$widget = $enrols[$instance->enrol]->enrol_page_hook($instance);
if ($widget) {
$widgets[$instance->id] = $widget;
}
}
@@ -98,30 +98,10 @@ $PAGE->set_title($course->shortname);
$PAGE->set_heading($course->fullname);
$PAGE->navbar->add(get_string('enrolmentoptions','enrol'));
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('enrolmentoptions','enrol'));
/** @var core_course_renderer $courserenderer */
$courserenderer = $PAGE->get_renderer('core', 'course');
echo $courserenderer->course_info_box($course);
//TODO: find if future enrolments present and display some info
foreach ($forms as $form) {
echo $form;
}
if (!$forms) {
if (isguestuser()) {
notice(get_string('noguestaccess', 'enrol'), get_login_url());
} else if ($returnurl) {
notice(get_string('notenrollable', 'enrol'), $returnurl);
} else {
$url = get_local_referer(false);
if (empty($url)) {
$url = new moodle_url('/index.php');
}
notice(get_string('notenrollable', 'enrol'), $url);
}
}
$content = $courserenderer->enrolment_options($course, $widgets,
$returnurl ? new \core\url($returnurl) : null);
echo $OUTPUT->header();
echo $content;
echo $OUTPUT->footer();
@@ -0,0 +1,44 @@
{{!
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 enrol/enrolment_options
This template will render information about available course enrolment options.
Context variables required for this template:
* courseinfobox - Course information box
* widgets - array of widgets to display
* message - Error message and continue button
Example context (json):
{
"courseinfobox": "<div>Course Information</div>",
"widgets": [],
"message": "Guests cannot access this course. Please log in.",
"continuebutton": "<a href=\"#\"><span class=\"instancename\">Continue</span></a>"
}
}}
<h2>{{heading}}</h2>
{{{courseinfobox}}}
{{#widgets}}{{{.}}}{{/widgets}}
{{^widgets}}
<div id="notice" class="box py-3 generalbox">
{{message}}
</div>
{{{continuebutton}}}
{{/widgets}}