From 24da81d5e413969be9bbfeefc94e4feb5cb4cbf6 Mon Sep 17 00:00:00 2001 From: Magdalena Holczik Date: Thu, 23 Jan 2025 12:13:59 +0100 Subject: [PATCH] Improvement: Filter and sorting for bookable/free places as settings to be turned on or off (Wunderbyte-GmbH/moodle-mod_booking#622) --- classes/shortcodes.php | 61 +++++++++++++++++++----------------------- lang/de/local_musi.php | 2 ++ lang/en/local_musi.php | 2 ++ settings.php | 22 +++++++++++++++ 4 files changed, 53 insertions(+), 34 deletions(-) diff --git a/classes/shortcodes.php b/classes/shortcodes.php index 46ac84f..6ffdf5f 100644 --- a/classes/shortcodes.php +++ b/classes/shortcodes.php @@ -758,15 +758,18 @@ private static function inittableforcourses($booking) { * */ private static function add_standardfilters(&$table) { - $callbackfilter = new callback('bookable', get_string('bookable', 'local_musi')); - $callbackfilter->add_options([ - 0 => get_string('notbookable', 'local_musi'), - 1 => get_string('bookable', 'local_musi'), - ]); - // This filter expects a record from booking options table. - // We check if it is bookable for the user. - $callbackfilter->define_callbackfunction('local_musi\shortcodes::filter_bookable'); - $table->add_filter($callbackfilter); + // Turn on or off. + if (get_config('local_musi', 'musishortcodesshowfilterbookable')) { + $callbackfilter = new callback('bookable', get_string('bookable', 'local_musi')); + $callbackfilter->add_options([ + 0 => get_string('notbookable', 'local_musi'), + 1 => get_string('bookable', 'local_musi'), + ]); + // This filter expects a record from booking options table. + // We check if it is bookable for the user. + $callbackfilter->define_callbackfunction('local_musi\shortcodes::filter_bookable'); + $table->add_filter($callbackfilter); + } $standardfilter = new standardfilter('sport', get_string('sport', 'local_musi')); $table->add_filter($standardfilter); @@ -861,33 +864,23 @@ private static function set_table_options_from_arguments(&$table, $args) { 'sportsdivision' => get_string('sportsdivision', 'local_musi'), 'sport' => get_string('sport', 'local_musi'), 'location' => get_string('location', 'local_musi'), - // 'bookedplaces' => get_string('freeplaces', 'local_musi'), ]; - // As the "free places sorting" functionality is not yet tested and not yet integrated into Wunderbyte table... - // ... we currently comment this out. We'll add it later on... - /* - $sortbycallback = new \local_wunderbyte_table\local\sortables\types\callback( - 'freeplaces', - get_string('freeplaces', 'local_musi') - ); - // $sortbycallback->define_callbackfunction('local_musi\shortcodes::sort_freeplaces'); - // $table->add_sortable($sortbycallback); - */ - - $standardsortable = new \local_wunderbyte_table\local\sortables\types\standardsortable( - 'freeplaces', - get_string('freeplaces', 'local_musi') - ); - $select = '(SELECT COALESCE(NULLIF(s1.maxanswers, 0), 999999) - COUNT(ba.id) - FROM {booking_answers} ba - WHERE ba.optionid = s1.id AND ba.waitinglist < 3) AS freeplaces'; - $from = ''; - $where = ''; - $standardsortable->define_sql($select, $from, $where); - - $standardsortable->define_cache('mod_booking', 'bookedusertable'); - $table->add_sortable($standardsortable); + if (get_config('local_musi', 'musishortcodesshowsortingfreeplaces')) { + $standardsortable = new \local_wunderbyte_table\local\sortables\types\standardsortable( + 'freeplaces', + get_string('freeplaces', 'local_musi') + ); + $select = '(SELECT COALESCE(NULLIF(s1.maxanswers, 0), 999999) - COUNT(ba.id) + FROM {booking_answers} ba + WHERE ba.optionid = s1.id AND ba.waitinglist < 3) AS freeplaces'; + $from = ''; + $where = ''; + $standardsortable->define_sql($select, $from, $where); + + $standardsortable->define_cache('mod_booking', 'bookedusertable'); + $table->add_sortable($standardsortable); + } if (get_config('local_musi', 'musishortcodesshowstart')) { $sortablecolumns['coursestarttime'] = get_string('coursestarttime', 'mod_booking'); diff --git a/lang/de/local_musi.php b/lang/de/local_musi.php index d814316..6316fd8 100644 --- a/lang/de/local_musi.php +++ b/lang/de/local_musi.php @@ -80,7 +80,9 @@ $string['musishortcodes:showbookablefrom'] = '"Buchbar ab" anzeigen'; $string['musishortcodes:showbookableuntil'] = '"Buchbar bis" anzeigen'; $string['musishortcodes:showfiltercoursetime'] = 'Filter "Kurs beginnt um" anzeigen'; +$string['musishortcodes:showfilterbookable'] = 'Filter "Buchbar" anzeigen'; $string['musishortcodes:showfilterbookingtime'] = 'Filter "Anmeldezeiten" anzeigen'; +$string['musishortcodes:showsortingfreeplaces'] = 'Sortiermöglichkeit "Freie Plätze" anzeigen'; // General strings. $string['campaigns'] = 'Kampagnen'; diff --git a/lang/en/local_musi.php b/lang/en/local_musi.php index 888e561..db3bbeb 100644 --- a/lang/en/local_musi.php +++ b/lang/en/local_musi.php @@ -79,7 +79,9 @@ $string['musishortcodes:showbookablefrom'] = 'Show "Bookable from"'; $string['musishortcodes:showbookableuntil'] = 'Show "Bookable until"'; $string['musishortcodes:showfiltercoursetime'] = 'Show filter "Course starts at"'; +$string['musishortcodes:showfilterbookable'] = 'Show filter "Bookable"'; $string['musishortcodes:showfilterbookingtime'] = 'Show filter "Booking time"'; +$string['musishortcodes:showsortingfreeplaces'] = 'Show sorting "Free places"'; // General strings. $string['campaigns'] = 'Campaigns'; diff --git a/settings.php b/settings.php index bd85edd..4948f7c 100644 --- a/settings.php +++ b/settings.php @@ -163,6 +163,28 @@ }); $settings->add($showfilterbookingtimesetting); + $showfilterbookable = new admin_setting_configcheckbox( + 'local_musi/musishortcodesshowfilterbookable', + get_string('musishortcodes:showfilterbookable', 'local_musi'), + '', + 0 + ); + $showfilterbookable->set_updatedcallback(function () { + cache_helper::purge_by_event('setbackoptionstable'); + }); + $settings->add($showfilterbookable); + + $showsortingfreeplaces = new admin_setting_configcheckbox( + 'local_musi/musishortcodesshowsortingfreeplaces', + get_string('musishortcodes:showsortingfreeplaces', 'local_musi'), + '', + 0 + ); + $showsortingfreeplaces->set_updatedcallback(function () { + cache_helper::purge_by_event('setbackoptionstable'); + }); + $settings->add($showsortingfreeplaces); + $collapsedescriptionoptions = [ 0 => get_string('collapsedescriptionoff', 'local_musi'), 100 => "100",