Skip to content

Commit

Permalink
Bug #6, add "survey-end" Javascript PHP; BOS piping [iet:10171030][ie…
Browse files Browse the repository at this point in the history
…t:10171110]

* BOS survey piping - pass "return-code" (x1) parameter in,
* And, process "return-code" in "survey-end" JS
  • Loading branch information
nfreear committed Oct 6, 2017
1 parent 9eebf60 commit 1bf0252
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 11 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
],
"jshint-config": "# echo '{ }' > .jshintrc;\necho '*.min.js\ndist.js' > .jshintignore",
"eslint-config": [
"echo '---\n extends: eslint:recommended\n globals:\n window: false\n root: true\n' > .eslintrc.yaml",
"echo '---\n extends: eslint:recommended\n globals:\n window: false\n console: false\n root: true\n' > .eslintrc.yaml",
"echo '{\n \"extends\": \"eslint:recommended\",\n \"globals\": { \"window\": false }\n}' > .eslintrc-XX.json"
],
"eslint-echo": "node_modules/.bin/eslint --print-config .eslintrc.yaml",
Expand All @@ -81,7 +81,7 @@
"phpcs --standard=PSR2 -n db/User.php",
"composer cn",
"# npm run semi",
"# npm run eslint",
"npm run eslint",
"php -r 'simplexml_load_file(\"db/install.xml\");' && echo install.xml OK!"
],
"mcs": "phpcs --standard=./moodle-core.xml --ignore=vendor --ignore=.git --extensions=php .",
Expand Down
4 changes: 3 additions & 1 deletion db/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,10 @@ public static function getSurveyUrls($profile)
$survey_urls = $CFG->auth_ouopenid_survey_urls;

$batch = isset($profile->batch) ? $profile->batch : 0;
$batch = isset($CFG->auth_ouopenid_batch_override) ? $CFG->auth_ouopenid_batch_override : $batch;

if (! isset($survey_urls[ $batch ])) {
self::debug([ __FUNCTION__, 'error', ]);
self::debug([ __FUNCTION__, 'error', $batch ]);
}
return isset($survey_urls[ $batch ]) ? $survey_urls[ $batch ] : $survey_urls[ 0 ];
}
Expand Down
2 changes: 2 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ require('./src/when-call')(
console.debug('>> getJSON..');

util.set_strings(data);
util.set_course_name($, data);
util.site_message($, data);

data.util = util;
data.$ = $;
Expand Down
27 changes: 24 additions & 3 deletions js/src/ouop-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ module.exports = {
return Math.floor(Math.random() * (max - min)) + min;
},

alert: function (msg, id, cls) {
return '<div id="' + (id || 'oua') + '" class="ouop alert ' +
(cls || 'alert-info') + '" role="alert">' + msg + '</div>';
site_message: function ($, resp) {
var message = resp.config.site_message;

if (message) {
$('#page').prepend(alert(message, 'oum', 'alert-info site-message'));
}
},

alert: alert,

// Javascript translation/localisation [i18n].
set_strings: function (resp) {
trans = resp.strings;
Expand All @@ -29,6 +34,17 @@ module.exports = {
return JSON.stringify(obj, null, 2).replace(/:/g, ',').replace(/_/g, ' ');
},

set_course_name: function ($, resp) {
var $course_name = $('.path-course-view, .path-mod-page').find('.breadcrumb-item a[ href *= "course/view.php" ]').first(); // .find('a[ data-key = coursehome ]');
var course_code = $course_name.text();

resp.course_code = course_code || null;

if (course_code) {
$('body').addClass('ouop-course-code-' + course_code).attr('data-course-code', course_code);
}
},

replace: replace_object,
less_test: ouop_less_test
};
Expand Down Expand Up @@ -59,3 +75,8 @@ function replace_object (str, mapObj) {
return mapObj[ matched ]; // Was: matched.toLowerCase().
});
}

