Skip to content

Commit

Permalink
MDL-41792 core_calendar: refactored the calendar to allow the use of …
Browse files Browse the repository at this point in the history
…multiple calendar types
  • Loading branch information
mdjnelson committed Oct 4, 2013
1 parent bb74cdf commit da30413
Show file tree
Hide file tree
Showing 14 changed files with 758 additions and 474 deletions.
14 changes: 12 additions & 2 deletions blocks/calendar_month/block_calendar_month.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,23 @@ public function get_content() {

$calm = optional_param('cal_m', 0, PARAM_INT);
$caly = optional_param('cal_y', 0, PARAM_INT);
$time = optional_param('time', 0, PARAM_INT);

require_once($CFG->dirroot.'/calendar/lib.php');

if ($this->content !== null) {
return $this->content;
}

// If a day, month and year were passed then convert it to a timestamp. If these were passed then we can assume
// the day, month and year are passed as Gregorian, as no where in core should we be passing these values rather
// than the time. This is done for BC.
if (!empty($calm) && (!empty($caly))) {
$time = make_timestamp($caly, $calm, 1);
} else if (empty($time)) {
$time = time();
}

$this->content = new stdClass;
$this->content->text = '';
$this->content->footer = '';
Expand All @@ -78,11 +88,11 @@ public function get_content() {
list($courses, $group, $user) = calendar_set_filters($filtercourse);
if ($issite) {
// For the front page.
$this->content->text .= calendar_get_mini($courses, $group, $user, $calm, $caly, 'frontpage', $courseid);
$this->content->text .= calendar_get_mini($courses, $group, $user, false, false, 'frontpage', $courseid, $time);
// No filters for now.
} else {
// For any other course.
$this->content->text .= calendar_get_mini($courses, $group, $user, $calm, $caly, 'course', $courseid);
$this->content->text .= calendar_get_mini($courses, $group, $user, false, false, 'course', $courseid, $time);
$this->content->text .= '<h3 class="eventskey">'.get_string('eventskey', 'calendar').'</h3>';
$this->content->text .= '<div class="filters calendar_filters">'.calendar_filter_controls($this->page->url).'</div>';
}
Expand Down
86 changes: 81 additions & 5 deletions calendar/classes/type_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
*/
abstract class type_base {

/**
* Returns the name of the calendar.
*
* This is the non-translated name, usually just
* the name of the calendar folder.
*
* @return string the calendar name
*/
public abstract function get_name();

/**
* Returns a list of all the possible days for all months.
*
Expand Down Expand Up @@ -62,10 +72,76 @@ public abstract function get_min_year();
*/
public abstract function get_max_year();

/**
* Returns the number of days in a week.
*
* @return int the number of days
*/
public abstract function get_num_weekdays();

/**
* Returns an indexed list of all the names of the weekdays.
*
* The list starts with the index 0. Each index, representing a
* day, must be an array that contains the indexes 'shortname'
* and 'fullname'.
*
* @return array array of days
*/
public abstract function get_weekdays();

/**
* Returns the index of the starting week day.
*
* This may vary, for example in the Gregorian calendar, some may consider Monday
* as the start of the week, where as others may consider Sunday the start.
*
* @return int
*/
public abstract function get_starting_weekday();

/**
* Returns the index of the weekday for a specific calendar date.
*
* @param int $year
* @param int $month
* @param int $day
* @return int
*/
public abstract function get_weekday($year, $month, $day);

/**
* Returns the number of days in a given month.
*
*
* @param int $year
* @param int $month
* @return int the number of days
*/
public abstract function get_num_days_in_month($year, $month);

/**
* Get the previous month.
*
* @param int $year
* @param int $month
* @return array previous month and year
*/
public abstract function get_prev_month($year, $month);

/**
* Get the next month.
*
* @param int $year
* @param int $month
* @return array the following month and year
*/
public abstract function get_next_month($year, $month);

/**
* Returns a formatted string that represents a date in user time.
*
* @param int $date the timestamp in UTC, as obtained from the database
* @param int $time the timestamp in UTC, as obtained from the database
* @param string $format strftime format
* @param int|float|string $timezone the timezone to use
* {@link http://docs.moodle.org/dev/Time_API#Timezone}
Expand All @@ -75,7 +151,7 @@ public abstract function get_max_year();
* if false then the leading zero is maintained
* @return string the formatted date/time
*/
public abstract function timestamp_to_date_string($date, $format, $timezone, $fixday, $fixhour);
public abstract function timestamp_to_date_string($time, $format, $timezone, $fixday, $fixhour);

/**
* Given a $time timestamp in GMT (seconds since epoch), returns an array that represents
Expand All @@ -86,7 +162,7 @@ public abstract function timestamp_to_date_string($date, $format, $timezone, $fi
* {@link http://docs.moodle.org/dev/Time_API#Timezone}
* @return array an array that represents the date in user time
*/
public abstract function timestamp_to_date_array($time, $timezone);
public abstract function timestamp_to_date_array($time, $timezone = 99);

/**
* Provided with a day, month, year, hour and minute in the specific
Expand All @@ -97,7 +173,7 @@ public abstract function timestamp_to_date_array($time, $timezone);
* @param int $day
* @param int $hour
* @param int $minute
* @return array the converted day, month and year.
* @return array the converted date
*/
public abstract function convert_to_gregorian($year, $month, $day, $hour = 0, $minute = 0);

Expand All @@ -110,7 +186,7 @@ public abstract function convert_to_gregorian($year, $month, $day, $hour = 0, $m
* @param int $day
* @param int $hour
* @param int $minute
* @return array the converted day, month and year.
* @return array the converted date
*/
public abstract function convert_from_gregorian($year, $month, $day, $hour = 0, $minute = 0);
}
3 changes: 2 additions & 1 deletion calendar/classes/type_factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public static function get_list_of_calendar_types() {
public static function get_calendar_type() {
global $CFG, $USER, $SESSION, $COURSE;

if (!empty($COURSE->id) and $COURSE->id != SITEID and !empty($COURSE->calendartype)) { // Course calendartype can override all other settings for this page.
// Course calendartype can override all other settings for this page.
if (!empty($COURSE->id) and $COURSE->id != SITEID and !empty($COURSE->calendartype)) {
$return = $COURSE->calendartype;
} else if (!empty($SESSION->calendartype)) { // Session calendartype can override other settings.
$return = $SESSION->calendartype;
Expand Down
3 changes: 1 addition & 2 deletions calendar/delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@

// Is used several times, and sometimes with modification if required
$viewcalendarurl = new moodle_url(CALENDAR_URL.'view.php', array('view'=>'upcoming'));
$viewcalendarurl->param('cal_y', userdate($event->timestart, '%Y'));
$viewcalendarurl->param('cal_m', userdate($event->timestart, '%m'));
$viewcalendarurl->param('time', $event->timestart, '%Y');

// If confirm is set (PARAM_BOOL) then we have confirmation of initention to delete
if ($confirm) {
Expand Down
49 changes: 23 additions & 26 deletions calendar/event.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,34 @@
$eventid = optional_param('id', 0, PARAM_INT);
$courseid = optional_param('courseid', SITEID, PARAM_INT);
$courseid = optional_param('course', $courseid, PARAM_INT);
$cal_y = optional_param('cal_y', 0, PARAM_INT);
$cal_m = optional_param('cal_m', 0, PARAM_INT);
$cal_d = optional_param('cal_d', 0, PARAM_INT);
$day = optional_param('cal_d', 0, PARAM_INT);
$month = optional_param('cal_m', 0, PARAM_INT);
$year = optional_param('cal_y', 0, PARAM_INT);
$time = optional_param('time', 0, PARAM_INT);

// If a day, month and year were passed then convert it to a timestamp. If these were passed
// then we can assume the day, month and year are passed as Gregorian, as no where in core
// should we be passing these values rather than the time. This is done for BC.
if (!empty($day) && !empty($month) && !empty($year)) {
if (checkdate($month, $day, $year)) {
$time = make_timestamp($year, $month, $day);
} else {
$time = time();
}
} else if (empty($time)) {
$time = time();
}

$url = new moodle_url('/calendar/event.php', array('action' => $action));

if ($eventid != 0) {
$url->param('id', $eventid);
}

if ($courseid != SITEID) {
$url->param('course', $courseid);
}
if ($cal_y !== 0) {
$url->param('cal_y', $cal_y);
}
if ($cal_m !== 0) {
$url->param('cal_m', $cal_m);
}
if ($cal_d !== 0) {
$url->param('cal_d', $cal_d);
}

$PAGE->set_url($url);
$PAGE->set_pagelayout('standard');

Expand All @@ -99,7 +107,7 @@
redirect($deleteurl);
}

$calendar = new calendar_information($cal_d, $cal_m, $cal_y);
$calendar = new calendar_information(0, 0, 0, $time);
$calendar->prepare_for_view($course, $courses);

$formoptions = new stdClass;
Expand Down Expand Up @@ -133,16 +141,7 @@
unset($formoptions->eventtypes->groups);
}
}
if($cal_y && $cal_m && $cal_d && checkdate($cal_m, $cal_d, $cal_y)) {
$event->timestart = make_timestamp($cal_y, $cal_m, $cal_d, 0, 0, 0);
} else if($cal_y && $cal_m && checkdate($cal_m, 1, $cal_y)) {
$now = usergetdate(time());
if($cal_y == $now['year'] && $cal_m == $now['mon']) {
$event->timestart = make_timestamp($cal_y, $cal_m, $now['mday'], 0, 0, 0);
} else {
$event->timestart = make_timestamp($cal_y, $cal_m, 1, 0, 0, 0);
}
}
$event->timestart = $time;
$event = new calendar_event($event);
if (!calendar_add_event_allowed($event)) {
print_error('nopermissions');
Expand All @@ -168,9 +167,7 @@

$params = array(
'view' => 'day',
'cal_d' => userdate($event->timestart, '%d'),
'cal_m' => userdate($event->timestart, '%m'),
'cal_y' => userdate($event->timestart, '%Y'),
'time' => $event->timestart,
);
$eventurl = new moodle_url('/calendar/view.php', $params);
if (!empty($event->courseid) && $event->courseid != SITEID) {
Expand Down
57 changes: 35 additions & 22 deletions calendar/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,28 @@

$courseid = optional_param('course', SITEID, PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHA);
$day = optional_param('cal_d', 0, PARAM_INT);
$mon = optional_param('cal_m', 0, PARAM_INT);
$yr = optional_param('cal_y', 0, PARAM_INT);
$day = optional_param('cal_d', 0, PARAM_INT);
$mon = optional_param('cal_m', 0, PARAM_INT);
$year = optional_param('cal_y', 0, PARAM_INT);
$time = optional_param('time', 0, PARAM_INT);
$generateurl = optional_param('generateurl', 0, PARAM_BOOL);

// Get the calendar type we are using.
$calendartype = \core_calendar\type_factory::get_calendar_instance();

// If a day, month and year were passed then convert it to a timestamp. If these were passed
// then we can assume the day, month and year are passed as Gregorian, as no where in core
// should we be passing these values rather than the time. This is done for BC.
if (!empty($day) && !empty($mon) && !empty($year)) {
if (checkdate($mon, $day, $year)) {
$time = make_timestamp($year, $mon, $day);
} else {
$time = time();
}
} else if (empty($time)) {
$time = time();
}

if ($courseid != SITEID && !empty($courseid)) {
$course = $DB->get_record('course', array('id' => $courseid));
$courses = array($course->id => $course);
Expand All @@ -72,36 +89,29 @@
}
require_course_login($course);

$url = new moodle_url('/calendar/export.php');
$url = new moodle_url('/calendar/export.php', array('time' => $time));

if ($action !== '') {
$url->param('action', $action);
}
if ($day !== 0) {
$url->param('cal_d', $day);
}
if ($mon !== 0) {
$url->param('cal_m', $mon);
}
if ($yr !== 0) {
$url->param('cal_y', $yr);
}

if ($course !== NULL) {
$url->param('course', $course->id);
}
$PAGE->set_url($url);

$calendar = new calendar_information($day, $mon, $yr);
$calendar = new calendar_information(0, 0, 0, $time);
$calendar->prepare_for_view($course, $courses);

$pagetitle = get_string('export', 'calendar');
$now = usergetdate(time());
$now = $calendartype->timestamp_to_date_array($time);

// Print title and header
if ($issite) {
$PAGE->navbar->add($course->shortname, new moodle_url('/course/view.php', array('id'=>$course->id)));
}
$link = new moodle_url(CALENDAR_URL.'view.php', array('view'=>'upcoming', 'course'=>$calendar->courseid));
$PAGE->navbar->add(get_string('calendar', 'calendar'), calendar_get_link_href($link, $now['mday'], $now['mon'], $now['year']));
$PAGE->navbar->add(get_string('calendar', 'calendar'), calendar_get_link_href($link, 0, 0, 0, $time));
$PAGE->navbar->add($pagetitle);

$PAGE->set_title($course->shortname.': '.get_string('calendar', 'calendar').': '.$pagetitle);
Expand All @@ -125,14 +135,17 @@
$weekend = intval($CFG->calendar_weekend);
}

// Get the number of days.
$numberofdaysinweek = $calendartype->get_num_weekdays();

$authtoken = sha1($USER->id . $DB->get_field('user', 'password', array('id'=>$USER->id)). $CFG->calendar_exportsalt);
// Let's populate some vars to let "common tasks" be somewhat smart...
// If today it's weekend, give the "next week" option
$allownextweek = $weekend & (1 << $now['wday']);
// If it's the last week of the month, give the "next month" option
$allownextmonth = calendar_days_in_month($now['mon'], $now['year']) - $now['mday'] < 7;
// If today it's weekend but tomorrow it isn't, do NOT give the "this week" option
$allowthisweek = !(($weekend & (1 << $now['wday'])) && !($weekend & (1 << (($now['wday'] + 1) % 7))));
// If today it's weekend, give the "next week" option.
$allownextweek = $weekend & (1 << $now['wday']);
// If it's the last week of the month, give the "next month" option.
$allownextmonth = calendar_days_in_month($now['mon'], $now['year']) - $now['mday'] < $numberofdaysinweek;
// If today it's weekend but tomorrow it isn't, do NOT give the "this week" option.
$allowthisweek = !(($weekend & (1 << $now['wday'])) && !($weekend & (1 << (($now['wday'] + 1) % $numberofdaysinweek))));
echo $renderer->basic_export_form($allowthisweek, $allownextweek, $allownextmonth, $USER->id, $authtoken);
break;
}
Expand Down
Loading

0 comments on commit da30413

Please sign in to comment.