2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This is the external API for this tool.
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once("$CFG->libdir/externallib.php");
28 require_once("$CFG->libdir/grade/grade_scale.php");
37 use external_function_parameters;
39 use external_format_value;
40 use external_single_structure;
41 use external_multiple_structure;
42 use invalid_parameter_exception;
43 use required_capability_exception;
45 use tool_lp\external\competency_framework_exporter;
46 use tool_lp\external\competency_summary_exporter;
47 use tool_lp\external\competency_path_exporter;
48 use tool_lp\external\cohort_summary_exporter;
49 use tool_lp\external\template_statistics_exporter;
50 use tool_lp\external\user_summary_exporter;
51 use tool_lp\external\user_competency_exporter;
52 use tool_lp\external\user_competency_plan_exporter;
53 use tool_lp\external\user_competency_summary_exporter;
54 use tool_lp\external\user_competency_summary_in_course_exporter;
55 use tool_lp\external\user_competency_summary_in_plan_exporter;
56 use tool_lp\external\user_evidence_exporter;
57 use tool_lp\external\user_evidence_summary_exporter;
58 use tool_lp\external\user_evidence_competency_exporter;
59 use tool_lp\external\competency_exporter;
60 use tool_lp\external\course_competency_exporter;
61 use tool_lp\external\course_summary_exporter;
62 use tool_lp\external\course_module_summary_exporter;
63 use tool_lp\external\plan_exporter;
64 use tool_lp\external\template_exporter;
65 use tool_lp\external\evidence_exporter;
66 use tool_lp\output\user_competency_summary_in_plan;
67 use tool_lp\output\user_competency_summary_in_course;
70 * This is the external API for this tool.
72 * @copyright 2015 Damyon Wiese
73 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
75 class external extends external_api {
78 * Returns description of a generic list() parameters.
80 * @return \external_function_parameters
82 protected static function list_parameters_structure() {
83 $filters = new external_multiple_structure(new external_single_structure(
85 'column' => new external_value(PARAM_ALPHANUMEXT, 'Column name to filter by'),
86 'value' => new external_value(PARAM_TEXT, 'Value to filter by. Must be exact match')
89 $sort = new external_value(
95 $order = new external_value(
97 'Sort direction. Should be either ASC or DESC',
101 $skip = new external_value(
103 'Skip this number of records before returning results',
107 $limit = new external_value(
109 'Return this number of records at most.',
115 'filters' => $filters,
121 return new external_function_parameters($params);
125 * Returns a prepared structure to use a context parameters.
126 * @return external_single_structure
128 protected static function get_context_parameters() {
129 $id = new external_value(
131 'Context ID. Either use this value, or level and instanceid.',
135 $level = new external_value(
137 'Context level. To be used with instanceid.',
141 $instanceid = new external_value(
143 'Context instance ID. To be used with level',
147 return new external_single_structure(array(
149 'contextlevel' => $level,
150 'instanceid' => $instanceid,
155 * Returns description of a generic count_x() parameters.
157 * @return \external_function_parameters
159 public static function count_parameters_structure() {
160 $filters = new external_multiple_structure(new external_single_structure(
162 'column' => new external_value(PARAM_ALPHANUMEXT, 'Column name to filter by'),
163 'value' => new external_value(PARAM_TEXT, 'Value to filter by. Must be exact match')
168 'filters' => $filters,
170 return new external_function_parameters($params);
174 * Returns description of create_competency_framework() parameters.
176 * @return \external_function_parameters
178 public static function create_competency_framework_parameters() {
179 $structure = competency_framework_exporter::get_create_structure();
180 $params = array('competencyframework' => $structure);
181 return new external_function_parameters($params);
185 * Create a new competency framework
187 * @param array $competencyframework A single param with all the fields for a competency framework.
188 * @return \stdClass The new record
190 public static function create_competency_framework($competencyframework) {
193 $params = self::validate_parameters(self::create_competency_framework_parameters(),
194 array('competencyframework' => $competencyframework));
196 $params = $params['competencyframework'];
198 $context = self::get_context_from_params($params);
199 self::validate_context($context);
200 $output = $PAGE->get_renderer('tool_lp');
202 unset($params['contextlevel']);
203 unset($params['instanceid']);
204 $params['contextid'] = $context->id;
206 $params = (object) $params;
207 $result = api::create_framework($params);
208 $exporter = new competency_framework_exporter($result);
209 $record = $exporter->export($output);
214 * Returns description of create_competency_framework() result value.
216 * @return \external_description
218 public static function create_competency_framework_returns() {
219 return competency_framework_exporter::get_read_structure();
223 * Returns description of read_competency_framework() parameters.
225 * @return \external_function_parameters
227 public static function read_competency_framework_parameters() {
228 $id = new external_value(
230 'Data base record id for the framework',
237 return new external_function_parameters($params);
241 * Read a competency framework by id.
243 * @param int $id The id of the framework.
246 public static function read_competency_framework($id) {
249 $params = self::validate_parameters(self::read_competency_framework_parameters(),
254 $framework = api::read_framework($params['id']);
255 self::validate_context($framework->get_context());
256 $output = $PAGE->get_renderer('tool_lp');
257 $exporter = new competency_framework_exporter($framework);
258 $record = $exporter->export($output);
263 * Returns description of read_competency_framework() result value.
265 * @return \external_description
267 public static function read_competency_framework_returns() {
268 return competency_framework_exporter::get_read_structure();
272 * Returns description of competency_viewed() parameters.
274 * @return \external_function_parameters
276 public static function competency_viewed_parameters() {
277 $id = new external_value(
285 return new external_function_parameters($params);
289 * Log event competency viewed.
291 * @param int $id The competency ID.
294 public static function competency_viewed($id) {
295 $params = self::validate_parameters(self::competency_viewed_parameters(),
299 return api::competency_viewed($params['id']);
303 * Returns description of competency_viewed() result value.
305 * @return \external_description
307 public static function competency_viewed_returns() {
308 return new external_value(PARAM_BOOL, 'True if the event competency viewed was logged');
312 * Returns description of duplicate_competency_framework() parameters.
314 * @return \external_function_parameters
316 public static function duplicate_competency_framework_parameters() {
317 $id = new external_value(
319 'Data base record id for the framework',
326 return new external_function_parameters($params);
330 * Duplicate a competency framework
332 * @param int $id The competency framework id
335 public static function duplicate_competency_framework($id) {
337 $params = self::validate_parameters(self::duplicate_competency_framework_parameters(),
342 $framework = api::read_framework($params['id']);
343 self::validate_context($framework->get_context());
345 $output = $PAGE->get_renderer('tool_lp');
346 $framework = api::duplicate_framework($params['id']);
347 $exporter = new competency_framework_exporter($framework);
348 $record = $exporter->export($output);
353 * Returns description of duplicate_competency_framework() result value.
355 * @return \external_description
357 public static function duplicate_competency_framework_returns() {
358 return competency_framework_exporter::get_read_structure();
362 * Returns description of delete_competency_framework() parameters.
364 * @return \external_function_parameters
366 public static function delete_competency_framework_parameters() {
367 $id = new external_value(
369 'Data base record id for the framework',
376 return new external_function_parameters($params);
380 * Delete a competency framework
382 * @param int $id The competency framework id
385 public static function delete_competency_framework($id) {
386 $params = self::validate_parameters(self::delete_competency_framework_parameters(),
391 $framework = api::read_framework($params['id']);
392 self::validate_context($framework->get_context());
394 return api::delete_framework($params['id']);
398 * Returns description of delete_competency_framework() result value.
400 * @return \external_description
402 public static function delete_competency_framework_returns() {
403 return new external_value(PARAM_BOOL, 'True if the delete was successful');
407 * Returns description of update_competency_framework() parameters.
409 * @return \external_function_parameters
411 public static function update_competency_framework_parameters() {
412 $structure = competency_framework_exporter::get_update_structure();
413 $params = array('competencyframework' => $structure);
414 return new external_function_parameters($params);
418 * Update an existing competency framework
420 * @param array $competencyframework An array with all the fields for a competency framework.
423 public static function update_competency_framework($competencyframework) {
425 $params = self::validate_parameters(self::update_competency_framework_parameters(),
427 'competencyframework' => $competencyframework
430 $params = $params['competencyframework'];
432 $framework = api::read_framework($params['id']);
433 self::validate_context($framework->get_context());
435 $params = (object) $params;
437 return api::update_framework($params);
441 * Returns description of update_competency_framework() result value.
443 * @return \external_description
445 public static function update_competency_framework_returns() {
446 return new external_value(PARAM_BOOL, 'True if the update was successful');
450 * Returns description of list_competency_frameworks() parameters.
452 * @return \external_function_parameters
454 public static function list_competency_frameworks_parameters() {
455 $sort = new external_value(
457 'Column to sort by.',
461 $order = new external_value(
463 'Sort direction. Should be either ASC or DESC',
467 $skip = new external_value(
469 'Skip this number of records before returning results',
473 $limit = new external_value(
475 'Return this number of records at most.',
479 $includes = new external_value(
481 'What other contextes to fetch the frameworks from. (children, parents, self)',
485 $onlyvisible = new external_value(
487 'Only visible frameworks will be returned if visible true',
491 $query = new external_value(
493 'A query string to filter the results',
503 'context' => self::get_context_parameters(),
504 'includes' => $includes,
505 'onlyvisible' => $onlyvisible,
508 return new external_function_parameters($params);
512 * List the existing competency frameworks
515 * @param string $order
516 * @param string $skip
518 * @param array $context
519 * @param bool $includes
520 * @param bool $onlyvisible
521 * @param string $query
524 * @throws \required_capability_exception
525 * @throws invalid_parameter_exception
527 public static function list_competency_frameworks($sort, $order, $skip, $limit, $context, $includes, $onlyvisible,
531 $params = self::validate_parameters(self::list_competency_frameworks_parameters(),
537 'context' => $context,
538 'includes' => $includes,
539 'onlyvisible' => $onlyvisible,
543 $context = self::get_context_from_params($params['context']);
544 self::validate_context($context);
545 $output = $PAGE->get_renderer('tool_lp');
547 if ($params['order'] !== '' && $params['order'] !== 'ASC' && $params['order'] !== 'DESC') {
548 throw new invalid_parameter_exception('Invalid order param. Must be ASC, DESC or empty.');
551 $results = api::list_frameworks($params['sort'],
557 $params['onlyvisible'],
560 foreach ($results as $result) {
561 $exporter = new competency_framework_exporter($result);
562 $record = $exporter->export($output);
563 array_push($records, $record);
569 * Returns description of list_competency_frameworks() result value.
571 * @return \external_description
573 public static function list_competency_frameworks_returns() {
574 return new external_multiple_structure(competency_framework_exporter::get_read_structure());
578 * Returns description of count_competency_frameworks() parameters.
580 * @return \external_function_parameters
582 public static function count_competency_frameworks_parameters() {
583 $includes = new external_value(
585 'What other contextes to fetch the frameworks from. (children, parents, self)',
591 'context' => self::get_context_parameters(),
592 'includes' => $includes
594 return new external_function_parameters($params);
598 * Count the existing competency frameworks
600 * @param array $context
601 * @param string $includes
604 public static function count_competency_frameworks($context, $includes) {
605 $params = self::validate_parameters(self::count_competency_frameworks_parameters(),
607 'context' => $context,
608 'includes' => $includes
611 $context = self::get_context_from_params($params['context']);
612 self::validate_context($context);
614 return api::count_frameworks($context, $params['includes']);
618 * Returns description of count_competency_frameworks() result value.
620 * @return \external_description
622 public static function count_competency_frameworks_returns() {
623 return new external_value(PARAM_INT, 'The number of competency frameworks found.');
627 * Returns description of competency_framework_viewed() parameters.
629 * @return \external_function_parameters
631 public static function competency_framework_viewed_parameters() {
632 $id = new external_value(
634 'The competency framework id',
641 return new external_function_parameters($params);
645 * Log event competency framework viewed.
647 * @param int $id The competency framework ID.
650 public static function competency_framework_viewed($id) {
651 $params = self::validate_parameters(self::competency_framework_viewed_parameters(),
655 return api::competency_framework_viewed($params['id']);
660 * Returns description of competency_framework_viewed() result value.
662 * @return \external_description
664 public static function competency_framework_viewed_returns() {
665 return new external_value(PARAM_BOOL, 'True if the event competency framework was logged');
669 * Returns description of data_for_competency_frameworks_manage_page() parameters.
671 * @return \external_function_parameters
673 public static function data_for_competency_frameworks_manage_page_parameters() {
674 $params = array('pagecontext' => self::get_context_parameters());
675 return new external_function_parameters($params);
679 * Loads the data required to render the competency_frameworks_manage_page template.
681 * @param context $pagecontext The page context
684 public static function data_for_competency_frameworks_manage_page($pagecontext) {
687 $params = self::validate_parameters(
688 self::data_for_competency_frameworks_manage_page_parameters(),
690 'pagecontext' => $pagecontext
693 $context = self::get_context_from_params($params['pagecontext']);
694 self::validate_context($context);
696 $renderable = new output\manage_competency_frameworks_page($context);
697 $renderer = $PAGE->get_renderer('tool_lp');
699 $data = $renderable->export_for_template($renderer);
705 * Returns description of data_for_competency_frameworks_manage_page() result value.
707 * @return \external_description
709 public static function data_for_competency_frameworks_manage_page_returns() {
710 return new external_single_structure(array (
711 'competencyframeworks' => new external_multiple_structure(
712 competency_framework_exporter::get_read_structure()
714 'pluginbaseurl' => new external_value(PARAM_LOCALURL, 'Url to the tool_lp plugin folder on this Moodle site'),
715 'navigation' => new external_multiple_structure(
716 new external_value(PARAM_RAW, 'HTML for a navigation item that should be on this page')
718 'pagecontextid' => new external_value(PARAM_INT, 'The page context id')
724 * Returns description of create_competency() parameters.
726 * @return \external_function_parameters
728 public static function create_competency_parameters() {
729 $structure = competency_exporter::get_create_structure();
730 $params = array('competency' => $structure);
731 return new external_function_parameters($params);
735 * Create a new competency
737 * @param array $competency All the fields for a competency record (including id)
738 * @return array the competency
740 public static function create_competency($competency) {
743 $params = self::validate_parameters(self::create_competency_parameters(),
744 array('competency' => $competency));
746 $params = $params['competency'];
747 $framework = api::read_framework($params['competencyframeworkid']);
748 $context = $framework->get_context();
749 self::validate_context($context);
750 $output = $PAGE->get_renderer('tool_lp');
752 $params = (object) $params;
753 $result = api::create_competency($params);
754 $exporter = new competency_exporter($result, array('context' => $context));
755 $record = $exporter->export($output);
760 * Returns description of create_competency() result value.
762 * @return \external_description
764 public static function create_competency_returns() {
765 return competency_exporter::get_read_structure();
769 * Returns description of read_competency() parameters.
771 * @return \external_function_parameters
773 public static function read_competency_parameters() {
774 $id = new external_value(
776 'Data base record id for the competency',
783 return new external_function_parameters($params);
787 * Read a competency by id.
789 * @param int $id The id of the competency
792 public static function read_competency($id) {
795 $params = self::validate_parameters(self::read_competency_parameters(),
800 $competency = api::read_competency($params['id']);
801 $context = $competency->get_context();
802 self::validate_context($context);
803 $output = $PAGE->get_renderer('tool_lp');
804 $exporter = new competency_exporter($competency, array('context' => $context));
805 $record = $exporter->export($output);
810 * Returns description of read_competency() result value.
812 * @return \external_description
814 public static function read_competency_returns() {
815 return competency_exporter::get_read_structure();
819 * Returns description of delete_competency() parameters.
821 * @return \external_function_parameters
823 public static function delete_competency_parameters() {
824 $id = new external_value(
826 'Data base record id for the competency',
833 return new external_function_parameters($params);
837 * Delete a competency
839 * @param int $id The competency id
842 public static function delete_competency($id) {
843 $params = self::validate_parameters(self::delete_competency_parameters(),
848 $competency = api::read_competency($params['id']);
849 $context = $competency->get_context();
850 self::validate_context($context);
852 return api::delete_competency($params['id']);
856 * Returns description of delete_competency() result value.
858 * @return \external_description
860 public static function delete_competency_returns() {
861 return new external_value(PARAM_BOOL, 'True if the delete was successful');
865 * Returns description of update_competency() parameters.
867 * @return \external_function_parameters
869 public static function update_competency_parameters() {
870 $structure = competency_exporter::get_update_structure();
871 $params = array('competency' => $structure);
872 return new external_function_parameters($params);
876 * Update an existing competency
878 * @param array $competency The array of competency fields (id is required).
881 public static function update_competency($competency) {
883 $params = self::validate_parameters(self::update_competency_parameters(),
884 array('competency' => $competency));
885 $params = $params['competency'];
887 $competency = api::read_competency($params['id']);
888 self::validate_context($competency->get_context());
890 $params = (object) $params;
892 return api::update_competency($params);
896 * Returns description of update_competency_framework() result value.
898 * @return \external_description
900 public static function update_competency_returns() {
901 return new external_value(PARAM_BOOL, 'True if the update was successful');
905 * Returns description of list_competencies() parameters.
907 * @return \external_function_parameters
909 public static function list_competencies_parameters() {
910 return self::list_parameters_structure();
914 * List the existing competency.
916 * @param string $filters
918 * @param string $order
919 * @param string $skip
923 * @throws \required_capability_exception
924 * @throws invalid_parameter_exception
926 public static function list_competencies($filters, $sort, $order, $skip, $limit) {
929 $params = self::validate_parameters(self::list_competencies_parameters(),
931 'filters' => $filters,
938 if ($params['order'] !== '' && $params['order'] !== 'ASC' && $params['order'] !== 'DESC') {
939 throw new invalid_parameter_exception('Invalid order param. Must be ASC, DESC or empty.');
942 $safefilters = array();
943 $validcolumns = array('id', 'shortname', 'description', 'sortorder',
944 'idnumber', 'parentid', 'competencyframeworkid');
945 foreach ($params['filters'] as $filter) {
946 if (!in_array($filter->column, $validcolumns)) {
947 throw new invalid_parameter_exception('Filter column was invalid');
949 $safefilters[$filter->column] = $filter->value;
953 if (isset($safefilters['competencyframeworkid'])) {
954 $framework = api::read_framework($safefilters['competencyframeworkid']);
955 $context = $framework->get_context();
957 $context = context_system::instance();
960 self::validate_context($context);
961 $output = $PAGE->get_renderer('tool_lp');
963 $results = api::list_competencies(
972 foreach ($results as $result) {
973 $exporter = new competency_exporter($result, array('context' => $context));
974 $record = $exporter->export($output);
975 array_push($records, $record);
981 * Returns description of list_competencies() result value.
983 * @return \external_description
985 public static function list_competencies_returns() {
986 return new external_multiple_structure(competency_exporter::get_read_structure());
990 * Returns description of search_competencies() parameters.
992 * @return \external_function_parameters
994 public static function search_competencies_parameters() {
995 $searchtext = new external_value(
997 'Text to search for',
1000 $frameworkid = new external_value(
1002 'Competency framework id',
1007 'searchtext' => $searchtext,
1008 'competencyframeworkid' => $frameworkid
1010 return new external_function_parameters($params);
1014 * List the existing competency frameworks
1016 * @param string $searchtext Text to search.
1017 * @param int $competencyframeworkid Framework id.
1021 public static function search_competencies($searchtext, $competencyframeworkid) {
1024 $params = self::validate_parameters(self::search_competencies_parameters(),
1026 'searchtext' => $searchtext,
1027 'competencyframeworkid' => $competencyframeworkid
1030 $framework = api::read_framework($params['competencyframeworkid']);
1031 $context = $framework->get_context();
1032 self::validate_context($context);
1033 $output = $PAGE->get_renderer('tool_lp');
1035 $results = api::search_competencies($params['searchtext'], $params['competencyframeworkid']);
1037 foreach ($results as $result) {
1038 $exporter = new competency_exporter($result, array('context' => $context));
1039 $record = $exporter->export($output);
1041 array_push($records, $record);
1048 * Returns description of search_competencies() result value.
1050 * @return \external_description
1052 public static function search_competencies_returns() {
1053 return new external_multiple_structure(competency_exporter::get_read_structure());
1057 * Returns description of count_competencies() parameters.
1059 * @return \external_function_parameters
1061 public static function count_competencies_parameters() {
1062 return self::count_parameters_structure();
1066 * Count the existing competency frameworks.
1068 * @param string $filters Filters to use.
1071 public static function count_competencies($filters) {
1072 $params = self::validate_parameters(self::count_competencies_parameters(),
1074 'filters' => $filters
1077 $safefilters = array();
1078 $validcolumns = array('id', 'shortname', 'description', 'sortorder', 'idnumber',
1079 'parentid', 'competencyframeworkid');
1080 foreach ($params['filters'] as $filter) {
1081 if (!in_array($filter['column'], $validcolumns)) {
1082 throw new invalid_parameter_exception('Filter column was invalid');
1084 $safefilters[$filter['column']] = $filter['value'];
1088 if (isset($safefilters['competencyframeworkid'])) {
1089 $framework = api::read_framework($safefilters['competencyframeworkid']);
1090 $context = $framework->get_context();
1092 $context = context_system::instance();
1095 self::validate_context($context);
1097 return api::count_competencies($safefilters);
1101 * Returns description of count_competencies() result value.
1103 * @return \external_description
1105 public static function count_competencies_returns() {
1106 return new external_value(PARAM_INT, 'The number of competencies found.');
1110 * Returns description of data_for_competencies_manage_page() parameters.
1112 * @return \external_function_parameters
1114 public static function data_for_competencies_manage_page_parameters() {
1115 $competencyframeworkid = new external_value(
1117 'The competency framework id',
1120 $search = new external_value(
1127 'competencyframeworkid' => $competencyframeworkid,
1130 return new external_function_parameters($params);
1134 * Loads the data required to render the competencies_manage_page template.
1136 * @param int $competencyframeworkid Framework id.
1137 * @param string $search Text to search.
1141 public static function data_for_competencies_manage_page($competencyframeworkid, $search) {
1144 $params = self::validate_parameters(self::data_for_competencies_manage_page_parameters(),
1146 'competencyframeworkid' => $competencyframeworkid,
1150 $framework = api::read_framework($params['competencyframeworkid']);
1151 self::validate_context($framework->get_context());
1152 $output = $PAGE->get_renderer('tool_lp');
1154 $renderable = new output\manage_competencies_page($framework, $params['search'], $framework->get_context());
1156 $data = $renderable->export_for_template($output);
1162 * Returns description of data_for_competencies_manage_page() result value.
1164 * @return \external_description
1166 public static function data_for_competencies_manage_page_returns() {
1167 return new external_single_structure(array (
1168 'framework' => competency_framework_exporter::get_read_structure(),
1169 'canmanage' => new external_value(PARAM_BOOL, 'True if this user has permission to manage competency frameworks'),
1170 'pagecontextid' => new external_value(PARAM_INT, 'Context id for the framework'),
1171 'search' => new external_value(PARAM_RAW, 'Current search string'),
1172 'rulesmodules' => new external_value(PARAM_RAW, 'JSON encoded data for rules')
1178 * Returns description of data_for_competency_summary() parameters.
1180 * @return \external_function_parameters
1182 public static function data_for_competency_summary_parameters() {
1183 $competencyid = new external_value(
1185 'The competency id',
1188 $includerelated = new external_value(
1190 'Include or not related competencies',
1194 $includecourses = new external_value(
1196 'Include or not competency courses',
1201 'competencyid' => $competencyid,
1202 'includerelated' => $includerelated,
1203 'includecourses' => $includecourses
1205 return new external_function_parameters($params);
1209 * Loads the data required to render the competency_page template.
1211 * @param int $competencyid Competency id.
1212 * @param boolean $includerelated Include or not related competencies.
1213 * @param boolean $includecourses Include or not competency courses.
1217 public static function data_for_competency_summary($competencyid, $includerelated = false, $includecourses = false) {
1219 $params = self::validate_parameters(self::data_for_competency_summary_parameters(),
1221 'competencyid' => $competencyid,
1222 'includerelated' => $includerelated,
1223 'includecourses' => $includecourses
1226 $competency = api::read_competency($params['competencyid']);
1227 $framework = api::read_framework($competency->get_competencyframeworkid());
1228 $renderable = new output\competency_summary($competency, $framework, $params['includerelated'], $params['includecourses']);
1229 $renderer = $PAGE->get_renderer('tool_lp');
1231 $data = $renderable->export_for_template($renderer);
1237 * Returns description of data_for_competency_summary_() result value.
1239 * @return \external_description
1241 public static function data_for_competency_summary_returns() {
1242 return competency_summary_exporter::get_read_structure();
1246 * Returns description of set_parent_competency() parameters.
1248 * @return \external_function_parameters
1250 public static function set_parent_competency_parameters() {
1251 $competencyid = new external_value(
1253 'The competency id',
1256 $parentid = new external_value(
1258 'The new competency parent id',
1262 'competencyid' => $competencyid,
1263 'parentid' => $parentid
1265 return new external_function_parameters($params);
1269 * Move the competency to a new parent.
1271 * @param int $competencyid Competency id.
1272 * @param int $parentid Parent id.
1276 public static function set_parent_competency($competencyid, $parentid) {
1277 $params = self::validate_parameters(self::set_parent_competency_parameters(),
1279 'competencyid' => $competencyid,
1280 'parentid' => $parentid
1283 $competency = api::read_competency($params['competencyid']);
1284 self::validate_context($competency->get_context());
1286 return api::set_parent_competency($params['competencyid'], $params['parentid']);
1290 * Returns description of set_parent_competency() result value.
1292 * @return \external_description
1294 public static function set_parent_competency_returns() {
1295 return new external_value(PARAM_BOOL, 'True if the update was successful');
1299 * Returns description of move_up_competency() parameters.
1301 * @return \external_function_parameters
1303 public static function move_up_competency_parameters() {
1304 $competencyid = new external_value(
1306 'The competency id',
1310 'id' => $competencyid,
1312 return new external_function_parameters($params);
1316 * Change the sort order of a competency.
1318 * @param int $competencyid Competency id.
1321 public static function move_up_competency($competencyid) {
1322 $params = self::validate_parameters(self::move_up_competency_parameters(),
1324 'id' => $competencyid,
1327 $competency = api::read_competency($params['id']);
1328 self::validate_context($competency->get_context());
1330 return api::move_up_competency($params['id']);
1334 * Returns description of move_up_competency() result value.
1336 * @return \external_description
1338 public static function move_up_competency_returns() {
1339 return new external_value(PARAM_BOOL, 'True if the update was successful');
1343 * Returns description of move_down_competency() parameters.
1345 * @return \external_function_parameters
1347 public static function move_down_competency_parameters() {
1348 $competencyid = new external_value(
1350 'The competency id',
1354 'id' => $competencyid,
1356 return new external_function_parameters($params);
1360 * Change the sort order of a competency.
1362 * @param int $competencyid Competency id.
1365 public static function move_down_competency($competencyid) {
1366 $params = self::validate_parameters(self::move_down_competency_parameters(),
1368 'id' => $competencyid,
1371 $competency = api::read_competency($params['id']);
1372 self::validate_context($competency->get_context());
1374 return api::move_down_competency($params['id']);
1378 * Returns description of move_down_competency() result value.
1380 * @return \external_description
1382 public static function move_down_competency_returns() {
1383 return new external_value(PARAM_BOOL, 'True if the update was successful');
1387 * Returns description of count_courses_using_competency() parameters.
1389 * @return \external_function_parameters
1391 public static function count_courses_using_competency_parameters() {
1392 $competencyid = new external_value(
1394 'The competency id',
1398 'id' => $competencyid,
1400 return new external_function_parameters($params);
1404 * Count the courses (visible to this user) that use this competency.
1406 * @param int $competencyid Competency id.
1409 public static function count_courses_using_competency($competencyid) {
1410 $params = self::validate_parameters(self::count_courses_using_competency_parameters(),
1412 'id' => $competencyid,
1415 $competency = api::read_competency($params['id']);
1416 self::validate_context($competency->get_context());
1418 return api::count_courses_using_competency($params['id']);
1422 * Returns description of count_courses_using_competency() result value.
1424 * @return \external_description
1426 public static function count_courses_using_competency_returns() {
1427 return new external_value(PARAM_INT, 'The number of courses using this competency');
1431 * Returns description of list_courses_using_competency() parameters.
1433 * @return \external_function_parameters
1435 public static function list_courses_using_competency_parameters() {
1436 $competencyid = new external_value(
1438 'The competency id',
1442 'id' => $competencyid,
1444 return new external_function_parameters($params);
1448 * Count the courses (visible to this user) that use this competency.
1450 * @param int $competencyid Competency id.
1453 public static function list_courses_using_competency($competencyid) {
1456 $params = self::validate_parameters(self::list_courses_using_competency_parameters(),
1458 'id' => $competencyid,
1461 $competency = api::read_competency($params['id']);
1462 self::validate_context($competency->get_context());
1463 $output = $PAGE->get_renderer('tool_lp');
1466 $courses = api::list_courses_using_competency($params['id']);
1467 foreach ($courses as $course) {
1468 $context = context_course::instance($course->id);
1469 $exporter = new course_summary_exporter($course, array('context' => $context));
1470 $result = $exporter->export($output);
1471 array_push($results, $result);
1477 * Returns description of list_courses_using_competency() result value.
1479 * @return \external_description
1481 public static function list_courses_using_competency_returns() {
1482 return new external_multiple_structure(course_summary_exporter::get_read_structure());
1486 * Returns description of count_competencies_in_course() parameters.
1488 * @return \external_function_parameters
1490 public static function count_competencies_in_course_parameters() {
1491 $courseid = new external_value(
1499 return new external_function_parameters($params);
1503 * Count the competencies (visible to this user) in this course.
1505 * @param int $courseid The course id to check.
1508 public static function count_competencies_in_course($courseid) {
1509 $params = self::validate_parameters(self::count_competencies_in_course_parameters(),
1514 self::validate_context(context_course::instance($params['id']));
1516 return api::count_competencies_in_course($params['id']);
1520 * Returns description of count_competencies_in_course() result value.
1522 * @return \external_description
1524 public static function count_competencies_in_course_returns() {
1525 return new external_value(PARAM_INT, 'The number of competencies in this course.');
1529 * Returns description of list_course_module_competencies() parameters.
1531 * @return \external_function_parameters
1533 public static function list_course_module_competencies_parameters() {
1534 $cmid = new external_value(
1536 'The course module id',
1542 return new external_function_parameters($params);
1546 * List the course modules using this competency (visible to this user) in this course.
1548 * @param int $cmid The course module id to check.
1551 public static function list_course_module_competencies($cmid) {
1554 $params = self::validate_parameters(self::list_course_module_competencies_parameters(),
1559 $context = context_module::instance($params['cmid']);
1560 self::validate_context($context);
1562 $output = $PAGE->get_renderer('tool_lp');
1564 $apiresult = api::list_course_module_competencies($params['cmid']);
1567 foreach ($apiresult as $cmrecord) {
1568 $one = new stdClass();
1569 $exporter = new competency_exporter($cmrecord['competency']);
1570 $one->competency = $exporter->export($output);
1571 $exporter = new course_module_competency_exporter($cmrecord['coursemodulecompetency']);
1572 $one->coursemodulecompetency = $exporter->export($output);
1574 $result[] = (array) $one;
1581 * Returns description of list_course_module_competencies() result value.
1583 * @return \external_description
1585 public static function list_course_module_competencies_returns() {
1586 return new external_multiple_structure(
1587 new external_single_structure(array(
1588 'competency' => competency_exporter::get_read_structure(),
1589 'coursemodulecompetency' => course_module_competency_exporter::get_read_structure()
1595 * Returns description of list_course_modules_using_competency() parameters.
1597 * @return \external_function_parameters
1599 public static function list_course_modules_using_competency_parameters() {
1600 $competencyid = new external_value(
1602 'The competency id',
1605 $courseid = new external_value(
1611 'competencyid' => $competencyid,
1612 'courseid' => $courseid,
1614 return new external_function_parameters($params);
1618 * List the course modules using this competency (visible to this user) in this course.
1620 * @param int $competencyid The competency id to check.
1621 * @param int $courseid The course id to check.
1624 public static function list_course_modules_using_competency($competencyid, $courseid) {
1627 $params = self::validate_parameters(self::list_course_modules_using_competency_parameters(),
1629 'competencyid' => $competencyid,
1630 'courseid' => $courseid,
1633 $coursecontext = context_course::instance($params['courseid']);
1634 self::validate_context($coursecontext);
1636 $output = $PAGE->get_renderer('tool_lp');
1638 $coursemodules = api::list_course_modules_using_competency($params['competencyid'], $params['courseid']);
1641 $fastmodinfo = get_fast_modinfo($cm->course);
1643 foreach ($coursemodules as $cmid) {
1644 $cminfo = $fastmodinfo->cms[$cmid];
1645 $exporter = new course_module_summary_exporter(null, array('cm' => $cminfo));
1646 $coursemodulesummary = $exporter->export($output);
1648 $result[] = $coursemodulesummary;
1655 * Returns description of list_course_modules_using_competency() result value.
1657 * @return \external_description
1659 public static function list_course_modules_using_competency_returns() {
1660 return new external_multiple_structure(course_module_summary_exporter::get_read_structure());
1664 * Returns description of list_course_competencies() parameters.
1666 * @return \external_function_parameters
1668 public static function list_course_competencies_parameters() {
1669 $courseid = new external_value(
1677 return new external_function_parameters($params);
1681 * List the competencies (visible to this user) in this course.
1683 * @param int $courseid The course id to check.
1686 public static function list_course_competencies($courseid) {
1689 $params = self::validate_parameters(self::list_course_competencies_parameters(),
1694 $coursecontext = context_course::instance($params['id']);
1695 self::validate_context($coursecontext);
1697 $output = $PAGE->get_renderer('tool_lp');
1699 $competencies = api::list_course_competencies($params['id']);
1702 $contextcache = array();
1703 foreach ($competencies as $competency) {
1704 if (!isset($contextcache[$competency['competency']->get_competencyframeworkid()])) {
1705 $contextcache[$competency['competency']->get_competencyframeworkid()] = $competency['competency']->get_context();
1707 $context = $contextcache[$competency['competency']->get_competencyframeworkid()];
1708 $exporter = new competency_exporter($competency['competency'], array('context' => $context));
1709 $competencyrecord = $exporter->export($output);
1710 $exporter = new course_competency_exporter($competency['coursecompetency'], array('context' => $context));
1711 $coursecompetencyrecord = $exporter->export($output);
1714 'competency' => $competencyrecord,
1715 'coursecompetency' => $coursecompetencyrecord
1723 * Returns description of list_course_competencies() result value.
1725 * @return \external_description
1727 public static function list_course_competencies_returns() {
1728 return new external_multiple_structure(
1729 new external_single_structure(array(
1730 'competency' => competency_exporter::get_read_structure(),
1731 'coursecompetency' => course_competency_exporter::get_read_structure()
1738 * Returns description of add_competency_to_course() parameters.
1740 * @return \external_function_parameters
1742 public static function add_competency_to_course_parameters() {
1743 $courseid = new external_value(
1748 $competencyid = new external_value(
1750 'The competency id',
1754 'courseid' => $courseid,
1755 'competencyid' => $competencyid,
1757 return new external_function_parameters($params);
1761 * Count the competencies (visible to this user) in this course.
1763 * @param int $courseid The course id to check.
1764 * @param int $competencyid Competency id.
1767 public static function add_competency_to_course($courseid, $competencyid) {
1768 $params = self::validate_parameters(self::add_competency_to_course_parameters(),
1770 'courseid' => $courseid,
1771 'competencyid' => $competencyid,
1774 self::validate_context(context_course::instance($params['courseid']));
1776 return api::add_competency_to_course($params['courseid'], $params['competencyid']);
1780 * Returns description of add_competency_to_course() result value.
1782 * @return \external_description
1784 public static function add_competency_to_course_returns() {
1785 return new external_value(PARAM_BOOL, 'True if successful.');
1789 * Returns description of remove_competency_from_course() parameters.
1791 * @return \external_function_parameters
1793 public static function remove_competency_from_course_parameters() {
1794 $courseid = new external_value(
1799 $competencyid = new external_value(
1801 'The competency id',
1805 'courseid' => $courseid,
1806 'competencyid' => $competencyid,
1808 return new external_function_parameters($params);
1812 * Count the competencies (visible to this user) in this course.
1814 * @param int $courseid The course id to check.
1815 * @param int $competencyid Competency id.
1818 public static function remove_competency_from_course($courseid, $competencyid) {
1819 $params = self::validate_parameters(self::remove_competency_from_course_parameters(),
1821 'courseid' => $courseid,
1822 'competencyid' => $competencyid,
1825 self::validate_context(context_course::instance($params['courseid']));
1827 return api::remove_competency_from_course($params['courseid'], $params['competencyid']);
1831 * Returns description of remove_competency_from_course() result value.
1833 * @return \external_description
1835 public static function remove_competency_from_course_returns() {
1836 return new external_value(PARAM_BOOL, 'True if successful.');
1840 * Returns description of data_for_course_competenies_page() parameters.
1842 * @return \external_function_parameters
1844 public static function data_for_course_competencies_page_parameters() {
1845 $courseid = new external_value(
1850 $params = array('courseid' => $courseid);
1851 return new external_function_parameters($params);
1855 * Loads the data required to render the course_competencies_page template.
1857 * @param int $courseid The course id to check.
1860 public static function data_for_course_competencies_page($courseid) {
1862 $params = self::validate_parameters(self::data_for_course_competencies_page_parameters(),
1864 'courseid' => $courseid,
1866 self::validate_context(context_course::instance($params['courseid']));
1868 $renderable = new output\course_competencies_page($params['courseid']);
1869 $renderer = $PAGE->get_renderer('tool_lp');
1871 $data = $renderable->export_for_template($renderer);
1877 * Returns description of data_for_course_competencies_page() result value.
1879 * @return \external_description
1881 public static function data_for_course_competencies_page_returns() {
1882 $uc = user_competency_exporter::get_read_structure();
1883 $uc->required = VALUE_OPTIONAL;
1885 return new external_single_structure(array (
1886 'courseid' => new external_value(PARAM_INT, 'The current course id'),
1887 'pagecontextid' => new external_value(PARAM_INT, 'The current page context ID.'),
1888 'gradableuserid' => new external_value(PARAM_INT, 'Current user id, if the user is a gradable user.', VALUE_OPTIONAL),
1889 'canmanagecompetencyframeworks' => new external_value(PARAM_BOOL, 'User can manage competency frameworks'),
1890 'canmanagecoursecompetencies' => new external_value(PARAM_BOOL, 'User can manage linked course competencies'),
1891 'competencies' => new external_multiple_structure(new external_single_structure(array(
1892 'competency' => competency_exporter::get_read_structure(),
1893 'coursecompetency' => course_competency_exporter::get_read_structure(),
1894 'coursemodules' => new external_multiple_structure(course_module_summary_exporter::get_read_structure()),
1895 'usercompetency' => $uc,
1896 'ruleoutcomeoptions' => new external_multiple_structure(
1897 new external_single_structure(array(
1898 'value' => new external_value(PARAM_INT, 'The option value'),
1899 'text' => new external_value(PARAM_NOTAGS, 'The name of the option'),
1900 'selected' => new external_value(PARAM_BOOL, 'If this is the currently selected option'),
1902 'comppath' => competency_path_exporter::get_read_structure(),
1904 'manageurl' => new external_value(PARAM_LOCALURL, 'Url to the manage competencies page.'),
1910 * Returns description of reorder_course_competency() parameters.
1912 * @return \external_function_parameters
1914 public static function reorder_course_competency_parameters() {
1915 $courseid = new external_value(
1920 $competencyidfrom = new external_value(
1922 'The competency id we are moving',
1925 $competencyidto = new external_value(
1927 'The competency id we are moving to',
1931 'courseid' => $courseid,
1932 'competencyidfrom' => $competencyidfrom,
1933 'competencyidto' => $competencyidto,
1935 return new external_function_parameters($params);
1939 * Change the order of course competencies.
1941 * @param int $courseid The course id
1942 * @param int $competencyidfrom The competency to move.
1943 * @param int $competencyidto The competency to move to.
1946 public static function reorder_course_competency($courseid, $competencyidfrom, $competencyidto) {
1947 $params = self::validate_parameters(self::reorder_course_competency_parameters(),
1949 'courseid' => $courseid,
1950 'competencyidfrom' => $competencyidfrom,
1951 'competencyidto' => $competencyidto,
1953 self::validate_context(context_course::instance($params['courseid']));
1955 return api::reorder_course_competency($params['courseid'], $params['competencyidfrom'], $params['competencyidto']);
1959 * Returns description of reorder_course_competency() result value.
1961 * @return \external_description
1963 public static function reorder_course_competency_returns() {
1964 return new external_value(PARAM_BOOL, 'True if successful.');
1968 * Returns description of reorder_template_competency() parameters.
1970 * @return \external_function_parameters
1972 public static function reorder_template_competency_parameters() {
1973 $templateid = new external_value(
1978 $competencyidfrom = new external_value(
1980 'The competency id we are moving',
1983 $competencyidto = new external_value(
1985 'The competency id we are moving to',
1989 'templateid' => $templateid,
1990 'competencyidfrom' => $competencyidfrom,
1991 'competencyidto' => $competencyidto,
1993 return new external_function_parameters($params);
1997 * Change the order of template competencies.
1999 * @param int $templateid The template id
2000 * @param int $competencyidfrom The competency to move.
2001 * @param int $competencyidto The competency to move to.
2004 public static function reorder_template_competency($templateid, $competencyidfrom, $competencyidto) {
2005 $params = self::validate_parameters(self::reorder_template_competency_parameters(),
2007 'templateid' => $templateid,
2008 'competencyidfrom' => $competencyidfrom,
2009 'competencyidto' => $competencyidto,
2012 $template = api::read_template($params['templateid']);
2013 self::validate_context($template->get_context());
2015 return api::reorder_template_competency($params['templateid'], $params['competencyidfrom'], $params['competencyidto']);
2019 * Returns description of reorder_template_competency() result value.
2021 * @return \external_description
2023 public static function reorder_template_competency_returns() {
2024 return new external_value(PARAM_BOOL, 'True if successful.');
2028 * Returns description of create_template() parameters.
2030 * @return \external_function_parameters
2032 public static function create_template_parameters() {
2033 $structure = template_exporter::get_create_structure();
2034 $params = array('template' => $structure);
2035 return new external_function_parameters($params);
2039 * Create a new learning plan template
2041 * @param array $template The list of fields for the template.
2042 * @return \stdClass Record of new template.
2044 public static function create_template($template) {
2047 $params = self::validate_parameters(self::create_template_parameters(),
2048 array('template' => $template));
2049 $params = $params['template'];
2050 $context = self::get_context_from_params($params);
2051 self::validate_context($context);
2052 $output = $PAGE->get_renderer('tool_lp');
2054 unset($params['contextlevel']);
2055 unset($params['instanceid']);
2056 $params = (object) $params;
2057 $params->contextid = $context->id;
2059 $result = api::create_template($params);
2060 $exporter = new template_exporter($result);
2061 $record = $exporter->export($output);
2066 * Returns description of create_template() result value.
2068 * @return \external_description
2070 public static function create_template_returns() {
2071 return template_exporter::get_read_structure();
2075 * Returns description of read_template() parameters.
2077 * @return \external_function_parameters
2079 public static function read_template_parameters() {
2080 $id = new external_value(
2082 'Data base record id for the template',
2089 return new external_function_parameters($params);
2093 * Read a learning plan template by id.
2095 * @param int $id The id of the template.
2098 public static function read_template($id) {
2101 $params = self::validate_parameters(self::read_template_parameters(),
2106 $template = api::read_template($params['id']);
2107 self::validate_context($template->get_context());
2108 $output = $PAGE->get_renderer('tool_lp');
2110 $exporter = new template_exporter($template);
2111 $record = $exporter->export($output);
2116 * Returns description of read_template() result value.
2118 * @return \external_description
2120 public static function read_template_returns() {
2121 return template_exporter::get_read_structure();
2125 * Returns description of delete_template() parameters.
2127 * @return \external_function_parameters
2129 public static function delete_template_parameters() {
2130 $id = new external_value(
2132 'Data base record id for the template',
2136 $deleteplans = new external_value(
2138 'Boolean to indicate if plans must be deleted',
2144 'deleteplans' => $deleteplans
2146 return new external_function_parameters($params);
2150 * Delete a learning plan template
2152 * @param int $id The learning plan template id
2153 * @param boolean $deleteplans True to delete the plans associated to template or false to unlink them
2156 public static function delete_template($id, $deleteplans = true) {
2157 $params = self::validate_parameters(self::delete_template_parameters(),
2160 'deleteplans' => $deleteplans,
2163 $template = api::read_template($params['id']);
2164 self::validate_context($template->get_context());
2166 return api::delete_template($params['id'], $params['deleteplans']);
2170 * Returns description of delete_template() result value.
2172 * @return \external_description
2174 public static function delete_template_returns() {
2175 return new external_value(PARAM_BOOL, 'True if the delete was successful');
2179 * Returns description of update_template() parameters.
2181 * @return \external_function_parameters
2183 public static function update_template_parameters() {
2184 $structure = template_exporter::get_update_structure();
2185 $params = array('template' => $structure);
2186 return new external_function_parameters($params);
2190 * Update an existing learning plan template
2192 * @param array $template The list of fields for the template.
2195 public static function update_template($template) {
2197 $params = self::validate_parameters(self::update_template_parameters(),
2198 array('template' => $template));
2199 $params = $params['template'];
2200 $template = api::read_template($params['id']);
2201 self::validate_context($template->get_context());
2203 $params = (object) $params;
2205 return api::update_template($params);
2209 * Returns description of update_template() result value.
2211 * @return \external_description
2213 public static function update_template_returns() {
2214 return new external_value(PARAM_BOOL, 'True if the update was successful');
2218 * Returns description of duplicate_template() parameters.
2220 * @return \external_function_parameters
2222 public static function duplicate_template_parameters() {
2223 $templateid = new external_value(
2232 return new external_function_parameters($params);
2236 * Duplicate a learning plan template.
2238 * @param int $id the id of the learning plan template to duplicate
2239 * @return boolean Record of new template.
2241 public static function duplicate_template($id) {
2244 $params = self::validate_parameters(self::duplicate_template_parameters(),
2249 $template = api::read_template($params['id']);
2250 self::validate_context($template->get_context());
2251 $output = $PAGE->get_renderer('tool_lp');
2253 $result = api::duplicate_template($params['id']);
2254 $exporter = new template_exporter($result);
2255 return $exporter->export($output);
2259 * Returns description of duplicate_template() result value.
2261 * @return \external_description
2263 public static function duplicate_template_returns() {
2264 return template_exporter::get_read_structure();
2268 * Returns description of list_templates() parameters.
2270 * @return \external_function_parameters
2272 public static function list_templates_parameters() {
2273 $sort = new external_value(
2275 'Column to sort by.',
2279 $order = new external_value(
2281 'Sort direction. Should be either ASC or DESC',
2285 $skip = new external_value(
2287 'Skip this number of records before returning results',
2291 $limit = new external_value(
2293 'Return this number of records at most.',
2297 $includes = new external_value(
2299 'What other contexts to fetch the templates from. (children, parents, self)',
2303 $onlyvisible = new external_value(
2305 'If should list only visible templates',
2315 'context' => self::get_context_parameters(),
2316 'includes' => $includes,
2317 'onlyvisible' => $onlyvisible
2319 return new external_function_parameters($params);
2323 * List the existing learning plan templates
2325 * @param string $sort Field to sort by.
2326 * @param string $order Sort order.
2327 * @param int $skip Limitstart.
2328 * @param int $limit Number of rows to return.
2329 * @param array $context
2330 * @param bool $includes
2331 * @param bool $onlyvisible
2335 public static function list_templates($sort, $order, $skip, $limit, $context, $includes, $onlyvisible) {
2338 $params = self::validate_parameters(self::list_templates_parameters(),
2344 'context' => $context,
2345 'includes' => $includes,
2346 'onlyvisible' => $onlyvisible
2349 $context = self::get_context_from_params($params['context']);
2350 self::validate_context($context);
2351 $output = $PAGE->get_renderer('tool_lp');
2353 if ($params['order'] !== '' && $params['order'] !== 'ASC' && $params['order'] !== 'DESC') {
2354 throw new invalid_parameter_exception('Invalid order param. Must be ASC, DESC or empty.');
2357 $results = api::list_templates($params['sort'],
2362 $params['includes'],
2363 $params['onlyvisible']);
2365 foreach ($results as $result) {
2366 $exporter = new template_exporter($result);
2367 $record = $exporter->export($output);
2368 array_push($records, $record);
2374 * Returns description of list_templates() result value.
2376 * @return \external_description
2378 public static function list_templates_returns() {
2379 return new external_multiple_structure(template_exporter::get_read_structure());
2383 * Returns description of count_templates() parameters.
2385 * @return \external_function_parameters
2387 public static function count_templates_parameters() {
2388 $includes = new external_value(
2390 'What other contextes to fetch the frameworks from. (children, parents, self)',
2396 'context' => self::get_context_parameters(),
2397 'includes' => $includes
2399 return new external_function_parameters($params);
2403 * Count the existing learning plan templates
2405 * @param array $context
2406 * @param string $includes
2409 public static function count_templates($context, $includes) {
2410 $params = self::validate_parameters(self::count_templates_parameters(),
2412 'context' => $context,
2413 'includes' => $includes
2415 $context = self::get_context_from_params($params['context']);
2416 self::validate_context($context);
2418 return api::count_templates($context, $includes);
2422 * Returns description of count_templates() result value.
2424 * @return \external_description
2426 public static function count_templates_returns() {
2427 return new external_value(PARAM_INT, 'The number of learning plan templates found.');
2431 * Returns description of data_for_templates_manage_page() parameters.
2433 * @return \external_function_parameters
2435 public static function data_for_templates_manage_page_parameters() {
2436 $params = array('pagecontext' => self::get_context_parameters());
2437 return new external_function_parameters($params);
2441 * Loads the data required to render the templates_manage_page template.
2443 * @param array $pagecontext The page context info.
2446 public static function data_for_templates_manage_page($pagecontext) {
2449 $params = self::validate_parameters(self::data_for_templates_manage_page_parameters(), array(
2450 'pagecontext' => $pagecontext
2452 $context = self::get_context_from_params($params['pagecontext']);
2453 self::validate_context($context);
2455 $renderable = new output\manage_templates_page($context);
2456 $renderer = $PAGE->get_renderer('tool_lp');
2458 $data = $renderable->export_for_template($renderer);
2464 * Returns description of data_for_templates_manage_page() result value.
2466 * @return \external_description
2468 public static function data_for_templates_manage_page_returns() {
2469 return new external_single_structure(array (
2470 'templates' => new external_multiple_structure(
2471 template_exporter::get_read_structure()
2473 'pluginbaseurl' => new external_value(PARAM_LOCALURL, 'Url to the tool_lp plugin folder on this Moodle site'),
2474 'navigation' => new external_multiple_structure(
2475 new external_value(PARAM_RAW, 'HTML for a navigation item that should be on this page')
2477 'pagecontextid' => new external_value(PARAM_INT, 'The page context id'),
2478 'canmanage' => new external_value(PARAM_BOOL, 'Whether the user manage the templates')
2484 * Returns description of count_templates_using_competency() parameters.
2486 * @return \external_function_parameters
2488 public static function count_templates_using_competency_parameters() {
2489 $competencyid = new external_value(
2491 'The competency id',
2495 'id' => $competencyid,
2497 return new external_function_parameters($params);
2501 * Count the learning plan templates (visible to this user) that use this competency.
2503 * @param int $competencyid Competency id.
2506 public static function count_templates_using_competency($competencyid) {
2507 $params = self::validate_parameters(self::count_templates_using_competency_parameters(),
2509 'id' => $competencyid,
2512 $competency = api::read_competency($params['id']);
2513 self::validate_context($competency->get_context());
2515 return api::count_templates_using_competency($params['id']);
2519 * Returns description of count_templates_using_competency() result value.
2521 * @return \external_description
2523 public static function count_templates_using_competency_returns() {
2524 return new external_value(PARAM_INT, 'The number of learning plan templates using this competency');
2528 * Returns description of list_templates_using_competency() parameters.
2530 * @return \external_function_parameters
2532 public static function list_templates_using_competency_parameters() {
2533 $competencyid = new external_value(
2535 'The competency id',
2539 'id' => $competencyid,
2541 return new external_function_parameters($params);
2545 * List the learning plan templates (visible to this user) that use this competency.
2547 * @param int $competencyid Competency id.
2550 public static function list_templates_using_competency($competencyid) {
2553 $params = self::validate_parameters(self::list_templates_using_competency_parameters(),
2555 'id' => $competencyid,
2558 $competency = api::read_competency($params['id']);
2559 self::validate_context($competency->get_context());
2560 $output = $PAGE->get_renderer('tool_lp');
2562 $templates = api::list_templates_using_competency($params['id']);
2565 foreach ($templates as $template) {
2566 $exporter = new template_exporter($template);
2567 $record = $exporter->export($output);
2568 $records[] = $record;
2575 * Returns description of list_templates_using_competency() result value.
2577 * @return \external_description
2579 public static function list_templates_using_competency_returns() {
2580 return new external_multiple_structure(template_exporter::get_read_structure());
2584 * Returns description of count_competencies_in_template() parameters.
2586 * @return \external_function_parameters
2588 public static function count_competencies_in_template_parameters() {
2589 $templateid = new external_value(
2595 'id' => $templateid,
2597 return new external_function_parameters($params);
2601 * Count the competencies (visible to this user) in this learning plan template.
2603 * @param int $templateid The template id to check
2606 public static function count_competencies_in_template($templateid) {
2608 $params = self::validate_parameters(self::count_competencies_in_template_parameters(),
2610 'id' => $templateid,
2612 $template = api::read_template($params['id']);
2613 self::validate_context($template->get_context());
2615 return api::count_competencies_in_template($params['id']);
2619 * Returns description of count_competencies_in_template() result value.
2621 * @return \external_description
2623 public static function count_competencies_in_template_returns() {
2624 return new external_value(PARAM_INT, 'The number of competencies in this learning plan template.');
2628 * Returns description of list_competencies_in_template() parameters.
2630 * @return \external_function_parameters
2632 public static function list_competencies_in_template_parameters() {
2633 $templateid = new external_value(
2639 'id' => $templateid,
2641 return new external_function_parameters($params);
2645 * List the competencies (visible to this user) in this learning plan template.
2647 * @param int $templateid Template id.
2650 public static function list_competencies_in_template($templateid) {
2653 $params = self::validate_parameters(self::list_competencies_in_template_parameters(),
2655 'id' => $templateid,
2658 $template = api::read_template($params['id']);
2659 self::validate_context($template->get_context());
2660 $output = $PAGE->get_renderer('tool_lp');
2662 $competencies = api::list_competencies_in_template($params['id']);
2664 $contextcache = array();
2666 foreach ($competencies as $competency) {
2667 if (!isset($contextcache[$competency->get_competencyframeworkid()])) {
2668 $contextcache[$competency->get_competencyframeworkid()] = $competency->get_context();
2670 $context = $contextcache[$competency->get_competencyframeworkid()];
2671 $exporter = new competency_exporter($competency, array('context' => $context));
2672 $record = $exporter->export($output);
2673 array_push($results, $record);
2679 * Returns description of list_competencies_in_template() result value.
2681 * @return \external_description
2683 public static function list_competencies_in_template_returns() {
2684 return new external_multiple_structure(competency_exporter::get_read_structure());
2688 * Returns description of add_competency_to_template() parameters.
2690 * @return \external_function_parameters
2692 public static function add_competency_to_template_parameters() {
2693 $templateid = new external_value(
2698 $competencyid = new external_value(
2700 'The competency id',
2704 'templateid' => $templateid,
2705 'competencyid' => $competencyid,
2707 return new external_function_parameters($params);
2711 * Count the competencies (visible to this user) in this template.
2713 * @param int $templateid Template id.
2714 * @param int $competencyid Competency id.
2717 public static function add_competency_to_template($templateid, $competencyid) {
2719 $params = self::validate_parameters(self::add_competency_to_template_parameters(),
2721 'templateid' => $templateid,
2722 'competencyid' => $competencyid,
2725 $template = api::read_template($params['templateid']);
2726 self::validate_context($template->get_context());
2728 return api::add_competency_to_template($params['templateid'], $params['competencyid']);
2732 * Returns description of add_competency_to_template() result value.
2734 * @return \external_description
2736 public static function add_competency_to_template_returns() {
2737 return new external_value(PARAM_BOOL, 'True if successful.');
2741 * Returns description of add_competency_to_plan() parameters.
2743 * @return \external_function_parameters
2745 public static function add_competency_to_plan_parameters() {
2746 $planid = new external_value(
2751 $competencyid = new external_value(
2753 'The competency id',
2757 'planid' => $planid,
2758 'competencyid' => $competencyid,
2760 return new external_function_parameters($params);
2764 * add competency to a learning plan.
2766 * @param int $planid Plan id.
2767 * @param int $competencyid Competency id.
2770 public static function add_competency_to_plan($planid, $competencyid) {
2771 $params = self::validate_parameters(self::add_competency_to_plan_parameters(),
2773 'planid' => $planid,
2774 'competencyid' => $competencyid,
2777 $plan = api::read_plan($params['planid']);
2778 self::validate_context($plan->get_context());
2780 return api::add_competency_to_plan($params['planid'], $params['competencyid']);
2784 * Returns description of add_competency_to_plan() result value.
2786 * @return \external_description
2788 public static function add_competency_to_plan_returns() {
2789 return new external_value(PARAM_BOOL, 'True if successful.');
2793 * Returns description of remove_competency_from_plan() parameters.
2795 * @return \external_function_parameters
2797 public static function remove_competency_from_plan_parameters() {
2798 $planid = new external_value(
2803 $competencyid = new external_value(
2805 'The competency id',
2809 'planid' => $planid,
2810 'competencyid' => $competencyid,
2812 return new external_function_parameters($params);
2816 * Remove a competency from plan.
2818 * @param int $planid Plan id.
2819 * @param int $competencyid Competency id.
2822 public static function remove_competency_from_plan($planid, $competencyid) {
2823 $params = self::validate_parameters(self::remove_competency_from_plan_parameters(),
2825 'planid' => $planid,
2826 'competencyid' => $competencyid,
2828 $plan = api::read_plan($params['planid']);
2829 self::validate_context($plan->get_context());
2831 return api::remove_competency_from_plan($params['planid'], $params['competencyid']);
2835 * Returns description of remove_competency_from_plan() result value.
2837 * @return \external_description
2839 public static function remove_competency_from_plan_returns() {
2840 return new external_value(PARAM_BOOL, 'True if successful.');
2844 * Returns description of remove_competency_from_template() parameters.
2846 * @return \external_function_parameters
2848 public static function remove_competency_from_template_parameters() {
2849 $templateid = new external_value(
2854 $competencyid = new external_value(
2856 'The competency id',
2860 'templateid' => $templateid,
2861 'competencyid' => $competencyid,
2863 return new external_function_parameters($params);
2867 * Returns description of reorder_plan_competency() parameters.
2869 * @return \external_function_parameters
2871 public static function reorder_plan_competency_parameters() {
2872 $planid = new external_value(
2877 $competencyidfrom = new external_value(
2879 'The competency id we are moving',
2882 $competencyidto = new external_value(
2884 'The competency id we are moving to',
2888 'planid' => $planid,
2889 'competencyidfrom' => $competencyidfrom,
2890 'competencyidto' => $competencyidto,
2892 return new external_function_parameters($params);
2896 * Change the order of plan competencies.
2898 * @param int $planid The plan id
2899 * @param int $competencyidfrom The competency to move.
2900 * @param int $competencyidto The competency to move to.
2903 public static function reorder_plan_competency($planid, $competencyidfrom, $competencyidto) {
2904 $params = self::validate_parameters(self::reorder_plan_competency_parameters(),
2906 'planid' => $planid,
2907 'competencyidfrom' => $competencyidfrom,
2908 'competencyidto' => $competencyidto,
2911 $plan = api::read_plan($params['planid']);
2912 self::validate_context($plan->get_context());
2914 return api::reorder_plan_competency($params['planid'], $params['competencyidfrom'], $params['competencyidto']);
2918 * Returns description of reorder_plan_competency() result value.
2920 * @return \external_description
2922 public static function reorder_plan_competency_returns() {
2923 return new external_value(PARAM_BOOL, 'True if successful.');
2927 * Returns description of external function parameters.
2929 * @return \external_function_parameters
2931 public static function user_competency_cancel_review_request_parameters() {
2932 return new external_function_parameters(array(
2933 'userid' => new external_value(PARAM_INT, 'The user ID'),
2934 'competencyid' => new external_value(PARAM_INT, 'The competency ID'),
2939 * External function user_competency_cancel_review_request.
2941 * @param int $userid The user ID.
2942 * @param int $competencyid The competency ID.
2945 public static function user_competency_cancel_review_request($userid, $competencyid) {
2946 $params = self::validate_parameters(self::user_competency_cancel_review_request_parameters(), array(
2947 'userid' => $userid,
2948 'competencyid' => $competencyid
2951 $context = context_user::instance($params['userid']);
2952 self::validate_context($context);
2954 return api::user_competency_cancel_review_request($userid, $competencyid);
2958 * Returns description of external function result value.
2960 * @return \external_function_parameters
2962 public static function user_competency_cancel_review_request_returns() {
2963 return new external_value(PARAM_BOOL, 'The success');
2967 * Returns description of external function parameters.
2969 * @return \external_function_parameters
2971 public static function user_competency_request_review_parameters() {
2972 return new external_function_parameters(array(
2973 'userid' => new external_value(PARAM_INT, 'The user ID'),
2974 'competencyid' => new external_value(PARAM_INT, 'The competency ID'),
2979 * External function user_competency_request_review.
2981 * @param int $userid The user ID.
2982 * @param int $competencyid The competency ID.
2985 public static function user_competency_request_review($userid, $competencyid) {
2986 $params = self::validate_parameters(self::user_competency_request_review_parameters(), array(
2987 'userid' => $userid,
2988 'competencyid' => $competencyid,
2991 $context = context_user::instance($params['userid']);
2992 self::validate_context($context);
2994 return api::user_competency_request_review($userid, $competencyid);
2998 * Returns description of external function result value.
3000 * @return \external_function_parameters
3002 public static function user_competency_request_review_returns() {
3003 return new external_value(PARAM_BOOL, 'The success');
3007 * Returns description of external function parameters.
3009 * @return \external_function_parameters
3011 public static function user_competency_start_review_parameters() {
3012 return new external_function_parameters(array(
3013 'userid' => new external_value(PARAM_INT, 'The user ID'),
3014 'competencyid' => new external_value(PARAM_INT, 'The competency ID'),
3019 * External function user_competency_start_review.
3021 * @param int $userid The user ID.
3022 * @param int $competencyid The competency ID.
3025 public static function user_competency_start_review($userid, $competencyid) {
3026 $params = self::validate_parameters(self::user_competency_start_review_parameters(), array(
3027 'userid' => $userid,
3028 'competencyid' => $competencyid
3031 $context = context_user::instance($params['userid']);
3032 self::validate_context($context);
3034 return api::user_competency_start_review($userid, $competencyid);
3038 * Returns description of external function result value.
3040 * @return \external_function_parameters
3042 public static function user_competency_start_review_returns() {
3043 return new external_value(PARAM_BOOL, 'The success');
3047 * Returns description of external function parameters.
3049 * @return \external_function_parameters
3051 public static function user_competency_stop_review_parameters() {
3052 return new external_function_parameters(array(
3053 'userid' => new external_value(PARAM_INT, 'The user ID'),
3054 'competencyid' => new external_value(PARAM_INT, 'The competency ID'),
3059 * External function user_competency_stop_review.
3061 * @param int $userid The user ID.
3062 * @param int $competencyid The competency ID.
3065 public static function user_competency_stop_review($userid, $competencyid) {
3066 $params = self::validate_parameters(self::user_competency_stop_review_parameters(), array(
3067 'userid' => $userid,
3068 'competencyid' => $competencyid
3071 $context = context_user::instance($params['userid']);
3072 self::validate_context($context);
3074 return api::user_competency_stop_review($userid, $competencyid);
3078 * Returns description of external function result value.
3080 * @return \external_function_parameters
3082 public static function user_competency_stop_review_returns() {
3083 return new external_value(PARAM_BOOL, 'The success');
3087 * Returns description of template_has_related_data() parameters.
3089 * @return \external_function_parameters
3091 public static function template_has_related_data_parameters() {
3092 $templateid = new external_value(
3098 'id' => $templateid,
3100 return new external_function_parameters($params);
3104 * Check if template has related data.
3106 * @param int $templateid Template id.
3109 public static function template_has_related_data($templateid) {
3110 $params = self::validate_parameters(self::template_has_related_data_parameters(),
3112 'id' => $templateid,
3115 $template = api::read_template($params['id']);
3116 self::validate_context($template->get_context());
3118 return api::template_has_related_data($params['id']);
3122 * Returns description of template_has_related_data() result value.
3124 * @return \external_description
3126 public static function template_has_related_data_returns() {
3127 return new external_value(PARAM_BOOL, 'True if the template has related data');
3131 * Count the competencies (visible to this user) in this learning plan template.
3133 * @param int $templateid Template id.
3134 * @param int $competencyid Competency id.
3137 public static function remove_competency_from_template($templateid, $competencyid) {
3138 $params = self::validate_parameters(self::remove_competency_from_template_parameters(),
3140 'templateid' => $templateid,
3141 'competencyid' => $competencyid,
3143 $template = api::read_template($params['templateid']);
3144 self::validate_context($template->get_context());
3146 return api::remove_competency_from_template($params['templateid'], $params['competencyid']);
3150 * Returns description of remove_competency_from_template() result value.
3152 * @return \external_description
3154 public static function remove_competency_from_template_returns() {
3155 return new external_value(PARAM_BOOL, 'True if successful.');
3159 * Returns description of data_for_template_competenies_page() parameters.
3161 * @return \external_function_parameters
3163 public static function data_for_template_competencies_page_parameters() {
3164 $templateid = new external_value(
3169 $params = array('templateid' => $templateid, 'pagecontext' => self::get_context_parameters());
3170 return new external_function_parameters($params);
3174 * Loads the data required to render the template_competencies_page template.
3176 * @param int $templateid Template id.
3177 * @param array $pagecontext The page context info.
3180 public static function data_for_template_competencies_page($templateid, $pagecontext) {
3182 $params = self::validate_parameters(self::data_for_template_competencies_page_parameters(),
3184 'templateid' => $templateid,
3185 'pagecontext' => $pagecontext
3188 $context = self::get_context_from_params($params['pagecontext']);
3189 self::validate_context($context);
3191 $template = api::read_template($params['templateid']);
3192 $renderable = new output\template_competencies_page($template, $context);
3193 $renderer = $PAGE->get_renderer('tool_lp');
3195 $data = $renderable->export_for_template($renderer);
3201 * Returns description of data_for_template_competencies_page() result value.
3203 * @return \external_description
3205 public static function data_for_template_competencies_page_returns() {
3206 return new external_single_structure(array (
3207 'template' => template_exporter::get_read_structure(),
3208 'pagecontextid' => new external_value(PARAM_INT, 'Context ID'),
3209 'canmanagecompetencyframeworks' => new external_value(PARAM_BOOL, 'User can manage competency frameworks'),
3210 'canmanagetemplatecompetencies' => new external_value(PARAM_BOOL, 'User can manage learning plan templates'),
3211 'competencies' => new external_multiple_structure(
3212 competency_summary_exporter::get_read_structure()
3214 'manageurl' => new external_value(PARAM_LOCALURL, 'Url to the manage competencies page.'),
3215 'pluginbaseurl' => new external_value(PARAM_LOCALURL, 'Base URL of the plugin.'),
3216 'statistics' => template_statistics_exporter::get_read_structure()
3222 * Returns description of data_for_plan_competenies_page() parameters.
3224 * @return \external_function_parameters
3226 public static function data_for_plan_page_parameters() {
3227 $planid = new external_value(
3232 $params = array('planid' => $planid);
3233 return new external_function_parameters($params);
3237 * Loads the data required to render the plan_page template.
3239 * @param int $planid Learning Plan id.
3242 public static function data_for_plan_page($planid) {
3244 $params = self::validate_parameters(self::data_for_plan_page_parameters(),
3248 $plan = api::read_plan($params['planid']);
3249 self::validate_context($plan->get_context());
3251 $renderable = new output\plan_page($plan);
3252 $renderer = $PAGE->get_renderer('tool_lp');
3254 $data = $renderable->export_for_template($renderer);
3260 * Returns description of data_for_plan_page() result value.
3262 * @return \external_description
3264 public static function data_for_plan_page_returns() {
3265 $uc = user_competency_exporter::get_read_structure();
3266 $ucp = user_competency_plan_exporter::get_read_structure();
3268 $uc->required = VALUE_OPTIONAL;
3269 $ucp->required = VALUE_OPTIONAL;
3271 return new external_single_structure(array (
3272 'plan' => plan_exporter::get_read_structure(),
3273 'contextid' => new external_value(PARAM_INT, 'Context ID.'),
3274 'pluginbaseurl' => new external_value(PARAM_URL, 'Plugin base URL.'),
3275 'competencies' => new external_multiple_structure(
3276 new external_single_structure(array(
3277 'competency' => competency_exporter::get_read_structure(),
3278 'comppath' => competency_path_exporter::get_read_structure(),
3279 'usercompetency' => $uc,
3280 'usercompetencyplan' => $ucp
3287 * Returns description of create_plan() parameters.
3289 * @return \external_function_parameters
3291 public static function create_plan_parameters() {
3292 $structure = plan_exporter::get_create_structure();
3293 $params = array('plan' => $structure);
3294 return new external_function_parameters($params);
3298 * Create a new learning plan.
3300 * @param array $plan List of fields for the plan.
3301 * @return array New plan record.
3303 public static function create_plan($plan) {
3306 $params = self::validate_parameters(self::create_plan_parameters(),
3307 array('plan' => $plan));
3308 $params = $params['plan'];
3310 $context = context_user::instance($params['userid']);
3311 self::validate_context($context);
3312 $output = $PAGE->get_renderer('tool_lp');
3314 $params = (object) $params;
3316 $result = api::create_plan($params);
3317 $exporter = new plan_exporter($result, array('template' => null));
3318 return $exporter->export($output);
3322 * Returns description of create_plan() result value.
3324 * @return \external_description
3326 public static function create_plan_returns() {
3327 return plan_exporter::get_read_structure();
3331 * Returns description of update_plan() parameters.
3333 * @return \external_function_parameters
3335 public static function update_plan_parameters() {
3336 $structure = plan_exporter::get_update_structure();
3337 $params = array('plan' => $structure);
3338 return new external_function_parameters($params);
3342 * Updates a new learning plan.
3344 * @param array $plan Fields for the plan (id is required)
3347 public static function update_plan($plan) {
3350 $params = self::validate_parameters(self::update_plan_parameters(),
3351 array('plan' => $plan));
3353 $params = $params['plan'];
3355 $plan = api::read_plan($params['id']);
3356 self::validate_context($plan->get_context());
3357 $output = $PAGE->get_renderer('tool_lp');
3359 $params = (object) $params;
3360 $result = api::update_plan($params);
3361 $exporter = plan_exporter($result);
3362 $record = $exporter->export($output);
3363 return external_api::clean_returnvalue(self::update_plan_returns(), $record);
3367 * Returns description of update_plan() result value.
3369 * @return \external_description
3371 public static function update_plan_returns() {
3372 return plan_exporter::get_read_structure();
3376 * Returns description of complete_plan() parameters.
3378 * @return \external_function_parameters
3380 public static function complete_plan_parameters() {
3381 $planid = new external_value(