diff --git a/competency/classes/api.php b/competency/classes/api.php index ffcda6b7fed..cc825050b8f 100644 --- a/competency/classes/api.php +++ b/competency/classes/api.php @@ -1203,6 +1203,31 @@ class api { return $uc; } + /** + * Count the competencies associated to a course module. + * + * @param mixed $cmorid The course module, or its ID. + * @return int + */ + public static function count_course_module_competencies($cmorid) { + static::require_enabled(); + $cm = $cmorid; + if (!is_object($cmorid)) { + $cm = get_coursemodule_from_id('', $cmorid, 0, true, MUST_EXIST); + } + + // Check the user have access to the course module. + self::validate_course_module($cm); + $context = context_module::instance($cm->id); + + $capabilities = array('moodle/competency:coursecompetencyview', 'moodle/competency:coursecompetencymanage'); + if (!has_any_capability($capabilities, $context)) { + throw new required_capability_exception($context, 'moodle/competency:coursecompetencyview', 'nopermissions', ''); + } + + return course_module_competency::count_competencies($cm->id); + } + /** * List the competencies associated to a course module. * @@ -1231,7 +1256,7 @@ class api { $result = array(); // TODO We could improve the performance of this into one single query. - $coursemodulecompetencies = course_competency::list_course_module_competencies($cm->id); + $coursemodulecompetencies = course_module_competency::list_course_module_competencies($cm->id); $competencies = course_module_competency::list_competencies($cm->id); // Build the return values. diff --git a/competency/classes/external.php b/competency/classes/external.php index 0c35f417c26..bd4b3017f1e 100644 --- a/competency/classes/external.php +++ b/competency/classes/external.php @@ -31,6 +31,7 @@ require_once("$CFG->libdir/grade/grade_scale.php"); use context; use context_system; use context_course; +use context_module; use context_helper; use context_user; use coding_exception; @@ -48,6 +49,7 @@ use core_competency\external\competency_exporter; use core_competency\external\competency_framework_exporter; use core_competency\external\course_competency_exporter; use core_competency\external\course_competency_settings_exporter; +use core_competency\external\course_module_competency_exporter; use core_competency\external\evidence_exporter; use core_competency\external\performance_helper; use core_competency\external\plan_exporter; @@ -1274,9 +1276,9 @@ class external extends external_api { foreach ($apiresult as $cmrecord) { $one = new \stdClass(); - $exporter = new competency_exporter($cmrecord['competency']); + $exporter = new competency_exporter($cmrecord['competency'], ['context' => $context]); $one->competency = $exporter->export($output); - $exporter = new course_module_competency_exporter($cmrecord['coursemodulecompetency']); + $exporter = new course_module_competency_exporter($cmrecord['coursemodulecompetency'], ['context' => $context]); $one->coursemodulecompetency = $exporter->export($output); $result[] = (array) $one; @@ -1316,6 +1318,49 @@ class external extends external_api { return new external_function_parameters($params); } + /** + * Returns description of count_course_module_competencies() parameters. + * + * @return \external_function_parameters + */ + public static function count_course_module_competencies_parameters() { + $cmid = new external_value( + PARAM_INT, + 'The course module id', + VALUE_REQUIRED + ); + $params = array( + 'cmid' => $cmid + ); + return new external_function_parameters($params); + } + + /** + * List the course modules using this competency (visible to this user) in this course. + * + * @param int $cmid The course module id to check. + * @return array + */ + public static function count_course_module_competencies($cmid) { + $params = self::validate_parameters(self::count_course_module_competencies_parameters(), array( + 'cmid' => $cmid + )); + + $context = context_module::instance($params['cmid']); + self::validate_context($context); + + return api::count_course_module_competencies($params['cmid']); + } + + /** + * Returns description of count_course_module_competencies() result value. + * + * @return \external_description + */ + public static function count_course_module_competencies_returns() { + return new external_value(PARAM_INT, 'The number of competencies found.'); + } + /** * List the competencies (visible to this user) in this course. * diff --git a/competency/tests/api_test.php b/competency/tests/api_test.php index 3ecf3e9942d..ffe15e344a2 100644 --- a/competency/tests/api_test.php +++ b/competency/tests/api_test.php @@ -2757,6 +2757,16 @@ class core_competency_api_testcase extends advanced_testcase { $result = api::list_course_module_competencies_in_course_module($cm->id); $this->assertEquals($result[0]->get('competencyid'), $c->get('id')); $this->assertEquals($result[1]->get('competencyid'), $c2->get('id')); + + // Now get the course competency and coursemodule competency together. + $result = api::list_course_module_competencies($cm->id); + // Now we should have an array and each element of the array should have a competency and + // a coursemodulecompetency. + foreach ($result as $instance) { + $cmc = $instance['coursemodulecompetency']; + $c = $instance['competency']; + $this->assertEquals($cmc->get('competencyid'), $c->get('id')); + } } /** diff --git a/competency/tests/external_test.php b/competency/tests/external_test.php index 1d72b05410a..83247d136a5 100644 --- a/competency/tests/external_test.php +++ b/competency/tests/external_test.php @@ -2838,4 +2838,50 @@ class core_competency_external_testcase extends externallib_advanced_testcase { $this->assertEquals($filter, $result[0]->shortname); } + /** + * Test that we can list competencies with a course module. + * + * @return void + */ + public function test_list_competencies_with_course_module() { + $this->resetAfterTest(true); + $this->setAdminUser(); + $dg = $this->getDataGenerator(); + $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); + $course = $dg->create_course(); + + $framework = $lpg->create_framework(); + $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'))); + $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'))); + $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'))); + $c4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'))); + $c5 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'))); + + $cc1 = api::add_competency_to_course($course->id, $c1->get('id')); + $cc2 = api::add_competency_to_course($course->id, $c2->get('id')); + $cc3 = api::add_competency_to_course($course->id, $c3->get('id')); + + $pagegenerator = $this->getDataGenerator()->get_plugin_generator('mod_page'); + $page = $pagegenerator->create_instance(array('course' => $course->id)); + + $cm = get_coursemodule_from_instance('page', $page->id); + // Add a link and list again. + $ccm1 = api::add_competency_to_course_module($cm, $c1->get('id')); + $ccm2 = api::add_competency_to_course_module($cm, $c2->get('id')); + + // Test list competencies for this course module. + $total = external::count_course_module_competencies($cm->id); + $result = external::list_course_module_competencies($cm->id); + $this->assertCount($total, $result); + + // Now we should have an array and each element of the array should have a competency and + // a coursemodulecompetency. + foreach ($result as $instance) { + $cmc = $instance['coursemodulecompetency']; + $c = $instance['competency']; + $this->assertEquals($cmc->competencyid, $c->id); + } + + } + } diff --git a/lib/db/services.php b/lib/db/services.php index 1e17cddc654..f6cd4ecee95 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -1966,6 +1966,24 @@ $functions = array( 'capabilities' => 'moodle/competency:competencymanage', 'ajax' => true, ), + 'core_competency_list_course_module_competencies' => array( + 'classname' => 'core_competency\external', + 'methodname' => 'list_course_module_competencies', + 'classpath' => '', + 'description' => 'List the competencies in a course module', + 'type' => 'read', + 'capabilities' => 'moodle/competency:coursecompetencyview', + 'ajax' => true, + ), + 'core_competency_count_course_module_competencies' => array( + 'classname' => 'core_competency\external', + 'methodname' => 'count_course_module_competencies', + 'classpath' => '', + 'description' => 'Count the competencies in a course module', + 'type' => 'read', + 'capabilities' => 'moodle/competency:coursecompetencyview', + 'ajax' => true, + ), 'core_competency_list_course_competencies' => array( 'classname' => 'core_competency\external', 'methodname' => 'list_course_competencies', diff --git a/version.php b/version.php index b111fac9855..0a1f3e267c5 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2019071200.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2019071200.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.