@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
        $services = \local_webhooks_api::get_services_by_event($this->get_custom_data()->eventname);
 | 
			
		||||
 | 
			
		||||
        require_once( $CFG->dirroot . "/local/webhooks/lib.php" );
 | 
			
		||||
 | 
			
		||||
        $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();
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user