Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core_course\output;
18
 
19
use action_link;
20
use cm_info;
21
use renderable;
22
use renderer_base;
23
use section_info;
24
use stdClass;
25
use templatable;
26
use core\di;
27
use core\hook;
28
 
29
/**
30
 * Class to render a activity chooser button.
31
 *
32
 * @package    core_course
33
 * @copyright  2024 Mikel Martín <mikel@moodle.com>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class activitychooserbutton implements templatable, renderable {
37
 
38
    /**
39
     * Constructor.
40
     *
41
     * @param section_info $section the section info
42
     * @param cm_info|null $mod the course module ionfo
43
     * @param int|null $sectionreturn the section to return to
44
     * @param array|null $actionlinks the action links
45
     */
46
    public function __construct(
47
        /** @var section_info the section object */
48
        protected section_info $section,
49
        /** @var cm_info|null the course module instance */
50
        protected ?cm_info $mod = null,
51
        /** @var int|null the section to return to */
52
        protected ?int $sectionreturn = null,
53
        /** @var array|null action_link[] the action links */
54
        protected ?array $actionlinks = [],
55
    ) {
56
    }
57
 
58
    /**
59
     * Export this data so it can be used as the context for a mustache template.
60
     *
61
     * @param renderer_base $output typically, the renderer that's calling this function
62
     * @return stdClass data context for a mustache template
63
     */
64
    public function export_for_template(renderer_base $output): stdClass {
65
        // Look for plugins that want to add extra action links to the activity chooser button.
66
        di::get(hook\manager::class)->dispatch(
67
            new \core_course\hook\before_activitychooserbutton_exported(
68
                $this,
69
                $this->section,
70
                $this->mod,
71
            ),
72
        );
73
 
74
        return (object)[
75
            'sectionnum' => $this->section->section,
76
            'sectionname' => get_section_name($this->section->course, $this->section),
77
            'sectionreturn' => $this->sectionreturn ?? false,
78
            'modid' => $this->mod ? $this->mod->id : false,
79
            'activityname' => $this->mod ? $this->mod->get_formatted_name() : false,
80
            'hasactionlinks' => !empty($this->actionlinks),
81
            'actionlinks' => array_map(fn(action_link $action) => $action->export_for_template($output), $this->actionlinks),
82
        ];
83
    }
84
 
85
    /**
86
     * Add an action link.
87
     *
88
     * @param action_link $action the action link to add
89
     */
90
    public function add_action_link(action_link $action): void {
91
        $this->actionlinks[] = $action;
92
    }
93
}