mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
MDL-57732 mod_choice: added action events
Part of MDL-55611 epic.
This commit is contained in:
parent
1d364a89e9
commit
213dcf5191
@ -126,6 +126,7 @@ $string['spaceleft'] = 'space available';
|
||||
$string['spacesleft'] = 'spaces available';
|
||||
$string['taken'] = 'Taken';
|
||||
$string['viewallresponses'] = 'View {$a} responses';
|
||||
$string['viewchoices'] = 'View choices';
|
||||
$string['withselected'] = 'With selected';
|
||||
$string['userchoosethisoption'] = 'Users who chose this option';
|
||||
$string['yourselection'] = 'Your selection';
|
||||
|
@ -42,6 +42,9 @@ define('CHOICE_SHOWRESULTS_ALWAYS', '3');
|
||||
define('CHOICE_DISPLAY_HORIZONTAL', '0');
|
||||
define('CHOICE_DISPLAY_VERTICAL', '1');
|
||||
|
||||
define('CHOICE_EVENT_TYPE_OPEN', 'open');
|
||||
define('CHOICE_EVENT_TYPE_CLOSE', 'close');
|
||||
|
||||
/** @global array $CHOICE_PUBLISH */
|
||||
global $CHOICE_PUBLISH;
|
||||
$CHOICE_PUBLISH = array (CHOICE_PUBLISH_ANONYMOUS => get_string('publishanonymous', 'choice'),
|
||||
@ -1179,6 +1182,51 @@ function choice_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_choice_core_calendar_is_event_visible(\core_calendar\event $event) {
|
||||
$cm = get_fast_modinfo($event->courseid)->instances['choice'][$event->instance];
|
||||
$context = context_module::instance($cm->id);
|
||||
|
||||
return has_capability('mod/choice: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_choice_core_calendar_provide_event_action(\core_calendar\event $event,
|
||||
\core_calendar\action_factory $factory) {
|
||||
global $DB;
|
||||
|
||||
$cm = get_fast_modinfo($event->courseid)->instances['choice'][$event->instance];
|
||||
$choice = $DB->get_record('choice', array('id' => $event->instance), 'id, timeopen, timeclose');
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return $factory->create_instance(
|
||||
get_string('viewchoices', 'choice'),
|
||||
new \moodle_url('/mod/choice/view.php', array('id' => $cm->id)),
|
||||
1,
|
||||
$actionable
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get icon mapping for font-awesome.
|
||||
*/
|
||||
|
@ -41,15 +41,20 @@ function choice_set_events($choice) {
|
||||
$cm = get_coursemodule_from_instance('choice', $choice->id, $choice->course);
|
||||
$choice->coursemodule = $cm->id;
|
||||
}
|
||||
|
||||
// Choice start calendar events.
|
||||
$event = new stdClass();
|
||||
$event->eventtype = CHOICE_EVENT_TYPE_OPEN;
|
||||
// The CHOICE_EVENT_TYPE_OPEN event should only be an action event if no close time is specified.
|
||||
$event->type = empty($choice->timeclose) ? CALENDAR_EVENT_TYPE_ACTION : CALENDAR_EVENT_TYPE_STANDARD;
|
||||
if ($event->id = $DB->get_field('event', 'id',
|
||||
array('modulename' => 'choice', 'instance' => $choice->id, 'eventtype' => 'open'))) {
|
||||
array('modulename' => 'choice', 'instance' => $choice->id, 'eventtype' => $event->eventtype))) {
|
||||
if ((!empty($choice->timeopen)) && ($choice->timeopen > 0)) {
|
||||
// Calendar event exists so update it.
|
||||
$event->name = get_string('calendarstart', 'choice', $choice->name);
|
||||
$event->description = format_module_intro('choice', $choice, $choice->coursemodule);
|
||||
$event->timestart = $choice->timeopen;
|
||||
$event->timesort = $choice->timeopen;
|
||||
$event->visible = instance_is_visible('choice', $choice);
|
||||
$event->timeduration = 0;
|
||||
$calendarevent = \core_calendar\event::load($event->id);
|
||||
@ -69,8 +74,8 @@ function choice_set_events($choice) {
|
||||
$event->userid = 0;
|
||||
$event->modulename = 'choice';
|
||||
$event->instance = $choice->id;
|
||||
$event->eventtype = 'open';
|
||||
$event->timestart = $choice->timeopen;
|
||||
$event->timesort = $choice->timeopen;
|
||||
$event->visible = instance_is_visible('choice', $choice);
|
||||
$event->timeduration = 0;
|
||||
\core_calendar\event::create($event);
|
||||
@ -79,13 +84,16 @@ function choice_set_events($choice) {
|
||||
|
||||
// Choice end calendar events.
|
||||
$event = new stdClass();
|
||||
$event->type = CALENDAR_EVENT_TYPE_ACTION;
|
||||
$event->eventtype = CHOICE_EVENT_TYPE_CLOSE;
|
||||
if ($event->id = $DB->get_field('event', 'id',
|
||||
array('modulename' => 'choice', 'instance' => $choice->id, 'eventtype' => 'close'))) {
|
||||
array('modulename' => 'choice', 'instance' => $choice->id, 'eventtype' => $event->eventtype))) {
|
||||
if ((!empty($choice->timeclose)) && ($choice->timeclose > 0)) {
|
||||
// Calendar event exists so update it.
|
||||
$event->name = get_string('calendarend', 'choice', $choice->name);
|
||||
$event->description = format_module_intro('choice', $choice, $choice->coursemodule);
|
||||
$event->timestart = $choice->timeclose;
|
||||
$event->timesort = $choice->timeclose;
|
||||
$event->visible = instance_is_visible('choice', $choice);
|
||||
$event->timeduration = 0;
|
||||
$calendarevent = \core_calendar\event::load($event->id);
|
||||
@ -98,7 +106,6 @@ function choice_set_events($choice) {
|
||||
} else {
|
||||
// Event doesn't exist so create one.
|
||||
if ((!empty($choice->timeclose)) && ($choice->timeclose > 0)) {
|
||||
$event = new stdClass();
|
||||
$event->name = get_string('calendarend', 'choice', $choice->name);
|
||||
$event->description = format_module_intro('choice', $choice, $choice->coursemodule);
|
||||
$event->courseid = $choice->course;
|
||||
@ -106,8 +113,8 @@ function choice_set_events($choice) {
|
||||
$event->userid = 0;
|
||||
$event->modulename = 'choice';
|
||||
$event->instance = $choice->id;
|
||||
$event->eventtype = 'close';
|
||||
$event->timestart = $choice->timeclose;
|
||||
$event->timesort = $choice->timeclose;
|
||||
$event->visible = instance_is_visible('choice', $choice);
|
||||
$event->timeduration = 0;
|
||||
\core_calendar\event::create($event);
|
||||
|
@ -262,7 +262,183 @@ class mod_choice_lib_testcase extends externallib_advanced_testcase {
|
||||
$this->assertEquals(false, $status);
|
||||
$this->assertCount(1, $warnings);
|
||||
$this->assertEquals('expired', array_keys($warnings)[0]);
|
||||
|
||||
}
|
||||
|
||||
public function test_choice_core_calendar_is_event_visible() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create a course.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
|
||||
// Create a choice.
|
||||
$choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
|
||||
|
||||
// Create a calendar event.
|
||||
$event = $this->create_action_event($course->id, $choice->id, CHOICE_EVENT_TYPE_OPEN);
|
||||
|
||||
// Check that we can see the event.
|
||||
$this->assertTrue(mod_choice_core_calendar_is_event_visible($event));
|
||||
}
|
||||
|
||||
public function test_choice_core_calendar_is_event_visible_as_non_user() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create a course.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
|
||||
// Create a choice.
|
||||
$choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
|
||||
|
||||
// Create a calendar event.
|
||||
$event = $this->create_action_event($course->id, $choice->id, CHOICE_EVENT_TYPE_OPEN);
|
||||
|
||||
// Log out the user and set force login to true.
|
||||
\core\session\manager::init_empty_session();
|
||||
$CFG->forcelogin = true;
|
||||
|
||||
// Check that we can't see the event.
|
||||
$this->assertFalse(mod_choice_core_calendar_is_event_visible($event));
|
||||
}
|
||||
|
||||
public function test_choice_core_calendar_provide_event_action_open() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create a course.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
|
||||
// Create a choice.
|
||||
$choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id,
|
||||
'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS));
|
||||
|
||||
// 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();
|
||||
|
||||
// Decorate action event.
|
||||
$actionevent = 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->assertTrue($actionevent->is_actionable());
|
||||
}
|
||||
|
||||
public function test_choice_core_calendar_provide_event_action_closed() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create a course.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
|
||||
// Create a choice.
|
||||
$choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id,
|
||||
'timeclose' => time() - DAYSECS));
|
||||
|
||||
// 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();
|
||||
|
||||
// Decorate action event.
|
||||
$actionevent = 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());
|
||||
}
|
||||
|
||||
public function test_choice_core_calendar_provide_event_action_open_in_future() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create a course.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
|
||||
// Create a choice.
|
||||
$choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id,
|
||||
'timeopen' => time() + DAYSECS));
|
||||
|
||||
// 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();
|
||||
|
||||
// Decorate action event.
|
||||
$actionevent = 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());
|
||||
}
|
||||
|
||||
public function test_choice_core_calendar_provide_event_action_no_time_specified() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create a course.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
|
||||
// Create a choice.
|
||||
$choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
|
||||
|
||||
// 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();
|
||||
|
||||
// Decorate action event.
|
||||
$actionevent = 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->assertTrue($actionevent->is_actionable());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an action event.
|
||||
*
|
||||
* @param int $courseid
|
||||
* @param int $instanceid The choice id.
|
||||
* @param string $eventtype The event type. eg. CHOICE_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 = 'choice';
|
||||
$event->courseid = $courseid;
|
||||
$event->instance = $instanceid;
|
||||
$event->type = CALENDAR_EVENT_TYPE_ACTION;
|
||||
$event->eventtype = $eventtype;
|
||||
$event->timestart = time();
|
||||
|
||||
return \core_calendar\event::create($event);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user