There are only 4 calls to the calendar_sql_where() function and each call is followed by identical code to select calendar events. This is not good practice and the accompanying patch embodies the calendar event selection within the renamed (calendar_get_events()) function, always returning an array (even if empty) of events.
Furthermore, there are issues with regards to selecting calendar events on installations with large numbers of users, groups and/or courses which will require changes to the way events are selected anyway, so it makes sense for this code to just be in one place.