public function getHit($id)
 {
     $turk = new CrowdTruth\Mturk\Turkapi\MechanicalTurk('https://mechanicalturk.amazonaws.com/', false, Config::get('mturk::accesskey'), Config::get('mturk::secretkey'));
     //$hit = $turk->getHIT($id);
     //dd($hit);
     //dd($turk->getAssignmentsForHIT($id));
     //echo json_encode($hit, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
     // Create activity: annotate
     $activity = new Activity();
     $activity->label = "Imported job from AMT.";
     $activity->softwareAgent_id = 'amt';
     $activity->save();
     $f = fopen(base_path() . '/public/csv/AMT_HITIds.csv', 'r');
     $array = array();
     $keys = fgetcsv($f);
     while (!feof($f)) {
         $array[] = @array_combine($keys, fgetcsv($f));
     }
     foreach ($array as $row) {
         set_time_limit(360);
         $id = $row['HITId'];
         $type = $row['Task'];
         $existing = \Workerunit::where('hitId', $id)->first();
         if (isset($existing)) {
             continue;
         }
         $hit = $turk->getHIT($id);
         $h = $hit->toArray();
         $jobId = isset($h['RequesterWorkerunit']) ? $h['RequesterWorkerunit'] : 'unknown';
         //$job->_id;
         /*
         			//Do this once:
         			if(empty($job->Expiration)) $job->Expiration = new MongoDate(strtotime($h['Expiration']));
         			if(empty($job->HITGroupId)) $job->HITGroupId = $h['HITGroupId'];
         			if(empty($job->HITTypeId)) 	$job->HITTypeId  = $h['HITTypeId'];*/
         // Convert status to our language
         if ($h['HITStatus'] == 'Assignable' or $h['HITStatus'] == 'Unassignable') {
             //and ($job->Expiration->sec > time())) // Not yet expired. TODO: Timezones
             $newstatus = 'running';
         } elseif ($h['HITStatus'] == 'Reviewable' or $h['HITStatus'] == 'Reviewing') {
             $newstatus = 'review';
         } elseif ($h['HITStatus'] == 'Disposed') {
             $newstatus = 'deleted';
         }
         $newplatformhitid[] = array('id' => $id, 'status' => $newstatus);
         // todo: IF each is disposed, newstatus = deleted.
         // Get Assignments.
         $assignments = $turk->getAssignmentsForHIT($id);
         print 'Got ' . count($assignments) . " Assignments for {$id}\n";
         foreach ($assignments as $ass) {
             $assignment = $ass->toArray();
             $workerunit = Workerunit::where('job_id', $jobId)->where('platformWorkerunitId', $assignment['AssignmentId'])->first();
             //print_r($workerunits); die();
             if ($workerunit) {
                 $annoldstatus = $workerunit['status'];
                 $annnewstatus = $assignment['AssignmentStatus'];
                 if ($annoldstatus != $annnewstatus) {
                     $workerunit->status = $annnewstatus;
                     $workerunit->update();
                     print "Status '{$annoldstatus}' changed to '{$annnewstatus}'.";
                     Log::debug("Status of Workerunit {$workerunit->_id} changed from {$annoldstatus} to {$annnewstatus}");
                 }
             } else {
                 // ASSIGNMENT entity not in DB: create activity, entity and refer to or create agent.
                 // Create or retrieve Agent
                 $workerId = $assignment['WorkerId'];
                 if (!($agentId = MongoDB\CrowdAgent::where('platformAgentId', $workerId)->where('softwareAgent_id', 'amt')->pluck('_id'))) {
                     $agent = new MongoDB\CrowdAgent();
                     $agent->_id = "crowdagent/amt/{$workerId}";
                     $agent->softwareAgent_id = 'amt';
                     $agent->platformAgentId = $workerId;
                     $agent->save();
                     $agentId = "crowdagent/amt/{$workerId}";
                 }
                 /*							$groupedbyid = array();
                 					foreach ($assignment['Answer'] as $q=>$ans){
                 						// Retrieve the unitID and the QuestionId from the name of the input field.
                 						$split = strpos($q, "_");
                 						$unitid = substr($q, 0, $split); 	 // before the first _
                 						$qid = substr($q, $split+1);		// after the first _
                 						$groupedbyid[$unitid][$qid] = $ans;// grouped to create an entity for every ID.
                 					}
                 					
                 					// Create entity FOR EACH UNIT
                 					foreach($groupedbyid as $uid=>$qidansarray){	*/
                 $workerunit = new Workerunit();
                 $workerunit->activity_id = $activity->_id;
                 $workerunit->type = $type;
                 $workerunit->crowdAgent_id = $agentId;
                 $workerunit->softwareAgent_id = 'amt';
                 $workerunit->job_id = $jobId;
                 $workerunit->unit_id = '';
                 $workerunit->hitId = $id;
                 $workerunit->question = $h['Question'];
                 $workerunit->platformWorkerunitId = $assignment['AssignmentId'];
                 $workerunit->acceptTime = new MongoDate(strtotime($assignment['AcceptTime']));
                 $workerunit->submitTime = new MongoDate(strtotime($assignment['SubmitTime']));
                 //
                 // Todo: Optionally compute time spent doing the assignment here.
                 //
                 if (!empty($assignment['AutoApprovalTime'])) {
                     $workerunit->autoApprovalTime = new MongoDate(strtotime($assignment['AutoApprovalTime']));
                 }
                 if (!empty($assignment['ApprovalTime'])) {
                     $workerunit->autoApprovalTime = new MongoDate(strtotime($assignment['ApprovalTime']));
                 }
                 if (!empty($assignment['RejectionTime'])) {
                     $workerunit->autoApprovalTime = new MongoDate(strtotime($assignment['RejectionTime']));
                 }
                 $workerunit->content = $assignment['Answer'];
                 $workerunit->status = $assignment['AssignmentStatus'];
                 // Submitted | Approved | Rejected
                 $workerunit->user_id = 'CrowdWatson';
                 $workerunit->save();
                 $newworkerunits[] = $workerunit;
                 //}
                 /*
                 	Possibly also:
                 
                 	HITId				2P3Z6R70G5RC7PEQC857ZSST0J2P9T
                 	Deadline	
                 */
             }
         }
     }
 }
Example #2
0
 public function getUpdatecrowdagent()
 {
     foreach (MongoDB\CrowdAgent::get() as $worker) {
         set_time_limit(30);
         Queue::push('Queues\\UpdateCrowdAgent', array('crowdagent' => serialize($worker)));
     }
 }