MDL-71790 calendar: Revamp the manage subscriptions page

This commit is contained in:
Huong Nguyen 2021-07-07 14:53:01 +07:00
parent ff035c0aa7
commit 48d21af177
10 changed files with 158 additions and 55 deletions

View File

@ -3101,6 +3101,7 @@ function calendar_import_icalendar_events($ical, $unused = null, $subscriptionid
}
}
$return .= html_writer::start_tag('ul');
$existing = $DB->get_field('event_subscriptions', 'lastupdated', ['id' => $subscriptionid]);
if (!empty($existing)) {
$eventsuuids = $DB->get_records_menu('event', ['subscriptionid' => $subscriptionid], '', 'id, uuid');
@ -3115,14 +3116,15 @@ function calendar_import_icalendar_events($ical, $unused = null, $subscriptionid
}
if (!empty($tobedeleted)) {
$DB->delete_records_list('event', 'id', $tobedeleted);
$return .= "<p> " . get_string('eventsdeleted', 'calendar') . ": " . count($tobedeleted) . "</p> ";
$return .= html_writer::tag('li', get_string('eventsdeleted', 'calendar', count($tobedeleted)));
}
}
}
$return .= "<p>" . get_string('eventsimported', 'calendar', $eventcount) . "</p> ";
$return .= "<p>" . get_string('eventsskipped', 'calendar', $skippedcount) . "</p> ";
$return .= "<p>" . get_string('eventsupdated', 'calendar', $updatecount) . "</p>";
$return .= html_writer::tag('li', get_string('eventsimported', 'calendar', $eventcount));
$return .= html_writer::tag('li', get_string('eventsskipped', 'calendar', $skippedcount));
$return .= html_writer::tag('li', get_string('eventsupdated', 'calendar', $updatecount));
$return .= html_writer::end_tag('ul');
return $return;
}
@ -3931,3 +3933,22 @@ function calendar_get_export_token(stdClass $user): string {
return sha1($user->id . $DB->get_field('user', 'password', ['id' => $user->id]) . $CFG->calendar_exportsalt);
}
/**
* Get the list of URL parameters for calendar expport and import links.
*
* @return array
*/
function calendar_get_export_import_link_params(): array {
global $PAGE;
$params = [];
if ($courseid = $PAGE->url->get_param('course')) {
$params['course'] = $courseid;
}
if ($categoryid = $PAGE->url->get_param('category')) {
$params['category'] = $categoryid;
}
return $params;
}

View File

