/**
  * Test all the core functions that use the calendar type system.
  *
  * @param string $type the calendar type we want to test
  */
 private function core_functions_test($type)
 {
     $this->set_calendar_type($type);
     // Get the calendar.
     $calendar = \core_calendar\type_factory::get_calendar_instance();
     // Test the userdate function.
     $this->assertEquals($calendar->timestamp_to_date_string($this->user->timecreated, '', 99, true, true), userdate($this->user->timecreated));
     // Test the calendar/lib.php functions.
     $this->assertEquals($calendar->get_weekdays(), calendar_get_days());
     $this->assertEquals($calendar->get_starting_weekday(), calendar_get_starting_weekday());
     $this->assertEquals($calendar->get_num_days_in_month('1986', '9'), calendar_days_in_month('9', '1986'));
     $this->assertEquals($calendar->get_next_month('1986', '9'), calendar_add_month('9', '1986'));
     $this->assertEquals($calendar->get_prev_month('1986', '9'), calendar_sub_month('9', '1986'));
     // Test the lib/moodle.php functions.
     $this->assertEquals($calendar->get_num_days_in_month('1986', '9'), days_in_month('9', '1986'));
     $this->assertEquals($calendar->get_weekday('1986', '9', '16'), dayofweek('16', '9', '1986'));
 }
