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
|
54 |
|
private function __construct(Client $client) { |
32
|
54 |
|
$this->data = array(); |
33
|
54 |
|
$this->additional = array( |
34
|
27 |
|
'mark_complete' => 0, |
35
|
|
|
); |
36
|
54 |
|
$this->client = $client; |
37
|
54 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Build a configuration |
41
|
|
|
* |
42
|
|
|
* @param \Qualia\Client $client |
43
|
|
|
* @return \Qualia\Submission\Entry |
44
|
|
|
*/ |
45
|
54 |
|
public static function build(Client $client) |
46
|
|
|
{ |
47
|
54 |
|
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
|
|
|
* Append name question |
80
|
|
|
* |
81
|
|
|
* @param $id Question identifier |
82
|
|
|
* @param $firstName First name |
83
|
|
|
* @param null $lastName Last name |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
6 |
|
public function name($id, $firstName = null, $lastName = null) { |
87
|
|
|
|
88
|
6 |
|
$this->data[$id] = array( |
89
|
6 |
|
'first_name' => $firstName, |
90
|
6 |
|
'last_name' => $lastName, |
91
|
|
|
); |
92
|
|
|
|
93
|
6 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Append email question |
98
|
|
|
* |
99
|
|
|
* @param $id Question identifier |
100
|
|
|
* @param $address E-mail address |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
30 |
|
public function email($id, $address) { |
104
|
30 |
|
$this->data[$id] = $address; |
105
|
|
|
|
106
|
30 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Append date question |
112
|
|
|
* |
113
|
|
|
* @param $id Question identifier |
114
|
|
|
* @param $date Date in Y-m-d format |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
24 |
|
public function date($id, $date) |
118
|
|
|
{ |
119
|
24 |
|
if (! Util::isValidDate($date)) { |
120
|
6 |
|
throw new InvalidArgumentException("Date must be provided in Y-m-d format"); |
121
|
|
|
} |
122
|
|
|
|
123
|
18 |
|
$this->data[$id] = $date; |
124
|
|
|
|
125
|
18 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Append any other type of response |
130
|
|
|
* |
131
|
|
|
* @param $id Question identifier |
132
|
|
|
* @param $response Response to a question |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
12 |
|
public function response($id, $response) |
136
|
|
|
{ |
137
|
12 |
|
$this->data[$id] = $response; |
138
|
|
|
|
139
|
12 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Once we have everything added, we can submit. |
144
|
|
|
* |
145
|
|
|
* @param bool $markComplete If you would like submit the survey and mark it as completed,please set this field to true, |
146
|
|
|
* otherwise, survey will not be marked as completed and an email will be sent to the user |
147
|
|
|
* asking to complete the rest of the survey. |
148
|
|
|
* |
149
|
|
|
* Depending on survey distribution channels this may vary: |
150
|
|
|
* Institution may have an Enrollment form enabled where it collects some data on site, |
151
|
|
|
* some through this API, some maybe shared in social media. If that's the case, must likely |
152
|
|
|
* you do not need to mark it as completed. |
153
|
|
|
* |
154
|
|
|
* However, |
155
|
|
|
* If all responses are collected via this API and second survey is send later, you may |
156
|
|
|
* need to mark it as completed. |
157
|
|
|
* @return array |
158
|
|
|
* @throws \Qualia\Exceptions\RequestException |
159
|
|
|
*/ |
160
|
48 |
|
public function send($markComplete = false) |
161
|
|
|
{ |
162
|
48 |
|
if ($markComplete) { |
163
|
6 |
|
$this->additional['mark_complete'] = 1; |
164
|
3 |
|
} |
165
|
|
|
|
166
|
48 |
|
return $this->client->post('survey/'. $this->client->getSurveyId() .'/entries', $this->data, $this->additional); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
} |