MDL-63152 mod_workshop: Add userid param to calendar callbacks

This commit is contained in:
Shamim Rezaie 2019-03-05 19:22:14 +11:00
parent 70c84f07d7
commit e3a7647ec3
2 changed files with 96 additions and 2 deletions

View file

@ -1835,12 +1835,18 @@ function workshop_calendar_update(stdClass $workshop, $cmid) {
*
* @param calendar_event $event
* @param \core_calendar\action_factory $factory
* @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
* @return \core_calendar\local\event\entities\action_interface|null
*/
function mod_workshop_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
\core_calendar\action_factory $factory, int $userid = 0) {
global $USER;
$cm = get_fast_modinfo($event->courseid)->instances['workshop'][$event->instance];
if (!$userid) {
$userid = $USER->id;
}
$cm = get_fast_modinfo($event->courseid, $userid)->instances['workshop'][$event->instance];
if (!$cm->uservisible) {
// The module is not visible to the user for any reason.

View file

@ -82,6 +82,36 @@ class mod_workshop_lib_testcase extends advanced_testcase {
$this->assertNull($actionevent);
}
/**
* Test calendar event provide action open when user id is provided.
*/
public function test_workshop_core_calendar_provide_event_action_open_for_user() {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
$now = time();
$course = $this->getDataGenerator()->create_course();
$workshop = $this->getDataGenerator()->create_module('workshop', ['course' => $course->id,
'submissionstart' => $now - DAYSECS, 'submissionend' => $now + DAYSECS]);
$event = $this->create_action_event($course->id, $workshop->id, WORKSHOP_EVENT_TYPE_SUBMISSION_OPEN);
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Now log out.
$CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
$this->setUser();
$factory = new \core_calendar\action_factory();
$actionevent = mod_workshop_core_calendar_provide_event_action($event, $factory, $student->id);
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('viewworkshopsummary', 'workshop'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertTrue($actionevent->is_actionable());
}
/**
* Test calendar event provide action closed.
*/
@ -129,6 +159,35 @@ class mod_workshop_lib_testcase extends advanced_testcase {
$this->assertNull($actionevent);
}
/**
* Test calendar event provide action closed when user id is provided.
*/
public function test_workshop_core_calendar_provide_event_action_closed_for_user() {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$workshop = $this->getDataGenerator()->create_module('workshop', array('course' => $course->id,
'submissionend' => time() - DAYSECS));
$event = $this->create_action_event($course->id, $workshop->id, WORKSHOP_EVENT_TYPE_SUBMISSION_OPEN);
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Now log out.
$CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
$this->setUser();
$factory = new \core_calendar\action_factory();
$actionevent = mod_workshop_core_calendar_provide_event_action($event, $factory, $student->id);
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('viewworkshopsummary', 'workshop'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertTrue($actionevent->is_actionable());
}
/**
* Test calendar event action open in future.
*/
@ -176,6 +235,35 @@ class mod_workshop_lib_testcase extends advanced_testcase {
$this->assertNull($actionevent);
}
/**
* Test calendar event action open in future when user id is provided.
*/
public function test_workshop_core_calendar_provide_event_action_open_in_future_for_user() {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$workshop = $this->getDataGenerator()->create_module('workshop', ['course' => $course->id,
'submissionstart' => time() + DAYSECS]);
$event = $this->create_action_event($course->id, $workshop->id, WORKSHOP_EVENT_TYPE_SUBMISSION_OPEN);
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Now log out.
$CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
$this->setUser();
$factory = new \core_calendar\action_factory();
$actionevent = mod_workshop_core_calendar_provide_event_action($event, $factory, $student->id);
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('viewworkshopsummary', 'workshop'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertTrue($actionevent->is_actionable());
}
/**
* Test calendar event with no time specified.
*/