Skip to content

Commit

Permalink
Merge pull request #16 from cellule-tice/browserpersistance
Browse files Browse the repository at this point in the history
Browserpersistance
  • Loading branch information
cellule-tice authored Oct 9, 2019
2 parents ed3eb87 + b4e0815 commit 870844d
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 159 deletions.
13 changes: 8 additions & 5 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ moodle-format_collapsibletopics
Changes
-------

### v3.7.2
* 2019-10-09 - Transfer toggle state persistance from db to browser storage.

### v3.7.1
* 2019-08-21 - Add section progress bar feature (initiated by Chris Kenniburg)
* 2019-08-23 - Fix display of section 0 title to be consistent with core topics format (issue #1)

### v3.7

* 2019-05-20 - Remove support for legacy themes.
* 2019-08-20 - Add toggle icon for rtl languages.
* 2019-08-20 - Fix issue #12 Wrong item labels in drop targets list.

### v3.7.1
* 2019-08-21 - Add section progress bar feature (initiated by Chris Kenniburg)
* 2019-08-23 - Fix display of section 0 title to be consistent with core topics format (issue #1)
* 2019-08-20 - Fix issue #12 Wrong item labels in drop targets list.
2 changes: 1 addition & 1 deletion amd/build/collapsibletopics.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 62 additions & 10 deletions amd/src/collapsibletopics.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,54 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define(['jquery', 'core/log'], function($, log) {
define(['jquery', 'core/log', 'core/str'], function($, log, str) {

"use strict";

/**
* Update toggles state of current course in browser storage.
*/
var setState = function(course, toggles, storage) {
if (storage == 'local') {
window.localStorage.setItem('sections-toggle-' + course, JSON.stringify(toggles));
} else if (storage == 'session') {
window.sessionStorage.setItem('sections-toggle-' + course, JSON.stringify(toggles));
}
};

/**
* Update toggles state of current course in browser storage.
*/
var getState = function(course, storage) {
var toggles;
if (storage == 'local') {
toggles = window.localStorage.getItem('sections-toggle-' + course);
} else if (storage == 'session') {
toggles = window.sessionStorage.getItem('sections-toggle-' + course);
}
if (toggles === null) {
return {};
} else {
return JSON.parse(toggles);
}
};

return {
init: function($args) {
init: function(args) {
log.debug('Format collapsibletopics AMD module initialized');
$(document).ready(function($) {
var sectiontoggles = JSON.parse($args.sectionstoggle);
var sectiontoggles;
var keepstateoversession = args.keepstateoversession;
var storage;
if (keepstateoversession == 1) {
// Use browser local storage.
storage = 'local';
} else {
// Use browser session storage.
storage = 'session';
}

sectiontoggles = getState(args.course, storage);

setTimeout(function() {
for (var section in sectiontoggles) {
Expand All @@ -52,7 +91,7 @@ define(['jquery', 'core/log'], function($, log) {
$(section).collapse('show');
if (!sectiontoggles.hasOwnProperty(index + 1)) {
sectiontoggles[index + 1] = "true";
M.util.set_user_preference('sections-toggle-' + $args.course, JSON.stringify(sectiontoggles));
setState(args.course, sectiontoggles, storage);
}
});
});
Expand All @@ -66,7 +105,7 @@ define(['jquery', 'core/log'], function($, log) {
$(section).collapse('hide');
if (sectiontoggles.hasOwnProperty(index + 1)) {
delete sectiontoggles[index + 1];
M.util.set_user_preference('sections-toggle-' + $args.course, JSON.stringify(sectiontoggles));
setState(args.course, sectiontoggles, storage);
}
});
});
Expand All @@ -87,7 +126,7 @@ define(['jquery', 'core/log'], function($, log) {

if (!sectiontoggles.hasOwnProperty(sectionid)) {
sectiontoggles[sectionid] = "true";
M.util.set_user_preference('sections-toggle-' + $args.course, JSON.stringify(sectiontoggles));
setState(args.course, sectiontoggles, storage);
}
});

Expand All @@ -97,21 +136,34 @@ define(['jquery', 'core/log'], function($, log) {

if (sectiontoggles.hasOwnProperty(sectionid)) {
delete sectiontoggles[sectionid];
M.util.set_user_preference('sections-toggle-' + $args.course, JSON.stringify(sectiontoggles));
setState(args.course, sectiontoggles, storage);
}
});
$('body').on('click', '.togglecompletion button', function(event) {
//event.preventDefault();
var target = event.target;
var state = $(target).parent().parent().children('input[name="completionstate"]').val();
var section = ($(target).closest('li.section'));
var progressbar = $(section).find('.progress-bar');
var oldvalue = parseInt($(progressbar).attr('aria-valuenow'));
var newvalue = state == 1 ? oldvalue + 1 : oldvalue - 1;
var percent = (newvalue / parseInt($(progressbar).attr('aria-valuemax')) * 100);

var total = parseInt($(progressbar).attr('aria-valuemax'));
var percent = Math.round((newvalue / total * 100));
$(progressbar).attr('aria-valuenow', newvalue);
$(progressbar).attr('style', 'width: ' + percent + '%');
var strings = [
{
key: 'progresstotal',
component: 'completion',
param : {
complete: newvalue,
total: total
}
}
];
str.get_strings(strings).then(function(progress) {
$(progressbar).attr('data-original-title', progress);
});

});
});
}
Expand Down
58 changes: 0 additions & 58 deletions classes/observer.php

This file was deleted.

49 changes: 7 additions & 42 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Privacy Subsystem implementation for format_collapsibletopics.
*
* @package format_collapsibletopics
* @copyright 2018 - Cellule TICE - Unversite de Namur
* @copyright 2018 - Cellule TICE - Unversite de Namur
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace format_collapsibletopics\privacy;
Expand All @@ -38,50 +38,15 @@
*/
class provider implements
// This plugin has data.
\core_privacy\local\metadata\provider,

// This plugin stores user data.
\core_privacy\local\request\user_preference_provider {

/**
* Returns meta data about this system.
*
* @param collection $items The initialised item collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $items) : collection {
// There are several user preferences.
$items->add_user_preference('sections-toggle', 'privacy:metadata:preference:sectionstoggle');

return $items;
}
\core_privacy\local\metadata\null_provider {

/**
* Store all user preferences for the plugin.
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @param int $userid The userid of the user whose data is to be exported.
* @return string
*/
public static function export_user_preferences(int $userid) {

$preferences = get_user_preferences(null, null, $userid);
foreach ($preferences as $prefname => $prefvalue) {
$courseid = null;
if (strpos($prefname, 'sections-toggle-') === 0) {
$courseid = substr($prefname, 16);
$decodedprefvalue = (array)json_decode($prefvalue);
$sectionsarray = array_keys($decodedprefvalue);
$sections = implode(', ', $sectionsarray);
writer::export_user_preference(
'format_collapsibletopics',
$prefname,
$sections,
get_string('privacy:request:preference:sectionstoggle', 'format_collapsibletopics', (object) [
'name' => $courseid,
'value' => $sections
])
);
}
}

public static function get_reason() : string {
return 'privacy:metadata';
}
}
33 changes: 0 additions & 33 deletions db/events.php

This file was deleted.

46 changes: 46 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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/>.

/**
* Plugin upgrade steps are defined here.
*
* @package format_collapsibletopics
* @category upgrade
* @copyright 2018 - Cellule TICE - University of Namur
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* Execute format_collapsibletopics upgrade from the given old version.
*
* @param int $oldversion
* @return bool
*/
function xmldb_format_collapsibletopics_upgrade($oldversion) {

global $DB;

if ($oldversion < 2019100900) {
// Delete all records related to sections toggle state that is now handled by browser local/session storage.
$where = 'name like :pattern';
$DB->delete_records_select('user_preferences', $where, array('pattern' => 'sections-toggle%'));
upgrade_plugin_savepoint(true, 2019100900, 'format', 'collapsibletopics');
}

return true;
}
8 changes: 4 additions & 4 deletions format.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@

$renderer->print_multiple_section_page($course, null, null, null, null);

user_preference_allow_ajax_update('sections-toggle-' . $PAGE->course->id, PARAM_RAW);
$sectionstogglestate = get_user_preferences('sections-toggle-' . $PAGE->course->id, '{}');
$params = [
'course' => $course->id,
'keepstateoversession' => get_config('format_collapsibletopics', 'keepstateoversession')
];

$numsections = course_get_format($course)->get_last_section_number();
$params = array('course' => $PAGE->course->id, 'sectionstoggle' => $sectionstogglestate, 'numsections' => $numsections);
// Include course format js module.
$PAGE->requires->js('/course/format/collapsibletopics/format.js');
$PAGE->requires->js_call_amd('format_collapsibletopics/collapsibletopics', 'init', array($params));
Expand Down
Loading

0 comments on commit 870844d

Please sign in to comment.