From 6880579736d5f192b0136e8762ae778906b99b26 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Fri, 27 Nov 2015 14:56:35 +0800 Subject: [PATCH] MDL-52198 cbe: Move user summary to an exporter --- .../lp/amd/build/form-user-selector.min.js | 2 +- admin/tool/lp/amd/src/form-user-selector.js | 2 +- admin/tool/lp/classes/external.php | 41 +++----- .../external/user_summary_exporter.php | 99 +++++++++++++++++++ admin/tool/lp/tests/externallib_test.php | 22 ++--- 5 files changed, 124 insertions(+), 42 deletions(-) create mode 100644 admin/tool/lp/classes/external/user_summary_exporter.php diff --git a/admin/tool/lp/amd/build/form-user-selector.min.js b/admin/tool/lp/amd/build/form-user-selector.min.js index 8c158e8f3b5..d228806883a 100644 --- a/admin/tool/lp/amd/build/form-user-selector.min.js +++ b/admin/tool/lp/amd/build/form-user-selector.min.js @@ -1 +1 @@ -define(["jquery","core/ajax","core/templates"],function(a,b,c){return{processResults:function(b,c){var d=[];return a.each(c,function(a,b){d.push({value:b.id,label:b._label})}),d},transport:function(d,e,f,g){var h,i=a(d).data("capability");"undefined"==typeof i&&(i=""),h=b.call([{methodname:"tool_lp_search_users",args:{query:e,capability:i}}]),h[0].then(function(b){var d=[],e=0;return a.each(b.users,function(b,e){var f=e,g=[];a.each(["idnumber","email","phone1","phone2","department","institution"],function(a,b){"undefined"!=typeof e[b]&&(f.hasidentity=!0,g.push(e[b]))}),f.identity=g.join(", "),d.push(c.render("tool_lp/form-user-selector-suggestion",f))}),a.when.apply(a.when,d).then(function(){var c=arguments;a.each(b.users,function(a,b){b._label=c[e],e++}),f(b.users)})},g)}}}); \ No newline at end of file +define(["jquery","core/ajax","core/templates"],function(a,b,c){return{processResults:function(b,c){var d=[];return a.each(c,function(a,b){d.push({value:b.id,label:b._label})}),d},transport:function(d,e,f,g){var h,i=a(d).data("capability");"undefined"==typeof i&&(i=""),h=b.call([{methodname:"tool_lp_search_users",args:{query:e,capability:i}}]),h[0].then(function(b){var d=[],e=0;return a.each(b.users,function(b,e){var f=e,g=[];a.each(["idnumber","email","phone1","phone2","department","institution"],function(a,b){"undefined"!=typeof e[b]&&""!==e[b]&&(f.hasidentity=!0,g.push(e[b]))}),f.identity=g.join(", "),d.push(c.render("tool_lp/form-user-selector-suggestion",f))}),a.when.apply(a.when,d).then(function(){var c=arguments;a.each(b.users,function(a,b){b._label=c[e],e++}),f(b.users)})},g)}}}); \ No newline at end of file diff --git a/admin/tool/lp/amd/src/form-user-selector.js b/admin/tool/lp/amd/src/form-user-selector.js index 0122fe936cb..8713d069d6a 100644 --- a/admin/tool/lp/amd/src/form-user-selector.js +++ b/admin/tool/lp/amd/src/form-user-selector.js @@ -62,7 +62,7 @@ define(['jquery', 'core/ajax', 'core/templates'], function($, Ajax, Templates) { var ctx = user, identity = []; $.each(['idnumber', 'email', 'phone1', 'phone2', 'department', 'institution'], function(i, k) { - if (typeof user[k] !== 'undefined') { + if (typeof user[k] !== 'undefined' && user[k] !== '') { ctx.hasidentity = true; identity.push(user[k]); } diff --git a/admin/tool/lp/classes/external.php b/admin/tool/lp/classes/external.php index 25b8c01df7a..36a811550d3 100644 --- a/admin/tool/lp/classes/external.php +++ b/admin/tool/lp/classes/external.php @@ -41,6 +41,7 @@ use invalid_parameter_exception; use grade_scale; use tool_lp\external\competency_framework_exporter; use tool_lp\external\competency_summary_exporter; +use tool_lp\external\user_summary_exporter; use tool_lp\external\user_competency_exporter; use tool_lp\external\user_competency_plan_exporter; use tool_lp\external\competency_exporter; @@ -3458,6 +3459,7 @@ class external extends external_api { $context = context_system::instance(); self::validate_context($context); + $output = $PAGE->get_renderer('tool_lp'); list($filtercapsql, $filtercapparams) = self::filter_users_with_capability_on_user_context_sql($cap, $USER->id, @@ -3482,22 +3484,16 @@ class external extends external_api { $users = array(); foreach ($result as $key => $user) { - $newuser = array( - 'id' => $user->id, - 'fullname' => fullname($user) - ); - - // Add user picture. - $userpicture = new \user_picture($user); - $userpicture->size = 1; // Size f1. - $newuser['profileimageurl'] = $userpicture->get_url($PAGE)->out(false); - $userpicture->size = 0; // Size f2. - $newuser['profileimageurlsmall'] = $userpicture->get_url($PAGE)->out(false); - - // Add identity fields. - foreach ($extrasearchfields as $field) { - $newuser[$field] = $user->$field; + // Make sure all required fields are set. + foreach (user_summary_exporter::define_properties() as $propertykey => $definition) { + if (empty($user->$propertykey) || !in_array($propertykey, $extrasearchfields)) { + if ($propertykey != 'id') { + $user->$propertykey = ''; + } + } } + $exporter = new user_summary_exporter($user); + $newuser = $exporter->export($output); $users[$key] = $newuser; } @@ -3518,20 +3514,7 @@ class external extends external_api { global $CFG; require_once($CFG->dirroot . '/user/externallib.php'); return new external_single_structure(array( - 'users' => new external_multiple_structure(new external_single_structure(array( - 'id' => new external_value(PARAM_INT, 'User ID'), - 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), - - 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), - 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL), - - 'idnumber' => new external_value(PARAM_NOTAGS, 'ID number', VALUE_OPTIONAL), - 'email' => new external_value(PARAM_TEXT, 'Email', VALUE_OPTIONAL), - 'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL), - 'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL), - 'department' => new external_value(PARAM_TEXT, 'Department', VALUE_OPTIONAL), - 'institution' => new external_value(PARAM_TEXT, 'Institution', VALUE_OPTIONAL), - ))), + 'users' => new external_multiple_structure(user_summary_exporter::get_read_structure()), 'count' => new external_value(PARAM_INT, 'Total number of results.') )); } diff --git a/admin/tool/lp/classes/external/user_summary_exporter.php b/admin/tool/lp/classes/external/user_summary_exporter.php new file mode 100644 index 00000000000..c72161dfe8d --- /dev/null +++ b/admin/tool/lp/classes/external/user_summary_exporter.php @@ -0,0 +1,99 @@ +. + +/** + * Class for exporting a user summary from an stdClass. + * + * @package tool_lp + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace tool_lp\external; + +use renderer_base; +use moodle_url; + +/** + * Class for exporting a user summary from an stdClass. + * + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class user_summary_exporter extends exporter { + + protected function get_other_values(renderer_base $output) { + global $PAGE; + + // Add user picture. + $userpicture = new \user_picture($this->data); + $userpicture->size = 1; // Size f1. + $profileimageurl = $userpicture->get_url($PAGE)->out(false); + $userpicture->size = 0; // Size f2. + $profileimageurlsmall = $userpicture->get_url($PAGE)->out(false); + + return array( + 'fullname' => fullname($this->data), + 'profileimageurl' => $profileimageurl, + 'profileimageurlsmall' => $profileimageurlsmall + ); + } + + public static function define_properties() { + return array( + 'id' => array( + 'type' => PARAM_INT, + ), + 'email' => array( + 'type' => PARAM_TEXT, + 'default' => '' + ), + 'idnumber' => array( + 'type' => PARAM_NOTAGS, + 'default' => '' + ), + 'phone1' => array( + 'type' => PARAM_NOTAGS, + 'default' => '' + ), + 'phone2' => array( + 'type' => PARAM_NOTAGS, + 'default' => '' + ), + 'department' => array( + 'type' => PARAM_TEXT, + 'default' => '' + ), + 'institution' => array( + 'type' => PARAM_TEXT, + 'default' => '' + ) + ); + } + + public static function define_other_properties() { + return array( + 'fullname' => array( + 'type' => PARAM_TEXT + ), + 'profileimageurl' => array( + 'type' => PARAM_URL + ), + 'profileimageurlsmall' => array( + 'type' => PARAM_URL + ), + ); + } +} diff --git a/admin/tool/lp/tests/externallib_test.php b/admin/tool/lp/tests/externallib_test.php index b3c752604c2..a6297158905 100644 --- a/admin/tool/lp/tests/externallib_test.php +++ b/admin/tool/lp/tests/externallib_test.php @@ -2565,11 +2565,11 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->assertEquals(1, $result['count']); $this->assertEquals($u1->id, $result['users'][0]['id']); $this->assertEquals($u1->idnumber, $result['users'][0]['idnumber']); - $this->assertArrayNotHasKey('email', $result['users'][0]); - $this->assertArrayNotHasKey('phone1', $result['users'][0]); - $this->assertArrayNotHasKey('phone2', $result['users'][0]); - $this->assertArrayNotHasKey('department', $result['users'][0]); - $this->assertArrayNotHasKey('institution', $result['users'][0]); + $this->assertEmpty($result['users'][0]['email']); + $this->assertEmpty($result['users'][0]['phone1']); + $this->assertEmpty($result['users'][0]['phone2']); + $this->assertEmpty($result['users'][0]['department']); + $this->assertEmpty($result['users'][0]['institution']); // Filter by email. $CFG->showuseridentity = 'email'; @@ -2612,12 +2612,12 @@ class tool_lp_external_testcase extends externallib_advanced_testcase { $this->assertCount(1, $result['users']); $this->assertEquals(1, $result['count']); $this->assertEquals($u1->id, $result['users'][0]['id']); - $this->assertArrayNotHasKey('idnumber', $result['users'][0]); - $this->assertArrayNotHasKey('email', $result['users'][0]); - $this->assertArrayNotHasKey('phone1', $result['users'][0]); - $this->assertArrayNotHasKey('phone2', $result['users'][0]); - $this->assertArrayNotHasKey('department', $result['users'][0]); - $this->assertArrayNotHasKey('institution', $result['users'][0]); + $this->assertEmpty($result['users'][0]['idnumber']); + $this->assertEmpty($result['users'][0]['email']); + $this->assertEmpty($result['users'][0]['phone1']); + $this->assertEmpty($result['users'][0]['phone2']); + $this->assertEmpty($result['users'][0]['department']); + $this->assertEmpty($result['users'][0]['institution']); } }