Skip to content

Commit

Permalink
issue #8: CRUD for presets
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Sep 22, 2024
1 parent acef4c8 commit 14ce980
Show file tree
Hide file tree
Showing 19 changed files with 1,056 additions and 62 deletions.
9 changes: 9 additions & 0 deletions amd/build/presets.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amd/build/presets.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 168 additions & 0 deletions amd/src/presets.js
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());
};
69 changes: 69 additions & 0 deletions classes/external/delete_preset.php
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;
}
}
45 changes: 45 additions & 0 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,49 @@ public static function validate_task_custom_data(stdClass $data, array $fields =
}
}
}

/**
* Gets a list of tags related to courses.
*
* @param int $courseid Optional to filter by course ID.
* @return array
*/
public static function get_course_tags(int $courseid = 0): array {
global $DB;

$params = [];
$where = '';
if (!empty($courseid)) {
$where = " AND ti.itemid = ?";
$params[] = $courseid;
}

$sql = "SELECT DISTINCT t.id, t.rawname
FROM {tag} t
JOIN {tag_instance} ti ON t.id = ti.tagid
WHERE ti.itemtype = 'course' $where ORDER BY t.id, t.rawname";
return $DB->get_records_sql($sql, $params);
}

/**
* Returns a list of courses.
*
* @return array
*/
public static function get_courses(): array {
global $DB;

return $DB->get_records('course', ['visible' => 1], 'fullname');
}

/**
* Get categories.
*
* @return array
*/
public static function get_categories(): array {
global $DB;

return $DB->get_records('course_categories', ['visible' => 1], 'name');
}
}
Loading

0 comments on commit 14ce980

Please sign in to comment.