MDL-76430 core_courseformat: implement fragment to course format

This commit is contained in:
Ferran Recio 2022-12-14 10:56:44 +01:00
parent df502b3e4c
commit d770ebd01b
6 changed files with 77 additions and 13 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -24,11 +24,12 @@
import {BaseComponent} from 'core/reactive';
import {getCurrentCourseEditor} from 'core_courseformat/courseeditor';
import Config from 'core/config';
import inplaceeditable from 'core/inplace_editable';
import Section from 'core_courseformat/local/content/section';
import CmItem from 'core_courseformat/local/content/section/cmitem';
// Course actions is needed for actions that are not migrated to components.
import courseActions from 'core_course/actions';
import Fragment from 'core/fragment';
import Templates from 'core/templates';
import DispatchActions from 'core_courseformat/local/content/actions';
import * as CourseEvents from 'core_course/events';
// The jQuery module is only used for interacting with Boostrap 4. It can we removed when MDL-71979 is integrated.
@ -219,6 +220,7 @@ export default class Component extends BaseComponent {
// State changes that require to reload some course modules.
{watch: `cm.visible:updated`, handler: this._reloadCm},
{watch: `cm.stealth:updated`, handler: this._reloadCm},
{watch: `cm.sectionid:updated`, handler: this._reloadCm},
// Update section number and title.
{watch: `section.number:updated`, handler: this._refreshSectionNumber},
// Collapse and expand sections.
@ -229,9 +231,6 @@ export default class Component extends BaseComponent {
{watch: `section.cmlist:updated`, handler: this._refreshSectionCmlist},
// Reindex sections and cms.
{watch: `state:updated`, handler: this._indexContents},
// State changes thaty require to reload course modules.
{watch: `cm.visible:updated`, handler: this._reloadCm},
{watch: `cm.sectionid:updated`, handler: this._reloadCm},
];
}
@ -506,8 +505,18 @@ export default class Component extends BaseComponent {
const pendingReload = new Pending(`courseformat/content:reloadCm_${element.id}`);
const cmitem = this.getElement(this.selectors.CM, element.id);
if (cmitem) {
const promise = courseActions.refreshModule(cmitem, element.id);
promise.then(() => {
const promise = Fragment.loadFragment(
'core_courseformat',
'cmitem',
Config.courseContextId,
{
id: element.id,
courseid: Config.courseId,
sr: this.reactive.sectionReturn ?? 0,
}
);
promise.then((html, js) => {
Templates.replaceNode(cmitem, html, js);
this._indexContents();
pendingReload.resolve();
return;
@ -528,8 +537,18 @@ export default class Component extends BaseComponent {
const pendingReload = new Pending(`courseformat/content:reloadSection_${element.id}`);
const sectionitem = this.getElement(this.selectors.SECTION, element.id);
if (sectionitem) {
const promise = courseActions.refreshSection(sectionitem, element.id);
promise.then(() => {
const promise = Fragment.loadFragment(
'core_courseformat',
'section',
Config.courseContextId,
{
id: element.id,
courseid: Config.courseId,
sr: this.reactive.sectionReturn ?? 0,
}
);
promise.then((html, js) => {
Templates.replaceNode(sectionitem, html, js);
this._indexContents();
pendingReload.resolve();
return;

View File

@ -199,3 +199,44 @@ function expand_value(array &$dest, array $source, array $option, string $option
$dest[$optionname] = clean_param($source[$optionname], $option['type'] ?? PARAM_RAW);
}
}
/**
* Course-module fragment renderer method.
*
* The fragment arguments are id and sr (section return).
*
* @param array $args The fragment arguments.
* @return string The rendered cm item.
*/
function core_courseformat_output_fragment_cmitem($args): string {
global $PAGE;
list($course, $cm) = get_course_and_cm_from_cmid($args['id']);
$format = course_get_format($course);
if (!empty($args['sr'])) {
$format->set_section_number($args['sr']);
}
$renderer = $format->get_renderer($PAGE);
$section = $cm->get_section_info();
return $renderer->course_section_updated_cm_item($format, $section, $cm);
}
/**
* Section fragment renderer method.
*
* The fragment arguments are courseid, section id and sr (section return).
*
* @param array $args The fragment arguments.
* @return string The rendered section.
*/
function core_courseformat_output_fragment_section($args): string {
global $PAGE;
$course = get_course($args['courseid']);
$format = course_get_format($course);
if (!empty($args['sr'])) {
$format->set_section_number($args['sr']);
}
$renderer = $format->get_renderer($PAGE);
$modinfo = $format->get_modinfo();
$section = $modinfo->get_section_info_by_id($args['id'], MUST_EXIST);
return $renderer->course_section_updated($format, $section);
}

View File

@ -6,6 +6,10 @@ Overview of this plugin type at http://docs.moodle.org/dev/Course_formats
* New core_courseformat\base::get_context() to get the course context directly from the format instance.
* New core_courseformat\base::delete_module() method. Now format plugins can extend the activity deletion logic
by overriding this method.
* The reactive course editor now uses the standard fragment library to refresh an individual section or activity card
in the course content. Instead of using adhoc YUI methods and webservice, the new fragment methods are:
- core_courseformat_output_fragment_cmitem
- core_courseformat_output_fragment_section
=== 4.1 ===
* New \core_courseformat\stateupdates methods add_section_remove() and add_cm_remove() have been added to replace

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2023012000.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2023012000.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.2dev (Build: 20230120)'; // Human-friendly version name