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