MDL-63143 mod_lesson: Add userid param to mod_lesson calendar callbacks

This commit is contained in:
Shamim Rezaie 2018-09-14 20:05:02 +10:00
parent 01389839df
commit a756b01f52
2 changed files with 196 additions and 4 deletions

View file

@ -1653,23 +1653,29 @@ function lesson_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_lesson_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
\core_calendar\action_factory $factory,
int $userid = 0) {
global $DB, $CFG, $USER;
require_once($CFG->dirroot . '/mod/lesson/locallib.php');
$cm = get_fast_modinfo($event->courseid)->instances['lesson'][$event->instance];
if (!$userid) {
$userid = $USER->id;
}
$cm = get_fast_modinfo($event->courseid, $userid)->instances['lesson'][$event->instance];
$lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
if ($lesson->count_user_retries($USER->id)) {
if ($lesson->count_user_retries($userid)) {
// If the user has attempted the lesson then there is no further action for the user.
return null;
}
// Apply overrides.
$lesson->update_effective_access($USER->id);
$lesson->update_effective_access($userid);
return $factory->create_instance(
get_string('startlesson', 'lesson'),

View file

@ -243,6 +243,40 @@ class mod_lesson_lib_testcase extends advanced_testcase {
$this->assertTrue($actionevent->is_actionable());
}
public function test_lesson_core_calendar_provide_event_action_open_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 lesson activity.
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id,
'available' => time() - DAYSECS, 'deadline' => time() + DAYSECS));
// Create a calendar event.
$event = $this->create_action_event($course->id, $lesson->id, LESSON_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_lesson_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('startlesson', 'lesson'), $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_lesson_core_calendar_provide_event_action_closed() {
$this->resetAfterTest();
$this->setAdminUser();
@ -271,6 +305,40 @@ class mod_lesson_lib_testcase extends advanced_testcase {
$this->assertFalse($actionevent->is_actionable());
}
public function test_lesson_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 lesson activity.
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id,
'deadline' => time() - DAYSECS));
// Create a calendar event.
$event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN);
// Now, log out.
$this->setUser();
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$actionevent = mod_lesson_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('startlesson', 'lesson'), $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_lesson_core_calendar_provide_event_action_open_in_future() {
$this->resetAfterTest();
$this->setAdminUser();
@ -299,6 +367,40 @@ class mod_lesson_lib_testcase extends advanced_testcase {
$this->assertFalse($actionevent->is_actionable());
}
public function test_lesson_core_calendar_provide_event_action_open_in_future_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 lesson activity.
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id,
'available' => time() + DAYSECS));
// Create a calendar event.
$event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN);
// Now, log out.
$this->setUser();
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$actionevent = mod_lesson_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('startlesson', 'lesson'), $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_lesson_core_calendar_provide_event_action_no_time_specified() {
$this->resetAfterTest();
$this->setAdminUser();
@ -326,6 +428,39 @@ class mod_lesson_lib_testcase extends advanced_testcase {
$this->assertTrue($actionevent->is_actionable());
}
public function test_lesson_core_calendar_provide_event_action_no_time_specified_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 lesson activity.
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id));
// Create a calendar event.
$event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN);
// Now, log out.
$this->setUser();
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$actionevent = mod_lesson_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('startlesson', 'lesson'), $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_lesson_core_calendar_provide_event_action_after_attempt() {
global $DB;
@ -376,6 +511,57 @@ class mod_lesson_lib_testcase extends advanced_testcase {
$this->assertNull($action);
}
public function test_lesson_core_calendar_provide_event_action_after_attempt_for_user() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create 2 students in the course.
$student1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
$student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Create a lesson activity.
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id));
// Create a calendar event.
$event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN);
$generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
$tfrecord = $generator->create_question_truefalse($lesson);
// Now, do something in the lesson as student1.
$this->setUser($student1);
mod_lesson_external::launch_attempt($lesson->id);
$data = array(
array(
'name' => 'answerid',
'value' => $DB->get_field('lesson_answers', 'id', array('pageid' => $tfrecord->id, 'jumpto' => -1)),
),
array(
'name' => '_qf__lesson_display_answer_form_truefalse',
'value' => 1,
)
);
mod_lesson_external::process_page($lesson->id, $tfrecord->id, $data);
mod_lesson_external::finish_attempt($lesson->id);
// Now, log in as the other student.
$this->setUser($student2);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$action = mod_lesson_core_calendar_provide_event_action($event, $factory, $student1->id);
// Confirm there was no action for the user.
$this->assertNull($action);
}
/**
* Creates an action event.
*