MDL-80746 core_course: fix group_selector rendering

This change:
- Fixes the group_selector constructor, removing the unnecessarily
deprecated param (this code is main-only, so no need to deprecate) and
fixes all calling code.
- moves the button and content into separate named_templatable
renderables, cleaning up the group_selector code so that it only needs
to make a single call to render and doesn't concern itself with contexts
of other renderables.
This commit is contained in:
Jake Dallimore 2024-09-16 15:18:50 +08:00 committed by Mihail Geshoski
parent 8d1cdf6f8e
commit c648839070
8 changed files with 151 additions and 45 deletions

View file

@ -28,28 +28,26 @@ use stdClass;
*/
class group_selector extends comboboxsearch {
/**
* @var stdClass The context object.
*/
private stdClass $context;
/** @var int|bool the active group, false if groups not used. */
private int|bool $activegroup;
/**
* The class constructor.
*
* @param null|stdClass $course This parameter has been deprecated since Moodle 4.5 and should not be used anymore.
* @param stdClass $context The context object.
*/
public function __construct(?stdClass $course, stdClass $context) {
if ($course !== null) {
debugging(
'The course argument has been deprecated. Please remove it from your group_selector class instances.',
DEBUG_DEVELOPER,
);
}
$this->context = $context;
public function __construct(private stdClass $context) {
$this->activegroup = $this->get_active_group();
$this->label = $this->get_label();
// The second and third arguments (buttoncontent and dropdowncontent) need to be rendered here, since the comboboxsearch
// template expects HTML in its respective context properties. Ideally, children of comboboxsearch would leverage Mustache's
// blocks pragma, meaning a child template could extend the comboboxsearch, allowing rendering of the child component,
// instead of needing to inject the child's content HTML as part of rendering the comboboxsearch parent, as is the case
// here. Achieving this, however, requires a refactor of comboboxsearch. For now, this must be pre-rendered and injected.
parent::__construct(false, $this->get_button_content(), $this->get_dropdown_content(), 'group-search',
'groupsearchwidget', 'groupsearchdropdown overflow-auto', null, true, $this->get_label(), 'group',
$this->get_active_group());
'groupsearchwidget', 'groupsearchdropdown overflow-auto', null, true, $this->label, 'group',
$this->activegroup);
}
/**
@ -58,23 +56,10 @@ class group_selector extends comboboxsearch {
* @return string HTML fragment
*/
private function get_button_content(): string {
global $OUTPUT;
global $PAGE;
$groupsselectorbutton = new group_selector_button($this->context, $this->activegroup, $this->label);
$activegroup = $this->get_active_group();
$buttondata = [
'label' => $this->get_label(),
'group' => $activegroup,
];
if ($activegroup) {
$group = groups_get_group($activegroup);
$buttondata['selectedgroup'] = format_string($group->name, true,
['context' => $this->context->get_course_context()]);
} else if ($activegroup === 0) {
$buttondata['selectedgroup'] = get_string('allparticipants');
}
return $OUTPUT->render_from_template('core_group/comboboxsearch/group_selector', $buttondata);
return $PAGE->get_renderer('core', 'course')->render($groupsselectorbutton);
}
/**
@ -83,13 +68,10 @@ class group_selector extends comboboxsearch {
* @return string HTML fragment
*/
private function get_dropdown_content(): string {
global $OUTPUT;
global $PAGE;
$groupsdropdownform = new group_selector_dropdown_form($this->context);
return $OUTPUT->render_from_template('core_group/comboboxsearch/searchbody', [
'courseid' => $this->context->get_course_context()->instanceid,
'currentvalue' => optional_param('groupsearchvalue', '', PARAM_NOTAGS),
'instance' => rand(),
]);
return $PAGE->get_renderer('core', 'course')->render($groupsdropdownform);
}
/**

View file

@ -0,0 +1,69 @@
<?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\output\actionbar;
use context;
use core\output\named_templatable;
use core\output\renderable;
use core\output\renderer_base;
/**
* Renderable class for the group selection button state.
*
* This form is the button state for the group_selector renderable, which itself is an extension of the comboboxsearch component.
* {@see group_selector}.
*
* @package core_course
* @copyright 2024 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class group_selector_button implements renderable, named_templatable {
/**
* The class constructor.
*
* @param context $context The context instance.
* @param int|bool $activegroup The active group, or false if groups not used.
* @param string $label the label string.
*/
public function __construct(
protected context $context,
protected int|bool $activegroup,
protected string $label
) {
}
public function export_for_template(renderer_base $output) {
$context = [
'label' => $this->label,
'group' => $this->activegroup,
];
if ($this->activegroup) {
$group = groups_get_group($this->activegroup);
$context['selectedgroup'] = format_string($group->name, true, ['context' => $this->context->get_course_context()]);
} else if ($this->activegroup === 0) {
$context['selectedgroup'] = get_string('allparticipants');
}
return $context;
}
public function get_template_name(renderer_base $renderer): string {
return 'core_group/comboboxsearch/group_selector';
}
}

View file

@ -0,0 +1,56 @@
<?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\output\actionbar;
use core\output\named_templatable;
use core\output\renderable;
use core\output\renderer_base;
/**
* Renderable class for the group selection dropdown form.
*
* This form is the content for the group_selector renderable, which itself is an extension of the comboboxsearch component.
* {@see group_selector}.
*
* @package core_course
* @copyright 2024 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class group_selector_dropdown_form implements renderable, named_templatable {
/**
* The class constructor.
*
* @param \context $context The context instance.
*/
public function __construct(
protected \context $context
) {
}
public function export_for_template(renderer_base $output) {
return [
'courseid' => $this->context->get_course_context()->instanceid,
'currentvalue' => optional_param('groupsearchvalue', '', PARAM_NOTAGS),
'instance' => rand(),
];
}
public function get_template_name(renderer_base $renderer): string {
return 'core_group/comboboxsearch/searchbody';
}
}