MDL-81767 course: Refactor activity chooser button output

- Add new 'activitychooserbutton' output class that allows adding extra action_links through hooks.
- Add new 'before_activitychooserbutton_exported' hook so plugins could add extra action links
  to the activity chooser button.
- Unify activitychooserbutton and activitychooserbuttonactivity into a single template.
This commit is contained in:
Mikel Martín 2024-08-08 15:26:30 +02:00
parent 072fb90384
commit 84d9e83588
15 changed files with 323 additions and 74 deletions

View File

@ -0,0 +1,12 @@
issueNumber: MDL-81767
notes:
core_course:
- message: >-
Added new 'activitychooserbutton' output class to display the
activitychooser button. New action_links can be added to the button via
hooks converting it into a dropdown.
type: improved
- message: >-
New `core_course\hook\before_activitychooserbutton_exported` hook added
to allow third-party plugins to extend activity chooser button options
type: improved

View File

@ -0,0 +1,93 @@
<?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\hook;
use cm_info;
use section_info;
use core\hook\described_hook;
use core_course\output\activitychooserbutton;
/**
* Hook before activity chooser button export.
*
* @package core_course
* @copyright 2024 Mikel Martín <mikel@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class before_activitychooserbutton_exported implements described_hook {
/**
* Constructor.
*
* @param activitychooserbutton $activitychooserbutton the activity chooser button output
* @param section_info $section the course section
* @param cm_info|null $cm the course module
*/
public function __construct(
/** @var activitychooserbutton the activity chooser button output */
protected activitychooserbutton $activitychooserbutton,
/** @var section_info the course section */
protected section_info $section,
/** @var cm_info|null the course module */
protected ?cm_info $cm = null,
) {
}
/**
* Describes the hook purpose.
*
* @return string
*/
public static function get_hook_description(): string {
return 'This hook is triggered when a activity chooser button is exported.';
}
/**
* List of tags that describe this hook.
*
* @return string[]
*/
public static function get_hook_tags(): array {
return ['course'];
}
/**
* Get activitychooserbutton output instance.
*
* @return activitychooserbutton
*/
public function get_activitychooserbutton(): activitychooserbutton {
return $this->activitychooserbutton;
}
/**
* Get course section instance.
*
* @return section_info
*/
public function get_section(): section_info {
return $this->section;
}
/**
* Get course module instance.
*
* @return cm_info|null
*/
public function get_cm(): ?cm_info {
return $this->cm;
}
}

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\output;
use action_link;
use cm_info;
use renderable;
use renderer_base;
use section_info;
use stdClass;
use templatable;
use core\di;
use core\hook;
/**
* Class to render a activity chooser button.
*
* @package core_course
* @copyright 2024 Mikel Martín <mikel@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activitychooserbutton implements templatable, renderable {
/**
* Constructor.
*
* @param section_info $section the section info
* @param cm_info|null $mod the course module ionfo
* @param int|null $sectionreturn the section to return to
* @param array|null $actionlinks the action links
*/
public function __construct(
/** @var section_info the section object */
protected section_info $section,
/** @var cm_info|null the course module instance */
protected ?cm_info $mod = null,
/** @var sectionreturn|null the section to return to */
protected ?int $sectionreturn = null,
/** @var array|null action_link[] the action links */
protected ?array $actionlinks = [],
) {
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return stdClass data context for a mustache template
*/
public function export_for_template(renderer_base $output): stdClass {
// Look for plugins that want to add extra action links to the activity chooser button.
di::get(hook\manager::class)->dispatch(
new \core_course\hook\before_activitychooserbutton_exported(
$this,
$this->section,
$this->mod,
),
);
return (object)[
'sectionnum' => $this->section->section,
'sectionreturn' => $this->sectionreturn ?? false,
'modid' => $this->mod ? $this->mod->id : false,
'activityname' => $this->mod ? $this->mod->get_formatted_name() : false,
'hasactionlinks' => !empty($this->actionlinks),
'actionlinks' => array_map(fn(action_link $action) => $action->export_for_template($output), $this->actionlinks),
];
}
/**
* Add an action link.
*
* @param action_link $action the action link to add
*/
public function add_action_link(action_link $action): void {
$this->actionlinks[] = $action;
}
}

View File