Example #2
0
function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_year = false)
{
    global $CFG, $USER;
    $display = new stdClass();
    $display->minwday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
    $display->maxwday = $display->minwday + 6;
    $content = '';
    if (!empty($cal_month) && !empty($cal_year)) {
        $thisdate = usergetdate(time());
        // Date and time the user sees at his location
        if ($cal_month == $thisdate['mon'] && $cal_year == $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($cal_month, 1, $cal_year)) {
                $date = array('mday' => 1, 'mon' => $thisdate['mon'], 'year' => $thisdate['year']);
                $display->thismonth = true;
            } else {
                $date = array('mday' => 1, 'mon' => $cal_month, 'year' => $cal_year);
                $display->thismonth = false;
            }
        }
    } else {
        $date = usergetdate(time());
        // Date and time the user sees at his location
        $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);
    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
    } 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 = dayofweek(1, $m, $y);
    // Align the starting weekday to fall in our display range
    // This is simple, not foolproof.
    if ($startwday < $display->minwday) {
        $startwday += 7;
    }
    // TODO: THIS IS TEMPORARY CODE!
    // [pj] I was just reading through this and realized that I when writing this code I was probably
    // asking for trouble, as all these time manipulations seem to be unnecessary and a simple
    // make_timestamp would accomplish the same thing. So here goes a test:
    //$test_start = make_timestamp($y, $m, 1);
    //$test_end   = make_timestamp($y, $m, $display->maxdays, 23, 59, 59);
    //if($test_start != usertime($display->tstart) - dst_offset_on($display->tstart)) {
    //notify('Failed assertion in calendar/lib.php line 126; display->tstart = '.$display->tstart.', dst_offset = '.dst_offset_on($display->tstart).', usertime = '.usertime($display->tstart).', make_t = '.$test_start);
    //}
    //if($test_end != usertime($display->tend) - dst_offset_on($display->tend)) {
    //notify('Failed assertion in calendar/lib.php line 130; display->tend = '.$display->tend.', dst_offset = '.dst_offset_on($display->tend).', usertime = '.usertime($display->tend).', make_t = '.$test_end);
    //}
    // Get the events matching our criteria. Don't forget to offset the timestamps for the user's TZ!
    $events = calendar_get_events(usertime($display->tstart) - dst_offset_on($display->tstart), usertime($display->tend) - dst_offset_on($display->tend), $users, $groups, $courses);
    // Set event course class for course events
    if (!empty($events)) {
        foreach ($events as $eventid => $event) {
            if (!empty($event->modulename)) {
                $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
                if (!groups_course_module_visible($cm)) {
                    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.
    $morehref = '';
    if (!empty($courses)) {
        $courses = array_diff($courses, array(SITEID));
        if (count($courses) == 1) {
            $morehref = '&amp;course=' . reset($courses);
        }
    }
    // We want to have easy access by day, since the display is on a per-day basis.
    // Arguments passed by reference.
    //calendar_events_by_day($events, $display->tstart, $eventsbyday, $durationbyday, $typesbyday);
    calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday, $courses);
    //Accessibility: added summary and <abbr> elements.
    ///global $CALENDARDAYS; appears to be broken.
    $days_title = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
    $summary = get_string('calendarheading', 'calendar', userdate(make_timestamp($y, $m), get_string('strftimemonthyear')));
    $summary = get_string('tabledata', 'access', $summary);
    $content .= '<table class="minicalendar" summary="' . $summary . '">';
    // Begin table
    $content .= '<tr class="weekdays">';
    // Header row: day names
    // Print out the names of the weekdays
    $days = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
    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
        $content .= '<th scope="col"><abbr title="' . get_string($days_title[$i % 7], 'calendar') . '">' . get_string($days[$i % 7], 'calendar') . "</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;
    // Paddding (the first week may have blank days in the beginning)
    for ($i = $display->minwday; $i < $startwday; ++$i) {
        $content .= '<td class="dayblank">&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)
            $content .= '</tr><tr>';
            $dayweek = $display->minwday;
        }
        // Reset vars
        $cell = '';
        if (CALENDAR_WEEKEND & 1 << $dayweek % 7) {
            // Weekend. This is true no matter what the exact range is.
            $class = 'weekend day';
        } else {
            // Normal working day.
            $class = 'day';
        }
        // Special visual fx if an event is defined
        if (isset($eventsbyday[$day])) {
            $dayhref = calendar_get_link_href(CALENDAR_URL . 'view.php?view=day' . $morehref . '&amp;', $day, $m, $y);
            // OverLib popup
            $popupcontent = '';
            foreach ($eventsbyday[$day] as $eventid) {
                if (!isset($events[$eventid])) {
                    continue;
                }
                $event = $events[$eventid];
                if (!empty($event->modulename)) {
                    $popupicon = $CFG->modpixpath . '/' . $event->modulename . '/icon.gif';
                    $popupalt = $event->modulename;
                } else {
                    if ($event->courseid == SITEID) {
                        // Site event
                        $popupicon = $CFG->pixpath . '/c/site.gif';
                        $popupalt = '';
                    } else {
                        if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
                            // Course event
                            $popupicon = $CFG->pixpath . '/c/course.gif';
                            $popupalt = '';
                        } else {
                            if ($event->groupid) {
                                // Group event
                                $popupicon = $CFG->pixpath . '/c/group.gif';
                                $popupalt = '';
                            } else {
                                if ($event->userid) {
                                    // User event
                                    $popupicon = $CFG->pixpath . '/c/user.gif';
                                    $popupalt = '';
                                }
                            }
                        }
                    }
                }
                $popupcontent .= '<div><img class="icon" src="' . $popupicon . '" alt="' . $popupalt . '" /><a href="' . $dayhref . '#event_' . $event->id . '">' . format_string($event->name, true) . '</a></div>';
            }
            //Accessibility: functionality moved to calendar_get_popup.
            if ($display->thismonth && $day == $d) {
                $popup = calendar_get_popup(true, $events[$eventid]->timestart, $popupcontent);
            } else {
                $popup = calendar_get_popup(false, $events[$eventid]->timestart, $popupcontent);
            }
            // Class and cell content
            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';
                        }
                    }
                }
            }
            $cell = '<a href="' . $dayhref . '" ' . $popup . '>' . $day . '</a>';
        } else {
            $cell = $day;
        }
        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';
                    }
                }
            }
        }
        // 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;
            }
        }
        // Special visual fx for today
        //Accessibility: hidden text for today, and popup.
        if ($display->thismonth && $day == $d) {
            $class .= ' today';
            $today = get_string('today', 'calendar') . ' ' . userdate(time(), get_string('strftimedayshort'));
            if (!isset($eventsbyday[$day])) {
                $class .= ' eventnone';
                $popup = calendar_get_popup(true, false);
                $cell = '<a href="#" ' . $popup . '>' . $day . '</a>';
            }
            $cell = get_accesshide($today . ' ') . $cell;
        }
        // Just display it
        if (!empty($class)) {
            $class = ' class="' . $class . '"';
        }
        $content .= '<td' . $class . '>' . $cell . "</td>\n";
    }
    // Paddding (the last week may have blank days at the end)
    for ($i = $dayweek; $i <= $display->maxwday; ++$i) {
        $content .= '<td class="dayblank">&nbsp;</td>';
    }
    $content .= '</tr>';
    // Last row ends
    $content .= '</table>';
    // Tabular display of days ends
    return $content;
}
Example #3
0
 /**
  * Displays a month in detail
  *
  * @param calendar_information $calendar
  * @param moodle_url $returnurl the url to return to
  * @return string
  */
 public function show_month_detailed(calendar_information $calendar, moodle_url $returnurl = null)
 {
     global $CFG;
     if (empty($returnurl)) {
         $returnurl = $this->page->url;
     }
     // Get the calendar type we are using.
     $calendartype = \core_calendar\type_factory::get_calendar_instance();
     // Store the display settings.
     $display = new stdClass();
     $display->thismonth = false;
     // Get the specified date in the calendar type being used.
     $date = $calendartype->timestamp_to_date_array($calendar->time);
     $thisdate = $calendartype->timestamp_to_date_array(time());
     if ($date['mon'] == $thisdate['mon'] && $date['year'] == $thisdate['year']) {
         $display->thismonth = true;
         $date = $thisdate;
         $calendar->time = time();
     }
     // 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 starting week day for this month.
     $startwday = dayofweek(1, $date['mon'], $date['year']);
     // Get the days in a week.
     $daynames = calendar_get_days();
     // Store the number of days in a week.
     $numberofdaysinweek = $calendartype->get_num_weekdays();
     $display->minwday = calendar_get_starting_weekday();
     $display->maxwday = $display->minwday + ($numberofdaysinweek - 1);
     $display->maxdays = calendar_days_in_month($date['mon'], $date['year']);
     // 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 events from database
     $events = calendar_get_events($display->tstart, $display->tend, $calendar->users, $calendar->groups, $calendar->courses);
     if (!empty($events)) {
         foreach ($events as $eventid => $event) {
             $event = new calendar_event($event);
             if (!empty($event->modulename)) {
                 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
                 if (!\core_availability\info_module::is_user_visible($cm, 0, false)) {
                     unset($events[$eventid]);
                 }
             }
         }
     }
     // Extract information: events vs. time
     calendar_events_by_day($events, $date['mon'], $date['year'], $eventsbyday, $durationbyday, $typesbyday, $calendar->courses);
     $output = html_writer::start_tag('div', array('class' => 'header'));
     $output .= $this->course_filter_selector($returnurl, get_string('detailedmonthviewfor', 'calendar'));
     if (calendar_user_can_add_event($calendar->course)) {
         $output .= $this->add_event_button($calendar->course->id, 0, 0, 0, $calendar->time);
     }
     $output .= html_writer::end_tag('div', array('class' => 'header'));
     // Controls
     $output .= html_writer::tag('div', calendar_top_controls('month', array('id' => $calendar->courseid, 'time' => $calendar->time)), array('class' => 'controls'));
     $table = new html_table();
     $table->attributes = array('class' => 'calendarmonth calendartable');
     $table->summary = get_string('calendarheading', 'calendar', userdate($calendar->time, get_string('strftimemonthyear')));
     $table->data = array();
     // Get the day names as the header.
     $header = array();
     for ($i = $display->minwday; $i <= $display->maxwday; ++$i) {
         $header[] = $daynames[$i % $numberofdaysinweek]['shortname'];
     }
     $table->head = $header;
     // For the table display. $week is the row; $dayweek is the column.
     $week = 1;
     $dayweek = $startwday;
     $row = new html_table_row(array());
     // Paddding (the first week may have blank days in the beginning)
     for ($i = $display->minwday; $i < $startwday; ++$i) {
         $cell = new html_table_cell('&nbsp;');
         $cell->attributes = array('class' => 'nottoday dayblank');
         $row->cells[] = $cell;
     }
     // Now display all the calendar
     $weekend = CALENDAR_DEFAULT_WEEKEND;
     if (isset($CFG->calendar_weekend)) {
         $weekend = intval($CFG->calendar_weekend);
     }
     $daytime = $display->tstart - DAYSECS;
     for ($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
         $daytime = $daytime + DAYSECS;
         if ($dayweek > $display->maxwday) {
             // We need to change week (table row)
             $table->data[] = $row;
             $row = new html_table_row(array());
             $dayweek = $display->minwday;
             ++$week;
         }
         // Reset vars
         $cell = new html_table_cell();
         $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL . 'view.php', array('view' => 'day', 'course' => $calendar->courseid)), 0, 0, 0, $daytime);
         $cellclasses = array();
         if ($weekend & 1 << $dayweek % $numberofdaysinweek) {
             // Weekend. This is true no matter what the exact range is.
             $cellclasses[] = 'weekend';
         }
         // Special visual fx if an event is defined
         if (isset($eventsbyday[$day])) {
             if (count($eventsbyday[$day]) == 1) {
                 $title = get_string('oneevent', 'calendar');
             } else {
                 $title = get_string('manyevents', 'calendar', count($eventsbyday[$day]));
             }
             $cell->text = html_writer::tag('div', html_writer::link($dayhref, $day, array('title' => $title)), array('class' => 'day'));
         } else {
             $cell->text = html_writer::tag('div', $day, array('class' => 'day'));
         }
         // Special visual fx if an event spans many days
         $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) {
             $cellclasses[] = 'duration';
             $cellclasses[] = $durationclass;
         }
         // Special visual fx for today
         if ($display->thismonth && $day == $date['mday']) {
             $cellclasses[] = 'day today';
         } else {
             $cellclasses[] = 'day nottoday';
         }
         $cell->attributes = array('class' => join(' ', $cellclasses));
         if (isset($eventsbyday[$day])) {
             $cell->text .= html_writer::start_tag('ul', array('class' => 'events-new'));
             foreach ($eventsbyday[$day] as $eventindex) {
                 // If event has a class set then add it to the event <li> tag
                 $attributes = array();
                 if (!empty($events[$eventindex]->class)) {
                     $attributes['class'] = $events[$eventindex]->class;
                 }
                 $dayhref->set_anchor('event_' . $events[$eventindex]->id);
                 $link = html_writer::link($dayhref, format_string($events[$eventindex]->name, true));
                 $cell->text .= html_writer::tag('li', $link, $attributes);
             }
             $cell->text .= html_writer::end_tag('ul');
         }
         if (isset($durationbyday[$day])) {
             $cell->text .= html_writer::start_tag('ul', array('class' => 'events-underway'));
             foreach ($durationbyday[$day] as $eventindex) {
                 $cell->text .= html_writer::tag('li', '[' . format_string($events[$eventindex]->name, true) . ']', array('class' => 'events-underway'));
             }
             $cell->text .= html_writer::end_tag('ul');
         }
         $row->cells[] = $cell;
     }
     // Paddding (the last week may have blank days at the end)
     for ($i = $dayweek; $i <= $display->maxwday; ++$i) {
         $cell = new html_table_cell('&nbsp;');
         $cell->attributes = array('class' => 'nottoday dayblank');
         $row->cells[] = $cell;
     }
     $table->data[] = $row;
     $output .= html_writer::table($table);
     return $output;
 }
