Example #1
0
                $timestart = make_timestamp($nextyear, $nextmonth, 1);
                $timeend = make_timestamp($nextyear, $nextmonth, calendar_days_in_month($nextmonth, $nextyear), 23, 59, 59);
                break;
            case 'recentupcoming':
                //Events in the last 5 or next 60 days
                $timestart = time() - 432000;
                $timeend = time() + 5184000;
                break;
        }
    } else {
        // Parameters given but incorrect, redirect back to export page
        redirect($CFG->wwwroot . '/calendar/export.php');
        die;
    }
}
$whereclause = calendar_sql_where($timestart, $timeend, $users, $groups, array_keys($courses), false);
if ($whereclause === false) {
    $events = array();
} else {
    $events = get_records_select('event', $whereclause, 'timestart');
}
if ($events === false) {
    $events = array();
}
$ical = new iCalendar();
$ical->add_property('method', 'PUBLISH');
foreach ($events as $event) {
    $ev = new iCalendar_event();
    $ev->add_property('summary', $event->name);
    $ev->add_property('description', $event->description);
    $ev->add_property('class', 'PUBLIC');
                $timestart = make_timestamp($nextyear, $nextmonth, 1);
                $timeend = make_timestamp($nextyear, $nextmonth, calendar_days_in_month($nextmonth, $nextyear), 23, 59, 59);
                break;
            case 'recentupcoming':
                //Events in the last 5 or next 60 days
                $timestart = time() - 432000;
                $timeend = time() + 5184000;
                break;
        }
    } else {
        // Parameters given but incorrect, redirect back to export page
        redirect($CFG->wwwroot . '/calendar/export.php');
        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();
}
$ical = new iCalendar();
$ical->add_property('method', 'PUBLISH');
foreach ($events as $event) {
    if (!empty($event->modulename)) {
        $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
        if (!groups_course_module_visible($cm)) {
            continue;
function calendar_show_month_detailed($m, $y, $courses, $groups, $users, $courseid)
{
    global $CFG, $SESSION, $USER, $CALENDARDAYS;
    global $day, $mon, $yr;
    $getvars = 'from=month&cal_d=' . $day . '&cal_m=' . $mon . '&cal_y=' . $yr;
    // For filtering
    $display =& new stdClass();
    $display->minwday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
    $display->maxwday = $display->minwday + 6;
    if (!empty($m) && !empty($y)) {
        $thisdate = usergetdate(time());
        // Time and day at the user's location
        if ($m == $thisdate['mon'] && $y == $thisdate['year']) {
            // Navigated to this month
            $date = $thisdate;
            $display->thismonth = true;
        } else {
            // Navigated to other month, let's do a nice trick and save us a lot of work...
            if (!checkdate($m, 1, $y)) {
                $date = array('mday' => 1, 'mon' => $thisdate['mon'], 'year' => $thisdate['year']);
                $display->thismonth = true;
            } else {
                $date = array('mday' => 1, 'mon' => $m, 'year' => $y);
                $display->thismonth = false;
            }
        }
    } else {
        $date = usergetdate(time());
        $display->thismonth = true;
    }
    // Fill in the variables we 're going to use, nice and tidy
    list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']);
    // This is what we want to display
    $display->maxdays = calendar_days_in_month($m, $y);
    $startwday = 0;
    if (get_user_timezone_offset() < 99) {
        // We 'll keep these values as GMT here, and offset them when the time comes to query the db
        $display->tstart = gmmktime(0, 0, 0, $m, 1, $y);
        // This is GMT
        $display->tend = gmmktime(23, 59, 59, $m, $display->maxdays, $y);
        // GMT
        $startwday = gmdate('w', $display->tstart);
        // $display->tstart is already GMT, so don't use date(): messes with server's TZ
    } else {
        // no timezone info specified
        $display->tstart = mktime(0, 0, 0, $m, 1, $y);
        $display->tend = mktime(23, 59, 59, $m, $display->maxdays, $y);
        $startwday = date('w', $display->tstart);
        // $display->tstart not necessarily GMT, so use date()
    }
    // Align the starting weekday to fall in our display range
    if ($startwday < $display->minwday) {
        $startwday += 7;
    }
    // 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');
    }
    // Extract information: events vs. time
    calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday);
    $text = '';
    if (!isguest() && !empty($USER->id) && calendar_user_can_add_event()) {
        $text .= '<div class="buttons"><form action="' . CALENDAR_URL . 'event.php" method="get">';
        $text .= '<div>';
        $text .= '<input type="hidden" name="action" value="new" />';
        $text .= '<input type="hidden" name="course" value="' . $courseid . '" />';
        $text .= '<input type="hidden" name="cal_m" value="' . $m . '" />';
        $text .= '<input type="hidden" name="cal_y" value="' . $y . '" />';
        $text .= '<input type="submit" value="' . get_string('newevent', 'calendar') . '" />';
        $text .= '</div></form></div>';
    }
    $text .= get_string('detailedmonthview', 'calendar') . ': ' . calendar_course_filter_selector($getvars);
    echo '<div class="header">' . $text . '</div>';
    echo '<div class="controls">';
    echo calendar_top_controls('month', array('id' => $courseid, 'm' => $m, 'y' => $y));
    echo '</div>';
    // Start calendar display
    echo '<table class="calendarmonth"><tr class="weekdays">';
    // Begin table. First row: day names
    // Print out the names of the weekdays
    for ($i = $display->minwday; $i <= $display->maxwday; ++$i) {
        // This uses the % operator to get the correct weekday no matter what shift we have
        // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
        echo '<th scope="col">' . get_string($CALENDARDAYS[$i % 7], 'calendar') . '</th>';
    }
    echo '</tr><tr>';
    // End of day names; prepare for day numbers
    // For the table display. $week is the row; $dayweek is the column.
    $week = 1;
    $dayweek = $startwday;
    // Paddding (the first week may have blank days in the beginning)
    for ($i = $display->minwday; $i < $startwday; ++$i) {
        echo '<td>&nbsp;</td>' . "\n";
    }
    // Now display all the calendar
    for ($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
        if ($dayweek > $display->maxwday) {
            // We need to change week (table row)
            echo "</tr>\n<tr>";
            $dayweek = $display->minwday;
            ++$week;
        }
        // Reset vars
        $cell = '';
        $dayhref = calendar_get_link_href(CALENDAR_URL . 'view.php?view=day&amp;course=' . $courseid . '&amp;', $day, $m, $y);
        if (CALENDAR_WEEKEND & 1 << $dayweek % 7) {
            // Weekend. This is true no matter what the exact range is.
            $class = 'weekend';
        } else {
            // Normal working day.
            $class = '';
        }
        // Special visual fx if an event is defined
        if (isset($eventsbyday[$day])) {
            if (isset($typesbyday[$day]['startglobal'])) {
                $class .= ' event_global';
            } else {
                if (isset($typesbyday[$day]['startcourse'])) {
                    $class .= ' event_course';
                } else {
                    if (isset($typesbyday[$day]['startgroup'])) {
                        $class .= ' event_group';
                    } else {
                        if (isset($typesbyday[$day]['startuser'])) {
                            $class .= ' event_user';
                        }
                    }
                }
            }
            if (count($eventsbyday[$day]) == 1) {
                $title = get_string('oneevent', 'calendar');
            } else {
                $title = get_string('manyevents', 'calendar', count($eventsbyday[$day]));
            }
            $cell = '<div class="day"><a href="' . $dayhref . '" title="' . $title . '">' . $day . '</a></div>';
        } else {
            $cell = '<div class="day">' . $day . '</div>';
        }
        // Special visual fx if an event spans many days
        if (isset($typesbyday[$day]['durationglobal'])) {
            $class .= ' duration_global';
        } else {
            if (isset($typesbyday[$day]['durationcourse'])) {
                $class .= ' duration_course';
            } else {
                if (isset($typesbyday[$day]['durationgroup'])) {
                    $class .= ' duration_group';
                } else {
                    if (isset($typesbyday[$day]['durationuser'])) {
                        $class .= ' duration_user';
                    }
                }
            }
        }
        // Special visual fx for today
        if ($display->thismonth && $day == $d) {
            $class .= ' today';
        }
        // Just display it
        if (!empty($class)) {
            $class = ' class="' . trim($class) . '"';
        }
        echo '<td' . $class . '>' . $cell;
        if (isset($eventsbyday[$day])) {
            echo '<ul class="events-new">';
            foreach ($eventsbyday[$day] as $eventindex) {
                echo '<li><a href="' . $dayhref . '#event_' . $events[$eventindex]->id . '">' . format_string($events[$eventindex]->name, true) . '</a></li>';
            }
            echo '</ul>';
        }
        if (isset($durationbyday[$day])) {
            echo '<ul class="events-underway">';
            foreach ($durationbyday[$day] as $eventindex) {
                echo '<li>[' . format_string($events[$eventindex]->name, true) . ']</li>';
            }
            echo '</ul>';
        }
        echo "</td>\n";
    }
    // Paddding (the last week may have blank days at the end)
    for ($i = $dayweek; $i <= $display->maxwday; ++$i) {
        echo '<td>&nbsp;</td>';
    }
    echo "</tr>\n";
    // Last row ends
    echo "</table>\n";
    // Tabular display of days ends
    // OK, now for the filtering display
    echo '<div class="filters"><table><tr>';
    // Global events
    if ($SESSION->cal_show_global) {
        echo '<td class="event_global" style="width: 8px;"></td><td><strong>' . get_string('globalevents', 'calendar') . ':</strong> ';
        echo get_string('shown', 'calendar') . ' (<a href="' . CALENDAR_URL . 'set.php?var=showglobal&amp;' . $getvars . '">' . get_string('clickhide', 'calendar') . '</a>)</td>' . "\n";
    } else {
        echo '<td style="width: 8px;"></td><td><strong>' . get_string('globalevents', 'calendar') . ':</strong> ';
        echo get_string('hidden', 'calendar') . ' (<a href="' . CALENDAR_URL . 'set.php?var=showglobal&amp;' . $getvars . '">' . get_string('clickshow', 'calendar') . '</a>)</td>' . "\n";
    }
    // Course events
    if (!empty($SESSION->cal_show_course)) {
        echo '<td class="event_course" style="width: 8px;"></td><td><strong>' . get_string('courseevents', 'calendar') . ':</strong> ';
        echo get_string('shown', 'calendar') . ' (<a href="' . CALENDAR_URL . 'set.php?var=showcourses&amp;' . $getvars . '">' . get_string('clickhide', 'calendar') . '</a>)</td>' . "\n";
    } else {
        echo '<td style="width: 8px;"></td><td><strong>' . get_string('courseevents', 'calendar') . ':</strong> ';
        echo get_string('hidden', 'calendar') . ' (<a href="' . CALENDAR_URL . 'set.php?var=showcourses&amp;' . $getvars . '">' . get_string('clickshow', 'calendar') . '</a>)</td>' . "\n";
    }
    echo "</tr>\n";
    if (!empty($USER->id) && !isguest()) {
        echo '<tr>';
        // Group events
        if ($SESSION->cal_show_groups) {
            echo '<td class="event_group" style="width: 8px;"></td><td><strong>' . get_string('groupevents', 'calendar') . ':</strong> ';
            echo get_string('shown', 'calendar') . ' (<a href="' . CALENDAR_URL . 'set.php?var=showgroups&amp;' . $getvars . '">' . get_string('clickhide', 'calendar') . '</a>)</td>' . "\n";
        } else {
            echo '<td style="width: 8px;"></td><td><strong>' . get_string('groupevents', 'calendar') . ':</strong> ';
            echo get_string('hidden', 'calendar') . ' (<a href="' . CALENDAR_URL . 'set.php?var=showgroups&amp;' . $getvars . '">' . get_string('clickshow', 'calendar') . '</a>)</td>' . "\n";
        }
        // User events
        if ($SESSION->cal_show_user) {
            echo '<td class="event_user" style="width: 8px;"></td><td><strong>' . get_string('userevents', 'calendar') . ':</strong> ';
            echo get_string('shown', 'calendar') . ' (<a href="' . CALENDAR_URL . 'set.php?var=showuser&amp;' . $getvars . '">' . get_string('clickhide', 'calendar') . '</a>)</td>' . "\n";
        } else {
            echo '<td style="width: 8px;"></td><td><strong>' . get_string('userevents', 'calendar') . ':</strong> ';
            echo get_string('hidden', 'calendar') . ' (<a href="' . CALENDAR_URL . 'set.php?var=showuser&amp;' . $getvars . '">' . get_string('clickshow', 'calendar') . '</a>)</td>' . "\n";
        }
        echo "</tr>\n";
    }
    echo '</table></div>';
}
Example #4
0
function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxevents, $fromtime = 0)
{
    global $CFG;
    $display =& new stdClass();
    $display->range = $daysinfuture;
    // How many days in the future we 'll look
    $display->maxevents = $maxevents;
    $output = array();
    // Prepare "course caching", since it may save us a lot of queries
    $coursecache = array();
    $processed = 0;
    $now = time();
    // We 'll need this later
    $usermidnighttoday = usergetmidnight($now);
    if ($fromtime) {
        $display->tstart = $fromtime;
    } else {
        $display->tstart = $usermidnighttoday;
    }
    // This works correctly with respect to the user's DST, but it is accurate
    // only because $fromtime is always the exact midnight of some day!
    $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');
    }
    // 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
    // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra
    // arguments to this function.
    $morehref = '';
    if (!empty($courses)) {
        $courses = array_diff($courses, array(SITEID));
        if (count($courses) == 1) {
            $morehref = '&amp;course=' . reset($courses);
        }
    }
    if ($events !== false) {
        foreach ($events as $event) {
            if (!empty($event->modulename)) {
                $mod = get_coursemodule_from_instance($event->modulename, $event->instance);
                if (!groups_course_module_visible($mod)) {
                    continue;
                }
            }
            if ($processed >= $display->maxevents) {
                break;
            }
            $event->time = calendar_format_event_time($event, $now, $morehref);
            $output[] = $event;
            ++$processed;
        }
    }
    return $output;
}