MDL-52198 cbe: Move user summary to an exporter

This commit is contained in:
Damyon Wiese
2016-04-18 10:58:44 +08:00
committed by Frederic Massart
parent 80583e0dff
commit 6880579736
5 changed files with 124 additions and 42 deletions
+1 -1
View File
@@ -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)}}});
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)}}});
+1 -1
View File
@@ -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]);
}
+12 -29
View File
@@ -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.')
));
}
@@ -0,0 +1,99 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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
),
);
}
}
+11 -11
View File
@@ -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']);
}
}