Skip to content

Commit

Permalink
First steps to adding disabling rules for bookingoptions #656
Browse files Browse the repository at this point in the history
  • Loading branch information
eynimeni committed Dec 19, 2024
1 parent f5c3a09 commit 01cab83
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 1 deletion.
15 changes: 15 additions & 0 deletions classes/booking_rules/booking_rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,19 @@ public static function delete_rules_by_context(int $contextid) {
rules_info::delete_rule($rule->id);
}
}

/**
* Check if rules in a given contextid match with the bookingid.
* @param int $bookingid
* @param int $contextid
*/
public static function booking_matches_rulecontext(int $bookingcmid, int $contextid) {

if ($contextid == 1) {
return true;
}
// Context of the rule.
$context = context::instance_by_id($contextid);
return $context->instanceid == $bookingcmid;
}
}
1 change: 0 additions & 1 deletion classes/option/field_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ public static function instance_form_definition(
$fieldstoinstanciate = [],
$applyheader = true
) {

}

/**
Expand Down
219 changes: 219 additions & 0 deletions classes/option/fields/rules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
<?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/>.

/**
* Control and manage booking dates.
*
* @package mod_booking
* @copyright 2024 Wunderbyte GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_booking\option\fields;

use mod_booking\booking_option;
use mod_booking\booking_option_settings;
use mod_booking\booking_rules\booking_rules;
use mod_booking\option\fields;
use mod_booking\option\fields_info;
use mod_booking\option\field_base;
use mod_booking\singleton_service;
use moodle_url;
use MoodleQuickForm;
use stdClass;

/**
* Class to handle one property of the booking_option_settings class.
*
* @copyright Wunderbyte GmbH <[email protected]>
* @author Georg Maißer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class rules extends field_base {

/**
* This ID is used for sorting execution.
* @var int
*/
public static $id = MOD_BOOKING_OPTION_FIELD_RULES;

/**
* Some fields are saved with the booking option...
* This is normal behaviour.
* Some can be saved only post save (when they need the option id).
* @var int
*/
public static $save = MOD_BOOKING_EXECUTION_NORMAL;

/**
* This identifies the header under which this particular field should be displayed.
* @var string
*/
public static $header = MOD_BOOKING_HEADER_RULES;

/**
* An int value to define if this field is standard or used in a different context.
* @var array
*/
public static $fieldcategories = [MOD_BOOKING_OPTION_FIELD_STANDARD];

/**
* Additionally to the classname, there might be others keys which should instantiate this class.
* @var array
*/
public static $alternativeimportidentifiers = ['rules'];

/**
* This is an array of incompatible field ids.
* @var array
*/
public static $incompatiblefields = [];

/**
* This function interprets the value from the form and, if useful...
* ... relays it to the new option class for saving or updating.
* @param stdClass $formdata
* @param stdClass $newoption
* @param int $updateparam
* @param ?mixed $returnvalue
* @return string // If no warning, empty string.
*/
public static function prepare_save_field(
stdClass &$formdata,
stdClass &$newoption,
int $updateparam,
$returnvalue = null): array {

// REgex ids gecheckte boxen von rules.
$rulesdeactivated = [];

foreach ($formdata as $key => $value) {
// Check if the key matches the pattern 'ruleinactive_' followed by a number.
if (preg_match('/^ruleinactive_(\d+)$/', $key, $matches)) {
// If the value is not empty, add the rule ID to the array.
if (!empty($value)) {
$rulesdeactivated[] = (int)$matches[1];
}
}
}
if (empty($rulesdeactivated)) {
// This will store the correct JSON to $optionvalues->json.
booking_option::remove_key_from_json($newoption, "rulesdeactivated");
} else {
booking_option::add_data_to_json($newoption, "rulesdeactivated", $rulesdeactivated);
}

// TODO: Correctly check for changes!
$mockdata = new stdClass();
$mockdata->id = $formdata->id;
$instance = new rules();
$changes = $instance->check_for_changes($formdata, $instance, $mockdata);

return $changes;
}

/**
* Instance form definition
* @param MoodleQuickForm $mform
* @param array $formdata
* @param array $optionformconfig
* @param array $fieldstoinstanciate
* @param bool $applyheader
* @return void
*/
public static function instance_form_definition(
MoodleQuickForm &$mform,
array &$formdata,
array $optionformconfig,
$fieldstoinstanciate = [],
$applyheader = true
) {

global $CFG, $COURSE;

// Standardfunctionality to add a header to the mform (only if its not yet there).
if ($applyheader) {
fields_info::add_header_to_mform($mform, self::$header);
}

$booking = singleton_service::get_instance_of_booking_by_bookingid($formdata['bookingid'] ?? 0);

$allrules = booking_rules::get_list_of_saved_rules();

if (empty($allrules)) {
$mform->addElement('static', 'norules', '', get_string('norulesfound', 'mod_booking'));
}
// Order rules by context.
$rulesbycontext = [];
foreach ($allrules as $rule) {
$rulesbycontext[$rule->contextid][] = $rule;
}

foreach ($rulesbycontext as $context => $rules) {
if (
!booking_rules::booking_matches_rulecontext($booking->cmid, $context)
) {
unset($rulesbycontext[$context]);
continue;
}
// Add header.
if ($context == 1) {
$url = new moodle_url('/mod/booking/edit_rules.php', ['cmid' => $booking->cmid]);
$mform->addElement(
'static',
'rulesheader_' . $context,
'',
get_string('rulesincontextglobalheader', 'mod_booking', $url->out())
);
} else {
$url = new moodle_url('/mod/booking/edit_rules.php', ['cmid' => $booking->cmid]);
$a = (object) [
'rulesincontexturl' => $url->out(),
'bookingname' => $booking->settings->name,
];
$mform->addElement('static', 'rulesheader_' . $context, '', get_string('rulesincontextheader', 'mod_booking', $a));
}

foreach ($rules as $rule) {
$data = json_decode($rule->rulejson);
$mform->addElement(
'advcheckbox',
'ruleinactive_' . $rule->id,
$data->name,
get_string('bookingruledeactivate', 'mod_booking'),
0,
[0, 1]
);
}
}
}

