Skip to content

Commit

Permalink
V2.7.1.3 - CONTRIB-5211
Browse files Browse the repository at this point in the history
  • Loading branch information
gjb2048 committed Aug 18, 2014
1 parent 88f1cae commit 6f3697c
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 17 deletions.
9 changes: 5 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Course Backup and Restore Instructions
======================================
1. Backup as you would any other course. The layout configuration will be stored with the course settings.
2. Restore as you would any other course. If you are offered the option of 'Overwrite Course Configuration'
you must say 'Yes' to have thelayout configuration restored otherwise the restored course will retain the
you must say 'Yes' to have the layout configuration restored otherwise the restored course will retain the
layout it previously had or the default in the 'config.php' file as mentioned in the 'Installation'
instructions above depending on the situation.
3. Note: I believe that if you restore a Collapsed Topic's course on an installation that does not have the
Expand Down Expand Up @@ -243,8 +243,9 @@ can be really useful in visualising the issue along with any files you consider

Version Information
===================
18th July 2014 Version 2.7.1.3
18th August 2014 Version 2.7.1.3
1. Fixed a few typos.
2. Fixed CONTRIB-5211: Section 5 does not remain expanded when browsing away and back.

17th July 2014 Version 2.7.1.2
1. Slight tweak to css to tidy things up when editing.
Expand Down Expand Up @@ -589,7 +590,7 @@ Change by G J Barnard
Thanks to [Rick Jerz](https://moodle.org/user/profile.php?id=520965) for reporting this.

18th October 2012 - Version 2.3.9.1
1. Fixed potenial issue when the course is first accessed and there is no user preference.
1. Fixed potential issue when the course is first accessed and there is no user preference.
2. Identified that sometimes when restoring a course, it is accessed for the first time and a toggle is clicked a 'Error
updating user preference 'topcoll_toggle_x'' (where 'x' is the course id as shown in the URL 'id=x') can occur. I'm not
completely sure why this is happening as the 'user_preference_allow_ajax_update' call in 'format.php' should establish that
Expand Down Expand Up @@ -1116,6 +1117,6 @@ Desired Enhancements

Me
==
G J Barnard MSc. BSc(Hons)(Sndw). MBCS. CEng. CITP. PGCE. - 18th July 2014.
G J Barnard MSc. BSc(Hons)(Sndw). MBCS. CEng. CITP. PGCE. - 18th August 2014.
Moodle profile: http://moodle.org/user/profile.php?id=442195.
Web profile : http://about.me/gjbarnard
4 changes: 2 additions & 2 deletions format.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @copyright © 2009-onwards G J Barnard in respect to modifications of standard topics format.
* @author G J Barnard - gjbarnard at gmail dot com and {@link http://moodle.org/user/profile.php?id=442195}
* @link http://docs.moodle.org/en/Collapsed_Topics_course_format
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
defined('MOODLE_INTERNAL') || die();
Expand Down Expand Up @@ -85,7 +85,7 @@
if (!empty($displaysection)) {
$renderer->print_single_section_page($course, null, null, null, null, $displaysection);
} else {
user_preference_allow_ajax_update('topcoll_toggle_' . $course->id, PARAM_TEXT);
user_preference_allow_ajax_update('topcoll_toggle_' . $course->id, PARAM_TOPCOLL);
$userpreference = get_user_preferences('topcoll_toggle_' . $course->id);
$renderer->set_user_preference($userpreference);

Expand Down
41 changes: 36 additions & 5 deletions module.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ M.format_topcoll.to2baseString = function(thirtysix) {
M.format_topcoll.save_toggles = function() {
"use strict";
if (this.togglePersistence == 1) { // Toggle persistence - 1 = on, 0 = off.
M.util.set_user_preference('topcoll_toggle_' + this.courseid, this.togglestate);
M.format_topcoll.set_user_preference('topcoll_toggle_' + this.courseid, this.togglestate);
}
};

Expand Down Expand Up @@ -260,7 +260,6 @@ M.format_topcoll.set_toggle_state = function(togglenum, state) {
value &= ~toggleflag;
}
var newchar = this.encode_value_to_character(value);
//this.togglestate[togglecharpos-1] = newchar;
var start = this.togglestate.substring(0,togglecharpos - 1);
var end = this.togglestate.substring(togglecharpos);
this.togglestate = start + newchar + end;
Expand All @@ -278,12 +277,12 @@ M.format_topcoll.get_toggle_pos = function(togglenum) {

M.format_topcoll.get_min_digit = function() {
"use strict";
return ':';
return ':'; // 58 ':';
};

M.format_topcoll.get_max_digit = function() {
"use strict";
return 'y';
return 'y'; // 58 'y';
};

M.format_topcoll.get_toggle_flag = function(togglenum, togglecharpos) {
Expand Down Expand Up @@ -321,4 +320,36 @@ M.format_topcoll.decode_character_to_value = function(character) {
M.format_topcoll.encode_value_to_character = function(val) {
"use strict";
return String.fromCharCode(val + 58);
};
};

/**
* Makes a best effort to connect back to Moodle to update a user preference,
* however, there is no mechanism for finding out if the update succeeded.
*
* Before you can use this function in your JavsScript, you must have called
* user_preference_allow_ajax_update from moodlelib.php to tell Moodle that
* the update is allowed, and how to safely clean and submitted values.
*
* @param String name the name of the setting to update.
* @param String the value to set it to.
*/
M.format_topcoll.set_user_preference = function(name, value) {
YUI().use('io', function(Y) {
var url = M.cfg.wwwroot + '/course/format/topcoll/settopcollpref.php?sesskey=' +
M.cfg.sesskey + '&pref=' + encodeURI(name) + '&value=' + encodeURI(value);

// If we are a developer, ensure that failures are reported.
var cfg = {
method: 'get',
on: {}
};
if (M.cfg.developerdebug) {
cfg.on.failure = function(id, o, args) {
console.log("Error updating topcoll preference '" + name + "' using AJAX. Almost certainly your session has timed out. Clicking this link will repeat the AJAX call that failed so you can see the error: ");
}
}

// Make the request.
Y.io(url, cfg);
});
};
54 changes: 54 additions & 0 deletions settopcollpref.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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/>.

/**
* Code to update a user preference in response to an ajax call.
*
* You should not send requests to this script directly. Instead use the set_user_preference
* function in /course/format/topcol/module.js.
*
* @package course/format
* @subpackage topcoll
* @version See the value of '$plugin->version' in below.
* @copyright &copy; 2014-onwards G J Barnard based upon code originally written by Tim Hunt.
* @author G J Barnard - gjbarnard at gmail dot com and {@link http://moodle.org/user/profile.php?id=442195}
* @link http://docs.moodle.org/en/Collapsed_Topics_course_format
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once(dirname(__FILE__) . '/../../../config.php');
require_once($CFG->dirroot . '/course/format/topcoll/togglelib.php');

// Check access.
if (!confirm_sesskey()) {
print_error('invalidsesskey');
}

// Get the name of the preference to update, and check that it is allowed.
$name = required_param('pref', PARAM_RAW);
if (!isset($USER->ajax_updatable_user_prefs[$name])) {
print_error('notallowedtoupdateprefremotely');
}

// Get and set the value.
$value = required_topcoll_param('value', $USER->ajax_updatable_user_prefs[$name]);
error_log($name.' is before: '.$USER->ajax_updatable_user_prefs[$name].' after: '.$value);
// Update
if (!set_user_preference($name, $value)) {
print_error('errorsettinguserpref');
}

echo 'OK';
57 changes: 57 additions & 0 deletions test/format_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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/>.

/**
* Collapsed Topics Information
*
* A topic based format that solves the issue of the 'Scroll of Death' when a course has many topics. All topics
* except zero have a toggle that displays that topic. One or more topics can be displayed at any given time.
* Toggles are persistent on a per browser session per course basis but can be made to persist longer by a small
* code change. Full installation instructions, code adaptions and credits are included in the 'Readme.txt' file.
*
* @package course/format
* @subpackage topcoll
* @version See the value of '$plugin->version' in below.
* @copyright &copy; 2014-onwards G J Barnard in respect to modifications of standard topics format.
* @author G J Barnard - gjbarnard at gmail dot com and {@link http://moodle.org/user/profile.php?id=442195}
* @link http://docs.moodle.org/en/Collapsed_Topics_course_format
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/course/format/topcoll/togglelib.php');

$run = false;
if ($run) {
// TEST CODE.
for ($i = 0; $i < 64; $i++) {
user_preference_allow_ajax_update('topcoll_toggle_a'.$i.'_' . $course->id, PARAM_TOPCOLL);
user_preference_allow_ajax_update('topcoll_toggle_b'.$i.'_' . $course->id, PARAM_TOPCOLL);
user_preference_allow_ajax_update('topcoll_toggle_c'.$i.'_' . $course->id, PARAM_TOPCOLL);
}
user_preference_allow_ajax_update('topcoll_toggle_bf_' . $course->id, PARAM_TOPCOLL);
user_preference_allow_ajax_update('topcoll_toggle_bf2_' . $course->id, PARAM_TOPCOLL);
user_preference_allow_ajax_update('topcoll_toggle_bf3_' . $course->id, PARAM_TOPCOLL);
user_preference_allow_ajax_update('topcoll_toggle_af_' . $course->id, PARAM_TOPCOLL);
user_preference_allow_ajax_update('topcoll_toggle_af2_' . $course->id, PARAM_TOPCOLL);
user_preference_allow_ajax_update('topcoll_toggle_af3_' . $course->id, PARAM_TOPCOLL);
// Test clean_param to see if it accepts '<' and '>' for PARAM_TEXT as stated in moodlelib.php.
echo '<h3>PARAM_TEXT < : '.clean_param('<',PARAM_TEXT).'</h3>';
echo '<h3>PARAM_TEXT > : '.clean_param('>',PARAM_TEXT).'</h3>';
echo '<h3>PARAM_RAW < : '.clean_param('<',PARAM_RAW).'</h3>';
echo '<h3>PARAM_RAW > : '.clean_param('>',PARAM_RAW).'</h3>';
}
62 changes: 62 additions & 0 deletions test/module_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Collapsed Topics Information
*
* A topic based format that solves the issue of the 'Scroll of Death' when a course has many topics. All topics
* except zero have a toggle that displays that topic. One or more topics can be displayed at any given time.
* Toggles are persistent on a per browser session per course basis but can be made to persist longer by a small
* code change. Full installation instructions, code adaptions and credits are included in the 'Readme.txt' file.
*
* @package course/format
* @subpackage topcoll
* @version See the value of '$plugin->version' in version.php.
* @copyright &copy; 2014-onwards G J Barnard in respect to modifications of standard topics format.
* @author G J Barnard - gjbarnard at gmail dot com and {@link http://moodle.org/user/profile.php?id=442195}
* @link http://docs.moodle.org/en/Collapsed_Topics_course_format
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*
* This program 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.
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

M.format_topcoll.test_all_states = function() {
"use strict";
// Reset for this course.
console.log('test_all_states: togglestate:' + this.togglestate);
console.log('test_all_states: reset course.');
M.format_topcoll.resetState(M.format_topcoll.get_min_digit());
M.format_topcoll.save_toggles();
console.log('test_all_states: togglestate:' + this.togglestate);

// Loop through all possible states, this involves the first twelve toggles.
console.log('test_all_states: start loop.');
var state = 0;

for (state = 0; state < 64; state++) {
var newchar = this.encode_value_to_character(state);
console.log('test_all_states: newchar: ' + newchar + ' - togglestate:' + this.togglestate);

//M.util.set_user_preference('topcoll_toggle_' + state + '_' + this.courseid, this.togglestate);
M.format_topcoll.set_user_preference('topcoll_toggle_a' + state + '_' + this.courseid, newchar);
M.format_topcoll.set_user_preference('topcoll_toggle_b' + state + '_' + this.courseid, newchar + ':');
M.format_topcoll.set_user_preference('topcoll_toggle_c' + state + '_' + this.courseid, ':' + newchar);
}
console.log('test_all_states: end loop.');
console.log('test_all_states: start invalid data.');
M.format_topcoll.set_user_preference('topcoll_toggle_bf_' + this.courseid, '9');
M.format_topcoll.set_user_preference('topcoll_toggle_af_' + this.courseid, 'z');
M.format_topcoll.set_user_preference('topcoll_toggle_bf2_' + this.courseid, '9:');
M.format_topcoll.set_user_preference('topcoll_toggle_af2_' + this.courseid, 'z:');
M.format_topcoll.set_user_preference('topcoll_toggle_bf3_' + this.courseid, ':9');
M.format_topcoll.set_user_preference('topcoll_toggle_af3_' + this.courseid, ':z');
console.log('test_all_states: end invalid data.');
};
Loading

0 comments on commit 6f3697c

Please sign in to comment.