MDL-67264 core_templates: If classes exists render it out
[moodle.git] / course / classes / output / modchooser_item.php
CommitLineData
4f2378d9
FM
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 * The modchooser_item renderable.
19 *
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
23 */
24
25namespace core_course\output;
26defined('MOODLE_INTERNAL') || die();
27
28use context;
29use lang_string;
30use pix_icon;
31
32/**
33 * The modchooser_item renderable class.
34 *
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
38 */
39class modchooser_item extends \core\output\chooser_item {
40
e86cbd49
MF
41 /** @var string */
42 protected $customiconurl;
43
4f2378d9
FM
44 /**
45 * Constructor.
46 *
47 * @param stdClass $module The module.
48 * @param context $context The relevant context.
49 */
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);
55 }
e86cbd49
MF
56 if (preg_match('/src="([^"]*)"/i', $module->icon, $matches)) {
57 // Use the custom icon.
58 $this->customiconurl = str_replace('&amp;', '&', $matches[1]);
59 }
60
4f2378d9
FM
61 $icon = new pix_icon('icon', '', $modulename, ['class' => 'icon']);
62 $help = isset($module->help) ? $module->help : new lang_string('nohelpforactivityorresource', 'moodle');
63
64 parent::__construct($module->name, $module->title, $module->link->out(false), $icon, $help, $context);
65 }
66
e86cbd49
MF
67 /**
68 * Export for template.
69 *
70 * @param \renderer_base $output The renderer
71 * @return \stdClass $data
72 */
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;
80 }
81 }
82 }
83 return $data;
84 }
4f2378d9 85}