Clonado de grid

This commit is contained in:
Diego Hernandez 2021-10-16 22:08:02 +02:00
commit 6b2b1db926
13 changed files with 1383 additions and 0 deletions

View File

@ -0,0 +1,69 @@
<?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/>.
/**
* unilabel type menu
*
* @package unilabeltype_menu
* @author Andreas Grabs <info@grabs-edv.de>
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Backup definition for this content type
* @package unilabeltype_menu
* @author Andreas Grabs <info@grabs-edv.de>
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_unilabeltype_menu_subplugin extends backup_subplugin {
/**
* Returns the assessment form definition to attach to 'unilabel' XML element
* @return \backup_subplugin_element
*/
protected function define_unilabel_subplugin_structure() {
// XML nodes declaration.
$subplugin = $this->get_subplugin_element();
$subpluginwrapper = new backup_nested_element($this->get_recommended_name());
$subpluginmenu = new backup_nested_element('unilabeltype_menu', array('id'), array(
'columns', 'height', 'showintro', 'usemobile'));
$subplugintile = new backup_nested_element('unilabeltype_menu_tile',
array('id'),
array('title', 'url', 'content')
);
// Connect XML elements into the tree.
$subplugin->add_child($subpluginwrapper);
$subpluginwrapper->add_child($subpluginmenu);
$subpluginmenu->add_child($subplugintile);
// Set source to populate the data.
$subpluginmenu->set_source_table('unilabeltype_menu', array('unilabelid' => backup::VAR_ACTIVITYID));
$subplugintile->set_source_table('unilabeltype_menu_tile', array('menuid' => backup::VAR_PARENTID));
// File annotations.
$subplugintile->annotate_files('unilabeltype_menu', 'image', 'id');
$subplugintile->annotate_files('unilabeltype_menu', 'image_mobile', 'id');
$subplugintile->annotate_files('unilabeltype_menu', 'content', 'id');
return $subplugin;
}
}

View File

@ -0,0 +1,92 @@
<?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/>.
/**
* unilabel type menu
*
* @package unilabeltype_menu
* @author Andreas Grabs <info@grabs-edv.de>
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Restore definition of this content type
* @package unilabeltype_menu
* @author Andreas Grabs <info@grabs-edv.de>
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restore_unilabeltype_menu_subplugin extends restore_subplugin {
/**
* Returns the paths to be handled by the subplugin at unilabel level
* @return array
*/
protected function define_unilabel_subplugin_structure() {
$paths = array();
$elename = $this->get_namefor();
$elepath = $this->get_pathfor('/unilabeltype_menu');
$paths[] = new restore_path_element($elename, $elepath);
$elename = $this->get_namefor('tile');
$elepath = $this->get_pathfor('/unilabeltype_menu/unilabeltype_menu_tile');
$paths[] = new restore_path_element($elename, $elepath);
return $paths; // And we return the interesting paths.
}
/**
* Processes the element
* @param array $data
* @return void
*/
public function process_unilabeltype_menu($data) {
global $DB;
$data = (object)$data;
$oldid = $data->id;
$data->unilabelid = $this->get_new_parentid('unilabel');
$newitemid = $DB->insert_record('unilabeltype_menu', $data);
$this->set_mapping($this->get_namefor(), $oldid, $newitemid, true);
}
/**
* Processes the unilabeltype_menu_tile element
* @param array $data
*/
public function process_unilabeltype_menu_tile($data) {
global $DB;
$data = (object)$data;
$oldid = $data->id;
$data->menuid = $this->get_new_parentid($this->get_namefor());
$newitemid = $DB->insert_record('unilabeltype_menu_tile', $data);
$this->set_mapping($this->get_namefor('tile'), $oldid, $newitemid, true);
// Process files.
$this->add_related_files('unilabeltype_menu', 'image', 'unilabeltype_menu_tile');
$this->add_related_files('unilabeltype_menu', 'image_mobile', 'unilabeltype_menu_tile');
$this->add_related_files('unilabeltype_menu', 'content', 'unilabeltype_menu_tile');
}
}

642
classes/content_type.php Normal file
View File

