Merge branch 'MDL-60959-master' of git://github.com/ryanwyllie/moodle

This commit is contained in:
Eloy Lafuente (stronk7)
2018-01-16 01:14:39 +01:00
4 changed files with 185 additions and 75 deletions
+5 -9
View File
@@ -93,6 +93,8 @@ class calendar_event_exporter extends event_exporter_base {
$values = parent::get_other_values($output);
$event = $this->event;
$course = $this->related['course'];
$hascourse = !empty($course);
// By default all events that can be edited are
// draggable.
@@ -109,12 +111,9 @@ class calendar_event_exporter extends event_exporter_base {
$values['editurl'] = $editurl->out(false);
} else if ($event->get_type() == 'category') {
$url = $event->get_category()->get_proxied_instance()->get_view_link();
} else if ($event->get_type() == 'course') {
$url = course_get_url($event->get_course()->get('id') ?: SITEID);
} else {
// TODO MDL-58866 We do not have any way to find urls for events outside of course modules.
$course = $event->get_course()->get('id') ?: SITEID;
$url = course_get_url($course);
$url = course_get_url($hascourse ? $course : SITEID);
}
$values['url'] = $url->out(false);
@@ -165,13 +164,10 @@ class calendar_event_exporter extends event_exporter_base {
}
// Include course's shortname into the event name, if applicable.
$course = $this->event->get_course();
if ($course && $course->get('id') && $course->get('id') !== SITEID) {
if ($hascourse && $course->id !== SITEID) {
$eventnameparams = (object) [
'name' => $values['popupname'],
'course' => format_string($course->get('shortname'), true, [
'context' => $this->related['context'],
])
'course' => $values['course']->shortname,
];
$values['popupname'] = get_string('eventnameandcourse', 'calendar', $eventnameparams);
}
+4 -6
View File
@@ -243,6 +243,7 @@ class event_exporter_base extends exporter {
$event = $this->event;
$legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
$context = $this->related['context'];
$course = $this->related['course'];
$values['isactionevent'] = false;
$values['iscourseevent'] = false;
$values['iscategoryevent'] = false;
@@ -268,10 +269,11 @@ class event_exporter_base extends exporter {
$values['category'] = $categorysummaryexporter->export($output);
}
if ($course = $this->related['course']) {
if ($course) {
$coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
$values['course'] = $coursesummaryexporter->export($output);
}
$courseid = (!$course) ? SITEID : $course->id;
$values['canedit'] = calendar_edit_event_allowed($legacyevent, true);
@@ -290,15 +292,11 @@ class event_exporter_base extends exporter {
$values['formattedtime'] = calendar_format_event_time($legacyevent, time(), null, false,
$timesort);
if ($course = $this->related['course']) {
$coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]);
$values['course'] = $coursesummaryexporter->export($output);
}
if ($group = $event->get_group()) {
$values['groupname'] = format_string($group->get('name'), true,
['context' => \context_course::instance($event->get_course()->get('id'))]);
}
return $values;
}
+15 -60
View File
@@ -38,6 +38,21 @@ use moodle_url;
*/
class week_day_exporter extends day_exporter {
/**
* Constructor.
*
* @param \calendar_information $calendar The calendar information for the period being displayed
* @param mixed $data Either an stdClass or an array of values.
* @param array $related Related objects.
*/
public function __construct(\calendar_information $calendar, $data, $related) {
parent::__construct($calendar, $data, $related);
// Fix the url for today to be based on the today timestamp
// rather than the calendar_information time set in the parent
// constructor.
$this->url->param('time', $this->data[0]);
}
/**
* Return the list of properties.
*
@@ -83,71 +98,11 @@ class week_day_exporter extends day_exporter {
* @return array Keys are the property names, values are their values.
*/
protected function get_other_values(renderer_base $output) {
$timestamp = $this->data[0];
// Need to account for user's timezone.
$usernow = usergetdate(time());
$today = new \DateTimeImmutable();
// The start time should use the day's date but the current
// time of the day (adjusted for user's timezone).
$neweventstarttime = $today->setTimestamp($timestamp)->setTime(
$usernow['hours'],
$usernow['minutes'],
$usernow['seconds']
);
$return = parent::get_other_values($output);
$url = new moodle_url('/calendar/view.php', [
'view' => 'day',
'time' => $timestamp,
]);
if ($this->calendar->course && SITEID !== $this->calendar->course->id) {
$url->param('course', $this->calendar->course->id);
} else if ($this->calendar->categoryid) {
$url->param('category', $this->calendar->categoryid);
}
$return['viewdaylink'] = $url->out(false);
if ($popovertitle = $this->get_popover_title()) {
$return['popovertitle'] = $popovertitle;
}
$cache = $this->related['cache'];
$eventexporters = array_map(function($event) use ($cache, $output, $url) {
$context = $cache->get_context($event);
$course = $cache->get_course($event);
$exporter = new calendar_event_exporter($event, [
'context' => $context,
'course' => $course,
'daylink' => $url,
'type' => $this->related['type'],
'today' => $this->data[0],
]);
return $exporter;
}, $this->related['events']);
$return['events'] = array_map(function($exporter) use ($output) {
return $exporter->export($output);
}, $eventexporters);
if ($popovertitle = $this->get_popover_title()) {
$return['popovertitle'] = $popovertitle;
}
$return['calendareventtypes'] = array_map(function($exporter) {
return $exporter->get_calendar_event_type();
}, $eventexporters);
$return['calendareventtypes'] = array_values(array_unique($return['calendareventtypes']));
$return['haslastdayofevent'] = false;
foreach ($return['events'] as $event) {
if ($event->islastday) {
$return['haslastdayofevent'] = true;
break;
}
}
return $return;
}
@@ -26,6 +26,9 @@ defined('MOODLE_INTERNAL') || die();
use core_calendar\external\calendar_event_exporter;
use core_calendar\local\event\container;
use core_calendar\type_factory;
require_once(__DIR__ . '/helpers.php');
/**
* Calendar event exporter testcase.
@@ -147,4 +150,162 @@ class core_calendar_event_exporter_testcase extends advanced_testcase {
$this->assertEquals($expected, $result['maxdaytimestamp']);
$this->assertEquals($max[1], $result['maxdayerror']);
}
/**
* Exporting a course event should generate the course URL.
*/
public function test_calendar_event_exporter_course_url_course_event() {
global $CFG, $PAGE;
require_once($CFG->dirroot . '/course/lib.php');
$this->resetAfterTest(true);
$this->setAdminUser();
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course = $generator->create_course();
$context = context_course::instance($course->id);
$now = time();
$mapper = container::get_event_mapper();
$legacyevent = create_event([
'courseid' => $course->id,
'userid' => 1,
'eventtype' => 'course',
'timestart' => $now
]);
$event = $mapper->from_legacy_event_to_event($legacyevent);
$exporter = new calendar_event_exporter($event, [
'context' => $context,
'course' => $course,
'moduleinstance' => null,
'daylink' => new moodle_url(''),
'type' => type_factory::get_calendar_instance(),
'today' => $now
]);
$courseurl = course_get_url($course->id);
$expected = $courseurl->out(false);
$renderer = $PAGE->get_renderer('core_calendar');
$exportedevent = $exporter->export($renderer);
// The exported URL should be for the course.
$this->assertEquals($expected, $exportedevent->url);
}
/**
* Exporting a user event should generate the site course URL.
*/
public function test_calendar_event_exporter_course_url_user_event() {
global $CFG, $PAGE;
require_once($CFG->dirroot . '/course/lib.php');
$this->resetAfterTest(true);
$this->setAdminUser();
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$context = context_user::instance($user->id);
$now = time();
$mapper = container::get_event_mapper();
$legacyevent = create_event([
'courseid' => 0,
'userid' => $user->id,
'eventtype' => 'user',
'timestart' => $now
]);
$event = $mapper->from_legacy_event_to_event($legacyevent);
$exporter = new calendar_event_exporter($event, [
'context' => $context,
'course' => null,
'moduleinstance' => null,
'daylink' => new moodle_url(''),
'type' => type_factory::get_calendar_instance(),
'today' => $now
]);
$courseurl = course_get_url(SITEID);
$expected = $courseurl->out(false);
$renderer = $PAGE->get_renderer('core_calendar');
$exportedevent = $exporter->export($renderer);
// The exported URL should be for the site course.
$this->assertEquals($expected, $exportedevent->url);
}
/**
* Popup name respects filters for course shortname.
*/
public function test_calendar_event_exporter_popupname_course_shortname_strips_links() {
global $CFG, $PAGE;
$this->resetAfterTest(true);
$this->setAdminUser();
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$rawshortname = 'Shortname <a href="#">link</a>';
$nolinkshortname = strip_links($rawshortname);
$course = $generator->create_course(['shortname' => $rawshortname]);
$coursecontext = context_course::instance($course->id);
$now = time();
$mapper = container::get_event_mapper();
$renderer = $PAGE->get_renderer('core_calendar');
$legacyevent = create_event([
'courseid' => $course->id,
'userid' => 1,
'eventtype' => 'course',
'timestart' => $now
]);
$event = $mapper->from_legacy_event_to_event($legacyevent);
$exporter = new calendar_event_exporter($event, [
'context' => $coursecontext,
'course' => $course,
'moduleinstance' => null,
'daylink' => new moodle_url(''),
'type' => type_factory::get_calendar_instance(),
'today' => $now
]);
$exportedevent = $exporter->export($renderer);
// Links should always be stripped from the course short name.
$this->assertRegExp("/$nolinkshortname/", $exportedevent->popupname);
}
/**
* Exported event contains the exported course.
*/
public function test_calendar_event_exporter_exports_course() {
global $CFG, $PAGE;
$this->resetAfterTest(true);
$this->setAdminUser();
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$rawshortname = 'Shortname <a href="#">link</a>';
$nolinkshortname = strip_links($rawshortname);
$course = $generator->create_course(['shortname' => $rawshortname]);
$coursecontext = context_course::instance($course->id);
$now = time();
$mapper = container::get_event_mapper();
$renderer = $PAGE->get_renderer('core_calendar');
$legacyevent = create_event([
'courseid' => $course->id,
'userid' => 1,
'eventtype' => 'course',
'timestart' => $now
]);
$event = $mapper->from_legacy_event_to_event($legacyevent);
$exporter = new calendar_event_exporter($event, [
'context' => $coursecontext,
'course' => $course,
'moduleinstance' => null,
'daylink' => new moodle_url(''),
'type' => type_factory::get_calendar_instance(),
'today' => $now
]);
$exportedevent = $exporter->export($renderer);
$courseexporter = new \core_course\external\course_summary_exporter($course, [
'context' => $coursecontext
]);
$exportedcourse = $courseexporter->export($renderer);
$this->assertEquals($exportedevent->course, $exportedcourse);
}
}