From fad3dc73db2b5776879aa4d38a3dac619c3a6f00 Mon Sep 17 00:00:00 2001 From: Aaron shiel <57824522+aaronshiel@users.noreply.github.com> Date: Thu, 28 Sep 2023 14:23:50 -0700 Subject: [PATCH] lrs reports extract referrer better, uploads are not removed from state when complete (#309) --- .../hooks/graphql/use-with-record-state.tsx | 26 ++++++---- client/src/lrs-helpers.ts | 47 +++++++++++++++++-- client/src/store/slices/mentor/index.ts | 2 +- 3 files changed, 60 insertions(+), 15 deletions(-) diff --git a/client/src/hooks/graphql/use-with-record-state.tsx b/client/src/hooks/graphql/use-with-record-state.tsx index ea7182f0..97dc0daa 100644 --- a/client/src/hooks/graphql/use-with-record-state.tsx +++ b/client/src/hooks/graphql/use-with-record-state.tsx @@ -120,10 +120,10 @@ export function useWithRecordState( return; } const { videoId, subject, category, status } = filter; - let answers = mentorAnswers; + let _answers = mentorAnswers; if (videoId && !subject) { const ids = Array.isArray(videoId) ? videoId : [videoId]; - answers = answers.filter( + _answers = _answers.filter( (a) => ids.includes(a.question) || ids.includes(a.questionClientId) ); } else if (subject) { @@ -132,22 +132,30 @@ export function useWithRecordState( const sQuestions = s.questions.filter( (q) => !category || `${q.category?.id}` === category ); - answers = answers.filter((a) => + _answers = _answers.filter((a) => sQuestions.map((q) => q.question).includes(a.question) ); } } const answerStates: AnswerState[] = []; - for (const a of answers) { + for (const a of _answers) { const q = getValueIfKeyExists(a.question, mentorQuestions); - let checkStatus = !status; + // we don't want to remove questions that are already in the recording state + const answerAlreadyInState = Boolean( + answers.find((as) => as.answer.question === a.question) + ); + let checkStatus = !status || answerAlreadyInState; if (status === Status.COMPLETE) { - checkStatus = isAnswerComplete(a, q?.question?.name, mentorType); + checkStatus = + isAnswerComplete(a, q?.question?.name, mentorType) || + answerAlreadyInState; } else if (status === Status.INCOMPLETE) { - checkStatus = !isAnswerComplete(a, q?.question?.name, mentorType); + checkStatus = + !isAnswerComplete(a, q?.question?.name, mentorType) || + answerAlreadyInState; } else if (status === Status.NONE) { - checkStatus = a.status === Status.NONE; + checkStatus = a.status === Status.NONE || answerAlreadyInState; } if ( q?.question && @@ -166,7 +174,7 @@ export function useWithRecordState( } } //if after filtering through the answers we end up with none, then go back to My Mentor page - if (!answers.length) { + if (!_answers.length) { navigate("/"); } setAnswers(answerStates); diff --git a/client/src/lrs-helpers.ts b/client/src/lrs-helpers.ts index 94b13739..02f88867 100644 --- a/client/src/lrs-helpers.ts +++ b/client/src/lrs-helpers.ts @@ -231,7 +231,6 @@ const getChatSessionId = (statement: Statement): string => { key.includes("chatSessionId") ); if (!targetKey) { - console.log("no chatSessionId key found in extensions"); return ""; } const chatSessionIdFromExtensions = extensions[targetKey]; @@ -252,7 +251,6 @@ const getSessionId = (statement: Statement): string => { key.includes("sessionId") ); if (!targetKey) { - console.log("no sessionId key found in extensions"); return ""; } const sessionIdFromExtensions = extensions[targetKey]; @@ -368,13 +366,52 @@ export const getVerb = (statement: Statement): string => { * @returns The referrer of the statement. */ const getReferrer = (statement: Statement): string => { + const resultExtensions = statement.result?.extensions; + if (!resultExtensions) { + return ""; + } + let referrer = ""; + const verbList = [ + "https://mentorpal.org/xapi/verb/initialized", + "https://mentorpal.org/xapi/verb/selected", + "https://mentorpal.org/xapi/verb/suspended", + "https://mentorpal.org/xapi/verb/answer-playback-started", + ]; + const verb = statement.verb?.id || ""; + + if (verb) { + try { + referrer = resultExtensions[verb]["referrer"] || ""; + } catch (err) { + referrer = ""; + } + } + + if (!referrer) { + for (let i = 0; i < verbList.length; i++) { + try { + referrer = resultExtensions[verbList[i]]["referrer"] || ""; + } catch (err) { + referrer = ""; + } + if (referrer) { + break; + } + } + } + + if (referrer && referrer !== "null" && referrer !== "no referrer") { + return referrer; + } + try { + // if no referrer found via extensions, try to find in url const object: Activity = statement.object as Activity; const url = object.id; - const referrer = new URLSearchParams(url).get("referrer") || "-"; - return referrer; + const referrerFromUrl = new URLSearchParams(url).get("referrer") || "-"; + return referrerFromUrl; } catch (err) { - return ""; + return "-"; } }; diff --git a/client/src/store/slices/mentor/index.ts b/client/src/store/slices/mentor/index.ts index 2449862f..ec1f79e5 100644 --- a/client/src/store/slices/mentor/index.ts +++ b/client/src/store/slices/mentor/index.ts @@ -197,7 +197,7 @@ export const mentorSlice = createSlice({ updateAnswer: (state, action: PayloadAction) => { if (state.data) { state.data.answers = state.data.answers.map((a) => - a._id === action.payload._id ? action.payload : a + a.question === action.payload.question ? action.payload : a ); } },