return $calendartype->get_starting_weekday();
}
-/**
- * Generates the HTML for a miniature calendar.
- *
- * @param array $courses list of course to list events from
- * @param array $groups list of group
- * @param array $users user's info
- * @param int|bool $calmonth calendar month in numeric, default is set to false
- * @param int|bool $calyear calendar month in numeric, default is set to false
- * @param string|bool $placement the place/page the calendar is set to appear - passed on the the controls function
- * @param int|bool $courseid id of the course the calendar is displayed on - passed on the the controls function
- * @param int $time the unixtimestamp representing the date we want to view, this is used instead of $calmonth
- * and $calyear to support multiple calendars
- * @return string $content return html table for mini calendar
- */
-function calendar_get_mini($courses, $groups, $users, $calmonth = false, $calyear = false, $placement = false,
- $courseid = false, $time = 0) {
- global $CFG, $OUTPUT;
-
- // Get the calendar type we are using.
- $calendartype = \core_calendar\type_factory::get_calendar_instance();
-
- $display = new \stdClass;
-
- // Assume we are not displaying this month for now.
- $display->thismonth = false;
-
- $content = '';
-
- // Do this check for backwards compatibility.
- // The core should be passing a timestamp rather than month and year.
- // If a month and year are passed they will be in Gregorian.
- if (!empty($calmonth) && !empty($calyear)) {
- // Ensure it is a valid date, else we will just set it to the current timestamp.
- if (checkdate($calmonth, 1, $calyear)) {
- $time = make_timestamp($calyear, $calmonth, 1);
- } else {
- $time = time();
- }
- $date = usergetdate($time);
- if ($calmonth == $date['mon'] && $calyear == $date['year']) {
- $display->thismonth = true;
- }
- // We can overwrite date now with the date used by the calendar type,
- // if it is not Gregorian, otherwise there is no need as it is already in Gregorian.
- if ($calendartype->get_name() != 'gregorian') {
- $date = $calendartype->timestamp_to_date_array($time);
- }
- } else if (!empty($time)) {
- // Get the specified date in the calendar type being used.
- $date = $calendartype->timestamp_to_date_array($time);
- $thisdate = $calendartype->timestamp_to_date_array(time());
- if ($date['month'] == $thisdate['month'] && $date['year'] == $thisdate['year']) {
- $display->thismonth = true;
- // If we are the current month we want to set the date to the current date, not the start of the month.
- $date = $thisdate;
- }
- } else {
- // Get the current date in the calendar type being used.
- $time = time();
- $date = $calendartype->timestamp_to_date_array($time);
- $display->thismonth = true;
- }
-
- list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display.
-
- // Get Gregorian date for the start of the month.
- $gregoriandate = $calendartype->convert_to_gregorian($date['year'], $date['mon'], 1);
-
- // Store the gregorian date values to be used later.
- list($gy, $gm, $gd, $gh, $gmin) = array($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
- $gregoriandate['hour'], $gregoriandate['minute']);
-
- // Get the max number of days in this month for this calendar type.
- $display->maxdays = calendar_days_in_month($m, $y);
- // Get the starting week day for this month.
- $startwday = dayofweek(1, $m, $y);
- // Get the days in a week.
- $daynames = calendar_get_days();
- // Store the number of days in a week.
- $numberofdaysinweek = $calendartype->get_num_weekdays();
-
- // Set the min and max weekday.
- $display->minwday = calendar_get_starting_weekday();
- $display->maxwday = $display->minwday + ($numberofdaysinweek - 1);
-
- // These are used for DB queries, so we want unixtime, so we need to use Gregorian dates.
- $display->tstart = make_timestamp($gy, $gm, $gd, $gh, $gmin, 0);
- $display->tend = $display->tstart + ($display->maxdays * DAYSECS) - 1;
-
- // Align the starting weekday to fall in our display range.
- // This is simple, not foolproof.
- if ($startwday < $display->minwday) {
- $startwday += $numberofdaysinweek;
- }
-
- // Get the events matching our criteria. Don't forget to offset the timestamps for the user's TZ.
- $events = calendar_get_legacy_events($display->tstart, $display->tend, $users, $groups, $courses);
-
- // Set event course class for course events.
- if (!empty($events)) {
- foreach ($events as $eventid => $event) {
- if (!empty($event->modulename)) {
- $instances = get_fast_modinfo($event->courseid)->get_instances_of($event->modulename);
- if (empty($instances[$event->instance]->uservisible)) {
- unset($events[$eventid]);
- }
- }
- }
- }
-
- // 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.
- $hrefparams = array();
- if (!empty($courses)) {
- $courses = array_diff($courses, array(SITEID));
- if (count($courses) == 1) {
- $hrefparams['course'] = reset($courses);
- }
- }
-
- // We want to have easy access by day, since the display is on a per-day basis.
- calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday, $courses);
-
- // Accessibility: added summary and <abbr> elements.
- $summary = get_string('calendarheading', 'calendar', userdate($display->tstart, get_string('strftimemonthyear')));
- // Begin table.
- $content .= '<table class="minicalendar calendartable" summary="' . $summary . '">';
- if (($placement !== false) && ($courseid !== false)) {
- $content .= '<caption>' . calendar_top_controls($placement,
- array('id' => $courseid, 'time' => $time)) . '</caption>';
- }
- $content .= '<tr class="weekdays">'; // Header row: day names.
-
- // Print out the names of the weekdays.
- for ($i = $display->minwday; $i <= $display->maxwday; $i++) {
- $pos = $i % $numberofdaysinweek;
- $content .= '<th scope="col"><abbr title="' . $daynames[$pos]['fullname'] . '">' .
- $daynames[$pos]['shortname'] . "</abbr></th>\n";
- }
-
- $content .= '</tr><tr>'; // End of day names; prepare for day numbers.
-
- // For the table display. $week is the row; $dayweek is the column.
- $dayweek = $startwday;
-
- // Padding (the first week may have blank days in the beginning).
- for ($i = $display->minwday; $i < $startwday; ++$i) {
- $content .= '<td class="dayblank"> </td>' ."\n";
- }
-
- $weekend = CALENDAR_DEFAULT_WEEKEND;
- if (isset($CFG->calendar_weekend)) {
- $weekend = intval($CFG->calendar_weekend);
- }
-
- // Now display all the calendar.
- $daytime = strtotime('-1 day', $display->tstart);
- for ($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
- $cellattributes = array();
- $daytime = strtotime('+1 day', $daytime);
- if ($dayweek > $display->maxwday) {
- // We need to change week (table row).
- $content .= '</tr><tr>';
- $dayweek = $display->minwday;
- }
-
- // Reset vars.
- if ($weekend & (1 << ($dayweek % $numberofdaysinweek))) {
- // Weekend. This is true no matter what the exact range is.
- $class = 'weekend day';
- } else {
- // Normal working day.
- $class = 'day';
- }
-
- $eventids = array();
- if (!empty($eventsbyday[$day])) {
- $eventids = $eventsbyday[$day];
- }
-
- if (!empty($durationbyday[$day])) {
- $eventids = array_unique(array_merge($eventids, $durationbyday[$day]));
- }
-
- $finishclass = false;
-
- if (!empty($eventids)) {
- // There is at least one event on this day.
- $class .= ' hasevent';
- $hrefparams['view'] = 'day';
- $dayhref = calendar_get_link_href(new \moodle_url(CALENDAR_URL . 'view.php', $hrefparams), 0, 0, 0, $daytime);
-
- $popupcontent = '';
- foreach ($eventids as $eventid) {
- if (!isset($events[$eventid])) {
- continue;
- }
- $event = new \calendar_event($events[$eventid]);
- $popupalt = '';
- $component = 'moodle';
- if (!empty($event->modulename)) {
- $popupicon = 'icon';
- $popupalt = $event->modulename;
- $component = $event->modulename;
- } else if ($event->courseid == SITEID) { // Site event.
- $popupicon = 'i/siteevent';
- } else if ($event->courseid != 0 && $event->courseid != SITEID
- && $event->groupid == 0) { // Course event.
- $popupicon = 'i/courseevent';
- } else if ($event->groupid) { // Group event.
- $popupicon = 'i/groupevent';
- } else { // Must be a user event.
- $popupicon = 'i/userevent';
- }
-
- if ($event->timeduration) {
- $startdate = $calendartype->timestamp_to_date_array($event->timestart);
- $enddate = $calendartype->timestamp_to_date_array($event->timestart + $event->timeduration - 1);
- if ($enddate['mon'] == $m && $enddate['year'] == $y && $enddate['mday'] == $day) {
- $finishclass = true;
- }
- }
-
- $dayhref->set_anchor('event_' . $event->id);
-
- $popupcontent .= \html_writer::start_tag('div');
- $popupcontent .= $OUTPUT->pix_icon($popupicon, $popupalt, $component);
- // Show ical source if needed.
- if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
- $a = new \stdClass();
- $a->name = format_string($event->name, true);
- $a->source = $event->subscription->name;
- $name = get_string('namewithsource', 'calendar', $a);
- } else {
- if ($finishclass) {
- $samedate = $startdate['mon'] == $enddate['mon'] &&
- $startdate['year'] == $enddate['year'] &&
- $startdate['mday'] == $enddate['mday'];
-
- if ($samedate) {
- $name = format_string($event->name, true);
- } else {
- $name = format_string($event->name, true) . ' (' . get_string('eventendtime', 'calendar') . ')';
- }
- } else {
- $name = format_string($event->name, true);
- }
- }
- // Include course's shortname into the event name, if applicable.
- if (!empty($event->courseid) && $event->courseid !== SITEID) {
- $course = get_course($event->courseid);
- $eventnameparams = (object)[
- 'name' => $name,
- 'course' => format_string($course->shortname, true, array('context' => $event->context))
- ];
- $name = get_string('eventnameandcourse', 'calendar', $eventnameparams);
- }
- $popupcontent .= \html_writer::link($dayhref, $name);
- $popupcontent .= \html_writer::end_tag('div');
- }
-
- if ($display->thismonth && $day == $d) {
- $popupdata = calendar_get_popup(true, $daytime, $popupcontent);
- } else {
- $popupdata = calendar_get_popup(false, $daytime, $popupcontent);
- }
-
- // Class and cell content.
- if (isset($typesbyday[$day]['startglobal'])) {
- $class .= ' calendar_event_site';
- } else if (isset($typesbyday[$day]['startcourse'])) {
- $class .= ' calendar_event_course';
- } else if (isset($typesbyday[$day]['startgroup'])) {
- $class .= ' calendar_event_group';
- } else if (isset($typesbyday[$day]['startuser'])) {
- $class .= ' calendar_event_user';
- }
- if ($finishclass) {
- $class .= ' duration_finish';
- }
- $data = array(
- 'url' => $dayhref->out(false),
- 'day' => $day,
- 'content' => $popupdata['data-core_calendar-popupcontent'],
- 'title' => $popupdata['data-core_calendar-title']
- );
- $cell = $OUTPUT->render_from_template('core_calendar/minicalendar_day_link', $data);
- } else {
- $cell = $day;
- }
-
- $durationclass = false;
- if (isset($typesbyday[$day]['durationglobal'])) {
- $durationclass = ' duration_global';
- } else if (isset($typesbyday[$day]['durationcourse'])) {
- $durationclass = ' duration_course';
- } else if (isset($typesbyday[$day]['durationgroup'])) {
- $durationclass = ' duration_group';
- } else if (isset($typesbyday[$day]['durationuser'])) {
- $durationclass = ' duration_user';
- }
- if ($durationclass) {
- $class .= ' duration ' . $durationclass;
- }
-
- // If event has a class set then add it to the table day <td> tag.
- // Note: only one colour for minicalendar.
- if (isset($eventsbyday[$day])) {
- foreach ($eventsbyday[$day] as $eventid) {
- if (!isset($events[$eventid])) {
- continue;
- }
- $event = $events[$eventid];
- if (!empty($event->class)) {
- $class .= ' ' . $event->class;
- }
- break;
- }
- }
-
- if ($display->thismonth && $day == $d) {
- // The current cell is for today - add appropriate classes and additional information for styling.
- $class .= ' today';
- $today = get_string('today', 'calendar') . ' ' . userdate(time(), get_string('strftimedayshort'));
-
- if (!isset($eventsbyday[$day]) && !isset($durationbyday[$day])) {
- $class .= ' eventnone';
- $popupdata = calendar_get_popup(true, false);
- $data = array(
- 'url' => '#',
- 'day' => $day,
- 'content' => $popupdata['data-core_calendar-popupcontent'],
- 'title' => $popupdata['data-core_calendar-title']
- );
- $cell = $OUTPUT->render_from_template('core_calendar/minicalendar_day_link', $data);
- }
- $cell = get_accesshide($today . ' ') . $cell;
- }
-
- // Just display it.
- $cellattributes['class'] = $class;
- $content .= \html_writer::tag('td', $cell, $cellattributes);
- }
-
- // Padding (the last week may have blank days at the end).
- for ($i = $dayweek; $i <= $display->maxwday; ++$i) {
- $content .= '<td class="dayblank"> </td>';
- }
- $content .= '</tr>'; // Last row ends.
-
- $content .= '</table>'; // Tabular display of days ends.
- return $content;
-}
-
-/**
- * Gets the calendar popup.
- *
- * It called at multiple points in from calendar_get_mini.
- * Copied and modified from calendar_get_mini.
- *
- * @param bool $today false except when called on the current day.
- * @param mixed $timestart $events[$eventid]->timestart, OR false if there are no events.
- * @param string $popupcontent content for the popup window/layout.
- * @return string eventid for the calendar_tooltip popup window/layout.
- */
-function calendar_get_popup($today = false, $timestart, $popupcontent = '') {
- $popupcaption = '';
- if ($today) {
- $popupcaption = get_string('today', 'calendar') . ' ';
- }
-
- if (false === $timestart) {
- $popupcaption .= userdate(time(), get_string('strftimedayshort'));
- $popupcontent = get_string('eventnone', 'calendar');
-
- } else {
- $popupcaption .= get_string('eventsfor', 'calendar', userdate($timestart, get_string('strftimedayshort')));
- }
-
- return array(
- 'data-core_calendar-title' => $popupcaption,
- 'data-core_calendar-popupcontent' => $popupcontent,
- );
-}
-
/**
* Gets the calendar upcoming event.
*