diff --git a/admin/tool/lp/amd/build/competencyactions.min.js b/admin/tool/lp/amd/build/competencyactions.min.js index 52bdb6b6bd9..c4df1882dc4 100644 --- a/admin/tool/lp/amd/build/competencyactions.min.js +++ b/admin/tool/lp/amd/build/competencyactions.min.js @@ -1 +1 @@ -define(["jquery","core/url","core/templates","core/notification","core/str","core/ajax","tool_lp/dragdrop-reorder","tool_lp/tree","tool_lp/dialogue","tool_lp/menubar","tool_lp/competencypicker","tool_lp/competency_outcomes","tool_lp/competencyruleconfig"],function(a,b,c,d,e,f,g,h,i,j,k,l,m){var n,o,p,q,r,s,t=null,u=null,v=null,w=function(){var c=a('[data-region="competencyactions"]').data("competency"),d={competencyframeworkid:t.getCompetencyFrameworkId(),pagecontextid:n};null!==c&&(d.parentid=c.id);var e=a.param(d);window.location=b.relativeUrl("/admin/tool/lp/editcompetency.php?"+e)},x=function(){if("undefined"==typeof v&&(v=0),v!=u){var b=a('[data-region="filtercompetencies"]').data("frameworkid"),c=f.call([{methodname:"tool_lp_set_parent_competency",args:{competencyid:u,parentid:v}},{methodname:"tool_lp_data_for_competencies_manage_page",args:{competencyframeworkid:b,search:a('[data-region="filtercompetencies"] input').val()}}]);c[1].done(C).fail(d.exception)}},y=function(b){var c=a(b.getContent()),d=c.find("[data-enhance=movetree]"),e=new h(d,!1);e.on("selectionchanged",function(b,c){var d=c.selected;v=a(d).data("id")}),d.show(),c.on("click",'[data-action="move"]',function(){b.close(),x()}),c.on("click",'[data-action="cancel"]',function(){b.close()})},z=function(a,b){var c;for(c=0;cspan",M).on("dragover","li>span",N).on("dragenter","li>span",O).on("dragleave","li>span",P).on("drop","li>span",Q),b.on("selectionchanged",Y),p=new m(t,s),p.on("save",J.bind(this))}}}); \ No newline at end of file +define(["jquery","core/url","core/templates","core/notification","core/str","core/ajax","tool_lp/dragdrop-reorder","tool_lp/tree","tool_lp/dialogue","tool_lp/menubar","tool_lp/competencypicker","tool_lp/competency_outcomes","tool_lp/competencyruleconfig"],function(a,b,c,d,e,f,g,h,i,j,k,l,m){var n,o,p,q,r,s,t=null,u=null,v=null,w=function(){var c=a('[data-region="competencyactions"]').data("competency"),d={competencyframeworkid:t.getCompetencyFrameworkId(),pagecontextid:n};null!==c&&(d.parentid=c.id);var e=a.param(d);window.location=b.relativeUrl("/admin/tool/lp/editcompetency.php?"+e)},x=function(){if("undefined"==typeof v&&(v=0),v!=u){var b=a('[data-region="filtercompetencies"]').data("frameworkid"),c=f.call([{methodname:"tool_lp_set_parent_competency",args:{competencyid:u,parentid:v}},{methodname:"tool_lp_data_for_competencies_manage_page",args:{competencyframeworkid:b,search:a('[data-region="filtercompetencies"] input').val()}}]);c[1].done(C).fail(d.exception)}},y=function(b){var c=a(b.getContent()),d=c.find("[data-enhance=movetree]"),e=new h(d,!1);e.on("selectionchanged",function(b,c){var d=c.selected;v=a(d).data("id")}),d.show(),c.on("click",'[data-action="move"]',function(){b.close(),x()}),c.on("click",'[data-action="cancel"]',function(){b.close()})},z=function(a,b){var c;for(c=0;cspan",M).on("dragover","li>span",N).on("dragenter","li>span",O).on("dragleave","li>span",P).on("drop","li>span",Q),b.on("selectionchanged",Y),p=new m(t,s),p.on("save",J.bind(this))}}}); \ No newline at end of file diff --git a/admin/tool/lp/amd/build/competencytree.min.js b/admin/tool/lp/amd/build/competencytree.min.js index ee13d81d64a..6d045efb919 100644 --- a/admin/tool/lp/amd/build/competencytree.min.js +++ b/admin/tool/lp/amd/build/competencytree.min.js @@ -1 +1 @@ -define(["core/ajax","core/notification","core/templates","tool_lp/tree","jquery"],function(a,b,c,d,e){var f={},g=0,h="",i="",j="",k=function(a,b){var c=0,d=!1;for(a.haschildren=!1,a.children=[],c=0;c0},reloadCompetencies:function(){return l("").fail(b.exception)},listCompetencies:function(){return f}}}); \ No newline at end of file +define(["core/ajax","core/notification","core/templates","tool_lp/tree","jquery"],function(a,b,c,d,e){var f={},g=0,h="",i="",j="",k=function(a,b){var c=0,d=!1;for(a.haschildren=!1,a.children=[],c=0;c0},reloadCompetencies:function(){return l("").fail(b.exception)},listCompetencies:function(){return f}}}); \ No newline at end of file diff --git a/admin/tool/lp/amd/src/competencyactions.js b/admin/tool/lp/amd/src/competencyactions.js index 8d73c4b2541..754e42b4405 100644 --- a/admin/tool/lp/amd/src/competencyactions.js +++ b/admin/tool/lp/amd/src/competencyactions.js @@ -156,8 +156,7 @@ define(['jquery', methodname: 'tool_lp_search_competencies', args: { competencyframeworkid: competency.competencyframeworkid, - searchtext: '', - includerelated: false + searchtext: '' } },{ methodname: 'tool_lp_read_competency_framework', @@ -372,19 +371,20 @@ define(['jquery', }; var ruleConfigSaveHandler = function(e, config) { + var update = { + id: relatedTarget.id, + shortname: relatedTarget.shortname, + idnumber: relatedTarget.idnumber, + description: relatedTarget.description, + descriptionformat: relatedTarget.descriptionformat, + visible: relatedTarget.visible, + ruletype: config.ruletype, + ruleoutcome: config.ruleoutcome, + ruleconfig: config.ruleconfig + }; var promise = ajax.call([{ methodname: 'tool_lp_update_competency', - args: { - id: relatedTarget.id, - shortname: relatedTarget.shortname, - idnumber: relatedTarget.idnumber, - description: relatedTarget.description, - descriptionformat: relatedTarget.descriptionformat, - visible: relatedTarget.visible, - ruletype: config.ruletype, - ruleoutcome: config.ruleoutcome, - ruleconfig: config.ruleconfig, - } + args: { competency: update } }]); promise[0].then(function(result) { if (result) { @@ -531,8 +531,6 @@ define(['jquery', // Listeners to newly loaded related competencies. $('[data-action="deleterelation"]').on('click', deleteRelatedHandler); - // We update the full list of competencies. - treeModel.reloadCompetencies(); }; /** @@ -600,6 +598,22 @@ define(['jquery', $('[data-region="competencyinfo"]').html(html); $('[data-action="deleterelation"]').on('click', deleteRelatedHandler); }); + }).then(function() { + return templates.render('tool_lp/loading', {}); + }).then(function(html, js) { + templates.replaceNodeContents('[data-region="relatedcompetencies"]', html, js); + }).done(function() { + ajax.call([{ + methodname: 'tool_lp_data_for_related_competencies_section', + args: { competencyid: competency.id }, + done: function(context) { + return templates.render('tool_lp/related_competencies', context).done(function(html, js) { + $('[data-region="relatedcompetencies"]').replaceWith(html); + templates.runTemplateJS(js); + updatedRelatedCompetencies(); + }); + } + }]); }).fail(notification.exception); }; diff --git a/admin/tool/lp/amd/src/competencytree.js b/admin/tool/lp/amd/src/competencytree.js index ee3903e4dc6..a90abdb85bf 100644 --- a/admin/tool/lp/amd/src/competencytree.js +++ b/admin/tool/lp/amd/src/competencytree.js @@ -75,8 +75,7 @@ define(['core/ajax', 'core/notification', 'core/templates', 'tool_lp/tree', 'jqu methodname: 'tool_lp_search_competencies', args: { searchtext: searchtext, - competencyframeworkid: competencyFrameworkId, - includerelated: true + competencyframeworkid: competencyFrameworkId } }]); promises[0].done(function(result) { diff --git a/admin/tool/lp/classes/external.php b/admin/tool/lp/classes/external.php index 8657355d29d..24408898ca5 100644 --- a/admin/tool/lp/classes/external.php +++ b/admin/tool/lp/classes/external.php @@ -149,169 +149,37 @@ class external extends external_api { return new external_function_parameters($params); } - /** - * Returns the external structure of a full competency_framework record. - * - * @return \external_single_structure - */ - protected static function get_competency_framework_external_structure() { - $id = new external_value( - PARAM_INT, - 'Database record id' - ); - $shortname = new external_value( - PARAM_TEXT, - 'Short name for the competency framework' - ); - $idnumber = new external_value( - PARAM_TEXT, - 'If provided, must be a unique string to identify this competency framework' - ); - $description = new external_value( - PARAM_RAW, - 'Description for the framework' - ); - $descriptionformat = new external_format_value( - 'Description format for the framework' - ); - $scaleid = new external_value( - PARAM_INT, - 'Scale id associated to the framework.' - ); - $scaleconfiguration = new external_value( - PARAM_TEXT, - 'Scale configuration.' - ); - $taxonomies = new external_value( - PARAM_RAW, - 'The taxonomy terms' - ); - $visible = new external_value( - PARAM_BOOL, - 'Is this framework visible?' - ); - $timecreated = new external_value( - PARAM_INT, - 'Timestamp this record was created' - ); - $timemodified = new external_value( - PARAM_INT, - 'Timestamp this record was modified' - ); - $usermodified = new external_value( - PARAM_INT, - 'User who modified this record last' - ); - - $returns = array( - 'id' => $id, - 'shortname' => $shortname, - 'idnumber' => $idnumber, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'scaleid' => $scaleid, - 'scaleconfiguration' => $scaleconfiguration, - 'taxonomies' => $taxonomies, - 'visible' => $visible, - 'timecreated' => $timecreated, - 'timemodified' => $timemodified, - 'usermodified' => $usermodified, - ); - return new external_single_structure($returns); - } - /** * Returns description of create_competency_framework() parameters. * * @return \external_function_parameters */ public static function create_competency_framework_parameters() { - $shortname = new external_value( - PARAM_TEXT, - 'Short name for the competency framework.', - VALUE_REQUIRED - ); - $idnumber = new external_value( - PARAM_TEXT, - 'If provided, must be a unique string to identify this competency framework.', - VALUE_DEFAULT, - '' - ); - $description = new external_value( - PARAM_RAW, - 'Optional description for the framework', - VALUE_DEFAULT, - '' - ); - $descriptionformat = new external_format_value( - 'Optional description format for the framework', - VALUE_DEFAULT, - FORMAT_HTML - ); - $scaleid = new external_value( - PARAM_INT, - 'Scale id associated to the framework.', - VALUE_REQUIRED - ); - $scaleconfiguration = new external_value( - PARAM_TEXT, - 'Scale configuration.', - VALUE_REQUIRED - ); - $visible = new external_value( - PARAM_BOOL, - 'Is this framework visible?', - VALUE_DEFAULT, - true - ); - - $params = array( - 'shortname' => $shortname, - 'idnumber' => $idnumber, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'scaleid' => $scaleid, - 'scaleconfiguration' => $scaleconfiguration, - 'visible' => $visible, - 'context' => self::get_context_parameters(), - ); + $structure = competency_framework_exporter::export_structure('create'); + $params = array('competencyframework' => $structure); return new external_function_parameters($params); } /** * Create a new competency framework * - * @param string $shortname The short name - * @param string $idnumber The idnumber - * @param string $description The description - * @param int $descriptionformat The description format - * @param int $scaleid The scale id associated to the framework - * @param string $scaleconfiguration The scale configuration - * @param bool $visible Is this framework visible. - * @param array $context + * @param array $competencyframework A single param with all the fields for a competency framework. * @return \stdClass The new record */ - public static function create_competency_framework($shortname, $idnumber, $description, $descriptionformat, $scaleid, - $scaleconfiguration, $visible, $context) { + public static function create_competency_framework($competencyframework) { global $PAGE; $params = self::validate_parameters(self::create_competency_framework_parameters(), - array( - 'shortname' => $shortname, - 'idnumber' => $idnumber, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'scaleid' => $scaleid, - 'scaleconfiguration' => $scaleconfiguration, - 'visible' => $visible, - 'context' => $context, - )); + array('competencyframework' => $competencyframework)); - $context = self::get_context_from_params($params['context']); + $params = $params['competencyframework']; + + $context = self::get_context_from_params($params); self::validate_context($context); $output = $PAGE->get_renderer('tool_lp'); - unset($params['context']); + unset($params['contextlevel']); + unset($params['instanceid']); $params['contextid'] = $context->id; $params = (object) $params; @@ -327,7 +195,7 @@ class external extends external_api { * @return \external_description */ public static function create_competency_framework_returns() { - return self::get_competency_framework_external_structure(); + return competency_framework_exporter::export_structure('read'); } /** @@ -376,7 +244,7 @@ class external extends external_api { * @return \external_description */ public static function read_competency_framework_returns() { - return self::get_competency_framework_external_structure(); + return competency_framework_exporter::export_structure('read'); } /** @@ -421,7 +289,7 @@ class external extends external_api { * @return \external_description */ public static function duplicate_competency_framework_returns() { - return self::get_competency_framework_external_structure(); + return competency_framework_exporter::export_structure('read'); } /** @@ -475,93 +343,26 @@ class external extends external_api { * @return \external_function_parameters */ public static function update_competency_framework_parameters() { - $id = new external_value( - PARAM_INT, - 'Data base record id for the framework', - VALUE_REQUIRED - ); - $shortname = new external_value( - PARAM_TEXT, - 'Short name for the competency framework.', - VALUE_REQUIRED - ); - $idnumber = new external_value( - PARAM_TEXT, - 'If provided, must be a unique string to identify this competency framework.', - VALUE_REQUIRED - ); - $description = new external_value( - PARAM_RAW, - 'Description for the framework', - VALUE_REQUIRED - ); - $descriptionformat = new external_format_value( - 'Description format for the framework', - VALUE_REQUIRED - ); - $scaleid = new external_value( - PARAM_INT, - 'Scale id associated to the framework.', - VALUE_REQUIRED - ); - $scaleconfiguration = new external_value( - PARAM_TEXT, - 'Scale configuration.', - VALUE_REQUIRED - ); - $visible = new external_value( - PARAM_BOOL, - 'Is this framework visible?', - VALUE_REQUIRED - ); - - $params = array( - 'id' => $id, - 'shortname' => $shortname, - 'idnumber' => $idnumber, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'scaleid' => $scaleid, - 'scaleconfiguration' => $scaleconfiguration, - 'visible' => $visible, - ); + $structure = competency_framework_exporter::export_structure('update'); + $params = array('competencyframework' => $structure); return new external_function_parameters($params); } /** * Update an existing competency framework * - * @param int $id The competency framework id - * @param string $shortname - * @param string $idnumber - * @param string $description - * @param int $descriptionformat - * @param int $scaleid The scale id associated to the framework - * @param string $scaleconfiguration The scale configuration - * @param boolean $visible + * @param array $competencyframework An array with all the fields for a competency framework. * @return boolean */ - public static function update_competency_framework($id, - $shortname, - $idnumber, - $description, - $descriptionformat, - $scaleid, - $scaleconfiguration, - $visible) { + public static function update_competency_framework($competencyframework) { $params = self::validate_parameters(self::update_competency_framework_parameters(), array( - 'id' => $id, - 'shortname' => $shortname, - 'idnumber' => $idnumber, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'scaleid' => $scaleid, - 'scaleconfiguration' => $scaleconfiguration, - 'visible' => $visible + 'competencyframework' => $competencyframework )); + $params = $params['competencyframework']; + $framework = api::read_framework($params['id']); self::validate_context($framework->get_context()); @@ -684,7 +485,7 @@ class external extends external_api { * @return \external_description */ public static function list_competency_frameworks_returns() { - return new external_multiple_structure(self::get_competency_framework_external_structure()); + return new external_multiple_structure(competency_framework_exporter::export_structure('read')); } /** @@ -779,7 +580,7 @@ class external extends external_api { return new external_single_structure(array ( 'canmanage' => new external_value(PARAM_BOOL, 'True if this user has permission to manage competency frameworks'), 'competencyframeworks' => new external_multiple_structure( - self::get_competency_framework_external_structure() + competency_framework_exporter::export_structure('read') ), 'pluginbaseurl' => new external_value(PARAM_LOCALURL, 'Url to the tool_lp plugin folder on this Moodle site'), 'navigation' => new external_multiple_structure( @@ -789,340 +590,30 @@ class external extends external_api { } - /** - * Returns the external structure of a full competency record. - * - * @param bool $includerelated Useful to avoid recursive structures. - * @return \external_single_structure - */ - protected static function get_competency_external_structure($includerelated = false) { - $id = new external_value( - PARAM_INT, - 'Database record id' - ); - $shortname = new external_value( - PARAM_TEXT, - 'Short name for the competency' - ); - $idnumber = new external_value( - PARAM_TEXT, - 'If provided, must be a unique string to identify this competency' - ); - $description = new external_value( - PARAM_RAW, - 'Description for the competency' - ); - $descriptionformat = new external_format_value( - 'Description format for the competency' - ); - $visible = new external_value( - PARAM_BOOL, - 'Is this competency visible?' - ); - $sortorder = new external_value( - PARAM_INT, - 'Relative sort order of this competency' - ); - $competencyframeworkid = new external_value( - PARAM_INT, - 'Competency framework id that this competency belongs to' - ); - $parentid = new external_value( - PARAM_INT, - 'Parent competency id. 0 means top level node.' - ); - $timecreated = new external_value( - PARAM_INT, - 'Timestamp this record was created' - ); - $timemodified = new external_value( - PARAM_INT, - 'Timestamp this record was modified' - ); - $usermodified = new external_value( - PARAM_INT, - 'User who modified this record last' - ); - $parentid = new external_value( - PARAM_INT, - 'The id of the parent competency.' - ); - $path = new external_value( - PARAM_RAW, - 'The path of parents all the way to the root of the tree.' - ); - - $returns = array( - 'id' => $id, - 'shortname' => $shortname, - 'idnumber' => $idnumber, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'visible' => $visible, - 'sortorder' => $sortorder, - 'timecreated' => $timecreated, - 'timemodified' => $timemodified, - 'usermodified' => $usermodified, - 'parentid' => $parentid, - 'competencyframeworkid' => $competencyframeworkid, - 'path' => $path, - ); - - if ($includerelated) { - $returns['relatedcompetencies'] = new external_multiple_structure( - self::get_competency_external_structure(false), - 'Related competencies', - VALUE_OPTIONAL - ); - } - - return new external_single_structure($returns); - } - - /** - * Returns the external structure of a full user_competency record. - * - * @param int $fordisplay When true, additional fields for display purposes will be added. - * @param int $required If the stucture is required. - * @return \external_single_structure - */ - protected static function get_user_competency_external_structure($fordisplay = false, $required = VALUE_REQUIRED) { - $id = new external_value( - PARAM_INT, - 'Database record id' - ); - $userid = new external_value( - PARAM_INT, - 'User to whom this record belongs to' - ); - $competencyid = new external_value( - PARAM_INT, - 'The competency associated with this record' - ); - $status = new external_value( - PARAM_INT, - 'The status of the user competency' - ); - $reviewerid = new external_value( - PARAM_INT, - 'The reviewer ID' - ); - $proficiency = new external_value( - PARAM_BOOL, - 'Whether or not the user is proficient' - ); - $grade = new external_value( - PARAM_INT, - 'The scale grade' - ); - $timecreated = new external_value( - PARAM_INT, - 'Timestamp this record was created' - ); - $timemodified = new external_value( - PARAM_INT, - 'Timestamp this record was modified' - ); - $usermodified = new external_value( - PARAM_INT, - 'User who modified this record last' - ); - - $returns = array( - 'id' => $id, - 'userid' => $userid, - 'competencyid' => $competencyid, - 'status' => $status, - 'reviewerid' => $reviewerid, - 'proficiency' => $proficiency, - 'grade' => $grade, - 'timecreated' => $timecreated, - 'timemodified' => $timemodified, - 'usermodified' => $usermodified, - ); - - if ($fordisplay) { - $gradename = new external_value( - PARAM_TEXT, - 'User competency status name' - ); - $proficiencyname = new external_value( - PARAM_TEXT, - 'User competency proficiency name' - ); - $statusname = new external_value( - PARAM_TEXT, - 'User competency status name' - ); - $returns['gradename'] = $gradename; - $returns['proficiencyname'] = $proficiencyname; - $returns['statusname'] = $statusname; - } - - return new external_single_structure($returns, '', $required); - } - - /** - * Returns the external structure of a full user_competency_plan record. - * - * @param int $fordisplay When true, additional fields for display purposes will be added. - * @param int $required If the stucture is required. - * @return \external_single_structure - */ - protected static function get_user_competency_plan_external_structure($fordisplay = false, $required = VALUE_REQUIRED) { - $id = new external_value( - PARAM_INT, - 'Database record id' - ); - $userid = new external_value( - PARAM_INT, - 'User to whom this record belongs to' - ); - $competencyid = new external_value( - PARAM_INT, - 'The competency associated with this record' - ); - $proficiency = new external_value( - PARAM_BOOL, - 'Whether or not the user is proficient' - ); - $grade = new external_value( - PARAM_INT, - 'The scale grade' - ); - $planid = new external_value( - PARAM_INT, - 'The plan id' - ); - $timecreated = new external_value( - PARAM_INT, - 'Timestamp this record was created' - ); - $timemodified = new external_value( - PARAM_INT, - 'Timestamp this record was modified' - ); - $usermodified = new external_value( - PARAM_INT, - 'User who modified this record last' - ); - - $returns = array( - 'id' => $id, - 'userid' => $userid, - 'competencyid' => $competencyid, - 'proficiency' => $proficiency, - 'grade' => $grade, - 'planid' => $planid, - 'timecreated' => $timecreated, - 'timemodified' => $timemodified, - 'usermodified' => $usermodified, - ); - - if ($fordisplay) { - $gradename = new external_value( - PARAM_TEXT, - 'User competency status name' - ); - $proficiencyname = new external_value( - PARAM_TEXT, - 'User competency proficiency name' - ); - $returns['gradename'] = $gradename; - $returns['proficiencyname'] = $proficiencyname; - } - - return new external_single_structure($returns, '', $required); - } - /** * Returns description of create_competency() parameters. * * @return \external_function_parameters */ public static function create_competency_parameters() { - $shortname = new external_value( - PARAM_TEXT, - 'Short name for the competency framework.', - VALUE_REQUIRED - ); - $idnumber = new external_value( - PARAM_TEXT, - 'If provided, must be a unique string to identify this competency framework.', - VALUE_DEFAULT, - '' - ); - $description = new external_value( - PARAM_RAW, - 'Optional description for the framework', - VALUE_DEFAULT, - '' - ); - $descriptionformat = new external_format_value( - 'Optional description format for the framework', - VALUE_DEFAULT, - FORMAT_HTML - ); - $visible = new external_value( - PARAM_BOOL, - 'Is this competency visible?', - VALUE_DEFAULT, - true - ); - $competencyframeworkid = new external_value( - PARAM_INT, - 'Which competency framework does this competency belong to?' - ); - $parentid = new external_value( - PARAM_INT, - 'The parent competency. 0 means this is a top level competency.' - ); - - $params = array( - 'shortname' => $shortname, - 'idnumber' => $idnumber, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'visible' => $visible, - 'competencyframeworkid' => $competencyframeworkid, - 'parentid' => $parentid, - ); + $structure = competency_exporter::export_structure('create'); + $params = array('competency' => $structure); return new external_function_parameters($params); } /** - * Create a new competency framework + * Create a new competency * - * @param string $shortname - * @param string $idnumber - * @param string $description - * @param int $descriptionformat - * @param bool $visible - * @param int $competencyframeworkid - * @param int $parentid - * @return string the template + * @param array $competency All the fields for a competency record (including id) + * @return array the competency */ - public static function create_competency($shortname, - $idnumber, - $description, - $descriptionformat, - $visible, - $competencyframeworkid, - $parentid) { + public static function create_competency($competency) { global $PAGE; $params = self::validate_parameters(self::create_competency_parameters(), - array( - 'shortname' => $shortname, - 'idnumber' => $idnumber, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'visible' => $visible, - 'competencyframeworkid' => $competencyframeworkid, - 'parentid' => $parentid, - )); - + array('competency' => $competency)); + $params = $params['competency']; $framework = api::read_framework($params['competencyframeworkid']); $context = $framework->get_context(); self::validate_context($context); @@ -1141,7 +632,7 @@ class external extends external_api { * @return \external_description */ public static function create_competency_returns() { - return self::get_competency_external_structure(); + return competency_exporter::export_structure('read'); } /** @@ -1192,7 +683,7 @@ class external extends external_api { * @return \external_description */ public static function read_competency_returns() { - return self::get_competency_external_structure(); + return competency_exporter::export_structure('read'); } /** @@ -1247,101 +738,22 @@ class external extends external_api { * @return \external_function_parameters */ public static function update_competency_parameters() { - $id = new external_value( - PARAM_INT, - 'Data base record id for the competency', - VALUE_REQUIRED - ); - $shortname = new external_value( - PARAM_TEXT, - 'Short name for the competency.', - VALUE_REQUIRED - ); - $idnumber = new external_value( - PARAM_TEXT, - 'If provided, must be a unique string to identify this competency.', - VALUE_REQUIRED - ); - $description = new external_value( - PARAM_RAW, - 'Description for the framework', - VALUE_REQUIRED - ); - $descriptionformat = new external_format_value( - 'Description format for the framework', - VALUE_REQUIRED - ); - $visible = new external_value( - PARAM_BOOL, - 'Is this framework visible?', - VALUE_REQUIRED - ); - $ruletype = new external_value( - PARAM_RAW, - 'The type of rule', - VALUE_DEFAULT, - null - ); - $ruleoutcome = new external_value( - PARAM_INT, - 'The outcome when the rule matches. Constant value competency::OUTCOME_*', - VALUE_DEFAULT, - competency::OUTCOME_NONE - ); - $ruleconfig = new external_value( - PARAM_RAW, - 'The extra config of the rule.', - VALUE_DEFAULT, - null - ); - - $params = array( - 'id' => $id, - 'shortname' => $shortname, - 'idnumber' => $idnumber, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'visible' => $visible, - 'ruletype' => $ruletype, - 'ruleoutcome' => $ruleoutcome, - 'ruleconfig' => $ruleconfig, - ); + $structure = competency_exporter::export_structure('update'); + $params = array('competency' => $structure); return new external_function_parameters($params); } /** * Update an existing competency * - * @param int $id The competency id - * @param string $shortname - * @param string $idnumber - * @param string $description - * @param int $descriptionformat - * @param boolean $visible + * @param array $competency The array of competency fields (id is required). * @return boolean */ - public static function update_competency($id, - $shortname, - $idnumber, - $description, - $descriptionformat, - $visible, - $ruletype = null, - $ruleoutcome = competency::OUTCOME_NONE, - $ruleconfig = null) { + public static function update_competency($competency) { $params = self::validate_parameters(self::update_competency_parameters(), - array( - 'id' => $id, - 'shortname' => $shortname, - 'idnumber' => $idnumber, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'visible' => $visible, - 'ruletype' => $ruletype, - 'ruleoutcome' => $ruleoutcome, - 'ruleconfig' => $ruleconfig, - )); + array('competency' => $competency)); + $params = $params['competency']; $competency = api::read_competency($params['id']); self::validate_context($competency->get_context()); @@ -1419,11 +831,14 @@ class external extends external_api { self::validate_context($context); $output = $PAGE->get_renderer('tool_lp'); - $results = api::list_competencies($safefilters, - $params['sort'], - $params['order'], - $params['skip'], - $params['limit']); + $results = api::list_competencies( + $safefilters, + $params['sort'], + $params['order'], + $params['skip'], + $params['limit'] + ); + $records = array(); foreach ($results as $result) { $exporter = new competency_exporter($result, array('context' => $context)); @@ -1439,7 +854,7 @@ class external extends external_api { * @return \external_description */ public static function list_competencies_returns() { - return new external_multiple_structure(self::get_competency_external_structure()); + return new external_multiple_structure(competency_exporter::export_structure('read')); } /** @@ -1458,17 +873,10 @@ class external extends external_api { 'Competency framework id', VALUE_REQUIRED ); - $includerelated = new external_value( - PARAM_BOOL, - 'Include or not related competencies', - VALUE_DEFAULT, - false - ); $params = array( 'searchtext' => $searchtext, - 'competencyframeworkid' => $frameworkid, - 'includerelated' => $includerelated + 'competencyframeworkid' => $frameworkid ); return new external_function_parameters($params); } @@ -1481,14 +889,13 @@ class external extends external_api { * * @return array */ - public static function search_competencies($searchtext, $competencyframeworkid, $includerelated = false) { + public static function search_competencies($searchtext, $competencyframeworkid) { global $PAGE; $params = self::validate_parameters(self::search_competencies_parameters(), array( 'searchtext' => $searchtext, - 'competencyframeworkid' => $competencyframeworkid, - 'includerelated' => $includerelated + 'competencyframeworkid' => $competencyframeworkid )); $framework = api::read_framework($params['competencyframeworkid']); @@ -1502,16 +909,6 @@ class external extends external_api { $exporter = new competency_exporter($result, array('context' => $context)); $record = $exporter->export($output); - if ($params['includerelated']) { - $record->relatedcompetencies = array(); - $relatedcomps = $result->get_related_competencies(); - foreach ($relatedcomps as $comp) { - $exporter = new competency_exporter($comp, array('context' => $context)); - $comprecord = $exporter->export($output); - $record->relatedcompetencies[] = $comprecord; - } - } - array_push($records, $record); } @@ -1524,7 +921,7 @@ class external extends external_api { * @return \external_description */ public static function search_competencies_returns() { - return new external_multiple_structure(self::get_competency_external_structure(true)); + return new external_multiple_structure(competency_exporter::export_structure('read')); } /** @@ -1639,7 +1036,7 @@ class external extends external_api { */ public static function data_for_competencies_manage_page_returns() { return new external_single_structure(array ( - 'framework' => self::get_competency_framework_external_structure(), + 'framework' => competency_framework_exporter::export_structure('read'), 'canmanage' => new external_value(PARAM_BOOL, 'True if this user has permission to manage competency frameworks'), 'pagecontextid' => new external_value(PARAM_INT, 'Context id for the framework'), 'search' => new external_value(PARAM_RAW, 'Current search string'), @@ -2008,7 +1405,7 @@ class external extends external_api { * @return \external_description */ public static function list_competencies_in_course_returns() { - return new external_multiple_structure(self::get_competency_external_structure()); + return new external_multiple_structure(competency_exporter::export_structure('read')); } /** @@ -2161,7 +1558,7 @@ class external extends external_api { 'canmanagecompetencyframeworks' => new external_value(PARAM_BOOL, 'User can manage competency frameworks'), 'canmanagecoursecompetencies' => new external_value(PARAM_BOOL, 'User can manage linked course competencies'), 'competencies' => new external_multiple_structure( - self::get_competency_external_structure() + competency_exporter::export_structure('read') ), 'manageurl' => new external_value(PARAM_LOCALURL, 'Url to the manage competencies page.'), )); @@ -2286,147 +1683,35 @@ class external extends external_api { return new external_value(PARAM_BOOL, 'True if successful.'); } - /** - * Returns the external structure of a full template record. - * - * @return \external_single_structure - */ - protected static function get_template_external_structure() { - $id = new external_value( - PARAM_INT, - 'Database record id' - ); - $shortname = new external_value( - PARAM_TEXT, - 'Short name for the learning plan template' - ); - $duedate = new external_value( - PARAM_INT, - 'The default due date for instances of this plan.' - ); - $duedateformatted = new external_value( - PARAM_RAW, - 'Due date that has been formatted for display' - ); - $description = new external_value( - PARAM_RAW, - 'Description for the template' - ); - $descriptionformat = new external_format_value( - 'Description format for the template' - ); - $visible = new external_value( - PARAM_BOOL, - 'Is this template visible?' - ); - $timecreated = new external_value( - PARAM_INT, - 'Timestamp this record was created' - ); - $timemodified = new external_value( - PARAM_INT, - 'Timestamp this record was modified' - ); - $usermodified = new external_value( - PARAM_INT, - 'User who modified this record last' - ); - $contextid = new external_value( - PARAM_INT, - 'The context ID the template belongs to' - ); - - $returns = array( - 'id' => $id, - 'shortname' => $shortname, - 'duedate' => $duedate, - 'duedateformatted' => $duedateformatted, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'visible' => $visible, - 'timecreated' => $timecreated, - 'timemodified' => $timemodified, - 'usermodified' => $usermodified, - 'contextid' => $contextid, - ); - return new external_single_structure($returns); - } - /** * Returns description of create_template() parameters. * * @return \external_function_parameters */ public static function create_template_parameters() { - $shortname = new external_value( - PARAM_TEXT, - 'Short name for the learning plan template.', - VALUE_REQUIRED - ); - $duedate = new external_value( - PARAM_INT, - 'The default due date for instances of this plan', - VALUE_DEFAULT, - 0 - ); - $description = new external_value( - PARAM_RAW, - 'Optional description for the learning plan template', - VALUE_DEFAULT, - '' - ); - $descriptionformat = new external_format_value( - 'Optional description format for the learning plan template', - VALUE_DEFAULT, - FORMAT_HTML - ); - $visible = new external_value( - PARAM_BOOL, - 'Is this learning plan template visible?', - VALUE_DEFAULT, - true - ); - - $params = array( - 'shortname' => $shortname, - 'duedate' => $duedate, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'visible' => $visible, - 'context' => self::get_context_parameters() - ); + $structure = template_exporter::export_structure('create'); + $params = array('template' => $structure); return new external_function_parameters($params); } /** * Create a new learning plan template * - * @param string $shortname The short name of the template. - * @param string $idnumber The idnumber of the template. - * @param int $duedate The due date for instances of this plan. - * @param string $description The description of the template. - * @param int $descriptionformat The format of the description - * @param bool $visible Is this template visible. - * @param array $context The context info. + * @param array $template The list of fields for the template. * @return \stdClass Record of new template. */ - public static function create_template($shortname, $duedate, $description, $descriptionformat, $visible, $context) { + public static function create_template($template) { global $PAGE; $params = self::validate_parameters(self::create_template_parameters(), - array( - 'shortname' => $shortname, - 'duedate' => $duedate, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'visible' => $visible, - 'context' => $context - )); - $context = self::get_context_from_params($params['context']); + array('template' => $template)); + $params = $params['template']; + $context = self::get_context_from_params($params); self::validate_context($context); $output = $PAGE->get_renderer('tool_lp'); - unset($params['context']); + unset($params['contextlevel']); + unset($params['instanceid']); $params = (object) $params; $params->contextid = $context->id; @@ -2442,7 +1727,7 @@ class external extends external_api { * @return \external_description */ public static function create_template_returns() { - return self::get_template_external_structure(); + return template_exporter::export_structure('read'); } /** @@ -2492,7 +1777,7 @@ class external extends external_api { * @return \external_description */ public static function read_template_returns() { - return self::get_template_external_structure(); + return template_exporter::export_structure('read'); } /** @@ -2546,75 +1831,22 @@ class external extends external_api { * @return \external_function_parameters */ public static function update_template_parameters() { - $id = new external_value( - PARAM_INT, - 'Data base record id for the template', - VALUE_REQUIRED - ); - $shortname = new external_value( - PARAM_TEXT, - 'Short name for the learning plan template.', - VALUE_REQUIRED - ); - $duedate = new external_value( - PARAM_INT, - 'Default due date for instances of this plan', - VALUE_REQUIRED - ); - $description = new external_value( - PARAM_RAW, - 'Description for the template', - VALUE_REQUIRED - ); - $descriptionformat = new external_format_value( - 'Description format for the template', - VALUE_REQUIRED - ); - $visible = new external_value( - PARAM_BOOL, - 'Is this template visible?', - VALUE_REQUIRED - ); - - $params = array( - 'id' => $id, - 'shortname' => $shortname, - 'duedate' => $duedate, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'visible' => $visible, - ); + $structure = template_exporter::export_structure('update'); + $params = array('template' => $structure); return new external_function_parameters($params); } /** * Update an existing learning plan template * - * @param int $id The learning plan template id - * @param string $shortname - * @param string $idnumber - * @param int $duedate - * @param string $description - * @param int $descriptionformat - * @param boolean $visible + * @param array $template The list of fields for the template. * @return boolean */ - public static function update_template($id, - $shortname, - $duedate, - $description, - $descriptionformat, - $visible) { + public static function update_template($template) { $params = self::validate_parameters(self::update_template_parameters(), - array( - 'id' => $id, - 'shortname' => $shortname, - 'duedate' => $duedate, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'visible' => $visible - )); + array('template' => $template)); + $params = $params['template']; $template = api::read_template($params['id']); self::validate_context($template->get_context()); @@ -2679,7 +1911,7 @@ class external extends external_api { * @return \external_description */ public static function duplicate_template_returns() { - return self::get_template_external_structure(); + return template_exporter::export_structure('read'); } /** @@ -2784,7 +2016,7 @@ class external extends external_api { * @return \external_description */ public static function list_templates_returns() { - return new external_multiple_structure(self::get_template_external_structure()); + return new external_multiple_structure(template_exporter::export_structure('read')); } /** @@ -2876,7 +2108,7 @@ class external extends external_api { return new external_single_structure(array ( 'canmanage' => new external_value(PARAM_BOOL, 'True if this user has permission to manage learning plan templates'), 'templates' => new external_multiple_structure( - self::get_template_external_structure() + template_exporter::export_structure('read') ), 'pluginbaseurl' => new external_value(PARAM_LOCALURL, 'Url to the tool_lp plugin folder on this Moodle site'), 'navigation' => new external_multiple_structure( @@ -2981,7 +2213,7 @@ class external extends external_api { * @return \external_description */ public static function list_templates_using_competency_returns() { - return new external_multiple_structure(self::get_template_external_structure()); + return new external_multiple_structure(template_exporter::export_structure('read')); } /** @@ -3085,7 +2317,7 @@ class external extends external_api { * @return \external_description */ public static function list_competencies_in_template_returns() { - return new external_multiple_structure(self::get_competency_external_structure()); + return new external_multiple_structure(competency_exporter::export_structure('read')); } /** @@ -3407,7 +2639,7 @@ class external extends external_api { 'canmanagecompetencyframeworks' => new external_value(PARAM_BOOL, 'User can manage competency frameworks'), 'canmanagetemplates' => new external_value(PARAM_BOOL, 'User can manage learning plan templates'), 'competencies' => new external_multiple_structure( - self::get_competency_external_structure() + competency_exporter::export_structure('read') ), 'manageurl' => new external_value(PARAM_LOCALURL, 'Url to the manage competencies page.'), )); @@ -3464,177 +2696,38 @@ class external extends external_api { 'iscompleted' => new external_value(PARAM_BOOL, 'Is the plan completed'), 'competencies' => new external_multiple_structure( new external_single_structure(array( - 'competency' => self::get_competency_external_structure(), - 'usercompetency' => self::get_user_competency_external_structure(true, VALUE_OPTIONAL), - 'usercompetencyplan' => self::get_user_competency_plan_external_structure(true, VALUE_OPTIONAL) + 'competency' => competency_exporter::export_structure('read'), + 'usercompetency' => user_competency_exporter::export_structure('read'), + 'usercompetencyplan' => user_competency_plan_exporter::export_structure('read') )) ) )); } - /** - * A learning plan structure. - * - * @return \external_single_structure - */ - protected static function get_plan_external_structure() { - $id = new external_value( - PARAM_INT, - 'Database record id' - ); - $name = new external_value( - PARAM_TEXT, - 'Name for the learning plan' - ); - $description = new external_value( - PARAM_RAW, - 'Description for the template' - ); - $descriptionformat = new external_format_value( - 'Description format for the template' - ); - $userid = new external_value( - PARAM_INT, - 'Learning plan user id' - ); - $templateid = new external_value( - PARAM_INT, - 'Learning plan templateid' - ); - $status = new external_value( - PARAM_INT, - 'Learning plan status identifier.' - ); - $duedate = new external_value( - PARAM_INT, - 'The default due date for instances of this plan.' - ); - $timecreated = new external_value( - PARAM_INT, - 'Timestamp this record was created' - ); - $timemodified = new external_value( - PARAM_INT, - 'Timestamp this record was modified' - ); - $usermodified = new external_value( - PARAM_INT, - 'User who modified this record last' - ); - - // Extra params. - $statusname = new external_value( - PARAM_TEXT, - 'Learning plan status name' - ); - $usercanupdate = new external_value( - PARAM_BOOL, - 'Whether the current user can update this plan or not' - ); - - $returns = array( - 'id' => $id, - 'name' => $name, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'userid' => $userid, - 'templateid' => $templateid, - 'status' => $status, - 'duedate' => $duedate, - 'timecreated' => $timecreated, - 'timemodified' => $timemodified, - 'usermodified' => $usermodified, - 'statusname' => $statusname, - 'usercanupdate' => $usercanupdate - ); - - return new external_single_structure($returns); - } - /** * Returns description of create_plan() parameters. * * @return \external_function_parameters */ public static function create_plan_parameters() { - $name = new external_value( - PARAM_TEXT, - 'Name for the learning plan template.', - VALUE_REQUIRED - ); - $description = new external_value( - PARAM_RAW, - 'Optional description for the learning plan description', - VALUE_DEFAULT, - '' - ); - $descriptionformat = new external_format_value( - 'Optional description format for the learning plan description', - VALUE_DEFAULT, - FORMAT_HTML - ); - $userid = new external_value( - PARAM_INT, - 'The learning plan user id', - VALUE_REQUIRED - ); - $templateid = new external_value( - PARAM_INT, - 'Optional template id', - VALUE_DEFAULT, - 0 - ); - $status = new external_value( - PARAM_INT, - 'Optional template id', - VALUE_DEFAULT, - plan::STATUS_DRAFT - ); - $duedate = new external_value( - PARAM_INT, - 'The default due date for this plan', - VALUE_DEFAULT, - 0 - ); - - $params = array( - 'name' => $name, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'userid' => $userid, - 'templateid' => $templateid, - 'status' => $status, - 'duedate' => $duedate - ); + $structure = plan_exporter::export_structure('create'); + $params = array('plan' => $structure); return new external_function_parameters($params); } /** * Create a new learning plan. * - * @param string $name Name. - * @param string $description Plan description. - * @param string $descriptionformat Plan description format. - * @param int $userid User id. - * @param int $templateid Related template id. - * @param int $status status. - * @param int $duedate due date. - * - * @return mixed + * @param array $plan List of fields for the plan. + * @return array New plan record. */ - public static function create_plan($name, $description, $descriptionformat, $userid, $templateid, $status, $duedate) { + public static function create_plan($plan) { global $PAGE; $params = self::validate_parameters(self::create_plan_parameters(), - array( - 'name' => $name, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'userid' => $userid, - 'templateid' => $templateid, - 'status' => $status, - 'duedate' => $duedate - )); + array('plan' => $plan)); + $params = $params['plan']; + $context = context_user::instance($params['userid']); self::validate_context($context); $output = $PAGE->get_renderer('tool_lp'); @@ -3644,7 +2737,6 @@ class external extends external_api { $result = api::create_plan($params); $exporter = new plan_exporter($result); $record = $exporter->export($output); - $record->usercanupdate = $result->can_manage(); return external_api::clean_returnvalue(self::create_plan_returns(), $record); } @@ -3654,7 +2746,7 @@ class external extends external_api { * @return \external_description */ public static function create_plan_returns() { - return self::get_plan_external_structure(); + return plan_exporter::export_structure('read'); } /** @@ -3663,92 +2755,24 @@ class external extends external_api { * @return \external_function_parameters */ public static function update_plan_parameters() { - $id = new external_value( - PARAM_INT, - 'Learning plan id', - VALUE_REQUIRED - ); - $name = new external_value( - PARAM_TEXT, - 'Name for the learning plan template.', - VALUE_REQUIRED - ); - $description = new external_value( - PARAM_RAW, - 'Optional description for the learning plan description', - VALUE_DEFAULT, - '' - ); - $descriptionformat = new external_format_value( - 'Optional description format for the learning plan description', - VALUE_DEFAULT, - FORMAT_HTML - ); - $userid = new external_value( - PARAM_INT, - 'The learning plan user id', - VALUE_REQUIRED - ); - $templateid = new external_value( - PARAM_INT, - 'Optional template id', - VALUE_DEFAULT, - 0 - ); - $status = new external_value( - PARAM_INT, - 'Optional template id', - VALUE_DEFAULT, - plan::STATUS_DRAFT - ); - $duedate = new external_value( - PARAM_INT, - 'The default due date for this plan', - VALUE_DEFAULT, - 0 - ); - - $params = array( - 'id' => $id, - 'name' => $name, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'userid' => $userid, - 'templateid' => $templateid, - 'status' => $status, - 'duedate' => $duedate - ); + $structure = plan_exporter::export_structure('update'); + $params = array('plan' => $structure); return new external_function_parameters($params); } /** * Updates a new learning plan. * - * @param int $id Plan id. - * @param string $name Name. - * @param string $description Plan description. - * @param string $descriptionformat Plan description format. - * @param int $userid User id. - * @param int $templateid Related template id. - * @param int $status status. - * @param int $duedate due date. - * + * @param array $plan Fields for the plan (id is required) * @return mixed */ - public static function update_plan($id, $name, $description, $descriptionformat, $userid, $templateid, $status, $duedate) { + public static function update_plan($plan) { global $PAGE; $params = self::validate_parameters(self::update_plan_parameters(), - array( - 'id' => $id, - 'name' => $name, - 'description' => $description, - 'descriptionformat' => $descriptionformat, - 'userid' => $userid, - 'templateid' => $templateid, - 'status' => $status, - 'duedate' => $duedate - )); + array('plan' => $plan)); + + $params = $params['plan']; $plan = api::read_plan($params['id']); self::validate_context($plan->get_context()); @@ -3758,7 +2782,6 @@ class external extends external_api { $result = api::update_plan($params); $exporter = plan_exporter($result); $record = $exporter->export($output); - $record->usercanupdate = $result->can_manage(); return external_api::clean_returnvalue(self::update_plan_returns(), $record); } @@ -3768,7 +2791,7 @@ class external extends external_api { * @return \external_description */ public static function update_plan_returns() { - return self::get_plan_external_structure(); + return plan_exporter::export_structure('read'); } /** @@ -3805,7 +2828,6 @@ class external extends external_api { $exporter = new plan_exporter($plan); $record = $exporter->export($output); - $record->usercanupdate = $plan->can_manage(); return external_api::clean_returnvalue(self::read_plan_returns(), $record); } @@ -3815,7 +2837,7 @@ class external extends external_api { * @return \external_description */ public static function read_plan_returns() { - return self::get_plan_external_structure(); + return plan_exporter::export_structure('read'); } /** @@ -3910,7 +2932,7 @@ class external extends external_api { return new external_single_structure(array ( 'userid' => new external_value(PARAM_INT, 'The learning plan user id'), 'plans' => new external_multiple_structure( - self::get_plan_external_structure() + plan_exporter::export_structure('read') ), 'pluginbaseurl' => new external_value(PARAM_LOCALURL, 'Url to the tool_lp plugin folder on this Moodle site'), 'navigation' => new external_multiple_structure( @@ -3989,13 +3011,19 @@ class external extends external_api { * @return \external_description */ public static function list_plan_competencies_returns() { + $uc = user_competency_exporter::export_structure('read'); + $ucp = user_competency_plan_exporter::export_structure('read'); + + $uc->required = VALUE_OPTIONAL; + $ucp->required = VALUE_OPTIONAL; + return new external_multiple_structure( new external_single_structure(array( - 'competency' => self::get_competency_external_structure(), - 'usercompetency' => self::get_user_competency_external_structure(false, VALUE_OPTIONAL), - 'usercompetencyplan' => self::get_user_competency_plan_external_structure(false, VALUE_OPTIONAL), - ) - )); + 'competency' => competency_exporter::export_structure('read'), + 'usercompetency' => $uc, + 'usercompetencyplan' => $ucp + )) + ); } /** @@ -4203,7 +3231,7 @@ class external extends external_api { */ public static function data_for_related_competencies_section_returns() { return new external_single_structure(array( - 'relatedcompetencies' => new external_multiple_structure(self::get_competency_external_structure(true)), + 'relatedcompetencies' => new external_multiple_structure(competency_exporter::export_structure('read')), 'showdeleterelatedaction' => new external_value(PARAM_BOOL, 'Whether to show the delete relation link or not') )); } diff --git a/admin/tool/lp/classes/external/competency_exporter.php b/admin/tool/lp/classes/external/competency_exporter.php index 79a9a0b11b5..a5aa45e13d9 100644 --- a/admin/tool/lp/classes/external/competency_exporter.php +++ b/admin/tool/lp/classes/external/competency_exporter.php @@ -31,12 +31,12 @@ namespace tool_lp\external; */ class competency_exporter extends persistent_exporter { - protected function get_related() { + protected static function get_related() { // We cache the context so it does not need to be retrieved from the framework every time. return array('context' => '\context'); } - protected function get_persistent_class() { + protected static function get_persistent_class() { return 'tool_lp\\competency'; } } diff --git a/admin/tool/lp/classes/external/competency_framework_exporter.php b/admin/tool/lp/classes/external/competency_framework_exporter.php index 4ea9f109023..8105566251d 100644 --- a/admin/tool/lp/classes/external/competency_framework_exporter.php +++ b/admin/tool/lp/classes/external/competency_framework_exporter.php @@ -33,7 +33,8 @@ use renderer_base; */ class competency_framework_exporter extends persistent_exporter { - protected function get_persistent_class() { + protected static function get_persistent_class() { return 'tool_lp\\competency_framework'; } + } diff --git a/admin/tool/lp/classes/external/course_competency_exporter.php b/admin/tool/lp/classes/external/course_competency_exporter.php index 5f349a20bbb..27bb70af2c1 100644 --- a/admin/tool/lp/classes/external/course_competency_exporter.php +++ b/admin/tool/lp/classes/external/course_competency_exporter.php @@ -31,7 +31,7 @@ namespace tool_lp\external; */ class course_competency_exporter extends persistent_exporter { - protected function get_persistent_class() { + protected static function get_persistent_class() { return 'tool_lp\\course_competency'; } } diff --git a/admin/tool/lp/classes/external/persistent_exporter.php b/admin/tool/lp/classes/external/persistent_exporter.php index aa23c5feb4c..eaaa6bfd846 100644 --- a/admin/tool/lp/classes/external/persistent_exporter.php +++ b/admin/tool/lp/classes/external/persistent_exporter.php @@ -30,6 +30,9 @@ use renderer_base; use context; use context_system; use coding_exception; +use external_single_structure; +use external_value; +use external_format_value; /** * An extended version of the persistent class with a default implementation of export @@ -51,7 +54,7 @@ abstract class persistent_exporter { * * @return array of 'propertyname' => classname */ - protected function get_related() { + protected static function get_related() { return array(); } @@ -60,7 +63,9 @@ abstract class persistent_exporter { * * @return string */ - abstract protected function get_persistent_class(); + protected static function get_persistent_class() { + throw new coding_exception('get_persistent_class() must be overidden.'); + } /** * Constructor - saves the persistent object, and the related objects. @@ -69,15 +74,15 @@ abstract class persistent_exporter { * @param array $related - An optional list of pre-loaded objects related to this persistent. */ function __construct(\tool_lp\persistent $persistent, $related = array()) { - $classname = $this->get_persistent_class(); + $classname = static::get_persistent_class(); if (!$persistent instanceof $classname) { throw new coding_exception('Invalid type for persistent. ' . - 'Expected: ' . $this->get_persistent_class() . ' got: ' . get_class($persistent)); + 'Expected: ' . static::get_persistent_class() . ' got: ' . get_class($persistent)); } $this->persistent = $persistent; // Cache the valid related objects. - foreach ($this->get_related() as $key => $classname) { + foreach (static::get_related() as $key => $classname) { if (isset($related[$key]) && ($related[$key] instanceof $classname)) { $this->related[$key] = $related[$key]; } else { @@ -136,4 +141,76 @@ abstract class persistent_exporter { } return $data; } + + /** + * Modify the list of fields exported for 'read'. This is used when we return additional 'virtual' fields + * that are useful for display, but are not part of the persistent definition. + * + * @param array $fields - The standard list of fields for this persistent. + * @return array The modified list of fields, passed to new external_single_structure. + */ + public static function export_read_properties_structure($fields) { + return $fields; + } + + /** + * Function to export the structure of the persistent, so it can be re-used in all + * external function params/returns definitions. + * + * @param string $for - One of 'create', 'update' or 'read' - There are different structures for each. + * @return external_single_structure + */ + public static function export_structure($for) { + $classname = static::get_persistent_class(); + $properties = $classname::properties_definition(); + $returns = array(); + + if (!in_array($for, array('create', 'update', 'read'))) { + throw new coding_exception('First parameter for persistent_exporter::export_structure must ' . + 'be one of "create", "update" or "read".'); + } + + foreach ($properties as $property => $definition) { + $required = VALUE_REQUIRED; + if ($for == 'update') { + $required = VALUE_OPTIONAL; + } + $default = null; + $nullallowed = NULL_NOT_ALLOWED; + // We cannot use isset here because we want to detect nulls. + if ($for == 'create' && array_key_exists('default', $definition)) { + $required = VALUE_DEFAULT; + $default = $definition['default']; + } + if (!empty($definition['null'])) { + $nullallowed = NULL_ALLOWED; + } + if ($property == 'id') { + if ($for == 'create') { + continue; + } else { + $required = VALUE_REQUIRED; + } + } + if ($property == 'contextid') { + if ($for == 'create') { + $returns['contextid'] = new external_value(PARAM_INT, 'The context id', VALUE_OPTIONAL); + $returns['contextlevel'] = new external_value(PARAM_ALPHA, 'The context level', VALUE_OPTIONAL); + $returns['instanceid'] = new external_value(PARAM_INT, 'The Instance id', VALUE_OPTIONAL); + } else { + $returns['contextid'] = new external_value(PARAM_INT, 'The context id', VALUE_OPTIONAL); + } + } else { + if ($definition['type'] == PARAM_FORMAT) { + $returns[$property] = new external_format_value($property, $required, $default, $nullallowed); + } else { + $returns[$property] = new external_value($definition['type'], $property, $required, $default, $nullallowed); + } + } + } + if ($for == 'read') { + $returns = static::export_read_properties_structure($returns); + } + return new external_single_structure($returns); + } } diff --git a/admin/tool/lp/classes/external/plan_competency_exporter.php b/admin/tool/lp/classes/external/plan_competency_exporter.php index 4cfe24af481..700bd980c8d 100644 --- a/admin/tool/lp/classes/external/plan_competency_exporter.php +++ b/admin/tool/lp/classes/external/plan_competency_exporter.php @@ -31,7 +31,7 @@ namespace tool_lp\external; */ class plan_competency_exporter extends persistent_exporter { - protected function get_persistent_class() { + protected static function get_persistent_class() { return 'tool_lp\\plan_competency'; } } diff --git a/admin/tool/lp/classes/external/plan_exporter.php b/admin/tool/lp/classes/external/plan_exporter.php index 1ef868b4307..65fa728fc00 100644 --- a/admin/tool/lp/classes/external/plan_exporter.php +++ b/admin/tool/lp/classes/external/plan_exporter.php @@ -24,6 +24,7 @@ namespace tool_lp\external; use renderer_base; +use external_value; /** * Class for exporting plan data. @@ -33,13 +34,20 @@ use renderer_base; */ class plan_exporter extends persistent_exporter { - protected function get_persistent_class() { + protected static function get_persistent_class() { return 'tool_lp\\plan'; } public function export(renderer_base $output) { $result = parent::export($output); $result->statusname = $this->persistent->get_statusname(); + $result->usercanupdate = $this->persistent->can_manage(); return $result; } + + public static function export_read_properties_structure($fields) { + $fields['statusname'] = new external_value(PARAM_TEXT, 'statusname, readonly'); + $fields['usercanupdate'] = new external_value(PARAM_BOOL, 'usercanupdate, readonly'); + return $fields; + } } diff --git a/admin/tool/lp/classes/external/related_competency_exporter.php b/admin/tool/lp/classes/external/related_competency_exporter.php index 8cccf6fcfae..03c96eedb3b 100644 --- a/admin/tool/lp/classes/external/related_competency_exporter.php +++ b/admin/tool/lp/classes/external/related_competency_exporter.php @@ -31,7 +31,7 @@ namespace tool_lp\external; */ class related_competency_exporter extends persistent_exporter { - protected function get_persistent_class() { + protected static function get_persistent_class() { return 'tool_lp\\related_competency'; } } diff --git a/admin/tool/lp/classes/external/template_competency_exporter.php b/admin/tool/lp/classes/external/template_competency_exporter.php index 14cc2ff9c17..b6e98775c3f 100644 --- a/admin/tool/lp/classes/external/template_competency_exporter.php +++ b/admin/tool/lp/classes/external/template_competency_exporter.php @@ -31,7 +31,7 @@ namespace tool_lp\external; */ class template_competency_exporter extends persistent_exporter { - protected function get_persistent_class() { + protected static function get_persistent_class() { return 'tool_lp\\template_competency'; } } diff --git a/admin/tool/lp/classes/external/template_exporter.php b/admin/tool/lp/classes/external/template_exporter.php index 0e431ef231d..ecbfca203c6 100644 --- a/admin/tool/lp/classes/external/template_exporter.php +++ b/admin/tool/lp/classes/external/template_exporter.php @@ -24,6 +24,7 @@ namespace tool_lp\external; use renderer_base; +use external_value; /** * Class for exporting template data. @@ -33,7 +34,7 @@ use renderer_base; */ class template_exporter extends persistent_exporter { - protected function get_persistent_class() { + protected static function get_persistent_class() { return 'tool_lp\\template'; } @@ -42,4 +43,9 @@ class template_exporter extends persistent_exporter { $result->duedateformatted = userdate($result->duedate); return $result; } + + public static function export_read_properties_structure($fields) { + $fields['duedateformatted'] = new external_value(PARAM_TEXT, 'duedateformatted, readonly'); + return $fields; + } } diff --git a/admin/tool/lp/classes/external/user_competency_exporter.php b/admin/tool/lp/classes/external/user_competency_exporter.php index 346ab84ba9b..417207322de 100644 --- a/admin/tool/lp/classes/external/user_competency_exporter.php +++ b/admin/tool/lp/classes/external/user_competency_exporter.php @@ -25,6 +25,7 @@ namespace tool_lp\external; use renderer_base; use context_user; +use external_value; use tool_lp\user_competency; /** @@ -35,11 +36,11 @@ use tool_lp\user_competency; */ class user_competency_exporter extends persistent_exporter { - protected function get_persistent_class() { + protected static function get_persistent_class() { return 'tool_lp\\user_competency'; } - protected function get_related() { + protected static function get_related() { // We cache the scale so it does not need to be retrieved from the framework every time. return array('scale' => 'grade_scale'); } @@ -68,4 +69,11 @@ class user_competency_exporter extends persistent_exporter { $result->statusname = $statusname; return $result; } + + public static function export_read_properties_structure($fields) { + $fields['gradename'] = new external_value(PARAM_TEXT, 'gradename, readonly'); + $fields['proficiencyname'] = new external_value(PARAM_TEXT, 'proficiencyname, readonly'); + $fields['statusname'] = new external_value(PARAM_TEXT, 'statusname, readonly'); + return $fields; + } } diff --git a/admin/tool/lp/classes/external/user_competency_plan_exporter.php b/admin/tool/lp/classes/external/user_competency_plan_exporter.php index eff82499fff..dfb773cfda8 100644 --- a/admin/tool/lp/classes/external/user_competency_plan_exporter.php +++ b/admin/tool/lp/classes/external/user_competency_plan_exporter.php @@ -33,7 +33,7 @@ use renderer_base; */ class user_competency_plan_exporter extends persistent_exporter { - protected function get_persistent_class() { + protected static function get_persistent_class() { return 'tool_lp\\user_competency_plan'; } diff --git a/admin/tool/lp/classes/output/plans_page.php b/admin/tool/lp/classes/output/plans_page.php index 84027c8885a..e21c69d44ed 100644 --- a/admin/tool/lp/classes/output/plans_page.php +++ b/admin/tool/lp/classes/output/plans_page.php @@ -89,7 +89,6 @@ class plans_page implements renderable, templatable { foreach ($this->plans as $plan) { $exporter = new plan_exporter($plan); $record = $exporter->export($output); - $record->usercanupdate = $plan->can_manage(); $data->plans[] = $record; } } diff --git a/admin/tool/lp/templates/related_competencies.mustache b/admin/tool/lp/templates/related_competencies.mustache index c3f0648a7eb..f26ec6d0eb7 100644 --- a/admin/tool/lp/templates/related_competencies.mustache +++ b/admin/tool/lp/templates/related_competencies.mustache @@ -16,4 +16,7 @@ {{/visible}} {{/relatedcompetencies}} +{{^relatedcompetencies}} + {{#str}}norelatedcompetencies, tool_lp{{/str}} +{{/relatedcompetencies}} diff --git a/admin/tool/lp/tests/externallib_test.php b/admin/tool/lp/tests/externallib_test.php index 55a228e8b96..7c4182f831f 100644 --- a/admin/tool/lp/tests/externallib_test.php +++ b/admin/tool/lp/tests/externallib_test.php @@ -148,14 +148,129 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { accesslib_clear_all_caches_for_unit_testing(); } + protected function create_competency_framework($number = 1, $system = true) { + $scalepropname = 'scaleconfiguration' . $number; + $framework = array( + 'shortname' => 'shortname' . $number, + 'idnumber' => 'idnumber' . $number, + 'description' => 'description' . $number, + 'descriptionformat' => FORMAT_HTML, + 'scaleid' => $number, + 'scaleconfiguration' => $this->$scalepropname, + 'visible' => true, + 'contextid' => $system ? context_system::instance()->id : context_coursecat::instance($this->category->id)->id + ); + $result = external::create_competency_framework($framework); + return (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); + } + + protected function create_plan($number, $userid, $templateid, $status, $duedate) { + $plan = array( + 'name' => 'name' . $number, + 'description' => 'description' . $number, + 'descriptionformat' => FORMAT_HTML, + 'userid' => $userid, + 'templateid' => $templateid, + 'status' => $status, + 'duedate' => $duedate + ); + $result = external::create_plan($plan); + return (object) external_api::clean_returnvalue(external::create_plan_returns(), $result); + } + + protected function create_template($number, $system) { + $template = array( + 'shortname' => 'shortname' . $number, + 'description' => 'description' . $number, + 'descriptionformat' => FORMAT_HTML, + 'duedate' => 0, + 'visible' => true, + 'contextid' => $system ? context_system::instance()->id : context_coursecat::instance($this->category->id)->id + ); + $result = external::create_template($template); + return (object) external_api::clean_returnvalue(external::create_template_returns(), $result); + } + + protected function update_template($templateid, $number) { + $template = array( + 'id' => $templateid, + 'shortname' => 'shortname' . $number, + 'description' => 'description' . $number, + 'descriptionformat' => FORMAT_HTML, + 'visible' => true + ); + $result = external::update_template($template); + return external_api::clean_returnvalue(external::update_template_returns(), $result); + } + + protected function update_plan($planid, $number, $userid, $templateid, $status, $duedate) { + $plan = array( + 'id' => $planid, + 'name' => 'name' . $number, + 'description' => 'description' . $number, + 'descriptionformat' => FORMAT_HTML, + 'userid' => $userid, + 'templateid' => $templateid, + 'status' => $status, + 'duedate' => $duedate + ); + $result = external::update_plan($plan); + return external_api::clean_returnvalue(external::update_plan_returns(), $result); + } + + protected function update_competency_framework($id, $number = 1, $system = true) { + $scalepropname = 'scaleconfiguration' . $number; + $framework = array( + 'id' => $id, + 'shortname' => 'shortname' . $number, + 'idnumber' => 'idnumber' . $number, + 'description' => 'description' . $number, + 'descriptionformat' => FORMAT_HTML, + 'scaleid' => $number, + 'scaleconfiguration' => $this->$scalepropname, + 'visible' => true, + 'contextid' => $system ? context_system::instance()->id : context_coursecat::instance($this->category->id)->id + ); + $result = external::update_competency_framework($framework); + return external_api::clean_returnvalue(external::update_competency_framework_returns(), $result); + } + + protected function create_competency($number, $frameworkid) { + $competency = array( + 'shortname' => 'shortname' . $number, + 'idnumber' => 'idnumber' . $number, + 'description' => 'description' . $number, + 'descriptionformat' => FORMAT_HTML, + 'visible' => true, + 'competencyframeworkid' => $frameworkid, + 'sortorder' => 0 + ); + $result = external::create_competency($competency); + return (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); + } + + protected function update_competency($id, $number) { + $competency = array( + 'id' => $id, + 'shortname' => 'shortname' . $number, + 'idnumber' => 'idnumber' . $number, + 'description' => 'description' . $number, + 'descriptionformat' => FORMAT_HTML, + 'visible' => true, + 'sortorder' => 0 + ); + $result = external::update_competency($competency); + return external_api::clean_returnvalue(external::update_competency_returns(), $result); + } + /** * Test we can't create a competency framework with only read permissions. */ public function test_create_competency_frameworks_with_read_permissions() { $this->setExpectedException('required_capability_exception'); $this->setUser($this->user); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); + + $result = $this->create_competency_framework(1, true); } /** @@ -164,8 +279,7 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { public function test_create_competency_frameworks_with_read_permissions_in_category() { $this->setExpectedException('required_capability_exception'); $this->setUser($this->catuser); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_coursecat::instance($this->category->id)->id)); + $result = $this->create_competency_framework(1, false); } /** @@ -173,16 +287,14 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_create_competency_frameworks_with_manage_permissions() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); + $result = $this->create_competency_framework(1, true); $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->creator->id, $result->usermodified); - $this->assertEquals('shortname', $result->shortname); - $this->assertEquals('idnumber', $result->idnumber); - $this->assertEquals('description', $result->description); + $this->assertEquals('shortname1', $result->shortname); + $this->assertEquals('idnumber1', $result->idnumber); + $this->assertEquals('description1', $result->description); $this->assertEquals(FORMAT_HTML, $result->descriptionformat); $this->assertEquals(1, $result->scaleid); $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration); @@ -194,24 +306,21 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_create_competency_frameworks_with_manage_permissions_in_category() { $this->setUser($this->catcreator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_coursecat::instance($this->category->id)->id)); - $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); + $result = $this->create_competency_framework(1, false); $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->catcreator->id, $result->usermodified); - $this->assertEquals('shortname', $result->shortname); - $this->assertEquals('idnumber', $result->idnumber); - $this->assertEquals('description', $result->description); + $this->assertEquals('shortname1', $result->shortname); + $this->assertEquals('idnumber1', $result->idnumber); + $this->assertEquals('description1', $result->description); $this->assertEquals(FORMAT_HTML, $result->descriptionformat); $this->assertEquals(1, $result->scaleid); $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration); $this->assertEquals(true, $result->visible); try { - external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 2, - $this->scaleconfiguration2, true, array('contextid' => context_system::instance()->id)); + $result = $this->create_competency_framework(1, true); $this->fail('User cannot create a framework at system level.'); } catch (required_capability_exception $e) { } @@ -223,8 +332,17 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { public function test_create_competency_frameworks_with_nasty_data() { $this->setUser($this->creator); $this->setExpectedException('invalid_parameter_exception'); - $result = external::create_competency_framework('short', 'id;"number', 'de<>\\..scription', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); + $framework = array( + 'shortname' => 'short', + 'idnumber' => 'id;"number', + 'description' => 'de<>\\..scription', + 'descriptionformat' => FORMAT_HTML, + 'scaleid' => 1, + 'scaleconfiguration' => $this->scaleconfiguration1, + 'visible' => true, + 'contextid' => context_system::instance()->id + ); + $result = external::create_competency_framework($framework); } /** @@ -232,9 +350,7 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_read_competency_frameworks_with_manage_permissions() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); + $result = $this->create_competency_framework(1, true); $id = $result->id; $result = external::read_competency_framework($id); @@ -243,9 +359,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->creator->id, $result->usermodified); - $this->assertEquals('shortname', $result->shortname); - $this->assertEquals('idnumber', $result->idnumber); - $this->assertEquals('description', $result->description); + $this->assertEquals('shortname1', $result->shortname); + $this->assertEquals('idnumber1', $result->idnumber); + $this->assertEquals('description1', $result->description); $this->assertEquals(FORMAT_HTML, $result->descriptionformat); $this->assertEquals(1, $result->scaleid); $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration); @@ -258,12 +374,8 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { public function test_read_competency_frameworks_with_manage_permissions_in_category() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $insystem = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); - $result = external::create_competency_framework('catshortname', 'catidnumber', 'catdescription', FORMAT_HTML, 2, - $this->scaleconfiguration2, true, array('contextid' => context_coursecat::instance($this->category->id)->id)); - $incat = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); + $insystem = $this->create_competency_framework(1, true); + $incat = $this->create_competency_framework(2, false); $this->setUser($this->catcreator); $id = $incat->id; @@ -273,9 +385,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->creator->id, $result->usermodified); - $this->assertEquals('catshortname', $result->shortname); - $this->assertEquals('catidnumber', $result->idnumber); - $this->assertEquals('catdescription', $result->description); + $this->assertEquals('shortname2', $result->shortname); + $this->assertEquals('idnumber2', $result->idnumber); + $this->assertEquals('description2', $result->description); $this->assertEquals(FORMAT_HTML, $result->descriptionformat); $this->assertEquals(2, $result->scaleid); $this->assertEquals($this->scaleconfiguration2, $result->scaleconfiguration); @@ -295,9 +407,7 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_read_competency_frameworks_with_read_permissions() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); + $result = $this->create_competency_framework(1, true); // Switch users to someone with less permissions. $this->setUser($this->user); @@ -308,9 +418,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->creator->id, $result->usermodified); - $this->assertEquals('shortname', $result->shortname); - $this->assertEquals('idnumber', $result->idnumber); - $this->assertEquals('description', $result->description); + $this->assertEquals('shortname1', $result->shortname); + $this->assertEquals('idnumber1', $result->idnumber); + $this->assertEquals('description1', $result->description); $this->assertEquals(FORMAT_HTML, $result->descriptionformat); $this->assertEquals(1, $result->scaleid); $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration); @@ -322,12 +432,8 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { public function test_read_competency_frameworks_with_read_permissions_in_category() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $insystem = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); - $result = external::create_competency_framework('catshortname', 'catidnumber', 'catdescription', FORMAT_HTML, 2, - $this->scaleconfiguration2, true, array('contextid' => context_coursecat::instance($this->category->id)->id)); - $incat = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); + $insystem = $this->create_competency_framework(1, true); + $incat = $this->create_competency_framework(2, false); // Switch users to someone with less permissions. $this->setUser($this->catuser); @@ -338,9 +444,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->creator->id, $result->usermodified); - $this->assertEquals('catshortname', $result->shortname); - $this->assertEquals('catidnumber', $result->idnumber); - $this->assertEquals('catdescription', $result->description); + $this->assertEquals('shortname2', $result->shortname); + $this->assertEquals('idnumber2', $result->idnumber); + $this->assertEquals('description2', $result->description); $this->assertEquals(FORMAT_HTML, $result->descriptionformat); $this->assertEquals(2, $result->scaleid); $this->assertEquals($this->scaleconfiguration2, $result->scaleconfiguration); @@ -359,9 +465,7 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_delete_competency_frameworks_with_manage_permissions() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); + $result = $this->create_competency_framework(1, true); $id = $result->id; $result = external::delete_competency_framework($id); @@ -376,12 +480,8 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { public function test_delete_competency_frameworks_with_manage_permissions_in_category() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $insystem = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); - $result = external::create_competency_framework('catshortname', 'catidnumber', 'catdescription', FORMAT_HTML, 2, - $this->scaleconfiguration2, true, array('contextid' => context_coursecat::instance($this->category->id)->id)); - $incat = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); + $insystem = $this->create_competency_framework(1, true); + $incat = $this->create_competency_framework(2, false); $this->setUser($this->catcreator); $id = $incat->id; @@ -405,9 +505,7 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { public function test_delete_competency_frameworks_with_read_permissions() { $this->setExpectedException('required_capability_exception'); $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); + $result = $this->create_competency_framework(1, true); $id = $result->id; // Switch users to someone with less permissions. @@ -420,13 +518,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_update_competency_frameworks_with_manage_permissions() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); + $result = $this->create_competency_framework(1, true); - $result = external::update_competency_framework($result->id, 'shortname2', - 'idnumber2', 'description2', FORMAT_PLAIN, 2, $this->scaleconfiguration2, false); - $result = external_api::clean_returnvalue(external::update_competency_framework_returns(), $result); + $result = $this->update_competency_framework($result->id, 2, true); $this->assertTrue($result); } @@ -437,28 +531,19 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { public function test_update_competency_frameworks_with_manage_permissions_in_category() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $insystem = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); - $result = external::create_competency_framework('catshortname', 'catidnumber', 'catdescription', FORMAT_HTML, 2, - $this->scaleconfiguration2, true, array('contextid' => context_coursecat::instance($this->category->id)->id)); - $incat = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); + $insystem = $this->create_competency_framework(1, true); + $incat = $this->create_competency_framework(2, false); $this->setUser($this->catcreator); $id = $incat->id; - $result = external::update_competency_framework($id, 'shortname2', 'idnumber2', 'description2', FORMAT_PLAIN, 3, - $this->scaleconfiguration3, false); - $result = external_api::clean_returnvalue(external::update_competency_framework_returns(), $result); + $result = $this->update_competency_framework($incat->id, 3, false); $this->assertTrue($result); try { - $id = $insystem->id; - $result = external::update_competency_framework($id, 'shortname3', 'idnumber3', 'description3', FORMAT_PLAIN, 4, - $this->scaleconfiguration4, false); - $result = external_api::clean_returnvalue(external::update_competency_framework_returns(), $result); - $this->fail('Current user cannot should not be able to update the framework.'); + $result = $this->update_competency_framework($insystem->id, 4, true); + $this->fail('Current user should not be able to update the framework.'); } catch (required_capability_exception $e) { } } @@ -480,16 +565,14 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $lpg->create_user_competency(array('userid' => $this->user->id, 'competencyid' => $c2->get_id())); // Changing the framework where the scale is not used. - $result = external::update_competency_framework($f1->get_id(), 'a', 'a', 'a', FORMAT_PLAIN, 3, $this->scaleconfiguration3, false); - $result = external_api::clean_returnvalue(external::update_competency_framework_returns(), $result); + $result = $this->update_competency_framework($f1->get_id(), 3, true); $f1 = new \tool_lp\competency_framework($f1->get_id()); $this->assertEquals(3, $f1->get_scaleid()); // Changing the framework where the scale is used. try { - $result = external::update_competency_framework($f2->get_id(), 'b', 'b', 'b', FORMAT_PLAIN, 3, $this->scaleconfiguration3, false); - $result = external_api::clean_returnvalue(external::update_competency_framework_returns(), $result); + $result = $this->update_competency_framework($f2->get_id(), 4, true); $this->fail('The scale cannot be changed once used.'); } catch (\tool_lp\invalid_persistent_exception $e) { $this->assertRegexp('/scaleid/', $e->getMessage()); @@ -502,13 +585,10 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { public function test_update_competency_frameworks_with_read_permissions() { $this->setExpectedException('required_capability_exception'); $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); + $result = $this->create_competency_framework(1, true); $this->setUser($this->user); - $result = external::update_competency_framework($result->id, 'shortname2', - 'idnumber2', 'description2', FORMAT_PLAIN, 2, $this->scaleconfiguration2, false); + $result = $this->update_competency_framework($result->id, 2, true); } /** @@ -516,14 +596,10 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_list_and_count_competency_frameworks_with_manage_permissions() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $result = external::create_competency_framework('shortname2', 'idnumber2', 'description', FORMAT_HTML, 2, - $this->scaleconfiguration2, true, array('contextid' => context_system::instance()->id)); - $result = external::create_competency_framework('shortname3', 'idnumber3', 'description', FORMAT_HTML, 3, - $this->scaleconfiguration3, true, array('contextid' => context_system::instance()->id)); - $result = external::create_competency_framework('shortname4', 'idnumber4', 'description', FORMAT_HTML, 4, - $this->scaleconfiguration4, true, array('contextid' => context_coursecat::instance($this->category->id)->id)); + $result = $this->create_competency_framework(1, true); + $result = $this->create_competency_framework(2, true); + $result = $this->create_competency_framework(3, true); + $result = $this->create_competency_framework(4, false); $result = external::count_competency_frameworks(array('contextid' => context_system::instance()->id), 'self'); $result = external_api::clean_returnvalue(external::count_competency_frameworks_returns(), $result); @@ -540,9 +616,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->creator->id, $result->usermodified); - $this->assertEquals('shortname', $result->shortname); - $this->assertEquals('idnumber', $result->idnumber); - $this->assertEquals('description', $result->description); + $this->assertEquals('shortname1', $result->shortname); + $this->assertEquals('idnumber1', $result->idnumber); + $this->assertEquals('description1', $result->description); $this->assertEquals(FORMAT_HTML, $result->descriptionformat); $this->assertEquals(1, $result->scaleid); $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration); @@ -554,14 +630,10 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_list_and_count_competency_frameworks_with_read_permissions() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $result = external::create_competency_framework('shortname2', 'idnumber2', 'description', FORMAT_HTML, 2, - $this->scaleconfiguration2, true, array('contextid' => context_system::instance()->id)); - $result = external::create_competency_framework('shortname3', 'idnumber3', 'description', FORMAT_HTML, 3, - $this->scaleconfiguration3, true, array('contextid' => context_system::instance()->id)); - $result = external::create_competency_framework('shortname4', 'idnumber4', 'description', FORMAT_HTML, 4, - $this->scaleconfiguration4, true, array('contextid' => context_coursecat::instance($this->category->id)->id)); + $result = $this->create_competency_framework(1, true); + $result = $this->create_competency_framework(2, true); + $result = $this->create_competency_framework(3, true); + $result = $this->create_competency_framework(4, false); $this->setUser($this->user); $result = external::count_competency_frameworks(array('contextid' => context_system::instance()->id), 'self'); @@ -578,9 +650,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->creator->id, $result->usermodified); - $this->assertEquals('shortname', $result->shortname); - $this->assertEquals('idnumber', $result->idnumber); - $this->assertEquals('description', $result->description); + $this->assertEquals('shortname1', $result->shortname); + $this->assertEquals('idnumber1', $result->idnumber); + $this->assertEquals('description1', $result->description); $this->assertEquals(FORMAT_HTML, $result->descriptionformat); $this->assertEquals(1, $result->scaleid); $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration); @@ -592,11 +664,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_create_competency_with_read_permissions() { $this->setExpectedException('required_capability_exception'); - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); + $framework = $this->create_competency_framework(1, true); $this->setUser($this->user); - $competency = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0); + $competency = $this->create_competency(1, $framework->id); } /** @@ -604,19 +674,15 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_create_competency_with_manage_permissions() { $this->setUser($this->creator); - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); - - $competency = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0); - $competency = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency); + $framework = $this->create_competency_framework(1, true); + $competency = $this->create_competency(1, $framework->id); $this->assertGreaterThan(0, $competency->timecreated); $this->assertGreaterThan(0, $competency->timemodified); $this->assertEquals($this->creator->id, $competency->usermodified); - $this->assertEquals('shortname', $competency->shortname); - $this->assertEquals('idnumber', $competency->idnumber); - $this->assertEquals('description', $competency->description); + $this->assertEquals('shortname1', $competency->shortname); + $this->assertEquals('idnumber1', $competency->idnumber); + $this->assertEquals('description1', $competency->description); $this->assertEquals(FORMAT_HTML, $competency->descriptionformat); $this->assertEquals(true, $competency->visible); $this->assertEquals(0, $competency->parentid); @@ -630,32 +696,26 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { public function test_create_competency_with_manage_permissions_in_category() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $insystem = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); - $result = external::create_competency_framework('catshortname', 'catidnumber', 'catdescription', FORMAT_HTML, 2, - $this->scaleconfiguration2, true, array('contextid' => context_coursecat::instance($this->category->id)->id)); - $incat = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); + $insystem = $this->create_competency_framework(1, true); + $incat = $this->create_competency_framework(2, false); $this->setUser($this->catcreator); - $competency = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $incat->id, 0); - $competency = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency); + $competency = $this->create_competency(1, $incat->id); $this->assertGreaterThan(0, $competency->timecreated); $this->assertGreaterThan(0, $competency->timemodified); $this->assertEquals($this->catcreator->id, $competency->usermodified); - $this->assertEquals('shortname', $competency->shortname); - $this->assertEquals('idnumber', $competency->idnumber); - $this->assertEquals('description', $competency->description); + $this->assertEquals('shortname1', $competency->shortname); + $this->assertEquals('idnumber1', $competency->idnumber); + $this->assertEquals('description1', $competency->description); $this->assertEquals(FORMAT_HTML, $competency->descriptionformat); $this->assertEquals(true, $competency->visible); $this->assertEquals(0, $competency->parentid); $this->assertEquals($incat->id, $competency->competencyframeworkid); try { - $competency = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $insystem->id, 0); - $competency = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency); + $competency = $this->create_competency(2, $insystem->id); $this->fail('User should not be able to create a competency in system context.'); } catch (required_capability_exception $e) { } @@ -666,12 +726,19 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_create_competency_with_nasty_data() { $this->setUser($this->creator); - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); + $framework = $this->create_competency_framework(1, true); $this->setExpectedException('invalid_parameter_exception'); - $competency = external::create_competency('shortname', 'id;"number', - 'de<>\\..scription', FORMAT_HTML, true, $framework->id, 0); + $competency = array( + 'shortname' => 'shortname', + 'idnumber' => 'id;"number', + 'description' => 'de<>\\..scription', + 'descriptionformat' => FORMAT_HTML, + 'visible' => true, + 'competencyframeworkid' => $framework->id, + 'sortorder' => 0 + ); + $result = external::create_competency($competency); + $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); } /** @@ -679,22 +746,19 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_read_competencies_with_manage_permissions() { $this->setUser($this->creator); - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0); - $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); + $framework = $this->create_competency_framework(1, true); + $competency = $this->create_competency(1, $framework->id); - $id = $result->id; + $id = $competency->id; $result = external::read_competency($id); $result = (object) external_api::clean_returnvalue(external::read_competency_returns(), $result); $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->creator->id, $result->usermodified); - $this->assertEquals('shortname', $result->shortname); - $this->assertEquals('idnumber', $result->idnumber); - $this->assertEquals('description', $result->description); + $this->assertEquals('shortname1', $result->shortname); + $this->assertEquals('idnumber1', $result->idnumber); + $this->assertEquals('description1', $result->description); $this->assertEquals(FORMAT_HTML, $result->descriptionformat); $this->assertEquals(true, $result->visible); $this->assertEquals(0, $result->parentid); @@ -707,17 +771,11 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { public function test_read_competencies_with_manage_permissions_in_category() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $sysframework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $result->id, 0); - $insystem = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); + $sysframework = $this->create_competency_framework(1, true); + $insystem = $this->create_competency(1, $sysframework->id); - $result = external::create_competency_framework('catshortname', 'catidnumber', 'catdescription', FORMAT_HTML, 2, - $this->scaleconfiguration2, true, array('contextid' => context_coursecat::instance($this->category->id)->id)); - $catframework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $result->id, 0); - $incat = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); + $catframework = $this->create_competency_framework(2, false); + $incat = $this->create_competency(2, $catframework->id); $this->setUser($this->catcreator); $id = $incat->id; @@ -727,9 +785,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->creator->id, $result->usermodified); - $this->assertEquals('shortname', $result->shortname); - $this->assertEquals('idnumber', $result->idnumber); - $this->assertEquals('description', $result->description); + $this->assertEquals('shortname2', $result->shortname); + $this->assertEquals('idnumber2', $result->idnumber); + $this->assertEquals('description2', $result->description); $this->assertEquals(FORMAT_HTML, $result->descriptionformat); $this->assertEquals(true, $result->visible); $this->assertEquals(0, $result->parentid); @@ -747,24 +805,21 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_read_competencies_with_read_permissions() { $this->setUser($this->creator); - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0); - $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); + $framework = $this->create_competency_framework(1, true); + $competency = $this->create_competency(1, $framework->id); // Switch users to someone with less permissions. $this->setUser($this->user); - $id = $result->id; + $id = $competency->id; $result = external::read_competency($id); $result = (object) external_api::clean_returnvalue(external::read_competency_returns(), $result); $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->creator->id, $result->usermodified); - $this->assertEquals('shortname', $result->shortname); - $this->assertEquals('idnumber', $result->idnumber); - $this->assertEquals('description', $result->description); + $this->assertEquals('shortname1', $result->shortname); + $this->assertEquals('idnumber1', $result->idnumber); + $this->assertEquals('description1', $result->description); $this->assertEquals(FORMAT_HTML, $result->descriptionformat); $this->assertEquals(true, $result->visible); $this->assertEquals(0, $result->parentid); @@ -776,18 +831,10 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_read_competencies_with_read_permissions_in_category() { $this->setUser($this->creator); - - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $sysframework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $result->id, 0); - $insystem = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); - - $result = external::create_competency_framework('catshortname', 'catidnumber', 'catdescription', FORMAT_HTML, 2, - $this->scaleconfiguration2, true, array('contextid' => context_coursecat::instance($this->category->id)->id)); - $catframework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $result->id, 0); - $incat = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); + $sysframework = $this->create_competency_framework(1, true); + $insystem = $this->create_competency(1, $sysframework->id); + $catframework = $this->create_competency_framework(2, false); + $incat = $this->create_competency(2, $catframework->id); // Switch users to someone with less permissions. $this->setUser($this->catuser); @@ -798,9 +845,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->creator->id, $result->usermodified); - $this->assertEquals('shortname', $result->shortname); - $this->assertEquals('idnumber', $result->idnumber); - $this->assertEquals('description', $result->description); + $this->assertEquals('shortname2', $result->shortname); + $this->assertEquals('idnumber2', $result->idnumber); + $this->assertEquals('description2', $result->description); $this->assertEquals(FORMAT_HTML, $result->descriptionformat); $this->assertEquals(true, $result->visible); $this->assertEquals(0, $result->parentid); @@ -818,11 +865,8 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_delete_competency_with_manage_permissions() { $this->setUser($this->creator); - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0); - $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); + $framework = $this->create_competency_framework(1, true); + $result = $this->create_competency(1, $framework->id); $id = $result->id; $result = external::delete_competency($id); @@ -837,17 +881,10 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { public function test_delete_competency_with_manage_permissions_in_category() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $sysframework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $result->id, 0); - $insystem = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); - - $result = external::create_competency_framework('catshortname', 'catidnumber', 'catdescription', FORMAT_HTML, 2, - $this->scaleconfiguration2, true, array('contextid' => context_coursecat::instance($this->category->id)->id)); - $catframework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $result->id, 0); - $incat = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); + $sysframework = $this->create_competency_framework(1, true); + $insystem = $this->create_competency(1, $sysframework->id); + $catframework = $this->create_competency_framework(2, false); + $incat = $this->create_competency(2, $catframework->id); $this->setUser($this->catcreator); $id = $incat->id; @@ -869,11 +906,8 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { public function test_delete_competency_with_read_permissions() { $this->setExpectedException('required_capability_exception'); $this->setUser($this->creator); - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0); - $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); + $framework = $this->create_competency_framework(1, true); + $result = $this->create_competency(1, $framework->id); $id = $result->id; // Switch users to someone with less permissions. @@ -886,14 +920,10 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_update_competency_with_manage_permissions() { $this->setUser($this->creator); - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0); - $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); + $framework = $this->create_competency_framework(1, true); + $result = $this->create_competency(1, $framework->id); - $result = external::update_competency($result->id, 'shortname2', 'idnumber2', 'description2', FORMAT_HTML, false); - $result = external_api::clean_returnvalue(external::update_competency_returns(), $result); + $result = $this->update_competency($result->id, 2); $this->assertTrue($result); } @@ -904,27 +934,19 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { public function test_update_competency_with_manage_permissions_in_category() { $this->setUser($this->creator); - $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $sysframework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $result->id, 0); - $insystem = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); - - $result = external::create_competency_framework('catshortname', 'catidnumber', 'catdescription', FORMAT_HTML, 2, - $this->scaleconfiguration2, true, array('contextid' => context_coursecat::instance($this->category->id)->id)); - $catframework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $result->id, 0); - $incat = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); + $sysframework = $this->create_competency_framework(1, true); + $insystem = $this->create_competency(1, $sysframework->id); + $catframework = $this->create_competency_framework(2, false); + $incat = $this->create_competency(2, $catframework->id); $this->setUser($this->catcreator); - $result = external::update_competency($incat->id, 'shortname2', 'idnumber2', 'description2', FORMAT_HTML, false); - $result = external_api::clean_returnvalue(external::update_competency_returns(), $result); + $result = $this->update_competency($incat->id, 2); $this->assertTrue($result); try { - external::update_competency($insystem->id, 'shortname2', 'idnumber2', 'description2', FORMAT_HTML, false); + $result = $this->update_competency($insystem->id, 3); $this->fail('User should not be able to update a competency in system context.'); } catch (required_capability_exception $e) { } @@ -936,14 +958,11 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { public function test_update_competency_with_read_permissions() { $this->setExpectedException('required_capability_exception'); $this->setUser($this->creator); - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0); - $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); + $framework = $this->create_competency_framework(1, true); + $result = $this->create_competency(1, $framework->id); $this->setUser($this->user); - $result = external::update_competency($result->id, 'shortname2', 'idnumber2', 'description2', FORMAT_HTML, false); + $result = $this->update_competency($result->id, 2); } /** @@ -979,12 +998,10 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_list_and_count_competencies_with_manage_permissions() { $this->setUser($this->creator); - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0); - $result = external::create_competency('shortname2', 'idnumber2', 'description2', FORMAT_HTML, true, $framework->id, 0); - $result = external::create_competency('shortname3', 'idnumber3', 'description3', FORMAT_HTML, true, $framework->id, 0); + $framework = $this->create_competency_framework(1, true); + $result = $this->create_competency(1, $framework->id); + $result = $this->create_competency(2, $framework->id); + $result = $this->create_competency(3, $framework->id); $result = external::count_competencies(array()); $result = external_api::clean_returnvalue(external::count_competencies_returns(), $result); @@ -1000,9 +1017,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->creator->id, $result->usermodified); - $this->assertEquals('shortname', $result->shortname); - $this->assertEquals('idnumber', $result->idnumber); - $this->assertEquals('description', $result->description); + $this->assertEquals('shortname1', $result->shortname); + $this->assertEquals('idnumber1', $result->idnumber); + $this->assertEquals('description1', $result->description); $this->assertEquals(true, $result->visible); } @@ -1011,12 +1028,10 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_list_and_count_competencies_with_read_permissions() { $this->setUser($this->creator); - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0); - $result = external::create_competency('shortname2', 'idnumber2', 'description2', FORMAT_HTML, true, $framework->id, 0); - $result = external::create_competency('shortname3', 'idnumber3', 'description3', FORMAT_HTML, true, $framework->id, 0); + $framework = $this->create_competency_framework(1, true); + $result = $this->create_competency(1, $framework->id); + $result = $this->create_competency(2, $framework->id); + $result = $this->create_competency(3, $framework->id); $this->setUser($this->user); @@ -1034,9 +1049,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->creator->id, $result->usermodified); - $this->assertEquals('shortname', $result->shortname); - $this->assertEquals('idnumber', $result->idnumber); - $this->assertEquals('description', $result->description); + $this->assertEquals('shortname1', $result->shortname); + $this->assertEquals('idnumber1', $result->idnumber); + $this->assertEquals('description1', $result->description); $this->assertEquals(true, $result->visible); } @@ -1045,12 +1060,10 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { */ public function test_search_competencies_with_read_permissions() { $this->setUser($this->creator); - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); - $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0); - $result = external::create_competency('shortname2', 'idnumber2', 'description2', FORMAT_HTML, true, $framework->id, 0); - $result = external::create_competency('shortname3', 'idnumber3', 'description3', FORMAT_HTML, true, $framework->id, 0); + $framework = $this->create_competency_framework(1, true); + $result = $this->create_competency(1, $framework->id); + $result = $this->create_competency(2, $framework->id); + $result = $this->create_competency(3, $framework->id); $this->setUser($this->user); @@ -1063,9 +1076,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->assertGreaterThan(0, $result->timecreated); $this->assertGreaterThan(0, $result->timemodified); $this->assertEquals($this->creator->id, $result->usermodified); - $this->assertEquals('shortname', $result->shortname); - $this->assertEquals('idnumber', $result->idnumber); - $this->assertEquals('description', $result->description); + $this->assertEquals('shortname1', $result->shortname); + $this->assertEquals('idnumber1', $result->idnumber); + $this->assertEquals('description1', $result->description); $this->assertEquals(true, $result->visible); } @@ -1076,14 +1089,12 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $syscontext = context_system::instance(); $this->setUser($this->creator); - $plan0 = external::create_plan('Complete plan', 'A description', - FORMAT_HTML, $this->creator->id, 0, plan::STATUS_COMPLETE, 0); + $plan0 = $this->create_plan(1, $this->creator->id, 0, plan::STATUS_COMPLETE, 0); $this->setUser($this->user); try { - $plan1 = external::create_plan('Draft plan (they can not with the default capabilities)', - 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_DRAFT, 0); + $plan1 = $this->create_plan(2, $this->user->id, 0, plan::STATUS_DRAFT, 0); $this->fail('Exception expected due to not permissions to create draft plans'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); @@ -1094,18 +1105,16 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->setUser($this->user); - $plan2 = external::create_plan('Draft plan', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_DRAFT, 0); + $plan2 = $this->create_plan(3, $this->user->id, 0, plan::STATUS_DRAFT, 0); try { - $plan3 = external::create_plan('Active plan (they can not)', 'A description', - FORMAT_HTML, $this->user->id, 0, plan::STATUS_ACTIVE, 0); + $plan3 = $this->create_plan(4, $this->user->id, 0, plan::STATUS_ACTIVE, 0); $this->fail('Exception expected due to not permissions to create active plans'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } try { - $plan3 = external::update_plan($plan2['id'], 'Updated active plan', 'A description', - FORMAT_HTML, $this->user->id, 0, plan::STATUS_COMPLETE, 0); + $plan3 = $this->update_plan($plan2->id, 4, $this->user->id, 0, plan::STATUS_COMPLETE, 0); $this->fail('Exception expected due to not permissions to update plans to complete status'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); @@ -1114,19 +1123,17 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { assign_capability('tool/lp:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id); accesslib_clear_all_caches_for_unit_testing(); - $plan3 = external::create_plan('Active plan', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_ACTIVE, 0); - $plan4 = external::create_plan('Complete plan', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_COMPLETE, 0); + $plan3 = $this->create_plan(4, $this->user->id, 0, plan::STATUS_ACTIVE, 0); + $plan4 = $this->create_plan(5, $this->user->id, 0, plan::STATUS_COMPLETE, 0); try { - $plan4 = external::create_plan('Plan for another user', 'A description', - FORMAT_HTML, $this->creator->id, 0, plan::STATUS_COMPLETE, 0); + $plan4 = $this->create_plan(6, $this->creator->id, 0, plan::STATUS_COMPLETE, 0); $this->fail('Exception expected due to not permissions to manage other users plans'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } try { - $plan0 = external::update_plan($plan0['id'], 'Can not update other users plans', - 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_ACTIVE, 0); + $plan0 = $this->update_plan($plan0->id, 1, $this->user->id, 0, plan::STATUS_ACTIVE, 0); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } @@ -1136,8 +1143,8 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { accesslib_clear_all_caches_for_unit_testing(); try { - $plan1 = external::update_plan($plan2['id'], 'Can not be updated even if they created it', - 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_COMPLETE, 0); + // Cannot be updated even if they created it. + $plan1 = $this->update_plan($plan0->id, 1, $this->user->id, 0, plan::STATUS_COMPLETE, 0); $this->fail('Exception expected due to not permissions to create draft plan'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); @@ -1152,39 +1159,36 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $syscontext = context_system::instance(); - $plan1 = external::create_plan('Plan draft by creator', 'A description', - FORMAT_HTML, $this->user->id, 0, plan::STATUS_DRAFT, 0); - $plan2 = external::create_plan('Plan active by creator', 'A description', - FORMAT_HTML, $this->user->id, 0, plan::STATUS_ACTIVE, 0); - $plan3 = external::create_plan('Plan complete by creator', 'A description', - FORMAT_HTML, $this->user->id, 0, plan::STATUS_COMPLETE, 0); + $plan1 = $this->create_plan(1, $this->user->id, 0, plan::STATUS_DRAFT, 0); + $plan2 = $this->create_plan(2, $this->user->id, 0, plan::STATUS_ACTIVE, 0); + $plan3 = $this->create_plan(3, $this->user->id, 0, plan::STATUS_COMPLETE, 0); - $this->assertEquals((Array)$plan1, external::read_plan($plan1['id'])); - $this->assertEquals((Array)$plan2, external::read_plan($plan2['id'])); - $this->assertEquals((Array)$plan3, external::read_plan($plan3['id'])); + $this->assertEquals((Array)$plan1, external::read_plan($plan1->id)); + $this->assertEquals((Array)$plan2, external::read_plan($plan2->id)); + $this->assertEquals((Array)$plan3, external::read_plan($plan3->id)); $this->setUser($this->user); // The normal user can not edit these plans. - $plan1['usercanupdate'] = false; - $plan2['usercanupdate'] = false; - $plan3['usercanupdate'] = false; + $plan1->usercanupdate = false; + $plan2->usercanupdate = false; + $plan3->usercanupdate = false; // Prevent the user from seeing their own non-draft plans. assign_capability('tool/lp:planviewown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true); assign_capability('tool/lp:planviewowndraft', CAP_ALLOW, $this->userrole, $syscontext->id, true); accesslib_clear_all_caches_for_unit_testing(); - $this->assertEquals((array)$plan1, external::read_plan($plan1['id'])); + $this->assertEquals((array)$plan1, external::read_plan($plan1->id)); try { - external::read_plan($plan2['id']); + external::read_plan($plan2->id); $this->fail('Exception expected due to not permissions to read plan'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } try { - external::read_plan($plan3['id']); + external::read_plan($plan3->id); $this->fail('Exception expected due to not permissions to read plan'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); @@ -1195,9 +1199,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { assign_capability('tool/lp:planmanageowndraft', CAP_PROHIBIT, $this->userrole, $syscontext->id, true); accesslib_clear_all_caches_for_unit_testing(); - $this->assertEquals((array)$plan1, external::read_plan($plan1['id'])); - $this->assertEquals((array)$plan2, external::read_plan($plan2['id'])); - $this->assertEquals((array)$plan3, external::read_plan($plan3['id'])); + $this->assertEquals((array)$plan1, external::read_plan($plan1->id)); + $this->assertEquals((array)$plan2, external::read_plan($plan2->id)); + $this->assertEquals((array)$plan3, external::read_plan($plan3->id)); // Allow use to manage their own draft plan. assign_capability('tool/lp:planviewown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true); @@ -1205,16 +1209,16 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $this->userrole, $syscontext->id, true); accesslib_clear_all_caches_for_unit_testing(); - $plan1['usercanupdate'] = true; - $this->assertEquals((array)$plan1, external::read_plan($plan1['id'])); + $plan1->usercanupdate = true; + $this->assertEquals((array)$plan1, external::read_plan($plan1->id)); try { - external::read_plan($plan2['id']); + external::read_plan($plan2->id); $this->fail('Exception expected due to not permissions to read plan'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } try { - external::read_plan($plan3['id']); + external::read_plan($plan3->id); $this->fail('Exception expected due to not permissions to read plan'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); @@ -1227,12 +1231,12 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { assign_capability('tool/lp:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id, true); accesslib_clear_all_caches_for_unit_testing(); - $plan1['usercanupdate'] = false; - $plan2['usercanupdate'] = true; - $plan3['usercanupdate'] = true; - $this->assertEquals((array)$plan1, external::read_plan($plan1['id'])); - $this->assertEquals((array)$plan2, external::read_plan($plan2['id'])); - $this->assertEquals((array)$plan3, external::read_plan($plan3['id'])); + $plan1->usercanupdate = false; + $plan2->usercanupdate = true; + $plan3->usercanupdate = true; + $this->assertEquals((array)$plan1, external::read_plan($plan1->id)); + $this->assertEquals((array)$plan2, external::read_plan($plan2->id)); + $this->assertEquals((array)$plan3, external::read_plan($plan3->id)); } public function test_delete_plans() { @@ -1240,17 +1244,17 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $syscontext = context_system::instance(); - $plan1 = external::create_plan('1', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_COMPLETE, 0); - $plan2 = external::create_plan('2', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_COMPLETE, 0); - $plan3 = external::create_plan('3', 'A description', FORMAT_HTML, $this->creator->id, 0, plan::STATUS_COMPLETE, 0); + $plan1 = $this->create_plan(1, $this->user->id, 0, plan::STATUS_COMPLETE, 0); + $plan2 = $this->create_plan(2, $this->user->id, 0, plan::STATUS_COMPLETE, 0); + $plan3 = $this->create_plan(3, $this->creator->id, 0, plan::STATUS_COMPLETE, 0); - $this->assertTrue(external::delete_plan($plan1['id'])); + $this->assertTrue(external::delete_plan($plan1->id)); unassign_capability('tool/lp:planmanage', $this->creatorrole, $syscontext->id); accesslib_clear_all_caches_for_unit_testing(); try { - external::delete_plan($plan2['id']); + external::delete_plan($plan2->id); $this->fail('Exception expected due to not permissions to manage plans'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); @@ -1260,7 +1264,7 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { // Can not delete plans created by other users. try { - external::delete_plan($plan2['id']); + external::delete_plan($plan2->id); $this->fail('Exception expected due to not permissions to manage plans'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); @@ -1269,18 +1273,18 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { assign_capability('tool/lp:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id); accesslib_clear_all_caches_for_unit_testing(); - $this->assertTrue(external::delete_plan($plan2['id'])); + $this->assertTrue(external::delete_plan($plan2->id)); // Can not delete plans created for other users. try { - external::delete_plan($plan3['id']); + external::delete_plan($plan3->id); $this->fail('Exception expected due to not permissions to manage plans'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $e->errorcode); } - $plan4 = external::create_plan('4', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_COMPLETE, 0); - $this->assertTrue(external::delete_plan($plan4['id'])); + $plan4 = $this->create_plan(4, $this->user->id, 0, plan::STATUS_COMPLETE, 0); + $this->assertTrue(external::delete_plan($plan4->id)); } public function test_list_plan_competencies() { @@ -1365,19 +1369,13 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->setUser($this->creator); $syscontext = context_system::instance(); - $onehour = time() + 60*60; // Create a template. - $template = external::create_template('shortname', $onehour, 'description', FORMAT_HTML, true, - array('contextid' => $syscontext->id)); - $template = (object) external_api::clean_returnvalue(external::create_template_returns(), $template); + $template = $this->create_template(1, true); // Create a competency. - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); - $competency = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0); - $competency = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency); + $framework = $this->create_competency_framework(1, true); + $competency = $this->create_competency(1, $framework->id); // Add the competency. external::add_competency_to_template($template->id, $competency->id); @@ -1399,16 +1397,12 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { } public function test_remove_competency_from_template() { + $syscontext = context_system::instance(); $this->setUser($this->creator); $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp'); - $syscontext = context_system::instance(); - $onehour = time() + 60*60; - // Create a template. - $template = external::create_template('shortname', $onehour, 'description', FORMAT_HTML, true, - array('contextid' => $syscontext->id)); - $template = (object) external_api::clean_returnvalue(external::create_template_returns(), $template); + $template = $this->create_template(1, true); // Create a competency. $framework = $lpg->create_framework(); @@ -1449,22 +1443,15 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $onehour = time() + 60*60; // Create a template. - $template = external::create_template('shortname', $onehour, 'description', FORMAT_HTML, true, - array('contextid' => $syscontext->id)); - $template = (object) external_api::clean_returnvalue(external::create_template_returns(), $template); + $template = $this->create_template(1, true); // Create a competency framework. - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); + $framework = $this->create_competency_framework(1, true); // Create multiple competencies. - $competency1 = external::create_competency('shortname1', 'idnumber1', 'description', FORMAT_HTML, true, $framework->id, 0); - $competency1 = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency1); - $competency2 = external::create_competency('shortname2', 'idnumber2', 'description', FORMAT_HTML, true, $framework->id, 0); - $competency2 = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency2); - $competency3 = external::create_competency('shortname3', 'idnumber3', 'description', FORMAT_HTML, true, $framework->id, 0); - $competency3 = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency3); + $competency1 = $this->create_competency(1, $framework->id); + $competency2 = $this->create_competency(2, $framework->id); + $competency3 = $this->create_competency(3, $framework->id); // Add the competencies. external::add_competency_to_template($template->id, $competency1->id); @@ -1512,22 +1499,15 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $onehour = time() + 60*60; // Create a template. - $template = external::create_template('shortname', $onehour, 'description', FORMAT_HTML, true, - array('contextid' => $syscontext->id)); - $template = (object) external_api::clean_returnvalue(external::create_template_returns(), $template); + $template = $this->create_template(1, true); // Create a competency framework. - $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1, - $this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id)); - $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework); + $framework = $this->create_competency_framework(1, true); // Create multiple competencies. - $competency1 = external::create_competency('shortname1', 'idnumber1', 'description', FORMAT_HTML, true, $framework->id, 0); - $competency1 = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency1); - $competency2 = external::create_competency('shortname2', 'idnumber2', 'description', FORMAT_HTML, true, $framework->id, 0); - $competency2 = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency2); - $competency3 = external::create_competency('shortname3', 'idnumber3', 'description', FORMAT_HTML, true, $framework->id, 0); - $competency3 = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency3); + $competency1 = $this->create_competency(1, $framework->id); + $competency2 = $this->create_competency(2, $framework->id); + $competency3 = $this->create_competency(3, $framework->id); // Add the competencies. external::add_competency_to_template($template->id, $competency1->id); @@ -1595,8 +1575,7 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { // A user without permission. $this->setUser($this->user); try { - $result = external::create_template('shortname', 0, 'description', FORMAT_HTML, true, - array('contextid' => $syscontextid)); + $result = $this->create_template(1, true); $this->fail('Invalid permissions'); } catch (required_capability_exception $e) { } @@ -1604,43 +1583,35 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { // A user without permission in a category. $this->setUser($this->catuser); try { - $result = external::create_template('shortname', 0, 'description', FORMAT_HTML, true, - array('contextid' => $catcontextid)); + $result = $this->create_template(1, false); $this->fail('Invalid permissions'); } catch (required_capability_exception $e) { } // A user with permissions in the system. $this->setUser($this->creator); - $result = external::create_template('shortname', 0, 'description', FORMAT_HTML, true, - array('contextid' => $syscontextid)); - $result = external_api::clean_returnvalue(external::create_template_returns(), $result); - $this->assertEquals('shortname', $result['shortname']); - $this->assertEquals($syscontextid, $result['contextid']); - $this->assertNotEmpty($result['id']); + $result = $this->create_template(1, true); + $this->assertEquals('shortname1', $result->shortname); + $this->assertEquals($syscontextid, $result->contextid); + $this->assertNotEmpty($result->id); - $result = external::create_template('catshortname', 0, 'description', FORMAT_HTML, true, - array('contextid' => $catcontextid)); - $result = external_api::clean_returnvalue(external::create_template_returns(), $result); - $this->assertEquals('catshortname', $result['shortname']); - $this->assertEquals($catcontextid, $result['contextid']); - $this->assertNotEmpty($result['id']); + $result = $this->create_template(2, false); + $this->assertEquals('shortname2', $result->shortname); + $this->assertEquals($catcontextid, $result->contextid); + $this->assertNotEmpty($result->id); // A user with permissions in the category. $this->setUser($this->catcreator); try { - $result = external::create_template('sysshortname', 0, 'description', FORMAT_HTML, true, - array('contextid' => $syscontextid)); + $result = $this->create_template(3, true); $this->fail('Invalid permissions'); } catch (required_capability_exception $e) { } - $result = external::create_template('catshortname2', 0, 'description', FORMAT_HTML, true, - array('contextid' => $catcontextid)); - $result = external_api::clean_returnvalue(external::create_template_returns(), $result); - $this->assertEquals('catshortname2', $result['shortname']); - $this->assertEquals($catcontextid, $result['contextid']); - $this->assertNotEmpty($result['id']); + $result = $this->create_template(3, false); + $this->assertEquals('shortname3', $result->shortname); + $this->assertEquals($catcontextid, $result->contextid); + $this->assertNotEmpty($result->id); } /** @@ -1657,10 +1628,8 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { // Creating two templates. $this->setUser($this->creator); - $systemplate = external::create_template('sys', $duedate, 'description', FORMAT_HTML, true, - array('contextid' => $syscontextid)); - $cattemplate = external::create_template('cat', $duedate, 'description', FORMAT_HTML, true, - array('contextid' => $catcontextid)); + $systemplate = $this->create_template(1, true); + $cattemplate = $this->create_template(2, false); // User without permissions to read in system. assign_capability('tool/lp:templateread', CAP_PROHIBIT, $this->userrole, $syscontextid, true); @@ -1693,12 +1662,12 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $result = external::read_template($cattemplate->id); $result = external_api::clean_returnvalue(external::read_template_returns(), $result); $this->assertEquals($cattemplate->id, $result['id']); - $this->assertEquals('cat', $result['shortname']); - $this->assertEquals('description', $result['description']); + $this->assertEquals('shortname2', $result['shortname']); + $this->assertEquals('description2', $result['description']); $this->assertEquals(FORMAT_HTML, $result['descriptionformat']); - $this->assertEquals(true, $result['visible']); - $this->assertEquals($duedate, $result['duedate']); - $this->assertEquals(userdate($duedate), $result['duedateformatted']); + $this->assertEquals(1, $result['visible']); + $this->assertEquals(0, $result['duedate']); + $this->assertEquals(userdate(0), $result['duedateformatted']); // User with permissions to read in the system. assign_capability('tool/lp:templateread', CAP_ALLOW, $this->userrole, $syscontextid, true); @@ -1707,22 +1676,22 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $result = external::read_template($systemplate->id); $result = external_api::clean_returnvalue(external::read_template_returns(), $result); $this->assertEquals($systemplate->id, $result['id']); - $this->assertEquals('sys', $result['shortname']); - $this->assertEquals('description', $result['description']); + $this->assertEquals('shortname1', $result['shortname']); + $this->assertEquals('description1', $result['description']); $this->assertEquals(FORMAT_HTML, $result['descriptionformat']); $this->assertEquals(true, $result['visible']); - $this->assertEquals($duedate, $result['duedate']); - $this->assertEquals(userdate($duedate), $result['duedateformatted']); + $this->assertEquals(0, $result['duedate']); + $this->assertEquals(userdate(0), $result['duedateformatted']); $result = external::read_template($cattemplate->id); $result = external_api::clean_returnvalue(external::read_template_returns(), $result); $this->assertEquals($cattemplate->id, $result['id']); - $this->assertEquals('cat', $result['shortname']); - $this->assertEquals('description', $result['description']); + $this->assertEquals('shortname2', $result['shortname']); + $this->assertEquals('description2', $result['description']); $this->assertEquals(FORMAT_HTML, $result['descriptionformat']); $this->assertEquals(true, $result['visible']); - $this->assertEquals($duedate, $result['duedate']); - $this->assertEquals(userdate($duedate), $result['duedateformatted']); + $this->assertEquals(0, $result['duedate']); + $this->assertEquals(userdate(0), $result['duedateformatted']); } /** @@ -1739,21 +1708,19 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { // Creating two templates. $this->setUser($this->creator); - $systemplate = external::create_template('sys', $duedate, 'description', FORMAT_HTML, true, - array('contextid' => $syscontextid)); - $cattemplate = external::create_template('cat', $duedate, 'description', FORMAT_HTML, true, - array('contextid' => $catcontextid)); + $systemplate = $this->create_template(1, true); + $cattemplate = $this->create_template(2, false); // Trying to update in a without permissions. $this->setUser($this->user); try { - external::update_template($systemplate->id, 'a', 1234, 'c', FORMAT_MARKDOWN, false); + $this->update_template($systemplate->id, 3); $this->fail('Invalid permissions'); } catch (required_capability_exception $e) { } try { - external::update_template($cattemplate->id, 'a', 1234, 'c', FORMAT_MARKDOWN, false); + $this->update_template($cattemplate->id, 3); $this->fail('Invalid permissions'); } catch (required_capability_exception $e) { } @@ -1761,55 +1728,48 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { // User with permissions to update in category. $this->setUser($this->catcreator); try { - external::update_template($systemplate->id, 'a', 1234, 'c', FORMAT_MARKDOWN, false); + $this->update_template($systemplate->id, 3); $this->fail('Invalid permissions'); } catch (required_capability_exception $e) { } - // Set a due date for the next 2 years. - $date->modify('+1 year'); - $duedateupdated = $date->getTimestamp(); - - $result = external::update_template($cattemplate->id, 'a', $duedateupdated, 'c', FORMAT_MARKDOWN, false); - $result = external_api::clean_returnvalue(external::update_template_returns(), $result); + $result = $this->update_template($cattemplate->id, 3); $this->assertTrue($result); $result = external::read_template($cattemplate->id); $result = external_api::clean_returnvalue(external::read_template_returns(), $result); $this->assertEquals($cattemplate->id, $result['id']); - $this->assertEquals('a', $result['shortname']); - $this->assertEquals("

c

\n", $result['description']); + $this->assertEquals('shortname3', $result['shortname']); + $this->assertEquals("description3", $result['description']); $this->assertEquals(FORMAT_HTML, $result['descriptionformat']); - $this->assertEquals(0, $result['visible']); - $this->assertEquals($duedateupdated, $result['duedate']); - $this->assertEquals(userdate($duedateupdated), $result['duedateformatted']); + $this->assertEquals(true, $result['visible']); + $this->assertEquals(0, $result['duedate']); + $this->assertEquals(userdate(0), $result['duedateformatted']); // User with permissions to update in the system. $this->setUser($this->creator); - $result = external::update_template($systemplate->id, 'x1', $duedateupdated, 'z1', FORMAT_PLAIN, false); - $result = external_api::clean_returnvalue(external::update_template_returns(), $result); + $result = $this->update_template($systemplate->id, 4); $this->assertTrue($result); $result = external::read_template($systemplate->id); $result = external_api::clean_returnvalue(external::read_template_returns(), $result); $this->assertEquals($systemplate->id, $result['id']); - $this->assertEquals('x1', $result['shortname']); - $this->assertEquals('z1', $result['description']); + $this->assertEquals('shortname4', $result['shortname']); + $this->assertEquals('description4', $result['description']); $this->assertEquals(FORMAT_HTML, $result['descriptionformat']); - $this->assertEquals(0, $result['visible']); - $this->assertEquals($duedateupdated, $result['duedate']); - $this->assertEquals(userdate($duedateupdated), $result['duedateformatted']); + $this->assertEquals(true, $result['visible']); + $this->assertEquals(0, $result['duedate']); + $this->assertEquals(userdate(0), $result['duedateformatted']); - $result = external::update_template($cattemplate->id, 'x2', $duedateupdated, 'z2', FORMAT_PLAIN, true); - $result = external_api::clean_returnvalue(external::update_template_returns(), $result); + $result = $this->update_template($cattemplate->id, 5); $this->assertTrue($result); $result = external::read_template($cattemplate->id); $result = external_api::clean_returnvalue(external::read_template_returns(), $result); $this->assertEquals($cattemplate->id, $result['id']); - $this->assertEquals('x2', $result['shortname']); - $this->assertEquals('z2', $result['description']); + $this->assertEquals('shortname5', $result['shortname']); + $this->assertEquals('description5', $result['description']); $this->assertEquals(FORMAT_HTML, $result['descriptionformat']); $this->assertEquals(1, $result['visible']); - $this->assertEquals($duedateupdated, $result['duedate']); - $this->assertEquals(userdate($duedateupdated), $result['duedateformatted']); + $this->assertEquals(0, $result['duedate']); + $this->assertEquals(userdate(0), $result['duedateformatted']); } /** @@ -1822,12 +1782,9 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { // Creating a few templates. $this->setUser($this->creator); - $sys1 = external::create_template('sys1', 0, 'description', FORMAT_HTML, true, - array('contextid' => $syscontextid)); - $cat1 = external::create_template('cat1', 0, 'description', FORMAT_HTML, true, - array('contextid' => $catcontextid)); - $cat2 = external::create_template('cat2', 0, 'description', FORMAT_HTML, true, - array('contextid' => $catcontextid)); + $sys1 = $this->create_template(1, true); + $cat1 = $this->create_template(2, false); + $cat2 = $this->create_template(3, false); $this->assertTrue($DB->record_exists('tool_lp_template', array('id' => $sys1->id))); $this->assertTrue($DB->record_exists('tool_lp_template', array('id' => $cat1->id))); $this->assertTrue($DB->record_exists('tool_lp_template', array('id' => $cat2->id))); @@ -1879,14 +1836,10 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { // Creating a few templates. $this->setUser($this->creator); - $sys1 = external::create_template('sys1', 0, 'description', FORMAT_HTML, true, - array('contextid' => $syscontextid)); - $sys2 = external::create_template('sys2', 0, 'description', FORMAT_HTML, true, - array('contextid' => $syscontextid)); - $cat1 = external::create_template('cat1', 0, 'description', FORMAT_HTML, true, - array('contextid' => $catcontextid)); - $cat2 = external::create_template('cat2', 0, 'description', FORMAT_HTML, true, - array('contextid' => $catcontextid)); + $sys1 = $this->create_template(1, true); + $sys2 = $this->create_template(2, true); + $cat1 = $this->create_template(3, false); + $cat2 = $this->create_template(4, false); // User without permission. $this->setUser($this->user); @@ -1925,16 +1878,11 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { // Creating a few templates. $this->setUser($this->creator); - $sys1 = external::create_template('sys1', 0, 'description', FORMAT_HTML, true, - array('contextid' => $syscontextid)); - $sys2 = external::create_template('sys2', 0, 'description', FORMAT_HTML, true, - array('contextid' => $syscontextid)); - $cat1 = external::create_template('cat1', 0, 'description', FORMAT_HTML, true, - array('contextid' => $catcontextid)); - $cat2 = external::create_template('cat2', 0, 'description', FORMAT_HTML, true, - array('contextid' => $catcontextid)); - $cat3 = external::create_template('cat3', 0, 'description', FORMAT_HTML, true, - array('contextid' => $catcontextid)); + $sys1 = $this->create_template(1, true); + $sys2 = $this->create_template(2, true); + $cat1 = $this->create_template(3, false); + $cat2 = $this->create_template(4, false); + $cat3 = $this->create_template(5, false); // User without permission. $this->setUser($this->user); @@ -2115,19 +2063,7 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $result = external_api::clean_returnvalue(external::search_competencies_returns(), $result); $this->assertCount(5, $result); - $this->assertCount(2, $result[0]['relatedcompetencies']); - $this->assertCount(2, $result[1]['relatedcompetencies']); - $this->assertCount(1, $result[2]['relatedcompetencies']); - $this->assertCount(1, $result[3]['relatedcompetencies']); - $this->assertTrue(empty($result[4]['relatedcompetencies'])); - // Checking that all competencies are returned. - $this->assertEquals($c2->get_id(), $result[0]['relatedcompetencies'][0]['id']); - $this->assertEquals($c3->get_id(), $result[0]['relatedcompetencies'][1]['id']); - $this->assertEquals($c1->get_id(), $result[1]['relatedcompetencies'][0]['id']); - $this->assertEquals($c4->get_id(), $result[1]['relatedcompetencies'][1]['id']); - $this->assertEquals($c1->get_id(), $result[2]['relatedcompetencies'][0]['id']); - $this->assertEquals($c2->get_id(), $result[3]['relatedcompetencies'][0]['id']); } /**