Example #4
0
/**
 * 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_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)) {
                $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
                if (!groups_course_module_visible($cm)) {
                    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')));
    $content .= '<table class="minicalendar calendartable" summary="' . $summary . '">';
    // Begin table.
    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;
    // Paddding (the first week may have blank days in the beginning)
    for ($i = $display->minwday; $i < $startwday; ++$i) {
        $content .= '<td class="dayblank">&nbsp;</td>' . "\n";
    }
    $weekend = CALENDAR_DEFAULT_WEEKEND;
    if (isset($CFG->calendar_weekend)) {
        $weekend = intval($CFG->calendar_weekend);
    }
    // Now display all the calendar
    $daytime = $display->tstart - DAYSECS;
    for ($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
        $daytime += DAYSECS;
        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';
        }
        // Special visual fx if an event is defined
        if (isset($eventsbyday[$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 ($eventsbyday[$day] 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';
                            }
                        }
                    }
                }
                $dayhref->set_anchor('event_' . $event->id);
                $popupcontent .= html_writer::start_tag('div');
                $popupcontent .= $OUTPUT->pix_icon($popupicon, $popupalt, $component);
                $name = format_string($event->name, true);
                // Show ical source if needed.
                if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
                    $a = new stdClass();
                    $a->name = $name;
                    $a->source = $event->subscription->name;
                    $name = get_string('namewithsource', 'calendar', $a);
                }
                $popupcontent .= html_writer::link($dayhref, $name);
                $popupcontent .= html_writer::end_tag('div');
            }
            //Accessibility: functionality moved to calendar_get_popup.
            if ($display->thismonth && $day == $d) {
                $popupid = calendar_get_popup(true, $events[$eventid]->timestart, $popupcontent);
            } else {
                $popupid = calendar_get_popup(false, $events[$eventid]->timestart, $popupcontent);
            }
            // Class and cell content
            if (isset($typesbyday[$day]['startglobal'])) {
                $class .= ' calendar_event_global';
            } 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';
                        }
                    }
                }
            }
            $cell = html_writer::link($dayhref, $day, array('id' => $popupid));
        } 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;
            }
        }
        // Special visual fx for today
        //Accessibility: hidden text for today, and popup.
        if ($display->thismonth && $day == $d) {
            $class .= ' today';
            $today = get_string('today', 'calendar') . ' ' . userdate(time(), get_string('strftimedayshort'));
            if (!isset($eventsbyday[$day])) {
                $class .= ' eventnone';
                $popupid = calendar_get_popup(true, false);
                $cell = html_writer::link('#', $day, array('id' => $popupid));
            }
            $cell = get_accesshide($today . ' ') . $cell;
        }
        // Just display it
        if (!empty($class)) {
            $class = ' class="' . $class . '"';
        }
        $content .= '<td' . $class . '>' . $cell . "</td>\n";
    }
    // Paddding (the last week may have blank days at the end)
    for ($i = $dayweek; $i <= $display->maxwday; ++$i) {
        $content .= '<td class="dayblank">&nbsp;</td>';
    }
    $content .= '</tr>';
    // Last row ends
    $content .= '</table>';
    // Tabular display of days ends
    return $content;
}
Example #5
0
/**
 * Calculates when the day appears in specific month
 *
 * @package core
 * @category time
 * @param int $startday starting day of the month
 * @param int $weekday The day when week starts (normally taken from user preferences)
 * @param int $month The month whose day is sought
 * @param int $year The year of the month whose day is sought
 * @return int
 */
