Skip to content

Commit

Permalink
MDL-65078 competencies: webservices
Browse files Browse the repository at this point in the history
The webservice list_course_module_competencies calls a function that does not exist and
is not exposed as a webservice.
  • Loading branch information
Damyon Wiese authored and peterRd committed Jul 24, 2019
1 parent f7e1084 commit bde9749
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 4 deletions.
27 changes: 26 additions & 1 deletion competency/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,31 @@ public static function get_user_competency_by_id($usercompetencyid) {
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.
*
Expand Down Expand Up @@ -1231,7 +1256,7 @@ public static function list_course_module_competencies($cmorid) {
$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.
Expand Down
49 changes: 47 additions & 2 deletions competency/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use context;
use context_system;
use context_course;
use context_module;
use context_helper;
use context_user;
use coding_exception;
Expand All @@ -48,6 +49,7 @@
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;
Expand Down Expand Up @@ -1274,9 +1276,9 @@ public static function list_course_module_competencies($cmid) {

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;
Expand Down Expand Up @@ -1316,6 +1318,49 @@ public static function list_course_competencies_parameters() {
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.
*
Expand Down
10 changes: 10 additions & 0 deletions competency/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2757,6 +2757,16 @@ public function test_list_course_modules_using_competency() {
$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'));
}
}

/**
Expand Down
46 changes: 46 additions & 0 deletions competency/tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2838,4 +2838,50 @@ public function test_list_competencies_with_filter() {
$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);
}

}

}
18 changes: 18 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1966,6 +1966,24 @@
'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',
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit bde9749

Please sign in to comment.