Skip to content

Commit

Permalink
Improvement: Json conditions as singleton and render string for overl…
Browse files Browse the repository at this point in the history
…apping
  • Loading branch information
eynimeni committed Dec 3, 2024
1 parent 7f6afc4 commit c3cbb5b
Show file tree
Hide file tree
Showing 15 changed files with 305 additions and 37 deletions.
28 changes: 23 additions & 5 deletions classes/bo_availability/bo_info.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ public static function get_condition_results(?int $optionid = null, int $userid
// We now set the id from the json for this instance.
// We might actually use a hardcoded condition with a negative id...
// ... also as customized condition with positive id.
$instance = new $classname($condition->id);
if (method_exists($classname, 'instance')) {
$instance = $classname::instance($condition->id);
} else {
$instance = new $classname($condition->id);
}
$instance->customsettings = $condition;

} else {
Expand Down Expand Up @@ -419,7 +423,12 @@ public static function set_defaults(stdClass &$defaultvalues, $jsonobject) {
foreach ($jsonobject as $conditionobject) {

$classname = $conditionobject->class;
$condition = new $classname($conditionobject->id);
if (method_exists($classname, 'instance')) {
$condition = $classname::instance($conditionobject->id);
} else {
$condition = new $classname($conditionobject->id);
}

$condition->set_defaults($defaultvalues, $conditionobject);
}
}
Expand Down Expand Up @@ -554,7 +563,12 @@ public static function get_conditions(int $condparam = MOD_BOOKING_CONDPARAM_ALL

// We instantiate all the classes, because we need some information.
if (class_exists($filename)) {
$instance = new $filename();
if (method_exists($filename, 'instance')) {
$instance = $filename::instance();
} else {
$instance = new $filename();
}


switch ($condparam) {
case MOD_BOOKING_CONDPARAM_HARDCODED_ONLY:
Expand Down Expand Up @@ -1255,12 +1269,16 @@ public static function validation(array $data, array $files, array &$errors) {
if (!empty($settings->availability)) {
$existingconditions = json_decode($settings->availability);
foreach ($existingconditions as $existingcondition) {
$class = new $existingcondition->class();
$classname = $existingcondition->class;
if (method_exists($classname, 'instance')) {
$class = $classname::instance();
} else {
$class = new $classname();
}
if (method_exists($class, 'validation')) {
$class->validation($data, $files, $errors);
};
}

}

return $errors;
Expand Down
24 changes: 22 additions & 2 deletions classes/bo_availability/conditions/allowedtobookininstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,33 @@ class allowedtobookininstance implements bo_condition {
/** @var stdClass $customsettings an stdclass coming from the json which passes custom settings */
public $customsettings = null;

/**
* Singleton instance.
*
* @var object
*/
private static $instance = null;

/**
* Singleton instance.
*
* @return object
*
*/
public static function instance(?int $id = null): object {
if (empty(self::$instance)) {
self::$instance = new self($id);
}
return self::$instance;
}

/**
* Constructor.
*
* @param ?int $id
* @return void
*/
public function __construct(?int $id = null) {
private function __construct(?int $id = null) {
if ($id) {
$this->id = $id;
}
Expand Down Expand Up @@ -275,7 +295,7 @@ public function add_condition_to_mform(MoodleQuickForm &$mform, int $optionid =
if (!empty($jsonconditions)) {
foreach ($jsonconditions as $jsoncondition) {
$currentclassname = $jsoncondition->class;
$currentcondition = new $currentclassname();
$currentcondition = $currentclassname::instance();
// Currently conditions of the same type cannot be combined with each other.
if (
$jsoncondition->id != $this->id
Expand Down
2 changes: 1 addition & 1 deletion classes/bo_availability/conditions/capbookingchoose.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function is_available(booking_option_settings $settings, int $userid, boo
// This check can be overridden by a json condition.
// Therefore, we use it's logic.

$allowedtobookininstance = new allowedtobookininstance();
$allowedtobookininstance = allowedtobookininstance::instance();
$allowedtobookininstance->apply_customdata($settings);
return $allowedtobookininstance->is_available($settings, $userid, $not);
}
Expand Down
23 changes: 21 additions & 2 deletions classes/bo_availability/conditions/customform.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,33 @@ class customform implements bo_condition {
/** @var stdClass $customsettings an stdclass coming from the json which passes custom settings */
public $customsettings = null;

/**
* Singleton instance.
*
* @var object
*/
private static $instance = null;

/**
* Singleton instance.
*
* @return object
*
*/
public static function instance(?int $id = null): object {
if (empty(self::$instance)) {
self::$instance = new self($id);
}
return self::$instance;
}

/**
* Constructor.
*
* @param ?int $id
* @return void
*/
public function __construct(?int $id = null) {

private function __construct(?int $id = null) {
if ($id) {
$this->id = $id;
}
Expand Down
24 changes: 22 additions & 2 deletions classes/bo_availability/conditions/enrolledincohorts.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,33 @@ class enrolledincohorts implements bo_condition {
/** @var stdClass $customsettings an stdclass coming from the json which passes custom settings */
public $customsettings = null;

/**
* Singleton instance.
*
* @var object
*/
private static $instance = null;

/**
* Singleton instance.
*
* @return object
*
*/
public static function instance(?int $id = null): object {
if (empty(self::$instance)) {
self::$instance = new self($id);
}
return self::$instance;
}

/**
* Constructor.
*
* @param ?int $id
* @return void
*/
public function __construct(?int $id = null) {
private function __construct(?int $id = null) {
if ($id) {
$this->id = $id;
}
Expand Down Expand Up @@ -395,7 +415,7 @@ public function add_condition_to_mform(MoodleQuickForm &$mform, int $optionid =
if (!empty($jsonconditions)) {
foreach ($jsonconditions as $jsoncondition) {
$currentclassname = $jsoncondition->class;
$currentcondition = new $currentclassname();
$currentcondition = $currentclassname::instance();
// Currently conditions of the same type cannot be combined with each other.
if ($jsoncondition->id != $this->id
&& isset($currentcondition->overridable)
Expand Down
24 changes: 22 additions & 2 deletions classes/bo_availability/conditions/enrolledincourse.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,33 @@ class enrolledincourse implements bo_condition {
/** @var stdClass $customsettings an stdclass coming from the json which passes custom settings */
public $customsettings = null;

/**
* Singleton instance.
*
* @var object
*/
private static $instance = null;

/**
* Singleton instance.
*
* @return object
*
*/
public static function instance(?int $id = null): object {
if (empty(self::$instance)) {
self::$instance = new self($id);
}
return self::$instance;
}

/**
* Constructor.
*
* @param ?int $id
* @return void
*/
public function __construct(?int $id = null) {
private function __construct(?int $id = null) {
if ($id) {
$this->id = $id;
}
Expand Down Expand Up @@ -304,7 +324,7 @@ public function add_condition_to_mform(MoodleQuickForm &$mform, int $optionid =
if (!empty($jsonconditions)) {
foreach ($jsonconditions as $jsoncondition) {
$currentclassname = $jsoncondition->class;
$currentcondition = new $currentclassname();
$currentcondition = $currentclassname::instance();
// Currently conditions of the same type cannot be combined with each other.
if ($jsoncondition->id != $this->id
&& isset($currentcondition->overridable)
Expand Down
85 changes: 82 additions & 3 deletions classes/bo_availability/conditions/nooverlapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use mod_booking\bo_availability\bo_info;
use mod_booking\booking_option_settings;
use mod_booking\singleton_service;
use moodle_url;
use MoodleQuickForm;
use stdClass;

Expand Down Expand Up @@ -59,6 +60,41 @@ class nooverlapping implements bo_condition {
*/
private int $handling = MOD_BOOKING_COND_OVERLAPPING_HANDLING_EMPTY;

/**
* Storing overlapping options.
*
* @var array
*/
private array $overlappinganswers = [];

/**
* Singleton instance.
*
* @var object
*/
private static $instance = null;

/**
* C
*
*
*/
private function __construct() {
}

/**
* Singleton instance.
*
* @return object
*
*/
public static function instance(): object {
if (empty(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}

/**
* Get the condition id.
*
Expand Down Expand Up @@ -115,7 +151,7 @@ public function is_available(booking_option_settings $settings, int $userid, boo
isset($bookinginformation['iambooked'])
|| isset($bookinginformation['onwaitinglist'])
)
|| !$bookinganswer->is_overlapping($userid)
|| empty($this->overlappinganswers = $bookinganswer->is_overlapping($userid))
) {
$isavailable = true;
}
Expand Down Expand Up @@ -184,7 +220,7 @@ public function get_description(booking_option_settings $settings, $userid = nul

$isavailable = $this->is_available($settings, $userid, $not);

$description = $this->get_description_string($isavailable, $full, $settings);
$description = $this->get_description_string($isavailable, $full, $settings, $userid);

$handling = $this->return_handling_from_settings($settings);
$buttonclass = $handling == MOD_BOOKING_COND_OVERLAPPING_HANDLING_BLOCK
Expand Down Expand Up @@ -287,9 +323,15 @@ public function render_button(
* @param bool $isavailable
* @param bool $full
* @param booking_option_settings $settings
* @param int $userid
* @return string
*/
private function get_description_string(bool $isavailable, bool $full, booking_option_settings $settings): string {
private function get_description_string(
bool $isavailable,
bool $full,
booking_option_settings $settings,
int $userid = 0
): string {

$description = "";
if (
Expand All @@ -314,6 +356,43 @@ private function get_description_string(bool $isavailable, bool $full, booking_o
return $description;
}

/**
* Return a rendered string with links to bookingoption.
*
* @param string $identifier
* @param object $settings
* @param int $userid
*
* @return [type]
*
*/
private function get_string_with_url(string $identifier, object $settings, int $userid = 0) {
global $CFG, $USER;

$optionid = 0;
$string = "";
foreach ($this->overlappinganswers as $answer) {
if (empty($optionid = $answer->optionid)) {
continue;
}

$booking = singleton_service::get_instance_of_booking_by_optionid($optionid);
$bookinoption = singleton_service::get_instance_of_booking_option_settings($optionid);
if (empty($userid)) {
$userid = $USER->id;
}

$title = $bookinoption->text;
$url = new moodle_url($CFG->wwwroot . '/mod/booking/optionview.php', [
'cmid' => $booking->cmid,
'optionid' => $optionid,
]);
$url = $url->out(false);
$string .= '<div><a href="' . $url . '" >"' . $title . '" </a></div>';
}
return get_string($identifier, 'mod_booking', $string);
}

/**
* Set data function to add the right values to the form.
* @param stdClass $defaultvalues
Expand Down
Loading

0 comments on commit c3cbb5b

Please sign in to comment.