Skip to content

Commit

Permalink
MDL-57879 mod_feedback: add action events
Browse files Browse the repository at this point in the history
Part of MDL-55611 epic.
  • Loading branch information
lameze authored and Damyon Wiese committed Apr 3, 2017
1 parent 6890a91 commit 90e8330
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 4 deletions.
1 change: 1 addition & 0 deletions mod/feedback/lang/en/feedback.php
Expand Up @@ -32,6 +32,7 @@
$string['anonymous_edit'] = 'Record user names';
$string['anonymous_entries'] = 'Anonymous entries ({$a})';
$string['anonymous_user'] = 'Anonymous user';
$string['answerquestions'] = 'Answer the questions';
$string['append_new_items'] = 'Append new items';
$string['autonumbering'] = 'Auto number questions';
$string['autonumbering_help'] = 'Enables or disables automated numbers for each question';
Expand Down
63 changes: 59 additions & 4 deletions mod/feedback/lib.php
Expand Up @@ -40,6 +40,10 @@
define('FEEDBACK_MAX_PIX_LENGTH', '400'); //max. Breite des grafischen Balkens in der Auswertung
define('FEEDBACK_DEFAULT_PAGE_COUNT', 20);

// Event types.
define('FEEDBACK_EVENT_TYPE_OPEN', 'open');
define('FEEDBACK_EVENT_TYPE_CLOSE', 'close');

/**
* Returns all other caps used in module.
*
Expand Down Expand Up @@ -799,13 +803,16 @@ function feedback_set_events($feedback) {

// Feedback start calendar events.
$eventid = $DB->get_field('event', 'id',
array('modulename' => 'feedback', 'instance' => $feedback->id, 'eventtype' => 'open'));
array('modulename' => 'feedback', 'instance' => $feedback->id, 'eventtype' => FEEDBACK_EVENT_TYPE_OPEN));

if (isset($feedback->timeopen) && $feedback->timeopen > 0) {
$event = new stdClass();
$event->eventtype = FEEDBACK_EVENT_TYPE_OPEN;
$event->type = empty($feedback->timeclose) ? CALENDAR_EVENT_TYPE_ACTION : CALENDAR_EVENT_TYPE_STANDARD;
$event->name = get_string('calendarstart', 'feedback', $feedback->name);
$event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule);
$event->timestart = $feedback->timeopen;
$event->timesort = $feedback->timeopen;
$event->visible = instance_is_visible('feedback', $feedback);
$event->timeduration = 0;
if ($eventid) {
Expand All @@ -820,7 +827,7 @@ function feedback_set_events($feedback) {
$event->userid = 0;
$event->modulename = 'feedback';
$event->instance = $feedback->id;
$event->eventtype = 'open';
$event->eventtype = FEEDBACK_EVENT_TYPE_OPEN;
\core_calendar\event::create($event);
}
} else if ($eventid) {
Expand All @@ -831,13 +838,16 @@ function feedback_set_events($feedback) {

// Feedback close calendar events.
$eventid = $DB->get_field('event', 'id',
array('modulename' => 'feedback', 'instance' => $feedback->id, 'eventtype' => 'close'));
array('modulename' => 'feedback', 'instance' => $feedback->id, 'eventtype' => FEEDBACK_EVENT_TYPE_CLOSE));

if (isset($feedback->timeclose) && $feedback->timeclose > 0) {
$event = new stdClass();
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->eventtype = FEEDBACK_EVENT_TYPE_CLOSE;
$event->name = get_string('calendarend', 'feedback', $feedback->name);
$event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule);
$event->timestart = $feedback->timeclose;
$event->timesort = $feedback->timeclose;
$event->visible = instance_is_visible('feedback', $feedback);
$event->timeduration = 0;
if ($eventid) {
Expand All @@ -852,7 +862,6 @@ function feedback_set_events($feedback) {
$event->userid = 0;
$event->modulename = 'feedback';
$event->instance = $feedback->id;
$event->eventtype = 'close';
\core_calendar\event::create($event);
}
} else if ($eventid) {
Expand Down Expand Up @@ -3359,3 +3368,49 @@ function feedback_check_updates_since(cm_info $cm, $from, $filter = array()) {

return $updates;
}

/**
* Is the event visible?
*
* @param \core_calendar\event $event
* @return bool Returns true if the event is visible to the current user, false otherwise.
*/
function mod_feedback_core_calendar_is_event_visible(\core_calendar\event $event) {
$cm = get_fast_modinfo($event->courseid)->instances['feedback'][$event->instance];
$context = context_module::instance($cm->id);
return has_capability('mod/feedback:view', $context);
}

/**
* Handles creating actions for events.
*
* @param \core_calendar\event $event
* @param \core_calendar\action_factory $factory
* @return \core_calendar\local\event\value_objects\action|\core_calendar\local\interfaces\action_interface|null
*/
function mod_feedback_core_calendar_provide_event_action(\core_calendar\event $event,
\core_calendar\action_factory $factory) {
global $DB;

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

$now = time();
if ($feedback->timeopen && $feedback->timeclose) {
$actionable = ($now >= $feedback->timeopen) && ($now <= $feedback->timeclose);
} else if ($feedback->timeclose) {
$actionable = $now < $feedback->timeclose;
} else if ($feedback->timeopen) {
$actionable = $now >= $feedback->timeopen;
} else {
$actionable = true;
}

return $factory->create_instance(
get_string('answerquestions', 'feedback'),
new \moodle_url('/mod/feedback/view.php', ['id' => $cm->id]),
1,
$actionable
);
}

