Skip to content

Commit

Permalink
Merge branch 'MDL-60063-master-2' of git://github.com/ryanwyllie/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Oct 17, 2017
2 parents 21bb56f + 83d4635 commit ae8353a
Show file tree
Hide file tree
Showing 3 changed files with 821 additions and 49 deletions.
1 change: 1 addition & 0 deletions mod/feedback/lang/en/feedback.php
Original file line number Diff line number Diff line change
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
171 changes: 155 additions & 16 deletions mod/feedback/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3440,22 +3440,6 @@ function feedback_check_updates_since(cm_info $cm, $from, $filter = array()) {
return $updates;
}

/**
* The event is only visible anywhere if the user can submit feedback.
*
* @param 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(calendar_event $event) {
global $DB;

$cm = get_fast_modinfo($event->courseid)->instances['feedback'][$event->instance];
$feedbackcompletion = new mod_feedback_completion(null, $cm, 0);

// The event is only visible if the user can submit it.
return $feedbackcompletion->can_complete();
}

/**
* This function receives a calendar event and returns the action associated with it, or null if there is none.
*
Expand All @@ -3477,6 +3461,11 @@ function mod_feedback_core_calendar_provide_event_action(calendar_event $event,
return null;
}

if (!$feedbackcompletion->can_complete()) {
// The user can't complete the feedback so there is no action for them.
return null;
}

// The feedback is actionable if it does not have timeopen or timeopen is in the past.
$actionable = $feedbackcompletion->is_open();

Expand Down Expand Up @@ -3567,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();
}
}
Loading

0 comments on commit ae8353a

Please sign in to comment.