1
|
|
|
<?php namespace Qualia\Submission; |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Qualia Analytics. |
5
|
|
|
* |
6
|
|
|
* (c) Qualia Analytics Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
use InvalidArgumentException; |
12
|
|
|
use Qualia\Client; |
13
|
|
|
use Qualia\Util; |
14
|
|
|
|
15
|
|
|
class Entry |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $additional; |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $data; |
25
|
|
|
/** |
26
|
|
|
* @var \Qualia\Client |
27
|
|
|
*/ |
28
|
|
|
private $client; |
29
|
|
|
|
30
|
|
|
|
31
|
78 |
|
private function __construct(Client $client) { |
32
|
78 |
|
$this->data = array(); |
33
|
78 |
|
$this->additional = array( |
34
|
39 |
|
'mark_complete' => 0, |
35
|
|
|
); |
36
|
78 |
|
$this->client = $client; |
37
|
78 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Build a configuration |
41
|
|
|
* |
42
|
|
|
* @param \Qualia\Client $client |
43
|
|
|
* @return \Qualia\Submission\Entry |
44
|
|
|
*/ |
45
|
78 |
|
public static function build(Client $client) |
46
|
|
|
{ |
47
|
78 |
|
return (new Entry($client)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Provide an unique identifier for an entry. It can be either string or integer e.g. incremental id from your database |
52
|
|
|
* This ensures you are not submitting the entry twice and even if you do submit it, it will update the previous entry |
53
|
|
|
* rather than creating a new one. |
54
|
|
|
* |
55
|
|
|
* @param $identifier |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
20 |
|
public function uniqueId($identifier) |
59
|
|
|
{ |
60
|
18 |
|
$this->additional['unique_id'] = $identifier; |
61
|
|
|
|
62
|
20 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Provide a language for an entry. e.g. 'en', 'es', 'pt' |
67
|
|
|
* |
68
|
|
|
* @param $language |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
12 |
|
public function language($language) |
72
|
|
|
{ |
73
|
12 |
|
$this->additional['language'] = $language; |
74
|
|
|
|
75
|
12 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* By default we will check and only allow submitting entries with unique email address. |
80
|
|
|
* You may want to allow duplicate emails in some cases |
81
|
|
|
* |
82
|
|
|
* @param bool $allow |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
6 |
|
public function allowDuplicates($allow = true) |
86
|
|
|
{ |
87
|
6 |
|
$this->additional['allow_duplicates'] = $allow ? 1 : 0; |
88
|
|
|
|
89
|
6 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Append name question |
94
|
|
|
* |
95
|
|
|
* @param $id Question identifier |
96
|
|
|
* @param $firstName First name |
97
|
|
|
* @param null $lastName Last name |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
6 |
|
public function name($id, $firstName = null, $lastName = null) { |
101
|
|
|
|
102
|
6 |
|
$this->data[$id] = array( |
103
|
6 |
|
'first_name' => $firstName, |
104
|
6 |
|
'last_name' => $lastName, |
105
|
|
|
); |
106
|
|
|
|
107
|
6 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Append email question |
112
|
|
|
* |
113
|
|
|
* @param $id Question identifier |
114
|
|
|
* @param $address E-mail address |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
54 |
|
public function email($id, $address) { |
118
|
54 |
|
$this->data[$id] = $address; |
119
|
|
|
|
120
|
54 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Append date question |
126
|
|
|
* |
127
|
|
|
* @param $id Question identifier |
128
|
|
|
* @param $date Date in Y-m-d format |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
24 |
|
public function date($id, $date) |
132
|
|
|
{ |
133
|
24 |
|
if (! Util::isValidDate($date)) { |
134
|
6 |
|
throw new InvalidArgumentException("Date must be provided in Y-m-d format"); |
135
|
|
|
} |
136
|
|
|
|
137
|
18 |
|
$this->data[$id] = $date; |
138
|
|
|
|
139
|
18 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Append any other type of response |
144
|
|
|
* |
145
|
|
|
* @param $id Question identifier |
146
|
|
|
* @param $response Response to a question |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
12 |
|
public function response($id, $response) |
150
|
|
|
{ |
151
|
12 |
|
$this->data[$id] = $response; |
152
|
|
|
|
153
|
12 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Once we have everything added, we can submit. |
158
|
|
|
* |
159
|
|
|
* @param bool $markComplete If you would like submit the survey and mark it as completed,please set this field to true, |
160
|
|
|
* otherwise, survey will not be marked as completed and an email will be sent to the user |
161
|
|
|
* asking to complete the rest of the survey. |
162
|
|
|
* |
163
|
|
|
* Depending on survey distribution channels this may vary: |
164
|
|
|
* Institution may have an Enrollment form enabled where it collects some data on site, |
165
|
|
|
* some through this API, some maybe shared in social media. If that's the case, must likely |
166
|
|
|
* you do not need to mark it as completed. |
167
|
|
|
* |
168
|
|
|
* However, |
169
|
|
|
* If all responses are collected via this API and second survey is send later, you may |
170
|
|
|
* need to mark it as completed. |
171
|
|
|
* @return array |
172
|
|
|
* @throws \Qualia\Exceptions\RequestException |
173
|
|
|
*/ |
174
|
72 |
|
public function send($markComplete = false) |
175
|
|
|
{ |
176
|
72 |
|
if ($markComplete) { |
177
|
6 |
|
$this->additional['mark_complete'] = 1; |
178
|
3 |
|
} |
179
|
|
|
|
180
|
72 |
|
return $this->client->post('survey/'. $this->client->getSurveyId() .'/entries', $this->data, $this->additional); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
} |