Skip to content

Commit

Permalink
Bug #6/#7, Re-factor Javascript - use Browserify [iet:9820855][iet:97…
Browse files Browse the repository at this point in the history
…02401]

* Move from: `user/*.js` to: `js/src/*.js`
* 03-15 August 2017
  • Loading branch information
nfreear committed Aug 15, 2017
1 parent 0c0cf99 commit d940b8d
Show file tree
Hide file tree
Showing 14 changed files with 663 additions and 21 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*dist*.js
*.min.js

/src

#/src
#/bin

thirdpartylibs.xml
Expand All @@ -18,7 +17,7 @@ composer.lock
vendor
*BAK*

package.json
#package.json
package-lock.json
node_modules
.eslintrc*
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

[![Build status — Travis-CI][travis-icon]][travis]
[![js-semistandard-style][semi-icon]][semi]
[![OU-OpenID moodle plugin][browse-icon]][gh]


# moodle-auth_ouopenid

This plugin is a wrapper around the [OpenID authentication plugin][openid] for [Moodle][].
This plugin is a wrapper around the [OpenID authentication plugin][openid] for [Moodle][],
plus other research-related functionality.

Available via IET-OU Satis:

Expand Down Expand Up @@ -95,7 +97,9 @@ Developed for the [TeSLA project][].
[Moodle]: https://moodle.org/
[openid]: https://github.com/remotelearner/moodle-auth_openid
[ouopenid]: https://github.com/IET-OU/moodle-auth_ouopenid
[gh]: https://github.com/IET-OU/moodle-auth_ouopenid
[composer]: https://getcomposer.org/
[npm]: https://npmjs.com/
[iet]: http://iet.open.ac.uk/
[ou]: http://www.open.ac.uk/
[travis]: https://travis-ci.org/IET-OU/moodle-auth_ouopenid
Expand All @@ -104,3 +108,6 @@ Developed for the [TeSLA project][].
[semi]: https://github.com/Flet/semistandard
[semi-icon]: https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?style=flat-square
"Javascript coding style — 'semistandard'"
[browse]: https://npmjs.com/package/browserify
[browse-icon]: https://img.shields.io/badge/built_with-browserify-blue.svg
"Built with browserify"
82 changes: 82 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*!
OU-OpenID. © Nick Freear. © The Open University.
NDF, 3-August-2017.
*/

// IMPORTANT: console.debug() does NOT print output !!

'use strict';

var util = require('./src/ouop-utils');
var userJsonUrl = '/auth/ouopenid/user/ajax.php?r=' + util.rand();

