Skip to content

Commit

Permalink
MDL-58460 completion: add callbacks to mod_data for bulk completion
Browse files Browse the repository at this point in the history
Part of MDL-58138 epic
  • Loading branch information
snake committed Apr 19, 2017
1 parent 6445f17 commit d233adb
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions mod/data/lang/en/data.php
Expand Up @@ -72,6 +72,7 @@
$string['commentsoff'] = 'Comments feature is not enabled';
$string['completionentries'] = 'Require entries';
$string['completionentriescount'] = 'Count of entries';
$string['completionentriesdesc'] = 'Minimum number of entries required: {$a}';
$string['configenablerssfeeds'] = 'This switch will enable the possibility of RSS feeds for all databases. You will still need to turn feeds on manually in the settings for each database.';
$string['confirmdeletefield'] = 'You are about to delete this field, are you sure?';
$string['confirmdeleterecord'] = 'Are you sure you want to delete this entry?';
Expand Down
59 changes: 59 additions & 0 deletions mod/data/lib.php
Expand Up @@ -4320,3 +4320,62 @@ function mod_data_core_calendar_provide_event_action(calendar_event $event,
$actionable
);
}

/**
* Add a get_coursemodule_info function in case any database type wants to add 'extra' information
* for the course (see resource).
*
* Given a course_module object, this function returns any "extra" information that may be needed
* when printing this activity in a course listing. See get_array_of_activities() in course/lib.php.
*
* @param stdClass $coursemodule The coursemodule object (record).
* @return cached_cm_info An object on information that the courses
* will know about (most noticeably, an icon).
*/
function data_get_coursemodule_info($coursemodule) {
global $DB;

$dbparams = ['id' => $coursemodule->instance];
$fields = 'id, completionentries';
if (!$data = $DB->get_record('data', $dbparams, $fields)) {
return false;
}

$result = new cached_cm_info();

// Populate the custom completion rules as key => value pairs, but only if the completion mode is 'automatic'.
if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) {
$result->customdata['customcompletionrules']['completionentries'] = $data->completionentries;
}

return $result;
}

/**
* Callback which returns human-readable strings describing the active completion custom rules for the module instance.
*
* @param cm_info|stdClass $cm object with fields ->completion and ->customdata['customcompletionrules']
* @return array $descriptions the array of descriptions for the custom rules.
*/
function mod_data_get_completion_active_rule_descriptions($cm) {
// Values will be present in cm_info, and we assume these are up to date.
if (empty($cm->customdata['customcompletionrules'])
|| $cm->completion != COMPLETION_TRACKING_AUTOMATIC) {
return [];
}

$descriptions = [];
foreach ($cm->customdata['customcompletionrules'] as $key => $val) {
switch ($key) {
case 'completionentries':
if (empty($val)) {
continue;
}
$descriptions[] = get_string('completionentriesdesc', 'data', $val);
break;
default:
break;
}
}
return $descriptions;
}
38 changes: 38 additions & 0 deletions mod/data/tests/lib_test.php
Expand Up @@ -1184,4 +1184,42 @@ private function create_action_event($courseid, $instanceid, $eventtype) {

return calendar_event::create($event);
}

/**
* Test the callback responsible for returning the completion rule descriptions.
* This function should work given either an instance of the module (cm_info), such as when checking the active rules,
* or if passed a stdClass of similar structure, such as when checking the the default completion settings for a mod type.
*/
public function test_mod_data_completion_get_active_rule_descriptions() {
$this->resetAfterTest();
$this->setAdminUser();

// Two activities, both with automatic completion. One has the 'completionentries' rule, one doesn't.
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 2]);
$data1 = $this->getDataGenerator()->create_module('data', [
'course' => $course->id,
'completion' => 2,
'completionentries' => 3
]);
$data2 = $this->getDataGenerator()->create_module('data', [
'course' => $course->id,
'completion' => 2,
'completionentries' => 0
]);
$cm1 = cm_info::create(get_coursemodule_from_instance('data', $data1->id));
$cm2 = cm_info::create(get_coursemodule_from_instance('data', $data2->id));

// Data for the stdClass input type.
// This type of input would occur when checking the default completion rules for an activity type, where we don't have
// any access to cm_info, rather the input is a stdClass containing completion and customdata attributes, just like cm_info.
$moddefaults = new stdClass();
$moddefaults->customdata = ['customcompletionrules' => ['completionentries' => 3]];
$moddefaults->completion = 2;

$activeruledescriptions = [get_string('completionentriesdesc', 'data', 3)];
$this->assertEquals(mod_data_get_completion_active_rule_descriptions($cm1), $activeruledescriptions);
$this->assertEquals(mod_data_get_completion_active_rule_descriptions($cm2), []);
$this->assertEquals(mod_data_get_completion_active_rule_descriptions($moddefaults), $activeruledescriptions);
$this->assertEquals(mod_data_get_completion_active_rule_descriptions(new stdClass()), []);
}
}

0 comments on commit d233adb

Please sign in to comment.