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.
--- /dev/null
+<?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 <david@moodle.com>
+ * @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');
+ }
+}