MDL-58424 core_calendar: moved logic from api.php to lib.php

Also moved related PHPUnit tests.

Part of MDL-55611 epic.
This commit is contained in:
Mark Nelson
2017-04-04 11:01:52 +01:00
committed by Dan Poltawski
parent 10515e152d
commit 23a29de7ee
23 changed files with 2111 additions and 2601 deletions
@@ -70,25 +70,25 @@ class block_calendar_month extends block_base {
if ($issite) {
// Being displayed at site level. This will cause the filter to fall back to auto-detecting
// the list of courses it will be grabbing events from.
$filtercourse = \core_calendar\api::get_default_courses();
$filtercourse = calendar_get_default_courses();
} else {
// Forcibly filter events to include only those from the particular course we are in.
$filtercourse = array($courseid => $this->page->course);
}
list($courses, $group, $user) = \core_calendar\api::set_filters($filtercourse);
list($courses, $group, $user) = calendar_set_filters($filtercourse);
if ($issite) {
// For the front page.
$this->content->text .= \core_calendar\api::get_mini_calendar($courses, $group, $user, false, false,
$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 .= \core_calendar\api::get_mini_calendar($courses, $group, $user, false, false,
$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">' .
\core_calendar\api::get_filter_controls($this->page->url) . '</div>';
calendar_filter_controls($this->page->url) . '</div>';
}
return $this->content;
@@ -65,14 +65,14 @@ class block_calendar_upcoming extends block_base {
if ($courseshown == SITEID) {
// Being displayed at site level. This will cause the filter to fall back to auto-detecting
// the list of courses it will be grabbing events from.
$filtercourse = \core_calendar\api::get_default_courses();
$filtercourse = calendar_get_default_courses();
} else {
// Forcibly filter events to include only those from the particular course we are in.
$filtercourse = array($courseshown => $this->page->course);
}
}
list($courses, $group, $user) = \core_calendar\api::set_filters($filtercourse);
list($courses, $group, $user) = calendar_set_filters($filtercourse);
$defaultlookahead = CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD;
if (isset($CFG->calendar_lookahead)) {
@@ -85,7 +85,7 @@ class block_calendar_upcoming extends block_base {
$defaultmaxevents = intval($CFG->calendar_maxevents);
}
$maxevents = get_user_preferences('calendar_maxevents', $defaultmaxevents);
$events = \core_calendar\api::get_upcoming($courses, $group, $user, $lookahead, $maxevents);
$events = calendar_get_upcoming($courses, $group, $user, $lookahead, $maxevents);
if (!empty($this->instance)) {
$link = 'view.php?view=day&amp;course='.$courseshown.'&amp;';
@@ -120,14 +120,14 @@ class block_calendar_upcoming extends block_base {
if (!isset($events[$i]->time)) {
continue;
}
$events[$i] = \core_calendar\api::add_event_metadata($events[$i]);
$events[$i] = calendar_add_event_metadata($events[$i]);
$content .= '<div class="event"><span class="icon c0">' . $events[$i]->icon . '</span>';
if (!empty($events[$i]->referer)) {
// That's an activity event, so let's provide the hyperlink.
$content .= $events[$i]->referer;
} else {
if (!empty($linkhref)) {
$href = \core_calendar\api::get_link_href(new \moodle_url(CALENDAR_URL . $linkhref), 0, 0, 0,
$href = calendar_get_link_href(new \moodle_url(CALENDAR_URL . $linkhref), 0, 0, 0,
$events[$i]->timestart);
$href->set_anchor('event_' . $events[$i]->id);
$content .= \html_writer::link($href, $events[$i]->name);
File diff suppressed because it is too large Load Diff
@@ -134,12 +134,12 @@ abstract class event_abstract_factory implements event_factory_interface {
}
$course = new std_proxy($dbrow->courseid, function($id) {
return \core_calendar\api::get_course_cached($this->coursecachereference, $id);
return calendar_get_course_cached($this->coursecachereference, $id);
});
if ($dbrow->groupid) {
$group = new std_proxy($dbrow->groupid, function($id) {
return \core_calendar\api::get_group_cached($id);
return calendar_get_group_cached($id);
});
}
@@ -155,7 +155,7 @@ abstract class event_abstract_factory implements event_factory_interface {
$dbrow->modulename,
$dbrow->instance,
function($modulename, $instance) {
return \core_calendar\api::get_module_cached(
return calendar_get_module_cached(
$this->modulecachereference,
$modulename,
$instance
@@ -166,7 +166,7 @@ abstract class event_abstract_factory implements event_factory_interface {
if ($dbrow->subscriptionid) {
$subscription = new std_proxy($dbrow->subscriptionid, function($id) {
return \core_calendar\api::get_subscription($id);
return calendar_get_subscription($id);
});
}
@@ -159,10 +159,10 @@ class raw_event_retrieval_strategy implements raw_event_retrieval_strategy_inter
$subqueryconditions = [];
// Get the user's courses. Otherwise, get the default courses being shown by the calendar.
$usercourses = \core_calendar\api::get_default_courses();
$usercourses = calendar_get_default_courses();
// Set calendar filters.
list($usercourses, $usergroups, $user) = \core_calendar\api::set_filters($usercourses, true);
list($usercourses, $usergroups, $user) = calendar_set_filters($usercourses, true);
$subqueryparams = [];
// Flag to indicate whether the query needs to exclude group overrides.
+1 -1
View File
@@ -59,7 +59,7 @@ if (!$course) {
}
// Check the user has the required capabilities to edit an event
if (!\core_calendar\api::can_edit_event($event)) {
if (!calendar_edit_event_allowed($event)) {
print_error('nopermissions');
}
+5 -5
View File
@@ -94,7 +94,7 @@ if ($courseid != SITEID && !empty($courseid)) {
$issite = false;
} else {
$course = get_site();
$courses = \core_calendar\api::get_default_courses();
$courses = calendar_get_default_courses();
$issite = true;
}
require_login($course, false);
@@ -114,7 +114,7 @@ $formoptions = new stdClass;
if ($eventid !== 0) {
$title = get_string('editevent', 'calendar');
$event = calendar_event::load($eventid);
if (!\core_calendar\api::can_edit_event($event)) {
if (!calendar_edit_event_allowed($event)) {
print_error('nopermissions');
}
$event->action = $action;
@@ -122,7 +122,7 @@ if ($eventid !== 0) {
$event->timedurationuntil = $event->timestart + $event->timeduration;
$event->count_repeats();
if (!\core_calendar\api::can_add_event($event)) {
if (!calendar_add_event_allowed($event)) {
print_error('nopermissions');
}
@@ -134,7 +134,7 @@ if ($eventid !== 0) {
} else {
$title = get_string('newevent', 'calendar');
\core_calendar\api::get_allowed_types($formoptions->eventtypes, $course);
calendar_get_allowed_types($formoptions->eventtypes, $course);
$event = new stdClass();
$event->action = $action;
$event->course = $courseid;
@@ -150,7 +150,7 @@ if ($eventid !== 0) {
}
$event->timestart = $time;
$event = new calendar_event($event);
if (!\core_calendar\api::can_add_event($event)) {
if (!calendar_add_event_allowed($event)) {
print_error('nopermissions');
}
}
+3 -3
View File
@@ -82,7 +82,7 @@ if ($courseid != SITEID && !empty($courseid)) {
$issite = false;
} else {
$course = get_site();
$courses = \core_calendar\api::get_default_courses();
$courses = calendar_get_default_courses();
$issite = true;
}
require_course_login($course);
@@ -108,7 +108,7 @@ 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'), \core_calendar\api::get_link_href($link, 0, 0, 0, $time));
$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);
@@ -133,7 +133,7 @@ $formdata = array(
// 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' => \core_calendar\api::get_days_in_month($now['mon'], $now['year']) - $now['mday'] < $numberofdaysinweek,
'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))))
);
+15 -15
View File
@@ -60,7 +60,7 @@ if (!empty($generateurl)) {
if(!empty($what) && !empty($time)) {
if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) {
$courses = enrol_get_users_courses($user->id, true, 'id, visible, shortname');
// Array of courses that we will pass to \core_calendar\api::get_events() which
// Array of courses that we will pass to calendar_get_events() which
// is initially set to the list of the user's courses.
$paramcourses = $courses;
if ($what == 'all' || $what == 'groups') {
@@ -95,12 +95,12 @@ if(!empty($what) && !empty($time)) {
switch($time) {
case 'weeknow':
$startweekday = \core_calendar\api::get_starting_weekday();
$startweekday = calendar_get_starting_weekday();
$startmonthday = find_day_in_month($now['mday'] - ($numberofdaysinweek - 1), $startweekday, $now['mon'], $now['year']);
$startmonth = $now['mon'];
$startyear = $now['year'];
if ($startmonthday > \core_calendar\api::get_days_in_month($startmonth, $startyear)) {
list($startmonth, $startyear) = \core_calendar\api::get_next_month($startmonth, $startyear);
if ($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
$startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
}
$gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
@@ -110,8 +110,8 @@ if(!empty($what) && !empty($time)) {
$endmonthday = $startmonthday + $numberofdaysinweek;
$endmonth = $startmonth;
$endyear = $startyear;
if ($endmonthday > \core_calendar\api::get_days_in_month($endmonth, $endyear)) {
list($endmonth, $endyear) = \core_calendar\api::get_next_month($endmonth, $endyear);
if ($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
$endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
}
$gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
@@ -119,12 +119,12 @@ if(!empty($what) && !empty($time)) {
$gregoriandate['hour'], $gregoriandate['minute']);
break;
case 'weeknext':
$startweekday = \core_calendar\api::get_starting_weekday();
$startweekday = calendar_get_starting_weekday();
$startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']);
$startmonth = $now['mon'];
$startyear = $now['year'];
if ($startmonthday > \core_calendar\api::get_days_in_month($startmonth, $startyear)) {
list($startmonth, $startyear) = \core_calendar\api::get_next_month($startmonth, $startyear);
if ($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
$startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
}
$gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
@@ -134,8 +134,8 @@ if(!empty($what) && !empty($time)) {
$endmonthday = $startmonthday + $numberofdaysinweek;
$endmonth = $startmonth;
$endyear = $startyear;
if ($endmonthday > \core_calendar\api::get_days_in_month($endmonth, $endyear)) {
list($endmonth, $endyear) = \core_calendar\api::get_next_month($endmonth, $endyear);
if ($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
$endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
}
$gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
@@ -148,11 +148,11 @@ if(!empty($what) && !empty($time)) {
$timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
$gregoriandate['hour'], $gregoriandate['minute']);
$timeend = $timestart + (\core_calendar\api::get_days_in_month($now['mon'], $now['year']) * DAYSECS);
$timeend = $timestart + (calendar_days_in_month($now['mon'], $now['year']) * DAYSECS);
break;
case 'monthnext':
// Get the next month for this calendar.
list($nextmonth, $nextyear) = \core_calendar\api::get_next_month($now['mon'], $now['year']);
list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
// Convert to gregorian.
$gregoriandate = $calendartype->convert_to_gregorian($nextyear, $nextmonth, 1);
@@ -160,7 +160,7 @@ if(!empty($what) && !empty($time)) {
// Create the timestamps.
$timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
$gregoriandate['hour'], $gregoriandate['minute']);
$timeend = $timestart + (\core_calendar\api::get_days_in_month($nextmonth, $nextyear) * DAYSECS);
$timeend = $timestart + (calendar_days_in_month($nextmonth, $nextyear) * DAYSECS);
break;
case 'recentupcoming':
//Events in the last 5 or next 60 days
@@ -180,7 +180,7 @@ if(!empty($what) && !empty($time)) {
die();
}
}
$events = \core_calendar\api::get_events($timestart, $timeend, $users, $groups, array_keys($paramcourses), false);
$events = calendar_get_events($timestart, $timeend, $users, $groups, array_keys($paramcourses), false);
$ical = new iCalendar;
$ical->add_property('method', 'PUBLISH');
+5 -5
View File
@@ -86,7 +86,7 @@ class core_calendar_external extends external_api {
$eventobj = calendar_event::load($event['eventid']);
// Let's check if the user is allowed to delete an event.
if (!\core_calendar\api::can_edit_event($eventobj)) {
if (!calendar_edit_event_allowed($eventobj)) {
throw new moodle_exception("nopermissions");
}
// Time to do the magic.
@@ -233,14 +233,14 @@ class core_calendar_external extends external_api {
}
// Event list does not check visibility and permissions, we'll check that later.
$eventlist = \core_calendar\api::get_events($params['options']['timestart'], $params['options']['timeend'],
$eventlist = calendar_get_events($params['options']['timestart'], $params['options']['timeend'],
$funcparam['users'], $funcparam['groups'], $funcparam['courses'], true, $params['options']['ignorehidden']);
// WS expects arrays.
$events = array();
// We need to get events asked for eventids.
if ($eventsbyid = \core_calendar\api::get_events_by_id($params['events']['eventids'])) {
if ($eventsbyid = calendar_get_events_by_id($params['events']['eventids'])) {
$eventlist += $eventsbyid;
}
@@ -265,7 +265,7 @@ class core_calendar_external extends external_api {
(!empty($eventobj->groupid) && in_array($eventobj->groupid, $groups)) ||
(!empty($eventobj->courseid) && in_array($eventobj->courseid, $courses)) ||
($USER->id == $eventobj->userid) ||
(\core_calendar\api::can_edit_event($eventid))) {
(calendar_edit_event_allowed($eventid))) {
$events[$eventid] = $event;
} else {
$warnings[] = array('item' => $eventid, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to view this event');
@@ -623,7 +623,7 @@ class core_calendar_external extends external_api {
$eventobj = new calendar_event($event);
// Let's check if the user is allowed to delete an event.
if (!\core_calendar\api::can_add_event($eventobj)) {
if (!calendar_add_event_allowed($eventobj)) {
$warnings [] = array('item' => $event['name'], 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to create this event');
continue;
}
+1615 -63
View File
File diff suppressed because it is too large Load Diff
+9 -9
View File
@@ -49,10 +49,10 @@ if ($courseid != SITEID && !empty($courseid)) {
$courses = array($course->id => $course);
} else {
$course = get_site();
$courses = \core_calendar\api::get_default_courses();
$courses = calendar_get_default_courses();
}
require_course_login($course);
if (!\core_calendar\api::can_add_event_to_course($course)) {
if (!calendar_user_can_add_event($course)) {
print_error('errorcannotimport', 'calendar');
}
@@ -66,20 +66,20 @@ $importresults = '';
$formdata = $form->get_data();
if (!empty($formdata)) {
require_sesskey(); // Must have sesskey for all actions.
$subscriptionid = \core_calendar\api::add_subscription($formdata);
$subscriptionid = calendar_add_subscription($formdata);
if ($formdata->importfrom == CALENDAR_IMPORT_FROM_FILE) {
// Blank the URL if it's a file import.
$formdata->url = '';
$calendar = $form->get_file_content('importfile');
$ical = new iCalendar();
$ical->unserialize($calendar);
$importresults = \core_calendar\api::import_icalendar_events($ical, $courseid, $subscriptionid);
$importresults = calendar_import_icalendar_events($ical, $courseid, $subscriptionid);
} else {
try {
$importresults = \core_calendar\api::update_subscription_events($subscriptionid);
$importresults = calendar_update_subscription_events($subscriptionid);
} catch (moodle_exception $e) {
// Delete newly added subscription and show invalid url error.
\core_calendar\api::delete_subscription($subscriptionid);
calendar_delete_subscription($subscriptionid);
print_error($e->errorcode, $e->module, $PAGE->url);
}
}
@@ -88,9 +88,9 @@ if (!empty($formdata)) {
} else if (!empty($subscriptionid)) {
// The user is wanting to perform an action upon an existing subscription.
require_sesskey(); // Must have sesskey for all actions.
if (\core_calendar\api::can_edit_subscription($subscriptionid)) {
if (calendar_can_edit_subscription($subscriptionid)) {
try {
$importresults = \core_calendar\api::process_subscription_row($subscriptionid, $pollinterval, $action);
$importresults = calendar_process_subscription_row($subscriptionid, $pollinterval, $action);
} catch (moodle_exception $e) {
// If exception caught, then user should be redirected to page where he/she came from.
print_error($e->errorcode, $e->module, $PAGE->url);
@@ -117,7 +117,7 @@ echo $OUTPUT->header();
// Filter subscriptions which user can't edit.
foreach($subscriptions as $subscription) {
if (!\core_calendar\api::can_edit_subscription($subscription)) {
if (!calendar_can_edit_subscription($subscription)) {
unset($subscriptions[$subscription->id]);
}
}
+2 -2
View File
@@ -63,7 +63,7 @@ class calendar_addsubscription_form extends moodleform {
$mform->setForceLtr('url');
// Poll interval
$choices = \core_calendar\api::get_poll_interval_choices();
$choices = calendar_get_pollinterval_choices();
$mform->addElement('select', 'pollinterval', get_string('pollinterval', 'calendar'), $choices);
$mform->setDefault('pollinterval', 604800);
$mform->addHelpButton('pollinterval', 'pollinterval', 'calendar');
@@ -78,7 +78,7 @@ class calendar_addsubscription_form extends moodleform {
$mform->disabledIf('importfile', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_URL);
// Eventtype: 0 = user, 1 = global, anything else = course ID.
list($choices, $groups) = \core_calendar\api::get_event_type_choices($courseid);
list($choices, $groups) = calendar_get_eventtype_choices($courseid);
$mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $choices);
$mform->addRule('eventtype', get_string('required'), 'required');
$mform->setType('eventtype', PARAM_ALPHA);
+25 -25
View File
@@ -64,7 +64,7 @@ class core_calendar_renderer extends plugin_renderer_base {
public function fake_block_filters($courseid, $day, $month, $year, $view, $courses) {
$returnurl = $this->page->url;
$returnurl->param('course', $courseid);
return html_writer::tag('div', \core_calendar\api::get_filter_controls($returnurl),
return html_writer::tag('div', calendar_filter_controls($returnurl),
array('class' => 'calendar_filters filters'));
}
@@ -82,26 +82,26 @@ class core_calendar_renderer extends plugin_renderer_base {
$date = $calendartype->timestamp_to_date_array($calendar->time);
$prevmonth = \core_calendar\api::get_prev_month($date['mon'], $date['year']);
$prevmonth = calendar_sub_month($date['mon'], $date['year']);
$prevmonthtime = $calendartype->convert_to_gregorian($prevmonth[1], $prevmonth[0], 1);
$prevmonthtime = make_timestamp($prevmonthtime['year'], $prevmonthtime['month'], $prevmonthtime['day'],
$prevmonthtime['hour'], $prevmonthtime['minute']);
$nextmonth = \core_calendar\api::get_next_month($date['mon'], $date['year']);
$nextmonth = calendar_add_month($date['mon'], $date['year']);
$nextmonthtime = $calendartype->convert_to_gregorian($nextmonth[1], $nextmonth[0], 1);
$nextmonthtime = make_timestamp($nextmonthtime['year'], $nextmonthtime['month'], $nextmonthtime['day'],
$nextmonthtime['hour'], $nextmonthtime['minute']);
$content = html_writer::start_tag('div', array('class' => 'minicalendarblock'));
$content .= \core_calendar\api::get_mini_calendar($calendar->courses, $calendar->groups, $calendar->users,
$content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users,
false, false, 'display', $calendar->courseid, $prevmonthtime);
$content .= html_writer::end_tag('div');
$content .= html_writer::start_tag('div', array('class' => 'minicalendarblock'));
$content .= \core_calendar\api::get_mini_calendar($calendar->courses, $calendar->groups, $calendar->users,
$content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users,
false, false, 'display', $calendar->courseid, $calendar->time);
$content .= html_writer::end_tag('div');
$content .= html_writer::start_tag('div', array('class' => 'minicalendarblock'));
$content .= \core_calendar\api::get_mini_calendar($calendar->courses, $calendar->groups, $calendar->users,
$content .= calendar_get_mini($calendar->courses, $calendar->groups, $calendar->users,
false, false, 'display', $calendar->courseid, $nextmonthtime);
$content .= html_writer::end_tag('div');
return $content;
@@ -168,17 +168,17 @@ class core_calendar_renderer extends plugin_renderer_base {
$returnurl = $this->page->url;
}
$events = \core_calendar\api::get_upcoming($calendar->courses, $calendar->groups, $calendar->users,
$events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users,
1, 100, $calendar->timestamp_today());
$output = html_writer::start_tag('div', array('class'=>'header'));
$output .= $this->course_filter_selector($returnurl, get_string('dayviewfor', 'calendar'));
if (\core_calendar\api::can_add_event_to_course($calendar->course)) {
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');
// Controls
$output .= html_writer::tag('div', \core_calendar\api::get_top_controls('day', array('id' => $calendar->courseid,
$output .= html_writer::tag('div', calendar_top_controls('day', array('id' => $calendar->courseid,
'time' => $calendar->time)), array('class' => 'controls'));
if (empty($events)) {
@@ -192,7 +192,7 @@ class core_calendar_renderer extends plugin_renderer_base {
$event = new calendar_event($event);
$event->calendarcourseid = $calendar->courseid;
if ($event->timestart >= $calendar->timestamp_today() && $event->timestart <= $calendar->timestamp_tomorrow()-1) { // Print it now
$event->time = \core_calendar\api::get_format_event_time($event, time(), null, false,
$event->time = calendar_format_event_time($event, time(), null, false,
$calendar->timestamp_today());
$output .= $this->event($event);
} else { // Save this for later
@@ -205,7 +205,7 @@ class core_calendar_renderer extends plugin_renderer_base {
$output .= html_writer::span(get_string('spanningevents', 'calendar'),
'calendar-information calendar-span-multiple-days');
foreach ($underway as $event) {
$event->time = \core_calendar\api::get_format_event_time($event, time(), null, false,
$event->time = calendar_format_event_time($event, time(), null, false,
$calendar->timestamp_today());
$output .= $this->event($event);
}
@@ -227,12 +227,12 @@ class core_calendar_renderer extends plugin_renderer_base {
public function event(calendar_event $event, $showactions=true) {
global $CFG;
$event = \core_calendar\api::add_event_metadata($event);
$event = calendar_add_event_metadata($event);
$context = $event->context;
$output = '';
$output .= $this->output->box_start('card-header clearfix');
if (\core_calendar\api::can_edit_event($event) && $showactions) {
if (calendar_edit_event_allowed($event) && $showactions) {
if (empty($event->cmid)) {
$editlink = new moodle_url(CALENDAR_URL.'event.php', array('action' => 'edit', 'id' => $event->id));
$deletelink = new moodle_url(CALENDAR_URL.'delete.php', array('id' => $event->id));
@@ -289,7 +289,7 @@ class core_calendar_renderer extends plugin_renderer_base {
$output .= html_writer::tag('span', $event->time, array('class' => 'date pull-xs-right m-r-1'));
} else {
$attrs = array('class' => 'date pull-xs-right m-r-1');
$output .= html_writer::tag('span', \core_calendar\api::get_time_representation($event->timestart), $attrs);
$output .= html_writer::tag('span', calendar_time_representation($event->timestart), $attrs);
}
if (!empty($event->courselink)) {
$output .= html_writer::tag('div', $event->courselink, array('class' => 'course'));
@@ -352,13 +352,13 @@ class core_calendar_renderer extends plugin_renderer_base {
// Get the starting week day for this month.
$startwday = dayofweek(1, $date['mon'], $date['year']);
// Get the days in a week.
$daynames = \core_calendar\api::get_days();
$daynames = calendar_get_days();
// Store the number of days in a week.
$numberofdaysinweek = $calendartype->get_num_weekdays();
$display->minwday = \core_calendar\api::get_starting_weekday();
$display->minwday = calendar_get_starting_weekday();
$display->maxwday = $display->minwday + ($numberofdaysinweek - 1);
$display->maxdays = \core_calendar\api::get_days_in_month($date['mon'], $date['year']);
$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);
@@ -371,7 +371,7 @@ class core_calendar_renderer extends plugin_renderer_base {
}
// Get events from database
$events = \core_calendar\api::get_events($display->tstart, $display->tend, $calendar->users, $calendar->groups,
$events = calendar_get_events($display->tstart, $display->tend, $calendar->users, $calendar->groups,
$calendar->courses);
if (!empty($events)) {
foreach($events as $eventid => $event) {
@@ -386,17 +386,17 @@ class core_calendar_renderer extends plugin_renderer_base {
}
// Extract information: events vs. time
\core_calendar\api::get_events_by_day($events, $date['mon'], $date['year'], $eventsbyday, $durationbyday,
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 (\core_calendar\api::can_add_event_to_course($calendar->course)) {
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', \core_calendar\api::get_top_controls('month', array('id' => $calendar->courseid,
$output .= html_writer::tag('div', calendar_top_controls('month', array('id' => $calendar->courseid,
'time' => $calendar->time)), array('class' => 'controls'));
$table = new html_table();
@@ -443,7 +443,7 @@ class core_calendar_renderer extends plugin_renderer_base {
// Reset vars
$cell = new html_table_cell();
$dayhref = \core_calendar\api::get_link_href(new moodle_url(CALENDAR_URL . 'view.php',
$dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL . 'view.php',
array('view' => 'day', 'course' => $calendar->courseid)), 0, 0, 0, $daytime);
$cellclasses = array();
@@ -539,12 +539,12 @@ class core_calendar_renderer extends plugin_renderer_base {
$returnurl = $this->page->url;
}
$events = \core_calendar\api::get_upcoming($calendar->courses, $calendar->groups, $calendar->users,
$events = calendar_get_upcoming($calendar->courses, $calendar->groups, $calendar->users,
$futuredays, $maxevents);
$output = html_writer::start_tag('div', array('class'=>'header'));
$output .= $this->course_filter_selector($returnurl, get_string('upcomingeventsfor', 'calendar'));
if (\core_calendar\api::can_add_event_to_course($calendar->course)) {
if (calendar_user_can_add_event($calendar->course)) {
$output .= $this->add_event_button($calendar->course->id);
}
$output .= html_writer::end_tag('div');
@@ -686,7 +686,7 @@ class core_calendar_renderer extends plugin_renderer_base {
// Assemble pollinterval control.
$html .= html_writer::start_tag('div', array('style' => 'float:left;'));
$html .= html_writer::start_tag('select', array('name' => 'pollinterval', 'class' => 'custom-select'));
foreach (\core_calendar\api::get_poll_interval_choices() as $k => $v) {
foreach (calendar_get_pollinterval_choices() as $k => $v) {
$attributes = array();
if ($k == $subscription->pollinterval) {
$attributes['selected'] = 'selected';
+4 -4
View File
@@ -44,16 +44,16 @@ $PAGE->set_context(context_system::instance());
switch($var) {
case 'showgroups':
\core_calendar\api::set_event_type_display(CALENDAR_EVENT_GROUP);
calendar_set_event_type_display(CALENDAR_EVENT_GROUP);
break;
case 'showcourses':
\core_calendar\api::set_event_type_display(CALENDAR_EVENT_COURSE);
calendar_set_event_type_display(CALENDAR_EVENT_COURSE);
break;
case 'showglobal':
\core_calendar\api::set_event_type_display(CALENDAR_EVENT_GLOBAL);
calendar_set_event_type_display(CALENDAR_EVENT_GLOBAL);
break;
case 'showuser':
\core_calendar\api::set_event_type_display(CALENDAR_EVENT_USER);
calendar_set_event_type_display(CALENDAR_EVENT_USER);
break;
}
-351
View File
@@ -44,357 +44,6 @@ class core_calendar_api_testcase extends advanced_testcase {
$this->resetAfterTest();
}
public function test_get_course_cached() {
// Setup some test courses.
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$course3 = $this->getDataGenerator()->create_course();
// Load courses into cache.
$coursecache = null;
\core_calendar\api::get_course_cached($coursecache, $course1->id);
\core_calendar\api::get_course_cached($coursecache, $course2->id);
\core_calendar\api::get_course_cached($coursecache, $course3->id);
// Verify the cache.
$this->assertArrayHasKey($course1->id, $coursecache);
$cachedcourse1 = $coursecache[$course1->id];
$this->assertEquals($course1->id, $cachedcourse1->id);
$this->assertEquals($course1->shortname, $cachedcourse1->shortname);
$this->assertEquals($course1->fullname, $cachedcourse1->fullname);
$this->assertArrayHasKey($course2->id, $coursecache);
$cachedcourse2 = $coursecache[$course2->id];
$this->assertEquals($course2->id, $cachedcourse2->id);
$this->assertEquals($course2->shortname, $cachedcourse2->shortname);
$this->assertEquals($course2->fullname, $cachedcourse2->fullname);
$this->assertArrayHasKey($course3->id, $coursecache);
$cachedcourse3 = $coursecache[$course3->id];
$this->assertEquals($course3->id, $cachedcourse3->id);
$this->assertEquals($course3->shortname, $cachedcourse3->shortname);
$this->assertEquals($course3->fullname, $cachedcourse3->fullname);
}
/**
* Test that the get_events() function only returns activity events that are enabled.
*/
public function test_get_events_with_disabled_module() {
global $DB;
$this->setAdminUser();
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$assigninstance = $assigngenerator->create_instance(['course' => $course->id]);
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
$lessoninstance = $lessongenerator->create_instance(['course' => $course->id]);
$student = $generator->create_user();
$generator->enrol_user($student->id, $course->id, 'student');
$this->setUser($student);
$events = [
[
'name' => 'Start of assignment',
'description' => '',
'format' => 1,
'courseid' => $course->id,
'groupid' => 0,
'userid' => 2,
'modulename' => 'assign',
'instance' => $assigninstance->id,
'eventtype' => 'due',
'timestart' => time(),
'timeduration' => 86400,
'visible' => 1
], [
'name' => 'Start of lesson',
'description' => '',
'format' => 1,
'courseid' => $course->id,
'groupid' => 0,
'userid' => 2,
'modulename' => 'lesson',
'instance' => $lessoninstance->id,
'eventtype' => 'end',
'timestart' => time(),
'timeduration' => 86400,
'visible' => 1
]
];
foreach ($events as $event) {
calendar_event::create($event, false);
}
$timestart = time() - 60;
$timeend = time() + 60;
// Get all events.
$events = \core_calendar\api::get_events($timestart, $timeend, true, 0, true);
$this->assertCount(2, $events);
// Disable the lesson module.
$modulerecord = $DB->get_record('modules', ['name' => 'lesson']);
$modulerecord->visible = 0;
$DB->update_record('modules', $modulerecord);
// Check that we only return the assign event.
$events = \core_calendar\api::get_events($timestart, $timeend, true, 0, true);
$this->assertCount(1, $events);
$event = reset($events);
$this->assertEquals('assign', $event->modulename);
}
/**
* Test for calendar_get_events() when there are user and group overrides.
*/
public function test_calendar_get_events_with_overrides() {
global $DB;
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
if (!isset($params['course'])) {
$params['course'] = $course->id;
}
$instance = $plugingenerator->create_instance($params);
// Create users.
$useroverridestudent = $generator->create_user();
$group1student = $generator->create_user();
$group2student = $generator->create_user();
$group12student = $generator->create_user();
$nogroupstudent = $generator->create_user();
// Enrol users.
$generator->enrol_user($useroverridestudent->id, $course->id, 'student');
$generator->enrol_user($group1student->id, $course->id, 'student');
$generator->enrol_user($group2student->id, $course->id, 'student');
$generator->enrol_user($group12student->id, $course->id, 'student');
$generator->enrol_user($nogroupstudent->id, $course->id, 'student');
// Create groups.
$group1 = $generator->create_group(['courseid' => $course->id]);
$group2 = $generator->create_group(['courseid' => $course->id]);
// Add members to groups.
$generator->create_group_member(['groupid' => $group1->id, 'userid' => $group1student->id]);
$generator->create_group_member(['groupid' => $group2->id, 'userid' => $group2student->id]);
$generator->create_group_member(['groupid' => $group1->id, 'userid' => $group12student->id]);
$generator->create_group_member(['groupid' => $group2->id, 'userid' => $group12student->id]);
$now = time();
// Events with the same module name, instance and event type.
$events = [
[
'name' => 'Assignment 1 due date',
'description' => '',
'format' => 0,
'courseid' => $course->id,
'groupid' => 0,
'userid' => 2,
'modulename' => 'assign',
'instance' => $instance->id,
'eventtype' => 'due',
'timestart' => $now,
'timeduration' => 0,
'visible' => 1
], [
'name' => 'Assignment 1 due date - User override',
'description' => '',
'format' => 1,
'courseid' => 0,
'groupid' => 0,
'userid' => $useroverridestudent->id,
'modulename' => 'assign',
'instance' => $instance->id,
'eventtype' => 'due',
'timestart' => $now + 86400,
'timeduration' => 0,
'visible' => 1,
'priority' => CALENDAR_EVENT_USER_OVERRIDE_PRIORITY
], [
'name' => 'Assignment 1 due date - Group A override',
'description' => '',
'format' => 1,
'courseid' => $course->id,
'groupid' => $group1->id,
'userid' => 2,
'modulename' => 'assign',
'instance' => $instance->id,
'eventtype' => 'due',
'timestart' => $now + (2 * 86400),
'timeduration' => 0,
'visible' => 1,
'priority' => 1,
], [
'name' => 'Assignment 1 due date - Group B override',
'description' => '',
'format' => 1,
'courseid' => $course->id,
'groupid' => $group2->id,
'userid' => 2,
'modulename' => 'assign',
'instance' => $instance->id,
'eventtype' => 'due',
'timestart' => $now + (3 * 86400),
'timeduration' => 0,
'visible' => 1,
'priority' => 2,
],
];
foreach ($events as $event) {
calendar_event::create($event, false);
}
$timestart = $now - 100;
$timeend = $now + (3 * 86400);
$groups = [$group1->id, $group2->id];
// Get user override events.
$this->setUser($useroverridestudent);
$events = \core_calendar\api::get_events($timestart, $timeend, $useroverridestudent->id, $groups, $course->id);
$this->assertCount(1, $events);
$event = reset($events);
$this->assertEquals('Assignment 1 due date - User override', $event->name);
// Get event for user with override but with the timestart and timeend parameters only covering the original event.
$events = \core_calendar\api::get_events($timestart, $now, $useroverridestudent->id, $groups, $course->id);
$this->assertCount(0, $events);
// Get events for user that does not belong to any group and has no user override events.
$this->setUser($nogroupstudent);
$events = \core_calendar\api::get_events($timestart, $timeend, $nogroupstudent->id, $groups, $course->id);
$this->assertCount(1, $events);
$event = reset($events);
$this->assertEquals('Assignment 1 due date', $event->name);
// Get events for user that belongs to groups A and B and has no user override events.
$this->setUser($group12student);
$events = \core_calendar\api::get_events($timestart, $timeend, $group12student->id, $groups, $course->id);
$this->assertCount(1, $events);
$event = reset($events);
$this->assertEquals('Assignment 1 due date - Group B override', $event->name);
// Get events for user that belongs to group A and has no user override events.
$this->setUser($group1student);
$events = \core_calendar\api::get_events($timestart, $timeend, $group1student->id, $groups, $course->id);
$this->assertCount(1, $events);
$event = reset($events);
$this->assertEquals('Assignment 1 due date - Group A override', $event->name);
// Add repeating events.
$repeatingevents = [
[
'name' => 'Repeating site event',
'description' => '',
'format' => 1,
'courseid' => SITEID,
'groupid' => 0,
'userid' => 2,
'repeatid' => $event->id,
'modulename' => '0',
'instance' => 0,
'eventtype' => 'site',
'timestart' => $now + 86400,
'timeduration' => 0,
'visible' => 1,
],
[
'name' => 'Repeating site event',
'description' => '',
'format' => 1,
'courseid' => SITEID,
'groupid' => 0,
'userid' => 2,
'repeatid' => $event->id,
'modulename' => '0',
'instance' => 0,
'eventtype' => 'site',
'timestart' => $now + (2 * 86400),
'timeduration' => 0,
'visible' => 1,
],
];
foreach ($repeatingevents as $event) {
calendar_event::create($event, false);
}
// Make sure repeating events are not filtered out.
$events = \core_calendar\api::get_events($timestart, $timeend, true, true, true);
$this->assertCount(3, $events);
}
/**
* Test the update_subscription() function.
*/
public function test_update_subscription() {
$this->resetAfterTest(true);
$subscription = new stdClass();
$subscription->eventtype = 'site';
$subscription->name = 'test';
$id = \core_calendar\api::add_subscription($subscription);
$subscription = \core_calendar\api::get_subscription($id);
$subscription->name = 'awesome';
\core_calendar\api::update_subscription($subscription);
$sub = \core_calendar\api::get_subscription($id);
$this->assertEquals($subscription->name, $sub->name);
$subscription = \core_calendar\api::get_subscription($id);
$subscription->name = 'awesome2';
$subscription->pollinterval = 604800;
\core_calendar\api::update_subscription($subscription);
$sub = \core_calendar\api::get_subscription($id);
$this->assertEquals($subscription->name, $sub->name);
$this->assertEquals($subscription->pollinterval, $sub->pollinterval);
$subscription = new stdClass();
$subscription->name = 'awesome4';
$this->expectException('coding_exception');
\core_calendar\api::update_subscription($subscription);
}
public function test_add_subscription() {
global $DB, $CFG;
require_once($CFG->dirroot . '/lib/bennu/bennu.inc.php');
$this->resetAfterTest(true);
// Test for Microsoft Outlook 2010.
$subscription = new stdClass();
$subscription->name = 'Microsoft Outlook 2010';
$subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
$subscription->eventtype = 'site';
$id = \core_calendar\api::add_subscription($subscription);
$calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/ms_outlook_2010.ics');
$ical = new iCalendar();
$ical->unserialize($calendar);
$this->assertEquals($ical->parser_errors, array());
$sub = \core_calendar\api::get_subscription($id);
\core_calendar\api::import_icalendar_events($ical, $sub->courseid, $sub->id);
$count = $DB->count_records('event', array('subscriptionid' => $sub->id));
$this->assertEquals($count, 1);
// Test for OSX Yosemite.
$subscription = new stdClass();
$subscription->name = 'OSX Yosemite';
$subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
$subscription->eventtype = 'site';
$id = \core_calendar\api::add_subscription($subscription);
$calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/osx_yosemite.ics');
$ical = new iCalendar();
$ical->unserialize($calendar);
$this->assertEquals($ical->parser_errors, array());
$sub = \core_calendar\api::get_subscription($id);
\core_calendar\api::import_icalendar_events($ical, $sub->courseid, $sub->id);
$count = $DB->count_records('event', array('subscriptionid' => $sub->id));
$this->assertEquals($count, 1);
// Test for Google Gmail.
$subscription = new stdClass();
$subscription->name = 'Google Gmail';
$subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
$subscription->eventtype = 'site';
$id = \core_calendar\api::add_subscription($subscription);
$calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/google_gmail.ics');
$ical = new iCalendar();
$ical->unserialize($calendar);
$this->assertEquals($ical->parser_errors, array());
$sub = \core_calendar\api::get_subscription($id);
\core_calendar\api::import_icalendar_events($ical, $sub->courseid, $sub->id);
$count = $DB->count_records('event', array('subscriptionid' => $sub->id));
$this->assertEquals($count, 1);
}
/**
* Requesting calendar events from a given time should return all events with a sort
* time at or after the requested time. All events prior to that time should not
+5 -5
View File
@@ -195,11 +195,11 @@ class core_calendar_type_testcase extends advanced_testcase {
userdate($this->user->timecreated));
// Test the calendar/lib.php functions.
$this->assertEquals($calendar->get_weekdays(), \core_calendar\api::get_days());
$this->assertEquals($calendar->get_starting_weekday(), \core_calendar\api::get_starting_weekday());
$this->assertEquals($calendar->get_num_days_in_month('1986', '9'), \core_calendar\api::get_days_in_month('9', '1986'));
$this->assertEquals($calendar->get_next_month('1986', '9'), \core_calendar\api::get_next_month('9', '1986'));
$this->assertEquals($calendar->get_prev_month('1986', '9'), \core_calendar\api::get_prev_month('9', '1986'));
$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'));
+5 -5
View File
@@ -438,7 +438,7 @@ class core_calendar_events_testcase extends advanced_testcase {
// Trigger and capture the event.
$sink = $this->redirectEvents();
$id = \core_calendar\api::add_subscription($subscription);
$id = calendar_add_subscription($subscription);
$events = $sink->get_events();
$event = reset($events);
@@ -465,13 +465,13 @@ class core_calendar_events_testcase extends advanced_testcase {
$subscription->eventtype = 'site';
$subscription->name = 'test';
$subscription->courseid = $this->course->id;
$subscription->id = \core_calendar\api::add_subscription($subscription);
$subscription->id = calendar_add_subscription($subscription);
// Now edit it.
$subscription->name = 'awesome';
// Trigger and capture the event.
$sink = $this->redirectEvents();
\core_calendar\api::update_subscription($subscription);
calendar_update_subscription($subscription);
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
@@ -497,11 +497,11 @@ class core_calendar_events_testcase extends advanced_testcase {
$subscription->eventtype = 'site';
$subscription->name = 'test';
$subscription->courseid = $this->course->id;
$subscription->id = \core_calendar\api::add_subscription($subscription);
$subscription->id = calendar_add_subscription($subscription);
// Trigger and capture the event.
$sink = $this->redirectEvents();
\core_calendar\api::delete_subscription($subscription);
calendar_delete_subscription($subscription);
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
+395
View File
@@ -0,0 +1,395 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains the class containing unit tests for the calendar lib.
*
* @package core_calendar
* @copyright 2017 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/helpers.php');
/**
* Class contaning unit tests for the calendar lib.
*
* @package core_calendar
* @copyright 2017 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_calendar_lib_testcase extends advanced_testcase {
/**
* Tests set up
*/
protected function setUp() {
$this->resetAfterTest();
}
/**
* Test that the get_events() function only returns activity events that are enabled.
*/
public function test_get_events_with_disabled_module() {
global $DB;
$this->setAdminUser();
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$assigninstance = $assigngenerator->create_instance(['course' => $course->id]);
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
$lessoninstance = $lessongenerator->create_instance(['course' => $course->id]);
$student = $generator->create_user();
$generator->enrol_user($student->id, $course->id, 'student');
$this->setUser($student);
$events = [
[
'name' => 'Start of assignment',
'description' => '',
'format' => 1,
'courseid' => $course->id,
'groupid' => 0,
'userid' => 2,
'modulename' => 'assign',
'instance' => $assigninstance->id,
'eventtype' => 'due',
'timestart' => time(),
'timeduration' => 86400,
'visible' => 1
], [
'name' => 'Start of lesson',
'description' => '',
'format' => 1,
'courseid' => $course->id,
'groupid' => 0,
'userid' => 2,
'modulename' => 'lesson',
'instance' => $lessoninstance->id,
'eventtype' => 'end',
'timestart' => time(),
'timeduration' => 86400,
'visible' => 1
]
];
foreach ($events as $event) {
calendar_event::create($event, false);
}
$timestart = time() - 60;
$timeend = time() + 60;
// Get all events.
$events = calendar_get_events($timestart, $timeend, true, 0, true);
$this->assertCount(2, $events);
// Disable the lesson module.
$modulerecord = $DB->get_record('modules', ['name' => 'lesson']);
$modulerecord->visible = 0;
$DB->update_record('modules', $modulerecord);
// Check that we only return the assign event.
$events = calendar_get_events($timestart, $timeend, true, 0, true);
$this->assertCount(1, $events);
$event = reset($events);
$this->assertEquals('assign', $event->modulename);
}
/**
* Test for calendar_get_events() when there are user and group overrides.
*/
public function test_calendar_get_events_with_overrides() {
global $DB;
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
if (!isset($params['course'])) {
$params['course'] = $course->id;
}
$instance = $plugingenerator->create_instance($params);
// Create users.
$useroverridestudent = $generator->create_user();
$group1student = $generator->create_user();
$group2student = $generator->create_user();
$group12student = $generator->create_user();
$nogroupstudent = $generator->create_user();
// Enrol users.
$generator->enrol_user($useroverridestudent->id, $course->id, 'student');
$generator->enrol_user($group1student->id, $course->id, 'student');
$generator->enrol_user($group2student->id, $course->id, 'student');
$generator->enrol_user($group12student->id, $course->id, 'student');
$generator->enrol_user($nogroupstudent->id, $course->id, 'student');
// Create groups.
$group1 = $generator->create_group(['courseid' => $course->id]);
$group2 = $generator->create_group(['courseid' => $course->id]);
// Add members to groups.
$generator->create_group_member(['groupid' => $group1->id, 'userid' => $group1student->id]);
$generator->create_group_member(['groupid' => $group2->id, 'userid' => $group2student->id]);
$generator->create_group_member(['groupid' => $group1->id, 'userid' => $group12student->id]);
$generator->create_group_member(['groupid' => $group2->id, 'userid' => $group12student->id]);
$now = time();
// Events with the same module name, instance and event type.
$events = [
[
'name' => 'Assignment 1 due date',
'description' => '',
'format' => 0,
'courseid' => $course->id,
'groupid' => 0,
'userid' => 2,
'modulename' => 'assign',
'instance' => $instance->id,
'eventtype' => 'due',
'timestart' => $now,
'timeduration' => 0,
'visible' => 1
], [
'name' => 'Assignment 1 due date - User override',
'description' => '',
'format' => 1,
'courseid' => 0,
'groupid' => 0,
'userid' => $useroverridestudent->id,
'modulename' => 'assign',
'instance' => $instance->id,
'eventtype' => 'due',
'timestart' => $now + 86400,
'timeduration' => 0,
'visible' => 1,
'priority' => CALENDAR_EVENT_USER_OVERRIDE_PRIORITY
], [
'name' => 'Assignment 1 due date - Group A override',
'description' => '',
'format' => 1,
'courseid' => $course->id,
'groupid' => $group1->id,
'userid' => 2,
'modulename' => 'assign',
'instance' => $instance->id,
'eventtype' => 'due',
'timestart' => $now + (2 * 86400),
'timeduration' => 0,
'visible' => 1,
'priority' => 1,
], [
'name' => 'Assignment 1 due date - Group B override',
'description' => '',
'format' => 1,
'courseid' => $course->id,
'groupid' => $group2->id,
'userid' => 2,
'modulename' => 'assign',
'instance' => $instance->id,
'eventtype' => 'due',
'timestart' => $now + (3 * 86400),
'timeduration' => 0,
'visible' => 1,
'priority' => 2,
],
];
foreach ($events as $event) {
calendar_event::create($event, false);
}
$timestart = $now - 100;
$timeend = $now + (3 * 86400);
$groups = [$group1->id, $group2->id];
// Get user override events.
$this->setUser($useroverridestudent);
$events = calendar_get_events($timestart, $timeend, $useroverridestudent->id, $groups, $course->id);
$this->assertCount(1, $events);
$event = reset($events);
$this->assertEquals('Assignment 1 due date - User override', $event->name);
// Get event for user with override but with the timestart and timeend parameters only covering the original event.
$events = calendar_get_events($timestart, $now, $useroverridestudent->id, $groups, $course->id);
$this->assertCount(0, $events);
// Get events for user that does not belong to any group and has no user override events.
$this->setUser($nogroupstudent);
$events = calendar_get_events($timestart, $timeend, $nogroupstudent->id, $groups, $course->id);
$this->assertCount(1, $events);
$event = reset($events);
$this->assertEquals('Assignment 1 due date', $event->name);
// Get events for user that belongs to groups A and B and has no user override events.
$this->setUser($group12student);
$events = calendar_get_events($timestart, $timeend, $group12student->id, $groups, $course->id);
$this->assertCount(1, $events);
$event = reset($events);
$this->assertEquals('Assignment 1 due date - Group B override', $event->name);
// Get events for user that belongs to group A and has no user override events.
$this->setUser($group1student);
$events = calendar_get_events($timestart, $timeend, $group1student->id, $groups, $course->id);
$this->assertCount(1, $events);
$event = reset($events);
$this->assertEquals('Assignment 1 due date - Group A override', $event->name);
// Add repeating events.
$repeatingevents = [
[
'name' => 'Repeating site event',
'description' => '',
'format' => 1,
'courseid' => SITEID,
'groupid' => 0,
'userid' => 2,
'repeatid' => $event->id,
'modulename' => '0',
'instance' => 0,
'eventtype' => 'site',
'timestart' => $now + 86400,
'timeduration' => 0,
'visible' => 1,
],
[
'name' => 'Repeating site event',
'description' => '',
'format' => 1,
'courseid' => SITEID,
'groupid' => 0,
'userid' => 2,
'repeatid' => $event->id,
'modulename' => '0',
'instance' => 0,
'eventtype' => 'site',
'timestart' => $now + (2 * 86400),
'timeduration' => 0,
'visible' => 1,
],
];
foreach ($repeatingevents as $event) {
calendar_event::create($event, false);
}
// Make sure repeating events are not filtered out.
$events = calendar_get_events($timestart, $timeend, true, true, true);
$this->assertCount(3, $events);
}
public function test_get_course_cached() {
// Setup some test courses.
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$course3 = $this->getDataGenerator()->create_course();
// Load courses into cache.
$coursecache = null;
calendar_get_course_cached($coursecache, $course1->id);
calendar_get_course_cached($coursecache, $course2->id);
calendar_get_course_cached($coursecache, $course3->id);
// Verify the cache.
$this->assertArrayHasKey($course1->id, $coursecache);
$cachedcourse1 = $coursecache[$course1->id];
$this->assertEquals($course1->id, $cachedcourse1->id);
$this->assertEquals($course1->shortname, $cachedcourse1->shortname);
$this->assertEquals($course1->fullname, $cachedcourse1->fullname);
$this->assertArrayHasKey($course2->id, $coursecache);
$cachedcourse2 = $coursecache[$course2->id];
$this->assertEquals($course2->id, $cachedcourse2->id);
$this->assertEquals($course2->shortname, $cachedcourse2->shortname);
$this->assertEquals($course2->fullname, $cachedcourse2->fullname);
$this->assertArrayHasKey($course3->id, $coursecache);
$cachedcourse3 = $coursecache[$course3->id];
$this->assertEquals($course3->id, $cachedcourse3->id);
$this->assertEquals($course3->shortname, $cachedcourse3->shortname);
$this->assertEquals($course3->fullname, $cachedcourse3->fullname);
}
/**
* Test the update_subscription() function.
*/
public function test_update_subscription() {
$this->resetAfterTest(true);
$subscription = new stdClass();
$subscription->eventtype = 'site';
$subscription->name = 'test';
$id = calendar_add_subscription($subscription);
$subscription = calendar_get_subscription($id);
$subscription->name = 'awesome';
calendar_update_subscription($subscription);
$sub = calendar_get_subscription($id);
$this->assertEquals($subscription->name, $sub->name);
$subscription = calendar_get_subscription($id);
$subscription->name = 'awesome2';
$subscription->pollinterval = 604800;
calendar_update_subscription($subscription);
$sub = calendar_get_subscription($id);
$this->assertEquals($subscription->name, $sub->name);
$this->assertEquals($subscription->pollinterval, $sub->pollinterval);
$subscription = new stdClass();
$subscription->name = 'awesome4';
$this->expectException('coding_exception');
calendar_update_subscription($subscription);
}
public function test_add_subscription() {
global $DB, $CFG;
require_once($CFG->dirroot . '/lib/bennu/bennu.inc.php');
$this->resetAfterTest(true);
// Test for Microsoft Outlook 2010.
$subscription = new stdClass();
$subscription->name = 'Microsoft Outlook 2010';
$subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
$subscription->eventtype = 'site';
$id = calendar_add_subscription($subscription);
$calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/ms_outlook_2010.ics');
$ical = new iCalendar();
$ical->unserialize($calendar);
$this->assertEquals($ical->parser_errors, array());
$sub = calendar_get_subscription($id);
calendar_import_icalendar_events($ical, $sub->courseid, $sub->id);
$count = $DB->count_records('event', array('subscriptionid' => $sub->id));
$this->assertEquals($count, 1);
// Test for OSX Yosemite.
$subscription = new stdClass();
$subscription->name = 'OSX Yosemite';
$subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
$subscription->eventtype = 'site';
$id = calendar_add_subscription($subscription);
$calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/osx_yosemite.ics');
$ical = new iCalendar();
$ical->unserialize($calendar);
$this->assertEquals($ical->parser_errors, array());
$sub = calendar_get_subscription($id);
calendar_import_icalendar_events($ical, $sub->courseid, $sub->id);
$count = $DB->count_records('event', array('subscriptionid' => $sub->id));
$this->assertEquals($count, 1);
// Test for Google Gmail.
$subscription = new stdClass();
$subscription->name = 'Google Gmail';
$subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
$subscription->eventtype = 'site';
$id = calendar_add_subscription($subscription);
$calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/google_gmail.ics');
$ical = new iCalendar();
$ical->unserialize($calendar);
$this->assertEquals($ical->parser_errors, array());
$sub = calendar_get_subscription($id);
calendar_import_icalendar_events($ical, $sub->courseid, $sub->id);
$count = $DB->count_records('event', array('subscriptionid' => $sub->id));
$this->assertEquals($count, 1);
}
}
+2 -2
View File
@@ -91,7 +91,7 @@ if ($courseid != SITEID && !empty($courseid)) {
navigation_node::override_active_url(new moodle_url('/course/view.php', array('id' => $course->id)));
} else {
$course = get_site();
$courses = \core_calendar\api::get_default_courses();
$courses = calendar_get_default_courses();
$issite = true;
}
@@ -158,7 +158,7 @@ switch($view) {
echo $OUTPUT->container_start('bottom');
if (!empty($CFG->enablecalendarexport)) {
echo $OUTPUT->single_button(new moodle_url('export.php', array('course'=>$courseid)), get_string('exportcalendar', 'calendar'));
if (\core_calendar\api::can_add_event_to_course($course)) {
if (calendar_user_can_add_event($course)) {
echo $OUTPUT->single_button(new moodle_url('/calendar/managesubscriptions.php', array('course'=>$courseid)), get_string('managesubscriptions', 'calendar'));
}
if (isloggedin()) {
+1 -1
View File
@@ -62,7 +62,7 @@ class calendar_cron_task extends scheduled_task {
foreach ($subscriptions as $sub) {
mtrace("Updating calendar subscription {$sub->name} in course {$sub->courseid}");
try {
$log = \core_calendar\api::update_subscription_events($sub->id);
$log = calendar_update_subscription_events($sub->id);
mtrace(trim(strip_tags($log)));
} catch (\moodle_exception $ex) {
mtrace('Error updating calendar subscription: ' . $ex->getMessage());
+2 -2
View File
@@ -53,7 +53,7 @@ class core_calendar_cron_task_testcase extends advanced_testcase {
$subscription->url = $subscriptionurl;
$subscription->pollinterval = 86400;
$subscription->lastupdated = 0;
\core_calendar\api::add_subscription($subscription);
calendar_add_subscription($subscription);
$this->expectOutputRegex('/Events imported: .* Events updated:/');
$task = new \core\task\calendar_cron_task();
@@ -70,7 +70,7 @@ class core_calendar_cron_task_testcase extends advanced_testcase {
$subscription->url = 'brokenurl';
$subscription->pollinterval = 86400;
$subscription->lastupdated = 0;
\core_calendar\api::add_subscription($subscription);
calendar_add_subscription($subscription);
$this->expectOutputRegex('/Error updating calendar subscription: The given iCal URL is invalid/');
$task = new \core\task\calendar_cron_task();
+1 -1
View File
@@ -46,7 +46,7 @@ if (isset($CFG->calendar_maxevents)) {
$calendarform = new core_user\form\calendar_form(null, array('userid' => $user->id));
$user->timeformat = get_user_preferences('calendar_timeformat', '');
$user->startwday = \core_calendar\api::get_starting_weekday();
$user->startwday = calendar_get_starting_weekday();
$user->maxevents = get_user_preferences('calendar_maxevents', $defaultmaxevents);
$user->lookahead = get_user_preferences('calendar_lookahead', $defaultlookahead);
$user->persistflt = get_user_preferences('calendar_persistflt', 0);