Skip to content

Commit

Permalink
MDL-63143 mod_lesson: Add userid param to mod_lesson calendar callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed Sep 14, 2018
1 parent 175b370 commit e56833c
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 4 deletions.
14 changes: 10 additions & 4 deletions mod/lesson/lib.php
Expand Up @@ -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'),
Expand Down
186 changes: 186 additions & 0 deletions mod/lesson/tests/lib_test.php
Expand Up @@ -243,6 +243,40 @@ public function test_lesson_core_calendar_provide_event_action_open() {
$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();
Expand Down Expand Up @@ -271,6 +305,40 @@ public function test_lesson_core_calendar_provide_event_action_closed() {
$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();
Expand Down Expand Up @@ -299,6 +367,40 @@ public function test_lesson_core_calendar_provide_event_action_open_in_future()
$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();
Expand Down Expand Up @@ -326,6 +428,39 @@ public function test_lesson_core_calendar_provide_event_action_no_time_specified
$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;

Expand Down Expand Up @@ -376,6 +511,57 @@ public function test_lesson_core_calendar_provide_event_action_after_attempt() {
$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.
*
Expand Down

0 comments on commit e56833c

Please sign in to comment.