Merge branch 'MDL-58907_master' of git://github.com/markn86/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2017-06-13 01:02:35 +02:00 committed by Dan Poltawski
commit bf3b9e57b8
10 changed files with 213 additions and 8 deletions

View File

@ -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}});

View File

@ -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 <http://www.gnu.org/licenses/>.
/**
* Javascript used to save the user's tab preference.
*
* @package block_myoverview
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @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
};
});

View File

@ -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;
}
}

View File

@ -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
];
}
}

View File

@ -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';

52
blocks/myoverview/lib.php Normal file
View File

@ -0,0 +1,52 @@
<?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/>.
/**
* Contains functions called by core.
*
* @package block_myoverview
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @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;
}

View File

@ -0,0 +1,39 @@
<?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/>.
/**
* Settings for the overview block.
*
* @package block_myoverview
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @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));
}

View File

@ -24,26 +24,32 @@
}}
<div id="block-myoverview-{{uniqid}}" class="block-myoverview" data-region="myoverview">
<ul class="nav nav-tabs" role="tablist">
<ul id="block-myoverview-view-choices-{{uniqid}}" class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" href="#myoverview_timeline_view" role="tab" data-toggle="tab">
<a class="nav-link {{#viewingtimeline}}active{{/viewingtimeline}}" href="#myoverview_timeline_view" role="tab" data-toggle="tab" data-tabname="timeline">
{{#str}} timeline, block_myoverview {{/str}}
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#myoverview_courses_view" role="tab" data-toggle="tab">
<a class="nav-link {{#viewingcourses}}active{{/viewingcourses}}" href="#myoverview_courses_view" role="tab" data-toggle="tab" data-tabname="courses">
{{#str}} courses {{/str}}
</a>
</li>
</ul>
<div class="tab-content content-centred">
<div role="tabpanel" class="tab-pane fade in active" id="myoverview_timeline_view">
<div role="tabpanel" class="tab-pane fade {{#viewingtimeline}}in active{{/viewingtimeline}}" id="myoverview_timeline_view">
{{> block_myoverview/timeline-view }}
</div>
<div role="tabpanel" class="tab-pane fade" id="myoverview_courses_view">
<div role="tabpanel" class="tab-pane fade {{#viewingcourses}}in active{{/viewingcourses}}" id="myoverview_courses_view">
{{#coursesview}}
{{> block_myoverview/courses-view }}
{{/coursesview}}
</div>
</div>
</div>
{{#js}}
require(['jquery', 'block_myoverview/tab_preferences'], function($, TabPreferences) {
var root = $('#block-myoverview-view-choices-{{uniqid}}');
TabPreferences.registerEventListeners(root);
});
{{/js}}

View File

@ -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"

View File

@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2017051500; // 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).