Skip to content

Commit

Permalink
MDL-58138 completion: manual completion may have expected date
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Apr 21, 2017
1 parent 35ec6be commit 694502b
Showing 1 changed file with 45 additions and 30 deletions.
75 changes: 45 additions & 30 deletions completion/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,20 @@ private function get_completion_detail($mod) {

case COMPLETION_TRACKING_AUTOMATIC:
$strings['string'] = get_string('withconditions', 'completion');

// Get the descriptions for all the active completion rules for the module.
if ($ruledescriptions = $this->get_completion_active_rule_descriptions($mod)) {
foreach ($ruledescriptions as $ruledescription) {
$strings['string'] .= \html_writer::empty_tag('br') . $ruledescription;
}
}

$strings['icon'] = $OUTPUT->pix_icon('i/completion-auto-y', get_string('completion_automatic', 'completion'));
break;

default:
$strings['string'] = get_string('none');
break;
}

// Get the descriptions for all the active completion rules for the module.
if ($ruledescriptions = $this->get_completion_active_rule_descriptions($mod)) {
foreach ($ruledescriptions as $ruledescription) {
$strings['string'] .= \html_writer::empty_tag('br') . $ruledescription;
}
}
return $strings;
}

Expand All @@ -170,24 +169,28 @@ private function get_completion_detail($mod) {
protected function get_completion_active_rule_descriptions($moduledata) {
$activeruledescriptions = [];

// Generate the description strings for the core conditional completion rules (if set).
if (!empty($moduledata->completionview)) {
$activeruledescriptions[] = get_string('completionview_desc', 'completion');
}
if ($moduledata instanceof cm_info && !is_null($moduledata->completiongradeitemnumber) ||
($moduledata instanceof stdClass && !empty($moduledata->completionusegrade))) {
$activeruledescriptions[] = get_string('completionusegrade_desc', 'completion');
}
if ($moduledata->completion == COMPLETION_TRACKING_AUTOMATIC) {
// Generate the description strings for the core conditional completion rules (if set).
if (!empty($moduledata->completionview)) {
$activeruledescriptions[] = get_string('completionview_desc', 'completion');
}
if ($moduledata instanceof cm_info && !is_null($moduledata->completiongradeitemnumber) ||
($moduledata instanceof stdClass && !empty($moduledata->completionusegrade))) {
$activeruledescriptions[] = get_string('completionusegrade_desc', 'completion');
}

// Now, ask the module to provide descriptions for its custom conditional completion rules.
if ($customruledescriptions = component_callback($moduledata->modname,
// Now, ask the module to provide descriptions for its custom conditional completion rules.
if ($customruledescriptions = component_callback($moduledata->modname,
'get_completion_active_rule_descriptions', [$moduledata])) {
$activeruledescriptions = array_merge($activeruledescriptions, $customruledescriptions);
$activeruledescriptions = array_merge($activeruledescriptions, $customruledescriptions);
}
}

if (!empty($moduledata->completionexpected)) {
$activeruledescriptions[] = get_string('completionexpecteddesc', 'completion',
userdate($moduledata->completionexpected));
if ($moduledata->completion != COMPLETION_TRACKING_NONE) {
if (!empty($moduledata->completionexpected)) {
$activeruledescriptions[] = get_string('completionexpecteddesc', 'completion',
userdate($moduledata->completionexpected));
}
}

return $activeruledescriptions;
Expand Down Expand Up @@ -297,7 +300,8 @@ public static function get_available_completion_tabs($courseorid) {
* if no module-specific completion rules were added to the form, update of the module table is not needed.
*/
public function apply_completion($data, $updateinstances) {
$updated = [];
$updated = false;
$needreset = [];
$modinfo = get_fast_modinfo($this->courseid);

$cmids = $data->cmid;
Expand All @@ -310,7 +314,11 @@ public function apply_completion($data, $updateinstances) {
foreach ($cmids as $cmid) {
$cm = $modinfo->get_cm($cmid);
if (self::can_edit_bulk_completion($this->courseid, $cm) && $this->apply_completion_cm($cm, $data, $updateinstances)) {
$updated[] = $cm->id;
$updated = true;
if ($cm->completion != COMPLETION_TRACKING_MANUAL || $data['completion'] != COMPLETION_TRACKING_MANUAL) {
// If completion was changed we will need to reset it's state. Exception is when completion was and remains as manual.
$needreset[] = $cm->id;
}
}
}
if ($updated) {
Expand All @@ -319,7 +327,7 @@ public function apply_completion($data, $updateinstances) {
rebuild_course_cache($this->courseid, true);
$modinfo = get_fast_modinfo($this->courseid);
$completion = new \completion_info($modinfo->get_course());
foreach ($updated as $cmid) {
foreach ($needreset as $cmid) {
$completion->reset_all_state($modinfo->get_cm($cmid));
}

Expand All @@ -343,15 +351,21 @@ protected function apply_completion_cm(\cm_info $cm, $data, $updateinstance) {
$defaults = ['completion' => COMPLETION_DISABLED, 'completionview' => COMPLETION_VIEW_NOT_REQUIRED,
'completionexpected' => 0, 'completiongradeitemnumber' => null];

if ($cm->completion == $data['completion'] && $cm->completion != COMPLETION_TRACKING_AUTOMATIC) {
// If old and new completion are either both "manual" or both "none" - no changes are needed.
return false;
}

$data += ['completion' => $cm->completion,
'completionexpected' => $cm->completionexpected,
'completionview' => $cm->completionview];

if ($cm->completion == $data['completion'] && $cm->completion == COMPLETION_TRACKING_NONE) {
// If old and new completion are both "none" - no changes are needed.
return false;
}

if ($cm->completion == $data['completion'] && $cm->completion == COMPLETION_TRACKING_NONE &&
$cm->completionexpected == $data['completionexpected']) {
// If old and new completion are both "manual" and completion expected date is not changed - no changes are needed.
return false;
}

if (array_key_exists('completionusegrade', $data)) {
// Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
$data['completiongradeitemnumber'] = !empty($data['completionusegrade']) ? 0 : null;
Expand All @@ -372,6 +386,7 @@ protected function apply_completion_cm(\cm_info $cm, $data, $updateinstance) {

\core\event\course_module_updated::create_from_cm($cm, $cm->context)->trigger();

// We need to reset completion data for this activity.
return true;
}

Expand Down

0 comments on commit 694502b

Please sign in to comment.