function alert (msg, id, cls) {
return '<div id="' + (id || 'oua') + '" class="ouop alert ' +
(cls || 'alert-info') + '" role="alert">' + msg + '</div>';
}
12 changes: 7 additions & 5 deletions js/src/survey-embed-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ function embed_pilot_surveys ($, resp) {
var m_height = url.match(/height=(\d+\w+);?/);
var height = 'height: ' + (m_height ? m_height[ 1 ] : '1000px;');

survey_url = survey_url.replace(/\{?OUCU\}?/, resp.profile.ouop_oucu);
survey_url = survey_url.replace(/\{?OUCU\}?/, resp.profile.ouop_oucu).replace(/\{COURSE\}/gi, resp.course_code);

$link.replaceWith(resp.util.replace('<iframe src="{u}" style="{h}" class="ouop-pilot-survey-ifr" id="s-{i}"></iframe>', {
'{u}': url, '{h}': height, '{i}': idx
$link.replaceWith(resp.util.replace('<iframe src="{u}" style="{h}" id="ifr-{i}"></iframe>', {
'{u}': survey_url, '{h}': height, '{i}': idx
})); // .replace(/%s/, survey_url).replace(/%h/, height).replace(/%d/, idx)
var $iframe = $('#s' + idx);

C.warn('ouop: pilot-survey-embeds', idx, $iframe);
var $iframe = $('#ifr-' + idx).addClass('ouop-pilot-survey-ifr');

C.warn('ouop: pilot-survey-embeds', idx, survey_url, $iframe);
});
}

// DEPRECATED.
function fix_pilot_survey_links ($, resp) {
var $links = $('a[ href = "#!-pre-survey-link" ], a[ href = "#!-post-survey-link" ]');
// var $links = $('#region-main a[ href *= OUCU ]');
Expand Down
52 changes: 52 additions & 0 deletions js/survey-end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*!
OU-OpenID. © Nick Freear, 03-Oct-2017. © The Open University.
*/

/* eslint camelcase: 0 */

// https://moodle.ouuk.tesla-project.eu/survey-end/?from=pre-survey&returncode=VR&embed=1

window.jQuery(function ($) {
'use strict';

var L = window.location;
var m_from = L.search.match(/from=((pre|post)-?survey)/);
var m_code = L.search.match(/return-?code=(return-code-)?(\w+)/);
var source = m_from ? m_from[ 1 ] : null;
var code = m_code ? m_code[ 2 ].toLowerCase() : null;

var config = getConfig('#survey-end-config');
var redirect = config.redirects[ code ] || null;

$('body').addClass(inIframe() ? 'is-embed' : 'not-embed');

console.warn(source, code, config);
console.warn(inIframe() ? 'is-embed' : 'not-embed');

if (inIframe() && redirect) {
$('body').addClass('redirect');

console.warn('Redirecting:', redirect.url);

window.setTimeout(function () {
window.location = redirect.url;
}, config.timeout || 500);
} else {
$('body').addClass('no-redirect');

console.error('No-redirect. Is "returncode" URL parameter present and correct?');
}

// https://stackoverflow.com/questions/326069/how-to-identify-if-a-webpage-is-being-loaded-inside-an-iframe-or-directly-into-t
function inIframe () {
try {
return window.self !== window.top || window.location.test(/&embed=/);
} catch (e) {
return true;
}
}

function getConfig(selector) {
return JSON.parse($(selector).text());
}
});
4 changes: 4 additions & 0 deletions lang/en/auth_ouopenid.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,9 @@
$string[ 'login_submit' ] = 'Sign in';
$string[ 'login_footer' ] = '© 2017 The Open University';
$string[ 'login_footer_link' ] = 'https://www.open.ac.uk/';
$string[ 'survey_end_title' ] = 'Redirecting ';
$string[ 'survey_end_msg' ] =
'<p>Thank you for completing the survey.
<p> Redirecting ...';

// End.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"eslintConfig": {
"extends": "eslint:recommended",
"env": { "commonjs": true },
"rules": {
"no-console": "off"
},
"globals": {
"window": false,
"console": false,
Expand Down
102 changes: 102 additions & 0 deletions survey-end/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* OU-OpenID SURVEY-END.
*
* @package auth_ouopenid
* @author Nick Freear, 03-October-2017, 08:07.
* @copyright (c) 2017 The Open University.
*/

// For 'print_string()' language support!
require_once __DIR__ . '/../../../config.php';

define( 'OUOP_STRING', 'auth_ouopenid' );

// TODO: check if plugin is enabled or not !!

class Ou_Open_Id_Survey_End {

const JQUERY_URL = 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js';

public static function versionParam() {
echo '?r=' . mt_rand(11, 9999);
}

public static function checkMaintenanceMode() {
global $CFG;
header('X-auth-ouopenid-maintain: ' . $CFG->maintenance_enabled);

if ($CFG->maintenance_enabled && ! filter_input(INPUT_GET, 'admin')) {
header('Location: '. $CFG->wwwroot, true, 302);
}
}
}
Ou_Open_Id_Survey_End::checkMaintenanceMode();


header('Content-Language: en');
header('X-Frame-Options: sameorigin');
header('X-UA-Compatible: IE=edge');

?>
<!doctype html><html lang="en"><meta charset="utf-8" />

<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="robots" content="noindex, nofollow" />

<title><?php // print_string( 'survey_end_title', OUOP_STRING ) ?> (survey-end)</title>

<link rel="X-stylesheet" href="/auth/ouopenid/style/login.css" />
<style>
body {
background-color: #fff;
color: #222;
font: 1.1em sans-serif;
line-height: 1.4em;
margin: 2em auto;
max-width: 40em;
X-min-width: 26em;
}
.X-is-embed { margin: 2px; }
.is_embed .footer { display: none; }
</style>

<body class="survey-end-page">

<div>

<div class="survey-end-msg">
<?php // print_string( 'survey_end_msg', OUOP_STRING ) ?>

<p> Thank you for completing the survey.
<p> Redirecting ...
</div>


<p class="footer"><small>
<a href="<?php print_string( 'login_footer_link', OUOP_STRING ) ?>"
><?php print_string( 'login_footer', OUOP_STRING ) ?></a>.
</small></p>

</div>


<script id="survey-end-config" type="application/json">
<?php
echo json_encode([
'redirects' => $CFG->auth_ouopenid_redirects,
'timeout' => 3000,
'other' => 1,
]);
?>
</script>


<script src="<?php echo Ou_Open_Id_Survey_End::JQUERY_URL ?>"></script>
<script src="/auth/ouopenid/user/ouop-analytics.js<?php Ou_Open_Id_Survey_End::versionParam() ?>"></script>
<script src="/auth/ouopenid/js/survey-end.js<?php Ou_Open_Id_Survey_End::versionParam() ?>"></script>
<script>
OUOP.analytics($, { config: { ga: <?php echo json_encode($CFG->auth_ouopenid_js_config[ 'ga' ]) ?> } });
</script>
</body>
</html>

0 comments on commit 1bf0252

Please sign in to comment.