--- /dev/null
+<?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/>.
+
+/**
+ * Author exporter.
+ *
+ * @package core_course
+ * @copyright 2019 Mihail Geshoski <mihail@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core_course\external;
+
+defined('MOODLE_INTERNAL') || die();
+
+use core\external\exporter;
+use renderer_base;
+
+/**
+ * Course module chooser exporter.
+ *
+ * @copyright 2019 Mihail Geshoski <mihail@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class course_module_chooser_exporter extends exporter {
+
+ /** @var array $modules Array containing the available modules */
+ private $modules;
+
+ /**
+ * Constructor.
+ *
+ * @param array $modules The available course modules
+ * @param array $related The related data for the export
+ */
+ public function __construct(array $modules, array $related = []) {
+ $this->modules = $modules;
+ return parent::__construct([], $related);
+ }
+
+ /**
+ * Return the list of additional properties.
+ *
+ * @return array
+ */
+ protected static function define_other_properties() {
+ return [
+ 'options' => [
+ 'multiple' => true,
+ 'optional' => true,
+ 'type' => [
+ 'label' => ['type' => PARAM_TEXT],
+ 'modulename' => ['type' => PARAM_TEXT],
+ 'description' => ['type' => PARAM_TEXT],
+ 'urls' => [
+ 'type' => [
+ 'addoption' => [
+ 'type' => PARAM_URL
+ ]
+ ]
+ ],
+ 'icon' => [
+ 'type' => PARAM_RAW,
+ 'optional' => true,
+ 'default' => null,
+ 'null' => NULL_ALLOWED
+ ]
+ ]
+ ]
+ ];
+ }
+
+ /**
+ * Get the additional values to inject while exporting.
+ *
+ * @param renderer_base $output The renderer.
+ * @return array Keys are the property names, values are their values.
+ */
+ protected function get_other_values(renderer_base $output) {
+
+ $options = new \stdClass();
+ $options->trusted = false;
+ $options->noclean = false;
+ $options->smiley = false;
+ $options->filter = false;
+ $options->para = true;
+ $options->newlines = false;
+ $options->overflowdiv = false;
+
+ $context = $this->related['context'];
+
+ $modulesdata = [];
+ foreach ($this->modules as $module) {
+ $customiconurl = null;
+
+ // The property 'name' may contain more than just the module, in which case we need to extract the true module name.
+ $modulename = $module->name;
+ if ($colon = strpos($modulename, ':')) {
+ $modulename = substr($modulename, 0, $colon);
+ }
+
+ if (isset($module->help) || !empty($module->help)) {
+ list($description) = external_format_text((string) $module->help, FORMAT_MARKDOWN,
+ $context->id, null, null, null, $options);
+ } else {
+ $description = get_string('nohelpforactivityorresource', 'moodle');
+ }
+
+ $icon = new \pix_icon('icon', '', $modulename);
+
+ // When exporting check if the title is an object, we assume it's a lang string object otherwise we send the raw string.
+ $modulesdata[] = [
+ 'label' => $module->title instanceof \lang_string ? $module->title->out() : $module->title,
+ 'modulename' => $modulename,
+ 'description' => $description,
+ 'urls' => [
+ 'addoption' => $module->link->out(false),
+ ],
+ 'icon' => $icon->export_for_template($output)
+ ];
+ }
+
+ return [
+ 'options' => $modulesdata
+ ];
+ }
+
+ /**
+ * Returns a list of objects that are related.
+ *
+ * @return array
+ */
+ protected static function define_related() {
+ return [
+ 'context' => 'context'
+ ];
+ }
+}
+++ /dev/null
-<?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/>.
-
-/**
- * The modchooser renderable.
- *
- * @package core_course
- * @copyright 2016 Frédéric Massart - FMCorz.net
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-namespace core_course\output;
-defined('MOODLE_INTERNAL') || die();
-
-use core\output\chooser;
-use core\output\chooser_section;
-use context_course;
-use lang_string;
-use moodle_url;
-use pix_icon;
-use renderer_base;
-use stdClass;
-
-/**
- * The modchooser renderable class.
- *
- * @package core_course
- * @copyright 2016 Frédéric Massart - FMCorz.net
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class modchooser extends chooser {
-
- /** @var stdClass The course. */
- public $course;
-
- /**
- * Constructor.
- *
- * @param stdClass $course The course.
- * @param stdClass[] $modules The modules.
- */
- public function __construct(stdClass $course, array $modules) {
- $this->course = $course;
-
- $sections = [];
- $context = context_course::instance($course->id);
-
- // Activities.
- $activities = array_filter($modules, function($mod) {
- return ($mod->archetype !== MOD_ARCHETYPE_RESOURCE && $mod->archetype !== MOD_ARCHETYPE_SYSTEM);
- });
- if (count($activities)) {
- $sections[] = new chooser_section('activities', new lang_string('activities'),
- array_map(function($module) use ($context) {
- return new modchooser_item($module, $context);
- }, $activities)
- );
- }
-
- $resources = array_filter($modules, function($mod) {
- return ($mod->archetype === MOD_ARCHETYPE_RESOURCE);
- });
- if (count($resources)) {
- $sections[] = new chooser_section('resources', new lang_string('resources'),
- array_map(function($module) use ($context) {
- return new modchooser_item($module, $context);
- }, $resources)
- );
- }
-
- $actionurl = new moodle_url('/course/jumpto.php');
- $title = new lang_string('addresourceoractivity');
- parent::__construct($actionurl, $title, $sections, 'jumplink');
-
- $this->set_instructions(new lang_string('selectmoduletoviewhelp'));
- $this->add_param('course', $course->id);
- }
-
- /**
- * Export for template.
- *
- * @param renderer_base The renderer.
- * @return stdClass
- */
- public function export_for_template(renderer_base $output) {
- $data = parent::export_for_template($output);
- $data->courseid = $this->course->id;
- return $data;
- }
-
-}
+++ /dev/null
-<?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/>.
-
-/**
- * The modchooser_item renderable.
- *
- * @package core_course
- * @copyright 2016 Frédéric Massart - FMCorz.net
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-namespace core_course\output;
-defined('MOODLE_INTERNAL') || die();
-
-use context;
-use lang_string;
-use pix_icon;
-
-/**
- * The modchooser_item renderable class.
- *
- * @package core_course
- * @copyright 2016 Frédéric Massart - FMCorz.net
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class modchooser_item extends \core\output\chooser_item {
-
- /** @var string */
- protected $customiconurl;
-
- /**
- * Constructor.
- *
- * @param stdClass $module The module.
- * @param context $context The relevant context.
- */
- public function __construct($module, context $context) {
- // The property 'name' may contain more than just the module, in which case we need to extract the true module name.
- $modulename = $module->name;
- if ($colon = strpos($modulename, ':')) {
- $modulename = substr($modulename, 0, $colon);
- }
- if (preg_match('/src="([^"]*)"/i', $module->icon, $matches)) {
- // Use the custom icon.
- $this->customiconurl = str_replace('&', '&', $matches[1]);
- }
-
- $icon = new pix_icon('icon', '', $modulename, ['class' => 'icon']);
- $help = isset($module->help) ? $module->help : new lang_string('nohelpforactivityorresource', 'moodle');
-
- parent::__construct($module->name, $module->title, $module->link->out(false), $icon, $help, $context);
- }
-
- /**
- * Export for template.
- *
- * @param \renderer_base $output The renderer
- * @return \stdClass $data
- */
- public function export_for_template(\renderer_base $output) {
- $data = parent::export_for_template($output);
- if ($this->customiconurl && !empty($data->icon['attributes'])) {
- // Replace icon source with a module-provided icon.
- foreach ($data->icon['attributes'] as &$attribute) {
- if ($attribute['name'] === 'src') {
- $attribute['value'] = $this->customiconurl;
- }
- }
- }
- return $data;
- }
-}
* @return string The composed HTML for the module
*/
public function course_modchooser($modules, $course) {
+ debugging('course_modchooser() is deprecated. Please use course_activitychooser() instead.', DEBUG_DEVELOPER);
if (!$this->page->requires->should_create_one_time_item_now('core_course_modchooser')) {
return '';
}
return $this->render($modchooser);
}
+ /**
+ * Build the HTML for the module chooser javascript popup.
+ *
+ * @param int $courseid The course id to fetch modules for.
+ * @return string
+ */
+ public function course_activitychooser($courseid) {
+
+ if (!$this->page->requires->should_create_one_time_item_now('core_course_modchooser')) {
+ return '';
+ }
+
+ $this->page->requires->js_call_amd('core_course/modchooser', 'init', [$courseid]);
+
+ return '';
+ }
+
/**
* Build the HTML for a specified set of modules
*
$modchooser.= html_writer::start_tag('div', array('class' => 'section-modchooser'));
$icon = $this->output->pix_icon('t/add', '');
$span = html_writer::tag('span', $straddeither, array('class' => 'section-modchooser-text'));
- $modchooser .= html_writer::tag('span', $icon . $span, array('class' => 'section-modchooser-link'));
+ $modchooser .= html_writer::tag('button', $icon . $span, array(
+ 'class' => 'section-modchooser-link btn btn-link',
+ 'data-action' => 'open-chooser',
+ 'data-sectionid' => $section,
+ 'disabled' => true
+ )
+ );
$modchooser.= html_writer::end_tag('div');
$modchooser.= html_writer::end_tag('div');
$output = html_writer::tag('div', $output, array('class' => 'show addresourcedropdown'));
$modchooser = html_writer::tag('div', $modchooser, array('class' => 'hide addresourcemodchooser'));
}
- $output = $this->course_modchooser($modules, $course) . $modchooser . $output;
+ $output = $this->course_activitychooser($course->id) . $modchooser . $output;
}
return $output;
var CSS = {
PAGECONTENT: 'body',
SECTION: null,
- SECTIONMODCHOOSER: 'span.section-modchooser-link',
+ SECTIONMODCHOOSER: 'button.section-modchooser-link',
SITEMENU: '.block_site_main_menu',
SITETOPIC: 'div.sitetopic'
};