| 1 |
/** |
| 2 |
* Reset course user progress handler. |
| 3 |
* |
| 4 |
* @since 4.4.6 |
| 5 |
* @version 1.0.0 |
| 6 |
*/ |
| 7 |
import * as lpUtils from 'lpAssetsJsPath/utils.js'; |
| 8 |
import * as lpToastify from 'lpAssetsJsPath/lpToastify.js'; |
| 9 |
import { LpPopupSelectItemToAdd } from 'lpAssetsJsPath/lpPopupSelectItemToAdd.js'; |
| 10 |
import SweetAlert from "sweetalert2"; |
| 11 |
|
| 12 |
const lpPopupSelectItemToAdd = new LpPopupSelectItemToAdd(); |
| 13 |
|
| 14 |
export default class ResetCourseProgress { |
| 15 |
static selectors = { |
| 16 |
elPopupTemplate: '#lp-tmpl-select-courses-to-reset-progress', |
| 17 |
elFilterField: '.lp-filter-field', |
| 18 |
elFormFilter: '.lp-form-filter-reset-course-progress', |
| 19 |
elPopupItemsToSelect: '.lp-popup-select-courses-to-reset-progress', |
| 20 |
elBtnResetAll: '.lp-btn-reset-all-courses-progress', |
| 21 |
}; |
| 22 |
|
| 23 |
constructor() { |
| 24 |
this.btnChooseCourses = null; |
| 25 |
this.elFormFilter = null; |
| 26 |
this.elPopupItemsToSelect = null; |
| 27 |
this.elBtnResetAll = null; |
| 28 |
this.elBtnAddItemsSelected = null; |
| 29 |
this.debouncedSearchUsers = lpUtils.debounce( ( elForm ) => { |
| 30 |
this.fetchCourses( elForm ) |
| 31 |
}, 800 ); |
| 32 |
} |
| 33 |
|
| 34 |
init() { |
| 35 |
this.events(); |
| 36 |
} |
| 37 |
|
| 38 |
events() { |
| 39 |
// Check and attach events only once. |
| 40 |
if ( ResetCourseProgress._loadedEvents ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
ResetCourseProgress._loadedEvents = this; |
| 45 |
|
| 46 |
// Click events. |
| 47 |
lpUtils.eventHandlers( 'click', [ |
| 48 |
{ |
| 49 |
selector: |
| 50 |
LpPopupSelectItemToAdd.selectors |
| 51 |
.elBtnShowPopupItemsToSelect, |
| 52 |
class: this, |
| 53 |
callBack: this.handleShowPopupItemsToSelect.name, |
| 54 |
}, |
| 55 |
{ |
| 56 |
selector: |
| 57 |
LpPopupSelectItemToAdd.selectors.elBtnAddItemsSelected, |
| 58 |
class: lpPopupSelectItemToAdd, |
| 59 |
callBack: lpPopupSelectItemToAdd.addItemsSelectedToSection.name, |
| 60 |
callBackHandle: this.addItemsSelectedToSection.bind( this ), |
| 61 |
conditionBeforeCallBack: ( args ) => { |
| 62 |
// Only run when the Add button inside this tool's popup is clicked. |
| 63 |
return !! args.target.closest( |
| 64 |
ResetCourseProgress.selectors.elPopupItemsToSelect |
| 65 |
); |
| 66 |
}, |
| 67 |
}, |
| 68 |
{ |
| 69 |
selector: |
| 70 |
ResetCourseProgress.selectors |
| 71 |
.elBtnResetAll, |
| 72 |
class: this, |
| 73 |
callBack: this.resetAllCoursesProgress.name, |
| 74 |
}, |
| 75 |
] ); |
| 76 |
|
| 77 |
// Change events. |
| 78 |
lpUtils.eventHandlers( 'keyup', [ |
| 79 |
{ |
| 80 |
selector: ResetCourseProgress.selectors.elFilterField, |
| 81 |
class: this, |
| 82 |
callBack: this.filterCourses.name, |
| 83 |
} |
| 84 |
] ) |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Called when the picker button is clicked. |
| 89 |
* |
| 90 |
* @param {Object} args Event arguments. |
| 91 |
*/ |
| 92 |
handleShowPopupItemsToSelect( args ) { |
| 93 |
const { e, target } = args; |
| 94 |
|
| 95 |
this.btnChooseCourses = target.closest( |
| 96 |
'.lp-btn-choose-courses-to-reset-progress' |
| 97 |
); |
| 98 |
if ( ! this.btnChooseCourses ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
this.elPopupItemsToSelect = SweetAlert.getPopup().querySelector( |
| 103 |
ResetCourseProgress.selectors.elPopupItemsToSelect |
| 104 |
); |
| 105 |
if ( ! this.elPopupItemsToSelect ) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
this.elBtnAddItemsSelected = this.elPopupItemsToSelect.querySelector( |
| 110 |
LpPopupSelectItemToAdd.selectors.elBtnAddItemsSelected |
| 111 |
); |
| 112 |
if ( ! this.elBtnAddItemsSelected ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
this.elBtnResetAll = this.elPopupItemsToSelect.querySelector( |
| 117 |
ResetCourseProgress.selectors.elBtnResetAll |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Called after courses are selected in the popup and the action button is clicked. |
| 123 |
* |
| 124 |
* @param {Array} itemsSelectedData Selected item data from the popup. |
| 125 |
*/ |
| 126 |
addItemsSelectedToSection( itemsSelectedData ) { |
| 127 |
if ( ! this.btnChooseCourses ) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
const messageConfirm = this.elBtnAddItemsSelected.dataset.messageConfirm; |
| 132 |
if ( ! messageConfirm || confirm( messageConfirm ) === false ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
this.btnChooseCourses.textContent = |
| 137 |
this.btnChooseCourses.dataset.messageResetting; |
| 138 |
lpUtils.lpSetLoadingEl( this.btnChooseCourses, 1 ); |
| 139 |
|
| 140 |
const userItemIds = itemsSelectedData |
| 141 |
.map( ( item ) => parseInt( item.id, 10 ) ) |
| 142 |
.filter( ( id ) => ! isNaN( id ) && id > 0 ); |
| 143 |
|
| 144 |
const elSearchUser = this.elFormFilter?.querySelector( '.lp-search-user' ); |
| 145 |
const searchUserValue = elSearchUser?.value || ''; |
| 146 |
|
| 147 |
window.lpAJAXG.fetchAJAX( |
| 148 |
{ |
| 149 |
id_url: 'course-reset-progress-tool', |
| 150 |
action: 'reset_progress_courses', |
| 151 |
user_item_ids: userItemIds, |
| 152 |
search_user: searchUserValue, |
| 153 |
}, |
| 154 |
{ |
| 155 |
success: ( response ) => { |
| 156 |
const { status, message } = response; |
| 157 |
lpToastify.show( message, status ); |
| 158 |
}, |
| 159 |
error: ( error ) => { |
| 160 |
lpToastify.show( error.message, 'error' ); |
| 161 |
}, |
| 162 |
completed: () => { |
| 163 |
this.btnChooseCourses.textContent = |
| 164 |
this.btnChooseCourses.dataset.messageChoose; |
| 165 |
lpUtils.lpSetLoadingEl( this.btnChooseCourses, 0 ); |
| 166 |
}, |
| 167 |
} |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Reset all courses progress. |
| 173 |
* |
| 174 |
* @param {Object} args Event arguments. |
| 175 |
*/ |
| 176 |
resetAllCoursesProgress( args ) { |
| 177 |
const { e, target } = args; |
| 178 |
|
| 179 |
const elPopupItemsToSelect = target.closest( ResetCourseProgress.selectors.elPopupItemsToSelect ); |
| 180 |
if ( ! elPopupItemsToSelect ) { |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
const elFormFilter = elPopupItemsToSelect.querySelector( ResetCourseProgress.selectors.elFormFilter ); |
| 185 |
if ( ! elFormFilter ) { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
const messageConfirm = this.elBtnResetAll.dataset.messageConfirm; |
| 190 |
if ( ! messageConfirm || confirm( messageConfirm ) === false ) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
// Show loading |
| 195 |
lpUtils.lpSetLoadingEl( this.btnChooseCourses, 1 ); |
| 196 |
|
| 197 |
SweetAlert.close(); |
| 198 |
|
| 199 |
const formData = lpUtils.getDataOfForm( elFormFilter ); |
| 200 |
|
| 201 |
window.lpAJAXG.fetchAJAX( |
| 202 |
{ |
| 203 |
id_url: 'course-reset-progress-tool', |
| 204 |
action: 'reset_progress_courses', |
| 205 |
reset_all: 1, |
| 206 |
...formData, |
| 207 |
}, |
| 208 |
{ |
| 209 |
success: ( response ) => { |
| 210 |
const { status, message } = response; |
| 211 |
lpToastify.show( message, status ); |
| 212 |
}, |
| 213 |
error: ( error ) => { |
| 214 |
lpToastify.show( error.message, 'error' ); |
| 215 |
}, |
| 216 |
completed: () => { |
| 217 |
this.btnChooseCourses.textContent = |
| 218 |
this.btnChooseCourses.dataset.messageChoose; |
| 219 |
lpUtils.lpSetLoadingEl( this.btnChooseCourses, 0 ); |
| 220 |
}, |
| 221 |
} |
| 222 |
); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Fetch courses to reset progress. |
| 227 |
* |
| 228 |
* @param {HTMLElement} elForm The form element. |
| 229 |
*/ |
| 230 |
fetchCourses( elForm ) { |
| 231 |
this.elFormFilter = elForm; |
| 232 |
const elPopup = elForm.closest( ResetCourseProgress.selectors.elPopupItemsToSelect ); |
| 233 |
const elLPTarget = elPopup.querySelector( '.lp-target' ); |
| 234 |
let dataSend = window.lpAJAXG.getDataSetCurrent( elLPTarget ); |
| 235 |
dataSend.args = lpUtils.mergeDataWithDatForm( elForm, dataSend.args ); |
| 236 |
dataSend.args.paged = 1; |
| 237 |
window.lpAJAXG.setDataSetCurrent( elLPTarget, dataSend ); |
| 238 |
|
| 239 |
// Show loading |
| 240 |
window.lpAJAXG.showHideLoading( elLPTarget, 1 ); |
| 241 |
|
| 242 |
window.lpAJAXG.fetchAJAX( dataSend, { |
| 243 |
success: ( response ) => { |
| 244 |
const { data } = response; |
| 245 |
elLPTarget.innerHTML = data.content || ''; |
| 246 |
}, |
| 247 |
error: ( error ) => { |
| 248 |
lpToastify.show( error, 'error' ); |
| 249 |
}, |
| 250 |
completed: () => { |
| 251 |
window.lpAJAXG.showHideLoading( elLPTarget, 0 ); |
| 252 |
}, |
| 253 |
} ); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Filter courses to reset progress. |
| 258 |
* |
| 259 |
* @param {Object} args Event arguments. |
| 260 |
*/ |
| 261 |
filterCourses( args ) { |
| 262 |
const { target } = args; |
| 263 |
const elFilterField = target.closest( |
| 264 |
ResetCourseProgress.selectors.elFilterField |
| 265 |
); |
| 266 |
if ( ! elFilterField ) { |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
const elForm = elFilterField.closest( ResetCourseProgress.selectors.elFormFilter ); |
| 271 |
if ( ! elForm ) { |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
this.debouncedSearchUsers( elForm ); |
| 276 |
} |
| 277 |
} |
| 278 |
|