Skip to content

Commit

Permalink
GH-744 Create first integrated template to ship booking with preconfi…
Browse files Browse the repository at this point in the history
…gured rules-templates
  • Loading branch information
georgmaisser committed Dec 4, 2024
1 parent cce2a69 commit c8b897b
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace mod_booking\booking_rules\rules\templates;

use context;
use mod_booking\booking_rules\actions_info;
use mod_booking\booking_rules\conditions_info;
use mod_booking\singleton_service;
use MoodleQuickForm;
use stdClass;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/mod/booking/lib.php');

/**
* Rule do something a specified number of days before a chosen date.
*
* @package mod_booking
* @copyright 2022 Wunderbyte GmbH <[email protected]>
* @author Georg Maißer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class ruletemplate_confirmbooking {
/** @var int $templateid */
public static $templateid = 1;

/** @var int $eventtype */
public static $eventtype = 'rule_react_on_event';

/**
* Returns the localized name of this template
*
* @return string
*
*/
public static function get_name() {
return get_string('ruletemplateconfirmbooking', 'booking');
}

/**
* Returns the template as if it came from DB.
*
* @return object
*
*/
public static function return_template() {

$rulejson = (object)[
"conditionname" => "select_user_from_event",
"conditiondata" => [
"userfromeventtype" => "relateduserid",
],
"name" => self::get_name(),
"actionname" => "send_mail",
"actiondata" => [
"subject" => get_string('ruletemplateconfirmbookingsubject', 'booking'),
"template" => get_string('ruletemplateconfirmbookingbody', 'booking'),
"templateformat" => "1",
],
"rulename" => "rule_react_on_event",
"ruledata" => [
"boevent" => "\\mod_booking\\event\bookingoption_booked",
"condition" => "0",
"aftercompletion" => 0,
"cancelrules" => [],
],
];

$returnobject = [
'id' => self::$templateid,
'rulename' => self::$eventtype,
'rulejson' => json_encode($rulejson),
'eventname' => "\\mod_booking\\event\bookingoption_booked",
'contextid' => 1,
'useastemplate' => 0,
];
return (object) $returnobject;
}
}
12 changes: 10 additions & 2 deletions classes/booking_rules/rules_info.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use context;
use context_module;
use core_component;
use dml_exception;
use context_system;
use mod_booking\local\templaterule;
Expand Down Expand Up @@ -201,8 +202,15 @@ public static function set_data_for_form(object &$data) {
return new stdClass();
}

// If we have an ID, we retrieve the right rule from DB.
$record = $DB->get_record('booking_rules', ['id' => $data->id]);
if ($data->id < 0) {
// We get the value von den predefined templates.

$record = templaterule::get_template_record_by_id($data->id);

} else {
// If we have an ID, we retrieve the right rule from DB.
$record = $DB->get_record('booking_rules', ['id' => $data->id]);
}

$data->contextid = $record->contextid;

Expand Down
10 changes: 8 additions & 2 deletions classes/form/rulesform.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace mod_booking\form;
use context_module;
use mod_booking\local\templaterule;
use mod_booking\singleton_service;

defined('MOODLE_INTERNAL') || die();
Expand Down Expand Up @@ -264,8 +265,13 @@ private function prepare_ajaxformdata(array &$ajaxformdata) {
'bookingruletemplate' => $id,
];
}
// If we have an ID, we retrieve the right rule from DB.
$record = $DB->get_record('booking_rules', ['id' => $id]);

if ($id < 0) {
$record = templaterule::get_template_record_by_id($id);
} else {
// If we have an ID, we retrieve the right rule from DB.
$record = $DB->get_record('booking_rules', ['id' => $id]);
}

$jsonboject = json_decode($record->rulejson);

Expand Down
50 changes: 45 additions & 5 deletions classes/local/templaterule.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

namespace mod_booking\local;

use core_component;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/mod/booking/lib.php');
Expand All @@ -44,13 +46,28 @@ class templaterule {
*/
public static function get_template_rules() {
global $DB;
$records = $DB->get_records_sql(
"SELECT boru.id, boru.rulejson
FROM {booking_rules} boru
WHERE boru.useastemplate = 1");

$selectoptions = [
'0' => get_string('bookingdefaulttemplate', 'mod_booking'),
'0' => get_string('bookingdefaulttemplate', 'mod_booking'),
];

$templates = core_component::get_component_classes_in_namespace(
"mod_booking",
'booking_rules\\rules\\templates'
);

foreach ($templates as $classname => $namespace) {
$class = new $classname();
$id = - $classname::$templateid;
$selectoptions[$id] = $class->get_name();
}

$records = $DB->get_records_sql(
"SELECT boru.id, boru.rulejson
FROM {booking_rules} boru
WHERE boru.useastemplate = 1"
);

foreach ($records as $record) {
$record->rulejson = json_decode($record->rulejson);
$selectoptions[$record->id] = $record->rulejson->name;
Expand All @@ -59,4 +76,27 @@ public static function get_template_rules() {
return $selectoptions;
}

/**
* Returns the template record by id.
*
* @param int $id
*
* @return object
*
*/
public static function get_template_record_by_id(int $id) {
$templateid = - $id;
$templates = core_component::get_component_classes_in_namespace(
"mod_booking",
'booking_rules\\rules\\templates'
);
foreach ($templates as $classname => $namespace) {
if ($classname::$templateid == $templateid) {

$class = new $classname();
$record = $class->return_template();
}
}
return $record;
}
}
3 changes: 3 additions & 0 deletions lang/de/booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,9 @@
Beispiel: Alle Nutzer:innen, die im benutzerdefinierten Feld "Studienzentrumsleitung" den Wert "SZL Wien" stehen haben.';
$string['rulessettings'] = "Einstellungen für Regeln";
$string['rulessettingsdesc'] = 'Einstellungen, die für die <a href="{$a}">Funktion Buchungs Regeln</a> gelten.';
$string['ruletemplateconfirmbooking'] = "Template - Bestätige Buchung";
$string['ruletemplateconfirmbookingbody'] = "<p>Sehr geehrte/r {firstname} {lastname},<\/p>\r\n<p>Vielen Dank für Ihre Buchung<\/p>\r\n<p>{bookingdetails}<\/p>\r\n<p>Alles Gute!<\/p>";
$string['ruletemplateconfirmbookingsubject'] = "Sie haben erfolgreich gebucht";
$string['rulevalue'] = 'Wert';
$string['sameday'] = 'Selber Tag';
$string['saturday'] = 'Samstag';
Expand Down
3 changes: 3 additions & 0 deletions lang/en/booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -1942,6 +1942,9 @@
Example: All users having the value "Vienna center" in a custom user profile field called "Study center".';
$string['rulessettings'] = "Settings for Booking Rules";
$string['rulessettingsdesc'] = 'Settings that apply to the <a href="{$a}">Booking Rules Feature</a>.';
$string['ruletemplateconfirmbooking'] = "Template - Confirm Booking";
$string['ruletemplateconfirmbookingbody'] = "<p>Dear {firstname} {lastname},<\/p>\r\n<p>Thank you very much for your booking<\/p>\r\n<p>{bookingdetails}<\/p>\r\n<p>All the best!<\/p>";
$string['ruletemplateconfirmbookingsubject'] = "You have successfully booked";
$string['rulevalue'] = 'Value';
$string['sameday'] = 'same day';
$string['saturday'] = 'Saturday';
Expand Down

0 comments on commit c8b897b

Please sign in to comment.