valentineus / moodle-webhooks

Webhooks allow applications to subscribe to Moodle events.
https://moodle.org/plugins/local_webhooks
GNU General Public License v3.0
17 stars 14 forks source link

checked events list not saved #11

Closed HobieCat closed 6 years ago

HobieCat commented 6 years ago

Hello,

i'm running a fresh install of Moodle 3.5.2 (Build: 20180910) with WebHook 3.0.1 (Build: 2018061900) and the list of checked events is never saved.

After some debugging, it showed up that after calling $mform->get_data() (editservice.php:59) the resulting $data object either has an empty events property or no events at all, while inspecting $_POST shows that events checkboxes array are actually there.

It looks like there's a problem with the backslashes used as the keys for the events array. As a quick and dirty fix, I tried replacing the backslashes with an arbitrary char sequence and got everything saved as expected.

Here's what I did (note that my code refers the the v3.0.0-stable branch) file classes/service_form.php, changed

/* Formation of the list of elements */
foreach ($eventlist as $event) {
    $events[$event["component"]][] =& $mform->createElement("checkbox", $event["eventname"], $event["eventname"]);
}

to

/* Formation of the list of elements */
foreach ($eventlist as $event) php
    // replace backslashes with 'XXX'
    $events[$event["component"]][] =& $mform->createElement("checkbox", str_replace("\\","XXX",$event["eventname"]), $event["eventname"]);
}

file classes/handler.php in the events method, changed

foreach ($callbacks as $callback) {
    self::handler_callback($data, $callback);
}

to

foreach ($callbacks as $callback) {
    // replace 'XXX' with the backslash
    foreach ($callback->events as $k=>$v) {
        $callback->events[str_replace("XXX","\\",$k)] = $v;
        unset($callback->events[$k]);
    }
    self::handler_callback($data, $callback);
}

Are you experiencing a similar issue? Shall we think of a more reliable way to fix it?

Cheers!

VFXpro commented 6 years ago

I'm experiencing the same thing... latest version of Moodle. Is your code above deployed? What can I do to get my triggers to stay selected? It's creating a huge problem for me right now... Tips?

HobieCat commented 6 years ago

As stated, this is a quick and dirty fix just to point out that my guessing about the backslahes could be good.

Should I ever come with a more elegant and acceptable solution, I'll surely make a pull request here. In the meantime if you're in a hurry i see no other tip than copy and paste the above code in the mentioned files.

NOTE: If you have some previously saved settings, applying my fix will surely erase them.

VFXpro commented 6 years ago

Hi Georgio. I'm not a developer, and don't know Linux or php but technically savvy in other areas. I have no previously applied settings. Is there any easier option where I could, for example, have the Hosting support team copy and paste your files into a specific directory? I really need the webhooks to work for integration requirements. Thanks for your help, truly appreciated.

valentineus commented 6 years ago

Hi @HobieCat, @VFXpro

Thank you so much for describing the problem in such detail and showing the solution. Your efforts helped me locate the problem much faster and release the patch.

@HobieCat, your solution works well, but there are problems with user events that may not match the pattern. I decided to simply encode and decode the names of events before displaying and changing the list.

If you're interested, you can see the patch here: https://github.com/valentineus/moodle-webhooks/commit/849d7126d8674faac056b941038f5fd59811e34f

VFXpro commented 6 years ago

Valentin,

Does the patch is something I can download and install like your plugin posted in the Moodle plugin directory or need to compile something?

Get Outlook Mobilehttps://aka.ms/qtex0l

valentineus commented 6 years ago

@VFXpro, The plugin has already placed in the Moodle plugin repository. You can update the plugin in the usual way.

VFXpro commented 6 years ago

Fantastic!! You are the best Sir!

VFXpro commented 6 years ago

The plugin update seems to be partially working, the triggers are not being saved (yes!), but the the http call is not being made to my end points. I have plenty of other identical end points being reached when a quiz is started or completed. Am I missing a setting, a config? FYI - I'm brand new at Moodle...

I installed the plugin but perhaps my environment is missing a dependency. Do I need the webservices to be turned on? SMTP to be setup?

Thanks for the help in advance...!

HobieCat commented 6 years ago

Hi @valentineus

thank you so much for your quick fix! Is working ok here, checked events are saved and endpoint called (as far as I can tell from my very basic setup in which a local endpoint is called). From my point of view you can close the issue.

@VFXpro : you can check under Site Administration > Report > Log if WebHooks events are triggered (it's the "component" column of the log) and possibily the return headers of the call in the "description" column. I usually have a either 404 Not Found or 200 OK there. Hope it helps

Cheers!

VFXpro commented 6 years ago

Hi @HobieCat, I truly appreciate the assitance. The log under "Component/webhook" returns a 202 Accepted but my callback URL receives absolutely nothing and is https but my Moodle instance is http. Do you think it could be an issue?