MDL-82349 courseformat: add reactivity to frontpage topic

This commit is contained in:
ferranrecio 2024-10-09 09:05:44 +02:00 committed by ferran
parent c6a9177252
commit c5133ddc47
5 changed files with 42 additions and 10 deletions

View File

@ -89,6 +89,7 @@ class frontpagesection implements named_templatable, renderable {
}
$data = (object)[
'editing' => $format->show_editor(),
'sections' => [$sectionoutput->export_for_template($output)],
];

View File

@ -51,7 +51,7 @@ class format_site extends course_format {
* Returns the display name of the given section that the course prefers.
*
* @param int|stdClass $section Section object from database or just field section.section
* @return Display name that the course format prefers, e.g. "Topic 2"
* @return string Display name that the course format prefers, e.g. "Topic 2"
*/
function get_section_name($section) {
$section = $this->get_section($section);
@ -88,6 +88,26 @@ class format_site extends course_format {
return blocks_get_default_site_course_blocks();
}
#[\Override]
public function supports_ajax() {
// All home page is rendered in the backend, we only need an ajax editor components in edit mode.
// This will also prevent redirectng to the login page when a guest tries to access the site,
// and will make the home page loading faster.
$ajaxsupport = new stdClass();
$ajaxsupport->capable = $this->show_editor();
return $ajaxsupport;
}
#[\Override]
public function supports_components() {
return true;
}
#[\Override]
public function uses_sections() {
return true;
}
/**
* Definitions of the additional options that site uses
*

View File

@ -21,6 +21,7 @@
Example context (json):
{
"editing": true,
"sections": [
"<li>This is the section content</li>"
],
@ -28,7 +29,7 @@
"settingsurl": "#"
}
}}
<div class="course-content">
<div class="course-content" id="courseformat-frontpage-main-topic">
{{#showsettings}}
<div class="mb-2">
<a href="{{{settingsurl}}}" title="{{#str}} edit {{/str}}" aria-label="{{#str}} edit {{/str}}">
@ -37,10 +38,18 @@
</div>
{{/showsettings}}
<div class="sitetopic">
<ul class="topics frontpage">
<ul class="topics frontpage" data-for="course_sectionlist">
{{#sections}}
{{> core_courseformat/local/content/section }}
{{/sections}}
</ul>
</div>
</div>
{{#js}}
{{! The home page should be as fast as possible, we only load the editor when needed.}}
{{#editing}}
require(['core_courseformat/local/content'], function(component) {
component.init('courseformat-frontpage-main-topic', {});
});
{{/editing}}
{{/js}}

View File

@ -2867,7 +2867,7 @@ function include_course_editor(course_format $format) {
$course = $format->get_course();
if ($SITE->id === $course->id) {
if (!$format->supports_ajax()?->capable) {
return;
}

View File

@ -109,6 +109,14 @@ $PAGE->set_title(get_string('home'));
$PAGE->set_heading($SITE->fullname);
$PAGE->set_secondary_active_tab('coursehome');
$siteformatoptions = course_get_format($SITE)->get_format_options();
$modinfo = get_fast_modinfo($SITE);
$modnamesused = $modinfo->get_used_module_names();
// The home page can have acitvities in the block aside. We should
// initialize the course editor before the page structure is rendered.
include_course_ajax($SITE, $modnamesused);
$courserenderer = $PAGE->get_renderer('core', 'course');
if ($hassiteconfig) {
@ -119,10 +127,6 @@ if ($hassiteconfig) {
echo $OUTPUT->header();
$siteformatoptions = course_get_format($SITE)->get_format_options();
$modinfo = get_fast_modinfo($SITE);
$modnamesused = $modinfo->get_used_module_names();
// Print Section or custom info.
if (!empty($CFG->customfrontpageinclude)) {
// Pre-fill some variables that custom front page might use.
@ -135,8 +139,6 @@ if (!empty($CFG->customfrontpageinclude)) {
} else if ($siteformatoptions['numsections'] > 0) {
echo $courserenderer->frontpage_section1();
}
// Include course AJAX.
include_course_ajax($SITE, $modnamesused);
echo $courserenderer->frontpage();