From d233adbb775eb93f0c0d4d8ddb845bcce3d812e2 Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Mon, 3 Apr 2017 08:56:27 +0800 Subject: [PATCH] MDL-58460 completion: add callbacks to mod_data for bulk completion Part of MDL-58138 epic --- mod/data/lang/en/data.php | 1 + mod/data/lib.php | 59 +++++++++++++++++++++++++++++++++++++ mod/data/tests/lib_test.php | 38 ++++++++++++++++++++++++ 3 files changed, 98 insertions(+) diff --git a/mod/data/lang/en/data.php b/mod/data/lang/en/data.php index ac9f7abac5e..d3d74271c67 100644 --- a/mod/data/lang/en/data.php +++ b/mod/data/lang/en/data.php @@ -72,6 +72,7 @@ $string['commentsn'] = '{$a} comment(s)'; $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?'; diff --git a/mod/data/lib.php b/mod/data/lib.php index ce175cffdaa..bdba9e9bd66 100644 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -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; +} diff --git a/mod/data/tests/lib_test.php b/mod/data/tests/lib_test.php index a4716f6ada2..d2a525dc620 100644 --- a/mod/data/tests/lib_test.php +++ b/mod/data/tests/lib_test.php @@ -1184,4 +1184,42 @@ class mod_data_lib_testcase extends advanced_testcase { 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()), []); + } }