@ -0,0 +1,642 @@
<?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/>.
/**
* unilabel type menu
*
* @package unilabeltype_menu
* @author Andreas Grabs <info@grabs-edv.de>
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace unilabeltype_menu;
defined('MOODLE_INTERNAL') || die;
/**
* Content type definition
* @package unilabeltype_menu
* @author Andreas Grabs <info@grabs-edv.de>
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class content_type extends \mod_unilabel\content_type {
/** @var \stdClass $unilabeltyperecord */
private $unilabeltyperecord;
/** @var array $tiles */
private $tiles;
/** @var \stdClass $cm */
private $cm;
/** @var \context $context */
private $context;
/** @var \stdClass $config */
private $config;
/**
* Constructor
*
* @return void
*/
public function __construct() {
$this->config = get_config('unilabeltype_menu');
}
/**
* Add elements to the activity settings form.
*
* @param \mod_unilabel\edit_content_form $form
* @param \context $context
* @return void
*/
public function add_form_fragment(\mod_unilabel\edit_content_form $form, \context $context) {
$unilabeltyperecord = $this->load_unilabeltype_record($form->unilabel->id);
$mform = $form->get_mform();
$prefix = 'unilabeltype_menu_';
$mform->addElement('advcheckbox', $prefix.'showintro', get_string('showunilabeltext', 'unilabeltype_menu'));
$mform->addElement('header', $prefix.'hdr', $this->get_name());
$mform->addHelpButton($prefix.'hdr', 'pluginname', 'unilabeltype_menu');
$numbers = array_combine(range(1, 6), range(1, 6));
$mform->addElement('select', $prefix.'columns', get_string('columns', 'unilabeltype_menu'), $numbers);
// In all smaller displays we can not use 5 columns. It is not supported by bootstrap and css injection will not work here.
$numbers = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 6 => 6);
$strdefaultcol = get_string('default_columns', 'unilabeltype_menu');
$columnsmiddle = $mform->createElement('select', $prefix.'columnsmiddle', '', $numbers);
$defaultmiddle = $mform->createElement('advcheckbox', $prefix.'defaultmiddle', $strdefaultcol);
$mform->addGroup(
array(
$columnsmiddle,
$defaultmiddle,
),
$prefix.'group_middle',
get_string('columnsmiddle', 'unilabeltype_menu'),
array(' '),
false
);
$mform->disabledIf($prefix.'columnsmiddle', $prefix.'defaultmiddle', 'checked');
$columnssmall = $mform->createElement('select', $prefix.'columnssmall', '', $numbers);
$defaultsmall = $mform->createElement('advcheckbox', $prefix.'defaultsmall', $strdefaultcol);
$mform->addGroup(
array(
$columnssmall,
$defaultsmall,
),
$prefix.'group_small',
get_string('columnssmall', 'unilabeltype_menu'),
array(' '),
false
);
$mform->disabledIf($prefix.'columnssmall', $prefix.'defaultsmall', 'checked');
$numbers = array_combine(range(100, 600, 50), range(100, 600, 50));
$numbers = [0 => get_string('autoheight', 'unilabeltype_menu')] + $numbers;
$mform->addElement('select', $prefix.'height', get_string('height', 'unilabeltype_menu'), $numbers);
$mform->addHelpButton($prefix.'height', 'height', 'unilabeltype_menu');
$mform->addElement('advcheckbox', $prefix.'usemobile', get_string('use_mobile_images', 'unilabeltype_menu'));
$mform->addHelpButton($prefix.'usemobile', 'use_mobile_images', 'unilabeltype_menu');
$repeatarray = [];
// If we want each repeated elment in a numbered group we add a header with '{no}' in its label.
// This is replaced by the number of element.
$repeatarray[] = $mform->createElement('header', $prefix.'tilehdr', get_string('tile', 'unilabeltype_menu').'-{no}');
$repeatarray[] = $mform->createElement(
'text',
$prefix.'title',
get_string('title', 'unilabeltype_menu').'-{no}',
['size' => 50]
);
$repeatarray[] = $mform->createElement(
'editor',
$prefix.'content',
get_string('content', 'unilabeltype_menu').'-{no}',
['rows' => 10],
$this->editor_options($form->context)
);
$repeatarray[] = $mform->createElement(
'text',
$prefix.'url',
get_string('url', 'unilabeltype_menu').'-{no}',
['size' => 50]
);
$repeatarray[] = $mform->createElement(
'filemanager',
$prefix.'image',
get_string('image', 'unilabeltype_menu').'-{no}',
null,
[
'maxbytes' => $form->get_course()->maxbytes,
'maxfiles' => 1,
'subdirs' => false,
'accepted_types' => ['web_image'],
]
);
$repeatarray[] = $mform->createElement(
'filemanager',
$prefix.'image_mobile',
get_string('image_mobile', 'unilabeltype_menu').'-{no}',
null,
[
'maxbytes' => $form->get_course()->maxbytes,
'maxfiles' => 1,
'subdirs' => false,
'accepted_types' => ['web_image'],
]
);
$repeatedoptions = [];
$repeatedoptions[$prefix.'title']['type'] = PARAM_TEXT;
$repeatedoptions[$prefix.'url']['type'] = PARAM_URL;
$repeatedoptions[$prefix.'content']['type'] = PARAM_RAW;
$repeatedoptions[$prefix.'image']['type'] = PARAM_FILE;
$repeatedoptions[$prefix.'image_mobile']['type'] = PARAM_FILE;
// Adding the help buttons.
$repeatedoptions[$prefix.'content']['helpbutton'] = ['content', 'unilabeltype_menu'];
$repeatedoptions[$prefix.'url']['helpbutton'] = ['url', 'unilabeltype_menu'];
$repeatedoptions[$prefix.'image_mobile']['helpbutton'] = ['image_mobile', 'unilabeltype_menu'];
$defaultrepeatcount = 4; // The default count for tiles.
$repeatcount = count($this->tiles);
if ($rest = count($this->tiles) % $defaultrepeatcount) {
$repeatcount = count($this->tiles) + ($defaultrepeatcount - $rest);
}
if ($repeatcount == 0) {
$repeatcount = $defaultrepeatcount;
}
$nextel = $form->repeat_elements(
$repeatarray,
$repeatcount,
$repeatedoptions,
$prefix.'chosen_tiles_count',
$prefix.'add_more_tiles_btn',
$defaultrepeatcount, // Each time we add 3 elements.
get_string('addmoretiles', 'unilabeltype_menu'),
false
);
}
/**
* Get the default values for the settings form
*
* @param array $data
* @param \stdClass $unilabel
* @return array
*/
public function get_form_default($data, $unilabel) {
global $DB;
$cm = get_coursemodule_from_instance('unilabel', $unilabel->id);
$context = \context_module::instance($cm->id);
$prefix = 'unilabeltype_menu_';
// Set default data for the menu in generel.
if (!$unilabeltyperecord = $this->load_unilabeltype_record($unilabel->id)) {
$data[$prefix.'columns'] = $this->config->columns;
$data[$prefix.'columnsmiddle'] = $this->get_default_col_middle($this->config->columns);
$data[$prefix.'defaultmiddle'] = true;
$data[$prefix.'columnssmall'] = $this->get_default_col_small();
$data[$prefix.'defaultsmall'] = true;
$data[$prefix.'height'] = $this->config->height;
$data[$prefix.'showintro'] = !empty($this->config->showintro);
$data[$prefix.'usemobile'] = !empty($this->config->usemobile);
return $data;
}
$data[$prefix.'columns'] = $unilabeltyperecord->columns;
if (empty($unilabeltyperecord->columnsmiddle)) {
$data[$prefix.'columnsmiddle'] = $this->get_default_col_middle($unilabeltyperecord->columns);
$data[$prefix.'defaultmiddle'] = true;
} else {
$data[$prefix.'columnsmiddle'] = $unilabeltyperecord->columnsmiddle;
$data[$prefix.'defaultmiddle'] = false;
}
if (empty($unilabeltyperecord->columnssmall)) {
$data[$prefix.'columnssmall'] = $this->get_default_col_small();
$data[$prefix.'defaultsmall'] = true;
} else {
$data[$prefix.'columnssmall'] = $unilabeltyperecord->columnssmall;
$data[$prefix.'defaultsmall'] = false;
}
$data[$prefix.'height'] = $unilabeltyperecord->height;
$data[$prefix.'showintro'] = $unilabeltyperecord->showintro;
$data[$prefix.'usemobile'] = $unilabeltyperecord->usemobile;
// Set default data for tiles.
if (!$tiles = $DB->get_records(
'unilabeltype_menu_tile',
['menuid' => $unilabeltyperecord->id],
'id ASC'
)) {
return $data;
}
$index = 0;
foreach ($tiles as $tile) {
// Prepare the title field.
$elementname = $prefix.'title['.$index.']';
$data[$elementname] = $tile->title;
// Prepare the url field.
$elementname = $prefix.'url['.$index.']';
$data[$elementname] = $tile->url;
// Prepare the content field.
$elementname = $prefix.'content['.$index.']';
$draftitemidcontent = 0;
$data[$elementname]['text'] =
file_prepare_draft_area($draftitemidcontent,
$context->id,
'unilabeltype_menu',
'content',
$tile->id,
array('subdirs' => true),
$tile->content);
$data[$elementname]['format'] = FORMAT_HTML;
$data[$elementname]['itemid'] = $draftitemidcontent;
// Prepare the images.
// $draftitemid is set by the function file_prepare_draft_area().
$draftitemidimage = 0; // This is needed to create a new draftitemid.
file_prepare_draft_area($draftitemidimage, $context->id, 'unilabeltype_menu', 'image', $tile->id);
$elementname = $prefix.'image['.$index.']';
$data[$elementname] = $draftitemidimage;
// Prepare the mobile images.
// $draftitemid is set by the function file_prepare_draft_area().
$draftitemidimagemobile = 0; // This is needed to create a new draftitemid.
file_prepare_draft_area($draftitemidimagemobile, $context->id, 'unilabeltype_menu', 'image_mobile', $tile->id);
$elementname = $prefix.'image_mobile['.$index.']';
$data[$elementname] = $draftitemidimagemobile;
$index++;
}
return $data;
}
/**
* Get the namespace of this content type
*
* @return string
*/
public function get_namespace() {
return __NAMESPACE__;
}
/**
* Get the html formated content for this type.
*
* @param \stdClass $unilabel
* @param \stdClass $cm
* @param \plugin_renderer_base $renderer
* @return string
*/
public function get_content($unilabel, $cm, \plugin_renderer_base $renderer) {
if (!$unilabeltyperecord = $this->load_unilabeltype_record($unilabel->id)) {
$content = [
'intro' => get_string('nocontent', 'unilabeltype_menu'),
'cmid' => $cm->id,
'hastiles' => false,
];
} else {
$intro = $this->format_intro($unilabel, $cm);
$showintro = !empty($unilabeltyperecord->showintro);
$content = [
'showintro' => $showintro,
'intro' => $showintro ? $intro : '',
'columnssmall' => 1,
'height' => $unilabeltyperecord->height,
'autoheight' => empty($unilabeltyperecord->height),
'tiles' => array_values($this->tiles),
'hastiles' => count($this->tiles) > 0,
'cmid' => $cm->id,
];
$content['colclasses'] = $this->get_bootstrap_cols(
$unilabeltyperecord->columns,
$unilabeltyperecord->columnsmiddle,
$unilabeltyperecord->columnssmall
);
}
$content = $renderer->render_from_template('unilabeltype_menu/menu', $content);
return $content;
}
/**
* Delete the content of this type
*
* @param int $unilabelid
* @return void
*/
public function delete_content($unilabelid) {
global $DB;
$unilabeltyperecord = $this->load_unilabeltype_record($unilabelid);
// Delete all tiles.
if (!empty($unilabeltyperecord)) {
$DB->delete_records('unilabeltype_menu_tile', ['menuid' => $unilabeltyperecord->id]);
}
$DB->delete_records('unilabeltype_menu', ['unilabelid' => $unilabelid]);
}
/**
* Save the content from settings page
*
* @param \stdClass $formdata
* @param \stdClass $unilabel
* @return bool
*/
public function save_content($formdata, $unilabel) {
global $DB, $USER;
// We want to keep the tiles consistent so we start a transaction here.
$transaction = $DB->start_delegated_transaction();
$prefix = 'unilabeltype_menu_';
// First save the menu record.
if (!$unilabeltyperecord = $DB->get_record('unilabeltype_menu', ['unilabelid' => $unilabel->id])) {
$unilabeltyperecord = new \stdClass();
$unilabeltyperecord->unilabelid = $unilabel->id;
$unilabeltyperecord->id = $DB->insert_record('unilabeltype_menu', $unilabeltyperecord);
}
$columns = !empty($formdata->{$prefix.'columns'}) ? $formdata->{$prefix.'columns'} : 0;
$unilabeltyperecord->columns = $columns;
$columnsmiddle = !empty($formdata->{$prefix.'defaultmiddle'}) ? null : $formdata->{$prefix.'columnsmiddle'};
$unilabeltyperecord->columnsmiddle = $columnsmiddle;
$columnssmall = !empty($formdata->{$prefix.'defaultsmall'}) ? null : $formdata->{$prefix.'columnssmall'};
$unilabeltyperecord->columnssmall = $columnssmall;
$unilabeltyperecord->height = $formdata->{$prefix.'height'};
$unilabeltyperecord->showintro = $formdata->{$prefix.'showintro'};
$unilabeltyperecord->usemobile = !empty($formdata->{$prefix.'usemobile'});
$DB->update_record('unilabeltype_menu', $unilabeltyperecord);
$fs = get_file_storage();
$context = \context_module::instance($formdata->cmid);
$usercontext = \context_user::instance($USER->id);
// First: remove old tile images.
// We use the module_context as context and this component as component.
$fs->delete_area_files($context->id, 'unilabeltype_menu', 'image');
$fs->delete_area_files($context->id, 'unilabeltype_menu', 'image_mobile');
$fs->delete_area_files($context->id, 'unilabeltype_menu', 'content');
// Second: remove old tile records.
$DB->delete_records('unilabeltype_menu_tile', ['menuid' => $unilabeltyperecord->id]);
// How many tiles could be defined (we have an array here)?
// They may not all used so some could be left out.
$potentialtilecount = $formdata->{$prefix.'chosen_tiles_count'};
for ($i = 0; $i < $potentialtilecount; $i++) {
// Get the draftitemids to identify the submitted files in image, imagemobile and content.
$draftitemid = $formdata->{$prefix.'image'}[$i];
if (!empty($unilabeltyperecord->usemobile)) {
$draftitemidmobile = $formdata->{$prefix.'image_mobile'}[$i];
}
$draftitemidcontent = $formdata->{$prefix.'content'}[$i]['itemid'];
// Do we have an image? We get this information with file_get_draft_area_info().
$fileinfo = file_get_draft_area_info($draftitemid);
// We only create a record if we have at least a title, a file or a content.
$title = $formdata->{$prefix.'title'}[$i];
$content = $formdata->{$prefix.'content'}[$i]['text'];
if (empty($title) and $fileinfo['filecount'] < 1 and !$this->html_has_content($content)) {
continue;
}
$tilerecord = new \stdClass();
$tilerecord->menuid = $unilabeltyperecord->id;
$tilerecord->title = $title;
$tilerecord->url = $formdata->{$prefix.'url'}[$i];
$tilerecord->content = ''; // Dummy content.
$tilerecord->id = $DB->insert_record('unilabeltype_menu_tile', $tilerecord);
// Save draft files from content and convert the pluginfile links.
$tilerecord->content = file_save_draft_area_files($draftitemidcontent,
$context->id,
'unilabeltype_menu',
'content',
$tilerecord->id,
$this->editor_options($context),
$content);
$DB->update_record('unilabeltype_menu_tile', $tilerecord);
// Now we can save our draft files for image and imagemobile.
file_save_draft_area_files($draftitemid, $context->id, 'unilabeltype_menu', 'image', $tilerecord->id);
if (!empty($formdata->{$prefix.'usemobile'})) {
file_save_draft_area_files(
$draftitemidmobile,
$context->id,
'unilabeltype_menu',
'image_mobile',
$tilerecord->id
);
}
}
$transaction->allow_commit();
return !empty($unilabeltyperecord->id);
}
/**
* Load and cache the unilabel record
*
* @param int $unilabelid
* @return \stdClass
*/
public function load_unilabeltype_record($unilabelid) {
global $DB;
if (empty($this->unilabeltyperecord)) {
if (!$this->unilabeltyperecord = $DB->get_record('unilabeltype_menu', ['unilabelid' => $unilabelid])) {
$this->tiles = [];
return;
}
$this->cm = get_coursemodule_from_instance('unilabel', $unilabelid);
$this->context = \context_module::instance($this->cm->id);
$tiles = $DB->get_records('unilabeltype_menu_tile', ['menuid' => $this->unilabeltyperecord->id]);
$index = 0;
foreach ($tiles as $tile) {
$tile->imageurl = $this->get_image_for_tile($tile);
$tile->imagemobileurl = $this->get_image_mobile_for_tile($tile);
$tile->title = empty($tile->title) ? get_string('tilenr', 'unilabeltype_menu', $index + 1) : $tile->title;
$tile->content = $this->format_content($tile, $this->context);
$tile->nr = $index;
$index++;
}
$this->tiles = $tiles;
}
return $this->unilabeltyperecord;
}
/**
* Get the image url for the given tile
*
* @param \stdClass $tile
* @return string
*/
private function get_image_for_tile($tile) {
$fs = get_file_storage();
$files = $fs->get_area_files($this->context->id, 'unilabeltype_menu', 'image', $tile->id, '', $includedirs = false);
if (!$file = array_shift($files)) {
return '';
}
$imageurl = \moodle_url::make_pluginfile_url(
$this->context->id,
'unilabeltype_menu',
'image',
$tile->id,
'/',
$file->get_filename()
);
return $imageurl;
}
/**
* Get the mobile image url
*
* @param \stdClass $tile
* @return string
*/
private function get_image_mobile_for_tile($tile) {
$fs = get_file_storage();
$files = $fs->get_area_files(
$this->context->id,
'unilabeltype_menu',
'image_mobile',
$tile->id,
'',
$includedirs = false
);
if (!$file = array_shift($files)) {
return '';
}
$imageurl = \moodle_url::make_pluginfile_url(
$this->context->id,
'unilabeltype_menu',
'image_mobile',
$tile->id,
'/',
$file->get_filename()
);
return $imageurl;
}
/**
* Check whether ther is content or not.
*
* @param string $content
* @return bool
*/
private function html_has_content($content) {
$searches = [
'<br>',
'<br />',
'<p>',
'</p>'
];
$check = trim(str_replace($searches, '', $content));
return !empty($check);
}
/**
* Get the options array to support files in editor.
*
* @param \context $context
* @return array
*/
public function editor_options($context) {
return [
'maxfiles' => EDITOR_UNLIMITED_FILES,
'noclean' => true,
'context' => $context,
'subdirs' => true
];
}
/**
* Get the format options array
*
* @param \context $context
* @return array
*/
public function format_options($context) {
return [
'noclean' => true,
'context' => $context
];
}
/**
* Format the content of a tile
*
* @param \stdClass $tile
* @param \context $context
* @return string
*/
public function format_content($tile, $context) {
global $CFG;
require_once($CFG->libdir.'/filelib.php');
$options = $this->format_options($context);
$content = file_rewrite_pluginfile_urls(
$tile->content,
'pluginfile.php',
$context->id,
'unilabeltype_menu',
'content',
$tile->id
);
return trim(format_text($content, FORMAT_HTML, $options, null));
}
/**
* Check that this plugin is activated on config settings.
*
* @return boolean
*/
public function is_active() {
return !empty($this->config->active);
}
}

