From ff6cd55d7922ffc8dc997eaa9c461f5180a49d55 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Tue, 6 Jun 2017 16:40:11 +0800 Subject: [PATCH 1/3] MDL-58907 block_myoverview: added setting to select default tab --- blocks/myoverview/block_myoverview.php | 23 +++++++++++- blocks/myoverview/classes/output/main.php | 27 +++++++++++++- .../myoverview/lang/en/block_myoverview.php | 2 + blocks/myoverview/settings.php | 37 +++++++++++++++++++ blocks/myoverview/templates/main.mustache | 8 ++-- blocks/myoverview/version.php | 2 +- 6 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 blocks/myoverview/settings.php diff --git a/blocks/myoverview/block_myoverview.php b/blocks/myoverview/block_myoverview.php index f22ce15decb..f3885a1a81b 100644 --- a/blocks/myoverview/block_myoverview.php +++ b/blocks/myoverview/block_myoverview.php @@ -24,6 +24,16 @@ defined('MOODLE_INTERNAL') || die(); +/** + * The timeline view. + */ +define('BLOCK_MYOVERVIEW_TIMELINE_VIEW', 'timeline'); + +/** + * The courses view. + */ +define('BLOCK_MYOVERVIEW_COURSES_VIEW', 'courses'); + /** * My overview block class. * @@ -50,7 +60,9 @@ class block_myoverview extends block_base { return $this->content; } - $renderable = new \block_myoverview\output\main(); + $config = get_config('block_myoverview'); + + $renderable = new \block_myoverview\output\main($config->defaulttab); $renderer = $this->page->get_renderer('block_myoverview'); $this->content = new stdClass(); @@ -68,4 +80,13 @@ class block_myoverview extends block_base { public function applicable_formats() { return array('my' => true); } + + /** + * This block does contain a configuration settings. + * + * @return boolean + */ + public function has_config() { + return true; + } } diff --git a/blocks/myoverview/classes/output/main.php b/blocks/myoverview/classes/output/main.php index 6215a5a372d..bf3268e814f 100644 --- a/blocks/myoverview/classes/output/main.php +++ b/blocks/myoverview/classes/output/main.php @@ -39,6 +39,20 @@ require_once($CFG->libdir . '/completionlib.php'); */ class main implements renderable, templatable { + /** + * @var string The tab to display. + */ + public $tab; + + /** + * Constructor. + * + * @param string $tab The tab to display. + */ + public function __construct($tab) { + $this->tab = $tab; + } + /** * Export this data so it can be used as the context for a mustache template. * @@ -73,13 +87,24 @@ class main implements renderable, templatable { $nocoursesurl = $output->image_url('courses', 'block_myoverview')->out(); $noeventsurl = $output->image_url('activities', 'block_myoverview')->out(); + // Now, set the tab we are going to be viewing. + $viewingtimeline = false; + $viewingcourses = false; + if ($this->tab == BLOCK_MYOVERVIEW_TIMELINE_VIEW) { + $viewingtimeline = true; + } else { + $viewingcourses = true; + } + return [ 'midnight' => usergetmidnight(time()), 'coursesview' => $coursesview->export_for_template($output), 'urls' => [ 'nocourses' => $nocoursesurl, 'noevents' => $noeventsurl - ] + ], + 'viewingtimeline' => $viewingtimeline, + 'viewingcourses' => $viewingcourses ]; } } diff --git a/blocks/myoverview/lang/en/block_myoverview.php b/blocks/myoverview/lang/en/block_myoverview.php index 4c464f57c0b..99fb83f8864 100644 --- a/blocks/myoverview/lang/en/block_myoverview.php +++ b/blocks/myoverview/lang/en/block_myoverview.php @@ -22,6 +22,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string['defaulttab'] = 'Default tab'; +$string['defaulttab_desc'] = 'This is the default tab that will be shown to a user.'; $string['future'] = 'Future'; $string['inprogress'] = 'In progress'; $string['morecourses'] = 'More courses'; diff --git a/blocks/myoverview/settings.php b/blocks/myoverview/settings.php new file mode 100644 index 00000000000..f545d17328c --- /dev/null +++ b/blocks/myoverview/settings.php @@ -0,0 +1,37 @@ +. + +/** + * Settings for the overview block. + * + * @package block_myoverview + * @copyright 2017 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +if ($ADMIN->fulltree) { + + $options = [ + BLOCK_MYOVERVIEW_TIMELINE_VIEW => get_string('timeline', 'block_myoverview'), + BLOCK_MYOVERVIEW_COURSES_VIEW => get_string('courses') + ]; + + $settings->add(new admin_setting_configselect('block_myoverview/defaulttab', + get_string('defaulttab', 'block_myoverview'), + get_string('defaulttab_desc', 'block_myoverview'), 'timeline', $options)); +} diff --git a/blocks/myoverview/templates/main.mustache b/blocks/myoverview/templates/main.mustache index 3a1a942c349..74bae299fa4 100644 --- a/blocks/myoverview/templates/main.mustache +++ b/blocks/myoverview/templates/main.mustache @@ -26,21 +26,21 @@
-
+
{{> block_myoverview/timeline-view }}
-
+
{{#coursesview}} {{> block_myoverview/courses-view }} {{/coursesview}} diff --git a/blocks/myoverview/version.php b/blocks/myoverview/version.php index fd45f7709a0..0452a78c820 100644 --- a/blocks/myoverview/version.php +++ b/blocks/myoverview/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2017051500; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2017051501; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2017050500; // Requires this Moodle version. $plugin->component = 'block_myoverview'; // Full name of the plugin (used for diagnostics). From 4dfb8cf060352d7fb5ef02a98195419253f9baa8 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Tue, 6 Jun 2017 19:14:46 +0800 Subject: [PATCH 2/3] MDL-58907 block_myoverview: display last tab user was on --- .../amd/build/tab_preferences.min.js | 1 + blocks/myoverview/amd/src/tab_preferences.js | 54 +++++++++++++++++++ blocks/myoverview/block_myoverview.php | 18 +++---- blocks/myoverview/classes/output/main.php | 1 + blocks/myoverview/lib.php | 52 ++++++++++++++++++ blocks/myoverview/settings.php | 2 + blocks/myoverview/templates/main.mustache | 12 +++-- .../behat/block_myoverview_progress.feature | 1 + blocks/myoverview/version.php | 2 +- 9 files changed, 127 insertions(+), 16 deletions(-) create mode 100644 blocks/myoverview/amd/build/tab_preferences.min.js create mode 100644 blocks/myoverview/amd/src/tab_preferences.js create mode 100644 blocks/myoverview/lib.php diff --git a/blocks/myoverview/amd/build/tab_preferences.min.js b/blocks/myoverview/amd/build/tab_preferences.min.js new file mode 100644 index 00000000000..7b3b97f4533 --- /dev/null +++ b/blocks/myoverview/amd/build/tab_preferences.min.js @@ -0,0 +1 @@ +define(["jquery","core/ajax","core/custom_interaction_events"],function(a,b,c){var d=function(d){c.define(d,[c.events.activate]),d.on(c.events.activate,"[data-toggle='tab']",function(c){var d={methodname:"core_user_update_user_preferences",args:{preferences:[{type:"block_myoverview_last_tab",value:a(c.currentTarget).data("tabname")}]}};b.call([d])[0].fail(Notification.exception)})};return{registerEventListeners:d}}); \ No newline at end of file diff --git a/blocks/myoverview/amd/src/tab_preferences.js b/blocks/myoverview/amd/src/tab_preferences.js new file mode 100644 index 00000000000..690e31ad39a --- /dev/null +++ b/blocks/myoverview/amd/src/tab_preferences.js @@ -0,0 +1,54 @@ +// 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 . + +/** + * Javascript used to save the user's tab preference. + * + * @package block_myoverview + * @copyright 2017 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +define(['jquery', 'core/ajax', 'core/custom_interaction_events'], function($, Ajax, CustomEvents) { + + /** + * Registers an event that saves the user's tab preference when switching between them. + * + * @param {object} root The container element + */ + var registerEventListeners = function(root) { + CustomEvents.define(root, [CustomEvents.events.activate]); + root.on(CustomEvents.events.activate, "[data-toggle='tab']", function(e) { + var request = { + methodname: 'core_user_update_user_preferences', + args: { + preferences: [ + { + type: 'block_myoverview_last_tab', + value: $(e.currentTarget).data('tabname') + } + ] + } + }; + + Ajax.call([request])[0] + .fail(Notification.exception); + }); + }; + + return { + registerEventListeners: registerEventListeners + }; +}); diff --git a/blocks/myoverview/block_myoverview.php b/blocks/myoverview/block_myoverview.php index f3885a1a81b..c18ccf5a436 100644 --- a/blocks/myoverview/block_myoverview.php +++ b/blocks/myoverview/block_myoverview.php @@ -24,16 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -/** - * The timeline view. - */ -define('BLOCK_MYOVERVIEW_TIMELINE_VIEW', 'timeline'); - -/** - * The courses view. - */ -define('BLOCK_MYOVERVIEW_COURSES_VIEW', 'courses'); - /** * My overview block class. * @@ -60,9 +50,13 @@ class block_myoverview extends block_base { return $this->content; } - $config = get_config('block_myoverview'); + // Wasn't set as a user preference so get the site setting. + if (!$tab = get_user_preferences('block_myoverview_last_tab')) { + $config = get_config('block_myoverview'); + $tab = $config->defaulttab; + } - $renderable = new \block_myoverview\output\main($config->defaulttab); + $renderable = new \block_myoverview\output\main($tab); $renderer = $this->page->get_renderer('block_myoverview'); $this->content = new stdClass(); diff --git a/blocks/myoverview/classes/output/main.php b/blocks/myoverview/classes/output/main.php index bf3268e814f..2435f54c4f7 100644 --- a/blocks/myoverview/classes/output/main.php +++ b/blocks/myoverview/classes/output/main.php @@ -29,6 +29,7 @@ use renderer_base; use templatable; use core_completion\progress; +require_once($CFG->dirroot . '/blocks/myoverview/lib.php'); require_once($CFG->libdir . '/completionlib.php'); /** diff --git a/blocks/myoverview/lib.php b/blocks/myoverview/lib.php new file mode 100644 index 00000000000..a73db2566ac --- /dev/null +++ b/blocks/myoverview/lib.php @@ -0,0 +1,52 @@ +. + +/** + * Contains functions called by core. + * + * @package block_myoverview + * @copyright 2017 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * The timeline view. + */ +define('BLOCK_MYOVERVIEW_TIMELINE_VIEW', 'timeline'); + +/** + * The courses view. + */ +define('BLOCK_MYOVERVIEW_COURSES_VIEW', 'courses'); + +/** + * Returns the name of the user preferences as well as the details this plugin uses. + * + * @return array + */ +function block_myoverview_user_preferences() { + $preferences = array(); + $preferences['block_myoverview_last_tab'] = array( + 'type' => PARAM_ALPHA, + 'null' => NULL_NOT_ALLOWED, + 'default' => BLOCK_MYOVERVIEW_TIMELINE_VIEW, + 'choices' => array(BLOCK_MYOVERVIEW_TIMELINE_VIEW, BLOCK_MYOVERVIEW_COURSES_VIEW) + ); + + return $preferences; +} diff --git a/blocks/myoverview/settings.php b/blocks/myoverview/settings.php index f545d17328c..10f084d6a1f 100644 --- a/blocks/myoverview/settings.php +++ b/blocks/myoverview/settings.php @@ -24,6 +24,8 @@ defined('MOODLE_INTERNAL') || die; +require_once($CFG->dirroot . '/blocks/myoverview/lib.php'); + if ($ADMIN->fulltree) { $options = [ diff --git a/blocks/myoverview/templates/main.mustache b/blocks/myoverview/templates/main.mustache index 74bae299fa4..e9b21bd69ae 100644 --- a/blocks/myoverview/templates/main.mustache +++ b/blocks/myoverview/templates/main.mustache @@ -24,14 +24,14 @@ }}
+{{#js}} +require(['jquery', 'block_myoverview/tab_preferences'], function($, TabPreferences) { + var root = $('#block-myoverview-view-choices-{{uniqid}}'); + TabPreferences.registerEventListeners(root); +}); +{{/js}} diff --git a/blocks/myoverview/tests/behat/block_myoverview_progress.feature b/blocks/myoverview/tests/behat/block_myoverview_progress.feature index 8bd3afe7f3e..e8d86921d4a 100644 --- a/blocks/myoverview/tests/behat/block_myoverview_progress.feature +++ b/blocks/myoverview/tests/behat/block_myoverview_progress.feature @@ -53,6 +53,7 @@ Feature: Course overview block show users their progress on courses And I am on "Course 1" course homepage And I follow "Test choice 1" And I follow "Dashboard" in the user menu + And I click on "Timeline" "link" in the "Course overview" "block" And I click on "Sort by courses" "link" in the "Course overview" "block" And I should see "100%" in the "Course overview" "block" And I click on "Courses" "link" in the "Course overview" "block" diff --git a/blocks/myoverview/version.php b/blocks/myoverview/version.php index 0452a78c820..a637f485322 100644 --- a/blocks/myoverview/version.php +++ b/blocks/myoverview/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2017051501; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2017051502; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2017050500; // Requires this Moodle version. $plugin->component = 'block_myoverview'; // Full name of the plugin (used for diagnostics). From 0ecb3d77863a018ef8fc92cff944d57e821754d8 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 7 Jun 2017 17:37:29 +0800 Subject: [PATCH 3/3] MDL-58907 block_myoverview: change URL when tab is selected We then check if this paramter exists in the URL and if it does we display the specified tab. --- blocks/myoverview/amd/build/tab_preferences.min.js | 2 +- blocks/myoverview/amd/src/tab_preferences.js | 8 +++++++- blocks/myoverview/block_myoverview.php | 11 +++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/blocks/myoverview/amd/build/tab_preferences.min.js b/blocks/myoverview/amd/build/tab_preferences.min.js index 7b3b97f4533..2bfb8fe06b0 100644 --- a/blocks/myoverview/amd/build/tab_preferences.min.js +++ b/blocks/myoverview/amd/build/tab_preferences.min.js @@ -1 +1 @@ -define(["jquery","core/ajax","core/custom_interaction_events"],function(a,b,c){var d=function(d){c.define(d,[c.events.activate]),d.on(c.events.activate,"[data-toggle='tab']",function(c){var d={methodname:"core_user_update_user_preferences",args:{preferences:[{type:"block_myoverview_last_tab",value:a(c.currentTarget).data("tabname")}]}};b.call([d])[0].fail(Notification.exception)})};return{registerEventListeners:d}}); \ No newline at end of file +define(["jquery","core/ajax","core/custom_interaction_events"],function(a,b,c){var d=function(d){c.define(d,[c.events.activate]),d.on(c.events.activate,"[data-toggle='tab']",function(c){var d=a(c.currentTarget).data("tabname");"function"==typeof window.history.pushState&&window.history.pushState(null,null,"?myoverviewtab="+d);var e={methodname:"core_user_update_user_preferences",args:{preferences:[{type:"block_myoverview_last_tab",value:d}]}};b.call([e])[0].fail(Notification.exception)})};return{registerEventListeners:d}}); \ No newline at end of file diff --git a/blocks/myoverview/amd/src/tab_preferences.js b/blocks/myoverview/amd/src/tab_preferences.js index 690e31ad39a..f6afc1b6193 100644 --- a/blocks/myoverview/amd/src/tab_preferences.js +++ b/blocks/myoverview/amd/src/tab_preferences.js @@ -31,13 +31,19 @@ define(['jquery', 'core/ajax', 'core/custom_interaction_events'], function($, Aj var registerEventListeners = function(root) { CustomEvents.define(root, [CustomEvents.events.activate]); root.on(CustomEvents.events.activate, "[data-toggle='tab']", function(e) { + var tabname = $(e.currentTarget).data('tabname'); + // Bootstrap does not change the URL when using BS tabs, so need to do this here. + // Also check to make sure the browser supports the history API. + if (typeof window.history.pushState === "function") { + window.history.pushState(null, null, '?myoverviewtab=' + tabname); + } var request = { methodname: 'core_user_update_user_preferences', args: { preferences: [ { type: 'block_myoverview_last_tab', - value: $(e.currentTarget).data('tabname') + value: tabname } ] } diff --git a/blocks/myoverview/block_myoverview.php b/blocks/myoverview/block_myoverview.php index c18ccf5a436..8afd4a10471 100644 --- a/blocks/myoverview/block_myoverview.php +++ b/blocks/myoverview/block_myoverview.php @@ -50,10 +50,13 @@ class block_myoverview extends block_base { return $this->content; } - // Wasn't set as a user preference so get the site setting. - if (!$tab = get_user_preferences('block_myoverview_last_tab')) { - $config = get_config('block_myoverview'); - $tab = $config->defaulttab; + // Check if the tab to select wasn't passed in the URL, if so see if the user has any preference. + if (!$tab = optional_param('myoverviewtab', null, PARAM_ALPHA)) { + // Check if the user has no preference, if so get the site setting. + if (!$tab = get_user_preferences('block_myoverview_last_tab')) { + $config = get_config('block_myoverview'); + $tab = $config->defaulttab; + } } $renderable = new \block_myoverview\output\main($tab);