Skip to content

Commit

Permalink
GH-746 Implemented further pricecategoryfallback strategy without bre…
Browse files Browse the repository at this point in the history
…aking current behaviour
  • Loading branch information
georgmaisser committed Dec 6, 2024
1 parent 0b79b00 commit 2a79d30
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 32 deletions.
68 changes: 44 additions & 24 deletions classes/price.php
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ public static function get_price(string $area, int $itemid, ?object $user = null
$pricerecorddefault = $pricerecord;
}
// Looking for matched pricecategory.
if (strpos($categoryidentifier, $pricecategoryidentifier) !== false) {
if (!empty($categoryidentifier) && strpos($categoryidentifier, $pricecategoryidentifier) !== false) {
$pricecategoryfound = true;
$price = [
"price" => $pricerecord->price,
Expand All @@ -770,30 +770,41 @@ public static function get_price(string $area, int $itemid, ?object $user = null
}
}

// We use the default record as a fallback.
switch ((int)get_config('booking', 'pricecategoryfallback')) {
case 1:
// Logic is: when categoryidentifer is empty, we use default.
$usedefault = true;
break;
case 2:
$usedefault = false;
break;
default:
$usedefault = false;
break;
}

if (
$pricecategoryfound === false
&& get_config('booking', 'pricecategoryfallback')
!$pricecategoryfound
&& $usedefault
&& !empty($pricerecorddefault)
) {
if (!empty($pricerecorddefault)) {
$price = [
"price" => $pricerecorddefault->price,
"currency" => $pricerecorddefault->currency,
"pricecategoryidentifier" => $pricerecorddefault->pricecategoryidentifier,
"pricecategoryname" =>
self::get_active_pricecategory_from_cache_or_db($pricerecorddefault->pricecategoryidentifier)->name,
];
} else {
return []; // No default for some reason (should never happens).
}
$price = [
"price" => $pricerecorddefault->price,
"currency" => $pricerecorddefault->currency,
"pricecategoryidentifier" => $pricerecorddefault->pricecategoryidentifier,
"pricecategoryname" =>
self::get_active_pricecategory_from_cache_or_db($pricerecorddefault->pricecategoryidentifier)->name,
];
} else if (
$pricecategoryfound === false
&& empty(get_config('booking', 'pricecategoryfallback'))
!$pricecategoryfound
&& !$usedefault
) {
return [];
return []; // No default for some reason (should never happens).
}

if ($area === "option" && isset($price['price'])) {
if (
$area === "option" && isset($price['price'])
) {
$customformstore = new customformstore($user->id, $itemid);
$price['price'] = $customformstore->modify_price($price['price'], $categoryidentifier);
}
Expand Down Expand Up @@ -849,17 +860,26 @@ public static function get_pricecategory_for_user(stdClass $user) {
// If a user profile field to story the price category identifiers for each user has been set,
// then retrieve it from config and set the correct category identifier for the current user.
$fieldshortname = get_config('booking', 'pricecategoryfield');
$pricecategoryfallback = get_config('booking', 'pricecategoryfallback');

if (!isset($user->profile) ||
!isset($user->profile[$fieldshortname])) {
if (
!isset($user->profile) ||
!isset($user->profile[$fieldshortname])
) {

require_once("$CFG->dirroot/user/profile/lib.php");
profile_load_custom_fields($user);
}

if (!isset($user->profile[$fieldshortname])
|| empty($user->profile[$fieldshortname])) {
$categoryidentifier = 'default'; // Default.
if (
!isset($user->profile[$fieldshortname])
|| empty($user->profile[$fieldshortname])
) {
if ($pricecategoryfallback == 2) {
$categoryidentifier = '';
} else {
$categoryidentifier = 'default'; // Default.
}
} else {
$categoryidentifier = $user->profile[$fieldshortname];
}
Expand Down
3 changes: 3 additions & 0 deletions lang/de/booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,9 @@
$string['extendlimitforoverbooked_help'] = 'Wählen Sie diese Option, passiert folgendes:
Ein Kurs hat ein Limit von 40. Er ist aber bereits mit 2 TN auf 42 TN überbucht.
Wird auf diesen Kurs eine Limiterhöhung um beispielsweise 10% angewandt, wird das Limit auf 46 erhöht (40 + 4 (10%) + 2 (bereits überbuchte)), statt auf 44 (40+4).';
$string['fallbackonlywhenempty'] = 'Fallback nur, wenn entsprechendes Nutzerprofilfeld leer ist';
$string['fallbackonlywhennotmatching'] = 'Fallback nur, wenn nicht übereinstimmend (auch wenn Feld leer ist)';
$string['fallbackturnedoff'] = 'Fallback deaktiviert';
$string['feedbackurl'] = 'Link zur Umfrage';
$string['feedbackurl_help'] = 'Link zu einem Feedback-Formular, das an Teilnehmer:innen gesendet werden soll.
Verwenden Sie in E-Mails den Platzhalter <b>{pollurl}</b>.';
Expand Down
3 changes: 3 additions & 0 deletions lang/en/booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,9 @@
$string['extendlimitforoverbooked_help'] = 'If you select this option, the following happens:
A course has a limit of 40 but is already overbooked with 2 participants to 42 participants.
If a limit increase of, for example, 10% is applied to this course, the limit will be increased to 46 (40 + 4 (10%) + 2 (overbooked seats)), instead of 44 (40 + 4).';
$string['fallbackonlywhenempty'] = 'Fallback only when user profile field is empty';
$string['fallbackonlywhennotmatching'] = 'Fallback when not matching (also empty)';
$string['fallbackturnedoff'] = 'Fallback turned off';
$string['feedbackurl'] = 'Poll url';
$string['feedbackurl_help'] = 'Enter a link to a feedback form that should be sent to participants.
It can be added to e-mails with the <b>{pollurl}</b> placeholder.';
Expand Down
26 changes: 18 additions & 8 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
get_string('optionformconfig', 'mod_booking'),
new moodle_url('/mod/booking/optionformconfig.php', [
'cmid' => 0,
])
])
)
);

Expand Down Expand Up @@ -163,7 +163,7 @@
'licensekeycfgheading',
get_string('licensekeycfg', 'mod_booking'),
$proversion ? get_string('licensekeycfgdesc:active', 'mod_booking') :
get_string('licensekeycfgdesc', 'mod_booking')
get_string('licensekeycfgdesc', 'mod_booking')
)
);

Expand Down Expand Up @@ -926,12 +926,20 @@
$userprofilefieldsarray
)
);

$defaultbehaviours = [
0 => get_string('fallbackonlywhenempty', 'booking'),
1 => get_string('fallbackonlywhennotmatching', 'booking'),
2 => get_string('fallbackturnedoff', 'booking'),
];

$settings->add(
new admin_setting_configcheckbox(
new admin_setting_configselect(
'booking/pricecategoryfallback',
get_string('pricecategoryfallback', 'mod_booking'),
get_string('pricecategoryfallback_desc', 'mod_booking'),
0
0,
$defaultbehaviours
)
);

Expand Down Expand Up @@ -1377,10 +1385,12 @@
)
);

$options = [1 => get_string('courseurl', 'mod_booking'),
2 => get_string('location', 'mod_booking'),
3 => get_string('institution', 'mod_booking'), 4 => get_string('address'),
];
$options = [
1 => get_string('courseurl', 'mod_booking'),
2 => get_string('location', 'mod_booking'),
3 => get_string('institution', 'mod_booking'),
4 => get_string('address'),
];
$settings->add(
new admin_setting_configselect(
'booking/icalfieldlocation',
Expand Down
Loading

0 comments on commit 2a79d30

Please sign in to comment.