@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,6 +1,10 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getAllGradesForAssignment = exports.getCurrentGrade = exports.getRegistrationAttemptInfo = void 0;
6
+ exports.getAssignmentGrades = exports.getCurrentGrade = exports.getScoreGrade = exports.getRegistrationAttemptInfo = void 0;
7
+ const partition_1 = __importDefault(require("lodash/fp/partition"));
4
8
  const __1 = require("../..");
5
9
  const attempt_utils_1 = require("./attempt-utils");
6
10
  const getRegistrationAttemptInfo = async (lrs, registration, options) => {
@@ -16,7 +20,7 @@ const getRegistrationAttemptInfo = async (lrs, registration, options) => {
16
20
  });
17
21
  const result = {};
18
22
  for (const [userUuid, userStatements] of Object.entries(statementsPerUser)) {
19
- result[userUuid] = (0, attempt_utils_1.resolveAttemptInfo)(userStatements);
23
+ result[userUuid] = (0, attempt_utils_1.resolveAttemptInfo)(userStatements, { currentPreference: 'oldest' });
20
24
  }
21
25
  return result;
22
26
  };
@@ -24,10 +28,8 @@ exports.getRegistrationAttemptInfo = getRegistrationAttemptInfo;
24
28
  // generates a payload that can be sent to the LMS, documentation here:
25
29
  // lti: http://www.imsglobal.org/spec/lti-ags/v2p0#score-publish-service
26
30
  // ltijs: https://cvmcosta.me/ltijs/#/grading
27
- const getInfoGrade = (info, userId, maxScore) => {
28
- var _a;
29
- const completed = info.currentAttemptCompleted;
30
- const { raw, scaled, max } = ((_a = completed === null || completed === void 0 ? void 0 : completed.result) === null || _a === void 0 ? void 0 : _a.score) || {};
31
+ const getScoreGrade = (score, completed, userId, maxScore) => {
32
+ const { raw, scaled, max } = score;
31
33
  const scoreMaximum = maxScore !== null && maxScore !== void 0 ? maxScore : 100;
32
34
  const scoreGiven = raw && max
33
35
  ? scoreMaximum / max * raw
@@ -42,20 +44,38 @@ const getInfoGrade = (info, userId, maxScore) => {
42
44
  scoreGiven: (0, __1.roundToPrecision)(scoreGiven, -2),
43
45
  };
44
46
  };
47
+ exports.getScoreGrade = getScoreGrade;
48
+ const getInfoGrade = (info, userId, maxScore) => {
49
+ var _a, _b;
50
+ return (0, exports.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);
51
+ };
45
52
  const getCurrentGrade = async (services, registration, options) => {
46
53
  var _a;
47
54
  const user = await services.ltiAuthProvider.getUser();
48
55
  if (!user) {
49
56
  return null;
50
57
  }
58
+ const userId = (_a = options === null || options === void 0 ? void 0 : options.userId) !== null && _a !== void 0 ? _a : user.uuid;
59
+ const scoreMaximum = options === null || options === void 0 ? void 0 : options.scoreMaximum;
51
60
  const infoPerUser = await (0, exports.getRegistrationAttemptInfo)(services.lrs, registration);
52
61
  const userInfo = infoPerUser[user.uuid];
53
- return getInfoGrade(userInfo !== null && userInfo !== void 0 ? userInfo : (0, attempt_utils_1.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);
62
+ if (!userInfo) {
63
+ return getInfoGrade((0, attempt_utils_1.resolveAttemptInfo)([]), userId, scoreMaximum);
64
+ }
65
+ if (userInfo.currentAttemptCompleted || !(options === null || options === void 0 ? void 0 : options.incompleteAttemptCallback)) {
66
+ return getInfoGrade(userInfo, userId, scoreMaximum);
67
+ }
68
+ return options.incompleteAttemptCallback(userInfo);
54
69
  };
55
70
  exports.getCurrentGrade = getCurrentGrade;
56
- const getAllGradesForAssignment = async (services, registration, options) => {
57
- const infoPerUserUuid = await (0, exports.getRegistrationAttemptInfo)(services.lrs, registration, { anyUser: true });
58
- const mappedResults = await services.accountsGateway.mapUserUuids(infoPerUserUuid, services.logger, options === null || options === void 0 ? void 0 : options.platformId);
59
- return mappedResults.map(([userId, userInfo]) => getInfoGrade(userInfo, userId, options === null || options === void 0 ? void 0 : options.scoreMaximum));
71
+ const getAssignmentGrades = async (services, registration, options) => {
72
+ const infoPerUserUuid = await (0, exports.getRegistrationAttemptInfo)(services.lrs, registration, { anyUser: options === null || options === void 0 ? void 0 : options.anyUser, user: options === null || options === void 0 ? void 0 : options.user });
73
+ const mappedInfo = await services.accountsGateway.mapUserUuids(infoPerUserUuid, services.logger, options === null || options === void 0 ? void 0 : options.platformId);
74
+ const gradeCompletedAttemptsOnly = (results) => results.map(([userId, userInfo]) => getInfoGrade(userInfo, userId, options === null || options === void 0 ? void 0 : options.scoreMaximum));
75
+ if (!(options === null || options === void 0 ? void 0 : options.incompleteAttemptsCallback)) {
76
+ return gradeCompletedAttemptsOnly(mappedInfo);
77
+ }
78
+ const [incompleteInfo, completedInfo] = (0, partition_1.default)((info) => info[1].currentAttemptCompleted === undefined)(mappedInfo);
79
+ return gradeCompletedAttemptsOnly(completedInfo).concat(await options.incompleteAttemptsCallback(incompleteInfo));
60
80
  };
61
- exports.getAllGradesForAssignment = getAllGradesForAssignment;
81
+ exports.getAssignmentGrades = getAssignmentGrades;