diff --git a/classes/shortcodes.php b/classes/shortcodes.php index 65ce0f94c..ffef4c5fb 100644 --- a/classes/shortcodes.php +++ b/classes/shortcodes.php @@ -231,8 +231,12 @@ public static function courselist($shortcode, $args, $content, $env, $next) { $wherearray['bookingid'] = (int)$booking->id; + // Additional where condition for both card and list views. + $additionalwhere = self::set_wherearray_from_arguments($args, $wherearray) ?? ''; + list($fields, $from, $where, $params, $filter) = - booking::get_options_filter_sql(0, 0, '', null, null, [], $wherearray); + booking::get_options_filter_sql(0, 0, '', null, null, [], $wherearray, null, + [MOD_BOOKING_STATUSPARAM_BOOKED], $additionalwhere); // By default, we do not show booking options that lie in the past. // Shortcode arg values get transmitted as string, so also check for "false" and "0". @@ -833,4 +837,63 @@ private static function apply_bookinginstance_filter(&$table) { $instancefilter->add_options($filterarray); $table->add_filter($instancefilter); } + + /** + * Modify there wherearray via arguments. + * + * @param array $args + * + * @return string + * + */ + private static function set_wherearray_from_arguments(array &$args, &$wherearray) { + + global $DB; + + $customfields = booking_handler::get_customfields(); + // Set given customfields (shortnames) as arguments. + $fields = []; + $additonalwhere = ''; + if (!empty($customfields) && !empty($args)) { + foreach ($args as $key => $value) { + foreach ($customfields as $customfield) { + if ($customfield->shortname == $key) { + $configdata = json_decode($customfield->configdata ?? '[]'); + + if (!empty($configdata->multiselect)) { + if (!empty($additonalwhere)) { + $additonalwhere .= " AND "; + } + + $values = explode(',', $value); + + if (!empty($values)) { + $additonalwhere .= " ( "; + } + + foreach ($values as $vkey => $vvalue) { + + $additonalwhere .= $vkey > 0 ? ' OR ' : ''; + $vvalue = "'%$vvalue%'"; + $additonalwhere .= " $key LIKE $vvalue "; + } + + if (!empty($values)) { + $additonalwhere .= " ) "; + } + + } else { + $argument = strip_tags($value); + $argument = trim($argument); + $wherearray[$key] = $argument; + } + + break; + } + } + } + } + + return $additonalwhere; + } }