Compare commits

...
Author SHA1 Message Date
Valentin Popov 040d7540d7 Reworked the main page
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-12 04:58:59 +04:00
Valentin Popov 9c2eefcaa5 Added a class table for list services
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-12 04:51:19 +04:00
Valentin Popov 98aecd35ce Added final improvements for external functions
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-12 03:34:03 +04:00
Valentin Popov c42e13d182 Fixed violations CI
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-11 13:54:14 +04:00
Valentin Popov 7dc023b05a Added external function update existing service
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-11 03:45:50 +04:00
Valentin Popov 3a865453a4 Added external function get the event's list
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-11 02:40:47 +04:00
Valentin Popov 747055f9d1 Added external function add service
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-11 00:57:18 +04:00
Valentin Popov ed0ce52060 Added external function delete service
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-11 00:17:20 +04:00
Valentin Popov 6b18cffc97 Added external function get the list services
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-10 23:45:18 +04:00
Valentin Popov 0cbc05d52b Added exact other tests
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-10 22:43:32 +04:00
Valentin Popov ae2f31261d Added exact test get record by an event
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-10 22:12:01 +04:00
Valentin Popov 46711c9942 Added correct test delete record
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-10 15:34:57 +04:00
Valentin Popov e84ac6859a Renamed a classes function "delete_service"
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-10 15:22:48 +04:00
Valentin Popov e550237b4b Renamed a classes function "update_service"
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-10 15:20:56 +04:00
Valentin Popov db2d98d210 Renamed a classes function "create_service"
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-10 15:19:18 +04:00
Valentin Popov 5b0dba4980 Added external function get service
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-10 14:22:56 +04:00
Valentin Popov d441f44698 Added get a total count records
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-09 20:35:59 +04:00
Valentin Popov cb6d5c7e13 Added sort fields
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-09 19:09:36 +04:00
Valentin Popov da306feae3 Added a minor fix
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-08 03:06:28 +04:00
Valentin Popov 9a1b781107 Fixed error's list with CI
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-08 02:54:48 +04:00
Valentin Popov 8bf6ed8122 Added function get an event's list
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-08 02:47:19 +04:00
Valentin Popov e2627eaf86 Fixed errors from CI
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-08 01:17:40 +04:00
Valentin Popov eb4b9b69f8 Added worked a class observer
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-06 05:47:03 +04:00
Valentin Popov bde135499d Added a new class for processing events
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-06 04:46:09 +04:00
Valentin Popov 4806fad728 Did a deep refactoring main class in the plugin
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-06 04:27:01 +04:00
Valentin Popov 654eade42b Added description record's class
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-06 00:10:26 +04:00
Valentin Popov 3f50d21a7f Primary test cases the library
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-05 17:18:08 +04:00
Valentin Popov 08a54b0e9f Primary test function for add, update and delete
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-05-05 16:15:38 +04:00
Valentin Popov 6f6d50776c Created a worked upgrade mechanism
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-04-28 17:54:09 +04:00
Valentin Popov 7ae31a6eee Created a worked upgrade mechanism
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-04-28 17:53:22 +04:00
Valentin Popov 0fe3b14f29 Fixed errors CI config
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-04-28 14:31:51 +04:00
Valentin Popov 0cdfc1b1cf Disable a tests old version
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-04-28 14:17:31 +04:00
Valentin Popov a2bc4b735c Removal of excess conversion
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-10-29 02:44:17 +04:00
Valentin Popov 0a44411f98 External service search function by event
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-10-29 02:43:09 +04:00
Valentin Popov b8e61c44ac External function of getting these services
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-10-29 02:30:45 +04:00
Valentin Popov 6c31017c8d External function to get service data
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-10-29 02:27:57 +04:00
Valentin Popov 00309285ba Connecting external reading services
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-10-29 01:39:05 +04:00
Valentin Popov ad5fb98070 Fix 'Line exceeds 132 characters'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-10-26 14:31:00 +04:00
Valentin Popov 09c54f6627 Update from version 4.0.0-rc.*
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-10-26 05:20:18 +04:00
Valentin Popov a01ad5f97c Update from version 3.*
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-10-26 04:42:26 +04:00
Valentin Popov d8f5be6670 Moodle style fixes
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-10-22 04:04:35 +04:00
Valentin Popov 99c835a63d Correction of implicit style errors
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-10-22 03:48:04 +04:00
Valentin Popov 14e5a16a33 Update file template
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-10-22 03:02:00 +04:00
Valentin Popov 1352b5ee79 Code style fix
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-10-22 02:59:06 +04:00
Valentin Popov f3b5ab5616 Updating the autotest file
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-25 07:52:01 +04:00
Valentin Popov f28ff05598 Removing the page restoring backups
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-25 07:39:58 +04:00
Valentin Popov 7892ecc2fd Updating the language file
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-20 18:41:40 +04:00
Valentin Popov 5115697a62 Comments added
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-20 18:36:28 +04:00
Valentin Popov 68a1928a4c Redesigned page of the service editor
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-20 18:29:50 +04:00
Valentin Popov 6e82b7cc19 Removing old web services
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-20 18:07:50 +04:00
Valentin Popov 36ddf415c4 Reducing nesting in the code
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-20 18:05:02 +04:00
Valentin Popov 67cf87d17c Correction of the file name
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-20 17:59:58 +04:00
Valentin Popov dc4bc38f80 Removing old event recorders
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-20 17:46:42 +04:00
Valentin Popov 73f45578b3 Correction of checks
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-09 21:47:37 +04:00
Valentin Popov c18d7107b9 Formatting the code on the settings page
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-09 21:13:57 +04:00
Valentin Popov 1701c9cf97 Adapted homepage
* The work of basic functions has been restored.
* The control buttons are moved down.
* Removed redundant functionality:
  * Backup data.
  * Delete all records.
  * Restore from a backup.

Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-09 21:09:12 +04:00
Valentin Popov d0e7af84ff A reworked table for displaying a list of services
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-09 05:33:14 +04:00
Valentin Popov aae3ea70b2 Fix table version
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-09 05:08:26 +04:00
Valentin Popov 16305051a9 TODO and function type change
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-09 05:04:36 +04:00
Valentin Popov 5d942ccbb2 Delete unnecessary files
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-09 05:00:43 +04:00
Valentin Popov 05ce3e0664 Task handler from the list
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-09 04:57:04 +04:00
Valentin Popov 17b9cfde78 Service search function on the event
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-09 04:38:27 +04:00
Valentin Popov 270c04a603 Minor fixes to the function 'get_services'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-09 04:29:38 +04:00
Valentin Popov 2194bc4a01 Refactoring the event handler
Event processing is asynchronous.  For asynchronous processing, events
are added to the job queue.

Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-09 03:23:12 +04:00
Valentin Popov 5884a58f54 Added TODO
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-08 18:13:45 +04:00
Valentin Popov 472f85850a Basic function of getting a list of all services
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-08 16:06:39 +04:00
Valentin Popov 816110da62 Basic function of obtaining data about the service
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-08 15:46:57 +04:00
Valentin Popov 295da67a0a Formatting the code
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-08 15:23:37 +04:00
Valentin Popov 7d2e288b70 Realization of the basic functionality
* Function of adding a service to the database.
* Function of updating the service in the database.
* Function of removing the service from the database.

Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-07 15:27:59 +04:00
Valentin Popov fa86c0329f Updating the table structure
* Fixed bug with 'XMLDB PATH'.  The latest update to Moodle includes
a strict check.
* Remotely the status field in the event table.  The logic is
simplified, if there is an event subscription, the event is stored in
a table.  There are no subscriptions, no events in the table.

