Skip to content

Commit

Permalink
Merge pull request #23 from clpetersonucf/issue/21-fix-question-text-…
Browse files Browse the repository at this point in the history
…id-array

Issue/21 fix question text id array
  • Loading branch information
clpetersonucf authored Jan 30, 2020
2 parents 3b4ae80 + 4f26453 commit 8719816
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"lines": 59
},
"src/scoreScreen.js": {
"statements": 99,
"branches": 99,
"statements": 97,
"branches": 89,
"functions": 99,
"lines": 99
}
Expand Down
19 changes: 15 additions & 4 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ SortItOut.controller("SortItOutEngineCtrl", ["$scope", "$rootScope", "$timeout",
let selectedElement // element that is being dragged
let pickupCount = 1 // every new item picked up will go to the top (z-index)
let itemSource // to track where the dragged item came from
let questionToId // used for scoring
let questionToId = new Map() // used for scoring

const SRC_DESKTOP = -1 // indicates drag started on desktop, otherwise itemSource is folderIndex
const MARGIN_SIZE = 20 // #preview-scroll-container margin size
Expand Down Expand Up @@ -69,10 +69,21 @@ SortItOut.controller("SortItOutEngineCtrl", ["$scope", "$rootScope", "$timeout",
$scope.$apply()
}

// hash function to be used in conjunction with questionToId
const hash = (str) => {
let hash = 0, i, char
if (str.length == 0) return hash
for (i = 0; i < str.length; i++) {
char = str.charCodeAt(i)
hash = ((hash << 5) - hash) + char
hash |= 0
}
return Math.abs(hash)
}

const generateQuestionToId = qset => {
questionToId = {}
for (let item of qset.items) {
questionToId[item.questions[0].text] = item.id
questionToId.set(hash(sanitizeHelper.desanitize(item.questions[0].text)), item.id)
}
}

Expand Down Expand Up @@ -486,7 +497,7 @@ SortItOut.controller("SortItOutEngineCtrl", ["$scope", "$rootScope", "$timeout",

$scope.folders.forEach( ({text, items}) => {
items.forEach( item => {
const id = questionToId[item.text]
const id = questionToId.get(hash(item.text))
Materia.Score.submitQuestionForScoring(id, text)
})
})
Expand Down
39 changes: 36 additions & 3 deletions src/scoreScreen.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const SortItOut = angular.module("SortItOutScore", [])

SortItOut.controller("SortItOutScoreCtrl", ["$scope", "$timeout", function ($scope, $timeout) {
SortItOut.controller("SortItOutScoreCtrl", ["$scope", "$timeout", "sanitizeHelper", function ($scope, $timeout, sanitizeHelper) {
$scope.loaded = false
$scope.zoomIndex = {
folder: -1,
Expand Down Expand Up @@ -65,7 +65,7 @@ SortItOut.controller("SortItOutScoreCtrl", ["$scope", "$timeout", function ($sco
folders[userFolderIndex].placeCount++

const item = {
text,
text: sanitizeHelper.desanitize(text),
correct,
userFolderName,
image: imageMap[text] || false
Expand All @@ -79,7 +79,7 @@ SortItOut.controller("SortItOutScoreCtrl", ["$scope", "$timeout", function ($sco
else {
folders[correctFolderIndex].items.push(item)
folders[userFolderIndex].extraItems.push({
text,
text: sanitizeHelper.desanitize(text),
image: imageMap[text] || false,
correctFolderName
})
Expand Down Expand Up @@ -108,3 +108,36 @@ SortItOut.controller("SortItOutScoreCtrl", ["$scope", "$timeout", function ($sco
Materia.ScoreCore.hideResultsTable()
Materia.ScoreCore.start($scope)
}])

SortItOut.service('sanitizeHelper', [ function() {
const SANITIZE_CHARACTERS = {
'&' : '&amp;',
'>' : '&gt;',
'<' : '&lt;',
'"' : '&#34;'
}

const sanitize = (input) => {
if (!input) return;
for (var k in SANITIZE_CHARACTERS) {
let v = SANITIZE_CHARACTERS[k]
let re = new RegExp(k, "g")
input = input.replace(re, v)
}
return input
}

const desanitize = (input) => {
if (!input) return;
for (var k in SANITIZE_CHARACTERS) {
let v = SANITIZE_CHARACTERS[k]
let re = new RegExp(v, "g")
input = input.replace(re, k)
}
return input
}
return {
sanitize: sanitize,
desanitize: desanitize
}
}])
17 changes: 16 additions & 1 deletion src/scoreScreen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ describe('ScoreScreen Controller', function() {
qsets = generateQsets()

// initialize the angular controller
inject(function(_$controller_, _$timeout_){
inject(function(_$controller_, _$timeout_, _sanitizeHelper_){
// instantiate the controller
$controller = _$controller_('SortItOutScoreCtrl', { $scope: $scope });
$timeout = _$timeout_;
$sanitizeHelper = _sanitizeHelper_;
})
})

Expand Down Expand Up @@ -216,6 +217,20 @@ describe('ScoreScreen Controller', function() {
expect($scope.zoomIndex.item).toBe(1);
});

it('should correctly sanitize item text', function() {
let text = '<p>This is a paragraph tag</p>';
let sanitized = $sanitizeHelper.sanitize(text);

expect(sanitized).toBe('&lt;p&gt;This is a paragraph tag&lt;/p&gt;');
});

it('should correctly desanitize item text', function() {
let text = '&lt;p&gt;This is a paragraph tag&lt;/p&gt;';
let desanitize = $sanitizeHelper.desanitize(text);

expect(desanitize).toBe('<p>This is a paragraph tag</p>');
})

var generateScoreTables = () => {
// the 100% correct score tables that match each qset
var scoreTable1 = [
Expand Down

0 comments on commit 8719816

Please sign in to comment.