Skip to content

Commit

Permalink
Improvement: Apply rules again after every time a user has booked (on…
Browse files Browse the repository at this point in the history
…ly for option & userid)
  • Loading branch information
georgmaisser committed Mar 29, 2024
1 parent e99efc8 commit ea119cd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions classes/booking_option.php
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,9 @@ public function after_successful_booking_routine(stdClass $user, int $waitinglis
}
}

// Now check, if there are rules to execute.
rules_info::execute_rules_for_option($this->optionid, $user->id);

if ($this->booking->settings->sendmail) {
$this->send_confirm_message($user);
}
Expand Down
5 changes: 5 additions & 0 deletions classes/booking_rules/rules/rule_daysbefore.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ public function get_records_for_execution(int $optionid = 0, int $userid = 0, bo
$params['optionid'] = $optionid;
}

// When we want to restrict the userid, we just pass on the param to the condition like this.
if (!empty($userid)) {
$params['userid'] = $userid;
}

$sql = new stdClass();

$sql->select = "bo.id optionid, cm.id cmid, bo." . $ruledata->datefield . " datefield";
Expand Down
9 changes: 7 additions & 2 deletions classes/booking_rules/rules_info.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

namespace mod_booking\booking_rules;

use dml_exception;
use Exception;
use MoodleQuickForm;
use stdClass;
Expand Down Expand Up @@ -227,9 +228,13 @@ public static function execute_booking_rules() {
/**
* After an option has been added or updated,
* we need to check if any rules need to be applied or changed.
* Also, after a user has booked we run this.
* @param int $optionid
* @param int $userid
* @return void
* @throws dml_exception
*/
public static function execute_rules_for_option(int $optionid) {
public static function execute_rules_for_option(int $optionid, int $userid = 0) {
global $DB;

// Only fetch rules which need to be reapplied. At the moment, it's just one.
Expand All @@ -242,7 +247,7 @@ public static function execute_rules_for_option(int $optionid) {
// Important: Load the rule data from JSON into the rule instance.
$rule->set_ruledata($record);
// Now the rule can be executed.
$rule->execute($optionid);
$rule->execute($optionid, $userid);
}
}
}
Expand Down

0 comments on commit ea119cd

Please sign in to comment.