Code style fix

Signed-off-by: Valentin Popov <info@valentineus.link>
This commit is contained in:
Valentin Popov 2018-10-22 02:17:12 +04:00
parent f3b5ab5616
commit 1352b5ee79
Signed by: Valentin Popov
GPG Key ID: 269A00ACA90A8EA3
13 changed files with 250 additions and 206 deletions

View File

@ -24,7 +24,7 @@
namespace local_webhooks; namespace local_webhooks;
defined( "MOODLE_INTERNAL" ) || die(); defined('MOODLE_INTERNAL') || die();
/** /**
* Defines event handlers. * Defines event handlers.
@ -40,9 +40,9 @@ class event_observer {
* *
* @param object $event * @param object $event
*/ */
public static function observe_all( $event ) { public static function observe_all($event) {
$task = new \local_webhooks\task\process_events_task(); $task = new \local_webhooks\task\process_events_task();
$task->set_custom_data( $event->get_data() ); $task->set_custom_data($event->get_data());
\core\task\manager::queue_adhoc_task( $task ); \core\task\manager::queue_adhoc_task($task);
} }
} }

View File

@ -24,7 +24,11 @@
namespace local_webhooks\task; 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 * Class process_events_task
@ -36,26 +40,25 @@ defined( "MOODLE_INTERNAL" ) || die();
class process_events_task extends \core\task\adhoc_task { class process_events_task extends \core\task\adhoc_task {
/** /**
* Task handler. * Task handler.
*
* @throws \dml_exception
* @throws \moodle_exception
*/ */
public function execute() { public function execute() {
global $CFG; $services = \local_webhooks_api::get_services_by_event($this->get_custom_data()->eventname);
require_once( $CFG->dirroot . "/local/webhooks/lib.php" ); foreach ($services as $service) {
if ((bool) $service->status !== true) {
$services = \local_webhooks_api::get_services_by_event( $this->get_custom_data()->eventname );
foreach ( $services as $service ) {
if ( empty( $service->status ) ) {
return; return;
} }
$curl = new \curl(); $curl = new \curl();
$event = (array) $this->get_custom_data(); $event = (array) $this->get_custom_data();
$event[ "token" ] = $service->token; $event['token'] = $service->token;
$curl->setHeader( array( "Content-Type: " . $service->header ) ); $curl->setHeader(array('Content-Type: ' . $service->header));
$curl->post( $service->point, json_encode( $event ) ); $curl->post($service->point, json_encode($event));
// TODO: Mark the log // TODO: Mark the log
$curl->getResponse(); $curl->getResponse();

View File

@ -22,10 +22,10 @@
* @package local_webhooks * @package local_webhooks
*/ */
defined( "MOODLE_INTERNAL" ) || die(); defined('MOODLE_INTERNAL') || die();
require_once( $CFG->dirroot . "/local/webhooks/lib.php" ); require_once $CFG->dirroot . '/local/webhooks/lib.php';
require_once( $CFG->libdir . "/formslib.php" ); require_once $CFG->libdir . '/formslib.php';
/** /**
* Description editing form definition. * Description editing form definition.
@ -38,66 +38,68 @@ class local_webhooks_service_edit_form extends moodleform {
/** /**
* @param string $baseUrl * @param string $baseUrl
*/ */
public function __construct( $baseUrl ) { public function __construct($baseUrl) {
parent::__construct( $baseUrl ); parent::__construct($baseUrl);
} }
/** /**
* Defines the standard structure of the form. * Defines the standard structure of the form.
*
* @throws \coding_exception
*/ */
protected function definition() { protected function definition() {
$mForm =& $this->_form; $mForm =& $this->_form;
$size = array( "size" => 60 ); $size = array('size' => 60);
/* Form heading */ /* Form heading */
$mForm->addElement( "header", "editserviceheadermain", new lang_string( "service", "webservice" ) ); $mForm->addElement('header', 'editserviceheadermain', new lang_string('service', 'webservice'));
/* Name of the service */ /* Name of the service */
$mForm->addElement( "text", "name", new lang_string( "name", "moodle" ), $size ); $mForm->addElement('text', 'name', new lang_string('name', 'moodle'), $size);
$mForm->addRule( "name", null, "required" ); $mForm->addRule('name', null, 'required');
$mForm->setType( "name", PARAM_RAW ); $mForm->setType('name', PARAM_RAW);
/* Callback address */ /* Callback address */
$mForm->addElement( "text", "point", new lang_string( "url", "moodle" ), $size ); $mForm->addElement('text', 'point', new lang_string('url', 'moodle'), $size);
$mForm->addRule( "point", null, "required" ); $mForm->addRule('point', null, 'required');
$mForm->setType( "point", PARAM_URL ); $mForm->setType('point', PARAM_URL);
/* Enabling the service */ /* Enabling the service */
$mForm->addElement( "advcheckbox", "status", new lang_string( "enable", "moodle" ) ); $mForm->addElement('advcheckbox', 'status', new lang_string('enable', 'moodle'));
$mForm->setType( "status", PARAM_BOOL ); $mForm->setType('status', PARAM_BOOL);
$mForm->setDefault( "status", 1 ); $mForm->setDefault('status', 1);
$mForm->setAdvanced( "status" ); $mForm->setAdvanced('status');
/* Token */ /* Token */
$mForm->addElement( "text", "token", new lang_string( "token", "webservice" ), $size ); $mForm->addElement('text', 'token', new lang_string('token', 'webservice'), $size);
$mForm->addRule( "token", null, "required" ); $mForm->addRule('token', null, 'required');
$mForm->setType( "token", PARAM_RAW ); $mForm->setType('token', PARAM_RAW);
/* Content type */ /* Content type */
$contentType = array( $contentType = array(
"application/json" => "application/json", 'application/json' => 'application/json',
"application/x-www-form-urlencoded" => "application/x-www-form-urlencoded", 'application/x-www-form-urlencoded' => 'application/x-www-form-urlencoded',
); );
$mForm->addElement( "select", "header", "Content-Type", $contentType ); $mForm->addElement('select', 'header', 'Content-Type', $contentType);
$mForm->setAdvanced( "header" ); $mForm->setAdvanced('header');
/* Form heading */ /* Form heading */
$mForm->addElement( "header", "editserviceheaderevent", new lang_string( "edulevel", "moodle" ) ); $mForm->addElement('header', 'editserviceheaderevent', new lang_string('edulevel', 'moodle'));
/* List of events */ /* 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(); $events = array();
foreach ( $eventList as $event ) { foreach ($eventList as $event) {
$events[ $event[ "component" ] ][] =& $mForm->createElement( "checkbox", $event[ "eventname" ], $event[ "eventname" ] ); $events[$event['component']][] =& $mForm->createElement('checkbox', $event['eventname'], $event['eventname']);
} }
foreach ( $events as $key => $event ) { foreach ($events as $key => $event) {
$mForm->addGroup( $event, "events", $key, "<br />", true ); $mForm->addGroup($event, 'events', $key, '<br />', true);
} }
/* Control Panel */ /* Control Panel */
$this->add_action_buttons( true ); $this->add_action_buttons(true);
} }
} }

View File

@ -22,10 +22,10 @@
* @package local_webhooks * @package local_webhooks
*/ */
defined( "MOODLE_INTERNAL" ) || die(); defined('MOODLE_INTERNAL') || die();
require_once( $CFG->dirroot . "/local/webhooks/lib.php" ); require_once $CFG->dirroot . '/local/webhooks/lib.php';
require_once( $CFG->libdir . "/tablelib.php" ); require_once $CFG->libdir . '/tablelib.php';
/** /**
* Display the list of services table. * Display the list of services table.
@ -38,20 +38,22 @@ class local_webhooks_services_table extends table_sql {
/** /**
* @var string $mainPage * @var string $mainPage
*/ */
protected static $mainPage = "/local/webhooks/index.php"; protected static $mainPage = '/local/webhooks/index.php';
/** /**
* @var string $editorPage * @var string $editorPage
*/ */
protected static $editorPage = "/local/webhooks/service.php"; protected static $editorPage = '/local/webhooks/service.php';
/** /**
* Constructor. * Constructor.
* *
* @param string $uniqueId * @param string $uniqueId
*
* @throws \coding_exception
*/ */
public function __construct( $uniqueId = "" ) { public function __construct($uniqueId = '') {
parent::__construct( $uniqueId ); parent::__construct($uniqueId);
$this->define_table_columns(); $this->define_table_columns();
$this->define_table_configs(); $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. * Query the database for results to display in the table.
* *
* @param number $pageSize * @param int $pageSize
* @param boolean $useInitialsBar * @param boolean $useInitialsBar
*
* @throws \dml_exception
*/ */
public function query_db( $pageSize = 0, $useInitialsBar = false ) { public function query_db($pageSize = 0, $useInitialsBar = false) {
$this->rawdata = local_webhooks_api::get_services( array(), $this->get_page_start(), $this->get_page_size() ); $this->rawdata = local_webhooks_api::get_services(array(), $this->get_page_start(), $this->get_page_size());
} }
/** /**
* Defines the basic settings of the table. * Defines the basic settings of the table.
*/ */
public function define_table_configs() { public function define_table_configs() {
$this->collapsible( false ); $this->collapsible(false);
$this->is_downloadable( false ); $this->is_downloadable(false);
$this->no_sorting( "actions" ); $this->no_sorting('actions');
$this->pageable( true ); $this->pageable(true);
} }
/** /**
* Defines the main columns and table headers. * Defines the main columns and table headers.
*
* @throws \coding_exception
*/ */
public function define_table_columns() { public function define_table_columns() {
$columns = array( $columns = array(
"name", 'name',
"point", 'point',
"events", 'events',
"actions" 'actions',
); );
$headers = array( $headers = array(
new lang_string( "name", "moodle" ), new lang_string('name', 'moodle'),
new lang_string( "url", "moodle" ), new lang_string('url', 'moodle'),
new lang_string( "edulevel", "moodle" ), new lang_string('edulevel', 'moodle'),
new lang_string( "actions", "moodle" ) new lang_string('actions', 'moodle'),
); );
$this->define_columns( $columns ); $this->define_columns($columns);
$this->define_headers( $headers ); $this->define_headers($headers);
} }
/** /**
* Specifies the display of a column with actions. * Specifies the display of a column with actions.
* *
* @param object $row * @param object $row
*
* @return string * @return string
* @throws \coding_exception
* @throws \moodle_exception
*/ */
public function col_actions( $row ) { public function col_actions($row) {
global $OUTPUT; global $OUTPUT;
$hideShowIcon = "t/show"; $hideShowIcon = 't/show';
$hideShowString = new lang_string( "enable", "moodle" ); $hideShowString = new lang_string('enable', 'moodle');
if ( !empty( $row->status ) ) { if (!empty($row->status)) {
$hideShowIcon = "t/hide"; $hideShowIcon = 't/hide';
$hideShowString = new lang_string( "disable", "moodle" ); $hideShowString = new lang_string('disable', 'moodle');
} }
/* Link for activation / deactivation */ /* Link for activation / deactivation */
$hideShowLink = new moodle_url( self::$mainPage, array( "hideshowid" => $row->id, "sesskey" => sesskey() ) ); $hideShowLink = new moodle_url(self::$mainPage, array('hideshowid' => $row->id, 'sesskey' => sesskey()));
$hideShowItem = $OUTPUT->action_icon( $hideShowLink, new pix_icon( $hideShowIcon, $hideShowString ) ); $hideShowItem = $OUTPUT->action_icon($hideShowLink, new pix_icon($hideShowIcon, $hideShowString));
/* Link for editing */ /* Link for editing */
$editLink = new moodle_url( self::$editorPage, array( "serviceid" => $row->id, "sesskey" => sesskey() ) ); $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" ) ) ); $editItem = $OUTPUT->action_icon($editLink, new pix_icon('t/edit', new lang_string('edit', 'moodle')));
/* Link to remove */ /* Link to remove */
$deleteLink = new moodle_url( self::$mainPage, array( "deleteid" => $row->id, "sesskey" => sesskey() ) ); $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" ) ) ); $deleteItem = $OUTPUT->action_icon($deleteLink, new pix_icon('t/delete', new lang_string('delete', 'moodle')));
return $hideShowItem . $editItem . $deleteItem; return $hideShowItem . $editItem . $deleteItem;
} }
@ -134,20 +143,24 @@ class local_webhooks_services_table extends table_sql {
* Specifies the display of a column with events. * Specifies the display of a column with events.
* *
* @param object $row * @param object $row
*
* @return number * @return number
*/ */
public function col_events( $row ) { public function col_events($row) {
return count( $row->events ); return count($row->events);
} }
/** /**
* Specifies the display of the column with the service name. * Specifies the display of the column with the service name.
* *
* @param object $row * @param object $row
*
* @return string * @return string
* @throws \moodle_exception
*/ */
public function col_name( $row ) { public function col_name($row) {
$link = new moodle_url( self::$editorPage, array( "serviceid" => $row->id, "sesskey" => sesskey() ) ); $link = new moodle_url(self::$editorPage, array('serviceid' => $row->id, 'sesskey' => sesskey()));
return html_writer::link( $link, $row->name );
return html_writer::link($link, $row->name);
} }
} }

View File

@ -22,11 +22,11 @@
* @package local_webhooks * @package local_webhooks
*/ */
defined( "MOODLE_INTERNAL" ) || die(); defined('MOODLE_INTERNAL') || die();
$observers = array( $observers = array(
array( array(
"callback" => "\local_webhooks\\event_observer::observe_all", 'callback' => '\local_webhooks\event_observer::observe_all',
"eventname" => "*" 'eventname' => '*',
) ),
); );

View File

@ -22,6 +22,6 @@
* @package local_webhooks * @package local_webhooks
*/ */
defined( "MOODLE_INTERNAL" ) || die(); defined('MOODLE_INTERNAL') || die();
$functions = array(); $functions = array();

View File

@ -22,9 +22,9 @@
* @package local_webhooks * @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. * External functions.

View File

@ -22,53 +22,53 @@
* @package local_webhooks * @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/classes/ui_tables_plugin.php';
require_once( $CFG->dirroot . "/local/webhooks/lib.php" ); require_once $CFG->dirroot . '/local/webhooks/lib.php';
require_once( $CFG->libdir . "/adminlib.php" ); require_once $CFG->libdir . '/adminlib.php';
$deleteId = optional_param( "deleteid", 0, PARAM_INT ); $deleteId = optional_param('deleteid', 0, PARAM_INT);
$hideShowId = optional_param( "hideshowid", 0, PARAM_INT ); $hideShowId = optional_param('hideshowid', 0, PARAM_INT);
$editPage = "/local/webhooks/service.php"; $editPage = '/local/webhooks/service.php';
$mainPage = "/local/webhooks/index.php"; $mainPage = '/local/webhooks/index.php';
$baseUrl = new moodle_url( $mainPage ); $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(); $context = context_system::instance();
/* Remove the service */ /* Remove the service */
if ( !empty( $deleteId ) && confirm_sesskey() ) { if (!empty($deleteId) && confirm_sesskey()) {
local_webhooks_api::delete_service( $deleteId ); local_webhooks_api::delete_service($deleteId);
redirect( $PAGE->url, new lang_string( "deleted", "moodle" ) ); redirect($PAGE->url, new lang_string('deleted', 'moodle'));
} }
/* Disable / Enable the service */ /* Disable / Enable the service */
if ( !empty( $hideShowId ) && confirm_sesskey() ) { if (!empty($hideShowId) && confirm_sesskey()) {
$service = local_webhooks_api::get_service( $hideShowId ); $service = local_webhooks_api::get_service($hideShowId);
$service->status = !(bool) $service->status; $service->status = !(bool) $service->status;
local_webhooks_api::update_service( (array) $service ); local_webhooks_api::update_service((array) $service);
redirect( $PAGE->url, new lang_string( "changessaved", "moodle" ) ); redirect($PAGE->url, new lang_string('changessaved', 'moodle'));
} }
/* The page title */ /* The page title */
$titlePage = new lang_string( "pluginname", "local_webhooks" ); $titlePage = new lang_string('pluginname', 'local_webhooks');
$PAGE->set_heading( $titlePage ); $PAGE->set_heading($titlePage);
$PAGE->set_title( $titlePage ); $PAGE->set_title($titlePage);
echo $OUTPUT->header(); echo $OUTPUT->header();
/* Displays the table */ /* Displays the table */
$table = new local_webhooks_services_table( "local-webhooks-table" ); $table = new local_webhooks_services_table('local-webhooks-table');
$table->define_baseurl( $baseUrl ); $table->define_baseurl($baseUrl);
$table->out( 25, true ); $table->out(25, true);
/* Separation */ /* Separation */
echo html_writer::empty_tag( "br" ); echo html_writer::empty_tag('br');
/* Adds the add button */ /* Adds the add button */
$addServiceUrl = new moodle_url( $editPage, array( "sesskey" => sesskey() ) ); $addServiceUrl = new moodle_url($editPage, array('sesskey' => sesskey()));
echo $OUTPUT->single_button( $addServiceUrl, new lang_string( "add", "moodle" ) ); echo $OUTPUT->single_button($addServiceUrl, new lang_string('add', 'moodle'));
/* Footer */ /* Footer */
echo $OUTPUT->footer(); echo $OUTPUT->footer();

View File

@ -22,4 +22,4 @@
* @package local_webhooks * @package local_webhooks
*/ */
$string[ "pluginname" ] = "WebHooks"; $string['pluginname'] = 'WebHooks';

106
lib.php
View File

@ -22,10 +22,10 @@
* @package local_webhooks * @package local_webhooks
*/ */
defined( "MOODLE_INTERNAL" ) || die(); defined('MOODLE_INTERNAL') || die();
define( "LW_TABLE_SERVICES", "local_webhooks_service" ); define('LW_TABLE_SERVICES', 'local_webhooks_service');
define( "LW_TABLE_EVENTS", "local_webhooks_events" ); define('LW_TABLE_EVENTS', 'local_webhooks_events');
/** /**
* Class local_webhooks_api * Class local_webhooks_api
@ -39,20 +39,23 @@ class local_webhooks_api {
* Get information about the service. * Get information about the service.
* *
* @param int $serviceId * @param int $serviceId
*
* @return object * @return object
* @throws \dml_exception
* @throws \moodle_exception
*/ */
public static function get_service( $serviceId = 0 ) { public static function get_service($serviceId = 0) {
global $DB; global $DB;
if ( empty( $serviceId ) || !is_numeric( $serviceId ) ) { if (!is_numeric($serviceId) || $serviceId === 0) {
print_error( "unknowparamtype", "error", null, "serviceId" ); print_error('unknowparamtype', 'error', null, 'serviceId');
} }
$service = $DB->get_record( LW_TABLE_SERVICES, array( "id" => $serviceId ), "*", MUST_EXIST ); $service = $DB->get_record(LW_TABLE_SERVICES, array('id' => $serviceId), '*', MUST_EXIST);
$events = $DB->get_records( LW_TABLE_EVENTS, array( "serviceid" => $serviceId ), "", "*", 0, 0 ); $events = $DB->get_records(LW_TABLE_EVENTS, array('serviceid' => $serviceId), '', '*', 0, 0);
$service->events = array(); $service->events = array();
foreach ( $events as $event ) { foreach ($events as $event) {
$service->events[] = $event->name; $service->events[] = $event->name;
} }
@ -66,18 +69,20 @@ class local_webhooks_api {
* @param array $conditions * @param array $conditions
* @param int $limitFrom * @param int $limitFrom
* @param int $limitNum * @param int $limitNum
*
* @return array * @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; 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 ) { foreach ($services as $service) {
$events = $DB->get_records( LW_TABLE_EVENTS, array( "serviceid" => $service->id ), "", "*", 0, 0 ); $events = $DB->get_records(LW_TABLE_EVENTS, array('serviceid' => $service->id), '', '*', 0, 0);
$service->events = array(); $service->events = array();
foreach ( $events as $event ) { foreach ($events as $event) {
$service->events[] = $event->name; $service->events[] = $event->name;
} }
} }
@ -89,20 +94,23 @@ class local_webhooks_api {
* Get the list of services subscribed to the event. * Get the list of services subscribed to the event.
* *
* @param string $eventName * @param string $eventName
*
* @return array * @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; global $DB;
if ( empty( $eventName ) || !is_string( $eventName ) ) { if (!is_string($eventName) || $eventName === '') {
print_error( "unknowparamtype", "error", null, "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(); $services = array();
foreach ( $events as $event ) { foreach ($events as $event) {
$services[] = local_webhooks_api::get_service( $event->serviceid ); $services[] = self::get_service($event->serviceid);
} }
return $services; return $services;
@ -112,18 +120,21 @@ class local_webhooks_api {
* Create service data in the database. * Create service data in the database.
* *
* @param array $service * @param array $service
*
* @return int * @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; global $DB;
if ( empty( $service ) || !is_array( $service ) ) { if (!is_array($service) || count($service) === 0) {
print_error( "unknowparamtype", "error", null, "service" ); print_error('unknowparamtype', 'error', null, 'service');
} }
$serviceId = $DB->insert_record( LW_TABLE_SERVICES, $service, true, false ); $serviceId = $DB->insert_record(LW_TABLE_SERVICES, (object) $service, true, false);
if ( $serviceId && !empty( $service[ "events" ] ) && is_array( $service[ "events" ] ) ) { if ($serviceId && !empty($service['events']) && is_array($service['events'])) {
self::insert_events( $service[ "events" ], $serviceId ); self::insert_events($service['events'], $serviceId);
} }
// TODO: Mark the log // TODO: Mark the log
@ -135,39 +146,47 @@ class local_webhooks_api {
* Delete the service data from the database. * Delete the service data from the database.
* *
* @param int $serviceId * @param int $serviceId
*
* @return bool * @return bool
* @throws \dml_exception
* @throws \moodle_exception
*/ */
public static function delete_service( $serviceId = 0 ) { public static function delete_service($serviceId = 0) {
global $DB; global $DB;
if ( empty( $serviceId ) || !is_numeric( $serviceId ) ) { if (!is_numeric($serviceId) || $serviceId === 0) {
print_error( "unknowparamtype", "error", null, "serviceId" ); print_error('unknowparamtype', 'error', null, 'serviceId');
} }
// TODO: Mark the log // TODO: Mark the log
$DB->delete_records( LW_TABLE_EVENTS, array( "serviceid" => $serviceId ) ); $DB->delete_records(LW_TABLE_EVENTS, array('serviceid' => $serviceId));
return $DB->delete_records( LW_TABLE_SERVICES, array( "id" => $serviceId ) );
return $DB->delete_records(LW_TABLE_SERVICES, array('id' => $serviceId));
} }
/** /**
* Update the service data in the database. * Update the service data in the database.
* *
* @param array $service * @param array $service
*
* @return bool * @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; global $DB;
if ( empty( $service ) || !is_array( $service ) || empty( $service[ "id" ] ) ) { if (!is_array($service) || count($service) === 0 || !isset($service['id'])) {
print_error( "unknowparamtype", "error", null, "service" ); print_error('unknowparamtype', 'error', null, 'service');
} }
// TODO: Add transactions for operations // TODO: Add transactions for operations
$result = $DB->update_record( LW_TABLE_SERVICES, $service, false ); $result = $DB->update_record(LW_TABLE_SERVICES, (object) $service, false);
$DB->delete_records( LW_TABLE_EVENTS, array( "serviceid" => $service[ "id" ] ) ); $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" ] ); if ($result && is_array($service['events']) && count($service) !== 0) {
self::insert_events($service['events'], $service['id']);
} }
// TODO: Mark the log // TODO: Mark the log
@ -180,15 +199,18 @@ class local_webhooks_api {
* *
* @param array $events * @param array $events
* @param int $serviceId * @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; global $DB;
$conditions = array(); $conditions = array();
foreach ( $events as $eventName ) { foreach ($events as $eventName) {
$conditions[] = array( "name" => $eventName, "serviceid" => $serviceId ); $conditions[] = array('name' => $eventName, 'serviceid' => $serviceId);
} }
$DB->insert_records( LW_TABLE_EVENTS, $conditions ); $DB->insert_records(LW_TABLE_EVENTS, $conditions);
} }
} }

View File

@ -22,57 +22,57 @@
* @package local_webhooks * @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/classes/ui_forms_plugin.php';
require_once( $CFG->dirroot . "/local/webhooks/lib.php" ); require_once $CFG->dirroot . '/local/webhooks/lib.php';
require_once( $CFG->libdir . "/adminlib.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 ); $urlParameters = array('serviceid' => $serviceId);
$baseUrl = new moodle_url( "/local/webhooks/service.php", $urlParameters ); $baseUrl = new moodle_url('/local/webhooks/service.php', $urlParameters);
$mainPage = new moodle_url( "/local/webhooks/index.php" ); $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(); $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(); $formData = (array) $mForm->get_data();
/* Cancel */ /* Cancel */
if ( $mForm->is_cancelled() ) { if ($mForm->is_cancelled()) {
redirect( $mainPage ); redirect($mainPage);
} }
/* Updating the data */ /* Updating the data */
if ( !empty( $formData ) && confirm_sesskey() ) { if (!empty($formData) && confirm_sesskey()) {
if ( isset( $formData[ "events" ] ) ) { if (isset($formData['events'])) {
$formData[ "events" ] = array_keys( $formData[ "events" ] ); $formData['events'] = array_keys($formData['events']);
} }
if ( !empty( $serviceId ) ) { if (!empty($serviceId)) {
$formData[ "id" ] = $serviceId; $formData['id'] = $serviceId;
local_webhooks_api::update_service( $formData ); local_webhooks_api::update_service($formData);
} else { } 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 */ /* Loading service data */
if ( !empty( $serviceId ) ) { if (!empty($serviceId)) {
$service = local_webhooks_api::get_service( $serviceId ); $service = local_webhooks_api::get_service($serviceId);
$service->events = array_fill_keys( $service->events, 1 ); $service->events = array_fill_keys($service->events, 1);
$mForm->set_data( $service ); $mForm->set_data($service);
} }
/* The page title */ /* The page title */
$titlePage = new lang_string( "externalservice", "webservice" ); $titlePage = new lang_string('externalservice', 'webservice');
$PAGE->navbar->add( $titlePage ); $PAGE->navbar->add($titlePage);
$PAGE->set_heading( $titlePage ); $PAGE->set_heading($titlePage);
$PAGE->set_title( $titlePage ); $PAGE->set_title($titlePage);
echo $OUTPUT->header(); echo $OUTPUT->header();
/* Displays the form */ /* Displays the form */

View File

@ -22,12 +22,16 @@
* @package local_webhooks * @package local_webhooks
*/ */
defined( "MOODLE_INTERNAL" ) || die(); defined('MOODLE_INTERNAL') || die();
if ( $hassiteconfig ) { /* @var \admin_root $ADMIN */
$ADMIN->add( "server", new admin_externalpage( "local_webhooks",
new lang_string( "pluginname", "local_webhooks" ), if ($hassiteconfig) {
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')
) )
); );
} }

View File

@ -22,11 +22,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @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->release = '4.0.0-rc.2 (Build: 2018022500)';
$plugin->version = 2018022500; $plugin->version = 2018022500;
$plugin->requires = 2016112900; $plugin->requires = 2016112900;
$plugin->component = "local_webhooks"; $plugin->component = 'local_webhooks';
$plugin->maturity = MATURITY_RC; $plugin->maturity = MATURITY_RC;
$plugin->dependencies = array("report_eventlist" => 2016120500); $plugin->dependencies = array('report_eventlist' => 2016120500);