require('./src/when-call')(
function () {
console.debug('>> when');
return window.jQuery;
},
function ($) {
var W = window;
var L = W.location;

console.debug('>> call');

if (L.pathname.match(/^\/admin\//)) {
return console.warn('ouopenid: admin page, exiting.');
}

var $body = $('body');

$body.addClass(L.href.match(/debug=1/) ? 'debug-param' : '');

console.warn('ouopenid $:', $.fn.jquery, W.M.cfg); // W.Y.version

$.getJSON(userJsonUrl).done(function (data, textStat, jqXHR) { // W.M.cfg.wwwroot + ..
console.debug('>> getJSON..');

util.set_strings(data);

data.util = util;
data.$ = $;

if (! data.profile.ouop_oucu) {
console.warn('ouopenid warning: missing profile.');

$body.addClass('ouop-ouopenid-warn-profile');
}

console.warn('ouopenid JSON: ', data, jqXHR);

require('./src/analytics').run($, data);
// ga.analytics($, data);
// ga.handle_moodle_events($);

$body.addClass(data.body_class)
.addClass(data.profile.ouop_is_team ? 'ouop-is-team' : 'ouop-not-team');

$body.addClass(data.debug);

require('./src/oupilot-ui')($, data);

require('./src/moodle-user-profile')($, data);

// ..

require('./src/local-fixes')($, data);

require('./src/survey-embed-link')($, data);

require('./src/tesla-statistics')($);
})
.fail(function (jqXHR, textStat, ex) {
console.error('ouopenid error: ', textStat, jqXHR, ex);

$body.addClass('ouop-ouopenid-error-' + jqXHR.status);
});

util.less_test($);
}
);

// End.
5 changes: 5 additions & 0 deletions js/src/_comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*!
OU-OpenID. © Nick Freear. © The Open University. @preserve
NDF, 3-August-2017.
*/
56 changes: 56 additions & 0 deletions js/src/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*!
OU-OpenID. © Nick Freear. © The Open University. (NDF / 02-April-2017)
*/

/* global ga: false */

'use strict';

module.exports = {
run: function ($, resp) {
include_ga_javascript();

pageview(resp);

handle_moodle_events($);
},

analytics: function ($, resp) {
include_ga_javascript();

pageview(resp);
},

handle_moodle_events: handle_moodle_events
};

var W = window;
var C = W.console;

function include_ga_javascript () {
/* eslint-disable *//* jshint ignore:start */
(function (i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments) }, i[r]. l = 1 * new Date(); a = s.createElement(o),
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, window.document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
/* eslint-enable *//* jshint ignore:end */
}

function pageview (resp) {
/* Google Analytics.
*/
ga('create', resp.config.ga, 'auto');
ga('send', 'pageview');

C.debug('ouop: analytics', resp.config.ga);
}

function handle_moodle_events ($) {
var $events = $('script[ data-ouop-event ]');

$events.each(function (idx, el) { // if ($events.length > 0) {
var event = JSON.parse($(el).text());

C.warn('ouop: event', idx, event);
});
}
62 changes: 62 additions & 0 deletions js/src/local-fixes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// TeSLA-specific fixes ~~ usability and clarity.
// OUOP.local_fixes = function

module.exports = function ($, resp) {

fix_typing_enrollment_page($);
fix_enrollment_start_page($, resp);

console.warn('ouop: tesla-local-fixes');
};

var tesla_inst_names = {
'Keystroke Dynamics': 'ks',
'Anti-plagiarism': 'tpt',
'Forensic Analysis': 'fa',
'Face Recognition': 'fr',
'Voice Recognition': 'vr'
};

function fix_typing_enrollment_page($) {
var $wordcount = $('.path-local-tesla-views .btn #word_counter');
var $btn = $wordcount.closest('button');
var $form = $wordcount.closest('form');
var $question = $form.find('legend').first();

$btn.html(
'<span id="ouop-wc-outer" title="Word count">Words: <span id="word_counter" class="badge">0 / 250</span></span> Continue');

// Was: $wordcount.attr('title', 'Word count');

$form.addClass('ouop-tesla-ks-enrollment-form');

var qm = $question.text().match(/Q #?(\d) of (\d)\./);
var qn_text;

if (qm) {
qn_text = '<small data-ouop-qn="%1">Question <i>%2</i><span> of %3</span></small>'
.replace(/%1/, qm[ 1 ]).replace(/%2/, qm[ 1 ]).replace(/%3/, qm[ 2 ]);

$question.html($question.text().replace(qm[ 0 ], qn_text));
}
}

// 14-August-2017.
function fix_enrollment_start_page($, resp) {
var $alert = $('#page-local-tesla-views-enrollment #user-notifications .alert');
var message = $alert.text();
var m_enroll = message ? message.match(/The required enrollments are: ([\w ]+)/) : null;
var inst = m_enroll ? m_enroll[ 1 ] : null;
var inst_code = inst in tesla_inst_names ? tesla_inst_names[ inst ] : null;

if (inst) {
$('body')
.addClass('ouop-enroll-page')
.addClass('ouop-enroll-' + inst.replace(' ', '-'))
.addClass('ouop-enroll-' + inst_code);

$alert.removeClass('alert-danger').addClass('alert-warning');

console.warn('ouop. Enroll instrument:', inst_code, inst);
}
}
85 changes: 85 additions & 0 deletions js/src/moodle-user-profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Streamline the Moodle user profile form, and pages.

var util;

module.exports = function ($, resp) {
util = resp.util;

complete_user_profile_form($, resp);
user_profile_form_redirect($, resp);
user_profile_continue_link($, resp);

// Was: if ( L.pathname.match(/^\/user\/edit/) )
if (! resp.profile.ouop_is_team) {
disable_user_profile_form($);
}
};

var W = window;
var C = W.console;

function complete_user_profile_form ($, resp) {
var $form = $('#page-user-edit #region-main form');
var $inp_fname = $form.find('[ name = firstname ]');
var $inp_lname = $form.find('[ name = lastname ]');
var $inp_email = $form.find('[ name = email ]');
var prof = resp.profile;
var isteam = prof.ouop_is_team;

if ($form.length && !$inp_fname.val()) {
$inp_fname.val(isteam ? prof.ouop_firstname : prof.ouop_oucu);
$inp_lname.val(isteam ? prof.ouop_lastname : 'test');
$inp_email.val(isteam ? prof.ouop_email : util.str('testmail', prof.ouop_oucu));
}

C.debug('ouop: complete-user-profile-form');
};

function user_profile_form_redirect ($, resp) {
var $form = $('#page-user-edit #region-main form');
var cfg = resp.config;

if ($form.length && cfg.user_form_redirect) {
W.setTimeout(function () {
C.warn('ouop: user-profile-form-redirect - trigger');

$form.trigger('submit');
},
cfg.redirect_timeout || 2000);
}

if (cfg.user_form_redirect) {
$form.before(util.alert(util.str('form_warning'), 'ouop-form-disable'));
} else {
$form.before(util.alert(util.str('form_redirect_msg'), 'ouop-form-disable'));
}
};

function user_profile_continue_link ($, resp) {
var $pages = $('#page-user-profile, #page-user-preferences');

$pages.find('#page-header')
.after(util.alert('<a href="' + resp.redirect_url + '">' + util.str('continuelink') + '</a>'));

if (!resp.profile.ouop_is_team && $('#page-user-edit').length) {
C.debug('ouopenid redirecting');

// Was: W.location = data.redirect_urL;
}
}

function disable_user_profile_form ($) {
var $form = $('#page-user-edit #region-main form');

$form
.attr('title', util.str('form_warning'))
// Was: .before(util.alert(util.str('form_warning'), 'ouop-form-disable'))
.find('input, select').each(function () {
// Was: $(this).attr('disabled', 'disabled');
if (!$(this).hasClass('btn')) {
$(this).attr('readonly', 'readonly');
}
});

$form.find('#id_submitbutton').val('Continue').text('Continue');
}
Loading

0 comments on commit d940b8d

Please sign in to comment.