MDL-65078 competencies: webservices

The webservice list_course_module_competencies calls a function that does not exist and
is not exposed as a webservice.
This commit is contained in:
Damyon Wiese 2019-03-15 09:44:51 +08:00 committed by Peter
parent f7e108438f
commit bde97497ef
6 changed files with 148 additions and 4 deletions

View File

@ -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.

View File

@ -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.
*

View File

@ -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'));
}
}
/**

View File

@ -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);
}
}
}

View File

@ -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',

View File

@ -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.