Navigation Menu

Skip to content

Commit

Permalink
Merge branch 'MDL-58557-master' of git://github.com/ryanwyllie/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Apr 27, 2017
2 parents 8d12f5f + 71cd51b commit 160c263
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
21 changes: 13 additions & 8 deletions mod/choice/lib.php
Expand Up @@ -1198,17 +1198,22 @@ function mod_choice_core_calendar_provide_event_action(calendar_event $event,

$cm = get_fast_modinfo($event->courseid)->instances['choice'][$event->instance];
$choice = $DB->get_record('choice', array('id' => $event->instance), 'id, timeopen, timeclose');
$now = time();

if ($choice->timeopen && $choice->timeclose) {
$actionable = (time() >= $choice->timeopen) && (time() <= $choice->timeclose);
} else if ($choice->timeclose) {
$actionable = time() < $choice->timeclose;
} else if ($choice->timeopen) {
$actionable = time() >= $choice->timeopen;
} else {
$actionable = true;
if ($choice->timeclose && $choice->timeclose < $now) {
// The choice has closed so the user can no longer submit anything.
return null;
}

if (choice_get_my_response($choice)) {
// There is no action if the user has already submitted their choice.
return null;
}

// The choice is actionable if we don't have a start time or the start time is
// in the past.
$actionable = (!$choice->timeopen || $choice->timeopen <= $now);

return $factory->create_instance(
get_string('viewchoices', 'choice'),
new \moodle_url('/mod/choice/view.php', array('id' => $cm->id)),
Expand Down
55 changes: 48 additions & 7 deletions mod/choice/tests/lib_test.php
Expand Up @@ -293,6 +293,51 @@ public function test_choice_core_calendar_provide_event_action_open() {
$this->assertTrue($actionevent->is_actionable());
}

/**
* An event should not have an action if the user has already submitted a response
* to the choice activity.
*/
public function test_choice_core_calendar_provide_event_action_already_submitted() {
global $DB;

$this->resetAfterTest();

$this->setAdminUser();

// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create user.
$student = $this->getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');

// Create a choice.
$choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id,
'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS));
$context = context_module::instance($choice->cmid);
$cm = get_coursemodule_from_instance('choice', $choice->id);

$choicewithoptions = choice_get_choice($choice->id);
$optionids = array_keys($choicewithoptions->option);

choice_user_submit_response($optionids[0], $choice, $student->id, $course, $cm);

// Create a calendar event.
$event = $this->create_action_event($course->id, $choice->id, CHOICE_EVENT_TYPE_OPEN);

// Create an action factory.
$factory = new \core_calendar\action_factory();

$this->setUser($student);

// Decorate action event.
$action = mod_choice_core_calendar_provide_event_action($event, $factory);

// Confirm no action was returned if the user has already submitted the
// choice activity.
$this->assertNull($action);
}

public function test_choice_core_calendar_provide_event_action_closed() {
$this->resetAfterTest();

Expand All @@ -312,14 +357,10 @@ public function test_choice_core_calendar_provide_event_action_closed() {
$factory = new \core_calendar\action_factory();

// Decorate action event.
$actionevent = mod_choice_core_calendar_provide_event_action($event, $factory);
$action = mod_choice_core_calendar_provide_event_action($event, $factory);

// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('viewchoices', 'choice'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertFalse($actionevent->is_actionable());
// Confirm not action was provided for a closed activity.
$this->assertNull($action);
}

public function test_choice_core_calendar_provide_event_action_open_in_future() {
Expand Down

0 comments on commit 160c263

Please sign in to comment.