2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * The modchooser_item renderable.
20 * @package core_course
21 * @copyright 2016 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_course\output;
26 defined('MOODLE_INTERNAL') || die();
33 * The modchooser_item renderable class.
35 * @package core_course
36 * @copyright 2016 Frédéric Massart - FMCorz.net
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class modchooser_item extends \core\output\chooser_item {
42 protected $customiconurl;
47 * @param stdClass $module The module.
48 * @param context $context The relevant context.
50 public function __construct($module, context $context) {
51 // The property 'name' may contain more than just the module, in which case we need to extract the true module name.
52 $modulename = $module->name;
53 if ($colon = strpos($modulename, ':')) {
54 $modulename = substr($modulename, 0, $colon);
56 if (preg_match('/src="([^"]*)"/i', $module->icon, $matches)) {
57 // Use the custom icon.
58 $this->customiconurl = str_replace('&', '&', $matches[1]);
61 $icon = new pix_icon('icon', '', $modulename, ['class' => 'icon']);
62 $help = isset($module->help) ? $module->help : new lang_string('nohelpforactivityorresource', 'moodle');
64 parent::__construct($module->name, $module->title, $module->link->out(false), $icon, $help, $context);
68 * Export for template.
70 * @param \renderer_base $output The renderer
71 * @return \stdClass $data
73 public function export_for_template(\renderer_base $output) {
74 $data = parent::export_for_template($output);
75 if ($this->customiconurl && !empty($data->icon['attributes'])) {
76 // Replace icon source with a module-provided icon.
77 foreach ($data->icon['attributes'] as &$attribute) {
78 if ($attribute['name'] === 'src') {
79 $attribute['value'] = $this->customiconurl;