Skip to content

Commit

Permalink
Merge branch 'MDL-75401-master-integration' of https://github.com/fer…
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Oct 20, 2022
2 parents de11a92 + 8305c1c commit 80d4981
Show file tree
Hide file tree
Showing 34 changed files with 891 additions and 126 deletions.
130 changes: 130 additions & 0 deletions lib/classes/output/sticky_footer.php
@@ -0,0 +1,130 @@
<?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 core\output;

use renderable;

/**
* Class to render a sticky footer element.
*
* Sticky footer can be rendered at any moment if the page (even inside a form) but
* it will be displayed at the bottom of the page.
*
* Important: note that pages can only display one sticky footer at once.
*
* Important: not all themes are compatible with sticky footer. If the current theme
* is not compatible it will be rendered as a standard div element.
*
* @package core
* @category output
* @copyright 2022 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class sticky_footer implements named_templatable, renderable {

/**
* @var string content of the sticky footer.
*/
protected $stickycontent = '';

/**
* @var string extra CSS classes. By default, elements are justified to the end.
*/
protected $stickyclasses = 'justify-content-end';

/**
* @var array extra HTML attributes (attribute => value).
*/
protected $attributes = [];

/**
* Constructor.
*
* @param string $stickycontent the footer content
* @param string|null $stickyclasses extra CSS classes
* @param array $attributes extra html attributes (attribute => value)
*/
public function __construct(string $stickycontent = '', ?string $stickyclasses = null, array $attributes = []) {
$this->stickycontent = $stickycontent;
if ($stickyclasses !== null) {
$this->stickyclasses = $stickyclasses;
}
$this->attributes = $attributes;
}

/**
* Set the footer contents.
*
* @param string $stickycontent the footer content
*/
public function set_content(string $stickycontent) {
$this->stickycontent = $stickycontent;
}

/**
* Add extra classes to the sticky footer.
*
* @param string $stickyclasses the extra classes
*/
public function add_classes(string $stickyclasses) {
if (!empty($this->stickyclasses)) {
$this->stickyclasses .= ' ';
}
$this->stickyclasses = $stickyclasses;
}

/**
* Add extra attributes to the sticky footer element.
*
* @param string $atribute the attribute
* @param string $value the value
*/
public function add_attribute(string $atribute, string $value) {
$this->attributes[$atribute] = $value;
}

/**
* 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) {
$extras = [];
foreach ($this->attributes as $attribute => $value) {
$extras[] = [
'attribute' => $attribute,
'value' => $value,
];
}
return [
'stickycontent' => (string)$this->stickycontent,
'stickyclasses' => $this->stickyclasses,
'extras' => $extras,
];
}

/**
* Get the name of the template to use for this templatable.
*
* @param \renderer_base $renderer The renderer requesting the template name
* @return string the template name
*/
public function get_template_name(\renderer_base $renderer): string {
return 'core/sticky_footer';
}
}
47 changes: 47 additions & 0 deletions lib/templates/sticky_footer.mustache
@@ -0,0 +1,47 @@
{{!
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 core/sticky_footer
Displays a page sticky footer element.
Sticky footer behaviour depends on the theme. The default template is
a regular element.
Example context (json):
{
"stickycontent" : "<a href=\"#\">Moodle</a>",
"extras" : [
{
"attribute" : "data-example",
"value" : "stickyfooter"
}
],
"stickyclasses" : "extraclasses"
}
}}
<div
id="sticky-footer"
class="{{$ stickyclasses }}{{stickyclasses}}{{/ stickyclasses }}"
{{#extras}}
{{attribute}}="{{value}}"
{{/extras}}
>
{{$ stickycontent }}
{{{stickycontent}}}
{{/ stickycontent }}
</div>
5 changes: 1 addition & 4 deletions mod/data/classes/output/view_action_bar.php
Expand Up @@ -58,15 +58,12 @@ public function __construct(int $id, \url_select $urlselect, bool $hasentries) {
* @return array
*/
public function export_for_template(\renderer_base $output): array {
global $PAGE, $DB;
global $PAGE;

$data = [
'urlselect' => $this->urlselect->export_for_template($output),
];

$addentrybutton = new add_entries_action($this->id);
$data['addentrybutton'] = $addentrybutton->export_for_template($output);

if (has_capability('mod/data:manageentries', $PAGE->context)) {
$importentrieslink = new moodle_url('/mod/data/import.php',
['d' => $this->id, 'backto' => $PAGE->url->out(false)]);
Expand Down
156 changes: 156 additions & 0 deletions mod/data/classes/output/view_footer.php
@@ -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' && $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 mr-1',
], true);
$data['selectall'] = $output->render($mastercheckbox);

$data['deleteselected'] = html_writer::empty_tag('input', [
'class' => 'btn btn-secondary',
'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', 'role' => 'button']
);
$data['addentrybutton'] = $addentrybutton->export_for_template($output);
}
return $output->render_from_template('mod_data/view_footer', $data);
}
}
9 changes: 9 additions & 0 deletions mod/data/classes/template.php
Expand Up @@ -215,6 +215,15 @@ protected function load_template_tags(string $templatecontent) {
$this->tags = $matches['tags'];
}

/**
* Return the current template name.
*
* @return string the template name
*/
public function get_template_name(): string {
return $this->templatename;
}

/**
* Generate the list of action icons.
*
Expand Down
4 changes: 3 additions & 1 deletion mod/data/edit.php
Expand Up @@ -217,7 +217,9 @@
]);
}

echo html_writer::div($actionbuttons, 'mdl-align mt-2');
$stickyfooter = new core\output\sticky_footer($actionbuttons);
echo $OUTPUT->render($stickyfooter);

echo $OUTPUT->box_end();
echo '</div></form>';

Expand Down

0 comments on commit 80d4981

Please sign in to comment.