@openstax/ts-utils 1.5.1 → 1.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  import { AccountsGateway } from '../accountsGateway';
2
2
  import { AuthProvider } from '../authProvider';
3
3
  import { Logger } from '../logger';
4
+ import { ActivityState } from './attempt-utils';
4
5
  import { LrsGateway } from '.';
5
6
  export interface Grade {
6
7
  scoreGiven: number;
@@ -12,21 +13,32 @@ export interface Grade {
12
13
  }
13
14
  export declare const getRegistrationAttemptInfo: (lrs: LrsGateway, registration: string, options?: {
14
15
  anyUser?: boolean | undefined;
16
+ user?: string | undefined;
15
17
  } | undefined) => Promise<{
16
- [key: string]: import("./attempt-utils").ActivityState;
18
+ [key: string]: ActivityState;
17
19
  }>;
20
+ export declare const getScoreGrade: (score: {
21
+ scaled?: number;
22
+ raw?: number;
23
+ min?: number;
24
+ max?: number;
25
+ }, completed: boolean, userId: string, maxScore?: number | undefined) => Grade;
18
26
  export declare const getCurrentGrade: (services: {
19
27
  lrs: LrsGateway;
20
28
  ltiAuthProvider: AuthProvider;
21
29
  }, registration: string, options?: {
30
+ incompleteAttemptCallback?: ((info: ActivityState) => Promise<Grade>) | undefined;
22
31
  scoreMaximum?: number | undefined;
23
32
  userId?: string | undefined;
24
33
  } | undefined) => Promise<Grade | null>;
25
- export declare const getAllGradesForAssignment: (services: {
34
+ export declare const getAssignmentGrades: (services: {
26
35
  accountsGateway: AccountsGateway;
27
36
  lrs: LrsGateway;
28
37
  logger: Logger;
29
38
  }, registration: string, options?: {
39
+ anyUser?: boolean | undefined;
40
+ incompleteAttemptsCallback?: ((mappedInfo: [string, ActivityState][]) => Promise<Grade[]>) | undefined;
30
41
  platformId?: string | undefined;
31
42
  scoreMaximum?: number | undefined;
43
+ user?: string | undefined;
32
44
  } | undefined) => Promise<Grade[]>;
@@ -1,3 +1,4 @@
1
+ import partition from 'lodash/fp/partition';
1
2
  import { roundToPrecision } from '../..';
2
3
  import { resolveAttemptInfo } from './attempt-utils';
3
4
  export const getRegistrationAttemptInfo = async (lrs, registration, options) => {
@@ -13,17 +14,15 @@ export const getRegistrationAttemptInfo = async (lrs, registration, options) =>
13
14
  });
14
15
  const result = {};
15
16
  for (const [userUuid, userStatements] of Object.entries(statementsPerUser)) {
16
- result[userUuid] = resolveAttemptInfo(userStatements);
17
+ result[userUuid] = resolveAttemptInfo(userStatements, { currentPreference: 'oldest' });
17
18
  }
18
19
  return result;
19
20
  };
20
21
  // generates a payload that can be sent to the LMS, documentation here:
21
22
  // lti: http://www.imsglobal.org/spec/lti-ags/v2p0#score-publish-service
22
23
  // ltijs: https://cvmcosta.me/ltijs/#/grading
23
- const getInfoGrade = (info, userId, maxScore) => {
24
- var _a;
25
- const completed = info.currentAttemptCompleted;
26
- const { raw, scaled, max } = ((_a = completed === null || completed === void 0 ? void 0 : completed.result) === null || _a === void 0 ? void 0 : _a.score) || {};
24
+ export const getScoreGrade = (score, completed, userId, maxScore) => {
25
+ const { raw, scaled, max } = score;
27
26
  const scoreMaximum = maxScore !== null && maxScore !== void 0 ? maxScore : 100;
28
27
  const scoreGiven = raw && max
29
28
  ? scoreMaximum / max * raw
@@ -38,18 +37,35 @@ const getInfoGrade = (info, userId, maxScore) => {
38
37
  scoreGiven: roundToPrecision(scoreGiven, -2),
39
38
  };
40
39
  };
40
+ const getInfoGrade = (info, userId, maxScore) => {
41
+ var _a, _b;
42
+ return getScoreGrade(((_b = (_a = info.currentAttemptCompleted) === null || _a === void 0 ? void 0 : _a.result) === null || _b === void 0 ? void 0 : _b.score) || {}, !!info.currentAttemptCompleted, userId, maxScore);
43
+ };
41
44
  export const getCurrentGrade = async (services, registration, options) => {
42
45
  var _a;
43
46
  const user = await services.ltiAuthProvider.getUser();
44
47
  if (!user) {
45
48
  return null;
46
49
  }
50
+ const userId = (_a = options === null || options === void 0 ? void 0 : options.userId) !== null && _a !== void 0 ? _a : user.uuid;
51
+ const scoreMaximum = options === null || options === void 0 ? void 0 : options.scoreMaximum;
47
52
  const infoPerUser = await getRegistrationAttemptInfo(services.lrs, registration);
48
53
  const userInfo = infoPerUser[user.uuid];
49
- return getInfoGrade(userInfo !== null && userInfo !== void 0 ? userInfo : resolveAttemptInfo([]), (_a = options === null || options === void 0 ? void 0 : options.userId) !== null && _a !== void 0 ? _a : user.uuid, options === null || options === void 0 ? void 0 : options.scoreMaximum);
54
+ if (!userInfo) {
55
+ return getInfoGrade(resolveAttemptInfo([]), userId, scoreMaximum);
56
+ }
57
+ if (userInfo.currentAttemptCompleted || !(options === null || options === void 0 ? void 0 : options.incompleteAttemptCallback)) {
58
+ return getInfoGrade(userInfo, userId, scoreMaximum);
59
+ }
60
+ return options.incompleteAttemptCallback(userInfo);
50
61
  };
51
- export const getAllGradesForAssignment = async (services, registration, options) => {
52
- const infoPerUserUuid = await getRegistrationAttemptInfo(services.lrs, registration, { anyUser: true });
53
- const mappedResults = await services.accountsGateway.mapUserUuids(infoPerUserUuid, services.logger, options === null || options === void 0 ? void 0 : options.platformId);
54
- return mappedResults.map(([userId, userInfo]) => getInfoGrade(userInfo, userId, options === null || options === void 0 ? void 0 : options.scoreMaximum));
62
+ export const getAssignmentGrades = async (services, registration, options) => {
63
+ const infoPerUserUuid = await getRegistrationAttemptInfo(services.lrs, registration, { anyUser: options === null || options === void 0 ? void 0 : options.anyUser, user: options === null || options === void 0 ? void 0 : options.user });
64
+ const mappedInfo = await services.accountsGateway.mapUserUuids(infoPerUserUuid, services.logger, options === null || options === void 0 ? void 0 : options.platformId);
65
+ const gradeCompletedAttemptsOnly = (results) => results.map(([userId, userInfo]) => getInfoGrade(userInfo, userId, options === null || options === void 0 ? void 0 : options.scoreMaximum));
66
+ if (!(options === null || options === void 0 ? void 0 : options.incompleteAttemptsCallback)) {
67
+ return gradeCompletedAttemptsOnly(mappedInfo);
68
+ }
69
+ const [incompleteInfo, completedInfo] = partition((info) => info[1].currentAttemptCompleted === undefined)(mappedInfo);
70
+ return gradeCompletedAttemptsOnly(completedInfo).concat(await options.incompleteAttemptsCallback(incompleteInfo));
55
71
  };