@@ -61,16 +61,18 @@ public static function create(stdClass $record) {
6161 return new $ class ($ record ->id , $ record );
6262 }
6363 // By default, we return the text controller.
64- return new \ local_wunderbyte_table \ local \ customfield \ field \ text \ wbt_field_controller ($ record ->id , $ record );
64+ return new wbt_field_controller ($ record ->id , $ record );
6565 }
6666
6767 /**
6868 * Create instances of field controllers for all provided customfield shortnames.
6969 *
7070 * @param array $shortnames array of customfield shortnames
71+ * @param string $component optional component to filter by (e.g. 'mod_booking')
72+ * @param string $area optional area to filter by (e.g. 'booking')
7173 * @return void
7274 */
73- public static function instantiate_by_shortnames (array $ shortnames ) {
75+ public static function instantiate_by_shortnames (array $ shortnames, string $ component = '' , string $ area = '' ) {
7476 global $ DB ;
7577
7678 if (empty ($ shortnames )) {
@@ -79,15 +81,30 @@ public static function instantiate_by_shortnames(array $shortnames) {
7981
8082 [$ inorequal , $ inparams ] = $ DB ->get_in_or_equal ($ shortnames , SQL_PARAMS_NAMED );
8183
84+ $ where = "cf.shortname $ inorequal " ;
85+ if (!empty ($ component )) {
86+ $ inparams ['cfcomponent ' ] = $ component ;
87+ $ where .= ' AND cc.component = :cfcomponent ' ;
88+ }
89+ if (!empty ($ area )) {
90+ $ inparams ['cfarea ' ] = $ area ;
91+ $ where .= ' AND cc.area = :cfarea ' ;
92+ }
93+
94+ // Order by cf.id DESC so the newest field per shortname is processed first.
8295 $ sql = "SELECT cf.shortname AS filtercolumn, cf.*
83- FROM {customfield_field} cf
84- WHERE cf.shortname $ inorequal " ;
96+ FROM {customfield_field} cf
97+ JOIN {customfield_category} cc ON cf.categoryid = cc.id
98+ WHERE $ where
99+ ORDER BY cf.id DESC " ;
85100 $ records = $ DB ->get_records_sql ($ sql , $ inparams );
86101
87102 foreach ($ records as $ record ) {
88- // We only add the instance, if a field controller exists.
89- if ($ instance = self ::create ($ record )) {
90- self ::$ instances [$ record ->shortname ] = $ instance ;
103+ // Only take the first (newest) instance per shortname.
104+ if (!isset (self ::$ instances [$ record ->shortname ])) {
105+ if ($ instance = self ::create ($ record )) {
106+ self ::$ instances [$ record ->shortname ] = $ instance ;
107+ }
91108 }
92109 }
93110 }
@@ -96,26 +113,43 @@ public static function instantiate_by_shortnames(array $shortnames) {
96113 * Get the field controller from the singleton $instances.
97114 *
98115 * @param string $shortname shortname of field controller customfield
116+ * @param string $component optional component to filter by (e.g. 'mod_booking')
117+ * @param string $area optional area to filter by (e.g. 'booking')
99118 * @return wbt_field_controller_base the field controller for the customfield record
100119 */
101- public static function get_instance_by_shortname (string $ shortname ) {
120+ public static function get_instance_by_shortname (string $ shortname, string $ component = '' , string $ area = '' ) {
102121 if (!empty (self ::$ instances [$ shortname ])) {
103122 return self ::$ instances [$ shortname ];
104123 } else {
105124 global $ DB ;
106- $ sql = "SELECT cf.shortname AS filtercolumn, cf.*
107- FROM {customfield_field} cf
108- WHERE cf.shortname = :shortname " ;
125+
126+ $ where = 'cf.shortname = :shortname ' ;
109127 $ params = ['shortname ' => $ shortname ];
110- if ($ record = $ DB ->get_record_sql ($ sql , $ params )) {
111- // We only add the instance, if a field controller exists.
128+
129+ if (!empty ($ component )) {
130+ $ params ['cfcomponent ' ] = $ component ;
131+ $ where .= ' AND cc.component = :cfcomponent ' ;
132+ }
133+ if (!empty ($ area )) {
134+ $ params ['cfarea ' ] = $ area ;
135+ $ where .= ' AND cc.area = :cfarea ' ;
136+ }
137+
138+ // Order by cf.id DESC and take the first record (newest) when not fully scoped.
139+ $ sql = "SELECT cf.shortname AS filtercolumn, cf.*
140+ FROM {customfield_field} cf
141+ JOIN {customfield_category} cc ON cf.categoryid = cc.id
142+ WHERE $ where
143+ ORDER BY cf.id DESC " ;
144+
145+ if ($ record = $ DB ->get_record_sql ($ sql , $ params , IGNORE_MULTIPLE )) {
112146 if ($ instance = self ::create ($ record )) {
113147 self ::$ instances [$ record ->shortname ] = $ instance ;
114148 return $ instance ;
115149 }
116150 }
117151 }
118- // Fallback: By default, we return text controller.
152+ // Fallback: By default, we return the text controller.
119153 return new wbt_field_controller ();
120154 }
121155}
0 commit comments