Skip to content

Commit e1ce614

Browse files
committed
CTP-4480 format_ucl: Fixed section selector and mod chooser bugs
1 parent ac052c2 commit e1ce614

9 files changed

Lines changed: 167 additions & 22 deletions

File tree

amd/build/section.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/section.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/src/section.js

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Section extends BaseComponent {
4040
this.selectors = {
4141
SECTION: `[data-for='section']`,
4242
SECTION_NAVIGATION: `[data-for='section-nav']`,
43+
SECTION_DIVIDER: `[data-for='section-divider']`,
4344
SETMARKER: `[data-action="sectionHighlight"]`,
4445
REMOVEMARKER: `[data-action="sectionUnhighlight"]`,
4546
ACTIONTEXT: `.menu-action-text`,
@@ -64,7 +65,8 @@ class Section extends BaseComponent {
6465
getWatchers() {
6566
return [
6667
{watch: `section.current:updated`, handler: this._refreshHighlight},
67-
{watch: `section.number:updated`, handler: this._reloadSectionNavigation},
68+
{watch: `section.number:updated`, handler: this._reloadSectionDividerAndNavigation},
69+
{watch: `section.number:updated`, handler: this._refreshSectionNumber},
6870
];
6971
}
7072

@@ -110,10 +112,7 @@ class Section extends BaseComponent {
110112
}
111113

112114
/**
113-
* Reload a course section contents.
114-
*
115-
* Section HTML is still strongly backend dependant.
116-
* Some changes require to get a new version of the section.
115+
* Reload a course section navigation.
117116
*
118117
* @param {details} param0 the watcher details
119118
* @param {object} param0.element the state object
@@ -142,6 +141,69 @@ class Section extends BaseComponent {
142141
});
143142
}
144143
}
144+
145+
/**
146+
* Reload a course section divider.
147+
*
148+
* @param {details} param0 the watcher details
149+
* @param {object} param0.element the state object
150+
*/
151+
_reloadSectionDivider({element}) {
152+
const pendingReload = new Pending(`format_ucl/section:reloadDivider_${element.id}`);
153+
const sectiondivider = this.getElement(this.selectors.SECTION_DIVIDER, element.id);
154+
155+
if (sectiondivider) {
156+
const promise = Fragment.loadFragment(
157+
'format_ucl',
158+
'section_divider',
159+
Config.courseContextId,
160+
{
161+
id: element.id,
162+
courseid: Config.courseId,
163+
}
164+
);
165+
promise.then((html, js) => {
166+
Templates.replaceNode(sectiondivider, html, js);
167+
pendingReload.resolve();
168+
}).catch(() => {
169+
pendingReload.resolve();
170+
});
171+
}
172+
}
173+
174+
/**
175+
* Reload a course section divider and navigation.
176+
*
177+
* Called when a section has been moved
178+
*
179+
* @param {details} param0 the watcher details
180+
* @param {object} param0.element the state object
181+
*/
182+
_reloadSectionDividerAndNavigation({element}) {
183+
this._reloadSectionDivider({element});
184+
this._reloadSectionNavigation({element});
185+
}
186+
187+
/**
188+
* Update a course section when the section number changes.
189+
*
190+
* The courseActions module used for most course section tools still depends on css classes and
191+
* section numbers (not id). To prevent inconsistencies when a section is moved, we need to refresh
192+
* the sectionreturnnum
193+
*
194+
* @param {Object} param
195+
* @param {Object} param.element details the update details.
196+
*/
197+
_refreshSectionNumber({element}) {
198+
// Find the element.
199+
const target = this.getElement(this.selectors.SECTION, element.id);
200+
if (!target) {
201+
// Job done. Nothing to refresh.
202+
return;
203+
}
204+
// The data-sectionnumber is the attribute used by components to store the section number.
205+
target.dataset.sectionreturnnum = element.number;
206+
}
145207
}
146208

147209
export const init = () => {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Contains the default section selector.
19+
*
20+
* @package core_courseformat
21+
* @copyright 2020 Ferran Recio <ferran@moodle.com>
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
namespace format_ucl\output\courseformat\content;
26+
27+
use core_courseformat\output\local\content\sectionselector as sectionselector_base;
28+
29+
/**
30+
* Represents the section selector.
31+
*
32+
* @package core_courseformat
33+
* @copyright 2020 Ferran Recio <ferran@moodle.com>
34+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35+
*/
36+
class sectionselector extends sectionselector_base {
37+
/**
38+
* Returns the output class template path.
39+
*
40+
* This method redirects the default template when the course content is rendered.
41+
*
42+
* @param \renderer_base $renderer
43+
* @return string
44+
*/
45+
public function get_template_name(\renderer_base $renderer): string {
46+
return 'format_ucl/local/content/sectionselector';
47+
}
48+
}

classes/output/renderer.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,25 @@ function course_section_add_cm_control($course, $section, $sectionreturn = null,
7979

8080
// Load the JS for the modal.
8181
$this->course_activitychooser($course->id);
82+
$format = course_get_format($course);
83+
$modinfo = $format->get_modinfo();
84+
$sectioninfo = $modinfo->get_section_info($section, MUST_EXIST);
8285

8386
return $this->render_from_template(
8487
'format_ucl/local/content/divider',
8588
[
89+
'id' => $sectioninfo->id,
8690
'section' => $section,
8791
'courseid' => $course->id,
88-
'labelurl' => new moodle_url('/course/mod.php',
89-
['id' => $course->id,
92+
'labelurl' => new moodle_url(
93+
'/course/mod.php',
94+
[
95+
'id' => $course->id,
9096
'add' => 'label',
9197
'section' => $section,
92-
'beforemod' => '0']
98+
'beforemod' => '0',
99+
'sr' => $section,
100+
]
93101
),
94102
]
95103
);

lib.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,41 @@ function format_ucl_output_fragment_section_navigation($args): string {
213213
}
214214

215215
$format = course_get_format($course);
216-
if (isset($args['pagesectionid'])) {
217-
$format->set_sectionid($args['pagesectionid']);
218-
} else if (isset($args['sr'])) {
219-
$format->set_sectionnum($args['sr']);
216+
$modinfo = $format->get_modinfo();
217+
$section = $modinfo->get_section_info_by_id($args['id'], MUST_EXIST);
218+
if (!$section->uservisible) {
219+
throw new require_login_exception('Section is not available');
220220
}
221221

222+
$renderer = $format->get_renderer($PAGE);
223+
return $renderer->course_section_navigation_updated($format, $section);
224+
}
225+
226+
/**
227+
* Section fragment renderer method.
228+
*
229+
* The fragment arguments are courseid, section id and sr (section return).
230+
*
231+
* @param array $args The fragment arguments.
232+
* @return string The rendered section.
233+
*
234+
* @throws require_login_exception
235+
*/
236+
function format_ucl_output_fragment_section_divider($args): string {
237+
global $PAGE;
238+
239+
$course = get_course($args['courseid']);
240+
if (!can_access_course($course, null, '', true)) {
241+
throw new require_login_exception('Course is not available');
242+
}
243+
244+
$format = course_get_format($course);
222245
$modinfo = $format->get_modinfo();
223246
$section = $modinfo->get_section_info_by_id($args['id'], MUST_EXIST);
224247
if (!$section->uservisible) {
225248
throw new require_login_exception('Section is not available');
226249
}
227250

228251
$renderer = $format->get_renderer($PAGE);
229-
return $renderer->course_section_navigation_updated($format, $section);
252+
return $renderer->course_section_add_cm_control($course, $section->section, $section->section);
230253
}

templates/local/content/cm.mustache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
}}
6060
{{< core_courseformat/local/content/cm}}
6161
{{$content}}
62-
{{! TODO Add text or media link }}
6362
<a role="button" href="{{{labelurl}}}" class="btn add-section bg-white mr-3">
6463
<i class="fa-solid fa-circle-plus mr-2"></i>
6564
{{#str}}addtextmedia, format_ucl{{/str}}

templates/local/content/divider.mustache

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,21 @@
2626
}
2727
}}
2828

29-
<div class="bg-primary-light text-center p-2 mt-4 bulk-hidden">
29+
<div class="bg-primary-light text-center p-2 mt-4 bulk-hidden" data-for="section-divider" data-id="{{id}}">
3030

31-
<a role="button" href="{{{ labelurl }}}" class="btn add-section bg-white mr-3">
31+
<a role="button" href="{{{labelurl}}}" class="btn add-section bg-white mr-3">
3232
<i class="fa-solid fa-circle-plus mr-2"></i>
33-
Add text and media
33+
{{#str}}addtextmedia, format_ucl{{/str}}
3434
</a>
3535

36-
<button class="btn bg-white add-section section-modchooser section-modchooser-link" data-action="open-chooser" data-sectionnum="{{section}}">
36+
<button
37+
class="btn bg-white add-section section-modchooser section-modchooser-link"
38+
data-action="open-chooser"
39+
data-sectionnum="{{section}}"
40+
data-sectionreturnnum="{{section}}"
41+
>
3742
<i class="fa-solid fa-file-circle-plus mr-2"></i>
38-
Add activity or resource
43+
{{#str}}addactivity, format_ucl{{/str}}
3944
</button>
4045

4146
</div>

version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626

2727
$plugin->component = 'format_ucl';
2828
$plugin->release = '0.1.0';
29-
$plugin->version = 2026031800;
29+
$plugin->version = 2026032400;
3030
$plugin->requires = 2022041900;
3131
$plugin->maturity = MATURITY_BETA;

0 commit comments

Comments
 (0)