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/>.
20 * @package core_course
21 * @copyright 2019 Mihail Geshoski <mihail@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_course\external;
27 defined('MOODLE_INTERNAL') || die();
29 use core\external\exporter;
33 * Course module chooser exporter.
35 * @copyright 2019 Mihail Geshoski <mihail@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class course_module_chooser_exporter extends exporter {
40 /** @var array $modules Array containing the available modules */
46 * @param array $modules The available course modules
47 * @param array $related The related data for the export
49 public function __construct(array $modules, array $related = []) {
50 $this->modules = $modules;
51 return parent::__construct([], $related);
55 * Return the list of additional properties.
59 protected static function define_other_properties() {
65 'label' => ['type' => PARAM_TEXT],
66 'modulename' => ['type' => PARAM_TEXT],
67 'description' => ['type' => PARAM_TEXT],
79 'null' => NULL_ALLOWED
87 * Get the additional values to inject while exporting.
89 * @param renderer_base $output The renderer.
90 * @return array Keys are the property names, values are their values.
92 protected function get_other_values(renderer_base $output) {
94 $options = new \stdClass();
95 $options->trusted = false;
96 $options->noclean = false;
97 $options->smiley = false;
98 $options->filter = false;
99 $options->para = true;
100 $options->newlines = false;
101 $options->overflowdiv = false;
103 $context = $this->related['context'];
106 foreach ($this->modules as $module) {
107 $customiconurl = null;
109 // The property 'name' may contain more than just the module, in which case we need to extract the true module name.
110 $modulename = $module->name;
111 if ($colon = strpos($modulename, ':')) {
112 $modulename = substr($modulename, 0, $colon);
115 if (isset($module->help) || !empty($module->help)) {
116 list($description) = external_format_text((string) $module->help, FORMAT_MARKDOWN,
117 $context->id, null, null, null, $options);
119 $description = get_string('nohelpforactivityorresource', 'moodle');
122 $icon = new \pix_icon('icon', '', $modulename);
124 // When exporting check if the title is an object, we assume it's a lang string object otherwise we send the raw string.
126 'label' => $module->title instanceof \lang_string ? $module->title->out() : $module->title,
127 'modulename' => $modulename,
128 'description' => $description,
130 'addoption' => $module->link->out(false),
132 'icon' => $icon->export_for_template($output)
137 'options' => $modulesdata
142 * Returns a list of objects that are related.
146 protected static function define_related() {
148 'context' => 'context'