View File

@ -0,0 +1,47 @@
<?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/>.
/**
* unilabel type menu
*
* @package unilabeltype_menu
* @author Andreas Grabs <info@grabs-edv.de>
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace unilabeltype_menu\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy api class
* @package unilabeltype_menu
* @author Andreas Grabs <info@grabs-edv.de>
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason() : string {
return 'privacy:metadata';
}
}

35
db/install.xml Normal file
View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/unilabel/type/menu/db" VERSION="20180729" COMMENT="XMLDB file for Moodle mod/unilabel/type/menu"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="unilabeltype_menu" COMMENT="Default comment for unilabeltype_menu, please edit me">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="unilabelid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="columns" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="columnsmiddle" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="columnssmall" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="height" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="showintro" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="usemobile" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="unilabeltype_menu_tile" COMMENT="Default comment for the table, please edit me">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="menuid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="title" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="url" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="content" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>

64
db/upgrade.php Normal file
View File

@ -0,0 +1,64 @@
<?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/>.
/**
* unilabel module
*
* @package mod_unilabel
* @author Andreas Grabs <info@grabs-edv.de>
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Upgrade hook for this plugin
*
* @param int $oldversion
* @return bool
*/
function xmldb_unilabeltype_menu_upgrade($oldversion) {
global $CFG, $DB;
$dbman = $DB->get_manager();
if ($oldversion < 2020022900) {
// Define field columnsmiddle to be added to unilabeltype_menu.
$table = new xmldb_table('unilabeltype_menu');
$field = new xmldb_field('columnsmiddle', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'columns');
// Conditionally launch add field columnsmiddle.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Define field columnssmall to be added to unilabeltype_menu.
$table = new xmldb_table('unilabeltype_menu');
$field = new xmldb_field('columnssmall', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'columnsmiddle');
// Conditionally launch add field columnssmall.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Grid savepoint reached.
upgrade_plugin_savepoint(true, 2020022900, 'unilabeltype', 'menu');
}
return true;
}

