Test Setup Failed
Push — master ( c7183e...00c715 )
by Julito
30:43
created

UserAvatar::setValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\UserBundle\Entity\User;
5
6
/**
7
 * Class UserAvatar
8
 * FormValidator element to add an user avatar wrapping a hidden input with its user ID
9
 * Is necessary set an instance of Chamilo\UserBundle\Entity\User as value. The exported value is the user ID
10
 */
11
class UserAvatar extends HTML_QuickForm_input
12
{
13
    /** @var User */
14
    private $user = null;
15
    private $imageSize = 'small';
16
    private $subTitle = '';
17
18
    /**
19
     * UserAvatar constructor.
20
     * @param string $name
21
     * @param string $label
22
     * @param array $attributes
23
     */
24
    public function __construct($name, $label, $attributes = [])
25
    {
26
        if (isset($attributes['image_size'])) {
27
            $this->imageSize = $attributes['image_size'];
28
            unset($attributes['image_size']);
29
        }
30
31
        if (isset($attributes['sub_title'])) {
32
            $this->subTitle = $attributes['sub_title'];
33
            unset($attributes['sub_title']);
34
        }
35
36
        parent::__construct($name, $label, $attributes);
37
38
        $this->setType('hidden');
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function setValue($value)
45
    {
46
        $this->user = !is_a($value, 'Chamilo\UserBundle\Entity\User')
47
            ? UserManager::getManager()->find($value)
48
            : $value;
49
50
        parent::setValue($this->user->getId());
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function toHtml()
57
    {
58
        if (!$this->user) {
59
            return '';
60
        }
61
62
        $userInfo = api_get_user_info($this->user->getId());
63
        $userPicture = isset($userInfo["avatar_{$this->imageSize}"])
64
            ? $userInfo["avatar_{$this->imageSize}"]
65
            : $userInfo["avatar"];
66
67
        if (!$this->subTitle) {
68
            $this->subTitle = $this->user->getUsername();
69
        }
70
71
        $html = parent::toHtml();
72
        $html .= '
73
            <div class="media">
74
                <div class="media-left">
75
                    <img src="'.$userPicture.'" alt="'.$this->user->getCompleteName().'">
76
                </div>
77
                <div class="media-body">
78
                    <h4 class="media-heading">'.$this->user->getCompleteName().'</h4>
79
                    '.$this->subTitle.'
80
                </div>
81
            </div>
82
        ';
83
84
        return $html;
85
    }
86
}
87