From cd2efd12cac1cb41ac39369cf0db3c4789ee527c Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Mon, 3 Feb 2020 09:31:44 +0800 Subject: [PATCH] MDL-67264 core_course: Begin set up for Activity chooser --- .../course_module_chooser_exporter.php | 151 ++++++++++++++++++ course/classes/output/modchooser.php | 104 ------------ course/classes/output/modchooser_item.php | 85 ---------- course/renderer.php | 28 +++- .../moodle-course-modchooser-debug.js | Bin 4789 -> 4791 bytes .../moodle-course-modchooser-min.js | Bin 1722 -> 1724 bytes .../moodle-course-modchooser.js | Bin 4789 -> 4791 bytes course/yui/src/modchooser/js/modchooser.js | 2 +- 8 files changed, 178 insertions(+), 192 deletions(-) create mode 100644 course/classes/external/course_module_chooser_exporter.php delete mode 100644 course/classes/output/modchooser.php delete mode 100644 course/classes/output/modchooser_item.php diff --git a/course/classes/external/course_module_chooser_exporter.php b/course/classes/external/course_module_chooser_exporter.php new file mode 100644 index 00000000000..01715e66303 --- /dev/null +++ b/course/classes/external/course_module_chooser_exporter.php @@ -0,0 +1,151 @@ +. + +/** + * Author exporter. + * + * @package core_course + * @copyright 2019 Mihail Geshoski + * @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 + * @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' + ]; + } +} diff --git a/course/classes/output/modchooser.php b/course/classes/output/modchooser.php deleted file mode 100644 index 95e487cfeaf..00000000000 --- a/course/classes/output/modchooser.php +++ /dev/null @@ -1,104 +0,0 @@ -. - -/** - * 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; - } - -} diff --git a/course/classes/output/modchooser_item.php b/course/classes/output/modchooser_item.php deleted file mode 100644 index 553e534c2b4..00000000000 --- a/course/classes/output/modchooser_item.php +++ /dev/null @@ -1,85 +0,0 @@ -. - -/** - * 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; - } -} diff --git a/course/renderer.php b/course/renderer.php index 51b52548782..b02df933065 100644 --- a/course/renderer.php +++ b/course/renderer.php @@ -141,6 +141,7 @@ class core_course_renderer extends plugin_renderer_base { * @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 ''; } @@ -148,6 +149,23 @@ class core_course_renderer extends plugin_renderer_base { 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 * @@ -323,7 +341,13 @@ class core_course_renderer extends plugin_renderer_base { $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'); @@ -337,7 +361,7 @@ class core_course_renderer extends plugin_renderer_base { $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; diff --git a/course/yui/build/moodle-course-modchooser/moodle-course-modchooser-debug.js b/course/yui/build/moodle-course-modchooser/moodle-course-modchooser-debug.js index f530680b8c0fbee6630aba39a8c4970deed76807..ad86243e7b5c086e900fbe2a1f3c36f8d405cc02 100644 GIT binary patch delta 16 Ycmdn0x?Od`3)ZC4l9K$5uTKdA06?n;*#H0l delta 14 Wcmdn4x>a?;3+Cd2#EoxH2?78zz6S3A diff --git a/course/yui/build/moodle-course-modchooser/moodle-course-modchooser-min.js b/course/yui/build/moodle-course-modchooser/moodle-course-modchooser-min.js index 703d338125486553174189cc27330cd470e6374a..785cf357db22d42350999e3d24bd401ed7176d7e 100644 GIT binary patch delta 16 XcmdnRyN7o|HfvI8NlE_3JRddyHpB)n delta 14 VcmdnPyNh>1Hgj=7;>H3WHUKIe1x5e> diff --git a/course/yui/build/moodle-course-modchooser/moodle-course-modchooser.js b/course/yui/build/moodle-course-modchooser/moodle-course-modchooser.js index f530680b8c0fbee6630aba39a8c4970deed76807..ad86243e7b5c086e900fbe2a1f3c36f8d405cc02 100644 GIT binary patch delta 16 Ycmdn0x?Od`3)ZC4l9K$5uTKdA06?n;*#H0l delta 14 Wcmdn4x>a?;3+Cd2#EoxH2?78zz6S3A diff --git a/course/yui/src/modchooser/js/modchooser.js b/course/yui/src/modchooser/js/modchooser.js index 6c75c604149..989adf700f3 100644 --- a/course/yui/src/modchooser/js/modchooser.js +++ b/course/yui/src/modchooser/js/modchooser.js @@ -7,7 +7,7 @@ var CSS = { PAGECONTENT: 'body', SECTION: null, - SECTIONMODCHOOSER: 'span.section-modchooser-link', + SECTIONMODCHOOSER: 'button.section-modchooser-link', SITEMENU: '.block_site_main_menu', SITETOPIC: 'div.sitetopic' }; -- 2.43.0