PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.6
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.6
4.4.9.1 4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 All 141 releases
learnpress / assets / src / apps / js / frontend / quiz / store / actions.js

actions.js in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.6, at assets/src/apps/js/frontend/quiz/store/actions.js

366 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { dispatch, select, apiFetch } from '@learnpress/data-controls';
2 import { select as wpSelect, dispatch as wpDispatch } from '@wordpress/data';
3
4 function _dispatch() {
5 const args = [].slice.call( arguments, 2 );
6 const d = wpDispatch( arguments[ 0 ] );
7 const f = arguments[ 1 ];
8 d[ f ]( ...args );
9 }
10
11 const { camelCaseDashObjectKeys, Hook } = LP;
12 /**
13 * Set user data for app.
14 *
15 * @param key
16 * @param data
17 */
18 export function setQuizData( key, data ) {
19 if ( typeof key === 'string' ) {
20 data = { [ key ]: data };
21 } else {
22 data = key;
23 }
24
25 return {
26 type: 'SET_QUIZ_DATA',
27 data: camelCaseDashObjectKeys( data ),
28 };
29 }
30
31 /**
32 * Set question will display.
33 *
34 * @param questionId
35 */
36 export function setCurrentQuestion( questionId ) {
37 return {
38 type: 'SET_CURRENT_QUESTION',
39 questionId,
40 };
41 }
42
43 export function setCurrentPage( currentPage ) {
44 return {
45 type: 'SET_CURRENT_PAGE',
46 currentPage,
47 };
48 }
49
50 export function __requestBeforeStartQuiz( quizId, courseId, userId ) {
51 return {
52 type: 'BEFORE_START_QUIZ',
53 };
54 }
55
56 export function __requestStartQuizSuccess( results, quizId, courseId, userId ) {
57 Hook.doAction( 'quiz-started', results, quizId, courseId, userId );
58 return {
59 type: 'START_QUIZ_SUCCESS',
60 quizId,
61 courseId,
62 userId,
63 results,
64 };
65 }
66
67 /**
68 * Request to api for starting a quiz.
69 */
70 const startQuiz = function*() {
71 const { itemId, courseId } = wpSelect( 'learnpress/quiz' ).getDefaultRestArgs();
72
73 const doStart = Hook.applyFilters( 'before-start-quiz', true, itemId, courseId );
74
75 if ( true !== doStart ) {
76 return;
77 }
78
79 let response = yield apiFetch( {
80 path: 'lp/v1/users/start-quiz',
81 method: 'POST',
82 data: {
83 item_id: itemId,
84 course_id: courseId,
85 },
86 } );
87
88 const btnStart = document.querySelector( '.lp-button.start' );
89
90 if ( response.status !== 'error' ) {
91 response = Hook.applyFilters( 'request-start-quiz-response', response, itemId, courseId );
92 const { results } = response;
93 const { duration, status, question_ids, questions } = results;
94
95 // No require enroll
96 if ( lpQuizSettings.checkNorequizenroll === 1 ) {
97 const keyQuizOff = 'quiz_off_' + lpQuizSettings.id;
98 window.localStorage.removeItem( keyQuizOff );
99 const quizDataOff = {
100 endTime: ( Date.now() + ( duration * 1000 ) ),
101 status,
102 question_ids,
103 questions,
104 };
105
106 window.localStorage.setItem( keyQuizOff, JSON.stringify( quizDataOff ) );
107
108 // Set Retake quiz
109 const keyQuizOffRetaken = 'quiz_off_retaken_' + lpQuizSettings.id;
110 let quizOffRetaken = window.localStorage.getItem( keyQuizOffRetaken );
111
112 if ( null === quizOffRetaken ) {
113 quizOffRetaken = 0;
114 } else {
115 quizOffRetaken++;
116 }
117
118 window.localStorage.setItem( keyQuizOffRetaken, quizOffRetaken );
119 // End
120 }
121
122 // Reload when start/retake quiz
123 window.localStorage.removeItem( 'LP' );
124 window.location.href = response.redirect_url;
125 //window.location.reload();
126
127 //yield _dispatch( 'learnpress/quiz', '__requestStartQuizSuccess', camelCaseDashObjectKeys( response ), itemId, courseId );
128 } else {
129 const elButtons = document.querySelector( '.quiz-buttons' );
130 const message = `<div class="learn-press-message error">${ response.message }</div>`;
131 elButtons.insertAdjacentHTML( 'afterend', message );
132 btnStart.classList.remove( 'loading' );
133 }
134 };
135
136 export { startQuiz };
137
138 export function __requestSubmitQuiz() {
139 return {
140 type: 'SUBMIT_QUIZ',
141 };
142 }
143
144 export function __requestSubmitQuizSuccess( results, quizId, courseId ) {
145 Hook.doAction( 'quiz-submitted', results, quizId, courseId );
146
147 return {
148 type: 'SUBMIT_QUIZ_SUCCESS',
149 results,
150 };
151 }
152
153 export function* submitQuiz() {
154 const {
155 getDefaultRestArgs,
156 getQuestionsSelectedAnswers,
157 } = wpSelect( 'learnpress/quiz' );
158
159 const {
160 itemId,
161 courseId,
162 } = getDefaultRestArgs();
163
164 const doSubmit = Hook.applyFilters( 'before-submit-quiz', true );
165
166 if ( true !== doSubmit ) {
167 return;
168 }
169
170 const answered = getQuestionsSelectedAnswers();
171
172 if ( lpQuizSettings.checkNorequizenroll === 1 ) {
173 const keyQuizOff = `quiz_off_${ lpQuizSettings.id }`;
174 const quizDataOffStr = window.localStorage.getItem( keyQuizOff );
175 const quizDataOff = JSON.parse( quizDataOffStr );
176 const keyAnswer = `LP_Quiz_${ itemId }_Answered`;
177 const answerDataStr = localStorage.getItem( keyAnswer );
178
179 if ( null !== answerDataStr ) {
180 const data = JSON.parse( answerDataStr );
181
182 for ( const [ k, v ] of Object.entries( data ) ) {
183 answered[ k ] = v.answered;
184 }
185 }
186
187 // Added questions not answered
188 quizDataOff.question_ids.forEach( ( question_id ) => {
189 if ( ! answered[ question_id ] ) {
190 answered[ question_id ] = '';
191 }
192 } );
193 }
194
195 // Get time spend did quiz - tungnx
196 let timeSpend = 0;
197 const elTimeSpend = document.querySelector( 'input[name=lp-quiz-time-spend]' );
198
199 if ( elTimeSpend ) {
200 timeSpend = elTimeSpend.value;
201 }
202 // End
203
204 let response = yield apiFetch( {
205 path: 'lp/v1/users/submit-quiz',
206 method: 'POST',
207 data: {
208 item_id: itemId,
209 course_id: courseId,
210 answered,
211 time_spend: timeSpend,
212 },
213 } );
214
215 response = Hook.applyFilters( 'request-submit-quiz-response', response, itemId, courseId );
216
217 if ( response.status === 'success' ) {
218 if ( lpQuizSettings.checkNorequizenroll === 1 ) {
219 const keyQuizOff = 'quiz_off_' + lpQuizSettings.id;
220 const quizDataOffStr = window.localStorage.getItem( keyQuizOff );
221 if ( null !== quizDataOffStr ) {
222 const quizDataOff = JSON.parse( quizDataOffStr );
223
224 quizDataOff.status = response.results.status;
225 quizDataOff.results = response.results.results;
226
227 window.localStorage.setItem( keyQuizOff, JSON.stringify( quizDataOff ) );
228 window.localStorage.removeItem( 'LP_Quiz_' + lpQuizSettings.id + '_Answered' );
229 }
230 }
231
232 yield _dispatch( 'learnpress/quiz', '__requestSubmitQuizSuccess', camelCaseDashObjectKeys( response.results ), itemId, courseId );
233 }
234 }
235
236 export function updateUserQuestionAnswers( questionId, answers, quizId, courseId = 0, userId = 0 ) {
237 return {
238 type: 'UPDATE_USER_QUESTION_ANSWERS',
239 questionId,
240 answers,
241 };
242 }
243
244 /**
245 * Handle when user change value on input fill in blanks.
246 *
247 * @param questionId
248 * @param idInput
249 * @param valueInput
250 * @param quizId
251 * @param courseId
252 * @param userId
253 * @since 4.2.5.9
254 * @version 1.0.0
255 */
256 export function updateUserQuestionFibAnswers( questionId, idInput, valueInput, quizId, courseId = 0, userId = 0 ) {
257 return {
258 type: 'UPDATE_USER_QUESTION_FIB_ANSWERS',
259 questionId,
260 idInput,
261 valueInput,
262 };
263 }
264
265 export function __requestShowHintSuccess( id, showHint ) {
266 return {
267 type: 'SET_QUESTION_HINT',
268 questionId: id,
269 showHint,
270 };
271 }
272
273 export function* showHint( id, showHint ) {
274 yield _dispatch( 'learnpress/quiz', '__requestShowHintSuccess', id, showHint );
275 }
276
277 export function __requestCheckAnswerSuccess( id, result ) {
278 return {
279 type: 'CHECK_ANSWER',
280 questionId: id,
281 ...result,
282 };
283 }
284
285 export function* checkAnswer( id ) {
286 const {
287 getDefaultRestArgs,
288 getQuestionAnswered,
289 } = wpSelect( 'learnpress/quiz' );
290
291 const {
292 itemId,
293 courseId,
294 } = getDefaultRestArgs();
295
296 const result = yield apiFetch( {
297 path: 'lp/v1/users/check-answer',
298 method: 'POST',
299 data: {
300 item_id: itemId,
301 course_id: courseId,
302 question_id: id,
303 answered: getQuestionAnswered( id ) || '',
304 },
305 } );
306
307 if ( result.status === 'success' ) {
308 // No require enroll
309 if ( lpQuizSettings.checkNorequizenroll === 1 ) {
310 const keyQuizOff = 'quiz_off_' + lpQuizSettings.id;
311 const quizDataOffStr = window.localStorage.getItem( keyQuizOff );
312
313 if ( null !== quizDataOffStr ) {
314 const quizDataOff = JSON.parse( quizDataOffStr );
315
316 const questionOptions = result.options;
317
318 if ( undefined === quizDataOff.checked_questions ) {
319 quizDataOff.checked_questions = [];
320 quizDataOff.checked_questions.push( id );
321 } else if ( -1 === quizDataOff.checked_questions.indexOf( id ) ) {
322 quizDataOff.checked_questions.push( id );
323 }
324
325 if ( undefined === quizDataOff.question_options ) {
326 quizDataOff.question_options = {};
327 quizDataOff.question_options[ id ] = questionOptions;
328 } else if ( undefined === quizDataOff.question_options[ id ] ) {
329 quizDataOff.question_options[ id ] = questionOptions;
330 }
331
332 window.localStorage.setItem( keyQuizOff, JSON.stringify( quizDataOff ) );
333
334 //console.log( quizDataOff );
335 }
336 }
337
338 yield _dispatch( 'learnpress/quiz', '__requestCheckAnswerSuccess', id, camelCaseDashObjectKeys( result ) );
339 }
340 }
341
342 export function markQuestionRendered( questionId ) {
343 return {
344 type: 'MARK_QUESTION_RENDERED',
345 questionId,
346 };
347 }
348
349 export function setQuizMode( mode ) {
350 return {
351 type: 'SET_QUIZ_MODE',
352 mode,
353 };
354 }
355
356 export function sendKey( keyPressed ) {
357 setTimeout( () => {
358 _dispatch( 'learnpress/quiz', 'sendKey', '' );
359 }, 300 );
360
361 return {
362 type: 'SEND_KEY',
363 keyPressed,
364 };
365 }
366