Signed-off-by: Valentin Popov <info@valentineus.link>
2018-09-07 14:36:17 +04:00
Valentin Popov 9ee0af7e28 Reworked the event handler
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-21 08:13:13 +04:00
Valentin Popov 9006e6b9dd Renamed function search services
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-21 08:12:22 +04:00
Valentin Popov 2b791958a7 Redesigned cache name
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-21 07:48:01 +04:00
Valentin Popov 4833a44b51 Redesigned feature a list of services
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-21 07:46:03 +04:00
Valentin Popov d7185f5925 Minor bug fixed
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-20 04:42:44 +04:00
Valentin Popov 975c6f8d20 Checking arguments is added
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-20 04:31:14 +04:00
Valentin Popov 092d0436e8 Comments have been added
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-20 04:23:13 +04:00
Valentin Popov 27e7f48f36 Updated description
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-20 04:19:08 +04:00
Valentin Popov 41a6a89ac8 Updated the status change function
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-20 04:13:47 +04:00
Valentin Popov 279fca5ed1 Added cache to the search function
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-20 03:58:45 +04:00
Valentin Popov 5036ef261c The function 'local_webhooks_get_total_count' is deleted
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-13 01:53:32 +04:00
Valentin Popov 6aed41d675 Fixed the name of the constant
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-13 01:52:44 +04:00
Valentin Popov 25f1756835 The internal functions 'local_webhooks_serialization_data' and 'local_webhooks_deserialization_data' are deleted
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-13 01:50:55 +04:00
Valentin Popov 490b8bb170 Update function 'local_webhooks_restore_backup'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-13 01:43:23 +04:00
Valentin Popov 95c54f8262 Update function 'local_webhooks_create_backup'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-13 01:34:19 +04:00
Valentin Popov 6a83be960e Update function 'local_webhooks_search_record'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-13 01:17:22 +04:00
Valentin Popov 82842e5ed8 Update function 'local_webhooks_change_status'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-13 01:01:42 +04:00
Valentin Popov 98c33a639a Added function 'local_webhooks_delete_all_records'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-13 00:52:51 +04:00
Valentin Popov 98e5a6470d Added function 'local_webhooks_delete_record'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-13 00:48:12 +04:00
Valentin Popov e8b59e7b0d Update function 'local_webhooks_get_list_records'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-13 00:25:55 +04:00
Valentin Popov 46b6022269 Added variables
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-13 00:18:28 +04:00
Valentin Popov 2bd5e80b10 Rewritten 'local_webhooks_get_record' feature
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-13 00:16:08 +04:00
Valentin Popov af4224e8db Renaming internal functions
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-13 00:10:56 +04:00
Valentin Popov 79fe101e1b Added function 'local_webhooks_get_list_events_for_service'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-13 00:08:18 +04:00
Valentin Popov b12c850a2f Fix: Global variable
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 23:52:38 +04:00
Valentin Popov 57a870f56f Update function 'local_webhooks_create_record'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 23:48:00 +04:00
Valentin Popov 639276c33d Added function 'local_webhooks_update_record'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 23:45:35 +04:00
Valentin Popov 0d9fe62ec6 The function 'local_webhooks_insert_event' is replaced by the function 'local_webhooks_insert_events'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 23:38:14 +04:00
Valentin Popov 2b7e419f8f Added internal function 'local_webhooks_insert_event'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 23:29:02 +04:00
Valentin Popov adb9a1e69c Added internal function 'local_webhooks_delete_events'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 23:23:57 +04:00
Valentin Popov 2c013b3230 The function 'local_webhooks_create_record' was rewritten for new storage
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 22:50:40 +04:00
Valentin Popov f3dc13ac91 Service edit form is focused on the new standard
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 22:09:36 +04:00
Valentin Popov 4d69ca0da6 The revised structure of information storage
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 21:54:39 +04:00
Valentin Popov f220e057bd The web service 'local_webhooks_get_list_records' has been expanded
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 14:48:20 +04:00
Valentin Popov 422cbfd57d New parameters of function 'local_webhooks_get_list_records' have been added
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 14:38:33 +04:00
Valentin Popov e0a43273a3 Added constants to the cache
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 14:20:30 +04:00
Valentin Popov cce50b316d Delete the variable
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 03:42:08 +04:00
Valentin Popov 55b1a6b5a9 Delimiter
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 03:30:57 +04:00
Valentin Popov 6a711bfc48 Fixed a potential performance hole
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 03:24:38 +04:00
Valentin Popov 9ada0cfe74 Using a constant
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 02:29:32 +04:00
Valentin Popov 0a07c2ab22 Added a new function 'local_webhooks_get_total_count'
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 02:05:27 +04:00
Valentin Popov 0e94634c49 Updated description
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 01:45:08 +04:00
Valentin Popov d29ee546a2 Renaming a variable
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-12 01:44:01 +04:00
Valentin Popov 27fe9e412b Fixed table in the latest version of Moodle
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-03-11 22:31:15 +04:00
Valentin Popov 9cf4fc575c Correction of spaces 2018-02-25 01:32:29 +04:00
Valentin Popov f3c2ebbe4d Correcting a stupid mistake 2018-02-25 01:27:40 +04:00
Valentin Popov 8f25379f32 Change version 2018-02-25 01:21:50 +04:00
Valentin Popov 90897b0e7d General refactoring of the web services code 2018-02-25 01:19:19 +04:00
Valentin Popov 685d9ee15e Change version 2018-02-22 12:58:37 +04:00
Valentin Popov db500cddce Fix 'invalid OPTIONAL value specified' 2018-02-22 12:55:20 +04:00
Valentin Popov 85bcb64e37 Added general list functions 2018-02-22 12:44:50 +04:00
Valentin Popov 1736128dbf Adding the function of forming events 2018-02-22 12:37:59 +04:00
Valentin Popov 5cdd8de9e3 Small code refactoring 2018-02-20 15:40:50 +04:00
Valentin Popov b6ddc8bd40 Some variables have been removed 2018-02-20 15:17:27 +04:00
Valentin Popov f10786bfb0 Update function 'local_webhooks_create_record' 2018-02-20 14:38:36 +04:00
Valentin Popov 899b129d23 The extension is added to the event handler 2018-02-19 15:26:27 +04:00
Valentin Popov dd19065313 The extension is added to external functions 2018-02-19 15:25:49 +04:00
Valentin Popov 3a2bddeea4 The search function is expanded 2018-02-19 15:24:34 +04:00
Valentin Popov a41dbbb950 Transferring internal functions to the internal library 2018-02-19 13:44:11 +04:00
Valentin Popov a68882140b An external service search function is declared 2018-02-19 13:38:27 +04:00
Valentin Popov 43f0bf98d2 Added external functions to find services 2018-02-19 13:37:27 +04:00
Valentin Popov 95c4cf061a Rename the function to reset the cache 2018-02-19 13:15:14 +04:00
Valentin Popov a1099245b5 Cache description added 2018-02-19 13:06:06 +04:00
Valentin Popov 66b5928e6e Added clearing the cache when changing data 2018-02-19 12:36:09 +04:00
Valentin Popov 4f75d9c5c8 Event handler is rewritten to work with the cache 2018-02-18 22:05:58 +04:00
Valentin Popov 29bdbc7456 Added a service search function that contains the specified event 2018-02-18 21:37:43 +04:00
Valentin Popov ebf76795da Change the location of the functions for working with the cache 2018-02-18 21:28:46 +04:00
Valentin Popov 53c49b69de Added functions for working with the cache 2018-02-18 20:01:55 +04:00
Valentin Popov f392fd42d9 Update the PHP version for testing. 2018-02-18 18:05:33 +04:00
Valentin Popov 67f8d73a15 Update versions of Moodle for testing. 2018-02-18 18:02:23 +04:00
Valentin Popov 22b7569f71 Correct the check for 'sesskey'. 2018-02-18 17:58:59 +04:00
Valentin Popov fa74622268 Links to the editor contain sesskey 2018-01-04 15:38:55 +04:00
Valentin Popov 9b7426fb20 Check the sesskey on the editor page 2018-01-04 15:35:27 +04:00
Valentin Popov 283c64f62e Check the sesskey on the recovery page 2018-01-04 15:34:51 +04:00
Valentin Popov 17ae01ab71 Change version 2017-12-29 14:29:39 +04:00
Valentin Popov 28e1169be8 Added correct update from old versions 2017-12-29 14:25:49 +04:00
Valentin Popov 2126708201 An additional column is added 2017-12-27 21:03:09 +04:00
Valentin Popov 0041667754 Refactoring the handler class 2017-12-27 20:45:31 +04:00
Valentin Popov 91bf79f030 Added function to send the request 2017-12-27 20:43:36 +04:00
Valentin Popov 7d1a0b9fec Updated the function to update the record 2017-12-27 20:30:27 +04:00
Valentin Popov 51007a0fb5 Register a new external function 2017-12-27 19:52:21 +04:00
Valentin Popov 2eaa622161 Added external function display a list of events 2017-12-27 19:51:34 +04:00
Valentin Popov 3d4b78bd8b Added the function of receiving all the events 2017-12-27 16:01:48 +04:00
Valentin Popov 41185c6db4 Reconstructed external restore function from the backup 2017-12-27 15:47:36 +04:00
Valentin Popov 7e1284bfe2 Edit the result of the function 2017-12-27 15:29:58 +04:00
Valentin Popov 785a541d1c Registration of external plug-in functions is added 2017-12-27 15:27:55 +04:00
Valentin Popov 6e3bf072af Added external plugin functions 2017-12-27 15:26:59 +04:00
Valentin Popov 2e57e732ba Fixed the text of the button 2017-12-22 18:58:27 +04:00
Valentin Popov 499f36d118 Integration of the event system into functions 2017-12-21 12:06:20 +04:00
Valentin Popov 49516bea82 Refactoring the system of events 2017-12-21 11:48:43 +04:00
Valentin Popov a6c6081139 Edit translation 2017-12-21 11:36:42 +04:00
Valentin Popov 9560267833 Refactoring the editor code 2017-12-21 11:31:58 +04:00
Valentin Popov 2057a1efb6 The functional was updated to a new form 2017-12-21 11:14:40 +04:00
Valentin Popov 0cbf2bbb87 The data recovery form has been extended 2017-12-21 11:12:40 +04:00
Valentin Popov 3e054dd94e Updated package dependencies 2017-12-21 11:11:24 +04:00
Valentin Popov 54fb29c5ea Correction on the main page 2017-12-21 10:01:37 +04:00
Valentin Popov 78e9e435cc Refactoring function code plugins 2017-12-21 09:59:14 +04:00
Valentin Popov 97711bb186 Added function to change the status of the service 2017-12-19 14:50:30 +04:00
Valentin Popov 999ad9f472 Refactoring the main page 2017-12-19 14:39:40 +04:00
Valentin Popov 82c7463e4a A class was added to display the table 2017-12-19 14:15:07 +04:00
Valentin Popov e0e9322644 Additions to the description file 2017-12-11 07:47:51 +04:00
Valentin Popov 7278ee951b Changes to the User's Guide 2017-12-11 07:43:17 +04:00
Valentin Popov 01e2ba9f2f User guide 2017-12-11 07:37:49 +04:00
Valentin Popov 99868c158a Screenshots for documentation 2017-12-11 07:37:09 +04:00
33 changed files with 2513 additions and 1282 deletions
+31 -47
View File
@@ -1,35 +1,41 @@
language: php
sudo: true
sudo: false
addons:
firefox: "latest-esr"
postgresql: "9.6"
os: "linux"
apt:
packages:
- "mysql-client-5.6"
- "mysql-client-core-5.6"
- "mysql-server-5.6"
- "openjdk-8-jre-headless"
cache:
directories:
- "$HOME/.composer/cache"
- "$HOME/.npm"
php:
- 5.6
- 7.1
- "7.0"
- "7.1"
- "7.2"
env:
- DB=pgsql MOODLE_BRANCH=MOODLE_26_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_27_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_28_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_29_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_30_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_31_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_32_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_33_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_26_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_27_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_28_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_29_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_30_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_31_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_32_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_33_STABLE
matrix:
- MOODLE_BRANCH=MOODLE_35_STABLE DB=mysqli
- MOODLE_BRANCH=MOODLE_35_STABLE DB=pgsql
- MOODLE_BRANCH=MOODLE_36_STABLE DB=mysqli
- MOODLE_BRANCH=MOODLE_36_STABLE DB=pgsql
before_install:
- phpenv config-rm xdebug.ini
- nvm install 8.9
- nvm use 8.9
- cd ../..
- composer selfupdate
- composer create-project -n --no-dev moodlerooms/moodle-plugin-ci ci ^1
- composer create-project -n --no-dev --prefer-dist moodlerooms/moodle-plugin-ci ci ^2
- export PATH="$(cd ci/bin; pwd):$(cd ci/vendor/bin; pwd):$PATH"
install:
@@ -40,31 +46,9 @@ script:
- moodle-plugin-ci phpcpd
- moodle-plugin-ci phpmd
- moodle-plugin-ci codechecker
- moodle-plugin-ci csslint
- moodle-plugin-ci shifter
- moodle-plugin-ci jshint
- moodle-plugin-ci validate
- moodle-plugin-ci savepoints
- moodle-plugin-ci mustache
- moodle-plugin-ci grunt
- moodle-plugin-ci phpunit
- moodle-plugin-ci behat
jobs:
include:
stage: deploy
env:
- DB=mysqli MOODLE_BRANCH=MOODLE_33_STABLE
php: 7.1
before_deploy:
- cd $TRAVIS_REPO_SLUG
- git stash --all
- /bin/sh build.sh
deploy:
provider: releases
skip_cleanup: true
file: "../local_webhooks.zip"
email: "info@valentineus.link"
api_key: $GITHUB_PROJECT_TOKEN
on:
tags: true
+4 -20
View File
@@ -20,27 +20,11 @@ Features:
* Use a secret phrase to authenticate requests;
* [JSON](https://en.wikipedia.org/wiki/JSON) - Format of outgoing data;
## Installation
## Documentation
Get the installation package in any of the available methods:
* [GitHub Releases](https://github.com/valentineus/moodle-webhooks/releases).
* [Compilation from the source code](#build).
## Build
Self-assembly package is as follows:
* Clone the repository:
```bash
git clone https://github.com/valentineus/moodle-webhooks.git moodle-webhooks
```
* Run the build script:
```bash
cd ./moodle-webhooks
/bin/sh build.sh
```
* [Install the plugin](docs/getting-started.md#installation).
* [User guide](docs/getting-started.md#user-guide).
* [Request format](docs/getting-started.md#request-format).
## License
-65
View File
@@ -1,65 +0,0 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* Registration of the system of events.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_webhooks\event;
defined("MOODLE_INTERNAL") || die();
/**
* Defines how to work with events.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_performed extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data["crud"] = "c";
$this->data["edulevel"] = self::LEVEL_OTHER;
$this->data["objecttable"] = "local_webhooks_service";
}
/**
* Return localised event name.
*/
public static function get_name() {
return new \lang_string("create", "moodle");
}
/**
* Returns description of what happened.
*/
public function get_description() {
return new \lang_string("backup", "moodle");
}
/**
* Get URL related to the action.
*/
public function get_url() {
return new \moodle_url("/local/webhooks/index.php");
}
}
-65
View File
@@ -1,65 +0,0 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* Registration of the system of events.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_webhooks\event;
defined("MOODLE_INTERNAL") || die();
/**
* Defines how to work with events.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_restored extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data["crud"] = "u";
$this->data["edulevel"] = self::LEVEL_OTHER;
$this->data["objecttable"] = "local_webhooks_service";
}
/**
* Return localised event name.
*/
public static function get_name() {
return new \lang_string("update", "moodle");
}
/**
* Returns description of what happened.
*/
public function get_description() {
return new \lang_string("backupfinished", "moodle");
}
/**
* Get URL related to the action.
*/
public function get_url() {
return new \moodle_url("/local/webhooks/index.php");
}
}
+44
View File
@@ -0,0 +1,44 @@
<?php
// This file is part of Moodle - http://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/>.
namespace local_webhooks\event;
defined('MOODLE_INTERNAL') || die();
use core\event\base as event;
use core\task\manager;
use local_webhooks\task\notify;
use function defined;
/**
* Class observer.
*
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package local_webhooks\event
*/
final class observer {
/**
* Main handler for events.
*
* @param \core\event\base $event
*/
public static function handler(event $event) {
$task = new notify();
$task->set_custom_data($event->get_data());
manager::queue_adhoc_task($task);
}
}
-65
View File
@@ -1,65 +0,0 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* Registration of the system of events.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_webhooks\event;
defined("MOODLE_INTERNAL") || die();
/**
* Defines how to work with events.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class response_answer extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data["crud"] = "r";
$this->data["edulevel"] = self::LEVEL_OTHER;
$this->data["objecttable"] = "local_webhooks_service";
}
/**
* Return localised event name.
*/
public static function get_name() {
return new \lang_string("answer", "moodle");
}
/**
* Returns description of what happened.
*/
public function get_description() {
return $this->other["status"];
}
/**
* Get URL related to the action.
*/
public function get_url() {
return new \moodle_url("/local/webhooks/editservice.php", array("serviceid" => $this->objectid));
}
}
-65
View File
@@ -1,65 +0,0 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* Registration of the system of events.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_webhooks\event;
defined("MOODLE_INTERNAL") || die();
/**
* Defines how to work with events.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class service_added extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data["crud"] = "c";
$this->data["edulevel"] = self::LEVEL_OTHER;
$this->data["objecttable"] = "local_webhooks_service";
}
/**
* Return localised event name.
*/
public static function get_name() {
return new \lang_string("create", "moodle");
}
/**
* Returns description of what happened.
*/
public function get_description() {
return new \lang_string("eventwebserviceservicecreated", "webservice");
}
/**
* Get URL related to the action.
*/
public function get_url() {
return new \moodle_url("/local/webhooks/editservice.php", array("serviceid" => $this->objectid));
}
}
-65
View File
@@ -1,65 +0,0 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* Registration of the system of events.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_webhooks\event;
defined("MOODLE_INTERNAL") || die();
/**
* Defines how to work with events.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class service_deleted extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data["crud"] = "d";
$this->data["edulevel"] = self::LEVEL_OTHER;
$this->data["objecttable"] = "local_webhooks_service";
}
/**
* Return localised event name.
*/
public static function get_name() {
return new \lang_string("deleted", "moodle");
}
/**
* Returns description of what happened.
*/
public function get_description() {
return new \lang_string("eventwebserviceservicedeleted", "webservice");
}
/**
* Get URL related to the action.
*/
public function get_url() {
return new \moodle_url("/local/webhooks/index.php");
}
}
-65
View File
@@ -1,65 +0,0 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* Registration of the system of events.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_webhooks\event;
defined("MOODLE_INTERNAL") || die();
/**
* Defines how to work with events.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class service_updated extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data["crud"] = "u";
$this->data["edulevel"] = self::LEVEL_OTHER;
$this->data["objecttable"] = "local_webhooks_service";
}
/**
* Return localised event name.
*/
public static function get_name() {
return new \lang_string("update", "moodle");
}
/**
* Returns description of what happened.
*/
public function get_description() {
return new \lang_string("eventwebserviceserviceupdated", "webservice");
}
/**
* Get URL related to the action.
*/
public function get_url() {
return new \moodle_url("/local/webhooks/editservice.php", array("serviceid" => $this->objectid));
}
}
-93
View File
@@ -1,93 +0,0 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* The event handler.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_webhooks;
defined("MOODLE_INTERNAL") || die();
require_once(__DIR__ . "/../lib.php");
require_once(__DIR__ . "/../locallib.php");
require_once($CFG->libdir . "/filelib.php");
/**
* Defines how to work with events.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class handler {
/**
* External handler.
*
* @param object $event
*/
public static function events($event) {
$data = $event->get_data();
if ($callbacks = local_webhooks_get_list_records()) {
foreach ($callbacks as $callback) {
self::handler_callback($data, $callback);
}
}
}
/**
* Processes each callback.
*
* @param array $data
* @param object $callback
*/
private static function handler_callback($data, $callback) {
global $CFG;
if (boolval($callback->enable)) {
if (!empty($callback->events[$data["eventname"]])) {
$urlparse = parse_url($CFG->wwwroot);
$data["host"] = $urlparse['host'];
$data["token"] = $callback->token;
$data["extra"] = $callback->other;
self::send($data, $callback);
}
}
}
/**
* Sending data to the node.
*
* @param array $data
* @param object $callback
*/
private static function send($data, $callback) {
$curl = new \curl();
$curl->setHeader(array("Content-Type: application/" . $callback->type));
$curl->post($callback->url, json_encode($data));
$response = $curl->getResponse();
\local_webhooks_events::response_answer($callback->id, $response);
return $response;
}
}
+361
View File
@@ -0,0 +1,361 @@
<?php
// This file is part of Moodle - http://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/>.
namespace local_webhooks\local;
defined('MOODLE_INTERNAL') || die();
define('LW_TABLE_EVENTS', 'local_webhooks_events');
define('LW_TABLE_SERVICES', 'local_webhooks_service');
global $CFG;
require_once($CFG->dirroot . '/local/webhooks/classes/local/record.php');
use coding_exception;
use core_component;
use ReflectionClass;
use function define;
use function defined;
use function is_array;
use function is_int;
use function is_object;
use function is_string;
use function strlen;
/**
* The main class for the plugin.
*
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package local_webhooks\local
*/
final class api {
/**
* Create a new record in the database.
*
* @param \local_webhooks\local\record $record
*
* @return int
*
* @throws \coding_exception
* @throws \dml_exception
*/
public static function add_service(record $record): int {
global $DB;
$id = $DB->insert_record(LW_TABLE_SERVICES, $record);
if (is_int($id) && is_array($record->events)) {
$DB->insert_records(LW_TABLE_EVENTS, array_map(static function (string $name) use ($id) {
return ['name' => $name, 'serviceid' => $id];
}, $record->events));
}
if (!is_int($id)) {
throw new coding_exception('Variable \'id\' must be type \'Integer\'');
}
return $id;
}
/**
* Delete an existing record in the database.
*
* @param int $id
*
* @return bool
*
* @throws \dml_exception
*/
public static function del_service(int $id): bool {
global $DB;
$DB->delete_records(LW_TABLE_EVENTS, ['serviceid' => $id]);
return $DB->delete_records(LW_TABLE_SERVICES, ['id' => $id]);
}
/**
* Get an event's list.
*
* @return array
*
* @throws \ReflectionException
*/
public static function get_events(): array {
return array_merge(self::get_core_events_list(), self::get_non_core_event_list());
}
/**
* Get an existing record from the database.
*
* @param int $id
*
* @return \local_webhooks\local\record
*
* @throws \dml_exception
*/
public static function get_service(int $id): record {
global $DB;
$record = $DB->get_record(LW_TABLE_SERVICES, ['id' => $id], '*', MUST_EXIST);
$events = $DB->get_records(LW_TABLE_EVENTS, ['serviceid' => $id]);
$service = new record();
$service->events = array_column($events, 'name');
$service->header = $record->header;
$service->id = $record->id;
$service->name = $record->name;
$service->point = $record->point;
$service->status = $record->status;
$service->token = $record->token;
return $service;
}
/**
* Get list records from the database.
*
* @param array|null $conditions
* @param string|null $sort
* @param int|null $from
* @param int|null $limit
*
* @return array
*
* @throws \dml_exception
*/
public static function get_services(array $conditions = null, string $sort = null, int $from = null, int $limit = null): array {
global $DB;
$records = $DB->get_records(LW_TABLE_SERVICES, $conditions ?? [], $sort ?? '', '*', $from ?? 0, $limit ?? 0);
$services = [];
foreach ($records as $record) {
if (!is_object($record)) {
continue;
}
$events = $DB->get_records(LW_TABLE_EVENTS, ['serviceid' => $record->id]);
$service = new record();
$service->events = array_column($events, 'name');
$service->header = $record->header;
$service->id = $record->id;
$service->name = $record->name;
$service->point = $record->point;
$service->status = $record->status;
$service->token = $record->token;
$services[] = $service;
}
return $services;
}
/**
* Get list records from the database by the event's name.
*
* @param string $name
*
* @return \local_webhooks\local\record[]
*
* @throws \dml_exception
*/
public static function get_services_by_event(string $name): array {
global $DB;
$events = $DB->get_records(LW_TABLE_EVENTS, ['name' => $name]);
$services = [];
foreach ($events as $event) {
if (!is_object($event)) {
continue;
}
$services[] = self::get_service($event->serviceid);
}
return $services;
}
/**
* Get a total count of existing records.
*
* @return int
*
* @throws \dml_exception
*/
public static function get_total_count(): int {
global $DB;
return $DB->count_records(LW_TABLE_SERVICES);
}
/**
* Update an existing record in the database.
*
* @param \local_webhooks\local\record $service
*
* @return bool
*
* @throws \coding_exception
* @throws \dml_exception
*/
public static function set_service(record $service): bool {
global $DB;
$result = $DB->update_record(LW_TABLE_SERVICES, $service);
$DB->delete_records(LW_TABLE_EVENTS, ['serviceid' => $service->id]);
if ($result && is_array($service->events)) {
$DB->insert_records(LW_TABLE_EVENTS, array_map(static function (string $name) use ($service) {
return ['name' => $name, 'serviceid' => $service->id];
}, $service->events));
}
return $result;
}
/**
* Get a system's events list.
*
* @return array
*
* @throws \ReflectionException
*/
private static function get_core_events_list(): array {
global $CFG;
$debugdeveloper = $CFG->debugdeveloper;
$debugdisplay = $CFG->debugdisplay;
$debuglevel = $CFG->debug;
$CFG->debug = 0;
$CFG->debugdeveloper = false;
$CFG->debugdisplay = false;
$directory = $CFG->libdir . '/classes/event';
$events = [];
$files = self::get_file_list($directory);
if (isset($files['unknown_logged'])) {
unset($files['unknown_logged']);
}
foreach (array_keys($files) as $file) {
$name = '\\core\\event\\' . $file;
if (method_exists($name, 'get_static_info')) {
$class = new ReflectionClass($name);
if ($file !== 'manager' && !$class->isAbstract()) {
$events[$name] = $name::get_static_info();
}
}
}
$CFG->debug = $debuglevel;
$CFG->debugdeveloper = $debugdeveloper;
$CFG->debugdisplay = $debugdisplay;
return $events;
}
/**
* Get a file's list in the directory.
*
* @param string $directory
*
* @return array
*/
private static function get_file_list(string $directory): array {
global $CFG;
$files = [];
$root = $CFG->dirroot;
if (is_dir($directory) && is_readable($directory)) {
$handle = opendir($directory);
if ($handle) {
foreach (scandir($directory, SCANDIR_SORT_NONE) as $file) {
if ($file !== '.' && $file !== '..' && strrpos($directory, $root) !== false) {
$location = substr($directory, strlen($root));
$eventname = substr($file, 0, -4);
if (is_string($eventname)) {
$files[$eventname] = $location . '/' . $file;
}
}
}
}
}
return $files;
}
/**
* Get a plugins' events list.
*
* @return array
*
* @throws \ReflectionException
*/
private static function get_non_core_event_list(): array {
global $CFG;
$debugdeveloper = $CFG->debugdeveloper;
$debugdisplay = $CFG->debugdisplay;
$debuglevel = $CFG->debug;
$CFG->debug = 0;
$CFG->debugdeveloper = false;
$CFG->debugdisplay = false;
$events = [];
foreach (array_keys(core_component::get_plugin_types()) as $type) {
foreach (core_component::get_plugin_list($type) as $plugin => $directory) {
$directory .= '/classes/event';
$files = self::get_file_list($directory);
if (isset($files['unknown_logged'])) {
unset($files['unknown_logged']);
}
foreach (array_keys($files) as $file) {
$name = '\\' . $type . '_' . $plugin . '\\event\\' . $file;
if (method_exists($name, 'get_static_info')) {
$class = new ReflectionClass($name);
if ($type . '_' . $plugin !== 'logstore_legacy' && !$class->isAbstract()) {
$events[$name] = $name::get_static_info();
}
}
}
}
}
$CFG->debug = $debuglevel;
$CFG->debugdeveloper = $debugdeveloper;
$CFG->debugdisplay = $debugdisplay;
return $events;
}
}
+95
View File
@@ -0,0 +1,95 @@
<?php
// This file is part of Moodle - http://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/>.
namespace local_webhooks\local;
defined('MOODLE_INTERNAL') || die();
use stdClass;
use function defined;
/**
* It's a class description record.
*
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package local_webhooks\local
*/
final class record extends stdClass {
/**
* List of some events.
*
* @var array|null
*/
public $events;
/**
* Type of the package.
*
* @var string
*/
public $header;
/**
* A unique identifier of the service.
*
* @var int|null
*/
public $id;
/**
* Name of the service.
*
* @var string
*/
public $name;
/**
* Url's an endpoint to send notifications.
*
* @var string
*/
public $point;
/**
* Status activity of the service.
*
* @var bool
*/
public $status;
/**
* Secret key of the service.
*
* @var string
*/
public $token;
/**
* Classes constructor.
*
* @param array|null $conditions
*/
public function __construct(array $conditions = null) {
$this->events = $conditions['events'] ?? null;
$this->header = $conditions['header'] ?? null;
$this->id = $conditions['id'] ?? null;
$this->name = $conditions['name'] ?? null;
$this->point = $conditions['point'] ?? null;
$this->status = $conditions['status'] ?? null;
$this->token = $conditions['token'] ?? null;
}
}
+184
View File
@@ -0,0 +1,184 @@
<?php
// This file is part of Moodle - http://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/>.
namespace local_webhooks\output;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
require_once($CFG->libdir . '/tablelib.php');
use html_writer;
use lang_string;
use local_webhooks\local\api;
use moodle_url;
use pix_icon;
use stdClass;
use table_sql;
use function defined;
use function is_int;
/**
* Class table for list services.
*
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package local_webhooks\output
*/
final class services_table extends table_sql {
/**
* URL editor page.
*
* @var string
*/
private static $editorpage = '/local/webhooks/service.php';
/**
* URL main page.
*
* @var string
*/
private static $mainpage = '/local/webhooks/index.php';
/**
* Class constructor.
*
* @param string $uniqueid
*
* @throws \coding_exception
*/
public function __construct(string $uniqueid) {
parent::__construct($uniqueid);
$this->define_table_columns();
$this->define_table_configs();
}
/**
* Define the action column.
*
* @param \stdClass $row
*
* @return string
*
* @throws \coding_exception
* @throws \moodle_exception
*/
public function col_actions(stdClass $row): string {
global $OUTPUT;
$deletelink = new moodle_url(self::$mainpage, ['deleteid' => $row->id, 'sesskey' => sesskey()]);
$deleteitem = $OUTPUT->action_icon($deletelink, new pix_icon('t/delete', new lang_string('delete', 'moodle')));
$editlink = new moodle_url(self::$editorpage, ['serviceid' => $row->id, 'sesskey' => sesskey()]);
$edititem = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', new lang_string('edit', 'moodle')));
$hideshowlink = new moodle_url(self::$mainpage, ['hideshowid' => $row->id, 'sesskey' => sesskey()]);
$hideshowitem = $OUTPUT->action_icon($hideshowlink, new pix_icon($row->status ? 't/hide' : 't/show', $row->status
? new lang_string('disable', 'moodle')
: new lang_string('enable', 'moodle')
));
return $hideshowitem . $edititem . $deleteitem;
}
/**
* Define the events column.
*
* @param \stdClass $row
*
* @return int
*/
public function col_events(stdClass $row): int {
$total = count($row->events);
return is_int($total) ? $total : 0;
}
/**
* Define the name column.
*
* @param \stdClass $row
*
* @return string
*
* @throws \moodle_exception
*/
public function col_name(stdClass $row): string {
$link = new moodle_url(self::$editorpage, [
'serviceid' => $row->id,
'sesskey' => sesskey(),
]);
return html_writer::link($link, $row->name);
}
/**
* Config table's columns.
*
* @throws \coding_exception
*/
public function define_table_columns() {
$this->define_columns([
'name',
'point',
'events',
'actions',
]);
$this->define_headers([
new lang_string('name', 'moodle'),
new lang_string('url', 'moodle'),
new lang_string('edulevel', 'moodle'),
new lang_string('actions', 'moodle'),
]);
}
/**
* Config table.
*/
public function define_table_configs() {
$this->collapsible(false);
$this->is_downloadable(false);
$this->no_sorting('actions');
$this->no_sorting('events');
$this->pageable(true);
}
/**
* The query for the database.
*
* @param int $pagesize
* @param bool $useinitialsbar
*
* @throws \dml_exception
*/
public function query_db($pagesize, $useinitialsbar = true) {
$this->pagesize($pagesize, $pagesize + 1);
$sort = (string) $this->get_sql_sort();
$this->rawdata = api::get_services(null, $sort, $this->get_page_start(), $this->get_page_size());
$total = api::get_total_count();
$this->pagesize($pagesize, $total);
if ($useinitialsbar) {
$this->initialbars($total > $pagesize);
}
}
}
-135
View File
@@ -1,135 +0,0 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* Defines forms.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined("MOODLE_INTERNAL") || die();
require_once($CFG->libdir . "/formslib.php");
/**
* Description editing form definition.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class service_edit_form extends moodleform {
/**
* @param string $baseurl
*/
public function __construct($baseurl) {
parent::__construct($baseurl);
}
/**
* Defines the standard structure of the form.
*/
protected function definition() {
$mform =& $this->_form;
$size = array("size" => 60);
/* Form heading */
$mform->addElement("header", "editserviceheader", new lang_string("service", "webservice"));
/* Name of the service */
$mform->addElement("text", "title", new lang_string("name", "moodle"), $size);
$mform->addRule("title", null, "required");
$mform->setType("title", PARAM_NOTAGS);
/* Callback address */
$mform->addElement("text", "url", new lang_string("url", "moodle"), $size);
$mform->addRule("url", null, "required");
$mform->setType("url", PARAM_URL);
/* Enabling the service */
$mform->addElement("advcheckbox", "enable", new lang_string("enable", "moodle"));
$mform->setType("enable", PARAM_BOOL);
$mform->setDefault("enable", 1);
$mform->setAdvanced("enable");
/* Token */
$mform->addElement("text", "token", new lang_string("token", "webservice"), $size);
$mform->setType("token", PARAM_NOTAGS);
/* Additional information */
$mform->addElement("text", "other", new lang_string("sourceext", "plugin"), $size);
$mform->setType("other", PARAM_NOTAGS);
$mform->setAdvanced("other");
/* Content type */
$contenttype = array("json" => "application/json", "x-www-form-urlencoded" => "application/x-www-form-urlencoded");
$mform->addElement("select", "type", "Content type", $contenttype);
$mform->setAdvanced("type");
/* Form heading */
$mform->addElement("header", "editserviceheaderevent", new lang_string("edulevel", "moodle"));
/* List of events */
$eventlist = report_eventlist_list_generator::get_all_events_list(true);
$events = array();
/* Formation of the list of elements */
foreach ($eventlist as $event) {
$events[$event["component"]][] =& $mform->createElement("checkbox", $event["eventname"], $event["eventname"]);
}
/* Displays groups of items */
foreach ($events as $key => $event) {
$mform->addGroup($event, "events", $key, "<br />", true);
}
/* Control Panel */
$this->add_action_buttons(true);
}
}
/**
* Description of the form of restoration.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class service_backup_form extends moodleform {
/**
* @param string $baseurl
*/
public function __construct($baseurl) {
parent::__construct($baseurl);
}
/**
* Defines the standard structure of the form.
*/
protected function definition() {
$mform =& $this->_form;
/* Form heading */
$mform->addElement("header", "editserviceheader", new lang_string("restore", "moodle"));
/* Download the file */
$mform->addElement("filepicker", "backupfile", new lang_string("file", "moodle"));
$mform->addRule("backupfile", null, "required");
/* Control Panel */
$this->add_action_buttons(true, new lang_string("restore", "moodle"));
}
}
+90
View File
@@ -0,0 +1,90 @@
<?php
// This file is part of Moodle - http://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/>.
namespace local_webhooks\task;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
use core\task\adhoc_task;
use curl;
use local_webhooks\local\api;
use local_webhooks\local\record;
use function defined;
use function is_object;
use function is_string;
/**
* Class for processing events.
*
* @package local_webhooks\task
*/
final class notify extends adhoc_task {
/**
* Debug information.
*
* @var array
*/
public $debug;
/**
* Process an event.
*
* @throws \dml_exception
*/
public function execute() {
$event = $this->get_custom_data();
if (!is_object($event) || !isset($event->eventname) || !is_string($event->eventname)) {
return;
}
foreach (api::get_services_by_event($event->eventname) as $service) {
if (!is_object($service)) {
continue;
}
if (!$service->status) {
continue;
}
$this->post($service, array_merge((array) $event, [
'token' => $service->token,
]));
}
}
/**
* Send a request for the service.
*
* @param \local_webhooks\local\record $service
* @param array $data
*/
public function post(record $service, array $data) {
$curl = new curl();
$curl->setHeader(['Content-Type: ' . $service->header]);
$curl->post($service->point, json_encode($data));
if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
$this->debug = array_merge($this->debug ?? [], [
compact('data', 'service'),
]);
}
}
}
+105
View File
@@ -0,0 +1,105 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* Defines forms.
*
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package local_webhooks
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/local/webhooks/lib.php');
require_once($CFG->libdir . '/formslib.php');
/**
* Description editing form definition.
*
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package local_webhooks
*/
class local_webhooks_service_edit_form extends moodleform {
/**
* @param string $baseurl
*/
public function __construct($baseurl) {
parent::__construct($baseurl);
}
/**
* Defines the standard structure of the form.
*
* @throws \coding_exception
*/
protected function definition() {
$mform =& $this->_form;
$size = array('size' => 60);
/* Form heading */
$mform->addElement('header', 'edit-service-header-main', new lang_string('service', 'webservice'));
/* Name of the service */
$mform->addElement('text', 'name', new lang_string('name', 'moodle'), $size);
$mform->addRule('name', null, 'required');
$mform->setType('name', PARAM_RAW);
/* Callback address */
$mform->addElement('text', 'point', new lang_string('url', 'moodle'), $size);
$mform->addRule('point', null, 'required');
$mform->setType('point', PARAM_URL);
/* Enabling the service */
$mform->addElement('advcheckbox', 'status', new lang_string('enable', 'moodle'));
$mform->setType('status', PARAM_BOOL);
$mform->setDefault('status', 1);
$mform->setAdvanced('status');
/* Token */
$mform->addElement('text', 'token', new lang_string('token', 'webservice'), $size);
$mform->addRule('token', null, 'required');
$mform->setType('token', PARAM_RAW);
/* Content type */
$contenttype = array(
'application/json' => 'application/json',
'application/x-www-form-urlencoded' => 'application/x-www-form-urlencoded',
);
$mform->addElement('select', 'header', 'Content-Type', $contenttype);
$mform->setAdvanced('header');
/* Form heading */
$mform->addElement('header', 'edit-service-header-event', new lang_string('edulevel', 'moodle'));
/* List of events */
$eventlist = report_eventlist_list_generator::get_all_events_list(true);
$events = array();
foreach ($eventlist as $event) {
$events[$event['component']][] =& $mform->createElement('checkbox', $event['eventname'], $event['eventname']);
}
foreach ($events as $key => $event) {
$mform->addGroup($event, 'events', $key, '<br />', true);
}
/* Control Panel */
$this->add_action_buttons(true);
}
}
+9 -13
View File
@@ -15,21 +15,17 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Interception of all events in the system.
* Registration of observers for events.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined("MOODLE_INTERNAL") || die();
defined('MOODLE_INTERNAL') || die();
$observers = array(
array(
"callback" => "\local_webhooks\\handler::events",
"eventname" => "*",
"includefile" => null,
"internal" => true,
"priority" => 200
)
);
$observers = [
[
'callback' => '\local_webhooks\event\observer::handler',
'eventname' => '*',
],
];
+22 -9
View File
@@ -1,20 +1,33 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="blocks/local_webhooks/db" VERSION="20171029" COMMENT="XMLDB file for Moodle" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd">
<XMLDB PATH="local/webhooks/db" VERSION="2018090700" COMMENT="XMLDB file for Moodle local/webhooks"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd">
<TABLES>
<TABLE NAME="local_webhooks_service" COMMENT="A table for storing callback services.">
<TABLE NAME="local_webhooks_service" COMMENT="Storing created services.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />
<FIELD NAME="enable" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" />
<FIELD NAME="title" TYPE="text" NOTNULL="true" SEQUENCE="false" />
<FIELD NAME="url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
<FIELD NAME="type" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
<FIELD NAME="token" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
<FIELD NAME="events" TYPE="text" NOTNULL="false" SEQUENCE="false" />
<FIELD NAME="other" TYPE="text" NOTNULL="false" SEQUENCE="false" />
<FIELD NAME="header" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" DEFAULT="application/json" COMMENT="Type of outgoing header." />
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Name of the service." />
<FIELD NAME="point" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Point of delivery of notifications." />
<FIELD NAME="status" TYPE="int" LENGTH="1" NOTNULL="true" SEQUENCE="false" DEFAULT="0" COMMENT="Current status of the service." />
<FIELD NAME="token" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Token for verification of requests." />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
</KEYS>
</TABLE>
<TABLE NAME="local_webhooks_events" COMMENT="Storing of viewed events.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Name of the service." />
<FIELD NAME="serviceid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Service ID." />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
+74
View File
@@ -0,0 +1,74 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* This file registers the plugin's external functions.
*
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$functions = [
'local_webhooks_add_service' => [
'classname' => 'local_webhooks_external',
'classpath' => 'local/webhooks/externallib.php',
'description' => 'Add a new service.',
'methodname' => 'add_service',
'type' => 'write',
],
'local_webhooks_del_service' => [
'classname' => 'local_webhooks_external',
'classpath' => 'local/webhooks/externallib.php',
'description' => 'Delete the existing service.',
'methodname' => 'del_service',
'type' => 'write',
],
'local_webhooks_get_events' => [
'classname' => 'local_webhooks_external',
'classpath' => 'local/webhooks/externallib.php',
'description' => 'Get the event\'s list.',
'methodname' => 'get_events',
'type' => 'read',
],
'local_webhooks_get_service' => [
'classname' => 'local_webhooks_external',
'classpath' => 'local/webhooks/externallib.php',
'description' => 'Get data by service.',
'methodname' => 'get_service',
'type' => 'read',
],
'local_webhooks_get_services' => [
'classname' => 'local_webhooks_external',
'classpath' => 'local/webhooks/externallib.php',
'description' => 'Get the service\'s list.',
'methodname' => 'get_services',
'type' => 'read',
],
'local_webhooks_set_service' => [
'classname' => 'local_webhooks_external',
'classpath' => 'local/webhooks/externallib.php',
'description' => 'Update the existing service.',
'methodname' => 'set_service',
'type' => 'write',
],
];
+28
View File
@@ -0,0 +1,28 @@
<?php
// This file is part of Moodle - http://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/>.
defined('MOODLE_INTERNAL') || die();
/**
* Function to upgrade 'local_webhooks'.
*
* @param int $oldversion
*
* @return bool
*/
function xmldb_local_webhooks_upgrade(int $oldversion) {
return true;
}
-80
View File
@@ -1,80 +0,0 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* Page for editing the service.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . "/../../config.php");
require_once(__DIR__ . "/classes/service_form.php");
require_once(__DIR__ . "/lib.php");
require_once($CFG->libdir . "/adminlib.php");
/* Optional parameters */
$serviceid = optional_param("serviceid", 0, PARAM_INT);
/* Link generation */
$urlparameters = array("serviceid" => $serviceid);
$baseurl = new moodle_url("/local/webhooks/editservice.php", $urlparameters);
$managerservice = new moodle_url("/local/webhooks/index.php");
/* Configure the context of the page */
admin_externalpage_setup("local_webhooks", "", null, $baseurl, array());
$context = context_system::instance();
/* Create an editing form */
$mform = new service_edit_form($PAGE->url);
/* Cancel processing */
if ($mform->is_cancelled()) {
redirect($managerservice);
}
/* Getting the data */
$servicerecord = new stdClass();
if ($editing = boolval($serviceid)) {
$servicerecord = local_webhooks_get_record($serviceid);
$mform->set_data($servicerecord);
}
/* Processing of received data */
if ($data = $mform->get_data()) {
if ($editing) {
$data->id = $serviceid;
local_webhooks_update_record($data, false);
redirect($managerservice, new lang_string("eventwebserviceserviceupdated", "webservice"));
} else {
local_webhooks_update_record($data, true);
redirect($managerservice, new lang_string("eventwebserviceservicecreated", "webservice"));
}
}
/* The page title */
$titlepage = new lang_string("externalservice", "webservice");
$PAGE->navbar->add($titlepage);
$PAGE->set_heading($titlepage);
$PAGE->set_title($titlepage);
echo $OUTPUT->header();
/* Displays the form */
$mform->display();
echo $OUTPUT->footer();
+366
View File
@@ -0,0 +1,366 @@
<?php
// This file is part of Moodle - http://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/>.
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' <info@valentineus.link>
* @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.');
}
}
+40 -96
View File
@@ -14,119 +14,63 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Service Management Manager.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__DIR__, 2) . '/config.php');
require_once(__DIR__ . "/../../config.php");
require_once(__DIR__ . "/lib.php");
global $CFG;
require_once($CFG->libdir . "/adminlib.php");
require_once($CFG->libdir . "/tablelib.php");
defined('MOODLE_INTERNAL') || die();
/* Optional parameters */
$backupservices = optional_param("getbackup", 0, PARAM_BOOL);
$deleteid = optional_param("deleteid", 0, PARAM_INT);
$hideshowid = optional_param("hideshowid", 0, PARAM_INT);
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
require_once($CFG->dirroot . '/local/webhooks/classes/output/services_table.php');
require_once($CFG->libdir . '/adminlib.php');
/* Link generation */
$editservice = "/local/webhooks/editservice.php";
$managerservice = "/local/webhooks/index.php";
$restorebackup = "/local/webhooks/restorebackup.php";
$baseurl = new moodle_url($managerservice);
use local_webhooks\local\api;
use local_webhooks\output\services_table;
/* Configure the context of the page */
admin_externalpage_setup("local_webhooks", "", null, $baseurl, array());
$deleteid = optional_param('deleteid', 0, PARAM_INT);
$hideshowid = optional_param('hideshowid', 0, PARAM_INT);
$editpage = '/local/webhooks/service.php';
$mainpage = '/local/webhooks/index.php';
$baseurl = new moodle_url($mainpage);
$context = context_system::instance();
/* Delete the service */
if (boolval($deleteid)) {
local_webhooks_remove_record($deleteid);
redirect($PAGE->url, new lang_string("eventwebserviceservicedeleted", "webservice"));
admin_externalpage_setup('local_webhooks', '', ['contextid' => $context->contextlevel], $baseurl, []);
// Deleting the existing service.
if ($deleteid !== 0 && confirm_sesskey()) {
api::del_service($deleteid);
redirect($PAGE->url, new lang_string('deleted', 'moodle'));
}
/* Retrieving a list of services */
$callbacks = local_webhooks_get_list_records();
// Setting status to existing service.
if ($hideshowid !== 0 && confirm_sesskey()) {
$service = api::get_service($hideshowid);
$service->status = !filter_var($service->status, FILTER_VALIDATE_BOOLEAN);
api::set_service($service);
/* Upload settings as a file */
if (boolval($backupservices)) {
$filecontent = local_webhooks_create_backup();
$filename = "webhooks_" . date("U") . ".backup";
send_file($filecontent, $filename, 0, 0, true, true);
redirect($PAGE->url, new lang_string('changessaved', 'moodle'));
}
/* Switching the status of the service */
if (boolval($hideshowid)) {
$callback = $callbacks[$hideshowid];
if (!empty($callback)) {
$callback->enable = !boolval($callback->enable);
local_webhooks_update_record($callback, false);
redirect($PAGE->url, new lang_string("eventwebserviceserviceupdated", "webservice"));
}
}
/* The page title */
$titlepage = new lang_string("pluginname", "local_webhooks");
// Page header.
$titlepage = new lang_string('pluginname', 'local_webhooks');
$PAGE->set_heading($titlepage);
$PAGE->set_title($titlepage);
echo $OUTPUT->header();
/* Table declaration */
$table = new flexible_table("webhooks-service-table");
// Adds the add button.
$addserviceurl = new moodle_url($editpage, ['sesskey' => sesskey()]);
echo $OUTPUT->single_button($addserviceurl, new lang_string('add', 'moodle'));
/* Customize the table */
$table->define_columns(array("title", "url", "actions"));
$table->define_headers(array(new lang_string("name", "moodle"), new lang_string("url", "moodle"), new lang_string("actions", "moodle")));
// Separation.
echo html_writer::empty_tag('br');
// Displays the table.
$table = new services_table('local-webhooks-services');
$table->define_baseurl($baseurl);
$table->setup();
foreach ($callbacks as $callback) {
/* Filling of information columns */
$titlecallback = html_writer::div($callback->title, "title");
$urlcallback = html_writer::div($callback->url, "url");
/* Defining service status */
$hideshowicon = "t/show";
$hideshowstring = new lang_string("enable", "moodle");
if (boolval($callback->enable)) {
$hideshowicon = "t/hide";
$hideshowstring = new lang_string("disable", "moodle");
}
/* Link to enable / disable the service */
$hideshowlink = new moodle_url($managerservice, array("hideshowid" => $callback->id));
$hideshowitem = $OUTPUT->action_icon($hideshowlink, new pix_icon($hideshowicon, $hideshowstring));
/* Link for editing */
$editlink = new moodle_url($editservice, array("serviceid" => $callback->id));
$edititem = $OUTPUT->action_icon($editlink, new pix_icon("t/edit", new lang_string("edit", "moodle")));
/* Link to remove */
$deletelink = new moodle_url($managerservice, array("deleteid" => $callback->id));
$deleteitem = $OUTPUT->action_icon($deletelink, new pix_icon("t/delete", new lang_string("delete", "moodle")));
/* Adding data to the table */
$table->add_data(array($titlecallback, $urlcallback, $hideshowitem . $edititem . $deleteitem));
}
/* Display the table */
$table->print_html();
/* Add service button */
$addserviceurl = new moodle_url($editservice);
echo $OUTPUT->single_button($addserviceurl, new lang_string("addaservice", "webservice"), "get");
/* Button to get a backup */
$backupurl = new moodle_url($managerservice, array("getbackup" => true));
echo $OUTPUT->single_button($backupurl, new lang_string("backup", "moodle"), "get");
/* Button for restoring settings */
$restorebackupurl = new moodle_url($restorebackup);
echo $OUTPUT->single_button($restorebackupurl, new lang_string("restore", "moodle"), "get");
$table->out(25, true);
// Footer.
echo $OUTPUT->footer();
+4 -4
View File
@@ -15,11 +15,11 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Strings for component "local_webhooks", language "en".
* Strings for component 'local_webhooks', language 'en'.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package local_webhooks
*/
$string["pluginname"] = "WebHooks";
$string['pluginname'] = 'WebHooks';
-165
View File
@@ -1,165 +0,0 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* Library code used by the service control interfaces.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined("MOODLE_INTERNAL") || die();
require_once(__DIR__ . "/locallib.php");
/**
* Getting a list of all services.
*
* @param number $limitfrom
* @param number $limitnum
* @return array
*/
function local_webhooks_get_list_records($limitfrom = 0, $limitnum = 0) {
global $DB;
$listservices = $DB->get_records("local_webhooks_service", null, "id", "*", $limitfrom, $limitnum);
foreach ($listservices as $servicerecord) {
if (!empty($servicerecord->events)) {
$servicerecord->events = local_webhooks_unarchive_data($servicerecord->events);
}
}
return $listservices;
}
/**
* Getting information about the service.
*
* @param number $serviceid
* @return object
*/
function local_webhooks_get_record($serviceid = 0) {
global $DB;
$servicerecord = $DB->get_record("local_webhooks_service", array("id" => $serviceid), "*", MUST_EXIST);
if (!empty($servicerecord->events)) {
$servicerecord->events = local_webhooks_unarchive_data($servicerecord->events);
}
return $servicerecord;
}
/**
* Clear the database table.
*/
function local_webhooks_remove_list_records() {
global $DB;
$DB->delete_records("local_webhooks_service", null);
}
/**
* Delete the record.
*
* @param number $serviceid
*/
function local_webhooks_remove_record($serviceid = 0) {
global $DB;
$DB->delete_records("local_webhooks_service", array("id" => $serviceid));
local_webhooks_events::service_deleted($serviceid);
}
/**
* Update the record in the database.
*
* @param object $data
* @param boolean $insert
* @return boolean
*/
function local_webhooks_update_record($data, $insert = true) {
global $DB;
if (empty($data->events)) {
$data->events = array();
}
$data->events = local_webhooks_archiving_data($data->events);
if (boolval($insert)) {
$result = $DB->insert_record("local_webhooks_service", $data, true, false);
local_webhooks_events::service_added($result);
} else {
$result = $DB->update_record("local_webhooks_service", $data, false);
local_webhooks_events::service_updated($data->id);
}
return boolval($result);
}
/**
* Make a backup copy of all the services.
*
* @return string
*/
function local_webhooks_create_backup() {
$listservices = local_webhooks_get_list_records();
$listservices = local_webhooks_archiving_data($listservices);
local_webhooks_events::backup_performed();
return $listservices;
}
/**
* Restore the data from the backup.
*
* @param string $data
*/
function local_webhooks_restore_backup($listservices = "") {
$listservices = local_webhooks_unarchive_data($listservices);
local_webhooks_remove_list_records();
foreach ($listservices as $servicerecord) {
local_webhooks_update_record($servicerecord, true);
}
local_webhooks_events::backup_restored();
}
/**
* Compress an array into a string.
*
* @param array $data
* @return string
*/
function local_webhooks_archiving_data($data = array()) {
$result = base64_encode(gzcompress(serialize($data), 3));
return $result;
}
/**
* Gets an array from a compressed string.
*
* @param string $data
* @return array
*/
function local_webhooks_unarchive_data($data = "") {
$result = unserialize(gzuncompress(base64_decode($data)));
return $result;
}
-144
View File
@@ -1,144 +0,0 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* Classes of modules.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined("MOODLE_INTERNAL") || die();
/**
* Description of functions of the call of events
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class local_webhooks_events {
/**
* Call the event when creating a backup.
*/
public static function backup_performed() {
$context = context_system::instance();
$event = local_webhooks\event\backup_performed::create(
array(
"context" => $context,
"objectid" => 0
)
);
$event->trigger();
}
/**
* Call the event when restoring from a backup.
*/
public static function backup_restored() {
$context = context_system::instance();
$event = local_webhooks\event\backup_restored::create(
array(
"context" => $context,
"objectid" => 0
)
);
$event->trigger();
}
/**
* Call event when the response is received from the service
*
* @param number $objectid Service ID
* @param array $response Server response
*/
public static function response_answer($objectid = 0, $response = array()) {
$context = context_system::instance();
$status = "Error sending request";
if (!empty($response["HTTP/1.1"])) {
$status = $response["HTTP/1.1"];
}
$event = local_webhooks\event\response_answer::create(
array(
"context" => $context,
"objectid" => $objectid,
"other" => array("status" => $status)
)
);
$event->trigger();
}
/**
* Call the event when the service is added.
*
* @param number $objectid Service ID
*/
public static function service_added($objectid = 0) {
$context = context_system::instance();
$event = local_webhooks\event\service_added::create(
array(
"context" => $context,
"objectid" => $objectid
)
);
$event->trigger();
}
/**
* Call the event when the service is deleted.
*
* @param number $objectid Service ID
*/
public static function service_deleted($objectid = 0) {
$context = context_system::instance();
$event = local_webhooks\event\service_deleted::create(
array(
"context" => $context,
"objectid" => $objectid
)
);
$event->trigger();
}
/**
* Call event when the service is updated.
*
* @param number $objectid Service ID
*/
public static function service_updated($objectid = 0) {
$context = context_system::instance();
$event = local_webhooks\event\service_updated::create(
array(
"context" => $context,
"objectid" => $objectid
)
);
$event->trigger();
}
}
-64
View File
@@ -1,64 +0,0 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* Restore the settings page.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . "/../../config.php");
require_once(__DIR__ . "/classes/service_form.php");
require_once(__DIR__ . "/lib.php");
require_once($CFG->libdir . "/adminlib.php");
/* Link generation */
$baseurl = new moodle_url("/local/webhooks/restorebackup.php");
$managerservice = new moodle_url("/local/webhooks/index.php");
/* Configure the context of the page */
admin_externalpage_setup("local_webhooks", "", null, $baseurl, array());
$context = context_system::instance();
/* Create an editing form */
$mform = new service_backup_form($PAGE->url);
/* Cancel processing */
if ($mform->is_cancelled()) {
redirect($managerservice);
}
/* Processing the received file */
if ($data = $mform->get_data()) {
$content = $mform->get_file_content("backupfile");
local_webhooks_restore_backup($content);
redirect($managerservice, new lang_string("restorefinished", "moodle"));
}
/* The page title */
$titlepage = new lang_string("backup", "moodle");
$PAGE->navbar->add($titlepage);
$PAGE->set_heading($titlepage);
$PAGE->set_title($titlepage);
echo $OUTPUT->header();
/* Displays the form */
$mform->display();
echo $OUTPUT->footer();
+82
View File
@@ -0,0 +1,82 @@
<?php
// This file is part of Moodle - http://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/>.
/**
* Page of the service editor.
*
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package local_webhooks
*/
require_once(__DIR__ . '/../../config.php');
require_once($CFG->dirroot . '/local/webhooks/classes/ui_forms_plugin.php');
require_once($CFG->dirroot . '/local/webhooks/lib.php');
require_once($CFG->libdir . '/adminlib.php');
$serviceid = optional_param('serviceid', 0, PARAM_INT);
$urlparameters = array('serviceid' => $serviceid);
$baseurl = new moodle_url('/local/webhooks/service.php', $urlparameters);
$mainpage = new moodle_url('/local/webhooks/index.php');
admin_externalpage_setup('local_webhooks', '', null, $baseurl, array());
$context = context_system::instance();
$mform = new local_webhooks_service_edit_form($PAGE->url);
$formdata = (array) $mform->get_data();
/* Cancel */
if ($mform->is_cancelled()) {
redirect($mainpage);
}
/* Updating the data */
if (!empty($formdata) && confirm_sesskey()) {
if (isset($formdata['events'])) {
$formdata['events'] = array_keys($formdata['events']);
}
if (!empty($serviceid)) {
$formdata['id'] = $serviceid;
local_webhooks_api::update_service($formdata);
} else {
local_webhooks_api::create_service($formdata);
}
redirect($mainpage, new lang_string('changessaved', 'moodle'));
}
/* Loading service data */
if (!empty($serviceid)) {
$service = local_webhooks_api::get_service($serviceid);
$service->events = array_fill_keys($service->events, 1);
$mform->set_data($service);
}
/* The page title */
$titlepage = new lang_string('externalservice', 'webservice');
$PAGE->navbar->add($titlepage);
$PAGE->set_heading($titlepage);
$PAGE->set_title($titlepage);
echo $OUTPUT->header();
/* Displays the form */
$mform->display();
/* Footer */
echo $OUTPUT->footer();
+12 -7
View File
@@ -17,16 +17,21 @@
/**
* Settings of the plugin.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package local_webhooks
*/
defined("MOODLE_INTERNAL") || die();
defined('MOODLE_INTERNAL') || die();
/* @var \admin_root $ADMIN */
if ($hassiteconfig) {
$ADMIN->add("server", new admin_externalpage("local_webhooks",
new lang_string("pluginname", "local_webhooks"),
new moodle_url("/local/webhooks/index.php")
));
$ADMIN->add(
'server', new admin_externalpage(
'local_webhooks',
new lang_string('pluginname', 'local_webhooks'),
new moodle_url('/local/webhooks/index.php')
)
);
}
+364
View File
@@ -0,0 +1,364 @@
<?php
// This file is part of Moodle - http://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/>.
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
use local_webhooks\local\api;
use local_webhooks\local\record;
/**
* Testing the API plugin class.
*
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class local_webhooks_api_testcase extends advanced_testcase {
/**
* Generate random an event's list.
*
* @return array
*
* @throws \ReflectionException
*/
private static function get_random_events(): array {
$result = array_rand(api::get_events(), random_int(2, 10));
return is_array($result) ? $result : [];
}
/**
* Generate a random record.
*
* @return \local_webhooks\local\record
*
* @throws \ReflectionException
*/
private static function get_random_record(): record {
$record = new record();
$record->events = self::get_random_events();
$record->header = 'application/json';
$record->name = uniqid('', false);
$record->point = 'http://example.org/' . urlencode($record->name);
$record->status = true;
$record->token = generate_uuid();
return $record;
}
/**
* Testing creation of the service.
*
* @group local_webhooks
*
* @throws \dml_exception
* @throws \moodle_exception
* @throws \ReflectionException
*/
public function test_adding() {
global $DB;
$this->resetAfterTest();
$record = self::get_random_record();
$record->id = api::add_service($record);
$events = $DB->get_records(LW_TABLE_EVENTS);
$services = $DB->get_records(LW_TABLE_SERVICES);
self::assertCount(1, $services);
$service = array_shift($services);
self::assertEquals($record->header, $service->header);
self::assertEquals($record->id, $service->id);
self::assertEquals($record->name, $service->name);
self::assertEquals($record->point, $service->point);
self::assertEquals($record->status, (bool) $service->status);
self::assertEquals($record->token, $service->token);
self::assertCount(count($record->events), $events);
foreach ($events as $event) {
self::assertContains($event->name, $record->events);
self::assertEquals($record->id, $event->serviceid);
}
}
/**
* Test deletion of the service.
*
* @group local_webhooks
*
* @throws \dml_exception
* @throws \moodle_exception
* @throws \ReflectionException
*/
public function test_deleting() {
global $DB;
$this->resetAfterTest();
// Testing correct delete record of the database.
$record = self::get_random_record();
$record->id = api::add_service($record);
self::assertTrue(api::del_service($record->id));
self::assertCount(0, $DB->get_records(LW_TABLE_EVENTS));
self::assertCount(0, $DB->get_records(LW_TABLE_SERVICES));
$ids = [];
$total = random_int(5, 20);
// Testing correct delete record of the record's list.
for ($i = 0; $i < $total; $i++) {
$record = self::get_random_record();
$ids[] = api::add_service($record);
}
self::assertEquals(count($ids), api::get_total_count());
self::assertTrue(api::del_service($ids[array_rand($ids, 1)]));
self::assertEquals(count($ids) - 1, api::get_total_count());
}
/**
* Testing get an event's list.
*
* @group local_webhooks
*
* @throws \ReflectionException
*/
public function test_get_events() {
$this->resetAfterTest();
$events = api::get_events();
self::assertNotCount(0, $events);
foreach ($events as $event) {
self::assertInternalType('array', $event);
self::assertEquals([
'eventname', 'component', 'target', 'action', 'crud', 'edulevel', 'objecttable',
], array_keys($event));
}
}
/**
* Testing get to a service.
*
* @group local_webhooks
*
* @throws \dml_exception
* @throws \moodle_exception
* @throws \ReflectionException
*/
public function test_get_service() {
$this->resetAfterTest();
$record = self::get_random_record();
$record->id = api::add_service($record);
$service = api::get_service($record->id);
self::assertEquals($record->header, $service->header);
self::assertEquals($record->id, $service->id);
self::assertEquals($record->name, $service->name);
self::assertEquals($record->point, $service->point);
self::assertEquals($record->status, (bool) $service->status);
self::assertEquals($record->token, $service->token);
self::assertInternalType('array', $service->events);
self::assertCount(count($record->events), $service->events);
foreach ($service->events as $event) {
self::assertContains($event, $record->events);
}
}
/**
* Testing get to the list services.
*
* @group local_webhooks
*
* @throws \dml_exception
* @throws \moodle_exception
* @throws \ReflectionException
*/
public function test_get_services() {
$this->resetAfterTest();
$records = [];
$total = random_int(5, 20);
for ($i = 0; $i < $total; $i++) {
$record = self::get_random_record();
$record->id = api::add_service($record);
$records[$record->id] = $record;
}
$services = api::get_services();
self::assertCount(count($records), $services);
foreach ($services as $service) {
$record = $records[$service->id];
self::assertEquals($record->header, $service->header);
self::assertEquals($record->id, $service->id);
self::assertEquals($record->name, $service->name);
self::assertEquals($record->point, $service->point);
self::assertEquals($record->status, $service->status);
self::assertEquals($record->token, $service->token);
self::assertInternalType('array', $service->events);
self::assertCount(count($record->events), $service->events);
foreach ($service->events as $event) {
self::assertContains($event, $record->events);
}
}
}
/**
* Testing get to the list services by event name.
*
* @group local_webhooks
*
* @throws \dml_exception
* @throws \moodle_exception
* @throws \ReflectionException
*/
public function test_get_services_by_event() {
$this->resetAfterTest();
$eventname = generate_uuid();
$limit = random_int(1, 5);
$total = random_int(5, 20);
$ids = [];
for ($i = 0; $i < $total; $i++) {
$record = self::get_random_record();
$record->events[] = $i < $limit ? $eventname : '';
$ids[] = api::add_service($record);
}
self::assertEquals(count($ids), api::get_total_count());
$services = api::get_services_by_event($eventname);
self::assertCount($limit, $services);
foreach ($services as $service) {
self::assertContains($service->id, $ids);
}
}
/**
* Testing get to the list services with conditions.
*
* @group local_webhooks
*
* @throws \dml_exception
* @throws \moodle_exception
* @throws \ReflectionException
*/
public function test_get_services_with_conditions() {
$this->resetAfterTest();
$records = [];
$total = random_int(5, 20);
$limit = intdiv($total, 2);
// Creating some records.
for ($i = 0; $i < $total; $i++) {
$record = self::get_random_record();
$record->id = api::add_service($record);
$records[] = $record;
}
// Testing condition fields.
$record = $records[array_rand($records, 1)];
self::assertCount(1, api::get_services([
'point' => $record->point,
]));
// Testing limit fields.
self::assertCount($limit, api::get_services([], null, 1, $limit));
// Testing sort fields.
$service1 = api::get_services(null, 'id asc')[0];
$service2 = api::get_services(null, 'id desc')[0];
self::assertNotEquals($service1->id, $service2->id);
}
/**
* Testing get a total count of existing records.
*
* @throws \coding_exception
* @throws \dml_exception
* @throws \ReflectionException
*/
public function test_total() {
$this->resetAfterTest();
$total = random_int(5, 20);
for ($i = 0; $i < $total; $i++) {
$record = self::get_random_record();
api::add_service($record);
}
self::assertEquals($total, api::get_total_count());
}
/**
* Testing of the service update.
*
* @group local_webhooks
*
* @throws \dml_exception
* @throws \moodle_exception
* @throws \ReflectionException
*/
public function test_updating() {
global $DB;
$this->resetAfterTest();
$record1 = self::get_random_record();
$record2 = self::get_random_record();
$record2->id = api::add_service($record1);
self::assertTrue(api::set_service($record2));
$events = $DB->get_records(LW_TABLE_EVENTS);
$services = $DB->get_records(LW_TABLE_SERVICES);
self::assertCount(1, $services);
$service = array_shift($services);
self::assertEquals($record2->header, $service->header);
self::assertEquals($record2->id, $service->id);
self::assertEquals($record2->name, $service->name);
self::assertEquals($record2->point, $service->point);
self::assertEquals($record2->status, (bool) $service->status);
self::assertEquals($record2->token, $service->token);
self::assertCount(count($record2->events), $events);
foreach ($events as $event) {
self::assertContains($event->name, $record2->events);
self::assertEquals($record2->id, $event->serviceid);
}
}
}
+386
View File
@@ -0,0 +1,386 @@
<?php
// This file is part of Moodle - http://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/>.
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/local/webhooks/externallib.php');
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
use local_webhooks\local\api;
use local_webhooks\local\record;
/**
* Testing external functions.
*
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class local_webhooks_external_testcase extends externallib_advanced_testcase {
/**
* Generate random an event's list.
*
* @return array
*
* @throws \ReflectionException
*/
private static function get_random_events(): array {
$result = array_rand(api::get_events(), random_int(2, 10));
return is_array($result) ? $result : [];
}
/**
* Generate a random record.
*
* @return \local_webhooks\local\record
*
* @throws \ReflectionException
*/
private static function get_random_record(): record {
$record = new record();
$record->events = self::get_random_events();
$record->header = 'application/json';
$record->name = uniqid('', false);
$record->point = 'http://example.org/' . urlencode($record->name);
$record->status = true;
$record->token = generate_uuid();
return $record;
}
/**
* Testing external add service.
*
* @throws \ReflectionException
* @throws \coding_exception
* @throws \dml_exception
* @throws \invalid_parameter_exception
* @throws \invalid_response_exception
* @throws \restricted_context_exception
*/
public function test_adding() {
$this->resetAfterTest();
self::setAdminUser();
$record = self::get_random_record();
$return = local_webhooks_external::add_service((array) $record);
$return = external_api::clean_returnvalue(local_webhooks_external::add_service_returns(), $return);
self::assertInternalType('integer', $return);
self::assertEquals(1, api::get_total_count());
$service = api::get_service($return);
self::assertEquals($record->header, $service->header);
self::assertEquals($record->name, $service->name);
self::assertEquals($record->point, $service->point);
self::assertEquals($record->status, $service->status);
self::assertEquals($record->token, $service->token);
self::assertEquals($return, $service->id);
self::assertInternalType('array', $service->events);
self::assertCount(count($record->events), $service->events);
foreach ($service->events as $event) {
self::assertContains($event, $record->events);
}
}
/**
* Testing the external delete service.
*
* @throws \ReflectionException
* @throws \coding_exception
* @throws \dml_exception
* @throws \invalid_parameter_exception
* @throws \invalid_response_exception
* @throws \restricted_context_exception
*/
public function test_deleting() {
$this->resetAfterTest();
self::setAdminUser();
// Testing correct delete record of the database.
$record = self::get_random_record();
$record->id = api::add_service($record);
$return = local_webhooks_external::del_service($record->id);
$return = external_api::clean_returnvalue(local_webhooks_external::del_service_returns(), $return);
self::assertEquals(0, api::get_total_count());
self::assertInternalType('bool', $return);
$ids = [];
$total = random_int(5, 20);
// Testing correct delete record of the record's list.
for ($i = 0; $i < $total; $i++) {
$record = self::get_random_record();
$ids[] = api::add_service($record);
}
self::assertEquals(count($ids), api::get_total_count());
$return = local_webhooks_external::del_service($ids[array_rand($ids, 1)]);
$return = external_api::clean_returnvalue(local_webhooks_external::del_service_returns(), $return);
self::assertEquals(count($ids) - 1, api::get_total_count());
self::assertInternalType('bool', $return);
}
/**
* Testing external function get the event's list.
*
* @throws \ReflectionException
* @throws \dml_exception
* @throws \invalid_parameter_exception
* @throws \invalid_response_exception
* @throws \restricted_context_exception
*/
public function test_get_events() {
$this->resetAfterTest();
self::setAdminUser();
$return = local_webhooks_external::get_events();
$return = external_api::clean_returnvalue(local_webhooks_external::get_events_returns(), $return);
self::assertInternalType('array', $return);
self::assertCount(count(api::get_events()), $return);
foreach ($return as $item) {
self::assertInternalType('array', $item);
self::assertEquals([
'action', 'component', 'crud', 'edulevel', 'eventname', 'objecttable', 'target',
], array_keys($item));
}
}
/**
* Testing external get record's data.
*
* @throws \ReflectionException
* @throws \coding_exception
* @throws \dml_exception
* @throws \invalid_parameter_exception
* @throws \invalid_response_exception
* @throws \restricted_context_exception
*/
public function test_get_service() {
$this->resetAfterTest();
self::setAdminUser();
// Creating a new record.
$record = self::get_random_record();
$record->id = api::add_service($record);
$return = local_webhooks_external::get_service($record->id);
$return = external_api::clean_returnvalue(local_webhooks_external::get_service_returns(), $return);
self::assertInternalType('array', $return);
// Testing the main fields.
self::assertEquals($record->header, $return['header']);
self::assertEquals($record->id, $return['id']);
self::assertEquals($record->name, $return['name']);
self::assertEquals($record->point, $return['point']);
self::assertEquals($record->status, (int) $return['status']);
self::assertEquals($record->token, $return['token']);
// Testing an event's list.
self::assertInternalType('array', $return['events']);
self::assertNotCount(0, $return['events']);
foreach ($return['events'] as $event) {
self::assertContains($event, $record->events);
}
}
/**
* Testing external get the record's list.
*
* @throws \ReflectionException
* @throws \coding_exception
* @throws \dml_exception
* @throws \invalid_parameter_exception
* @throws \invalid_response_exception
* @throws \restricted_context_exception
*/
public function test_get_services() {
$this->resetAfterTest();
self::setAdminUser();
$records = [];
$total = random_int(5, 10);
// Creating some records.
for ($i = 0; $i < $total; $i++) {
$record = self::get_random_record();
$record->id = api::add_service($record);
$records[$record->id] = $record;
}
self::assertEquals(count($records), api::get_total_count());
$return = local_webhooks_external::get_services();
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
// Testing received item's list.
self::assertInternalType('array', $return);
self::assertCount(count($records), $return);
foreach ($return as $item) {
self::assertInternalType('array', $item);
$record = $records[$item['id']];
self::assertEquals($record->header, $item['header']);
self::assertEquals($record->id, $item['id']);
self::assertEquals($record->name, $item['name']);
self::assertEquals($record->point, $item['point']);
self::assertEquals($record->status, (int) $item['status']);
self::assertEquals($record->token, $item['token']);
self::assertInternalType('array', $item['events']);
self::assertNotCount(0, $item['events']);
foreach ($item['events'] as $event) {
self::assertContains($event, $record->events);
}
}
}
/**
* Testing external get to the list services with conditions.
*
* @throws \ReflectionException
* @throws \coding_exception
* @throws \dml_exception
* @throws \invalid_parameter_exception
* @throws \invalid_response_exception
* @throws \restricted_context_exception
*/
public function test_get_services_with_conditions() {
$this->resetAfterTest();
self::setAdminUser();
$records = [];
$total = random_int(5, 10);
$limit = intdiv($total, 2);
// Creating some records.
for ($i = 0; $i < $total; $i++) {
$record = self::get_random_record();
$record->id = api::add_service($record);
$records[$record->id] = $record;
}
self::assertEquals(count($records), api::get_total_count());
// Testing condition fields.
$record = $records[array_rand($records, 1)];
$return = local_webhooks_external::get_services(['point' => $record->point]);
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
self::assertCount(1, $return);
self::assertEquals($return[0]['id'], $record->id);
// Testing limit fields.
$return = local_webhooks_external::get_services(null, null, 1, $limit);
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
self::assertCount($limit, $return);
// Testing sort fields.
$return = local_webhooks_external::get_services(null, 'id asc');
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
$service1 = array_shift($return);
$return = local_webhooks_external::get_services(null, 'id desc');
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
$service2 = array_shift($return);
self::assertNotEquals($service1['id'], $service2['id']);
}
/**
* Testing full update parameters of the service.
*
* @throws \ReflectionException
* @throws \coding_exception
* @throws \dml_exception
* @throws \invalid_parameter_exception
* @throws \restricted_context_exception
*/
public function test_updating_full() {
$this->resetAfterTest();
self::setAdminUser();
$record = self::get_random_record();
$record2 = self::get_random_record();
$record2->id = api::add_service($record);
$return = local_webhooks_external::set_service((array) $record2);
$return = external_api::validate_parameters(local_webhooks_external::set_service_returns(), $return);
self::assertInternalType('bool', $return);
$service = api::get_service($record2->id);
self::assertEquals($record2->id, $service->id);
self::assertEquals($record2->header, $service->header);
self::assertEquals($record2->name, $service->name);
self::assertEquals($record2->point, $service->point);
self::assertEquals($record2->status, $service->status);
self::assertEquals($record2->token, $service->token);
self::assertInternalType('array', $service->events);
self::assertCount(count($record2->events), $service->events);
foreach ($service->events as $event) {
self::assertContains($event, $record2->events);
}
}
/**
* Testing selective update parameters of the service.
*
* @throws \ReflectionException
* @throws \coding_exception
* @throws \dml_exception
* @throws \invalid_parameter_exception
* @throws \restricted_context_exception
*/
public function test_updating_short() {
$this->resetAfterTest();
self::setAdminUser();
$record = self::get_random_record();
$record2 = self::get_random_record();
$record->id = api::add_service($record);
$return = local_webhooks_external::set_service(['id' => $record->id, 'name' => $record2->name, 'point' => $record2->point]);
$return = external_api::validate_parameters(local_webhooks_external::set_service_returns(), $return);
self::assertInternalType('bool', $return);
$service = api::get_service($record->id);
self::assertEquals($record->header, $service->header);
self::assertEquals($record->id, $service->id);
self::assertEquals($record->status, $service->status);
self::assertEquals($record->token, $service->token);
self::assertEquals($record2->name, $service->name);
self::assertEquals($record2->point, $service->point);
self::assertInternalType('array', $service->events);
self::assertCount(count($record->events), $service->events);
foreach ($service->events as $event) {
self::assertContains($event, $record->events);
}
}
}
+205
View File
@@ -0,0 +1,205 @@
<?php
// This file is part of Moodle - http://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/>.
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
use core\event\course_created;
use core\task\manager;
use local_webhooks\local\api;
use local_webhooks\local\record;
use local_webhooks\task\notify;
/**
* Class local_webhooks_task_testcase.
*
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class local_webhooks_task_testcase extends advanced_testcase {
const EVENTNAME = '\\' . course_created::class;
/**
* Generate a random record.
*
* @return \local_webhooks\local\record
*/
private static function get_random_record(): record {
$record = new record();
$record->events = [self::EVENTNAME];
$record->header = 'application/json';
$record->name = uniqid('', false);
$record->point = 'http://example.org/' . urlencode($record->name);
$record->status = true;
$record->token = generate_uuid();
return $record;
}
/**
* Testing add a task to the queue.
*/
public function test_add_task() {
global $DB;
$DB->delete_records('task_adhoc');
$this->resetAfterTest();
manager::queue_adhoc_task(new notify());
$tasks = manager::get_adhoc_tasks(notify::class);
self::assertCount(1, $tasks);
self::assertInstanceOf(notify::class, array_shift($tasks));
}
/**
* Testing disabled processing service.
*
* @throws \coding_exception
* @throws \dml_exception
*/
public function test_execute_disabled() {
global $DB;
$DB->delete_records('task_adhoc');
curl::mock_response('{}');
$this->resetAfterTest();
$record = self::get_random_record();
$record->status = false;
$record->id = api::add_service($record);
$task = new notify();
$task->set_custom_data(['eventname' => self::EVENTNAME]);
$task->execute();
self::assertNull($task->debug);
}
/**
* Testing enabled processing service.
*
* @throws \coding_exception
* @throws \dml_exception
*/
public function test_execute_enabled() {
global $DB;
$DB->delete_records('task_adhoc');
curl::mock_response('{}');
$this->resetAfterTest();
$record = self::get_random_record();
$record->id = api::add_service($record);
$task = new notify();
$task->set_custom_data(['eventname' => self::EVENTNAME]);
$task->execute();
self::assertCount(1, $task->debug);
self::assertInternalType('array', $task->debug);
$element = array_shift($task->debug);
self::assertInternalType('array', $element['data']);
self::assertInternalType('object', $element['service']);
self::assertEquals($record->events[0], $element['data']['eventname']);
self::assertEquals($record->id, $element['service']->id);
self::assertEquals($record->token, $element['data']['token']);
}
/**
* Testing count creating tasks.
*
* @throws \coding_exception
* @throws \dml_exception
*/
public function test_observer_multiple() {
global $DB;
$DB->delete_records('task_adhoc');
curl::mock_response('{}');
$this->resetAfterTest();
$generator = self::getDataGenerator();
$record = self::get_random_record();
$record->id = api::add_service($record);
$total = random_int(5, 20);
for ($i = 0; $i < $total; $i++) {
$generator->create_course();
}
$debug = [];
foreach (manager::get_adhoc_tasks(notify::class) as $event) {
self::assertInstanceOf(notify::class, $event);
/** @var \local_webhooks\task\notify $event */
$event->execute();
if (isset($event->debug) && is_array($event->debug)) {
$debug[] = array_merge(...$event->debug);
}
}
self::assertCount($total, $debug);
}
/**
* Testing structure a creating task.
*
* @throws \coding_exception
* @throws \dml_exception
*/
public function test_observer_single() {
global $DB;
$DB->delete_records('task_adhoc');
curl::mock_response('{}');
$this->resetAfterTest();
$generator = self::getDataGenerator();
$record = self::get_random_record();
$record->id = api::add_service($record);
$course = $generator->create_course();
$debug = [];
foreach (manager::get_adhoc_tasks(notify::class) as $event) {
self::assertInstanceOf(notify::class, $event);
/** @var \local_webhooks\task\notify $event */
$event->execute();
if (isset($event->debug) && is_array($event->debug)) {
$debug[] = array_merge(...$event->debug);
}
}
self::assertCount(1, $debug);
$element = array_shift($debug);
self::assertInternalType('array', $element['data']);
self::assertEquals($course->id, $element['data']['courseid']);
}
}
+6 -14
View File
@@ -14,18 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Information about the version of the plugin.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
defined("MOODLE_INTERNAL") || die();
$plugin->release = "3.0.0 (Build: 2017112600)";
$plugin->version = 2017112600;
$plugin->requires = 2016112900;
$plugin->component = "local_webhooks";
$plugin->maturity = MATURITY_STABLE;
$plugin->component = 'local_webhooks';
$plugin->maturity = MATURITY_ALPHA;
$plugin->release = '5.0.0-rc.1 (Build: 2019040100)';
$plugin->requires = 2016052300;
$plugin->version = 2019040100;