View File

@ -0,0 +1,57 @@
<?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/>.
/**
* unilabel type menu
*
* @package unilabeltype_menu
* @author Andreas Grabs <info@grabs-edv.de>
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
$string['addmoretiles'] = 'Add more tiles';
$string['autoheight'] = 'Auto height';
$string['background'] = 'Background';
$string['columns'] = 'Columns';
$string['columnsmiddle'] = 'Columns middle';
$string['columnssmall'] = 'Columns small';
$string['content'] = 'Content';
$string['content_help'] = 'The content text will be displayed as a modal dialog only if url is not set.';
$string['default_columns'] = 'Default columns';
$string['default_height'] = 'Default height';
$string['default_showintro'] = 'Default show unilabel text';
$string['default_usemobile'] = 'Default use mobile images';
$string['height'] = 'Height';
$string['height_help'] = 'If your pictures are of a different size, use the fixed height option. This scales all images to the same height. If your pictures have the same height, you can also use "auto height".';
$string['image'] = 'Image';
$string['image_mobile'] = 'Image mobile';
$string['image_mobile_help'] = 'Mobile images will be shown on screens smaller than 768px. If you do not define a mobile image the general image is shown on all screens.';
$string['nocontent'] = 'No content';
$string['pluginname'] = 'Menu';
$string['pluginname_help'] = 'This plugin type creates a menu consisting of pictures. A click on the image opens a modal dialog field or brings the user to a defined url.';
$string['privacy:metadata'] = 'The unilabel type Grid does not store any personal data.';
$string['showunilabeltext'] = 'Show unilabel text';
$string['tile'] = 'Tile';
$string['tilenr'] = 'Tile {$a}';
$string['tiles'] = 'Tiles';
$string['title'] = 'Title';
$string['url'] = 'Url';
$string['url_help'] = 'By defining a url the whole slide becomes a clickable button linked to the defined url.';
$string['use_mobile_images'] = 'Use mobile images';
$string['use_mobile_images_help'] = 'If you activate the usage of mobile images you can upload a mobile image in addition to standard one for every slide. The mobile image will be displayed on screens smaller than 768px.';

63
lib.php Normal file
View File

@ -0,0 +1,63 @@
<?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/>.
/**
* unilabel type menu
*
* @package unilabeltype_menu
* @author Andreas Grabs <info@grabs-edv.de>
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Send files provided by this plugin
*
* @param \stdClass $course
* @param \stdClass $cm
* @param \context $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @return bool
*/
function unilabeltype_menu_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}
require_course_login($course, true, $cm);
if (!has_capability('mod/unilabel:view', $context)) {
return false;
}
if (($filearea !== 'image') AND ($filearea !== 'image_mobile') AND ($filearea !== 'content')) {
return false;
}
$relativepath = implode('/', $args);
$fullpath = '/'.$context->id.'/unilabeltype_menu/'.$filearea.'/'.$relativepath;
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
send_stored_file($file, 0, 0, true); // Download MUST be forced - security!
}

