Skip to content

Commit

Permalink
Merge pull request adaptlearning#30 from adaptlearning/feature/#1611
Browse files Browse the repository at this point in the history
drop stringReplace now that handlebars can do all the replacement
  • Loading branch information
moloko authored Aug 18, 2017
2 parents 2919720 + aae761d commit 3521cd4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 48 deletions.
65 changes: 18 additions & 47 deletions js/adapt-contrib-assessmentResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,20 @@ define([
onAssessmentsComplete: function(state) {
if (this.model.get("_assessmentId") === undefined ||
this.model.get("_assessmentId") != state.id) return;

this.model.set("_state", state);
/*
make shortcuts to some of the key properties in the state object so that
content developers can just use {{attemptsLeft}} in json instead of {{state.attemptsLeft}}
*/
this.model.set( {
_state: state,
attempts: state.attempts,
attemptsSpent: state.attemptsSpent,
attemptsLeft: state.attemptsLeft,
score: state.score,
scoreAsPercent: state.scoreAsPercent,
maxScore: state.maxScore,
isPass: state.isPass
});

var feedbackBand = this.getFeedbackBand();

Expand Down Expand Up @@ -179,19 +191,15 @@ define([
},

setFeedback: function(feedbackBand) {

var completionBody = this.model.get("_completionBody");

var state = this.model.get("_state");
state.feedbackBand = feedbackBand;
state.feedback = feedbackBand.feedback;

this.checkRetryEnabled();

completionBody = this.stringReplace(completionBody, state);
// ensure any handlebars expressions in the .feedback are handled...
this.model.set('feedback', Handlebars.compile(feedbackBand.feedback)(this.model.toJSON()));

this.model.set("body", completionBody);
this.checkRetryEnabled();

this.model.set("body", this.model.get("_completionBody"));
},

/**
Expand Down Expand Up @@ -233,49 +241,12 @@ define([

if (showRetry) {
var retryFeedback = this.model.get("_retry").feedback;
retryFeedback = this.stringReplace(retryFeedback, state);
this.model.set("retryFeedback", retryFeedback);
} else {
this.model.set("retryFeedback", "");
}
},

stringReplace: function(string, context) {
//use handlebars style escaping for string replacement
//only supports unescaped {{{ attributeName }}} and html escaped {{ attributeName }}
//will string replace recursively until no changes have occured

var changed = true;
while (changed) {
changed = false;
for (var k in context) {
var contextValue = context[k];

switch (typeof contextValue) {
case "object":
continue;
case "number":
contextValue = Math.floor(contextValue);
break;
}

var regExNoEscaping = new RegExp("((\\{\\{\\{){1}[\\ ]*"+k+"[\\ ]*(\\}\\}\\}){1})","g");
var regExEscaped = new RegExp("((\\{\\{){1}[\\ ]*"+k+"[\\ ]*(\\}\\}){1})","g");

var preString = string;

string = string.replace(regExNoEscaping, contextValue);
var escapedText = $("<p>").text(contextValue).html();
string = string.replace(regExEscaped, escapedText);

if (string != preString) changed = true;

}
}

return string;
},

onRemove: function() {
if (this.model.unsetLocking) this.model.unsetLocking("_isVisible");

Expand Down
4 changes: 3 additions & 1 deletion templates/assessmentResults.hbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{{! make the _globals object in course.json available to this template}}
{{import_globals}}
<div class="results-inner component-inner">
{{> component this}}
<div class="results-widget component-widget">
{{#if _isRetryEnabled}}
<div class="results-retry component-feedback">
<div class="results-retry-inner component-feedback-inner">
{{{a11y_text retryFeedback}}}
{{{compile_a11y_text retryFeedback}}}
</div>
<div class="results-retry-button">
<button>{{#if _retry.button}}{{_retry.button}}{{else}}Retry{{/if}}</button>
Expand Down

0 comments on commit 3521cd4

Please sign in to comment.