aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValentin Popov <info@valentineus.link>2018-10-22 01:17:12 +0300
committerValentin Popov <info@valentineus.link>2018-10-22 01:59:06 +0300
commit1352b5ee796c9a9e106cb9746b9201e8038b6210 (patch)
treec922ed9bacd8b2ac0d278cb44697ade9124b380f
parentf3b5ab561616ce42c5ddff11b632635a1c71f88f (diff)
downloadlocal_webhooks-1352b5ee796c9a9e106cb9746b9201e8038b6210.tar.xz
local_webhooks-1352b5ee796c9a9e106cb9746b9201e8038b6210.zip
Code style fix
Signed-off-by: Valentin Popov <info@valentineus.link>
-rw-r--r--classes/event_observer.php8
-rw-r--r--classes/task/process_events_task.php25
-rw-r--r--classes/ui_forms_plugin.php66
-rw-r--r--classes/ui_tables_plugin.php95
-rw-r--r--db/events.php8
-rw-r--r--db/services.php2
-rw-r--r--externallib.php6
-rw-r--r--index.php52
-rw-r--r--lang/en/local_webhooks.php2
-rw-r--r--lib.php108
-rw-r--r--service.php56
-rw-r--r--settings.php14
-rw-r--r--version.php14
13 files changed, 250 insertions, 206 deletions
diff --git a/classes/event_observer.php b/classes/event_observer.php
index 9ab4a35..983552a 100644
--- a/classes/event_observer.php
+++ b/classes/event_observer.php
@@ -24,7 +24,7 @@
namespace local_webhooks;
-defined( "MOODLE_INTERNAL" ) || die();
+defined('MOODLE_INTERNAL') || die();
/**
* Defines event handlers.
@@ -40,9 +40,9 @@ class event_observer {
*
* @param object $event
*/
- public static function observe_all( $event ) {
+ public static function observe_all($event) {
$task = new \local_webhooks\task\process_events_task();
- $task->set_custom_data( $event->get_data() );
- \core\task\manager::queue_adhoc_task( $task );
+ $task->set_custom_data($event->get_data());
+ \core\task\manager::queue_adhoc_task($task);
}
} \ No newline at end of file
diff --git a/classes/task/process_events_task.php b/classes/task/process_events_task.php
index 84b5c88..02e54a2 100644
--- a/classes/task/process_events_task.php
+++ b/classes/task/process_events_task.php
@@ -24,7 +24,11 @@
namespace local_webhooks\task;
-defined( "MOODLE_INTERNAL" ) || die();
+global $CFG;
+
+require_once $CFG->dirroot . '/local/webhooks/lib.php';
+
+defined('MOODLE_INTERNAL') || die();
/**
* Class process_events_task
@@ -36,26 +40,25 @@ defined( "MOODLE_INTERNAL" ) || die();
class process_events_task extends \core\task\adhoc_task {
/**
* Task handler.
+ *
+ * @throws \dml_exception
+ * @throws \moodle_exception
*/
public function execute() {
- global $CFG;
-
- require_once( $CFG->dirroot . "/local/webhooks/lib.php" );
-
- $services = \local_webhooks_api::get_services_by_event( $this->get_custom_data()->eventname );
+ $services = \local_webhooks_api::get_services_by_event($this->get_custom_data()->eventname);
- foreach ( $services as $service ) {
- if ( empty( $service->status ) ) {
+ foreach ($services as $service) {
+ if ((bool) $service->status !== true) {
return;
}
$curl = new \curl();
$event = (array) $this->get_custom_data();
- $event[ "token" ] = $service->token;
+ $event['token'] = $service->token;
- $curl->setHeader( array( "Content-Type: " . $service->header ) );
- $curl->post( $service->point, json_encode( $event ) );
+ $curl->setHeader(array('Content-Type: ' . $service->header));
+ $curl->post($service->point, json_encode($event));
// TODO: Mark the log
$curl->getResponse();
diff --git a/classes/ui_forms_plugin.php b/classes/ui_forms_plugin.php
index 2cd7541..6c9a070 100644
--- a/classes/ui_forms_plugin.php
+++ b/classes/ui_forms_plugin.php
@@ -22,10 +22,10 @@
* @package local_webhooks
*/
-defined( "MOODLE_INTERNAL" ) || die();
+defined('MOODLE_INTERNAL') || die();
-require_once( $CFG->dirroot . "/local/webhooks/lib.php" );
-require_once( $CFG->libdir . "/formslib.php" );
+require_once $CFG->dirroot . '/local/webhooks/lib.php';
+require_once $CFG->libdir . '/formslib.php';
/**
* Description editing form definition.
@@ -38,66 +38,68 @@ class local_webhooks_service_edit_form extends moodleform {
/**
* @param string $baseUrl
*/
- public function __construct( $baseUrl ) {
- parent::__construct( $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 );
+ $size = array('size' => 60);
/* Form heading */
- $mForm->addElement( "header", "editserviceheadermain", new lang_string( "service", "webservice" ) );
+ $mForm->addElement('header', 'editserviceheadermain', 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 );
+ $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 );
+ $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" );
+ $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 );
+ $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",
+ 'application/json' => 'application/json',
+ 'application/x-www-form-urlencoded' => 'application/x-www-form-urlencoded',
);
- $mForm->addElement( "select", "header", "Content-Type", $contentType );
- $mForm->setAdvanced( "header" );
+ $mForm->addElement('select', 'header', 'Content-Type', $contentType);
+ $mForm->setAdvanced('header');
/* Form heading */
- $mForm->addElement( "header", "editserviceheaderevent", new lang_string( "edulevel", "moodle" ) );
+ $mForm->addElement('header', 'editserviceheaderevent', new lang_string('edulevel', 'moodle'));
/* List of events */
- $eventList = report_eventlist_list_generator::get_all_events_list( true );
+ $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 ($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 );
+ foreach ($events as $key => $event) {
+ $mForm->addGroup($event, 'events', $key, '<br />', true);
}
/* Control Panel */
- $this->add_action_buttons( true );
+ $this->add_action_buttons(true);
}
-}
+} \ No newline at end of file
diff --git a/classes/ui_tables_plugin.php b/classes/ui_tables_plugin.php
index 7e39598..ecc0c52 100644
--- a/classes/ui_tables_plugin.php
+++ b/classes/ui_tables_plugin.php
@@ -22,10 +22,10 @@
* @package local_webhooks
*/
-defined( "MOODLE_INTERNAL" ) || die();
+defined('MOODLE_INTERNAL') || die();
-require_once( $CFG->dirroot . "/local/webhooks/lib.php" );
-require_once( $CFG->libdir . "/tablelib.php" );
+require_once $CFG->dirroot . '/local/webhooks/lib.php';
+require_once $CFG->libdir . '/tablelib.php';
/**
* Display the list of services table.
@@ -38,20 +38,22 @@ class local_webhooks_services_table extends table_sql {
/**
* @var string $mainPage
*/
- protected static $mainPage = "/local/webhooks/index.php";
+ protected static $mainPage = '/local/webhooks/index.php';
/**
* @var string $editorPage
*/
- protected static $editorPage = "/local/webhooks/service.php";
+ protected static $editorPage = '/local/webhooks/service.php';
/**
* Constructor.
*
* @param string $uniqueId
+ *
+ * @throws \coding_exception
*/
- public function __construct( $uniqueId = "" ) {
- parent::__construct( $uniqueId );
+ public function __construct($uniqueId = '') {
+ parent::__construct($uniqueId);
$this->define_table_columns();
$this->define_table_configs();
}
@@ -59,73 +61,80 @@ class local_webhooks_services_table extends table_sql {
/**
* Query the database for results to display in the table.
*
- * @param number $pageSize
+ * @param int $pageSize
* @param boolean $useInitialsBar
+ *
+ * @throws \dml_exception
*/
- public function query_db( $pageSize = 0, $useInitialsBar = false ) {
- $this->rawdata = local_webhooks_api::get_services( array(), $this->get_page_start(), $this->get_page_size() );
+ public function query_db($pageSize = 0, $useInitialsBar = false) {
+ $this->rawdata = local_webhooks_api::get_services(array(), $this->get_page_start(), $this->get_page_size());
}
/**
* Defines the basic settings of the table.
*/
public function define_table_configs() {
- $this->collapsible( false );
- $this->is_downloadable( false );
- $this->no_sorting( "actions" );
- $this->pageable( true );
+ $this->collapsible(false);
+ $this->is_downloadable(false);
+ $this->no_sorting('actions');
+ $this->pageable(true);
}
/**
* Defines the main columns and table headers.
+ *
+ * @throws \coding_exception
*/
public function define_table_columns() {
$columns = array(
- "name",
- "point",
- "events",
- "actions"
+ 'name',
+ 'point',
+ 'events',
+ 'actions',
);
$headers = array(
- new lang_string( "name", "moodle" ),
- new lang_string( "url", "moodle" ),
- new lang_string( "edulevel", "moodle" ),
- new lang_string( "actions", "moodle" )
+ new lang_string('name', 'moodle'),
+ new lang_string('url', 'moodle'),
+ new lang_string('edulevel', 'moodle'),
+ new lang_string('actions', 'moodle'),
);
- $this->define_columns( $columns );
- $this->define_headers( $headers );
+ $this->define_columns($columns);
+ $this->define_headers($headers);
}
/**
* Specifies the display of a column with actions.
*
* @param object $row
+ *
* @return string
+ * @throws \coding_exception
+ * @throws \moodle_exception
*/
- public function col_actions( $row ) {
+ public function col_actions($row) {
global $OUTPUT;
- $hideShowIcon = "t/show";
- $hideShowString = new lang_string( "enable", "moodle" );
+ $hideShowIcon = 't/show';
+ $hideShowString = new lang_string('enable', 'moodle');
- if ( !empty( $row->status ) ) {
- $hideShowIcon = "t/hide";
- $hideShowString = new lang_string( "disable", "moodle" );
+ if (!empty($row->status)) {
+ $hideShowIcon = 't/hide';
+ $hideShowString = new lang_string('disable', 'moodle');
}
/* Link for activation / deactivation */
- $hideShowLink = new moodle_url( self::$mainPage, array( "hideshowid" => $row->id, "sesskey" => sesskey() ) );
- $hideShowItem = $OUTPUT->action_icon( $hideShowLink, new pix_icon( $hideShowIcon, $hideShowString ) );
+ $hideShowLink = new moodle_url(self::$mainPage, array('hideshowid' => $row->id, 'sesskey' => sesskey()));
+ $hideShowItem = $OUTPUT->action_icon($hideShowLink, new pix_icon($hideShowIcon, $hideShowString));
/* Link for editing */
- $editLink = new moodle_url( self::$editorPage, array( "serviceid" => $row->id, "sesskey" => sesskey() ) );
- $editItem = $OUTPUT->action_icon( $editLink, new pix_icon( "t/edit", new lang_string( "edit", "moodle" ) ) );
+ $editLink = new moodle_url(self::$editorPage, array('serviceid' => $row->id, 'sesskey' => sesskey()));
+ $editItem = $OUTPUT->action_icon($editLink, new pix_icon('t/edit', new lang_string('edit', 'moodle')));
/* Link to remove */
- $deleteLink = new moodle_url( self::$mainPage, array( "deleteid" => $row->id, "sesskey" => sesskey() ) );
- $deleteItem = $OUTPUT->action_icon( $deleteLink, new pix_icon( "t/delete", new lang_string( "delete", "moodle" ) ) );
+ $deleteLink = new moodle_url(self::$mainPage, array('deleteid' => $row->id, 'sesskey' => sesskey()));
+ $deleteItem = $OUTPUT->action_icon($deleteLink, new pix_icon('t/delete', new lang_string('delete', 'moodle')));
return $hideShowItem . $editItem . $deleteItem;
}
@@ -134,20 +143,24 @@ class local_webhooks_services_table extends table_sql {
* Specifies the display of a column with events.
*
* @param object $row
+ *
* @return number
*/
- public function col_events( $row ) {
- return count( $row->events );
+ public function col_events($row) {
+ return count($row->events);
}
/**
* Specifies the display of the column with the service name.
*
* @param object $row
+ *
* @return string
+ * @throws \moodle_exception
*/
- public function col_name( $row ) {
- $link = new moodle_url( self::$editorPage, array( "serviceid" => $row->id, "sesskey" => sesskey() ) );
- return html_writer::link( $link, $row->name );
+ public function col_name($row) {
+ $link = new moodle_url(self::$editorPage, array('serviceid' => $row->id, 'sesskey' => sesskey()));
+
+ return html_writer::link($link, $row->name);
}
} \ No newline at end of file
diff --git a/db/events.php b/db/events.php
index 9fdf7d4..0fc9abb 100644
--- a/db/events.php
+++ b/db/events.php
@@ -22,11 +22,11 @@
* @package local_webhooks
*/
-defined( "MOODLE_INTERNAL" ) || die();
+defined('MOODLE_INTERNAL') || die();
$observers = array(
array(
- "callback" => "\local_webhooks\\event_observer::observe_all",
- "eventname" => "*"
- )
+ 'callback' => '\local_webhooks\event_observer::observe_all',
+ 'eventname' => '*',
+ ),
); \ No newline at end of file
diff --git a/db/services.php b/db/services.php
index 7fa6414..f74a310 100644
--- a/db/services.php
+++ b/db/services.php
@@ -22,6 +22,6 @@
* @package local_webhooks
*/
-defined( "MOODLE_INTERNAL" ) || die();
+defined('MOODLE_INTERNAL') || die();
$functions = array(); \ No newline at end of file
diff --git a/externallib.php b/externallib.php
index 87e77e9..76697d0 100644
--- a/externallib.php
+++ b/externallib.php
@@ -22,9 +22,9 @@
* @package local_webhooks
*/
-defined( "MOODLE_INTERNAL" ) || die();
+defined('MOODLE_INTERNAL') || die();
-require_once( $CFG->libdir . "/externallib.php" );
+require_once $CFG->libdir . '/externallib.php';
/**
* External functions.
@@ -34,4 +34,4 @@ require_once( $CFG->libdir . "/externallib.php" );
* @package local_webhooks
*/
class local_webhooks_external extends external_api {
-}
+} \ No newline at end of file
diff --git a/index.php b/index.php
index 9d42e27..ea52769 100644
--- a/index.php
+++ b/index.php
@@ -22,53 +22,53 @@
* @package local_webhooks
*/
-require_once( __DIR__ . "/../../config.php" );
+require_once __DIR__ . '/../../config.php';
-require_once( $CFG->dirroot . "/local/webhooks/classes/ui_tables_plugin.php" );
-require_once( $CFG->dirroot . "/local/webhooks/lib.php" );
-require_once( $CFG->libdir . "/adminlib.php" );
+require_once $CFG->dirroot . '/local/webhooks/classes/ui_tables_plugin.php';
+require_once $CFG->dirroot . '/local/webhooks/lib.php';
+require_once $CFG->libdir . '/adminlib.php';
-$deleteId = optional_param( "deleteid", 0, PARAM_INT );
-$hideShowId = optional_param( "hideshowid", 0, PARAM_INT );
+$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 );
+$editPage = '/local/webhooks/service.php';
+$mainPage = '/local/webhooks/index.php';
+$baseUrl = new moodle_url($mainPage);
-admin_externalpage_setup( "local_webhooks", "", null, $baseUrl, array() );
+admin_externalpage_setup('local_webhooks', '', null, $baseUrl, array());
$context = context_system::instance();
/* Remove the service */
-if ( !empty( $deleteId ) && confirm_sesskey() ) {
- local_webhooks_api::delete_service( $deleteId );
- redirect( $PAGE->url, new lang_string( "deleted", "moodle" ) );
+if (!empty($deleteId) && confirm_sesskey()) {
+ local_webhooks_api::delete_service($deleteId);
+ redirect($PAGE->url, new lang_string('deleted', 'moodle'));
}
/* Disable / Enable the service */
-if ( !empty( $hideShowId ) && confirm_sesskey() ) {
- $service = local_webhooks_api::get_service( $hideShowId );
+if (!empty($hideShowId) && confirm_sesskey()) {
+ $service = local_webhooks_api::get_service($hideShowId);
$service->status = !(bool) $service->status;
- local_webhooks_api::update_service( (array) $service );
- redirect( $PAGE->url, new lang_string( "changessaved", "moodle" ) );
+ local_webhooks_api::update_service((array) $service);
+ redirect($PAGE->url, new lang_string('changessaved', 'moodle'));
}
/* The page title */
-$titlePage = new lang_string( "pluginname", "local_webhooks" );
-$PAGE->set_heading( $titlePage );
-$PAGE->set_title( $titlePage );
+$titlePage = new lang_string('pluginname', 'local_webhooks');
+$PAGE->set_heading($titlePage);
+$PAGE->set_title($titlePage);
echo $OUTPUT->header();
/* Displays the table */
-$table = new local_webhooks_services_table( "local-webhooks-table" );
-$table->define_baseurl( $baseUrl );
-$table->out( 25, true );
+$table = new local_webhooks_services_table('local-webhooks-table');
+$table->define_baseurl($baseUrl);
+$table->out(25, true);
/* Separation */
-echo html_writer::empty_tag( "br" );
+echo html_writer::empty_tag('br');
/* Adds the add button */
-$addServiceUrl = new moodle_url( $editPage, array( "sesskey" => sesskey() ) );
-echo $OUTPUT->single_button( $addServiceUrl, new lang_string( "add", "moodle" ) );
+$addServiceUrl = new moodle_url($editPage, array('sesskey' => sesskey()));
+echo $OUTPUT->single_button($addServiceUrl, new lang_string('add', 'moodle'));
/* Footer */
echo $OUTPUT->footer(); \ No newline at end of file
diff --git a/lang/en/local_webhooks.php b/lang/en/local_webhooks.php
index 63645cf..1b1f13b 100644
--- a/lang/en/local_webhooks.php
+++ b/lang/en/local_webhooks.php
@@ -22,4 +22,4 @@
* @package local_webhooks
*/
-$string[ "pluginname" ] = "WebHooks"; \ No newline at end of file
+$string['pluginname'] = 'WebHooks'; \ No newline at end of file
diff --git a/lib.php b/lib.php
index 9e26b26..5987033 100644
--- a/lib.php
+++ b/lib.php
@@ -22,10 +22,10 @@
* @package local_webhooks
*/
-defined( "MOODLE_INTERNAL" ) || die();
+defined('MOODLE_INTERNAL') || die();
-define( "LW_TABLE_SERVICES", "local_webhooks_service" );
-define( "LW_TABLE_EVENTS", "local_webhooks_events" );
+define('LW_TABLE_SERVICES', 'local_webhooks_service');
+define('LW_TABLE_EVENTS', 'local_webhooks_events');
/**
* Class local_webhooks_api
@@ -39,20 +39,23 @@ class local_webhooks_api {
* Get information about the service.
*
* @param int $serviceId
+ *
* @return object
+ * @throws \dml_exception
+ * @throws \moodle_exception
*/
- public static function get_service( $serviceId = 0 ) {
+ public static function get_service($serviceId = 0) {
global $DB;
- if ( empty( $serviceId ) || !is_numeric( $serviceId ) ) {
- print_error( "unknowparamtype", "error", null, "serviceId" );
+ if (!is_numeric($serviceId) || $serviceId === 0) {
+ print_error('unknowparamtype', 'error', null, 'serviceId');
}
- $service = $DB->get_record( LW_TABLE_SERVICES, array( "id" => $serviceId ), "*", MUST_EXIST );
- $events = $DB->get_records( LW_TABLE_EVENTS, array( "serviceid" => $serviceId ), "", "*", 0, 0 );
+ $service = $DB->get_record(LW_TABLE_SERVICES, array('id' => $serviceId), '*', MUST_EXIST);
+ $events = $DB->get_records(LW_TABLE_EVENTS, array('serviceid' => $serviceId), '', '*', 0, 0);
$service->events = array();
- foreach ( $events as $event ) {
+ foreach ($events as $event) {
$service->events[] = $event->name;
}
@@ -66,18 +69,20 @@ class local_webhooks_api {
* @param array $conditions
* @param int $limitFrom
* @param int $limitNum
+ *
* @return array
+ * @throws \dml_exception
*/
- public static function get_services( $conditions = array(), $limitFrom = 0, $limitNum = 0 ) {
+ public static function get_services(array $conditions = array(), $limitFrom = 0, $limitNum = 0) {
global $DB;
- $services = $DB->get_records( LW_TABLE_SERVICES, $conditions, "", "*", $limitFrom, $limitNum );
+ $services = $DB->get_records(LW_TABLE_SERVICES, $conditions, '', '*', $limitFrom, $limitNum);
- foreach ( $services as $service ) {
- $events = $DB->get_records( LW_TABLE_EVENTS, array( "serviceid" => $service->id ), "", "*", 0, 0 );
+ foreach ($services as $service) {
+ $events = $DB->get_records(LW_TABLE_EVENTS, array('serviceid' => $service->id), '', '*', 0, 0);
$service->events = array();
- foreach ( $events as $event ) {
+ foreach ($events as $event) {
$service->events[] = $event->name;
}
}
@@ -89,20 +94,23 @@ class local_webhooks_api {
* Get the list of services subscribed to the event.
*
* @param string $eventName
+ *
* @return array
+ * @throws \dml_exception
+ * @throws \moodle_exception
*/
- public static function get_services_by_event( $eventName = "" ) {
+ public static function get_services_by_event($eventName = '') {
global $DB;
- if ( empty( $eventName ) || !is_string( $eventName ) ) {
- print_error( "unknowparamtype", "error", null, "eventName" );
+ if (!is_string($eventName) || $eventName === '') {
+ print_error('unknowparamtype', 'error', null, 'eventName');
}
- $events = $DB->get_records( LW_TABLE_EVENTS, array( "name" => $eventName ), "", "*", 0, 0 );
+ $events = $DB->get_records(LW_TABLE_EVENTS, array('name' => $eventName), '', '*', 0, 0);
$services = array();
- foreach ( $events as $event ) {
- $services[] = local_webhooks_api::get_service( $event->serviceid );
+ foreach ($events as $event) {
+ $services[] = self::get_service($event->serviceid);
}
return $services;
@@ -112,18 +120,21 @@ class local_webhooks_api {
* Create service data in the database.
*
* @param array $service
+ *
* @return int
+ * @throws \dml_exception
+ * @throws \moodle_exception
*/
- public static function create_service( $service = array() ) {
+ public static function create_service(array $service = array()) {
global $DB;
- if ( empty( $service ) || !is_array( $service ) ) {
- print_error( "unknowparamtype", "error", null, "service" );
+ if (!is_array($service) || count($service) === 0) {
+ print_error('unknowparamtype', 'error', null, 'service');
}
- $serviceId = $DB->insert_record( LW_TABLE_SERVICES, $service, true, false );
- if ( $serviceId && !empty( $service[ "events" ] ) && is_array( $service[ "events" ] ) ) {
- self::insert_events( $service[ "events" ], $serviceId );
+ $serviceId = $DB->insert_record(LW_TABLE_SERVICES, (object) $service, true, false);
+ if ($serviceId && !empty($service['events']) && is_array($service['events'])) {
+ self::insert_events($service['events'], $serviceId);
}
// TODO: Mark the log
@@ -135,39 +146,47 @@ class local_webhooks_api {
* Delete the service data from the database.
*
* @param int $serviceId
+ *
* @return bool
+ * @throws \dml_exception
+ * @throws \moodle_exception
*/
- public static function delete_service( $serviceId = 0 ) {
+ public static function delete_service($serviceId = 0) {
global $DB;
- if ( empty( $serviceId ) || !is_numeric( $serviceId ) ) {
- print_error( "unknowparamtype", "error", null, "serviceId" );
+ if (!is_numeric($serviceId) || $serviceId === 0) {
+ print_error('unknowparamtype', 'error', null, 'serviceId');
}
// TODO: Mark the log
- $DB->delete_records( LW_TABLE_EVENTS, array( "serviceid" => $serviceId ) );
- return $DB->delete_records( LW_TABLE_SERVICES, array( "id" => $serviceId ) );
+ $DB->delete_records(LW_TABLE_EVENTS, array('serviceid' => $serviceId));
+
+ return $DB->delete_records(LW_TABLE_SERVICES, array('id' => $serviceId));
}
/**
* Update the service data in the database.
*
* @param array $service
+ *
* @return bool
+ * @throws \dml_exception
+ * @throws \moodle_exception
*/
- public static function update_service( $service = array() ) {
+ public static function update_service(array $service = array()) {
global $DB;
- if ( empty( $service ) || !is_array( $service ) || empty( $service[ "id" ] ) ) {
- print_error( "unknowparamtype", "error", null, "service" );
+ if (!is_array($service) || count($service) === 0 || !isset($service['id'])) {
+ print_error('unknowparamtype', 'error', null, 'service');
}
// TODO: Add transactions for operations
- $result = $DB->update_record( LW_TABLE_SERVICES, $service, false );
- $DB->delete_records( LW_TABLE_EVENTS, array( "serviceid" => $service[ "id" ] ) );
- if ( $result && !empty( $service[ "events" ] ) && is_array( $service[ "events" ] ) ) {
- self::insert_events( $service[ "events" ], $service[ "id" ] );
+ $result = $DB->update_record(LW_TABLE_SERVICES, (object) $service, false);
+ $DB->delete_records(LW_TABLE_EVENTS, array('serviceid' => $service['id']));
+
+ if ($result && is_array($service['events']) && count($service) !== 0) {
+ self::insert_events($service['events'], $service['id']);
}
// TODO: Mark the log
@@ -180,15 +199,18 @@ class local_webhooks_api {
*
* @param array $events
* @param int $serviceId
+ *
+ * @throws \coding_exception
+ * @throws \dml_exception
*/
- protected static function insert_events( $events = array(), $serviceId = 0 ) {
+ protected static function insert_events(array $events = array(), $serviceId = 0) {
global $DB;
$conditions = array();
- foreach ( $events as $eventName ) {
- $conditions[] = array( "name" => $eventName, "serviceid" => $serviceId );
+ foreach ($events as $eventName) {
+ $conditions[] = array('name' => $eventName, 'serviceid' => $serviceId);
}
- $DB->insert_records( LW_TABLE_EVENTS, $conditions );
+ $DB->insert_records(LW_TABLE_EVENTS, $conditions);
}
-}
+} \ No newline at end of file
diff --git a/service.php b/service.php
index 093936f..21f9827 100644
--- a/service.php
+++ b/service.php
@@ -22,57 +22,57 @@
* @package local_webhooks
*/
-require_once( __DIR__ . "/../../config.php" );
+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" );
+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 );
+$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" );
+$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() );
+admin_externalpage_setup('local_webhooks', '', null, $baseUrl, array());
$context = context_system::instance();
-$mForm = new local_webhooks_service_edit_form( $PAGE->url );
+$mForm = new local_webhooks_service_edit_form($PAGE->url);
$formData = (array) $mForm->get_data();
/* Cancel */
-if ( $mForm->is_cancelled() ) {
- redirect( $mainPage );
+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($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 );
+ if (!empty($serviceId)) {
+ $formData['id'] = $serviceId;
+ local_webhooks_api::update_service($formData);
} else {
- local_webhooks_api::create_service( $formData );
+ local_webhooks_api::create_service($formData);
}
- redirect( $mainPage, new lang_string( "changessaved", "moodle" ) );
+ 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 );
+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 );
+$titlePage = new lang_string('externalservice', 'webservice');
+$PAGE->navbar->add($titlePage);
+$PAGE->set_heading($titlePage);
+$PAGE->set_title($titlePage);
echo $OUTPUT->header();
/* Displays the form */
diff --git a/settings.php b/settings.php
index f67daa5..e69898d 100644
--- a/settings.php
+++ b/settings.php
@@ -22,12 +22,16 @@
* @package local_webhooks
*/
-defined( "MOODLE_INTERNAL" ) || die();
+defined('MOODLE_INTERNAL') || die();
-if ( $hassiteconfig ) {
- $ADMIN->add( "server", new admin_externalpage( "local_webhooks",
- new lang_string( "pluginname", "local_webhooks" ),
- new moodle_url( "/local/webhooks/index.php" )
+/* @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')
)
);
} \ No newline at end of file
diff --git a/version.php b/version.php
index 188422d..f3f079e 100644
--- a/version.php
+++ b/version.php
@@ -22,11 +22,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-defined("MOODLE_INTERNAL") || die();
+defined('MOODLE_INTERNAL') || die();
-$plugin->release = "4.0.0-rc.2 (Build: 2018022500)";
-$plugin->version = 2018022500;
-$plugin->requires = 2016112900;
-$plugin->component = "local_webhooks";
-$plugin->maturity = MATURITY_RC;
-$plugin->dependencies = array("report_eventlist" => 2016120500); \ No newline at end of file
+$plugin->release = '4.0.0-rc.2 (Build: 2018022500)';
+$plugin->version = 2018022500;
+$plugin->requires = 2016112900;
+$plugin->component = 'local_webhooks';
+$plugin->maturity = MATURITY_RC;
+$plugin->dependencies = array('report_eventlist' => 2016120500); \ No newline at end of file