70
settings.php Normal file
View File

@ -0,0 +1,70 @@
<?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/>.
/**
* unilabel type menu
*
* @package unilabeltype_menu
* @author Andreas Grabs <info@grabs-edv.de>
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
$page = new admin_settingpage('unilabeltype_menu', get_string('pluginname', 'unilabeltype_menu'));
$menusettings = array();
$menusettings[] = new admin_setting_configcheckbox('unilabeltype_menu/active',
get_string('active'),
'',
true);
$numbers = array_combine(range(1, 6), range(1, 6));
$menusettings[] = new admin_setting_configselect('unilabeltype_menu/columns',
get_string('default_columns', 'unilabeltype_menu'),
'',
4,
$numbers
);
$numbers = array_combine(range(100, 600, 50), range(100, 600, 50));
$numbers = array(0 => get_string('autoheight', 'unilabeltype_menu')) + $numbers;
$menusettings[] = new admin_setting_configselect('unilabeltype_menu/height',
get_string('default_height', 'unilabeltype_menu'),
get_string('height_help', 'unilabeltype_menu'),
300,
$numbers
);
$menusettings[] = new admin_setting_configcheckbox('unilabeltype_menu/showintro',
get_string('default_showintro', 'unilabeltype_menu'),
'',
false
);
$menusettings[] = new admin_setting_configcheckbox('unilabeltype_menu/usemobile',
get_string('default_usemobile', 'unilabeltype_menu'),
'',
true
);
foreach ($menusettings as $setting) {
$page->add($setting);
}
$settingscategory->add($page);

View File

@ -0,0 +1,50 @@
{{!
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/>.
}}
{{!
@template unilabeltype_menu/colourpicker
Template to show a colour picker in the content settings form to choose the background colour.
Example context (json):
{
"iconurl": "https://<url-to-moodle>/theme/image.php/svg_bootstrap4/core/1544826417/i/colourpicker",
"inputname": "unilabeltype_menu_background",
"inputid": "id_unilabeltype_menu_background_colourpicker",
"label": "Background",
"defaultvalue": "#A0E2FD"
}
}}
<div class="form-group row fitem ">
<div class="col-md-3">
<span class="float-sm-right text-nowrap"> </span>
<label class="col-form-label d-inline " for="{{inputid}}">
{{label}}
</label>
</div>
<div class="col-md-9 form-inline felement" data-fieldtype="text">
<div class="form-colourpicker defaultsnext">
<div class="admin_colourpicker clearfix">
<img alt="" class="colourdialogue" src="{{{iconurl}}}">
<div class="previewcolour" style="width: 50px; height: 50px; background-color: rgb(137, 27, 0);"></div>
<div class="currentcolour" style="width: 50px; height: 49px; background-color: rgb(58, 69, 75);"></div>
</div>
<input name="{{inputname}}" id="{{inputid}}" value="{{{defaultvalue}}}" size="12" class="form-control text-ltr" type="text">
<span>{{defaultvalue}}</span>
</div>
</div>
</div>

62
templates/dialog.mustache Normal file
View File

@ -0,0 +1,62 @@
{{!
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/>.
}}
{{!
@template unilabeltype_menu/dialog
Template to show a bootstrap modal dialog.
Example context (json):
{
"title": "Title",
"name": "Topic 1",
"section": "1",
"summary": "",
"cmlist": "<ul class=\"section img-text\"><\/ul>",
"nr": 0,
"first": true
}
}}
<!-- The Modal -->
<div class="modal fade" tabindex="-1" id="menu-modal-{{cmid}}-{{nr}}">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">{{title}}</h4>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<!-- Modal body -->
<div class="modal-body">
{{{content}}}
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">{{#str}} closebuttontitle {{/str}}</button>
</div>
</div>
</div>
</div>
{{#js}}
require(['jquery'], function($) {
$('#menu-modal-{{cmid}}-{{nr}}').on('show.bs.modal', function() {
$('#menu-modal-{{cmid}}-{{nr}}').appendTo('body');
});
});
{{/js}}

102
templates/menu.mustache Normal file
View File

@ -0,0 +1,102 @@
{{!
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/>.
}}
{{!
@template unilabeltype_menu/menu
Template to show a bootstrap modal dialog.
Example context (json):
{
"showintro": false,
"intro": "",
"columnssmall": 1,
"height": "300",
"autoheight": false,
"tiles": [
{
"id": "1",
"menuid": "1",
"title": "Title 1",
"url": "",
"content": "<p>fdsafsdf<br><\/p>",
"imageurl": "https://url-to-image",
"imagemobileurl": "",
"nr": 0
},
{
"id": "2",
"menuid": "1",
"title": "Title 2",
"url": "",
"content": "<p>fdfdfdfdf<br><\/p>",
"imageurl": "https://url-to-image",
"imagemobileurl": "",
"nr": 1
}
],
"hastiles": true,
"cmid": "55",
"colclasses": "col-lg-3 col-md-6 col-sm-12"
}
}}
{{#showintro}}
{{{intro}}}
{{/showintro}}
{{#hastiles}}
<div class="row mx-0">
{{#tiles}}
<div class="text-center {{{colclasses}}} py-4">
<!-- The overlay is used as button to show the modal dialog -->
{{#url}}
<a href="{{{url}}}">
<div class="unilabel-overlay-effect bg-primary h-100 w-100"></div>
</a>
{{/url}}
{{^url}}
<a href="#" data-toggle="modal" data-target="#menu-modal-{{cmid}}-{{nr}}">
<div class="unilabel-overlay-effect bg-primary h-100 w-100"></div>
</a>
{{/url}}
{{^autoheight}}<div style="height:{{{height}}}px;width:100%;">{{/autoheight}}
{{#imagemobileurl}}
{{#imageurl}}
<img class="img-fluid d-none d-md-inline" alt="{{title}}" src="{{{imageurl}}}"{{^autoheight}} style="height:100%;object-fit:contain;"{{/autoheight}}/>
{{/imageurl}}
<img class="img-fluid d-inline d-md-none" alt="{{title}}" src="{{{imagemobileurl}}}"{{^autoheight}} style="height:100%;object-fit:contain;"{{/autoheight}}/>
{{/imagemobileurl}}
{{^imagemobileurl}}
{{#imageurl}}
<img class="img-fluid" alt="{{title}}" src="{{{imageurl}}}"{{^autoheight}} style="height:100%;object-fit:contain;"{{/autoheight}}/>
{{/imageurl}}
{{/imagemobileurl}}
{{^autoheight}}</div>{{/autoheight}}
<h5>{{title}}</h5>
</div>
{{/tiles}}
</div>
{{#tiles}}
{{^url}}
{{> unilabeltype_menu/dialog }}
{{/url}}
{{/tiles}}
{{/hastiles}}
{{^hastiles}}
<span>{{#str}} nocontent , unilabeltype_menu {{/str}}</span>
{{/hastiles}}

30
version.php Normal file
View File

@ -0,0 +1,30 @@
<?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/>.
/**
* unilabel type menu
*
* @package unilabeltype_menu
* @author Andreas Grabs <info@grabs-edv.de>
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
$plugin->component = 'unilabeltype_menu';
$plugin->version = 2020110703;
$plugin->requires = 2019111200;