Commit 4f2378d9 authored by Frederic Massart's avatar Frederic Massart Committed by Dan Poltawski
Browse files

MDL-55796 core_course: Convert course modchooser to templates

Part of MDL-55071
parent ee9dd6a2
<?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;
}
}
<?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 {
/**
* 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);
}
$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);
}
}
......@@ -166,6 +166,16 @@ class core_course_renderer extends plugin_renderer_base {
return '';
}
/**
* Render a modchooser.
*
* @param renderable $modchooser The chooser.
* @return string
*/
public function render_modchooser(renderable $modchooser) {
return $this->render_from_template('core_course/modchooser', $modchooser->export_for_template($this));
}
/**
* Build the HTML for the module chooser javascript popup
*
......@@ -178,69 +188,8 @@ class core_course_renderer extends plugin_renderer_base {
if (!$this->page->requires->should_create_one_time_item_now('core_course_modchooser')) {
return '';
}
// Add the module chooser
$this->page->requires->yui_module('moodle-course-modchooser',
'M.course.init_chooser',
array(array('courseid' => $course->id, 'closeButtonTitle' => get_string('close', 'editor')))
);
$this->page->requires->strings_for_js(array(
'addresourceoractivity'
), 'moodle');
// Add the header
$header = html_writer::tag('div', get_string('addresourceoractivity', 'moodle'),
array('class' => 'hd choosertitle'));
$formcontent = html_writer::start_tag('form', array('action' => new moodle_url('/course/jumpto.php'),
'id' => 'chooserform', 'method' => 'post'));
$formcontent .= html_writer::start_tag('div', array('id' => 'typeformdiv'));
$formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'id' => 'course',
'name' => 'course', 'value' => $course->id));
$formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => 'sesskey',
'value' => sesskey()));
$formcontent .= html_writer::end_tag('div');
// Put everything into one tag 'options'
$formcontent .= html_writer::start_tag('div', array('class' => 'options'));
$formcontent .= html_writer::tag('div', get_string('selectmoduletoviewhelp', 'moodle'),
array('class' => 'instruction'));
// Put all options into one tag 'alloptions' to allow us to handle scrolling
$formcontent .= html_writer::start_tag('div', array('class' => 'alloptions'));
// Activities
$activities = array_filter($modules, create_function('$mod', 'return ($mod->archetype !== MOD_ARCHETYPE_RESOURCE && $mod->archetype !== MOD_ARCHETYPE_SYSTEM);'));
if (count($activities)) {
$formcontent .= $this->course_modchooser_title('activities');
$formcontent .= $this->course_modchooser_module_types($activities);
}
// Resources
$resources = array_filter($modules, create_function('$mod', 'return ($mod->archetype === MOD_ARCHETYPE_RESOURCE);'));
if (count($resources)) {
$formcontent .= $this->course_modchooser_title('resources');
$formcontent .= $this->course_modchooser_module_types($resources);
}
$formcontent .= html_writer::end_tag('div'); // modoptions
$formcontent .= html_writer::end_tag('div'); // types
$formcontent .= html_writer::start_tag('div', array('class' => 'submitbuttons'));
$formcontent .= html_writer::tag('input', '',
array('type' => 'submit', 'name' => 'submitbutton', 'class' => 'submitbutton', 'value' => get_string('add')));
$formcontent .= html_writer::tag('input', '',
array('type' => 'submit', 'name' => 'addcancel', 'class' => 'addcancel', 'value' => get_string('cancel')));
$formcontent .= html_writer::end_tag('div');
$formcontent .= html_writer::end_tag('form');
// Wrap the whole form in a div
$formcontent = html_writer::tag('div', $formcontent, array('id' => 'chooseform'));
// Put all of the content together
$content = $formcontent;
$content = html_writer::tag('div', $content, array('class' => 'choosercontainer'));
return $header . html_writer::tag('div', $content, array('class' => 'chooserdialoguebody'));
$modchooser = new \core_course\output\modchooser($course, $modules);
return $this->render($modchooser);
}
/**
......@@ -251,11 +200,9 @@ class core_course_renderer extends plugin_renderer_base {
* @return string The composed HTML for the module
*/
protected function course_modchooser_module_types($modules) {
$return = '';
foreach ($modules as $module) {
$return .= $this->course_modchooser_module($module);
}
return $return;
debugging('Method core_course_renderer::course_modchooser_module_types() is deprecated, ' .
'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
return '';
}
/**
......@@ -270,51 +217,15 @@ class core_course_renderer extends plugin_renderer_base {
* @return string The composed HTML for the module
*/
protected function course_modchooser_module($module, $classes = array('option')) {
$output = '';
$output .= html_writer::start_tag('div', array('class' => implode(' ', $classes)));
$output .= html_writer::start_tag('label', array('for' => 'module_' . $module->name));
if (!isset($module->types)) {
$output .= html_writer::tag('input', '', array('type' => 'radio',
'name' => 'jumplink', 'id' => 'module_' . $module->name, 'value' => $module->link));
}
$output .= html_writer::start_tag('span', array('class' => 'modicon'));
if (isset($module->icon)) {
// Add an icon if we have one
$output .= $module->icon;
}
$output .= html_writer::end_tag('span');
$output .= html_writer::tag('span', $module->title, array('class' => 'typename'));
if (!isset($module->help)) {
// Add help if found
$module->help = get_string('nohelpforactivityorresource', 'moodle');
}
// Format the help text using markdown with the following options
$options = new stdClass();
$options->trusted = false;
$options->noclean = false;
$options->smiley = false;
$options->filter = false;
$options->para = true;
$options->newlines = false;
$options->overflowdiv = false;
$module->help = format_text($module->help, FORMAT_MARKDOWN, $options);
$output .= html_writer::tag('span', $module->help, array('class' => 'typesummary'));
$output .= html_writer::end_tag('label');
$output .= html_writer::end_tag('div');
return $output;
debugging('Method core_course_renderer::course_modchooser_module() is deprecated, ' .
'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
return '';
}
protected function course_modchooser_title($title, $identifier = null) {
$module = new stdClass();
$module->name = $title;
$module->types = array();
$module->title = get_string($title, $identifier);
$module->help = '';
return $this->course_modchooser_module($module, array('moduletypetitle'));
debugging('Method core_course_renderer::course_modchooser_title() is deprecated, ' .
'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
return '';
}
/**
......@@ -2151,6 +2062,7 @@ class core_course_renderer extends plugin_renderer_base {
set_attributes(array('class' => 'frontpage-category-names'));
return $this->coursecat_tree($chelper, coursecat::get(0));
}
}
/**
......
{{!
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/>.
}}
{{!
Course module chooser.
}}
{{> core/chooser }}
{{#js}}
require([
'core/yui',
'core/str'
], function(Y, Str) {
Str.get_strings([
{ key: 'addresourceoractivity', component: 'moodle' },
{ key: 'close', component: 'editor' },
]).then(function(add, close) {
Y.use('moodle-course-modchooser', function() {
M.course.init_chooser({
courseid: {{courseid}},
closeButtonTitle: close
});
});
});
});
{{/js}}
......@@ -7,4 +7,9 @@ information provided here is intended especially for developers.
* External functions that were returning file information now return the following file fields:
filename, filepath, mimetype, filesize, timemodified and fileurl.
Those fields are now marked as VALUE_OPTIONAL for backwards compatibility.
* The modchooser now uses a renderable and a template, thus its associated renderer
methods have been deprecated. Note that the call to core_course_render::course_modchooser,
is still required. Deprecated methods:
- core_course_renderer::course_modchooser_module_types
- core_course_renderer::course_modchooser_module
- core_course_renderer::course_modchooser_title
<?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 chooser renderable.
*
* @package core
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\output;
defined('MOODLE_INTERNAL') || die();
use lang_string;
use moodle_url;
use renderer_base;
use renderable;
use stdClass;
use templatable;
/**
* The chooser renderable class.
*
* @package core
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class chooser implements renderable, templatable {
/** @var moodle_url The form action URL. */
public $actionurl;
/** @var lang_string The instructions to display. */
public $instructions;
/** @var string The name of the parameter for the items value. */
public $paramname;
/** @var array The list of hidden parameters. See {@link self::add_param}. */
public $params = [];
/** @var chooser_section[] The sections */
public $sections;
/** @var lang_string The chooser title. */
public $title;
/**
* Constructor.
*
* @param moodle_url $actionurl The form action URL.
* @param lang_string $title The title of the chooser.
* @param chooser_section[] $sections The sections.
* @param string $paramname The name of the parameter for the items value.
*/
public function __construct(moodle_url $actionurl, lang_string $title, array $sections, $paramname) {
$this->actionurl = $actionurl;
$this->title = $title;
$this->sections = $sections;
$this->paramname = $paramname;
}
/**
* Add a parameter to submit with the form.
*
* @param string $name The parameter name.
* @param string $value The parameter value.
* @param string $id The parameter ID.
*/
public function add_param($name, $value, $id = null) {
if (!$id) {
$id = $name;
}
$this->params[] = [
'name' => $name,
'value' => $value,
'id' => $id
];
}
/**
* Set the chooser instructions.
*
* @param lang_string $value The instructions.
*/
public function set_instructions(lang_string $value) {
$this->instructions = $value;
}
/**
* Export for template.
*
* @param renderer_base The renderer.
* @return stdClass
*/
public function export_for_template(renderer_base $output) {
$data = new stdClass();
$data->actionurl = $this->actionurl->out(false);
$data->instructions = (string) $this->instructions;
$data->paramname = $this->paramname;
$data->params = $this->params;
$data->sesskey = sesskey();
$data->title = (string) $this->title;
$data->sections = array_map(function($section) use ($output) {
return $section->export_for_template($output);
}, $this->sections);
return $data;
}
}
<?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 chooser_item renderable.
*
* @package core
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\output;
defined('MOODLE_INTERNAL') || die();
use coding_exception;
use context;
use pix_icon;
use renderer_base;
use renderable;
use stdClass;
use templatable;
/**
* The chooser_item renderable class.
*
* @package core
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class chooser_item implements renderable, templatable {
/** @var string An identifier for the item. */
public $id;
/** @var string The label of this item. */
public $label;
/** @var string The value this item represents. */
public $value;
/** @var pix_icon The icon for this item. */
public $icon;
/** @var string The item description. */
public $description;
/** @var context The relevant context. */
public $context;
/**
* Constructor.
*/
public function __construct($id, $label, $value, pix_icon $icon, $description = null, context $context = null) {
$this->id = $id;
$this->label = $label;
$this->value = $value;
$this->icon = $icon;
$this->description = $description;
if (!empty($description) && empty($context)) {
throw new coding_exception('The context must be passed when there is a description.');
}
$this->context = $context;
}
/**
* Export for template.
*
* @param renderer_base The renderer.
* @return stdClass
*/
public function export_for_template(renderer_base $output) {
$data = new stdClass();
$data->id = $this->id;
$data->label = $this->label;
$data->value = $this->value;
$data->icon = $this->icon->export_for_template($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;
$data->description = '';
if (!empty($this->description)) {
list($data->description) = external_format_text((string) $this->description, FORMAT_MARKDOWN,
$this->context->id, null, null, null, $options);
}
return $data;
}
}
<?php
// This file is part of Moodle - http://moodle.org/