forked from openedx/frontend-app-learning
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.js
More file actions
129 lines (112 loc) · 5.32 KB
/
Copy pathapi.js
File metadata and controls
129 lines (112 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { camelCaseObject, getConfig } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient, getAuthenticatedUser } from '@edx/frontend-platform/auth';
import { appendBrowserTimezoneToUrl } from '../../utils';
import {
normalizeLearningSequencesData, normalizeMetadata, normalizeOutlineBlocks, normalizeSequenceMetadata,
} from './utils';
// Do not add further calls to this API - we don't like making use of the modulestore if we can help it
export const getSequenceForUnitDeprecatedUrl = (courseId) => {
const authenticatedUser = getAuthenticatedUser();
const url = new URL(`${getConfig().LMS_BASE_URL}/api/courses/v2/blocks/`);
url.searchParams.append('course_id', courseId);
url.searchParams.append('username', authenticatedUser ? authenticatedUser.username : '');
url.searchParams.append('depth', 3);
url.searchParams.append('requested_fields', 'children,discussions_url');
return url;
};
export async function getSequenceForUnitDeprecated(courseId, unitId) {
const url = getSequenceForUnitDeprecatedUrl(courseId);
const { data } = await getAuthenticatedHttpClient().get(url.href, {});
const parent = Object.values(data.blocks).find(block => block.type === 'sequential' && block.children.includes(unitId));
return parent?.id;
}
export async function getLearningSequencesOutline(courseId) {
const outlineUrl = new URL(`${getConfig().LMS_BASE_URL}/api/learning_sequences/v1/course_outline/${courseId}`);
const { data } = await getAuthenticatedHttpClient().get(outlineUrl.href, {});
return normalizeLearningSequencesData(data);
}
export async function getCourseMetadata(courseId) {
let url = `${getConfig().LMS_BASE_URL}/api/courseware/course/${courseId}`;
url = appendBrowserTimezoneToUrl(url);
const metadata = await getAuthenticatedHttpClient().get(url);
return normalizeMetadata(metadata);
}
export async function getSequenceMetadata(sequenceId, params) {
const { data } = await getAuthenticatedHttpClient()
.get(`${getConfig().LMS_BASE_URL}/api/courseware/sequence/${sequenceId}`, { params });
return normalizeSequenceMetadata(data);
}
const getSequenceHandlerUrl = (courseId, sequenceId) => `${getConfig().LMS_BASE_URL}/courses/${courseId}/xblock/${sequenceId}/handler`;
export async function getBlockCompletion(courseId, sequenceId, usageKey) {
const { data } = await getAuthenticatedHttpClient().post(
`${getSequenceHandlerUrl(courseId, sequenceId)}/get_completion`,
{ usage_key: usageKey },
);
return data.complete === true;
}
export async function postSequencePosition(courseId, sequenceId, activeUnitIndex) {
const { data } = await getAuthenticatedHttpClient().post(
`${getSequenceHandlerUrl(courseId, sequenceId)}/goto_position`,
// Position is 1-indexed on the server and 0-indexed in this app. Adjust here.
{ position: activeUnitIndex + 1 },
);
return data;
}
export async function getResumeBlock(courseId) {
const url = new URL(`${getConfig().LMS_BASE_URL}/api/courseware/resume/${courseId}`);
const { data } = await getAuthenticatedHttpClient().get(url.href, {});
return camelCaseObject(data);
}
export async function postIntegritySignature(courseId) {
const { data } = await getAuthenticatedHttpClient().post(`${getConfig().LMS_BASE_URL}/api/agreements/v1/integrity_signature/${courseId}`, {});
return camelCaseObject(data);
}
export async function sendActivationEmail() {
const url = new URL(`${getConfig().LMS_BASE_URL}/api/send_account_activation_email`);
const { data } = await getAuthenticatedHttpClient().post(url.href, {});
return data;
}
export async function getCourseDiscussionConfig(courseId) {
const url = `${getConfig().LMS_BASE_URL}/api/discussion/v1/courses/${courseId}`;
const { data } = await getAuthenticatedHttpClient().get(url);
return data;
}
export async function getCourseTopics(courseId) {
const { data } = await getAuthenticatedHttpClient()
.get(`${getConfig().LMS_BASE_URL}/api/discussion/v2/course_topics/${courseId}`);
return camelCaseObject(data);
}
/**
* Get course outline structure for the courseware navigation sidebar.
* @param {string} courseId - The unique identifier for the course.
* @returns {Promise<{units: {}, sequences: {}, sections: {}}|null>}
*/
export async function getCourseOutline(courseId) {
const { data } = await getAuthenticatedHttpClient()
.get(`${getConfig().LMS_BASE_URL}/api/course_home/v1/navigation/${courseId}`);
return data.blocks ? normalizeOutlineBlocks(courseId, data.blocks) : null;
}
/**
* Get waffle flag value that enables completion tracking.
* @param {string} courseId - The unique identifier for the course.
* @returns {Promise<{enable_completion_tracking: boolean}>} - The object
* of boolean values of enabling of the completion tracking.
*/
export async function getCoursewareOutlineSidebarToggles(courseId) {
const url = new URL(`${getConfig().LMS_BASE_URL}/courses/${courseId}/courseware-navigation-sidebar/toggles/`);
const { data } = await getAuthenticatedHttpClient().get(url.href);
return {
enable_completion_tracking: data.enable_completion_tracking || false,
};
}
export async function postTimeSpent(courseId, seconds) {
try {
const { data } = await getAuthenticatedHttpClient().patch(
`${getConfig().LMS_BASE_URL}/api/course_assignments/v1/time-spent/${courseId}/`,
{ seconds_spent: seconds },
);
return data;
} catch (error) {
return null;
}
}