-
Notifications
You must be signed in to change notification settings - Fork 2
/
mturk-example.php
128 lines (100 loc) · 4.27 KB
/
mturk-example.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
<?php
require_once(dirname(__FILE__) . '/php-mturk-api/MechanicalTurk.class.php');
// Fill out your credentials in config.php.
// $example = new Example;
// $example->notifyWorkers();
class Example {
private $mturk;
public function __construct(){
$this->mturk = new MechanicalTurk();
}
public function notifyWorkers(){
$this->mturk->notifyWorkers('test message', 'body of the message', 'A1M46IEXAMPLE');
}
public function setNotificationOfTheFirstHITTypeToInactive(){
// Not a very useful function, but it demonstrates how to get all the HITs as well as the Notification operation.
$hits = $this->mturk->getAllHITs();
$hit = $this->mturk->getHIT($hits[0]);
$this->mturk->setIfActiveHITTypeNotification($hit->getHITTypeId(), false);
}
public function createHIT(){
//A HTMLQuestion should be in this format. Other types are QuestionForm and ExternalQuestion
//(http://docs.aws.amazon.com/AWSMechTurk/latest/AWSMturkAPI/ApiReference_QuestionAnswerDataArticle.html)
$question =
"<?xml version='1.0' ?>
<HTMLQuestion xmlns='http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2011-11-11/HTMLQuestion.xsd'>
<HTMLContent><![CDATA[
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
<script type='text/javascript' src='https://s3.amazonaws.com/mturk-public/externalHIT_v1.js'></script>
</head>
<body>
<form name='mturk_form' method='post' id='mturk_form' action='https://www.mturk.com/mturk/externalSubmit'>
<input type='hidden' value='' name='assignmentId' id='assignmentId'/>
<h1>What's up?</h1>
<p><textarea name='Q1' cols='80' rows='3'></textarea></p>
<p><input name='Q2' type='text'/></p>
<p><input type='submit' id='submitButton' value='Submit' /></p></form>
<script language='Javascript'>turkSetAssignmentID();</script>
</body>
</html>
]]>
</HTMLContent>
<FrameHeight>450</FrameHeight>
</HTMLQuestion>
";
// Instantiate and fill a new HIT Object.
$hit = new Hit();
$hit->setTitle('Title');
$hit->setDescription('This is an example.');
$hit->setKeywords('some, keywords, separated, by, commas');
$hit->setReward(array('Amount' => 0.01, 'CurrencyCode' => 'USD'));
$hit->setAssignmentDurationInSeconds(60);
$hit->setAutoApprovalDelayInSeconds(86400);
$hit->setLifetimeInSeconds(86400);
$hit->setMaxAssignments(1);
$hit->setQuestion($question);
/*
// Instead of a question, you can also use a HITLayoutId and LayoutParameters.
$hit->setHITLayoutId('20L11K274J7CJ22DALE49UXXXXXXXXX'); // Found in the GUI.
$hit->setLayoutParameters(array('sentence'=>'This sentence is just an example',
'term1'=>'sentence',
'term2'=>'example'));
*/
// Workers can only do the assignment if they qualify.
$hit->setQualificationRequirement(array(
array('QualificationTypeId' => '000000000000000000L0', //AssignmentsApproved. See APIdocs for more.
'Comparator' => 'GreaterThan',
'IntegerValue' => '90',
'RequiredToPreview' => '90'),
array('QualificationTypeId' => '00000000000000000071', //Country.
'Comparator' => 'EqualTo',
'LocaleValue' => 'US'))); // ISO 3166. http://www.iso.org/iso/country_codes/iso_3166_code_lists.htm
// Gold standard questions and what to do with them.
$hit->setAssignmentReviewPolicy(array(
'AnswerKey' => array( 'Q1' => 'answer1', // AnswerKey is mandatory.
'Q2' => 'answer2'), // QuestionId => Correct answer
'Parameters' => array( 'RejectIfKnownAnswerScoreIsLessThan' => 100, // %
'RejectReason' => 'Too many mistakes, sorry.') // Worker will see this in the comment.
));
// Create the HIT. It will be published right away.
try {
echo "Created HIT with the ID " . $this->mturk->createHIT($hit) . ".";
} catch (AMTException $e) {
echo $e->getMessage();
}
}
public function getAllAnswersToHIT(){
try {
$result = $this->mturk->getAssignmentsForHIT('2GKMIKMN9U8S8A9F29NXL3SUAF5XXX');
} catch (AMTException $e) {
echo $e->getMessage();
}
foreach($result as $r){
print_r($r->getAnswer()); // See the Assignment class for the getters.
}
}
}
?>