. defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php'); require_once($CFG->libdir . '/externallib.php'); use local_webhooks\local\api; use local_webhooks\local\record; /** * WebHooks external functions. * * @copyright 2019 'Valentin Popov' * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class local_webhooks_external extends external_api { /** * Add a new service. * * @param array $conditions * * @return int * * @throws \coding_exception * @throws \dml_exception * @throws \invalid_parameter_exception * @throws \restricted_context_exception */ public static function add_service(array $conditions): int { $parameters = self::validate_parameters(self::add_service_parameters(), [ 'record' => array_filter($conditions), ]); $context = context_system::instance(); self::validate_context($context); $parameters = array_filter($parameters['record']); $record = new record($parameters); return api::add_service($record); } /** * Returns description of the method parameters. * * @return \external_function_parameters */ public static function add_service_parameters(): external_function_parameters { return new external_function_parameters([ 'record' => new external_single_structure([ 'events' => new external_multiple_structure( new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.', false ), 'header' => new external_value(PARAM_RAW, 'The request\'s header or type', false, 'application/json'), 'name' => new external_value(PARAM_RAW, 'The service\'s name.'), 'point' => new external_value(PARAM_URL, 'The service\'s endpoint.'), 'status' => new external_value(PARAM_BOOL, 'The service\'s status.', false, true), 'token' => new external_value(PARAM_RAW, 'The service\'s secret key.'), ], 'The new service\'s data.'), ]); } /** * Returns description of the method result value. * * @return \external_value */ public static function add_service_returns(): external_value { return new external_value(PARAM_INT, 'The service\'s ID.'); } /** * Delete the existing service. * * @param int $serviceid * * @return bool * * @throws \dml_exception * @throws \invalid_parameter_exception * @throws \restricted_context_exception */ public static function del_service(int $serviceid): bool { $parameters = self::validate_parameters(self::del_service_parameters(), [ 'serviceid' => $serviceid, ]); $context = context_system::instance(); self::validate_context($context); return api::del_service($parameters['serviceid']); } /** * Returns description of the method parameters. * * @return \external_function_parameters */ public static function del_service_parameters(): external_function_parameters { return new external_function_parameters([ 'serviceid' => new external_value(PARAM_INT, 'The service\'s ID.'), ]); } /** * Returns description of the method result value. * * @return \external_value */ public static function del_service_returns(): external_value { return new external_value(PARAM_BOOL, 'The result operation.'); } /** * Get the event's list. * * @return array * * @throws \ReflectionException * @throws \dml_exception * @throws \invalid_parameter_exception * @throws \restricted_context_exception */ public static function get_events(): array { $context = context_system::instance(); self::validate_context($context); return api::get_events(); } /** * Returns description of the method parameters. * * @return \external_function_parameters */ public static function get_events_parameters(): external_function_parameters { return new external_function_parameters([]); } /** * Returns description of the method result value. * * @return \external_multiple_structure */ public static function get_events_returns(): external_multiple_structure { return new external_multiple_structure( new external_single_structure([ 'action' => new external_value(PARAM_ALPHANUMEXT, 'The action name.'), 'component' => new external_value(PARAM_COMPONENT, 'The component name.'), 'crud' => new external_value(PARAM_ALPHA, 'The transaction type.'), 'edulevel' => new external_value(PARAM_INT, 'The level of educational value.'), 'eventname' => new external_value(PARAM_RAW, 'The event name.'), 'objecttable' => new external_value(PARAM_RAW, 'The database table name.'), 'target' => new external_value(PARAM_RAW, 'The target on which the action.'), ], 'The event\'s data.'), 'The event\'s list.' ); } /** * Get data by service. * * @param int $serviceid * * @return \local_webhooks\local\record * * @throws \dml_exception * @throws \invalid_parameter_exception * @throws \restricted_context_exception */ public static function get_service(int $serviceid): record { $parameters = self::validate_parameters(self::get_service_parameters(), [ 'serviceid' => $serviceid, ]); $context = context_system::instance(); self::validate_context($context); return api::get_service($parameters['serviceid']); } /** * Returns description of the method parameters. * * @return \external_function_parameters */ public static function get_service_parameters(): external_function_parameters { return new external_function_parameters([ 'serviceid' => new external_value(PARAM_INT, 'The service\'s ID.'), ]); } /** * Returns description of the method result value. * * @return \external_single_structure */ public static function get_service_returns(): external_single_structure { return new external_single_structure([ 'events' => new external_multiple_structure( new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.' ), 'header' => new external_value(PARAM_RAW, 'The request\'s header or type'), 'id' => new external_value(PARAM_INT, 'The service\'s ID.'), 'name' => new external_value(PARAM_RAW, 'The service\'s name.'), 'point' => new external_value(PARAM_URL, 'The service\'s endpoint.'), 'status' => new external_value(PARAM_BOOL, 'The service\'s status.'), 'token' => new external_value(PARAM_RAW, 'The service\'s secret key.'), ], 'The service\'s data.'); } /** * Get the service's list. * * @param array|null $conditions * @param string|null $sort * @param int|null $from * @param int|null $limit * * @return array * * @throws \dml_exception * @throws \invalid_parameter_exception * @throws \restricted_context_exception */ public static function get_services(array $conditions = null, string $sort = null, int $from = null, int $limit = null): array { $parameters = self::validate_parameters(self::get_services_parameters(), [ 'conditions' => $conditions ?? [], 'from' => $from, 'limit' => $limit, 'sort' => $sort, ]); $context = context_system::instance(); self::validate_context($context); return api::get_services( array_filter($parameters['conditions']), $parameters['sort'], $parameters['from'], $parameters['limit'] ); } /** * Returns description of the method parameters. * * @return \external_function_parameters */ public static function get_services_parameters(): external_function_parameters { return new external_function_parameters([ 'conditions' => new external_single_structure([ 'header' => new external_value(PARAM_RAW, 'The request\'s header or type', false), 'name' => new external_value(PARAM_RAW, 'The service\'s name.', false), 'point' => new external_value(PARAM_URL, 'The service\'s endpoint.', false), 'status' => new external_value(PARAM_BOOL, 'The service\'s status.', false), 'token' => new external_value(PARAM_RAW, 'The service\'s secret key.', false), ], 'SQL conditions.', false), 'sort' => new external_value(PARAM_RAW, 'SQL sort parameters.', false), 'from' => new external_value(PARAM_INT, 'The start index.', false), 'limit' => new external_value(PARAM_INT, 'The count elements.', false), ]); } /** * Returns description of the method result value. * * @return \external_multiple_structure */ public static function get_services_returns(): external_multiple_structure { return new external_multiple_structure( new external_single_structure([ 'events' => new external_multiple_structure( new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.' ), 'header' => new external_value(PARAM_RAW, 'The request\'s header or type'), 'id' => new external_value(PARAM_INT, 'The service\'s ID.'), 'name' => new external_value(PARAM_RAW, 'The service\'s name.'), 'point' => new external_value(PARAM_URL, 'The service\'s endpoint.'), 'status' => new external_value(PARAM_BOOL, 'The service\'s status.'), 'token' => new external_value(PARAM_RAW, 'The service\'s secret key.'), ], 'The service\'s data.'), 'The service\'s list.' ); } /** * Update the existing service. * * @param array $conditions * * @return bool * * @throws \coding_exception * @throws \dml_exception * @throws \invalid_parameter_exception * @throws \restricted_context_exception */ public static function set_service(array $conditions): bool { $conditions = array_filter($conditions); $conditions['events'] = $conditions['events'] ?? []; $parameters = self::validate_parameters(self::set_service_parameters(), [ 'record' => $conditions, ]); $context = context_system::instance(); self::validate_context($context); $parameters = array_filter($parameters['record']); $record = api::get_service($parameters['id']); foreach ($parameters as $index => $value) { if (property_exists($record, $index)) { $record->$index = $value; } } return api::set_service($record); } /** * Returns description of the method parameters. * * @return \external_function_parameters */ public static function set_service_parameters(): external_function_parameters { return new external_function_parameters([ 'record' => new external_single_structure([ 'events' => new external_multiple_structure( new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.', false ), 'header' => new external_value(PARAM_RAW, 'The request\'s header or type', false), 'id' => new external_value(PARAM_INT, 'The service\'s ID.'), 'name' => new external_value(PARAM_RAW, 'The service\'s name.', false), 'point' => new external_value(PARAM_URL, 'The service\'s endpoint.', false), 'status' => new external_value(PARAM_BOOL, 'The service\'s status.', false), 'token' => new external_value(PARAM_RAW, 'The service\'s secret key.', false), ], 'The new service\'s data.'), ]); } /** * Returns description of the method result value. * * @return \external_value */ public static function set_service_returns(): external_value { return new external_value(PARAM_BOOL, 'The result operation.'); } }