-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,056 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// This file is part of Moodle - https://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Presets management. | ||
* @module tool_enrolprofile/prestes | ||
* @copyright 2024 Dmitrii Metelkin <[email protected]> | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
import {add as notifyUser} from 'core/toast'; | ||
import Ajax from 'core/ajax'; | ||
import * as DynamicTable from 'core_table/dynamic'; | ||
import DynamicTableSelectors from 'core_table/local/dynamic/selectors'; | ||
import {get_string as getString, get_strings as getStrings} from 'core/str'; | ||
import ModalForm from 'core_form/modalform'; | ||
import Notification from 'core/notification'; | ||
import Pending from 'core/pending'; | ||
|
||
/** | ||
* | ||
* @type {{ADD_NEW_RECORD_BUTTON: string}} | ||
*/ | ||
const SELECTORS = { | ||
ADD_PRESET_BUTTON: '#addpresetbutton', | ||
ACTION: '[data-action]', | ||
}; | ||
|
||
|
||
/** | ||
* Gets dynamic table root container. | ||
* | ||
* @returns {*} | ||
*/ | ||
const getTableRoot = () => { | ||
return document.querySelector(DynamicTableSelectors.main.region); | ||
}; | ||
|
||
/** | ||
* Edit the log record. | ||
* | ||
* @param {string} id record ID. | ||
*/ | ||
const editPreset = (id) => { | ||
const form = new ModalForm({ | ||
formClass: "tool_enrolprofile\\preset_form", | ||
args: { | ||
id: id, | ||
}, | ||
modalConfig: { | ||
title: getString('preset', 'tool_enrolprofile'), | ||
}, | ||
}); | ||
|
||
form.addEventListener(form.events.FORM_SUBMITTED, () => { | ||
if (id === 0) { | ||
sendFeedback('add'); | ||
} else { | ||
sendFeedback('edit'); | ||
} | ||
}); | ||
|
||
form.show(); | ||
}; | ||
|
||
/** | ||
* Delete the log record. | ||
* @param {string} id ruleid. | ||
*/ | ||
const deletePreset = (id) => { | ||
const pendingPromise = new Pending('tool_enrolprofile/form:confirmdelete'); | ||
|
||
getStrings([ | ||
{'key': 'confirm'}, | ||
{'key': 'confirm:delete', component: 'tool_enrolprofile'}, | ||
{'key': 'yes'}, | ||
{'key': 'no'}, | ||
]) | ||
.then(strings => { | ||
return Notification.confirm(strings[0], strings[1], strings[2], strings[3], function() { | ||
Ajax.call([{ | ||
methodname: 'tool_enrolprofile_delete_preset', | ||
args: { | ||
id: id, | ||
} | ||
}])[0] | ||
.then(function () { | ||
sendFeedback('delete'); | ||
}) | ||
.catch(Notification.exception); | ||
}); | ||
}) | ||
.then(pendingPromise.resolve) | ||
.catch(Notification.exception); | ||
}; | ||
|
||
|
||
/** | ||
* Send feedback to a user. | ||
* | ||
* @param {string} action Action to send feedback about. | ||
*/ | ||
const sendFeedback = (action) => { | ||
getString('completed:' + action, 'tool_enrolprofile') | ||
.then(message => { | ||
notifyUser(message); | ||
const tableRoot = getTableRoot(); | ||
if (tableRoot) { | ||
DynamicTable.refreshTableContent(tableRoot).catch(Notification.exception); | ||
} | ||
}).catch(Notification.exception); | ||
}; | ||
|
||
/** | ||
* Attach events to add new record button. | ||
*/ | ||
const attachAddNewButtonEvents = () => { | ||
const addNewRecordButton = document.querySelector(SELECTORS.ADD_PRESET_BUTTON); | ||
if (addNewRecordButton) { | ||
addNewRecordButton.addEventListener('click', e => { | ||
e.preventDefault(); | ||
editPreset(0); | ||
}); | ||
} | ||
}; | ||
|
||
/** | ||
* Attach events to actions. | ||
*/ | ||
const attachTableActionEvents = () => { | ||
document.querySelectorAll(SELECTORS.ACTION).forEach(element => { | ||
element.addEventListener('click', e => { | ||
e.preventDefault(); | ||
const actionElement = e.target.closest(SELECTORS.ACTION); | ||
const id = actionElement.dataset.id; | ||
const action = actionElement.dataset.action; | ||
switch (action) { | ||
case 'edit': | ||
editPreset(id); | ||
break; | ||
case 'delete': | ||
deletePreset(id); | ||
break; | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
/** | ||
* Init. | ||
*/ | ||
export const init = () => { | ||
attachAddNewButtonEvents(); | ||
attachTableActionEvents(); | ||
document.addEventListener(DynamicTable.Events.tableContentRefreshed, () => attachTableActionEvents()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
// This file is part of Moodle - https://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
namespace tool_enrolprofile\external; | ||
|
||
use core_external\external_api; | ||
use core_external\external_function_parameters; | ||
use core_external\external_value; | ||
use invalid_parameter_exception; | ||
use tool_enrolprofile\preset; | ||
|
||
/** | ||
* Delete preset API class. | ||
* | ||
* @package tool_enrolprofile | ||
* @copyright 2024 Dmitrii Metelkin <[email protected]> | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class delete_preset extends external_api { | ||
|
||
/** | ||
* Describes the parameters for validate_form webservice. | ||
* | ||
* @return external_function_parameters | ||
*/ | ||
public static function execute_parameters(): external_function_parameters { | ||
return new external_function_parameters([ | ||
'id' => new external_value(PARAM_INT, 'Preset ID'), | ||
]); | ||
} | ||
|
||
/** | ||
* Execute API action. | ||
* | ||
* @param int $id record ID. | ||
*/ | ||
public static function execute(int $id): void { | ||
$params = self::validate_parameters(self::execute_parameters(), ['id' => $id]); | ||
|
||
require_admin(); | ||
|
||
$preset = preset::get_record(['id' => $params['id']]); | ||
if (!$preset) { | ||
throw new invalid_parameter_exception('Invalid preset'); | ||
} | ||
|
||
$preset->delete(); | ||
} | ||
|
||
/** | ||
* Returns description of method result value. | ||
*/ | ||
public static function execute_returns() { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.