forked from tldev/litmos-php
/
Users.php
120 lines (101 loc) · 2.67 KB
/
Users.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace Litmos;
use Litmos\User\Basic as UserBasic;
class Users
{
/**
* @var Service
*/
private $service;
public function __construct(Service $litmos_service)
{
$this->service = $litmos_service;
}
/**
* @param PagingSearch $ps
*
* @return UserBasic[]
*/
public function getAll(PagingSearch $ps = null)
{
$response = $this->service->get('/users', $ps);
$xml = new \SimpleXMLElement($response);
$users_nodes = $xml->children();
$users = array();
foreach ($users_nodes as $user_node) {
$id = (string)$user_node->Id;
$username = (string)$user_node->UserName;
$first_name = (string)$user_node->FirstName;
$last_name = (string)$user_node->LastName;
$users[] = new UserBasic($this->service, $id, $username, $first_name, $last_name);
}
return $users;
}
/**
* @param string|UserBasic $user_id
* @return User
*/
public function get($user_id)
{
if ($user_id instanceof UserBasic) {
$user_id = $user_id->getUserId();
}
if (empty($user_id)) {
throw new Exception\InvalidArgumentException('No User Id was specified.');
}
$response = $this->service->get("/users/{$user_id}");
return User::FromXml($this->service, $response);
}
/**
* @param string $user_name
* @param string $first_name
* @param string $last_name
* @param string $email
* @param string $skype
* @param string $phone_work
* @param string $phone_mobile
* @param bool $skip_first_login
*
* @return User
*/
public function create(
$user_name,
$first_name,
$last_name,
$email = '',
$skype = '',
$phone_work = '',
$phone_mobile = '',
$skip_first_login = false
) {
$user = new User(
$this->service,
'',
$user_name,
$first_name,
$last_name,
'',
$email,
'Learner',
false,
true,
$skype,
$phone_work,
$phone_mobile,
new \DateTime(),
'',
$skip_first_login
);
$req_xml = $user->toXml();
$rep_xml = $this->service->post('/users', $req_xml);
return User::FromXml($this->service, $rep_xml);
}
/**
* @param User $user
*/
public function update(User $user)
{
$xml = $user->toXml();
$this->service->put("/users/{$user->getUserId()}", $xml);
}
}