PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / assets / src / js / admin / courses / view-students-modal.js

view-students-modal.js in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.8, at assets/src/js/admin/courses/view-students-modal.js

355 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import SweetAlert from 'sweetalert2';
2 import * as lpUtils from 'lpAssetsJsPath/utils.js';
3
4 export class ViewStudentsModal {
5 constructor() {
6 this.isRequesting = false;
7 this.activeCourseId = 0;
8 this.init();
9 }
10
11 static selectors = {
12 wrap: '#lp-modal-enrolled-wrap',
13 form: '#lp-modal-enrolled-form',
14 toolbarTemplate: '#lp-tmpl-enrolled-students-toolbar-modal',
15 targetTemplate: '#lp-tmpl-enrolled-students-target-modal',
16 toolbar: '.lp-enrolled-students-table-toolbar--modal',
17 courseTrigger: '.lp-btn-view-students',
18 searchInput: '#lp-modal-enrolled-search-input',
19 startDateInput: '#lp-modal-enrolled-filter-start-date',
20 endDateInput: '#lp-modal-enrolled-filter-end-date',
21 searchBtn: '.lp-enrolled-btn-search-modal',
22 clearBtn: '.lp-enrolled-btn-clear-modal',
23 modalSearchFields:
24 '#lp-modal-enrolled-search-input, #lp-modal-enrolled-filter-start-date, #lp-modal-enrolled-filter-end-date',
25 };
26
27 setButtonLoadingState( btn, isLoading ) {
28 if ( ! btn ) {
29 return;
30 }
31
32 lpUtils.lpSetLoadingEl( btn, isLoading ? 1 : 0 );
33 }
34
35 init() {
36 if ( ViewStudentsModal._loadedEvents ) {
37 return;
38 }
39
40 ViewStudentsModal._loadedEvents = true;
41
42 lpUtils.eventHandlers( 'click', [
43 {
44 selector: ViewStudentsModal.selectors.courseTrigger,
45 class: this,
46 callBack: this.handleOpenModal.name,
47 },
48 {
49 selector: ViewStudentsModal.selectors.searchBtn,
50 class: this,
51 callBack: this.handleModalSearch.name,
52 },
53 {
54 selector: ViewStudentsModal.selectors.clearBtn,
55 class: this,
56 callBack: this.handleModalClear.name,
57 },
58 ] );
59
60 lpUtils.eventHandlers( 'keydown', [
61 {
62 selector: ViewStudentsModal.selectors.modalSearchFields,
63 class: this,
64 callBack: this.handleModalSearchOnEnter.name,
65 checkIsEventEnter: true,
66 },
67 ] );
68
69 lpUtils.eventHandlers( 'change', [
70 {
71 selector: ViewStudentsModal.selectors.startDateInput,
72 class: this,
73 callBack: this.checkDatesRange.name,
74 },
75 {
76 selector: ViewStudentsModal.selectors.endDateInput,
77 class: this,
78 callBack: this.checkDatesRange.name,
79 },
80 ] );
81 }
82
83 handleOpenModal( args ) {
84 const btn = args?.target?.closest(
85 ViewStudentsModal.selectors.courseTrigger
86 );
87 if (
88 ! btn ||
89 this.isRequesting ||
90 btn.classList.contains( 'loading' ) ) {
91 return;
92 }
93
94 const courseId = parseInt( btn.dataset.courseId, 10 ) || 0;
95 if ( ! courseId ) {
96 return;
97 }
98
99 const courseTitle = btn.dataset.courseTitle || '';
100 this.activeCourseId = courseId;
101
102 this.setButtonLoadingState( btn, true );
103 this.openModal( courseId, courseTitle, btn );
104 }
105
106 handleModalSearch( args ) {
107 const btn = args?.target?.closest(
108 ViewStudentsModal.selectors.searchBtn
109 );
110 if ( ! btn || ! this.activeCourseId ) {
111 return;
112 }
113
114 if ( args?.e ) {
115 args.e.preventDefault();
116 }
117
118 if (
119 this.isRequesting ||
120 btn.classList.contains( 'loading' ) ||
121 btn.disabled
122 ) {
123 return;
124 }
125
126 this.setButtonLoadingState( btn, true );
127 this.loadEnrolledStudents(
128 this.activeCourseId,
129 1,
130 btn
131 );
132 }
133
134 handleModalSearchOnEnter( args ) {
135 if ( args?.e ) {
136 args.e.preventDefault();
137 }
138
139 const form = this.getModalForm();
140 if ( ! form ) {
141 return;
142 }
143
144 const btn = form.querySelector( ViewStudentsModal.selectors.searchBtn );
145 if ( ! btn ) {
146 return;
147 }
148
149 this.handleModalSearch( { ...args, target: btn } );
150 }
151
152 handleModalClear( args ) {
153 const btn = args?.target?.closest(
154 ViewStudentsModal.selectors.clearBtn
155 );
156 const form = this.getModalForm();
157 if ( ! btn || ! form || ! this.activeCourseId ) {
158 return;
159 }
160
161 if ( args?.e ) {
162 args.e.preventDefault();
163 }
164
165 if (
166 this.isRequesting ||
167 btn.classList.contains( 'loading' )
168 ) {
169 return;
170 }
171
172 form.reset();
173 this.setButtonLoadingState( btn, true );
174 this.loadEnrolledStudents(
175 this.activeCourseId,
176 1,
177 btn
178 );
179 }
180
181 getModalPopup() {
182 return SweetAlert.getPopup ? SweetAlert.getPopup() : null;
183 }
184
185 getModalToolbarHtml() {
186 const template = document.querySelector(
187 ViewStudentsModal.selectors.toolbarTemplate
188 );
189
190 return template ? template.innerHTML : '';
191 }
192
193 getModalTargetHtml() {
194 const template = document.querySelector(
195 ViewStudentsModal.selectors.targetTemplate
196 );
197
198 return template ? template.innerHTML : '';
199 }
200
201 getAjaxHandle() {
202 const ajaxHandle = window.lpAJAXG;
203 if (
204 ! ajaxHandle ||
205 typeof ajaxHandle.getDataSetCurrent !== 'function' ||
206 typeof ajaxHandle.setDataSetCurrent !== 'function' ||
207 typeof ajaxHandle.showHideLoading !== 'function' ||
208 typeof ajaxHandle.fetchAJAX !== 'function'
209 ) {
210 return null;
211 }
212
213 return ajaxHandle;
214 }
215
216 getModalForm() {
217 const popup = this.getModalPopup();
218 if ( ! popup ) {
219 return null;
220 }
221
222 return popup.querySelector( ViewStudentsModal.selectors.form );
223 }
224
225 getModalFilterArgs( dataArgs = {} ) {
226 const form = this.getModalForm();
227 if ( ! form ) {
228 return dataArgs;
229 }
230
231 return lpUtils.mergeDataWithDatForm( form, dataArgs );
232 }
233
234 loadEnrolledStudents( courseId, paged, elLoading = null ) {
235 const wrap = document.querySelector( ViewStudentsModal.selectors.wrap );
236 const elTarget = wrap?.querySelector( '.lp-target' );
237 const ajaxHandle = this.getAjaxHandle();
238 if ( ! wrap || ! elTarget || ! ajaxHandle || this.isRequesting ) {
239 return;
240 }
241
242 this.isRequesting = true;
243 if ( elLoading ) {
244 this.setButtonLoadingState( elLoading, true );
245 }
246
247 const dataSend = ajaxHandle.getDataSetCurrent( elTarget );
248 dataSend.args = this.getModalFilterArgs( dataSend.args || {} );
249 dataSend.args.course_id = parseInt( courseId, 10 ) || 0;
250 dataSend.args.paged = paged;
251 ajaxHandle.setDataSetCurrent( elTarget, dataSend );
252 ajaxHandle.showHideLoading( elTarget, 1 );
253
254 const callBack = {
255 success: ( response ) => {
256 elTarget.innerHTML = response.data.content;
257 },
258 error: ( err ) => {
259 console.error( err );
260 },
261 completed: () => {
262 this.isRequesting = false;
263 ajaxHandle.showHideLoading( elTarget, 0 );
264 if ( elLoading ) {
265 this.setButtonLoadingState( elLoading, false );
266 }
267 },
268 };
269
270 ajaxHandle.fetchAJAX( dataSend, callBack );
271 }
272
273 openModal( courseId, courseTitle, elTrigger = null ) {
274 const modalToolbarHtml = this.getModalToolbarHtml();
275 const modalTargetHtml = this.getModalTargetHtml();
276
277 if ( ! modalToolbarHtml || ! modalTargetHtml ) {
278 if ( elTrigger ) {
279 this.setButtonLoadingState( elTrigger, false );
280 }
281 return;
282 }
283
284 this.activeCourseId = parseInt( courseId, 10 ) || 0;
285
286 SweetAlert.fire( {
287 title: `${ courseTitle }`,
288 html: modalToolbarHtml + modalTargetHtml,
289 width: '80%',
290 showConfirmButton: false,
291 showCloseButton: true,
292 didOpen: () => {
293 this.loadEnrolledStudents(
294 this.activeCourseId,
295 1,
296 elTrigger
297 );
298 },
299 didClose: () => {
300 this.activeCourseId = 0;
301 if ( elTrigger ) {
302 this.setButtonLoadingState( elTrigger, false );
303 }
304 },
305 } );
306 }
307
308 // Ensure start date is not after end date and vice versa. If invalid, adjust the other date to match.
309 checkDatesRange( args ) {
310 const { e } = args;
311 const elInput = e?.target;
312 if ( ! elInput ) {
313 return;
314 }
315
316 const elForm = elInput.closest( ViewStudentsModal.selectors.form );
317 if ( ! elForm ) {
318 return;
319 }
320
321 const startDateInput = elForm.querySelector(
322 ViewStudentsModal.selectors.startDateInput,
323 );
324 const endDateInput = elForm.querySelector(
325 ViewStudentsModal.selectors.endDateInput,
326 );
327
328 if ( elInput === startDateInput ) {
329 if ( startDateInput.value ) {
330 endDateInput.min = startDateInput.value;
331 if (
332 endDateInput.value &&
333 endDateInput.value < startDateInput.value
334 ) {
335 endDateInput.value = startDateInput.value;
336 }
337 } else {
338 endDateInput.min = '';
339 }
340 } else if ( elInput === endDateInput ) {
341 if ( endDateInput.value ) {
342 startDateInput.max = endDateInput.value;
343 if (
344 startDateInput.value &&
345 startDateInput.value > endDateInput.value
346 ) {
347 startDateInput.value = endDateInput.value;
348 }
349 } else {
350 startDateInput.max = '';
351 }
352 }
353 }
354 }
355