Skip to content

Commit

Permalink
Added answerMasking(0 and lineReader() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsharman committed Aug 29, 2024
1 parent c7a1838 commit f6a069f
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/assessment/core/player.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { hasAnswerMasking } from './activity';
import * as app from './app';
import * as items from './items';
import logger from '../../utils/logger';
Expand All @@ -7,6 +8,40 @@ import logger from '../../utils/logger';
* @module Assessment/Player
*/

const state = {
answerMasking: {
enabled: null,
},
lineReader: {
enabled: null,
id: null,
},
};

/**
* Shows or hides the player answer masking tool. Answer masking has to be enabled in
* Items API configuration for this to work.
* @since 2.15.0
* @param {boolean} action - Whether to show (`true`) or hide (`false`).
*/
export function answerMasking(action) {
if (state.answerMasking.enabled === null) {
const buttonElement = document.querySelector('.test-answer-masking');

if (buttonElement) {
state.answerMasking.enabled = true;
} else {
state.answerMasking.enabled = false;
}
}

if (state.answerMasking.enabled) {
if (action !== undefined) app.appInstance().questionsApp().masking(action);
} else {
logger.warn('Answer masking is not enabled in the Items API configuration.');
}
}

/**
* Renders an Items API custom dialog.
* @since 0.1.0
Expand Down Expand Up @@ -69,6 +104,50 @@ export function isReviewScreen() {
}, 500);
}

/**
* Shows or hides the player line reader. The line reader has to be enabled in
* Items API configuration for this to work.
* @since 2.15.0
* @param {string=} action - Whether to `toggle` (default), `show` or `hide` the line reader.
*/
export function lineReader(action) {
if (state.lineReader.enabled === null) {
const buttonElement = document.querySelector('.lrn_linereader-toggle');

if (buttonElement) {
state.lineReader.enabled = true;

const dataAttributeValue = buttonElement.querySelector('[data-lrn-widget-container]').getAttribute('data-lrn-widget-container');
const uniqueValue = dataAttributeValue.match(/\d+$/);

if (uniqueValue) {
state.lineReader.id = uniqueValue[0];
} else {
logger.warn('Could not find the line reader unique id.');
}
} else {
state.lineReader.enabled = false;
}
}

if (state.lineReader.enabled && state.lineReader.id !== null) {
const lineReader = app.appInstance().features()[`lrn-assessapp-feature_${state.lineReader.id}`];

switch (action) {
case 'show':
lineReader.show();
break;
case 'hide':
lineReader.hide();
break;
default:
lineReader.toggle();
}
} else {
logger.warn('Line reader is not enabled in the Items API configuration.');
}
}

/**
* Generic function to call API navigation methods. Supports:
* - `previous`
Expand Down

0 comments on commit f6a069f

Please sign in to comment.