Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions classes/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ public function definition() {
$mform->addHelpButton('workflowname', 'workflowname', 'tool_trigger');

// Workflow description.
$editoroptions = array(
$editoroptions = [
'subdirs' => 0,
'maxbytes' => 0,
'maxfiles' => 0,
'changeformat' => 0,
'context' => \context_system::instance(),
'noclean' => 0,
'trusttext' => 0
);
'trusttext' => 0,
];
$mform->addElement('editor', 'workflowdescription', get_string ('workflowdescription', 'tool_trigger'), $editoroptions);
$mform->setType('workflowdescription', PARAM_RAW_TRIMMED);
$mform->addHelpButton('workflowdescription', 'workflowdescription', 'tool_trigger');
Expand Down Expand Up @@ -95,7 +95,7 @@ public function definition() {
$mform->addElement('advcheckbox',
'workflowactive',
get_string ('workflowactive', 'tool_trigger'),
'Enable', array(), array(0, 1));
'Enable', [], [0, 1]);
$mform->setType('workflowactive', PARAM_INT);
$mform->addHelpButton('workflowactive', 'workflowactive', 'tool_trigger');
$mform->setDefault('workflowactive', 1);
Expand All @@ -104,7 +104,7 @@ public function definition() {
$mform->addElement('advcheckbox',
'workflowrealtime',
get_string ('workflowrealtime', 'tool_trigger'),
'Enable', array(), array(0, 1));
'Enable', [], [0, 1]);
$mform->setType('workflowrealtime', PARAM_INT);
$mform->addHelpButton('workflowrealtime', 'workflowrealtime', 'tool_trigger');
$mform->setDefault('workflowrealtime', 0);
Expand All @@ -113,7 +113,7 @@ public function definition() {
$mform->addElement('advcheckbox',
'workflowdebug',
get_string('workflowdebug', 'tool_trigger'),
'Enable', array(), array(0, 1));
'Enable', [], [0, 1]);
$mform->setType('workflowdebug', PARAM_INT);
$mform->addHelpButton('workflowdebug', 'workflowdebug', 'tool_trigger');
$mform->setDefault('workflowdebug', 0);
Expand Down
49 changes: 28 additions & 21 deletions classes/event_processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Process trigger system events.
*
* @package tool_trigger
* @copyright Matt Porritt <mattp@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_trigger;

use tool_trigger\helper\processor_helper;
Expand Down Expand Up @@ -138,7 +130,7 @@ protected function is_event_ignored(\core\event\base $event) {
// If we do not have the triggers in the cache then return them from the DB.
if ($sitesubscriptions === false) {
// Set the array for the cache.
$sitesubscriptions = array();
$sitesubscriptions = [];
if ($subscriptions = $DB->get_records_sql($sql)) {
foreach ($subscriptions as $subscription) {
$sitesubscriptions[$subscription->event] = true;
Expand Down Expand Up @@ -198,6 +190,21 @@ private function process_realtime_workflows($evententry) {
}
}

/**
* Processes a real-time workflow by executing its steps immediately in response to an event.
*
* This method:
* - Marks the workflow as triggered and updates its record in the database.
* - Records the workflow trigger in history.
* - Restores the triggering event from stored data.
* - Iterates through workflow steps in order, executing each step and recording results.
* - Stops execution early if a step fails or throws an error.
* - Handles transactional safety for each step to prevent partial writes.
* - If an error occurs, records the failed step and queues the workflow for retry via cron.
*
* @param \stdClass $workflow The workflow object containing workflow metadata.
* @param \stdClass $evententry The event entry object representing the trigger event data.
*/
private function process_realtime_workflow($workflow, $evententry) {
global $DB;

Expand Down Expand Up @@ -292,20 +299,20 @@ public static function record_workflow_trigger(int $workflowid, $event, int $att

// Get new run number.
$sqlfrag = "SELECT MAX(number) FROM {tool_trigger_workflow_hist} WHERE workflowid = :wfid";
$runnumber = $DB->get_field_sql($sqlfrag, array('wfid' => $workflowid)) + 1;
$runnumber = $DB->get_field_sql($sqlfrag, ['wfid' => $workflowid]) + 1;

// Encode event data as JSON.
$eventdata = json_encode($event);

$id = $DB->insert_record('tool_trigger_workflow_hist', array(
$id = $DB->insert_record('tool_trigger_workflow_hist', [
'workflowid' => $workflowid,
'number' => $runnumber,
'timecreated' => time(),
'event' => $eventdata,
'eventid' => $event->id,
'userid' => $event->userid,
'attemptnum' => $attemptnum
), true);
'attemptnum' => $attemptnum,
], true);

// Return the id for use in other tables.
return $id;
Expand Down Expand Up @@ -488,7 +495,7 @@ public static function execute_next_step_historic(int $stepid, int $origrun = 0,
'workflow' => $step->workflowid,
'run' => $origrun,
'number' => $step->number + 1,
'previd' => $step->id
'previd' => $step->id,
];
$nextstep = $DB->get_record_sql($nextstepsql, $params);

Expand Down Expand Up @@ -529,7 +536,7 @@ public static function execute_next_step_historic(int $stepid, int $origrun = 0,
'workflow' => $nextstep->workflowid,
'run' => $runid,
'stepname' => $nextstep->name,
'previd' => $previd
'previd' => $previd,
]);

// Return the ID just executed for use in moving through the historic chain.
Expand Down Expand Up @@ -559,7 +566,7 @@ public static function execute_next_step_current(int $stepid) {
$nextstep = $DB->get_record_sql($nextstepsql, [
'workflow' => $step->workflowid,
'run' => $step->runid,
'number' => $step->number + 1
'number' => $step->number + 1,
]);

// If no nextstep is found, jump out.
Expand All @@ -582,7 +589,7 @@ public static function execute_next_step_current(int $stepid) {
$newstepid = $DB->get_field_sql($newstepsql, [
'workflow' => $nextstep->workflowid,
'run' => $nextstep->runid,
'stepname' => $nextstep->name
'stepname' => $nextstep->name,
]);
return $newstepid;
}
Expand Down Expand Up @@ -619,7 +626,7 @@ public static function execute_step_and_continue_historic(int $stepid, bool $com
$newstep = $DB->get_record_sql($newstepsql, [
'workflow' => $step->workflowid,
'run' => $newrunid,
'stepname' => $step->name
'stepname' => $step->name,
]);

// Now execute the next step in the historic chain.
Expand Down Expand Up @@ -660,7 +667,7 @@ public static function execute_step_and_continue_current(int $stepid, bool $comp
$newstep = $DB->get_record_sql($newstepsql, [
'workflow' => $step->workflowid,
'run' => $step->runid,
'stepname' => $step->name
'stepname' => $step->name,
]);

// Now execute the next step, based on the step we just created.
Expand Down Expand Up @@ -809,7 +816,7 @@ public static function record_cancelled_workflow($workflowid, $event, $runid = n

// Get new run number.
$sqlfrag = "SELECT MAX(number) FROM {tool_trigger_workflow_hist} WHERE workflowid = :wfid";
$runnumber = $DB->get_field_sql($sqlfrag, array('wfid' => $workflowid)) + 1;
$runnumber = $DB->get_field_sql($sqlfrag, ['wfid' => $workflowid]) + 1;

// Encode event data as JSON.
$eventdata = json_encode($event);
Expand All @@ -822,7 +829,7 @@ public static function record_cancelled_workflow($workflowid, $event, $runid = n
'timecreated' => time(),
'event' => $eventdata,
'eventid' => $event->id,
'failedstep' => $status
'failedstep' => $status,
];

if (empty($runid)) {
Expand Down
13 changes: 4 additions & 9 deletions classes/helper/datafield_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* A lookup step that takes a user's ID and adds standard data about the user.
*
* @package tool_trigger
* @author Aaron Wells <aaronw@catalyst.net.nz>
* @copyright Catalyst IT, 2018
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_trigger\helper;

/**
Expand All @@ -36,6 +27,10 @@
*/
trait datafield_manager {

/**
* Data fields.
* @var array
*/
protected $datafields = [];

/** @var string regex to determine data fields - should ideally be readonly */
Expand Down
9 changes: 4 additions & 5 deletions classes/helper/processor_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace tool_trigger\helper;

/**
* trait to help in processing events.
*
Expand All @@ -22,9 +24,6 @@
* @copyright 2019 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_trigger\helper;

trait processor_helper {

/**
Expand All @@ -37,11 +36,11 @@ public function restore_event(\stdClass $data) {
if (empty((array)$data)) {
return null;
}
$extra = array('origin' => $data->origin, 'ip' => $data->ip, 'realuserid' => $data->realuserid);
$extra = ['origin' => $data->origin, 'ip' => $data->ip, 'realuserid' => $data->realuserid];
$data = (array)$data;
$data['other'] = unserialize($data['other']);
if ($data['other'] === false) {
$data['other'] = array();
$data['other'] = [];
}

// Insert eventid into other data.
Expand Down
4 changes: 2 additions & 2 deletions classes/import_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function definition() {

// Workflow file.
$mform->addElement('filepicker', 'userfile', get_string('workflowfile', 'tool_trigger'), null,
array('maxbytes' => 256000, 'accepted_types' => '.json'));
['maxbytes' => 256000, 'accepted_types' => '.json']);
$mform->addRule('userfile', get_string('required'), 'required');
}

Expand All @@ -73,7 +73,7 @@ public function definition() {
public function validation($data, $files) {
global $USER;

$validationerrors = array();
$validationerrors = [];

// Get the file from the filestystem. $files will always be empty.
$fs = get_file_storage();
Expand Down
2 changes: 1 addition & 1 deletion classes/json/json_export.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private function get_workflow_json($workflowrecord) {
*
* @param string $workflowjson
*/
private function print_json_data ($workflowjson) {
private function print_json_data($workflowjson) {
echo $workflowjson;
}

Expand Down
38 changes: 19 additions & 19 deletions classes/learn_process.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class learn_process {
*
* @var array
*/
private $typearray = array();
private $typearray = [];

/**
* Get a list of all the distinct events names in the learning table.
Expand All @@ -64,7 +64,7 @@ private function get_learnt_events() {
private function get_learnt_records($learntevent) {
global $DB;

$learntrecords = $DB->get_recordset('tool_trigger_learn_events', array('eventname' => $learntevent));
$learntrecords = $DB->get_recordset('tool_trigger_learn_events', ['eventname' => $learntevent]);

return $learntrecords;
}
Expand Down Expand Up @@ -161,7 +161,7 @@ public function store_json_fields($learntevent, $jsonfields) {
$transaction = $DB->start_delegated_transaction();

// Check for existing record in DB.
$exists = $DB->get_record('tool_trigger_event_fields', array('eventname' => $learntevent), '*', IGNORE_MISSING);
$exists = $DB->get_record('tool_trigger_event_fields', ['eventname' => $learntevent], '*', IGNORE_MISSING);

if ($exists) { // If record exists update.
$record->id = $exists->id;
Expand All @@ -180,24 +180,24 @@ public function store_json_fields($learntevent, $jsonfields) {
/**
* Process the learnt events and extract the field names.
*/
public function process () {
public function process() {
global $DB;

// Get a list of the event types from the learn table.
$learntevents = $this->get_learnt_events();

// For each type of event get all the entries for that event from the learn table.
foreach ($learntevents as $learntevent) {
$processedrecords = array();
$processedrecords = [];
$learntrecords = $this->get_learnt_records($learntevent);

foreach ($learntrecords as $record) {
$this->typearray = array(); // Reset typearray before calling convert_record_type.
$this->typearray = []; // Reset typearray before calling convert_record_type.
// Convert each record into an array where key is field name and value is type.
$processedrecords[] = $this->convert_record_type($record, false);

// Remove learnt event from DB.
$DB->delete_records('tool_trigger_learn_events', array('id' => $record->id));
$DB->delete_records('tool_trigger_learn_events', ['id' => $record->id]);
}

$learntrecords->close(); // Don't forget to close the recordset!
Expand Down Expand Up @@ -239,7 +239,7 @@ public function get_event_fields_json($eventname) {
global $DB;
$jsonfields = $DB->get_record(
'tool_trigger_event_fields',
array('eventname' => $eventname), 'jsonfields', IGNORE_MISSING);
['eventname' => $eventname], 'jsonfields', IGNORE_MISSING);

return $jsonfields;
}
Expand All @@ -252,30 +252,30 @@ public function get_event_fields_json($eventname) {
*/
public function get_event_fields_with_type($eventname) {
global $DB;
$fieldarray = array();
$fieldarray = [];

$jsonfields = $DB->get_record(
'tool_trigger_event_fields',
array('eventname' => $eventname), 'jsonfields', IGNORE_MISSING);
['eventname' => $eventname], 'jsonfields', IGNORE_MISSING);

if ($jsonfields) {
$fields = json_decode($jsonfields->jsonfields, true);
foreach ($fields as $field => $type) {
$fieldarray[] = array(
$fieldarray[] = [
'field' => $field,
'type' => $type
);
'type' => $type,
];
}
}

$fieldarray[] = array(
$fieldarray[] = [
'field' => 'wwwroot',
'type' => 'string'
);
$fieldarray[] = array(
'type' => 'string',
];
$fieldarray[] = [
'field' => 'wwwroot_domain',
'type' => 'string'
);
'type' => 'string',
];

return $fieldarray;
}
Expand Down
Loading
Loading