/**
* Standard function to transfer stored value to form.
* @param stdClass $data
* @param booking_option_settings $settings
* @return void
* @throws dml_exception
*/
public static function set_data(stdClass &$data, booking_option_settings $settings) {

$deactivatedrules = booking_option::get_value_of_json_by_key($data->id, "rulesdeactivated");
foreach ($deactivatedrules as $ruleid) {
$key = 'ruleinactive_' . $ruleid;
$data->$key = 1;
}
}
}
5 changes: 5 additions & 0 deletions lang/de/booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@
$string['bookingruleaction'] = "Aktion der Regel";
$string['bookingruleapply'] = "Regel anwenden";
$string['bookingruleapplydesc'] = "Entfernen Sie den Haken, wenn Sie die Regel deaktivieren möchten.";
$string['bookingruledeactivate'] = "Regel für diese Buchungsoption <b>deaktivieren</b>";
$string['bookingrulecondition'] = "Kondition der Regel";
$string['bookingruleisactive'] = "Regel ist aktiv und wird angewandt";
$string['bookingruleisnotactive'] = "Regel ist nicht aktiv und wird nicht angewandt";
Expand Down Expand Up @@ -1903,6 +1904,10 @@
$string['rulesendmailcpf_desc'] = 'Wählen Sie ein Event aus, auf das reagiert werden soll. Legen Sie eine E-Mail-Vorlage an
(Sie können auch Platzhalter wie {bookingdetails} verwenden) und legen Sie fest, an welche Nutzer:innen die E-Mail versendet werden soll.
Beispiel: Alle Nutzer:innen, die im benutzerdefinierten Feld "Studienzentrumsleitung" den Wert "SZL Wien" stehen haben.';
$string['rulesheader'] = '<i class="fa fa-fw fa-pencil-square" aria-hidden="true"></i>&nbsp; Regeln';
$string['rulesincontextglobalheader'] = '<a href="{$a}" target="_blank">Globale Regeln</a>';
$string['rulesincontextheader'] = '<a href="{$a->rulesincontexturl}" target="_blank">Regeln in Buchungsinstanz "{$a->bookingname}"</a>';
$string['rulesnotfound'] = 'Keine Regeln für diese Buchungsoption gefunden';
$string['rulessettings'] = "Einstellungen für Regeln";
$string['rulessettingsdesc'] = 'Einstellungen, die für die <a href="{$a}">Funktion Buchungs Regeln</a> gelten.';
$string['rulevalue'] = 'Wert';
Expand Down
4 changes: 4 additions & 0 deletions lang/en/booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,10 @@
$string['rulesendmailcpf_desc'] = 'Choose an event that should trigger the "Send an e-mail" rule. Enter an e-mail template
(you can use placeholders like {bookingdetails}) and define to which users the e-mail should be sent.
Example: All users having the value "Vienna center" in a custom user profile field called "Study center".';
$string['rulesheader'] = '<i class="fa fa-fw fa-pencil-square" aria-hidden="true"></i>&nbsp; Regeln';
$string['rulesincontextglobalheader'] = '<a href="{$a}" target="_blank">Global rules</a>';
$string['rulesincontextheader'] = '<a href="{$a->rulesincontexturl}" target="_blank">Rules in bookinginstance "{$a->bookingname}"</a>';
$string['rulesnotfound'] = 'No rules found for this bookingoption';
$string['rulessettings'] = "Settings for Booking Rules";
$string['rulessettingsdesc'] = 'Settings that apply to the <a href="{$a}">Booking Rules Feature</a>.';
$string['rulevalue'] = 'Value';
Expand Down
2 changes: 2 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@
define('MOD_BOOKING_OPTION_FIELD_TIMEMODIFIED', 530);
define('MOD_BOOKING_OPTION_FIELD_TEMPLATESAVE', 600);
define('MOD_BOOKING_OPTION_FIELD_EVENTSLIST', 700);
define('MOD_BOOKING_OPTION_FIELD_RULES', 800);
define('MOD_BOOKING_OPTION_FIELD_AFTERSUBMITACTION', 999);

// To define execution of field methods.
Expand All @@ -276,6 +277,7 @@
define('MOD_BOOKING_HEADER_CUSTOMFIELDS', 'category_'); // There can be multiple headers, with custom names.
define('MOD_BOOKING_HEADER_TEMPLATESAVE', 'templateheader');
define('MOD_BOOKING_HEADER_COURSES', 'coursesheader');
define('MOD_BOOKING_HEADER_RULES', 'rulesheader');

define('MOD_BOOKING_MAX_CUSTOM_FIELDS', 3);
define('MOD_BOOKING_FORM_OPTIONDATEID', 'optiondateid_');
Expand Down

0 comments on commit 01cab83

Please sign in to comment.