| 1 |
import { select } from '@wordpress/data'; |
| 2 |
const { get, isArray } = lodash; |
| 3 |
|
| 4 |
const getQuestionOptions = function getQuestionOptions( state, id ) { |
| 5 |
console.time( 'parseOptions' ); |
| 6 |
|
| 7 |
const question = getQuestion( state, id ); |
| 8 |
let options = question.options; |
| 9 |
|
| 10 |
options = ! isArray( options ) ? JSON.parse( CryptoJS.AES.decrypt( options.data, options.key, { format: CryptoJSAesJson } ).toString( CryptoJS.enc.Utf8 ) ) : options; |
| 11 |
options = ! isArray( options ) ? JSON.parse( options ) : options; |
| 12 |
|
| 13 |
console.timeEnd( 'parseOptions' ); |
| 14 |
return options; |
| 15 |
}; |
| 16 |
|
| 17 |
export { getQuestionOptions }; |
| 18 |
|
| 19 |
/** |
| 20 |
* Get current status of an item in course. |
| 21 |
* |
| 22 |
* @param state |
| 23 |
* @param itemId |
| 24 |
*/ |
| 25 |
export function getItemStatus( state, itemId ) { |
| 26 |
const item = select( 'course-learner/user' ).getItemById( itemId ); |
| 27 |
return item ? get( item, 'userSettings.status' ) : ''; |
| 28 |
} |
| 29 |
|
| 30 |
export function getProp( state, prop, defaultValue ) { |
| 31 |
return state[ prop ] || defaultValue; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get quiz attempted. |
| 36 |
* |
| 37 |
* @param state |
| 38 |
* @param itemId |
| 39 |
* @return {Array} |
| 40 |
*/ |
| 41 |
export function getQuizAttempts( state, itemId ) { |
| 42 |
const item = select( 'course-learner/user' ).getItemById( itemId ); |
| 43 |
return item ? get( item, 'userSettings.attempts' ) : []; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get answers for a quiz user has did. |
| 48 |
* |
| 49 |
* @param state |
| 50 |
* @param itemId |
| 51 |
* @return {{}} |
| 52 |
*/ |
| 53 |
export function getQuizAnswered( state, itemId ) { |
| 54 |
const item = select( 'course-learner/user' ).getItemById( itemId ); |
| 55 |
return item ? get( item, 'userSettings.answered', {} ) : {}; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get all questions in quiz. |
| 60 |
* |
| 61 |
* @param state |
| 62 |
* @return {*} |
| 63 |
*/ |
| 64 |
export function getQuestions( state ) { |
| 65 |
const { userQuiz } = state; |
| 66 |
const questions = get( userQuiz, 'questions' ); |
| 67 |
return questions ? Object.values( questions ) : []; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get property of store data. |
| 72 |
* |
| 73 |
* @param state - Store data |
| 74 |
* @param prop - Optional. NULL will return all data. |
| 75 |
* @return {*} |
| 76 |
*/ |
| 77 |
export function getData( state, prop ) { |
| 78 |
const { userQuiz } = state; |
| 79 |
|
| 80 |
if ( prop ) { |
| 81 |
return get( userQuiz, prop ); |
| 82 |
} |
| 83 |
|
| 84 |
return userQuiz; |
| 85 |
} |
| 86 |
|
| 87 |
export function getDefaultRestArgs( state ) { |
| 88 |
const { userQuiz } = state; |
| 89 |
|
| 90 |
return { |
| 91 |
itemId: userQuiz.id, |
| 92 |
courseId: userQuiz.courseId, |
| 93 |
}; |
| 94 |
} |
| 95 |
|
| 96 |
export function getQuestionAnswered( state, id ) { |
| 97 |
const { userQuiz } = state; |
| 98 |
|
| 99 |
return get( userQuiz, `answered.${ id }.answered` ) || undefined; |
| 100 |
} |
| 101 |
|
| 102 |
export function getQuestionMark( state, id ) { |
| 103 |
const { userQuiz } = state; |
| 104 |
|
| 105 |
return get( userQuiz, `answered.${ id }.mark` ) || undefined; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get current question is doing. |
| 110 |
* |
| 111 |
* @param {Object} state |
| 112 |
* @param {string} ret |
| 113 |
* @return {*} |
| 114 |
*/ |
| 115 |
export function getCurrentQuestion( state, ret = '' ) { |
| 116 |
const questionsPerPage = get( state, 'userQuiz.questionsPerPage' ) || 1; |
| 117 |
|
| 118 |
if ( questionsPerPage > 1 ) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
const currentPage = get( state, 'userQuiz.currentPage' ) || 1; |
| 123 |
return ret === 'object' ? get( state, `userQuiz.questions[${ currentPage - 1 }]` ) : get( state, `userQuiz.questionIds[${ currentPage - 1 }]` ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Return a question contains fully data with title, content, ... |
| 128 |
* |
| 129 |
* @param state |
| 130 |
* @param theId |
| 131 |
*/ |
| 132 |
const getQuestion = function getQuestion( state, theId ) { |
| 133 |
const { userQuiz } = state; |
| 134 |
const s = select( 'learnpress/quiz' ); |
| 135 |
const questions = s.getQuestions(); |
| 136 |
|
| 137 |
return questions.find( ( q ) => { |
| 138 |
return q.id == theId; |
| 139 |
} ); |
| 140 |
}; |
| 141 |
|
| 142 |
export { getQuestion }; |
| 143 |
|
| 144 |
/** |
| 145 |
* If user has used 'Instant check' for a question. |
| 146 |
* |
| 147 |
* @param {Object} state - Global state for app. |
| 148 |
* @param {number} id |
| 149 |
* @return {boolean} |
| 150 |
*/ |
| 151 |
export function isCheckedAnswer( state, id ) { |
| 152 |
const checkedQuestions = get( state, 'userQuiz.checkedQuestions' ) || []; |
| 153 |
|
| 154 |
return checkedQuestions.indexOf( id ) !== -1; |
| 155 |
} |
| 156 |
|
| 157 |
export function isCorrect( state, id ) { |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Get questions user has selected answers. |
| 163 |
* |
| 164 |
* @param {Object} state. Global app state |
| 165 |
* @param state |
| 166 |
* @param {number} questionId |
| 167 |
* @return {{}} |
| 168 |
*/ |
| 169 |
const getQuestionsSelectedAnswers = function( state, questionId ) { |
| 170 |
const data = get( state, 'userQuiz.answered' ); |
| 171 |
const returnData = {}; |
| 172 |
|
| 173 |
for ( const loopId in data ) { |
| 174 |
if ( ! data.hasOwnProperty( loopId ) ) { |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
// Answer filled by user |
| 179 |
if ( ( data[ loopId ].temp || data[ loopId ].blanks ) ) { |
| 180 |
// If specific a question then return it only. |
| 181 |
if ( questionId && loopId === questionId ) { |
| 182 |
return data[ loopId ].answered; |
| 183 |
} |
| 184 |
|
| 185 |
returnData[ loopId ] = data[ loopId ].answered; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
return returnData; |
| 190 |
}; |
| 191 |
|
| 192 |
export { getQuestionsSelectedAnswers }; |
| 193 |
|
| 194 |
/** |
| 195 |
* Get mark user earned. |
| 196 |
* Just for questions user has used 'Instant check' button. |
| 197 |
* |
| 198 |
* @param state |
| 199 |
* @return {number} |
| 200 |
*/ |
| 201 |
export function getUserMark( state ) { |
| 202 |
const userQuiz = state.userQuiz || {}; |
| 203 |
const { |
| 204 |
answered, |
| 205 |
negativeMarking, |
| 206 |
questions, |
| 207 |
checkedQuestions, |
| 208 |
} = userQuiz; |
| 209 |
|
| 210 |
let totalMark = 0; |
| 211 |
|
| 212 |
for ( let id in answered ) { |
| 213 |
if ( ! answered.hasOwnProperty( id ) ) { |
| 214 |
continue; |
| 215 |
} |
| 216 |
|
| 217 |
id = parseInt( id ); |
| 218 |
const data = answered[ id ]; |
| 219 |
const questionMark = data.questionMark ? data.questionMark : ( function() { |
| 220 |
const question = questions.find( ( q ) => { |
| 221 |
return q.id === id; |
| 222 |
} ); |
| 223 |
|
| 224 |
return question ? question.point : 0; |
| 225 |
}() ); |
| 226 |
|
| 227 |
const isChecked = checkedQuestions.indexOf( id ) !== -1; |
| 228 |
|
| 229 |
if ( data.temp ) { |
| 230 |
continue; |
| 231 |
} |
| 232 |
|
| 233 |
if ( negativeMarking ) { |
| 234 |
if ( data.answered ) { |
| 235 |
totalMark = data.correct ? totalMark + data.mark : totalMark - questionMark; |
| 236 |
} |
| 237 |
} else if ( data.answered && data.correct ) { |
| 238 |
totalMark += data.mark; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
return totalMark > 0 ? totalMark : 0; |
| 243 |
} |
| 244 |
|