@ -22,6 +22,8 @@
* @package calendar
*/
use core\notification;
require_once('../config.php');
require_once($CFG->libdir.'/bennu/bennu.inc.php');
require_once($CFG->dirroot.'/course/lib.php');
@ -69,6 +71,7 @@ if (!empty($subscriptionid)) {
if (calendar_can_edit_subscription($subscriptionid)) {
try {
$importresults = calendar_process_subscription_row($subscriptionid, $pollinterval, $action);
notification::add($importresults, \core\output\notification::NOTIFY_INFO);
} catch (moodle_exception $e) {
// If exception caught, then user should be redirected to page where he/she came from.
print_error($e->errorcode, $e->module, $PAGE->url);
@ -153,6 +156,7 @@ $PAGE->set_heading($course->fullname);
$renderer = $PAGE->get_renderer('core_calendar');
echo $OUTPUT->header();
echo $renderer->render_subscriptions_header();
// Filter subscriptions which user can't edit.
foreach($subscriptions as $subscription) {
@ -162,5 +166,9 @@ foreach($subscriptions as $subscription) {
}
// Display a table of subscriptions.
echo $renderer->subscription_details($courseid, $subscriptions, $importresults);
if (empty($subscription)) {
echo $renderer->render_no_calendar_subscriptions();
} else {
echo $renderer->subscription_details($courseid, $subscriptions);
}
echo $OUTPUT->footer();

View File

@ -308,15 +308,52 @@ class core_calendar_renderer extends plugin_renderer_base {
return $select;
}
/**
* Render the subscriptions header
*
* @return string
*/
public function render_subscriptions_header(): string {
$importcalendarbutton = new single_button(new moodle_url('/calendar/import.php', calendar_get_export_import_link_params()),
get_string('importcalendar', 'calendar'), 'get', true);
$importcalendarbutton->class .= ' float-sm-right float-right';
$exportcalendarbutton = new single_button(new moodle_url('/calendar/export.php', calendar_get_export_import_link_params()),
get_string('exportcalendar', 'calendar'), 'get', true);
$exportcalendarbutton->class .= ' float-sm-right float-right';
$output = $this->output->heading(get_string('managesubscriptions', 'calendar'));
$output .= html_writer::start_div('header d-flex flex-wrap mt-5');
$output .= html_writer::tag('h3', get_string('yoursubscriptions', 'calendar'), ['class' => 'mr-auto']);
$output .= $this->output->render($importcalendarbutton);
$output .= $this->output->render($exportcalendarbutton);
$output .= html_writer::end_div();
return $output;
}
/**
* Render the subscriptions blank state appearance
*
* @return string
*/
public function render_no_calendar_subscriptions(): string {
$output = html_writer::start_div('mt-5');
$importlink = html_writer::link((new moodle_url('/calendar/import.php', calendar_get_export_import_link_params()))->out(),
get_string('importcalendarexternal', 'calendar'));
$output .= get_string('nocalendarsubscriptions', 'calendar', $importlink);
$output .= html_writer::end_div();
return $output;
}
/**
* Renders a table containing information about calendar subscriptions.
*
* @param int $unused
* @param array $subscriptions
* @param string $importresults
* @param string $unused2
* @return string
*/
public function subscription_details($unused, $subscriptions, $importresults = '') {
public function subscription_details($unused, $subscriptions, $unused2 = '') {
$table = new html_table();
$table->head = array(
get_string('colcalendar', 'calendar'),
@ -325,9 +362,10 @@ class core_calendar_renderer extends plugin_renderer_base {
get_string('colpoll', 'calendar'),
get_string('colactions', 'calendar')
);
$table->align = array('left', 'left', 'left', 'center');
$table->align = array('left', 'left', 'left', 'left', 'left');
$table->width = '100%';
$table->data = array();
$table->id = 'subscription_details_table';
if (empty($subscriptions)) {
$cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
@ -346,26 +384,56 @@ class core_calendar_renderer extends plugin_renderer_base {
$lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
}
$cell = new html_table_cell($this->subscription_action_form($sub));
$cell->colspan = 2;
$type = $sub->eventtype . 'events';
$table->data[] = new html_table_row(array(
new html_table_cell($label),
new html_table_cell($lastupdated),
new html_table_cell(get_string($type, 'calendar')),
$cell
new html_table_cell($this->render_subscription_update_interval($sub)),
new html_table_cell($this->subscription_action_form($sub))
));
}
$out = $this->output->box_start('generalbox calendarsubs');
$out .= $importresults;
$out .= html_writer::table($table);
$out .= $this->output->box_end();
return $out;
}
/**
* Render subscription update interval form.
*
* @param stdClass $subscription
* @return string
*/
protected function render_subscription_update_interval(stdClass $subscription): string {
// Assemble form for the subscription row.
$output = html_writer::start_tag('form',
['action' => new moodle_url('/calendar/managesubscriptions.php', calendar_get_export_import_link_params()),
'method' => 'post']);
if (empty($subscription->url)) {
$output .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'pollinterval', 'value' => '0']);
} else {
// Assemble pollinterval control.
$output .= html_writer::start_div();
$output .= html_writer::start_tag('select', ['name' => 'pollinterval', 'class' => 'custom-select']);
foreach (calendar_get_pollinterval_choices() as $k => $v) {
$attributes = array();
if ($k == $subscription->pollinterval) {
$attributes['selected'] = 'selected';
}
$attributes['value'] = $k;
$output .= html_writer::tag('option', $v, $attributes);
}
$output .= html_writer::end_tag('select');
$output .= html_writer::end_div();
}
return $output;
}
/**
* Creates a form to perform actions on a given subscription.
*
@ -373,37 +441,17 @@ class core_calendar_renderer extends plugin_renderer_base {
* @return string
*/
protected function subscription_action_form($subscription) {
// Assemble form for the subscription row.
$html = html_writer::start_tag('form', ['action' => $this->page->url->out(false) , 'method' => 'post']);
if (empty($subscription->url)) {
// Don't update an iCal file, which has no URL.
$html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pollinterval', 'value' => '0'));
} else {
// Assemble pollinterval control.
$html .= html_writer::start_tag('div', array('style' => 'float:left;'));
$html .= html_writer::start_tag('select', array('name' => 'pollinterval', 'class' => 'custom-select'));
foreach (calendar_get_pollinterval_choices() as $k => $v) {
$attributes = array();
if ($k == $subscription->pollinterval) {
$attributes['selected'] = 'selected';
}
$attributes['value'] = $k;
$html .= html_writer::tag('option', $v, $attributes);
}
$html .= html_writer::end_tag('select');
$html .= html_writer::end_tag('div');
}
$html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
$html = html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
$html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
$html .= html_writer::start_tag('div', array('class' => 'btn-group float-right'));
$html .= html_writer::start_tag('div', array('class' => 'btn-group float-left'));
if (!empty($subscription->url)) {
$html .= html_writer::tag('button', get_string('update'), array('type' => 'submit', 'name' => 'action',
'class' => 'btn btn-secondary',
'value' => CALENDAR_SUBSCRIPTION_UPDATE));
$html .= html_writer::tag('button', get_string('update'), array('type' => 'submit', 'name' => 'action',
'class' => 'btn btn-link',
'value' => CALENDAR_SUBSCRIPTION_UPDATE));
}
$html .= html_writer::tag('button', get_string('remove'), array('type' => 'submit', 'name' => 'action',
'class' => 'btn btn-secondary',
'value' => CALENDAR_SUBSCRIPTION_REMOVE));
$html .= html_writer::tag('button', get_string('remove'), array('type' => 'submit', 'name' => 'action',
'class' => 'btn btn-link',
'value' => CALENDAR_SUBSCRIPTION_REMOVE));
$html .= html_writer::end_tag('div');
$html .= html_writer::end_tag('form');
return $html;

View File

@ -19,13 +19,14 @@ Feature: Import and edit calendar events
Given I log in as "teacher1"
And I view the calendar for "1" "2016"
And I click on "Manage subscriptions" "link"
And I press "Import calendar"
And I set the following fields to these values:
| Calendar name | Test Import |
| Import from | Calendar file (.ics) |
| Type of event | User |
And I upload "calendar/tests/fixtures/import.ics" file to "Calendar file (.ics)" filemanager
And I press "Add"
And I should see "Events imported: 2"
And I press "Import calendar"
And I should see "2 events were imported"
And I view the calendar for "2" "2017"
And I should see "February 2017"
And I should see "Event on 2-15-2017"
@ -52,25 +53,28 @@ Feature: Import and edit calendar events
Given I log in as "admin"
And I view the calendar for "1" "2016"
And I click on "Manage subscriptions" "link"
And I press "Import calendar"
And I set the following fields to these values:
| Calendar name | Test Import |
| Import from | Calendar file (.ics) |
| Type of event | User |
And I upload "calendar/tests/fixtures/import.ics" file to "Calendar file (.ics)" filemanager
And I press "Add"
And I press "Import calendar"
And I should see "User events"
And I press "Import calendar"
And I set the following fields to these values:
| Calendar name | Test Import |
| Import from | Calendar file (.ics) |
| Type of event | Category |
| Category | Miscellaneous |
And I upload "calendar/tests/fixtures/import.ics" file to "Calendar file (.ics)" filemanager
And I press "Add"
And I press "Import calendar"
And I should see "Category events"
And I press "Import calendar"
And I set the following fields to these values:
| Calendar name | Test Import |
| Import from | Calendar file (.ics) |
| Type of event | Site |
And I upload "calendar/tests/fixtures/import.ics" file to "Calendar file (.ics)" filemanager
And I press "Add"
And I press "Import calendar"
And I should see "Site events"

View File

@ -242,9 +242,9 @@ class core_calendar_lib_testcase extends advanced_testcase {
$sub = calendar_get_subscription($id);
$output = calendar_import_icalendar_events($ical, null, $sub->id);
$this->assertStringNotContainsString('Events deleted: 17', $output);
$this->assertStringContainsString('Events imported: 1', $output);
$this->assertStringContainsString('Events skipped: 0', $output);
$this->assertStringContainsString('Events updated: 0', $output);
$this->assertStringContainsString('1 events were imported', $output);
$this->assertStringContainsString('0 events were skipped', $output);
$this->assertStringContainsString('0 events were updated', $output);
}
/**

View File

@ -114,11 +114,11 @@ $string['eventnone'] = 'No events';
$string['eventrepeat'] = 'Repeats';
$string['events'] = 'Events';
$string['eventsall'] = 'All events';
$string['eventsdeleted'] = 'Events deleted';
$string['eventsimported'] = 'Events imported: {$a}';
$string['eventsdeleted'] = '{$a} events were deleted';
$string['eventsimported'] = '{$a} events were imported';
$string['eventsource'] = 'Event source';
$string['eventsskipped'] = 'Events skipped: {$a}';
$string['eventsupdated'] = 'Events updated: {$a}';
$string['eventsskipped'] = '{$a} events were skipped';
$string['eventsupdated'] = '{$a} events were updated';
$string['eventsfor'] = '{$a} events';
$string['eventskey'] = 'Events key';
$string['eventspersonal'] = 'My personal events';
@ -162,6 +162,7 @@ $string['eventtypeuser'] = 'user';
$string['hideeventtype'] = 'Hide {$a} events';
$string['showeventtype'] = 'Show {$a} events';
$string['hourly'] = 'Hourly';
$string['importcalendarexternal'] = 'Import an external calendar?';
$string['importcalendar'] = 'Import calendar';
$string['importcalendarheading'] = 'Import calendar...';
$string['importcalendarfrom'] = 'Import from';
@ -188,7 +189,7 @@ $string['never'] = 'Never';
$string['newevent'] = 'New event';
$string['notitle'] = 'no title';
$string['noupcomingevents'] = 'There are no upcoming events';
$string['nocalendarsubscriptions'] = 'You have no calendar subscriptions.';
$string['nocalendarsubscriptions'] = 'No calendar subscriptions yet. Do you want to {$a}';
$string['oneevent'] = '1 event';
$string['pollinterval'] = 'Update interval';
$string['pollinterval_help'] = 'How often you would like the calendar to update with new events.';
@ -235,7 +236,7 @@ $string['subscriptions'] = 'Subscriptions';
$string['subscriptionname'] = 'Calendar name';
$string['subscriptionremoved'] = 'Calendar subscription {$a} removed';
$string['subscriptionsource'] = 'Event source: {$a}';
$string['subscriptionupdated'] = 'Calendar subscription {$a} updated';
$string['subscriptionupdated'] = 'The \'{$a}\' calendar subscription has been updated';
$string['sun'] = 'Sun';
$string['sunday'] = 'Sunday';
$string['thu'] = 'Thu';
@ -275,6 +276,7 @@ $string['when'] = 'When';
$string['whendate'] = 'When: {$a}';
$string['yesterday'] = 'Yesterday';
$string['youcandeleteallrepeats'] = 'This event is part of a repeating event series. You can delete this event only, or all {$a} events in the series at once.';
$string['yoursubscriptions'] = 'Your Subscriptions';
// Deprecated since Moodle 3.8.
$string['global'] = 'Global';

View File

@ -58,7 +58,7 @@ class core_calendar_cron_task_testcase extends advanced_testcase {
$subscription->lastupdated = 0;
calendar_add_subscription($subscription);
$this->expectOutputRegex('/Events imported: .* Events skipped: .* Events updated:/');
$this->expectOutputRegex('/.* events were imported.* events were skipped.* events were updated/');
$task = new \core\task\calendar_cron_task();
$task->execute();
}

View File

@ -300,6 +300,16 @@ $calendarEventColor: #0d5ca1 !default;
}
}
table#subscription_details_table {
td {
vertical-align: middle;
> .btn-group button {
padding-left: 0;
}
}
}
}
// Calendar export.

View File

@ -13008,6 +13008,11 @@ input[disabled] {
word-break: break-all;
overflow-wrap: break-word; }
.path-calendar table#subscription_details_table td {
vertical-align: middle; }
.path-calendar table#subscription_details_table td > .btn-group button {
padding-left: 0; }
#page-calendar-export .indent {
padding-left: 20px; }

View File

@ -13230,6 +13230,11 @@ input[disabled] {
word-break: break-all;
overflow-wrap: break-word; }
.path-calendar table#subscription_details_table td {
vertical-align: middle; }
.path-calendar table#subscription_details_table td > .btn-group button {
padding-left: 0; }
#page-calendar-export .indent {
padding-left: 20px; }