function find_day_in_month($startday, $weekday, $month, $year)
{
    $calendartype = \core_calendar\type_factory::get_calendar_instance();
    $daysinmonth = days_in_month($month, $year);
    $daysinweek = count($calendartype->get_weekdays());
    if ($weekday == -1) {
        // Don't care about weekday, so return:
        //    abs($startday) if $startday != -1
        //    $daysinmonth otherwise.
        return $startday == -1 ? $daysinmonth : abs($startday);
    }
    // From now on we 're looking for a specific weekday.
    // Give "end of month" its actual value, since we know it.
    if ($startday == -1) {
        $startday = -1 * $daysinmonth;
    }
    // Starting from day $startday, the sign is the direction.
    if ($startday < 1) {
        $startday = abs($startday);
        $lastmonthweekday = dayofweek($daysinmonth, $month, $year);
        // This is the last such weekday of the month.
        $lastinmonth = $daysinmonth + $weekday - $lastmonthweekday;
        if ($lastinmonth > $daysinmonth) {
            $lastinmonth -= $daysinweek;
        }
        // Find the first such weekday <= $startday.
        while ($lastinmonth > $startday) {
            $lastinmonth -= $daysinweek;
        }
        return $lastinmonth;
    } else {
        $indexweekday = dayofweek($startday, $month, $year);
        $diff = $weekday - $indexweekday;
        if ($diff < 0) {
            $diff += $daysinweek;
        }
        // This is the first such weekday of the month equal to or after $startday.
        $firstfromindex = $startday + $diff;
        return $firstfromindex;
    }
}
Example #6
0
    }
    if ($event != 0) {
        $WORK = insert_into_template($LINK_DAY, "{MONTH}", $month);
        $WORK = insert_into_template($WORK, "{DAY}", $iday);
        $DAYS .= insert_into_template($WORK, "{YEAR}", $year);
    } else {
        $DAYS .= insert_into_template($DAY, "{DAY}", $iday);
    }
    if (6 == dayofweek($month, $iday, $year)) {
        //we will append these DAYS to WEEKS and empty DAYS
        $WEEKS .= insert_into_template($WEEK, "{DAYS}", $DAYS);
        $DAYS = "";
    }
}
//now lets pad the last week of the calendar
$lastday = dayofweek($month, $iday, $year);
while ($lastday < 6) {
    $DAYS .= $DAY;
    $lastday++;
}
//now lets close out the calendar.
$WEEKS .= insert_into_template($WEEK, "{DAYS}", $DAYS);
//lets add the month, year, prev/next month and year
$WORK = insert_into_template($MONTH, "{PREVMONTH}", $prevmonth);
$WORK = insert_into_template($WORK, "{PREVYEAR}", $prevyear);
$WORK = insert_into_template($WORK, "{NEXTMONTH}", $nextmonth);
$WORK = insert_into_template($WORK, "{NEXTYEAR}", $nextyear);
$WORK = insert_into_template($WORK, "{MONTH}", $monthname);
$WORK = insert_into_template($WORK, "{YEAR}", $year);
//lets add our weeks to the calendar
$WORK = insert_into_template($WORK, "{WEEKS}", $WEEKS);
Example #7
0
$EVENTS = loadtmplate("events");
//now lets get to work.
if (isset($HTTP_GET_VARS['month']) && isset($HTTP_GET_VARS['day']) && isset($HTTP_GET_VARS['year']) && is_numeric($HTTP_GET_VARS['month']) && is_numeric($HTTP_GET_VARS['day']) && is_numeric($HTTP_GET_VARS['year'])) {
    //lets initialize our variables
    $CONTENT = "";
    $day = $HTTP_GET_VARS['day'];
    $month = $HTTP_GET_VARS['month'];
    $year = $HTTP_GET_VARS['year'];
    if ($day < 10) {
        $day = "0" . 1 * $day;
    }
    if ($month < 10) {
        $month = "0" . 1 * $month;
    }
    //lets get the weekly events
    $sql = "SELECT * FROM " . $list_prefix . "calendar WHERE (`weekly`='" . dayofweek($month, $day, $year) . "' OR `monthly`='" . $day . "' OR `yearly`='" . $month . $day . "' OR `date`='" . $year . $month . $day . "') ORDER BY `time`;";
    $result = db_query($sql);
    if ($result) {
        $rows = db_num_rows($result);
    } else {
        $rows = 0;
    }
    if ($rows > 0) {
        $i = 0;
        $WORK = "";
        while ($i < $rows) {
            $row = db_fetch_array($result);
            //calculate the time
            $time = $row['time'];
            if ($time[0] > 0) {
                $hour = $time[0] . $time[1];