Merge branch 'MDL-63136-master' of git://github.com/rezaies/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2018-09-11 23:52:02 +02:00
commit 6625cff604
2 changed files with 227 additions and 2 deletions

View File

@ -4457,12 +4457,25 @@ function data_check_updates_since(cm_info $cm, $from, $filter = array()) {
*
* @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_data_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
\core_calendar\action_factory $factory,
int $userid = 0) {
global $USER;
if (!$userid) {
$userid = $USER->id;
}
$cm = get_fast_modinfo($event->courseid, $userid)->instances['data'][$event->instance];
if (!$cm->uservisible) {
// The module is not visible to the user for any reason.
return null;
}
$cm = get_fast_modinfo($event->courseid)->instances['data'][$event->instance];
$now = time();
if (!empty($cm->customdata['timeavailableto']) && $cm->customdata['timeavailableto'] < $now) {

View File

@ -1314,6 +1314,74 @@ class mod_data_lib_testcase extends advanced_testcase {
$this->assertEquals([$datarecor1did, $datarecor2did], $updates->entries->itemids, '', 0, 10, true);
}
public function test_data_core_calendar_provide_event_action_in_hidden_section() {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Create a database activity.
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id,
'timeavailablefrom' => time() - DAYSECS, 'timeavailableto' => time() + DAYSECS));
// Create a calendar event.
$event = $this->create_action_event($course->id, $data->id, DATA_EVENT_TYPE_OPEN);
// Set sections 0 as hidden.
set_section_visible($course->id, 0, 0);
// 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();
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event for the student.
$actionevent = mod_data_core_calendar_provide_event_action($event, $factory, $student->id);
// Confirm the event is not shown at all.
$this->assertNull($actionevent);
}
public function test_data_core_calendar_provide_event_action_for_non_user() {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a database activity.
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id,
'timeavailablefrom' => time() - DAYSECS, 'timeavailableto' => time() + DAYSECS));
// Create a calendar event.
$event = $this->create_action_event($course->id, $data->id, DATA_EVENT_TYPE_OPEN);
// 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();
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$actionevent = mod_data_core_calendar_provide_event_action($event, $factory);
// Confirm the event is not shown at all.
$this->assertNull($actionevent);
}
public function test_data_core_calendar_provide_event_action_open() {
$this->resetAfterTest();
@ -1343,6 +1411,44 @@ class mod_data_lib_testcase extends advanced_testcase {
$this->assertTrue($actionevent->is_actionable());
}
public function test_data_core_calendar_provide_event_action_open_for_user() {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Create a database activity.
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id,
'timeavailablefrom' => time() - DAYSECS, 'timeavailableto' => time() + DAYSECS));
// Create a calendar event.
$event = $this->create_action_event($course->id, $data->id, DATA_EVENT_TYPE_OPEN);
// 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();
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event for the student.
$actionevent = mod_data_core_calendar_provide_event_action($event, $factory, $student->id);
// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('add', 'data'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertTrue($actionevent->is_actionable());
}
public function test_data_core_calendar_provide_event_action_closed() {
$this->resetAfterTest();
@ -1368,6 +1474,37 @@ class mod_data_lib_testcase extends advanced_testcase {
$this->assertNull($actionevent);
}
public function test_data_core_calendar_provide_event_action_closed_for_user() {
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Create a database activity.
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id,
'timeavailableto' => time() - DAYSECS));
// Create a calendar event.
$event = $this->create_action_event($course->id, $data->id, DATA_EVENT_TYPE_OPEN);
// Now log out.
$this->setUser();
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event for the student.
$actionevent = mod_data_core_calendar_provide_event_action($event, $factory, $student->id);
// No event on the dashboard if module is closed.
$this->assertNull($actionevent);
}
public function test_data_core_calendar_provide_event_action_open_in_future() {
$this->resetAfterTest();
@ -1397,6 +1534,44 @@ class mod_data_lib_testcase extends advanced_testcase {
$this->assertFalse($actionevent->is_actionable());
}
public function test_data_core_calendar_provide_event_action_open_in_future_for_user() {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Create a database activity.
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id,
'timeavailablefrom' => time() + DAYSECS));
// Create a calendar event.
$event = $this->create_action_event($course->id, $data->id, DATA_EVENT_TYPE_OPEN);
// 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();
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event for the student.
$actionevent = mod_data_core_calendar_provide_event_action($event, $factory, $student->id);
// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('add', 'data'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertFalse($actionevent->is_actionable());
}
public function test_data_core_calendar_provide_event_action_no_time_specified() {
$this->resetAfterTest();
@ -1425,6 +1600,43 @@ class mod_data_lib_testcase extends advanced_testcase {
$this->assertTrue($actionevent->is_actionable());
}
public function test_data_core_calendar_provide_event_action_no_time_specified_for_user() {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Create a database activity.
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id));
// Create a calendar event.
$event = $this->create_action_event($course->id, $data->id, DATA_EVENT_TYPE_OPEN);
// 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();
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event for the student.
$actionevent = mod_data_core_calendar_provide_event_action($event, $factory, $student->id);
// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('add', 'data'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertTrue($actionevent->is_actionable());
}
/**
* Creates an action event.
*