Merge branch 'MDL-25886-user_picture_fields' of git://github.com/mudrd8mz/moodle
This commit is contained in:
@@ -177,9 +177,10 @@ class user_picture implements renderable {
|
||||
* @param string $tableprefix name of database table prefix in query
|
||||
* @param array $extrafields extra fields to be included in result (do not include TEXT columns because it would break SELECT DISTINCT in MSSQL and ORACLE)
|
||||
* @param string $idalias alias of id field
|
||||
* @param string $fieldprefix prefix to add to all columns in their aliases, does not apply to 'id'
|
||||
* @return string
|
||||
*/
|
||||
public static function fields($tableprefix = '', array $extrafields = NULL, $idalias = 'id') {
|
||||
public static function fields($tableprefix = '', array $extrafields = NULL, $idalias = 'id', $fieldprefix = '') {
|
||||
if (!$tableprefix and !$extrafields and !$idalias) {
|
||||
return implode(',', self::$fields);
|
||||
}
|
||||
@@ -191,7 +192,11 @@ class user_picture implements renderable {
|
||||
if ($field === 'id' and $idalias and $idalias !== 'id') {
|
||||
$fields[$field] = "$tableprefix$field AS $idalias";
|
||||
} else {
|
||||
$fields[$field] = $tableprefix.$field;
|
||||
if ($fieldprefix and $field !== 'id') {
|
||||
$fields[$field] = "$tableprefix$field AS $fieldprefix$field";
|
||||
} else {
|
||||
$fields[$field] = "$tableprefix$field";
|
||||
}
|
||||
}
|
||||
}
|
||||
// add extra fields if not already there
|
||||
@@ -200,13 +205,60 @@ class user_picture implements renderable {
|
||||
if ($e === 'id' or isset($fields[$e])) {
|
||||
continue;
|
||||
}
|
||||
$fields[$e] = $tableprefix.$e;
|
||||
if ($fieldprefix) {
|
||||
$fields[$e] = "$tableprefix$e AS $fieldprefix$e";
|
||||
} else {
|
||||
$fields[$e] = "$tableprefix$e";
|
||||
}
|
||||
}
|
||||
}
|
||||
return implode(',', $fields);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the aliased user fields from a given record
|
||||
*
|
||||
* Given a record that was previously obtained using {@link self::fields()} with aliases,
|
||||
* this method extracts user related unaliased fields.
|
||||
*
|
||||
* @param stdClass $record containing user picture fields
|
||||
* @param array $extrafields extra fields included in the $record
|
||||
* @param string $idalias alias of the id field
|
||||
* @param string $fieldprefix prefix added to all columns in their aliases, does not apply to 'id'
|
||||
* @return stdClass object with unaliased user fields
|
||||
*/
|
||||
public static function unalias(stdClass $record, array $extrafields=null, $idalias='id', $fieldprefix='') {
|
||||
|
||||
if (empty($idalias)) {
|
||||
$idalias = 'id';
|
||||
}
|
||||
|
||||
$return = new stdClass();
|
||||
|
||||
foreach (self::$fields as $field) {
|
||||
if ($field === 'id') {
|
||||
if (isset($record->{$idalias})) {
|
||||
$return->id = $record->{$idalias};
|
||||
}
|
||||
} else {
|
||||
if (isset($record->{$fieldprefix.$field})) {
|
||||
$return->{$field} = $record->{$fieldprefix.$field};
|
||||
}
|
||||
}
|
||||
}
|
||||
// add extra fields if not already there
|
||||
if ($extrafields) {
|
||||
foreach ($extrafields as $e) {
|
||||
if ($e === 'id' or isset($return->{$e})) {
|
||||
continue;
|
||||
}
|
||||
$return->{$e} = $record->{$fieldprefix.$e};
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data structure representing a help icon.
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Unit tests for lib/outputcomponents.php.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2011 David Mudrak <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once($CFG->libdir . '/outputcomponents.php');
|
||||
|
||||
/**
|
||||
* Unit tests for the user_picture class
|
||||
*/
|
||||
class user_picture_test extends UnitTestCase {
|
||||
|
||||
public static $includecoverage = array('lib/outputcomponents.php');
|
||||
|
||||
public function test_user_picture_fields_aliasing() {
|
||||
$fields = user_picture::fields();
|
||||
$fields = array_map('trim', explode(',', $fields));
|
||||
$this->assertTrue(in_array('id', $fields));
|
||||
|
||||
$aliased = array();
|
||||
foreach ($fields as $field) {
|
||||
if ($field === 'id') {
|
||||
$aliased['id'] = 'aliasedid';
|
||||
} else {
|
||||
$aliased[$field] = 'prefix'.$field;
|
||||
}
|
||||
}
|
||||
|
||||
$returned = user_picture::fields('', array('custom1', 'id'), 'aliasedid', 'prefix');
|
||||
$returned = array_map('trim', explode(',', $returned));
|
||||
$this->assertEqual(count($returned), count($fields) + 1); // only one extra field added
|
||||
|
||||
foreach ($fields as $field) {
|
||||
if ($field === 'id') {
|
||||
$expected = "id AS aliasedid";
|
||||
} else {
|
||||
$expected = "$field AS prefix$field";
|
||||
}
|
||||
$this->assertTrue(in_array($expected, $returned), "Expected pattern '$expected' not returned");
|
||||
}
|
||||
$this->assertTrue(in_array("custom1 AS prefixcustom1", $returned), "Expected pattern 'custom1 AS prefixcustom1' not returned");
|
||||
}
|
||||
|
||||
public function test_user_picture_fields_unaliasing() {
|
||||
$fields = user_picture::fields();
|
||||
$fields = array_map('trim', explode(',', $fields));
|
||||
|
||||
$fakerecord = new stdClass();
|
||||
$fakerecord->aliasedid = 42;
|
||||
foreach ($fields as $field) {
|
||||
if ($field !== 'id') {
|
||||
$fakerecord->{'prefix'.$field} = "Value of $field";
|
||||
}
|
||||
}
|
||||
$fakerecord->prefixcustom1 = 'Value of custom1';
|
||||
|
||||
$returned = user_picture::unalias($fakerecord, array('custom1'), 'aliasedid', 'prefix');
|
||||
|
||||
$this->assertEqual($returned->id, 42);
|
||||
foreach ($fields as $field) {
|
||||
if ($field !== 'id') {
|
||||
$this->assertEqual($returned->{$field}, "Value of $field");
|
||||
}
|
||||
}
|
||||
$this->assertEqual($returned->custom1, 'Value of custom1');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user