first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+337
View File
@@ -0,0 +1,337 @@
<?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/>.
namespace mod_data\output;
use mod_data\manager;
use mod_data\preset;
use moodle_url;
use url_select;
/**
* Class responsible for generating the action bar elements in the database module pages.
*
* @package mod_data
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class action_bar {
/** @var int $id The database module id. */
private $id;
/** @var int $cmid The database course module id. */
private $cmid;
/** @var moodle_url $currenturl The URL of the current page. */
private $currenturl;
/**
* The class constructor.
*
* @param int $id The database module id.
* @param moodle_url $pageurl The URL of the current page.
*/
public function __construct(int $id, moodle_url $pageurl) {
$this->id = $id;
[$course, $cm] = get_course_and_cm_from_instance($this->id, 'data');
$this->cmid = $cm->id;
$this->currenturl = $pageurl;
}
/**
* Generate the output for the action bar in the field page.
*
* @param bool $hasfieldselect Whether the field selector element should be rendered.
* @param null $unused1 This parameter has been deprecated since 4.1 and should not be used anymore.
* @param null $unused2 This parameter has been deprecated since 4.1 and should not be used anymore.
* @return string The HTML code for the action bar.
*/
public function get_fields_action_bar(
bool $hasfieldselect = false,
?bool $unused1 = null,
?bool $unused2 = null
): string {
global $PAGE;
if ($unused1 !== null || $unused2 !== null) {
debugging('Deprecated argument passed to get_fields_action_bar method', DEBUG_DEVELOPER);
}
$renderer = $PAGE->get_renderer('mod_data');
$fieldsactionbar = new fields_action_bar($this->id);
return $renderer->render_fields_action_bar($fieldsactionbar);
}
/**
* Generate the output for the action bar in the field mappings page.
*
* @return string The HTML code for the action bar.
*/
public function get_fields_mapping_action_bar(): string {
global $PAGE;
$renderer = $PAGE->get_renderer('mod_data');
$fieldsactionbar = new fields_mappings_action_bar($this->id);
$data = $fieldsactionbar->export_for_template($renderer);
return $renderer->render_from_template('mod_data/fields_action_bar', $data);
}
/**
* Generate the output for the create a new field action menu.
*
* @param bool $isprimarybutton is the action trigger a primary or secondary button?
* @return \action_menu Action menu to create a new field
*/
public function get_create_fields(bool $isprimarybutton = false): \action_menu {
// Get the list of possible fields (plugins).
$plugins = \core_component::get_plugin_list('datafield');
$menufield = [];
foreach ($plugins as $plugin => $fulldir) {
$menufield[$plugin] = get_string('pluginname', "datafield_{$plugin}");
}
asort($menufield);
$fieldselect = new \action_menu();
$triggerclasses = ['btn'];
$triggerclasses[] = $isprimarybutton ? 'btn-primary' : 'btn-secondary';
$fieldselect->set_menu_trigger(get_string('newfield', 'mod_data'), join(' ', $triggerclasses));
$fieldselectparams = ['id' => $this->cmid, 'mode' => 'new'];
foreach ($menufield as $fieldtype => $fieldname) {
$fieldselectparams['newtype'] = $fieldtype;
$fieldselect->add(new \action_menu_link(
new moodle_url('/mod/data/field.php', $fieldselectparams),
new \pix_icon('field/' . $fieldtype, $fieldname, 'data'),
$fieldname,
false
));
}
$fieldselect->set_additional_classes('singlebutton');
return $fieldselect;
}
/**
* Generate the output for the action selector in the view page.
*
* @param bool $hasentries Whether entries exist.
* @param string $mode The current view mode (list, view...).
* @return string The HTML code for the action selector.
*/
public function get_view_action_bar(bool $hasentries, string $mode): string {
global $PAGE;
$viewlistlink = new moodle_url('/mod/data/view.php', ['d' => $this->id]);
$viewsinglelink = new moodle_url('/mod/data/view.php', ['d' => $this->id, 'mode' => 'single']);
$menu = [
$viewlistlink->out(false) => get_string('listview', 'mod_data'),
$viewsinglelink->out(false) => get_string('singleview', 'mod_data'),
];
$activeurl = $this->currenturl;
if ($this->currenturl->get_param('rid') || $this->currenturl->get_param('mode') == 'single') {
$activeurl = $viewsinglelink;
}
$urlselect = new url_select($menu, $activeurl->out(false), null, 'viewactionselect');
$urlselect->set_label(get_string('viewnavigation', 'mod_data'), ['class' => 'sr-only']);
$renderer = $PAGE->get_renderer('mod_data');
$viewactionbar = new view_action_bar($this->id, $urlselect, $hasentries, $mode);
return $renderer->render_view_action_bar($viewactionbar);
}
/**
* Generate the output for the action selector in the templates page.
*
* @return string The HTML code for the action selector.
*/
public function get_templates_action_bar(): string {
global $PAGE;
$listtemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id,
'mode' => 'listtemplate']);
$singletemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id,
'mode' => 'singletemplate']);
$advancedsearchtemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id,
'mode' => 'asearchtemplate']);
$addtemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'addtemplate']);
$rsstemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'rsstemplate']);
$csstemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'csstemplate']);
$jstemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'jstemplate']);
$menu = [
$addtemplatelink->out(false) => get_string('addtemplate', 'mod_data'),
$singletemplatelink->out(false) => get_string('singletemplate', 'mod_data'),
$listtemplatelink->out(false) => get_string('listtemplate', 'mod_data'),
$advancedsearchtemplatelink->out(false) => get_string('asearchtemplate', 'mod_data'),
$csstemplatelink->out(false) => get_string('csstemplate', 'mod_data'),
$jstemplatelink->out(false) => get_string('jstemplate', 'mod_data'),
$rsstemplatelink->out(false) => get_string('rsstemplate', 'mod_data'),
];
$selectmenu = new \core\output\select_menu('presetsactions', $menu, $this->currenturl->out(false));
$selectmenu->set_label(get_string('templatesnavigation', 'mod_data'), ['class' => 'sr-only']);
$renderer = $PAGE->get_renderer('mod_data');
$presetsactions = $this->get_presets_actions_select(false);
// Reset single template action.
$resetcurrrent = new moodle_url($this->currenturl);
$resetcurrrent->param('action', 'resettemplate');
$presetsactions->add(new \action_menu_link(
$resetcurrrent,
null,
get_string('resettemplate', 'mod_data'),
false,
['data-action' => 'resettemplate', 'data-dataid' => $this->id]
));
// Reset all templates action.
$resetallurl = new moodle_url($this->currenturl);
$resetallurl->params([
'action' => 'resetalltemplates',
'sesskey' => sesskey(),
]);
$presetsactions->add(new \action_menu_link(
$resetallurl,
null,
get_string('resetalltemplates', 'mod_data'),
false,
['data-action' => 'resetalltemplates', 'data-dataid' => $this->id]
));
$templatesactionbar = new templates_action_bar($this->id, $selectmenu, null, null, $presetsactions);
return $renderer->render_templates_action_bar($templatesactionbar);
}
/**
* Generate the output for the action selector in the presets page.
*
* @return string The HTML code for the action selector.
*/
public function get_presets_action_bar(): string {
global $PAGE;
$renderer = $PAGE->get_renderer('mod_data');
$presetsactionbar = new presets_action_bar($this->cmid, $this->get_presets_actions_select(true));
return $renderer->render_presets_action_bar($presetsactionbar);
}
/**
* Generate the output for the action selector in the presets preview page.
*
* @param manager $manager the manager instance
* @param string $fullname the preset fullname
* @param string $current the current template name
* @return string The HTML code for the action selector
*/
public function get_presets_preview_action_bar(manager $manager, string $fullname, string $current): string {
global $PAGE;
$renderer = $PAGE->get_renderer(manager::PLUGINNAME);
$cm = $manager->get_coursemodule();
$menu = [];
$selected = null;
foreach (['listtemplate', 'singletemplate'] as $templatename) {
$link = new moodle_url('/mod/data/preset.php', [
'd' => $this->id,
'template' => $templatename,
'fullname' => $fullname,
'action' => 'preview',
]);
$menu[$link->out(false)] = get_string($templatename, manager::PLUGINNAME);
if (!$selected || $templatename == $current) {
$selected = $link->out(false);
}
}
$urlselect = new url_select($menu, $selected, null);
$urlselect->set_label(get_string('templatesnavigation', manager::PLUGINNAME), ['class' => 'sr-only']);
$data = [
'title' => get_string('preview', manager::PLUGINNAME, preset::get_name_from_plugin($fullname)),
'hasback' => true,
'backtitle' => get_string('back'),
'backurl' => new moodle_url('/mod/data/preset.php', ['id' => $cm->id]),
'extraurlselect' => $urlselect->export_for_template($renderer),
];
return $renderer->render_from_template('mod_data/action_bar', $data);
}
/**
* Helper method to get the selector for the presets action.
*
* @param bool $hasimport Whether the Import buttons must be included or not.
* @return \action_menu|null The selector object used to display the presets actions. Null when the import button is not
* displayed and the database hasn't any fields.
*/
protected function get_presets_actions_select(bool $hasimport = false): ?\action_menu {
global $DB;
$hasfields = $DB->record_exists('data_fields', ['dataid' => $this->id]);
// Early return if the database has no fields and the import action won't be displayed.
if (!$hasfields && !$hasimport) {
return null;
}
$actionsselect = new \action_menu();
$actionsselect->set_menu_trigger(get_string('actions'), 'btn btn-secondary');
if ($hasimport) {
// Import.
$actionsselectparams = ['id' => $this->cmid];
$actionsselect->add(new \action_menu_link(
new moodle_url('/mod/data/preset.php', $actionsselectparams),
null,
get_string('importpreset', 'mod_data'),
false,
['data-action' => 'importpresets', 'data-dataid' => $this->cmid]
));
}
// If the database has no fields, export and save as preset options shouldn't be displayed.
if ($hasfields) {
// Export.
$actionsselectparams = ['id' => $this->cmid, 'action' => 'export'];
$actionsselect->add(new \action_menu_link(
new moodle_url('/mod/data/preset.php', $actionsselectparams),
null,
get_string('exportpreset', 'mod_data'),
false
));
// Save as preset.
$actionsselect->add(new \action_menu_link(
new moodle_url('/mod/data/preset.php', $actionsselectparams),
null,
get_string('saveaspreset', 'mod_data'),
false,
['data-action' => 'saveaspreset', 'data-dataid' => $this->id]
));
}
return $actionsselect;
}
}
@@ -0,0 +1,67 @@
<?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/>.
namespace mod_data\output;
use moodle_url;
use templatable;
use renderable;
/**
* Renderable class for the Add entries button in the database activity.
*
* @package mod_data
* @copyright 2022 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class add_entries_action implements templatable, renderable {
/** @var int $id The database module id. */
private $id;
/**
* The class constructor.
*
* @param int $id The database module id.
* @param bool $hasentries Whether entries exist.
*/
public function __construct(int $id) {
$this->id = $id;
}
/**
* Export the data for the mustache template.
*
* @param \renderer_base $output The renderer to be used to render the add entries button.
* @return \stdClass or null if the user has no permission to add new entries.
*/
public function export_for_template(\renderer_base $output): ?\stdClass {
global $PAGE, $DB;
$database = $DB->get_record('data', ['id' => $this->id]);
$cm = get_coursemodule_from_instance('data', $this->id);
$currentgroup = groups_get_activity_group($cm);
$groupmode = groups_get_activity_groupmode($cm);
if (data_user_can_add_entry($database, $currentgroup, $groupmode, $PAGE->context)) {
$addentrylink = new moodle_url('/mod/data/edit.php', ['d' => $this->id, 'backto' => $PAGE->url->out(false)]);
$button = new \single_button($addentrylink, get_string('add', 'mod_data'), 'get', \single_button::BUTTON_PRIMARY);
return $button->export_for_template($output);
}
return null;
}
}
+102
View File
@@ -0,0 +1,102 @@
<?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/>.
namespace mod_data\output;
use core_tag_tag;
use mod_data\manager;
use templatable;
use renderable;
/**
* Renderable class for the default templates in the database activity.
*
* @package mod_data
* @copyright 2022 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class defaulttemplate implements templatable, renderable {
/** @var array $fields The array containing the existing fields. */
private $fields;
/** @var string $templatename The template name (addtemplate, listtemplate...). */
private $templatename;
/** @var bool $isform Whether a form should be displayed instead of data. */
private $isform;
/**
* The class constructor.
*
* @param array $fields The array containing the existing fields.
* @param string $templatename The template name (addtemplate, listtemplate...).
* @param bool $isform Whether a form should be displayed instead of data.
*/
public function __construct(array $fields, string $templatename, bool $isform) {
$this->fields = $fields;
$this->templatename = $templatename;
$this->isform = $isform;
}
/**
* Obtains the mustache template name for this database template.
*
* @return string the file mustache path for this template.
*/
public function get_templatename(): string {
return 'mod_data/defaulttemplate_' . $this->templatename;
}
/**
* Export the data for the mustache template.
*
* @param \renderer_base $output The renderer to be used to render the action bar elements.
* @return array The data to display.
*/
public function export_for_template(\renderer_base $output): array {
$result = [];
$exportedfields = [];
foreach ($this->fields as $field) {
$fieldname = $field->field->name;
if ($this->isform) {
$fieldcontent = $field->display_add_field();
} else {
$fieldcontent = '[[' . $fieldname . ']]';
}
$exportedfields[] = [
'fieldname' => $fieldname,
'fieldcontent' => $fieldcontent,
];
}
if (!empty($exportedfields)) {
$result['fields'] = $exportedfields;
}
if (core_tag_tag::is_enabled(manager::PLUGINNAME, 'data_records')) {
// Add tags information only if they are enabled.
if ($this->isform) {
$tags = data_generate_tag_form();
} else {
$tags = '##tags##';
}
$result['tags'] = $tags;
}
return $result;
}
}
@@ -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/>.
namespace mod_data\output;
use mod_data\manager;
use moodle_url;
use templatable;
use renderable;
/**
* Renderable class for the action bar elements for an empty database activity.
*
* @package mod_data
* @copyright 2022 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class empty_database_action_bar implements templatable, renderable {
/** @var manager The manager instance. */
protected $manager;
/**
* The class constructor.
*
* @param int $id The database module id.
*/
public function __construct(manager $manager) {
$this->manager = $manager;
}
/**
* Export the data for the mustache template.
*
* @param \renderer_base $output The renderer to be used to render the action bar elements.
* @return array
*/
public function export_for_template(\renderer_base $output): array {
global $PAGE;
$instance = $this->manager->get_instance();
$addentrybutton = new add_entries_action($instance->id);
$data = ['addentrybutton' => $addentrybutton->export_for_template($output)];
if (has_capability('mod/data:manageentries', $PAGE->context)) {
$params = ['d' => $instance->id, 'backto' => $PAGE->url->out(false)];
$importentrieslink = new moodle_url('/mod/data/import.php', $params);
$importentriesbutton = new \single_button($importentrieslink,
get_string('importentries', 'mod_data'), 'get');
$data['importentriesbutton'] = $importentriesbutton->export_for_template($output);
}
return $data;
}
}
@@ -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/>.
namespace mod_data\output;
use templatable;
use renderable;
/**
* Renderable class for the action bar elements in the field pages in the database activity.
*
* @package mod_data
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class fields_action_bar implements templatable, renderable {
/** @var int $id The database module id. */
private $id;
/**
* The class constructor.
*
* @param int $id The database module id
* @param null $unused1 This parameter has been deprecated since 4.1 and should not be used anymore.
* @param null $unused2 This parameter has been deprecated since 4.1 and should not be used anymore.
* @param null $unused3 This parameter has been deprecated since 4.1 and should not be used anymore.
* @param null $unused4 This parameter has been deprecated since 4.1 and should not be used anymore.
* @param \action_menu|null $unused5 This parameter has been deprecated since 4.2 and should not be used anymore.
*/
public function __construct(int $id, $unused1 = null, $unused2 = null,
$unused3 = null, $unused4 = null,
?\action_menu $unused5 = null) {
if ($unused1 !== null || $unused2 !== null || $unused3 !== null || $unused4 !== null || $unused5 !== null) {
debugging('Deprecated argument passed to fields_action_bar constructor', DEBUG_DEVELOPER);
}
$this->id = $id;
}
/**
* Export the data for the mustache template.
*
* @param \renderer_base $output The renderer to be used to render the action bar elements.
* @return array
*/
public function export_for_template(\renderer_base $output): array {
$data = [
'd' => $this->id,
'title' => get_string('managefields', 'mod_data'),
];
return $data;
}
}
@@ -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/>.
namespace mod_data\output;
use templatable;
use renderable;
/**
* Renderable class for the action bar elements in the fields mapping page in the database activity.
*
* @package mod_data
* @copyright 2022 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class fields_mappings_action_bar implements templatable, renderable {
/** @var int $id The database module id. */
private $id;
/**
* The class constructor.
*
* @param int $id The database module id
*/
public function __construct(int $id) {
$this->id = $id;
}
/**
* Export the data for the mustache template.
*
* @param \renderer_base $output The renderer to be used to render the action bar elements.
* @return array
*/
public function export_for_template(\renderer_base $output): array {
return [
'tertiarytitle' => get_string('fieldmappings', 'mod_data'),
'hasback' => true,
'backtitle' => get_string('back'),
'backurl' => new \moodle_url('/mod/data/preset.php', ['d' => $this->id]),
];
}
}
+118
View File
@@ -0,0 +1,118 @@
<?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/>.
namespace mod_data\output;
use templatable;
use renderable;
use mod_data\manager;
use mod_data\preset;
use mod_data\template;
use moodle_page;
use moodle_url;
/**
* Preset preview output class.
*
* @package mod_data
* @copyright 2022 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class preset_preview implements templatable, renderable {
/** @var manager manager instance. */
private $manager;
/** @var preset the preset. */
private $preset;
/** @var string the template to preview. */
private $templatename;
/**
* The class constructor.
*
* @param manager $manager the activity instance manager
* @param preset $preset the preset
* @param string $templatename the templatename
*/
public function __construct(manager $manager, preset $preset, string $templatename) {
$this->manager = $manager;
$this->preset = $preset;
$this->templatename = $templatename;
}
/**
* Add the preset CSS and JS to the page.
*
* @param moodle_page $page the current page instance
*/
public function prepare_page(moodle_page $page) {
$instance = $this->manager->get_instance();
$preset = $this->preset;
// Add CSS and JS.
$csscontent = $preset->get_template_content('csstemplate');
if (!empty($csscontent)) {
$url = new moodle_url('/mod/data/css.php', ['d' => $instance->id, 'preset' => $preset->get_fullname()]);
$page->requires->css($url);
}
$jscontent = $preset->get_template_content('jstemplate');
if (!empty($jscontent)) {
$url = new moodle_url('/mod/data/js.php', ['d' => $instance->id, 'preset' => $preset->get_fullname()]);
$page->requires->js($url);
}
}
/**
* Export the data for the mustache template.
*
* @param \renderer_base $output renderer to be used to render the action bar elements.
* @return array
*/
public function export_for_template(\renderer_base $output): array {
$coursemodule = $this->manager->get_coursemodule();
$preset = $this->preset;
// Get fields for preview.
$count = ($this->templatename == 'listtemplate') ? 2 : 1;
$fields = $preset->get_fields(true);
$entries = $preset->get_sample_entries($count);
$templatecontent = $preset->get_template_content($this->templatename);
$useurl = new moodle_url('/mod/data/field.php');
// Generate preview content.
$options = ['templatename' => $this->templatename];
if ($this->templatename == 'listtemplate') {
$options['showmore'] = true;
}
$parser = new template($this->manager, $templatecontent, $options, $fields);
$content = $parser->parse_entries($entries);
if ($this->templatename == 'listtemplate') {
$listtemplateheader = $preset->get_template_content('listtemplateheader');
$listtemplatefooter = $preset->get_template_content('listtemplatefooter');
$content = $listtemplateheader . $content . $listtemplatefooter;
}
return [
'cmid' => $coursemodule->id,
'description' => $preset->description ?? '',
'preview' => $content,
'formactionurl' => $useurl->out(),
'userid' => $preset->get_userid() ?? 0,
'shortname' => $preset->shortname,
];
}
}
+238
View File
@@ -0,0 +1,238 @@
<?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/>.
namespace mod_data\output;
use action_menu;
use action_menu_link_secondary;
use mod_data\manager;
use mod_data\preset;
use moodle_url;
use templatable;
use renderable;
use renderer_base;
use stdClass;
/**
* Renderable class for the presets table in the database activity.
*
* @package mod_data
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class presets implements templatable, renderable {
/** @var array $presets The array containing the existing presets. */
private $presets;
/** @var moodle_url $formactionurl The action url for the form. */
private $formactionurl;
/** @var bool $manage Whether the manage preset options should be displayed. */
private $manage;
/** @var int $id instance id */
private $id;
/** @var int $cmid course module id */
private $cmid;
/**
* The class constructor.
*
* @param manager $manager The database manager
* @param array $presets The array containing the existing presets
* @param moodle_url $formactionurl The action url for the form
* @param bool $manage Whether the manage preset options should be displayed
*/
public function __construct(manager $manager, array $presets, moodle_url $formactionurl, bool $manage = false) {
$this->id = $manager->get_instance()->id;
$this->cmid = $manager->get_coursemodule()->id;
$this->presets = $presets;
$this->formactionurl = $formactionurl;
$this->manage = $manage;
}
/**
* Export the data for the mustache template.
*
* @param renderer_base $output The renderer to be used to render the action bar elements.
* @return array
*/
public function export_for_template(renderer_base $output): array {
$presets = $this->get_presets($output);
return [
'id' => $this->cmid,
'formactionurl' => $this->formactionurl->out(),
'showmanage' => $this->manage,
'presets' => $presets,
];
}
/**
* Returns the presets list with the information required to display them.
*
* @param renderer_base $output The renderer to be used to render the action bar elements.
* @return array Presets list.
*/
private function get_presets(renderer_base $output): array {
$presets = [];
foreach ($this->presets as $index => $preset) {
$presetname = $preset->name;
$userid = $preset instanceof preset ? $preset->get_userid() : $preset->userid;
if (!empty($userid)) {
// If the preset has the userid field, the full name of creator it will be added to the end of the name.
$userfieldsapi = \core_user\fields::for_name();
$namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects;
$fields = 'id, ' . $namefields;
$presetuser = \core_user::get_user($userid, $fields, MUST_EXIST);
$username = fullname($presetuser, true);
$presetname = "{$presetname} ({$username})";
}
$actions = $this->get_preset_action_menu($output, $preset, $userid);
$fullname = $preset->get_fullname();
$previewurl = new moodle_url(
'/mod/data/preset.php',
['d' => $this->id, 'fullname' => $fullname, 'action' => 'preview']
);
$presets[] = [
'id' => $this->id,
'cmid' => $this->cmid,
'name' => $preset->name,
'url' => $previewurl->out(),
'shortname' => $preset->shortname,
'fullname' => $presetname,
'description' => $preset->description,
'userid' => $userid,
'actions' => $actions,
'presetindex' => $index,
];
}
return $presets;
}
/**
* Return the preset action menu data.
*
* @param renderer_base $output The renderer to be used to render the action bar elements.
* @param preset|stdClass $preset the preset object
* @param int|null $userid the user id (null for plugin presets)
* @return stdClass the resulting action menu
*/
private function get_preset_action_menu(renderer_base $output, $preset, ?int $userid): stdClass {
$actions = new stdClass();
// If we cannot manage then return an empty menu.
if (!$this->manage) {
return $actions;
}
$actionmenu = new action_menu();
$actionmenu->set_kebab_trigger();
$actionmenu->set_action_label(get_string('actions'));
$actionmenu->set_additional_classes('presets-actions');
$canmanage = $preset->can_manage();
$usepreseturl = new moodle_url('/mod/data/preset.php', [
'action' => 'usepreset',
'cmid' => $this->cmid,
]);
$this->add_action_menu($actionmenu, get_string('usepreset', 'mod_data'), $usepreseturl, [
'data-action' => 'selectpreset',
'data-presetname' => $preset->get_fullname(),
'data-cmid' => $this->cmid,
]
);
// Attention: the id here is the cm->id, not d->id.
$previewpreseturl = new moodle_url('/mod/data/preset.php', [
'fullname' => $preset->get_fullname(),
'action' => 'preview',
'id' => $this->cmid,
]);
$this->add_action_menu($actionmenu, get_string('previewaction', 'mod_data'), $previewpreseturl, [
'data-action' => 'preview',
]
);
// Presets saved by users can be edited or removed.
if (!$preset->isplugin) {
// Edit.
if ($canmanage) {
$editactionurl = new moodle_url('/mod/data/preset.php', [
'action' => 'edit',
'd' => $this->id,
]);
$this->add_action_menu($actionmenu, get_string('edit'), $editactionurl, [
'data-action' => 'editpreset',
'data-presetname' => $preset->name,
'data-presetdescription' => $preset->description,
]);
}
// Export.
$exporturl = new moodle_url('/mod/data/preset.php', [
'presetname' => $preset->name,
'action' => 'export',
'd' => $this->id,
]);
$this->add_action_menu($actionmenu, get_string('export', 'mod_data'), $exporturl, [
'data-action' => 'exportpreset',
'data-presetname' => $preset->name,
'data-presetdescription' => $preset->description,
]);
// Delete.
if ($canmanage) {
$deleteactionurl = new moodle_url('/mod/data/preset.php', [
'action' => 'delete',
'd' => $this->id,
]);
$this->add_action_menu($actionmenu, get_string('delete'), $deleteactionurl, [
'data-action' => 'deletepreset',
'data-presetname' => $preset->name,
]);
}
}
$actions = $actionmenu->export_for_template($output);
return $actions;
}
/**
* Add action to the action menu
*
* @param action_menu $actionmenu
* @param string $actionlabel
* @param moodle_url $actionurl
* @param array $otherattributes
* @return void
*/
private function add_action_menu(action_menu &$actionmenu, string $actionlabel, moodle_url $actionurl,
array $otherattributes) {
$attributes = [
'data-dataid' => $this->id,
];
$actionmenu->add(new action_menu_link_secondary(
$actionurl,
null,
$actionlabel,
array_merge($attributes, $otherattributes),
));
}
}
@@ -0,0 +1,65 @@
<?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/>.
namespace mod_data\output;
use templatable;
use renderable;
/**
* Renderable class for the action bar elements in the presets page in the database activity.
*
* @package mod_data
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class presets_action_bar implements templatable, renderable {
/** @var int $id The database module id. */
private $cmid;
/** @var \action_menu $actionsselect The presets actions selector object. */
private $actionsselect;
/**
* The class constructor.
*
* @param int $cmid The database module id
* @param \action_menu|null $actionsselect The presets actions selector object.
*/
public function __construct(int $cmid, ?\action_menu $actionsselect) {
$this->cmid = $cmid;
$this->actionsselect = $actionsselect;
}
/**
* Export the data for the mustache template.
*
* @param \renderer_base $output The renderer to be used to render the action bar elements.
* @return array
*/
public function export_for_template(\renderer_base $output): array {
$data = [
'id' => $this->cmid,
];
if ($this->actionsselect) {
$data['actionsselect'] = $this->actionsselect->export_for_template($output);
}
return $data;
}
}
+193
View File
@@ -0,0 +1,193 @@
<?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/>.
namespace mod_data\output;
use templatable;
use renderable;
use mod_data\manager;
use moodle_url;
use texteditor;
/**
* Renderable class for template editor.
*
* @package mod_data
* @copyright 2022 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class template_editor implements templatable, renderable {
/** @var manager manager instance. */
private $manager;
/** @var string the template name. */
private $templatename;
/**
* The class constructor.
*
* @param manager $manager the activity instance manager
* @param string $templatename the template to edit
*/
public function __construct(manager $manager, string $templatename) {
$this->manager = $manager;
$this->templatename = $templatename;
}
/**
* Export the data for the mustache template.
*
* @param \renderer_base $output renderer to be used to render the action bar elements.
* @return array
*/
public function export_for_template(\renderer_base $output): array {
$instance = $this->manager->get_instance();
$cm = $this->manager->get_coursemodule();
$data = [
'title' => get_string('header' . $this->templatename, 'data'),
'sesskey' => sesskey(),
'disableeditor' => true,
'url' => new moodle_url('/mod/data/templates.php', ['id' => $cm->id, 'mode' => $this->templatename]),
];
// Determine whether to use HTML editors.
$usehtmleditor = false;
$disableeditor = false;
if (($this->templatename !== 'csstemplate') && ($this->templatename !== 'jstemplate')) {
$usehtmleditor = data_get_config($instance, "editor_{$this->templatename}", true);
$disableeditor = true;
}
$data['usehtmleditor'] = $usehtmleditor;
// Some templates, like CSS, cannot enable the wysiwyg editor.
$data['disableeditor'] = $disableeditor;
$tools = new template_editor_tools($this->manager, $this->templatename);
$data['toolbar'] = $tools->export_for_template($output);
$data['editors'] = $this->get_editors_data($usehtmleditor);
return $data;
}
/**
* Get the editors data.
*
* @param bool $usehtmleditor if the user wants wysiwyg editor or not
* @return array editors data
*/
private function get_editors_data(bool $usehtmleditor): array {
global $PAGE;
$result = [];
$manager = $this->manager;
$instance = $manager->get_instance();
// Setup editor.
editors_head_setup();
$PAGE->requires->js_call_amd(
'mod_data/templateseditor',
'init',
['d' => $instance->id, 'mode' => $this->templatename]
);
$format = FORMAT_PLAIN;
if ($usehtmleditor) {
$format = FORMAT_HTML;
}
$editor = editors_get_preferred_editor($format);
// Add editors.
if ($this->templatename === 'listtemplate') {
$template = $manager->get_template('listtemplateheader');
$result[] = $this->generate_editor_data(
$editor,
'header',
'listtemplateheader',
$template->get_template_content()
);
$maineditorname = 'multientry';
} else {
$maineditorname = $this->templatename;
}
$template = $manager->get_template($this->templatename);
$result[] = $this->generate_editor_data(
$editor,
$maineditorname,
$this->templatename,
$template->get_template_content()
);
if ($this->templatename === 'listtemplate') {
$template = $manager->get_template('listtemplatefooter');
$result[] = $this->generate_editor_data(
$editor,
'footer',
'listtemplatefooter',
$template->get_template_content()
);
}
if ($this->templatename === 'rsstemplate') {
$template = $manager->get_template('rsstitletemplate');
$result[] = $this->generate_editor_data(
$editor,
'rsstitletemplate',
'rsstitletemplate',
$template->get_template_content()
);
}
return $result;
}
/**
* Generate a single editor data.
*
* @param texteditor $editor the editor object
* @param string $name the editor name
* @param string $fieldname the field name
* @param string|null $value the current value
* @return array the editor data
*/
private function generate_editor_data(
texteditor $editor,
string $name,
string $fieldname,
?string $value
): array {
$options = [
'trusttext' => false,
'forcehttps' => false,
'subdirs' => false,
'maxfiles' => 0,
'maxbytes' => 0,
'changeformat' => 0,
'noclean' => false,
];
$result = [
'name' => get_string($name, 'data'),
'fieldname' => $fieldname,
'value' => $value,
];
$editor->set_text($value);
$editor->use_editor($fieldname, $options);
return $result;
}
}
@@ -0,0 +1,206 @@
<?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/>.
namespace mod_data\output;
use templatable;
use renderable;
use core_tag_tag;
use mod_data\manager;
/**
* Renderable class for template editor tools.
*
* @package mod_data
* @copyright 2022 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class template_editor_tools implements templatable, renderable {
/** @var manager manager instance. */
private $manager;
/** @var string the template name. */
private $templatename;
/**
* The class constructor.
*
* @param manager $manager the activity instance manager
* @param string $templatename the template to edit
*/
public function __construct(manager $manager, string $templatename) {
$this->manager = $manager;
$this->templatename = $templatename;
}
/**
* Export the data for the mustache template.
*
* @param \renderer_base $output renderer to be used to render the action bar elements.
* @return array
*/
public function export_for_template(\renderer_base $output): array {
$tools = [
$this->get_field_tags($this->templatename),
$this->get_field_info_tags($this->templatename),
$this->get_action_tags($this->templatename),
$this->get_other_tags($this->templatename),
];
$tools = array_filter($tools, static function ($value) {
return !empty($value['tags']);
});
return [
'toolshelp' => $output->help_icon('availabletags', 'data'),
'hastools' => !empty($tools),
'tools' => array_values($tools),
];
}
/**
* Return the field template tags.
*
* @param string $templatename the template name
* @return array|null array of tags.
*/
protected function get_field_tags(string $templatename): array {
$name = get_string('fields', 'data');
if ($templatename == 'csstemplate' || $templatename == 'jstemplate') {
return $this->get_optgroup_data($name, []);
}
$taglist = [];
$fields = $this->manager->get_fields();
foreach ($fields as $field) {
if ($field->type === 'unknown') {
continue;
}
$fieldname = $field->get_name();
$taglist["[[$fieldname]]"] = $fieldname;
}
$taglist['##otherfields##'] = get_string('otherfields', 'data');
return $this->get_optgroup_data($name, $taglist);
}
/**
* Return the field information template tags.
*
* @param string $templatename the template name
* @return array|null array of tags.
*/
protected function get_field_info_tags(string $templatename): array {
$name = get_string('fieldsinformationtags', 'data');
$taglist = [];
$fields = $this->manager->get_fields();
foreach ($fields as $field) {
if ($field->type === 'unknown') {
continue;
}
$fieldname = $field->get_name();
if ($templatename == 'addtemplate') {
$taglist["[[$fieldname#id]]"] = get_string('fieldtagid', 'mod_data', $fieldname);
}
$taglist["[[$fieldname#name]]"] = get_string('fieldtagname', 'mod_data', $fieldname);
$taglist["[[$fieldname#description]]"] = get_string('fieldtagdescription', 'mod_data', $fieldname);
}
return $this->get_optgroup_data($name, $taglist);
}
/**
* Return the field action tags.
*
* @param string $templatename the template name
* @return array|null array of tags.
*/
protected function get_action_tags(string $templatename): array {
$name = get_string('actions');
if ($templatename == 'addtemplate' || $templatename == 'asearchtemplate') {
return $this->get_optgroup_data($name, []);
}
$taglist = [
'##actionsmenu##' => get_string('actionsmenu', 'data'),
'##edit##' => get_string('edit', 'data'),
'##delete##' => get_string('delete', 'data'),
'##approve##' => get_string('approve', 'data'),
'##disapprove##' => get_string('disapprove', 'data'),
];
if ($templatename != 'rsstemplate') {
$taglist['##export##'] = get_string('export', 'data');
}
if ($templatename != 'singletemplate') {
$taglist['##more##'] = get_string('more', 'data');
$taglist['##moreurl##'] = get_string('moreurl', 'data');
$taglist['##delcheck##'] = get_string('delcheck', 'data');
}
return $this->get_optgroup_data($name, $taglist);
}
/**
* Return the available other tags
*
* @param string $templatename the template name
* @return array associative array of tags => tag name
*/
protected function get_other_tags(string $templatename): array {
$name = get_string('other', 'data');
$taglist = [];
if ($templatename == 'asearchtemplate') {
$taglist['##firstname##'] = get_string('firstname');
$taglist['##lastname##'] = get_string('lastname');
return $this->get_optgroup_data($name, $taglist);
}
if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
$taglist['##tags##'] = get_string('tags');
}
if ($templatename == 'addtemplate') {
return $this->get_optgroup_data($name, $taglist);
}
$taglist['##timeadded##'] = get_string('timeadded', 'data');
$taglist['##timemodified##'] = get_string('timemodified', 'data');
$taglist['##user##'] = get_string('user');
$taglist['##userpicture##'] = get_string('userpic');
$taglist['##approvalstatus##'] = get_string('approvalstatus', 'data');
$taglist['##id##'] = get_string('id', 'data');
if ($templatename == 'singletemplate') {
return $this->get_optgroup_data($name, $taglist);
}
$taglist['##comments##'] = get_string('comments', 'data');
return $this->get_optgroup_data($name, $taglist);
}
/**
* Generate a valid optgroup data.
*
* @param string $name the optgroup name
* @param array $taglist the indexed array of taglists ($tag => $tagname)
* @return array of optgroup data
*/
protected function get_optgroup_data(string $name, array $taglist): array {
$tags = [];
foreach ($taglist as $tag => $tagname) {
$tags[] = [
'tag' => "$tag",
'tagname' => $tagname . ' - ' . $tag,
];
}
return [
'name' => $name,
'tags' => $tags,
];
}
}
@@ -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/>.
namespace mod_data\output;
use core\output\select_menu;
use templatable;
use renderable;
/**
* Renderable class for the action bar elements in the template pages in the database activity.
*
* @package mod_data
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class templates_action_bar implements templatable, renderable {
/** @var int $id The database module id. */
private $id;
/** @var select_menu $selectmenu The URL selector object. */
private $selectmenu;
/** @var \action_menu $actionsselect The presets actions selector object. */
private $actionsselect;
/**
* The class constructor.
*
* @param int $id The database module id.
* @param select_menu $selectmenu The URL selector object.
* @param null $unused1 This parameter has been deprecated since 4.1 and should not be used anymore.
* @param null $unused2 This parameter has been deprecated since 4.1 and should not be used anymore.
* @param \action_menu $actionsselect The presets actions selector object.
*/
public function __construct(int $id, select_menu $selectmenu, $unused1, $unused2, \action_menu $actionsselect) {
$this->id = $id;
$this->selectmenu = $selectmenu;
$this->actionsselect = $actionsselect;
}
/**
* Export the data for the mustache template.
*
* @param \renderer_base $output renderer to be used to render the action bar elements.
* @return array
*/
public function export_for_template(\renderer_base $output): array {
return [
'd' => $this->id,
'selectmenu' => $this->selectmenu->export_for_template($output),
'actionsselect' => $this->actionsselect->export_for_template($output),
];
}
}
+148
View File
@@ -0,0 +1,148 @@
<?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/>.
namespace mod_data\output;
use data_portfolio_caller;
use mod_data\manager;
use moodle_url;
use portfolio_add_button;
use templatable;
use renderable;
/**
* Renderable class for the action bar elements in the view pages in the database activity.
*
* @package mod_data
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class view_action_bar implements templatable, renderable {
/** @var int $id The database module id. */
private $id;
/** @var \url_select $urlselect The URL selector object. */
private $urlselect;
/** @var bool $hasentries Whether entries exist. */
private $hasentries;
/** @var bool $mode The current view mode (list, view...). */
private $mode;
/**
* The class constructor.
*
* @param int $id The database module id.
* @param \url_select $urlselect The URL selector object.
* @param bool $hasentries Whether entries exist.
* @param string $mode The current view mode (list, view...).
*/
public function __construct(int $id, \url_select $urlselect, bool $hasentries, string $mode) {
$this->id = $id;
$this->urlselect = $urlselect;
$this->hasentries = $hasentries;
$this->mode = $mode;
}
/**
* Export the data for the mustache template.
*
* @param \renderer_base $output The renderer to be used to render the action bar elements.
* @return array
*/
public function export_for_template(\renderer_base $output): array {
global $PAGE, $DB, $CFG;
$data = [
'urlselect' => $this->urlselect->export_for_template($output),
];
$activity = $DB->get_record('data', ['id' => $this->id], '*', MUST_EXIST);
$manager = manager::create_from_instance($activity);
$actionsselect = null;
// Import entries.
if (has_capability('mod/data:manageentries', $manager->get_context())) {
$actionsselect = new \action_menu();
$actionsselect->set_menu_trigger(get_string('actions'), 'btn btn-secondary');
$importentrieslink = new moodle_url('/mod/data/import.php', ['d' => $this->id, 'backto' => $PAGE->url->out(false)]);
$actionsselect->add(new \action_menu_link(
$importentrieslink,
null,
get_string('importentries', 'mod_data'),
false
));
}
// Export entries.
if (has_capability(DATA_CAP_EXPORT, $manager->get_context()) && $this->hasentries) {
if (!$actionsselect) {
$actionsselect = new \action_menu();
$actionsselect->set_menu_trigger(get_string('actions'), 'btn btn-secondary');
}
$exportentrieslink = new moodle_url('/mod/data/export.php', ['d' => $this->id, 'backto' => $PAGE->url->out(false)]);
$actionsselect->add(new \action_menu_link(
$exportentrieslink,
null,
get_string('exportentries', 'mod_data'),
false
));
}
// Export to portfolio. This is for exporting all records, not just the ones in the search.
if ($this->mode == '' && !empty($CFG->enableportfolios) && $this->hasentries) {
if ($manager->can_export_entries()) {
// Add the portfolio export button.
require_once($CFG->libdir . '/portfoliolib.php');
$cm = $manager->get_coursemodule();
$button = new portfolio_add_button();
$button->set_callback_options(
'data_portfolio_caller',
['id' => $cm->id],
'mod_data'
);
if (data_portfolio_caller::has_files($activity)) {
// No plain HTML.
$button->set_formats([PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_LEAP2A]);
}
$exporturl = $button->to_html(PORTFOLIO_ADD_MOODLE_URL);
if (!is_null($exporturl)) {
if (!$actionsselect) {
$actionsselect = new \action_menu();
$actionsselect->set_menu_trigger(get_string('actions'), 'btn btn-secondary');
}
$actionsselect->add(new \action_menu_link(
$exporturl,
null,
get_string('addtoportfolio', 'portfolio'),
false
));
}
}
}
if ($actionsselect) {
$data['actionsselect'] = $actionsselect->export_for_template($output);
}
return $data;
}
}
+156
View File
@@ -0,0 +1,156 @@
<?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/>.
namespace mod_data\output;
use action_link;
use core\output\sticky_footer;
use html_writer;
use mod_data\manager;
use mod_data\template;
use moodle_url;
use renderer_base;
/**
* Renderable class for sticky footer in the view pages of the database activity.
*
* @package mod_data
* @copyright 2022 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class view_footer extends sticky_footer {
/** @var int $totalcount the total records count. */
private $totalcount;
/** @var int $currentpage the current page */
private $currentpage;
/** @var int $nowperpage the number of elements per page */
private $nowperpage;
/** @var moodle_url $baseurl the page base url */
private $baseurl;
/** @var template $parser the template name */
private $parser;
/** @var manager $manager if the user can manage capabilities or not */
private $manager;
/**
* The class constructor.
*
* @param manager $manager the activity manager
* @param int $totalcount the total records count
* @param int $currentpage the current page
* @param int $nowperpage the number of elements per page
* @param moodle_url $baseurl the page base url
* @param template $parser the current template name
*/
public function __construct(
manager $manager,
int $totalcount,
int $currentpage,
int $nowperpage,
moodle_url $baseurl,
template $parser
) {
$this->manager = $manager;
$this->totalcount = $totalcount;
$this->currentpage = $currentpage;
$this->nowperpage = $nowperpage;
$this->baseurl = $baseurl;
$this->parser = $parser;
}
/**
* Export this data so it can be used as the context for a mustache template (core/inplace_editable).
*
* @param renderer_base $output typically, the renderer that's calling this function
* @return array data context for a mustache template
*/
public function export_for_template(renderer_base $output) {
$this->set_content(
$this->get_footer_output($output)
);
return parent::export_for_template($output);
}
/**
* Generate the pre-rendered footer content.
*
* @param \renderer_base $output The renderer to be used to render the action bar elements.
* @return string the rendered content
*/
public function get_footer_output(renderer_base $output): string {
$data = [];
$cm = $this->manager->get_coursemodule();
$instance = $this->manager->get_instance();
$currentgroup = groups_get_activity_group($cm);
$groupmode = groups_get_activity_groupmode($cm);
$context = $this->manager->get_context();
$canmanageentries = has_capability('mod/data:manageentries', $context);
$parser = $this->parser;
// Sticky footer content.
$data['pagination'] = $output->paging_bar(
$this->totalcount,
$this->currentpage,
$this->nowperpage,
$this->baseurl
);
if ($parser->get_template_name() != 'singletemplate' && $parser->has_tag('delcheck') && $canmanageentries) {
// Build the select/deselect all control.
$selectallid = 'selectall-listview-entries';
$togglegroup = 'listview-entries';
$mastercheckbox = new \core\output\checkbox_toggleall($togglegroup, true, [
'id' => $selectallid,
'name' => $selectallid,
'value' => 1,
'label' => get_string('selectall'),
'classes' => 'btn-secondary mx-1',
], true);
$data['selectall'] = $output->render($mastercheckbox);
$data['deleteselected'] = html_writer::empty_tag('input', [
'class' => 'btn btn-secondary mx-1',
'type' => 'submit',
'value' => get_string('deleteselected'),
'disabled' => true,
'data-action' => 'toggle',
'data-togglegroup' => $togglegroup,
'data-toggle' => 'action',
]);
}
if (data_user_can_add_entry($instance, $currentgroup, $groupmode, $context)) {
$addentrylink = new moodle_url(
'/mod/data/edit.php',
['id' => $cm->id, 'backto' => $this->baseurl]
);
$addentrybutton = new action_link(
$addentrylink,
get_string('add', 'mod_data'),
null,
['class' => 'btn btn-primary mx-1', 'role' => 'button']
);
$data['addentrybutton'] = $addentrybutton->export_for_template($output);
}
return $output->render_from_template('mod_data/view_footer', $data);
}
}
@@ -0,0 +1,80 @@
<?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/>.
namespace mod_data\output;
use mod_data\manager;
use moodle_url;
use templatable;
use renderable;
/**
* Renderable class for the action bar elements in the zero state (no fields created) pages in the database activity.
*
* @package mod_data
* @copyright 2022 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class zero_state_action_bar implements templatable, renderable {
/** @var manager The manager instance. */
protected $manager;
/**
* The class constructor.
*
* @param manager $manager The manager instance.
*/
public function __construct(manager $manager) {
$this->manager = $manager;
}
/**
* Export the data for the mustache template.
*
* @param \renderer_base $output The renderer to be used to render the action bar elements.
* @return array
*/
public function export_for_template(\renderer_base $output): array {
global $PAGE;
$data = [];
if ($this->manager->can_manage_templates()) {
$cm = $this->manager->get_coursemodule();
$instance = $this->manager->get_instance();
$params = ['id' => $cm->id, 'backto' => $PAGE->url->out(false)];
$usepresetlink = new moodle_url('/mod/data/preset.php', $params);
$usepresetbutton = new \single_button($usepresetlink,
get_string('usestandard', 'mod_data'), 'get', \single_button::BUTTON_PRIMARY);
$data['usepresetbutton'] = $usepresetbutton->export_for_template($output);
$actionbar = new \mod_data\output\action_bar($instance->id, $PAGE->url);
$createfieldbutton = $actionbar->get_create_fields();
$data['createfieldbutton'] = $createfieldbutton->export_for_template($output);
$importpresetlink = new moodle_url('/mod/data/preset.php', $params);
$importpresetbutton = new \single_button($importpresetlink,
get_string('importapreset', 'mod_data'), 'get', \single_button::BUTTON_SECONDARY, [
'data-action' => 'importpresets',
'data-dataid' => $cm->id,
]);
$data['importpresetbutton'] = $importpresetbutton->export_for_template($output);
}
return $data;
}
}