Skip to content

Commit

Permalink
MDL-12313 basically rationalising getting calendar events function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
dwoolhead committed Nov 26, 2007
1 parent 0f6475b commit 8263f80
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 36 deletions.
12 changes: 1 addition & 11 deletions calendar/export_execute.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,7 @@
die();
}
}
$whereclause = calendar_sql_where($timestart, $timeend, $include_user ? array($user->id) : false, false, array_keys($courses), false);
if($whereclause === false) {
$events = array();
}
else {
$events = get_records_select('event', $whereclause, 'timestart');
}

if ($events === false) {
$events = array();
}
$events = calendar_get_events($timestart, $timeend, $include_user ? array($user->id) : false, false, array_keys($courses), false);

$ical = new iCalendar;
$ical->add_property('method', 'PUBLISH');
Expand Down
40 changes: 22 additions & 18 deletions calendar/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,11 @@ function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_y


// Get the events matching our criteria. Don't forget to offset the timestamps for the user's TZ!
$whereclause = calendar_sql_where(
$events = calendar_get_events(
usertime($display->tstart) - dst_offset_on($display->tstart),
usertime($display->tend) - dst_offset_on($display->tend),
$users, $groups, $courses);

if($whereclause === false) {
$events = array();
}
else {
$events = get_records_select('event', $whereclause, 'timestart');
}

// Set event course class for course events
if (!empty($events)) {
foreach ($events as $eventid => $event) {
Expand Down Expand Up @@ -396,12 +389,7 @@ function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxeve
$display->tend = usergetmidnight($display->tstart + DAYSECS * $display->range + 3 * HOURSECS) - 1;

// Get the events matching our criteria
$whereclause = calendar_sql_where($display->tstart, $display->tend, $users, $groups, $courses);
if ($whereclause === false) {
$events = false;
} else {
$events = get_records_select('event', $whereclause, 'timestart');
}
$events = calendar_get_events($display->tstart, $display->tend, $users, $groups, $courses);

// This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after
// possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month
Expand Down Expand Up @@ -570,11 +558,23 @@ function calendar_print_event($event) {

}

function calendar_sql_where($tstart, $tend, $users, $groups, $courses, $withduration=true, $ignorehidden=true) {
/**
* Get calendar events
* @param int $tstart Start time of time range for events
* @param int $tend End time of time range for events
* @param array/int/boolean $users array of users, user id or boolean for all/no user events
* @param array/int/boolean $groups array of groups, group id or boolean for all/no group events
* @param array/int/boolean $courses array of courses, course id or boolean for all/no course events
* @param boolean $withduration whether only events starting within time range selected
* or events in progress/already started selected as well
* @param boolean $ignorehidden whether to select only visible events or all events
* @return array of selected events or an empty array if there aren't any (or there was an error)
*/
function calendar_get_events($tstart, $tend, $users, $groups, $courses, $withduration=true, $ignorehidden=true) {
$whereclause = '';
// Quick test
if(is_bool($users) && is_bool($groups) && is_bool($courses)) {
return false;
return array();
}

if(is_array($users) && !empty($users)) {
Expand Down Expand Up @@ -642,7 +642,7 @@ function calendar_sql_where($tstart, $tend, $users, $groups, $courses, $withdura
// events no matter what. Allowing the code to proceed might return a completely
// valid query with only time constraints, thus selecting ALL events in that time frame!
if(empty($whereclause)) {
return false;
return array();
}

if($withduration) {
Expand All @@ -664,7 +664,11 @@ function calendar_sql_where($tstart, $tend, $users, $groups, $courses, $withdura
$whereclause .= ' AND visible = 1';
}

return $whereclause;
$events = get_records_select('event', $whereclause, 'timestart');
if ($events === false) {
$events = array();
}
return $events;
}

function calendar_top_controls($type, $data) {
Expand Down
8 changes: 1 addition & 7 deletions calendar/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,7 @@ function calendar_show_month_detailed($m, $y, $courses, $groups, $users, $course
}

// Get events from database
$whereclause = calendar_sql_where(usertime($display->tstart), usertime($display->tend), $users, $groups, $courses);
if($whereclause === false) {
$events = array();
}
else {
$events = get_records_select('event', $whereclause, 'timestart');
}
$events = calendar_get_events(usertime($display->tstart), usertime($display->tend), $users, $groups, $courses);
if (!empty($events)) {
foreach($events as $eventid => $event) {
if (!empty($event->modulename)) {
Expand Down

0 comments on commit 8263f80

Please sign in to comment.