Skip to content

Commit

Permalink
MDL-75137 mod_data: Redesign zero state page
Browse files Browse the repository at this point in the history
Co-authored-by: Ferran Recio <ferran@moodle.com>
  • Loading branch information
Amaia Anabitarte and ferranrecio committed Aug 12, 2022
1 parent cb01747 commit 8768a7a
Show file tree
Hide file tree
Showing 14 changed files with 527 additions and 20 deletions.
2 changes: 2 additions & 0 deletions lang/en/moodle.php
Expand Up @@ -38,6 +38,7 @@
$string['activityheader'] = 'Activity menu';
$string['activitymodule'] = 'Activity module';
$string['activitymodules'] = 'Activity modules';
$string['activitynotready'] = 'Activity not ready yet';
$string['activityreport'] = 'Activity report';
$string['activityreports'] = 'Activity reports';
$string['activityselect'] = 'Select this activity to be moved elsewhere';
Expand Down Expand Up @@ -267,6 +268,7 @@
$string['collapse'] = 'Collapse';
$string['collapseall'] = 'Collapse all';
$string['collapsecategory'] = 'Collapse {$a}';
$string['comebacklater'] = 'Please come back later.';
$string['commentincontext'] = 'Find this comment in context';
$string['comments'] = 'Comments';
$string['commentscount'] = 'Comments ({$a})';
Expand Down
13 changes: 13 additions & 0 deletions mod/data/classes/manager.php
Expand Up @@ -192,6 +192,19 @@ public function set_template_viewed() {
$event->trigger();
}

/**
* Return if the database has fields.
*
* @return bool true if the database has fields
*/
public function has_fields(): bool {
global $DB;
if ($this->_fieldrecords === null) {
return $DB->record_exists('data_fields', ['dataid' => $this->instance->id]);
}
return !empty($this->_fieldrecords);
}

/**
* Return the database fields.
*
Expand Down
67 changes: 67 additions & 0 deletions mod/data/classes/output/add_entries_action.php
@@ -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', true);
return $button->export_for_template($output);
}

return null;
}
}
70 changes: 70 additions & 0 deletions mod/data/classes/output/empty_database_action_bar.php
@@ -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', false);
$data['importentriesbutton'] = $importentriesbutton->export_for_template($output);
}

return $data;
}
}

12 changes: 2 additions & 10 deletions mod/data/classes/output/view_action_bar.php
Expand Up @@ -64,16 +64,8 @@ public function export_for_template(\renderer_base $output): array {
'urlselect' => $this->urlselect->export_for_template($output),
];

$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)]);
$addentrybutton = new \single_button($addentrylink, get_string('add', 'mod_data'), 'get', true);
$data['addentrybutton'] = $addentrybutton->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',
Expand Down
79 changes: 79 additions & 0 deletions mod/data/classes/output/zero_state_action_bar.php
@@ -0,0 +1,79 @@
<?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 (has_capability('mod/data:managetemplates', $PAGE->context)) {
$instance = $this->manager->get_instance();
$params = ['d' => $instance->id, 'backto' => $PAGE->url->out(false)];

$usepresetlink = new moodle_url('/mod/data/preset.php', $params);
$usepresetbutton = new \single_button($usepresetlink,
get_string('usepreset', 'mod_data'), 'get', true);
$data['usepresetbutton'] = $usepresetbutton->export_for_template($output);

$createfieldlink = new moodle_url('/mod/data/field.php', $params);
$createfieldbutton = new \single_button($createfieldlink,
get_string('newfield', 'mod_data'), 'get', false);
$data['createfieldbutton'] = $createfieldbutton->export_for_template($output);

$params['action'] = 'import';
$importpresetlink = new moodle_url('/mod/data/preset.php', $params);
$importpresetbutton = new \single_button($importpresetlink,
get_string('importpreset', 'mod_data'), 'get', false);
$data['importpresetbutton'] = $importpresetbutton->export_for_template($output);
}

return $data;
}
}

5 changes: 4 additions & 1 deletion mod/data/lang/en/data.php
Expand Up @@ -78,6 +78,7 @@
$string['confirmdeletefield'] = 'You are about to delete this field, are you sure?';
$string['confirmdeleterecord'] = 'Are you sure you want to delete this entry?';
$string['confirmdeleterecords'] = 'Are you sure you want to delete these entries?';
$string['createfields'] = 'Create your own fields to collect data, or use a preset which includes fields already.';
$string['csstemplate'] = 'CSS template';
$string['csvfailed'] = 'Unable to read the raw data from the CSV file';
$string['csvfile'] = 'CSV file';
Expand Down Expand Up @@ -293,7 +294,7 @@
$string['nolisttemplate'] = 'List template is not yet defined';
$string['nomatch'] = 'No matching entries found!';
$string['nomaximum'] = 'No maximum';
$string['norecords'] = 'No entries in database';
$string['norecords'] = 'No entries yet';
$string['nosingletemplate'] = 'Single template is not yet defined';
$string['notapproved'] = 'Entry is not approved yet.';
$string['notinjectivemap'] = 'Not an injective map';
Expand Down Expand Up @@ -389,6 +390,7 @@
$string['single'] = 'View single';
$string['singleview'] = 'Single view';
$string['singletemplate'] = 'Single template';
$string['startbuilding'] = 'Start building your activity';
$string['subplugintype_datafield'] = 'Database field type';
$string['subplugintype_datafield_plural'] = 'Database field types';
$string['subplugintype_datapreset'] = 'Preset';
Expand All @@ -407,6 +409,7 @@
$string['todatabase'] = 'to this database.';
$string['type'] = 'Field type';
$string['undefinedprocessactionmethod'] = 'No action method defined in Data_Preset to handle action "{$a}".';
$string['underconstruction_title'] = 'Under construction';
$string['unsupportedfields'] = 'Unsupported fields';
$string['unsupportedfieldslist'] = 'The following fields cannot be exported:';
$string['updatefield'] = 'Update an existing field';
Expand Down
41 changes: 41 additions & 0 deletions mod/data/pix/nofields.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions mod/data/renderer.php
Expand Up @@ -139,4 +139,42 @@ public function render_presets(\mod_data\output\presets $presets): string {
$data = $presets->export_for_template($this);
return $this->render_from_template('mod_data/presets', $data);
}

/**
* Renders the action bar for the zero state (no fields created) page.
*
* @param \mod_data\manager $manager The manager instance.
*
* @return string The HTML output
*/
public function render_zero_state(\mod_data\manager $manager): string {
$actionbar = new \mod_data\output\zero_state_action_bar($manager);
$data = $actionbar->export_for_template($this);
if (empty($data)) {
// No actions for the user.
$data['title'] = get_string('activitynotready');
$data['intro'] = get_string('comebacklater');
} else {
$data['title'] = get_string('startbuilding', 'mod_data');
$data['intro'] = get_string('createfields', 'mod_data');
}
$data['noitemsimgurl'] = $this->output->image_url('nofields', 'mod_data')->out();

return $this->render_from_template('mod_data/zero_state', $data);
}

/**
* Renders the action bar for an empty database view page.
*
* @param \mod_data\manager $manager The manager instance.
*
* @return string The HTML output
*/
public function render_empty_database(\mod_data\manager $manager): string {
$actionbar = new \mod_data\output\empty_database_action_bar($manager);
$data = $actionbar->export_for_template($this);
$data['noitemsimgurl'] = $this->output->image_url('nofields', 'mod_data')->out();

return $this->render_from_template('mod_data/view_noentries', $data);
}
}

0 comments on commit 8768a7a

Please sign in to comment.