From f9b5fc258310d7c7549c585033b07ca8645139fc Mon Sep 17 00:00:00 2001 From: Nick Freear Date: Thu, 31 May 2018 16:35:36 +0100 Subject: [PATCH] Bug #8, fix to `generic_embeds()` Javascript function; BRASIL [iet:10334595] * Plus, add `create_survey_url()` function * https://iet.eu.teamwork.com/desk/#/tickets/248080 --- js/src/ouop-utils.js | 4 ++-- js/src/oupilot-ui.js | 2 +- js/src/survey-embed-link.js | 22 +++++++++++++++------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/js/src/ouop-utils.js b/js/src/ouop-utils.js index 1b06f84..aea3178 100644 --- a/js/src/ouop-utils.js +++ b/js/src/ouop-utils.js @@ -123,8 +123,8 @@ function ouop_less_test ($) { } // https://github.com/nfreear/gaad-widget/blob/3.x/src/methods.js#L79-L85 -function replace_object (str, mapObj) { - var re = new RegExp(Object.keys(mapObj).join('|'), 'g'); // Was: "gi". +function replace_object (str, mapObj, reFlags) { + var re = new RegExp(Object.keys(mapObj).join('|'), reFlags || 'g'); // Was: "gi". return str.replace(re, function (matched) { return mapObj[ matched ]; // Was: matched.toLowerCase(). diff --git a/js/src/oupilot-ui.js b/js/src/oupilot-ui.js index f23bd43..05036c9 100644 --- a/js/src/oupilot-ui.js +++ b/js/src/oupilot-ui.js @@ -12,7 +12,7 @@ var L = W.location; var C = W.console; function add_course_code_to_urls ($, resp) { - var $links = $('a').filter('[ href *= "/mod/" ], [ href *= "/course/" ]'); + var $links = $('a').filter('[href *= "/mod/"], [href *= "/course/"]'); $links.each(function (idx, el) { var url = $(el).attr('href'); diff --git a/js/src/survey-embed-link.js b/js/src/survey-embed-link.js index 10255a5..9e2ca60 100644 --- a/js/src/survey-embed-link.js +++ b/js/src/survey-embed-link.js @@ -22,15 +22,15 @@ function generic_embeds ($, resp) { } var $links = $('a[ href *= _EMBED_ME_ ]'); - var util = resp.util; + var replaceObj = resp.util.replace; // Was: var util = resp.util; $links.each(function (idx, el) { var $link = $(el); - var url = util.replace($link.attr('href'), { '{oucu}': resp.profile.ouop_oucu }); + var url = create_survey_url($link.attr('href'), resp); var m_height = url.match(/height=(\d+\w+);?/); var height = 'height: ' + (m_height ? m_height[ 1 ] : '1050px;'); - $link.replaceWith(util.replace('', { + $link.replaceWith(replaceObj('', { '{u}': url, '{h}': height, '{i}': idx })); // .replace(/%s/, url).replace(/%h/, height).replace(/%d/, idx) var $iframe = $('#s' + idx); @@ -40,7 +40,9 @@ function generic_embeds ($, resp) { } function embed_pilot_surveys ($, resp) { - var $links = $('a[ href *= -pre-survey-embed ], a[ href *= -post-survey-embed ]'); + var $links = $('a').filter('[ href *= -pre-survey-embed ], [ href *= -post-survey-embed ]'); + // Was: var $links = $('a[ href *= -pre-survey-embed ], a[ href *= -post-survey-embed ]'); + var replaceObj = resp.util.replace; $links.each(function (idx, el) { var $link = $(el); @@ -50,9 +52,10 @@ function embed_pilot_surveys ($, resp) { var m_height = url.match(/height=(\d+\w+);?/); var height = m_height ? ('height: '+ m_height[ 1 ]) : ''; - survey_url = survey_url.replace(/\{?OUCU\}?/, resp.profile.ouop_oucu).replace(/\{COURSE\}/gi, resp.course_code); + survey_url = create_survey_url(survey_url, resp); + // Was: survey_url = survey_url.replace(/\{?OUCU\}?/, resp.profile.ouop_oucu).replace(/\{COURSE\}/gi, resp.course_code); - $link.replaceWith(resp.util.replace('', { + $link.replaceWith(replaceObj('', { '{u}': survey_url, '{h}': height, '{i}': idx })); // .replace(/%s/, survey_url).replace(/%h/, height).replace(/%d/, idx) @@ -90,7 +93,8 @@ function inject_post_activity_survey_link ($, resp) { // var $container_quiz_rev = $('#page-mod-quiz-review #user-notifications'); var $container_quiz = $('.path-mod-quiz'); // Was: $('#page-mod-quiz-view'); var $container_assign = $('#page-mod-assign-view'); - var survey_url = resp.survey_urls.post.replace('{OUCU}', resp.profile.ouop_oucu).replace('{COURSE}', resp.course_code); + var survey_url = create_survey_url(resp.survey_urls.post, resp); + // Was: resp.survey_urls.post.replace('{OUCU}', resp.profile.ouop_oucu).replace('{COURSE}', resp.course_code); // WAS: var survey_url = resp.survey_urls[ resp.course_code ].post.replace('{OUCU}', resp.profile.ouop_oucu); var util = resp.util; @@ -130,3 +134,7 @@ function survey_return_redirect ($, resp) { W.location = resp.redirect_url; } } + +function create_survey_url(url, resp) { + return resp.util.replace(url, { '{oucu}': resp.profile.ouop_oucu, '{course}': resp.course_code }, 'gi'); +}