@ -136,6 +136,7 @@ class cm implements named_templatable, renderable {
$haspartials['editor'] = $this->add_editor_data($data, $output);
$haspartials['groupmode'] = $this->add_groupmode_data($data, $output);
$haspartials['visibility'] = $this->add_visibility_data($data, $output);
$this->add_actvitychooserbutton_data($data, $output);
$this->add_format_data($data, $haspartials, $output);
// Calculated fields.
@ -359,6 +360,17 @@ class cm implements named_templatable, renderable {
return false;
}
/**
* Add the activity chooser button data to the data structure.
*
* @param stdClass $data the current cm data reference
* @param renderer_base $output typically, the renderer that's calling this function
*/
protected function add_actvitychooserbutton_data(stdClass &$data, renderer_base $output): void {
$activitychooserbutton = new \core_course\output\activitychooserbutton($this->section, $this->mod);
$data->activitychooserbutton = $activitychooserbutton->export_for_template($output);
}
/**
* Returns the CSS classes for the activity name/content
*

View File

@ -59,7 +59,9 @@
}}
{{#editing}}
{{< core_courseformat/local/content/divider}}
{{$content}}{{> core_course/activitychooserbuttonactivity}}{{/content}}
{{$content}}
{{#activitychooserbutton}}{{> core_course/activitychooserbutton}}{{/activitychooserbutton}}
{{/content}}
{{/ core_courseformat/local/content/divider}}
{{/editing}}
<div class="activity-item focus-control {{#modstealth}}hiddenactivity{{/modstealth}}{{!

View File

@ -29,6 +29,6 @@
<div class="divider bulk-hidden d-flex justify-content-center align-items-center {{$extraclasses}}{{extraclasses}}{{/extraclasses}}">
<hr>
<div class="divider-content px-3">
{{$content}}{{content}}{{/content}}
{{$content}}{{{content}}}{{/content}}
</div>
</div>

View File

@ -39,7 +39,7 @@
{{$extraclasses}}always-hidden mt-2{{/extraclasses}}
{{$content}}
<a href="{{{url}}}"
class="btn add-content section-modchooser section-modchooser-link activitychooser-button d-flex justify-content-center align-items-center p-1 icon-no-margin
class="btn add-content section-modchooser section-modchooser-link d-flex justify-content-center align-items-center p-1 icon-no-margin
{{^canaddsection}}disabled{{/canaddsection}}"
data-add-sections="{{title}}"
data-new-sections="{{newsection}}"

View File

@ -224,16 +224,20 @@ class core_course_renderer extends plugin_renderer_base {
return '';
}
$data = [
'sectionnum' => $section,
'sectionreturn' => $sectionreturn
];
$ajaxcontrol = $this->render_from_template('course/activitychooserbutton', $data);
$sectioninfo = get_fast_modinfo($course)->get_section_info($section);
$activitychooserbutton = new \core_course\output\activitychooserbutton($sectioninfo, null, $sectionreturn);
// Load the JS for the modal.
$this->course_activitychooser($course->id);
return $ajaxcontrol;
return $this->render_from_template(
'core_courseformat/local/content/divider',
[
'content' => $this->render($activitychooserbutton),
'extraclasses' => 'always-visible my-3',
]
);
}
/**

View File

@ -19,26 +19,41 @@
Displays a add activity or resource button.
Context variables required for this template:
* sectionid - Relative section number (field course_sections.section).
* sectionreturn - The section to link back to.
Example context (json):
{
"sectionid": 1,
"hasactionlinks": false,
"sectionnum": 0,
"modid": 1,
"activityname": "Activity example",
"sectionreturn": 0
}
}}
{{< core_courseformat/local/content/divider}}
{{$extraclasses}}always-visible my-3{{/extraclasses}}
{{$content}}
<button class="btn add-content section-modchooser section-modchooser-link d-flex justify-content-center align-items-center py-1 px-2"
data-action="open-chooser"
data-sectionnum="{{sectionnum}}"
{{#sectionreturn}}data-sectionreturnnum="{{.}}"{{/sectionreturn}}
>
{{#pix}} t/add, core {{/pix}}
<span class="activity-add-text pe-1">{{#str}}addresourceoractivity, core{{/str}}</span>
</button>
{{/content}}
{{/ core_courseformat/local/content/divider}}
{{! Single button to add a new activity or resource. }}
{{^hasactionlinks}}
{{> core_course/addresourceoractivitybutton }}
{{/hasactionlinks}}
{{! Dropdown with "add a new activity or resource" item and any other action links. }}
{{#hasactionlinks}}
<div class="dropdown">
<button class="btn add-content d-flex justify-content-center align-items-center p-1 icon-no-margin"
type="button"
id="dropdownMenuButton"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
aria-label="{{#str}}insertcontentbefore, core, { "activityname": {{#quote}} {{activityname}} {{/quote}} } {{/str}}"
tabindex="0"
title="{{#str}}addcontent, core{{/str}}"
>
{{#pix}} t/add, core {{/pix}}
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
{{> core_course/addresourceoractivitybutton }}
{{#actionlinks}}
{{> core/action_link}}
{{/actionlinks}}
</div>
</div>
{{/hasactionlinks}}

View File

@ -1,44 +0,0 @@
{{!
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 core_course/activitychooserbuttonactivity
Displays a add activity or resource button.
Context variables required for this template:
* id - Which activity we want to add the new activity before.
* num - Relative section number (field course_sections.section).
* sectionreturn - The section to link back to.
Example context (json):
{
"id": 0,
"sectionnum": 1,
"sectionreturn": 5
}
}}
<button class="btn add-content section-modchooser section-modchooser-link activitychooser-button d-flex justify-content-center align-items-center p-1 icon-no-margin"
data-action="open-chooser"
data-sectionnum="{{sectionnum}}"
{{#sectionreturn}}data-sectionreturnnum="{{.}}"{{/sectionreturn}}
data-beforemod="{{id}}"
aria-label="{{#str}}insertresourceoractivitybefore, core, { "activityname": {{#quote}} {{activityname}} {{/quote}} } {{/str}}"
tabindex="0"
title="{{#str}}addresourceoractivity, core{{/str}}"
>
{{#pix}} t/add, core {{/pix}}
</button>

View File

@ -0,0 +1,57 @@
{{!
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 core_course/addresourceoractivitybutton
Displays a add resource or activity button.
Example context (json):
{
"sectionnum": 0,
"modid": 1,
"activityname": "Activity example",
"sectionreturn": 0
}
}}
<button class="section-modchooser section-modchooser-link {{!
}}{{^hasactionlinks}}btn add-content d-flex justify-content-center align-items-center p-1 icon-no-margin{{/hasactionlinks}}{{!
}}{{#hasactionlinks}}dropdown-item{{/hasactionlinks}}"
data-action="open-chooser"
data-sectionnum="{{sectionnum}}"
{{#sectionreturn}}data-sectionreturnnum="{{.}}"{{/sectionreturn}}
{{#modid}}
data-beforemod="{{modid}}"
aria-label="{{#str}}insertresourceoractivitybefore, core, { "activityname": {{#quote}} {{activityname}} {{/quote}} } {{/str}}"
tabindex="0"
title="{{#str}}addresourceoractivity, core{{/str}}"
{{/modid}}
>
{{^hasactionlinks}}
{{#modid}}
{{#pix}} t/add, core {{/pix}}
{{/modid}}
{{^modid}}
<div class="px-1">
{{#pix}} t/add, core {{/pix}}
<span class="activity-add-text pr-1">{{#str}}addresourceoractivity, core{{/str}}</span>
</div>
{{/modid}}
{{/hasactionlinks}}
{{#hasactionlinks}}
{{#pix}} i/activities, core {{/pix}}{{#str}}activityorresource, core{{/str}}
{{/hasactionlinks}}
</button>

View File

@ -40,6 +40,7 @@ $string['activityheader'] = 'Activity menu';
$string['activitymodule'] = 'Activity module';
$string['activitymodules'] = 'Activity modules';
$string['activitynotready'] = 'Activity not ready yet';
$string['activityorresource'] = 'Activity or resource';
$string['activityreport'] = 'Activity report';
$string['activityreports'] = 'Activity reports';
$string['activityselect'] = 'Select this activity to be moved elsewhere';
@ -54,6 +55,7 @@ $string['addadmin'] = 'Add admin';
$string['addblock'] = 'Add a block';
$string['addcomment'] = 'Add a comment...';
$string['addcondition'] = 'Add condition';
$string['addcontent'] = 'Add content';
$string['addcountertousername'] = 'Create user by adding number to username';
$string['addcreator'] = 'Add course creator';
$string['adddots'] = 'Add...';
@ -1168,6 +1170,7 @@ $string['indicator:userforumstracking'] = 'User is tracking forums';
$string['indicator:userforumstracking_help'] = 'This indicator represents whether or not the student has tracking turned on in the forums.';
$string['info'] = 'Information';
$string['inprogress'] = 'In progress';
$string['insertcontentbefore'] = 'Insert content before \'{$a->activityname}\'';
$string['insertresourceoractivitybefore'] = 'Insert an activity or resource before \'{$a->activityname}\'';
$string['institution'] = 'Institution';
$string['instudentview'] = 'in student view';

View File

@ -1629,7 +1629,7 @@ $divider-hover-color: $primary !default;
.btn.add-content {
position: relative;
z-index: 1;
@include border-radius($rounded-pill);
@include border-radius();
font-size: $font-size-sm;
font-weight: bold;
color: theme-color-level("primary", $alert-color-level);
@ -1796,6 +1796,7 @@ $divider-hover-color: $primary !default;
> .activity-item {
border: $border-width solid $border-color;
padding: 0;
margin: map-get($spacers, 2) 0;
.activity-altcontent {
margin-top: 0;
}

View File

@ -29832,7 +29832,7 @@ span.editinstructions .alert-link {
.btn.add-content {
position: relative;
z-index: 1;
border-radius: 50rem;
border-radius: 0.5rem;
font-size: 0.8203125rem;
font-weight: bold;
color: #083863;
@ -29970,6 +29970,7 @@ span.editinstructions .alert-link {
.activity.subsection > .activity-item {
border: 1px solid #dee2e6;
padding: 0;
margin: 0.5rem 0;
}
.activity.subsection > .activity-item .activity-altcontent {
margin-top: 0;

View File

@ -29832,7 +29832,7 @@ span.editinstructions .alert-link {
.btn.add-content {
position: relative;
z-index: 1;
border-radius: 50rem;
border-radius: 0.25rem;
font-size: 0.8203125rem;
font-weight: bold;
color: #083863;
@ -29970,6 +29970,7 @@ span.editinstructions .alert-link {
.activity.subsection > .activity-item {
border: 1px solid #dee2e6;
padding: 0;
margin: 0.5rem 0;
}
.activity.subsection > .activity-item .activity-altcontent {
margin-top: 0;