-
Notifications
You must be signed in to change notification settings - Fork 0
/
CFJob.inc.php
325 lines (298 loc) · 11.8 KB
/
CFJob.inc.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
namespace crowdwatson;
require_once 'CFBasicRequests.inc.php';
require_once 'CFAddFunctions.php';
class Job extends CFBasicRequests {
function __construct($api_key) {
$this->setApiKey($api_key);
$this->setReferenceResource("jobs");
}
/**
* Pause a CrowdFlower job in order to temporarily stop judgments from coming in.
* @param string $job_id
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function pauseJob($job_id) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/pause.json";
$result = $this->curlRequest($url, "GET", null);
return $result;
}
/**
* Resume a paused CrowdFlower job.
* @param string $job_id
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function resumeJob($job_id) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/resume.json";
$result = $this->curlRequest($url, "GET", null);
return $result;
}
/**
* Cancel a CrowdFlower job if you want to permanently stop judgments from coming in and refund your account for any judgments not yet received.
* @param string $job_id
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function cancelJob($job_id) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/cancel.json";
$result = $this->curlRequest($url, "GET", null);
return $result;
}
/**
* Delete a CrowdFlower job that was either cancelled or not ordered.
* @param string $job_id
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function deleteJob($job_id) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . ".json";
$result = $this->curlRequest($url, "DELETE", null);
return $result;
}
/**
* Copy a CrowdFlower job with all its units.
* @param string $job_id
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function copyJobWithAllUnits($job_id) {
$data = array("all_units" => "true");
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/copy.json";
$result = $this->curlRequest($url, "POST", $data);
return $result;
}
/**
* Copy a CrowdFlower job without units.
* @param string $job_id
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function copyJobWithoutUnits($job_id) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/copy.json";
$result = $this->curlRequest($url, "POST", null);
return $result;
}
/**
* Copy a CrowdFlower job by extracting only its gold units.
* @param string $job_id
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function copyJobOnlyGoldUnits($job_id) {
$data = array("gold" => "true");
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/copy.json";
$result = $this->curlRequest($url, "POST", $data);
return $result;
}
/**
* Create a CrowdFlower job.
* @param string $data (fields that can be part of the data: title, judgments_per_unit, max_judgments_per_worker, units_per_assignment, max_judgments_per_ip, webhook_uri, send_judgments_webhook => true, payment_cents, instructions, css, js, cml)
* @return Array $result (answer returned by the cURL request) => from here we can extract the id of the job just created
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function createJob($data) {
$url = $this->getRequestURL() . $this->getReferenceResource() . ".json";
$result = $this->curlRequest($url, "POST", prefixData($data, "job"));
return $result;
}
/**
* Update an existing CrowdFlower job.
* @param string $job_id
* @param string $data (same attributes as described for createJob)
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function updateJob($job_id, $data) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . ".json";
$result = $this->curlRequest($url, "PUT", prefixData($data, "job"));
return $result;
}
/**
* Add data to an existing job by uploading a CSV file.
* @param string $job_id
* @param string $file_path
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#uploading
*/
public function uploadInputFile($job_id, $file_path) {
$data['file_path'] = $file_path;
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/upload.json";
$result = $this->curlRequest($url, "UPLOAD", $data);
return $result;
}
/**
* Check the status/progress of a CrowdFlower job.
* @param string $job_id
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function statusJob($job_id) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/ping.json";
$result = $this->curlRequest($url, "GET", null);
return $result;
}
/**
* Check the legend of a CrowdFlower job (shows the generated keys that will end up being submitted with your form).
* @param string $job_id
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function legendJob($job_id) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/legend.json";
$result = $this->curlRequest($url, "GET", null);
return $result;
}
/**
* Read all the information about an existing CrowdFlower job.
* @param string $job_id
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function readJob($job_id) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . ".json";
$result = $this->curlRequest($url, "GET", null);
return $result;
}
/**
* Set the channels for a CrowdFlower job (to be published on).
* @param string $job_id
* @param Array $channels (cf_internal and on_demand)
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function setChannels($job_id, $channels) {
$data = array();
$data["stringRequest"] = "";
for ($i = 0; $i < count($channels); $i ++) {
$data["stringRequest"] .= "channels[]=".$channels[$i]."&";
}
$data["stringRequest"] = substr($data["stringRequest"], 0, -1);
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/channels";
$result = $this->curlRequest($url, "PUT", $data);
return $result;
}
/**
* Get the channels of a CrowdFlower job.
* @param string $job_id
* @return Array $result (answer returned by the cURL request): contains the list of enabled channels
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function getChannels($job_id) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/channels";
$result = $this->curlRequest($url, "GET", null);
// for all the channels available, please use this return call:
// return $result["result"]["available_channels"];
// for both available and enabled channels, please use this return call:
// return $result["result"];
return $result["result"]["enabled_channels"];
}
/**
* Set the countries whose workers are allowed to work on a CrowdFlower job.
* @param string $job_id
* @param Array $countries (contains the codes for those countries) => please check the CF website for the list of codes
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function setIncludedCountries($job_id, $countries) {
$data = array();
$data["stringRequest"] = "";
for ($i = 0; $i < count($countries); $i ++) {
$data["stringRequest"] .= "job[included_countries][]=".$countries[$i]."&";
}
$data["stringRequest"] = substr($data["stringRequest"], 0, -1);
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . ".json";
$result = $this->curlRequest($url, "PUT", $data);
return $result;
}
/**
* Set the list of options for a CrowdFlower job.
* @param string $job_id
* @param Array $data (req_ttl_in_seconds, mail_to, keywords)
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function setOptions($job_id, $data) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . ".json";
$result = $this->curlRequest($url, "PUT", prefixData($data, "job"));
return $result;
}
/**
* Set or reset the gold questions for a CrowdFlower job. Depends on the input file uploaded.
* @param string $job_id
* @param Array $data (possible fields: reset, check, with)
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function manageGold($job_id, $data) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/gold";
if (isset($data["reset"]))
$result = $this->curlRequest($url, "PUT", $data);
else
$result = $this->curlRequest($url, "PUT", prefixData($data, "job"));
return $result;
}
/**
* Return a list of ids representing the CrowdFlower unit_id for each input sentence in the CSV file.
* @param string $job_id
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#units
*/
public function getUnitsIds($job_id) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/units.json";
$result = $this->curlRequest($url, "GET", null);
return array_keys($result["result"]);
}
/**
* Return the judgments received for a unit
* @param string $job_id
* @param string $job_id
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#judgments
*/
public function getUnitJudgments($job_id, $unit_id) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/units/". $unit_id . ".json";
//$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/". $unit_id . ".json";
$result = $this->curlRequest($url, "GET", null);
return $result["result"];
}
/**
* Order a created job.
* @param string $job_id
* @param string $unitsToOrder count of the units to be ordered.
* @return Array $result (answer returned by the cURL request)
* @throws
* @link http://crowdflower.com/docs-api#jobs
*/
public function sendOrder($job_id, $unitsToOrder, $channels) {
$url = $this->getRequestURL() . $this->getReferenceResource() . "/" . $job_id . "/orders";
$data["stringRequest"] = "debit[units_count]=" . $unitsToOrder;
for ($i = 0; $i < count($channels); $i ++) {
$data["stringRequest"] .= "&channels[]=".$channels[$i];
}
$result = $this->curlRequest($url, "POST", $data);
return $result;
}
}
?>