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..2bfb8fe06b0 --- /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=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 new file mode 100644 index 00000000000..f6afc1b6193 --- /dev/null +++ b/blocks/myoverview/amd/src/tab_preferences.js @@ -0,0 +1,60 @@ +// 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 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: 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 f22ce15decb..8afd4a10471 100644 --- a/blocks/myoverview/block_myoverview.php +++ b/blocks/myoverview/block_myoverview.php @@ -50,7 +50,16 @@ class block_myoverview extends block_base { return $this->content; } - $renderable = new \block_myoverview\output\main(); + // 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); $renderer = $this->page->get_renderer('block_myoverview'); $this->content = new stdClass(); @@ -68,4 +77,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..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'); /** @@ -39,6 +40,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 +88,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/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 new file mode 100644 index 00000000000..10f084d6a1f --- /dev/null +++ b/blocks/myoverview/settings.php @@ -0,0 +1,39 @@ +. + +/** + * 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; + +require_once($CFG->dirroot . '/blocks/myoverview/lib.php'); + +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..e9b21bd69ae 100644 --- a/blocks/myoverview/templates/main.mustache +++ b/blocks/myoverview/templates/main.mustache @@ -24,26 +24,32 @@ }}
-