Skip to content

Commit

Permalink
MDL-60063 feedback: add drag and drop of open close events
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwyllie committed Oct 17, 2017
1 parent f2c3818 commit 83d4635
Show file tree
Hide file tree
Showing 3 changed files with 811 additions and 0 deletions.
1 change: 1 addition & 0 deletions mod/feedback/lang/en/feedback.php
Expand Up @@ -207,6 +207,7 @@
$string['oldvaluespreserved'] = 'All old questions and the assigned values will be preserved';
$string['oldvalueswillbedeleted'] = 'Current questions and all responses will be deleted.';
$string['only_one_captcha_allowed'] = 'Only one captcha is allowed in a feedback';
$string['openafterclose'] = 'You have specified an open date after the close date';
$string['overview'] = 'Overview';
$string['page'] = 'Page';
$string['page-mod-feedback-x'] = 'Any feedback module page';
Expand Down
150 changes: 150 additions & 0 deletions mod/feedback/lib.php
Expand Up @@ -3556,3 +3556,153 @@ function mod_feedback_get_completion_active_rule_descriptions($cm) {
}
return $descriptions;
}

/**
* This function calculates the minimum and maximum cutoff values for the timestart of
* the given event.
*
* It will return an array with two values, the first being the minimum cutoff value and
* the second being the maximum cutoff value. Either or both values can be null, which
* indicates there is no minimum or maximum, respectively.
*
* If a cutoff is required then the function must return an array containing the cutoff
* timestamp and error string to display to the user if the cutoff value is violated.
*
* A minimum and maximum cutoff return value will look like:
* [
* [1505704373, 'The due date must be after the sbumission start date'],
* [1506741172, 'The due date must be before the cutoff date']
* ]
*
* @param calendar_event $event The calendar event to get the time range for
* @param stdClass|null $instance The module instance to get the range from
* @return array
*/
function mod_feedback_core_calendar_get_valid_event_timestart_range(\calendar_event $event, \stdClass $instance = null) {
global $DB;

if (!$instance) {
$instance = $DB->get_record('feedback', ['id' => $event->instance], '*', MUST_EXIST);
}

$mindate = null;
$maxdate = null;

if ($event->eventtype == FEEDBACK_EVENT_TYPE_OPEN) {
// The start time of the open event can't be equal to or after the
// close time of the choice activity.
if (!empty($instance->timeclose)) {
$maxdate = [
$instance->timeclose,
get_string('openafterclose', 'feedback')
];
}
} else if ($event->eventtype == FEEDBACK_EVENT_TYPE_CLOSE) {
// The start time of the close event can't be equal to or earlier than the
// open time of the choice activity.
if (!empty($instance->timeopen)) {
$mindate = [
$instance->timeopen,
get_string('closebeforeopen', 'feedback')
];
}
}

return [$mindate, $maxdate];
}

/**
* This function will check that the given event is valid for it's
* corresponding feedback module.
*
* An exception is thrown if the event fails validation.
*
* @throws \moodle_exception
* @param \calendar_event $event
*/
function mod_feedback_core_calendar_validate_event_timestart(\calendar_event $event) {
global $DB;

$record = $DB->get_record('feedback', ['id' => $event->instance], '*', MUST_EXIST);
$timestart = $event->timestart;

list($min, $max) = mod_feedback_core_calendar_get_valid_event_timestart_range($event, $record);

if ($min && $timestart < $min[0]) {
throw new \moodle_exception($min[1]);
}

if ($max && $timestart > $max[0]) {
throw new \moodle_exception($max[1]);
}
}

/**
* This function will update the feedback module according to the
* event that has been modified.
*
* It will set the timeopen or timeclose value of the feedback instance
* according to the type of event provided.
*
* @throws \moodle_exception
* @param \calendar_event $event
*/
function mod_feedback_core_calendar_event_timestart_updated(\calendar_event $event) {
global $CFG, $DB;

if (empty($event->instance) || $event->modulename != 'feedback') {
return;
}

$coursemodule = get_coursemodule_from_instance('feedback',
$event->instance,
$event->courseid,
false,
MUST_EXIST);

if (empty($coursemodule)) {
// If we don't have a course module yet then it likely means
// the activity is still being set up. In this case there is
// nothing for us to do anyway.
return;
}

$context = context_module::instance($coursemodule->id);

// The user does not have the capability to modify this activity.
if (!has_capability('moodle/course:manageactivities', $context)) {
return;
}

$modified = false;

if ($event->eventtype == FEEDBACK_EVENT_TYPE_OPEN) {
// If the event is for the feedback activity opening then we should
// set the start time of the feedback activity to be the new start
// time of the event.
$record = $DB->get_record('feedback', ['id' => $event->instance], '*', MUST_EXIST);

if ($record->timeopen != $event->timestart) {
$record->timeopen = $event->timestart;
$record->timemodified = time();
$modified = true;
}
} else if ($event->eventtype == FEEDBACK_EVENT_TYPE_CLOSE) {
// If the event is for the feedback activity closing then we should
// set the end time of the feedback activity to be the new start
// time of the event.
$record = $DB->get_record('feedback', ['id' => $event->instance], '*', MUST_EXIST);

if ($record->timeclose != $event->timestart) {
$record->timeclose = $event->timestart;
$record->timemodified = time();
$modified = true;
}
}

if ($modified) {
$DB->update_record('feedback', $record);
$event = \core\event\course_module_updated::create_from_cm($coursemodule, $context);
$event->trigger();
}
}

0 comments on commit 83d4635

Please sign in to comment.