146 changes: 146 additions & 0 deletions mod/feedback/tests/lib_test.php
Expand Up @@ -136,4 +136,150 @@ public function test_check_updates_since() {
$this->assertTrue($updates->attemptsfinished->updated);
$this->assertCount(1, $updates->attemptsfinished->itemids);
}

/**
* Test calendar event visibility.
*/
public function test_feedback_core_calendar_is_event_visible() {
$this->resetAfterTest();
$this->setAdminUser();

$course = $this->getDataGenerator()->create_course();
$feedback = $this->getDataGenerator()->create_module('feedback', ['course' => $course->id]);
$event = $this->create_action_event($course->id, $feedback->id, FEEDBACK_EVENT_TYPE_OPEN);

$this->assertTrue(mod_feedback_core_calendar_is_event_visible($event));
}

/**
* Test calendar event visibility to a non-user.
*/
public function test_feedback_core_calendar_is_event_visible_as_non_user() {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();

$course = $this->getDataGenerator()->create_course();
$feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $course->id));
$event = $this->create_action_event($course->id, $feedback->id, FEEDBACK_EVENT_TYPE_OPEN);

// Log out the user and set force login to true.
\core\session\manager::init_empty_session();
$CFG->forcelogin = true;

$this->assertFalse(mod_feedback_core_calendar_is_event_visible($event));
}

/**
* Test calendar event provide action open.
*/
public function test_feedback_core_calendar_provide_event_action_open() {
$this->resetAfterTest();
$this->setAdminUser();

$now = time();
$course = $this->getDataGenerator()->create_course();
$feedback = $this->getDataGenerator()->create_module('feedback', ['course' => $course->id,
'timeopen' => $now - DAYSECS, 'timeclose' => $now + DAYSECS]);
$event = $this->create_action_event($course->id, $feedback->id, FEEDBACK_EVENT_TYPE_OPEN);

$factory = new \core_calendar\action_factory();
$actionevent = mod_feedback_core_calendar_provide_event_action($event, $factory);

$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('answerquestions', 'feedback'), $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.
*/
public function test_feedback_core_calendar_provide_event_action_closed() {
$this->resetAfterTest();
$this->setAdminUser();

$course = $this->getDataGenerator()->create_course();
$feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $course->id,
'timeclose' => time() - DAYSECS));
$event = $this->create_action_event($course->id, $feedback->id, FEEDBACK_EVENT_TYPE_OPEN);

$factory = new \core_calendar\action_factory();
$actionevent = mod_feedback_core_calendar_provide_event_action($event, $factory);

$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('answerquestions', 'feedback'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertFalse($actionevent->is_actionable());
}

/**
* Test calendar event action open in future.
*
* @throws coding_exception
*/
public function test_feedback_core_calendar_provide_event_action_open_in_future() {
$this->resetAfterTest();
$this->setAdminUser();

$course = $this->getDataGenerator()->create_course();
$feedback = $this->getDataGenerator()->create_module('feedback', ['course' => $course->id,
'timeopen' => time() + DAYSECS]);
$event = $this->create_action_event($course->id, $feedback->id, FEEDBACK_EVENT_TYPE_OPEN);

$factory = new \core_calendar\action_factory();
$actionevent = mod_feedback_core_calendar_provide_event_action($event, $factory);

$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('answerquestions', 'feedback'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertFalse($actionevent->is_actionable());
}

/**
* Test calendar event with no time specified.
*
* @throws coding_exception
*/
public function test_feedback_core_calendar_provide_event_action_no_time_specified() {
$this->resetAfterTest();
$this->setAdminUser();

$course = $this->getDataGenerator()->create_course();
$feedback = $this->getDataGenerator()->create_module('feedback', ['course' => $course->id]);
$event = $this->create_action_event($course->id, $feedback->id, FEEDBACK_EVENT_TYPE_OPEN);

$factory = new \core_calendar\action_factory();
$actionevent = mod_feedback_core_calendar_provide_event_action($event, $factory);

$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('answerquestions', 'feedback'), $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.
*
* @param int $courseid The course id.
* @param int $instanceid The feedback id.
* @param string $eventtype The event type. eg. FEEDBACK_EVENT_TYPE_OPEN.
* @return bool|\core_calendar\event
*/
private function create_action_event($courseid, $instanceid, $eventtype) {
$event = new stdClass();
$event->name = 'Calendar event';
$event->modulename = 'feedback';
$event->courseid = $courseid;
$event->instance = $instanceid;
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->eventtype = $eventtype;
$event->timestart = time();

return \core_calendar\event::create($event);
}
}

0 comments on commit 90e8330

Please sign in to comment.