public function getConvertcsv()
 {
     if (($handle = fopen(storage_path() . '/jobs.csv', 'r')) === false) {
         die('Error opening file');
     }
     /*		foreach (\QuestionTemplate::get() as $q)
     			$q->forceDelete();
     
     		foreach (\JobConfiguration::get() as $q)
     			$q->forceDelete();
     
     		foreach (\Job::get() as $q)
     			$q->forceDelete();
     */
     Activity::truncate();
     $activity = new Activity();
     $activity->label = "Imported jobs from CSV file.";
     //$activity->used = $job->_id;
     $activity->softwareAgent_id = 'importer';
     $activity->save();
     $headers = fgetcsv($handle, 1024, ',');
     $count = 0;
     $complete = array();
     while ($row = fgetcsv($handle, 1024, ',')) {
         $c = array_combine($headers, $row);
         $c['platform'] = array($c['platform']);
         $j = new JobConfiguration();
         //$j->_id = "entity/text/medical/jobconf/$count";
         $j->type = isset($row['type']) ? $row['type'] : 'todo';
         $j->content = $c;
         $j->hash = md5(serialize($j->content));
         $j->activity_id = $activity->_id;
         $j->user_id = 'CrowdWatson';
         $j->save();
         $job = new Job();
         $job->jobConf_id = $j->_id;
         $job->activity_id = $activity->_id;
         $job->batch_id = "entity/text/medical/batch/{$count}";
         $job->type = isset($row['type']) ? $row['type'] : 'todo';
         $job->user_id = 'CrowdWatson';
         $job->status = 'finished';
         $job->save();
         $count++;
     }
     //new MongoDate(strtotime(
     fclose($handle);
     echo json_encode($complete, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
 }
Esempio n. 2
0
 public function postSubmitFinal($ordersandbox = 'order')
 {
     $batch = unserialize(Session::get('batch'));
     $batchColumnsNewTemplate = array();
     $batchColumnsExtraChosenTemplate = array();
     $associationsTemplBatch = array();
     $ownTemplate = false;
     // Use existing job configuration content (if available)
     if (!($jc = unserialize(Session::get('jobconf')))) {
         $jc = new JobConfiguration();
         $jc->documentType = "jobconf";
         $jcco = array();
     } else {
         $jcco = $jc->content;
     }
     $own = false;
     if (Input::has('templateTypeOwn') && strlen(Input::get('templateTypeOwn')) > 0) {
         $jcco['type'] = Input::get('templateTypeOwn');
         $batchColumns = Input::get('batchColumns');
         $newNamesForBatchColumns = Input::get('newcolnames');
         $newNamesForBatchColumnsArray = explode(",", $newNamesForBatchColumns);
         array_pop($newNamesForBatchColumnsArray);
         if ($batchColumns == null) {
             return Redirect::back()->with('flashError', "You did not choose the batch columns");
         } else {
             foreach ($newNamesForBatchColumnsArray as $value) {
                 $oldNewArray = explode(" - ", $value);
                 if (in_array($oldNewArray[0], $batchColumns)) {
                     $batchColumnsNewTemplate[$oldNewArray[0]] = $oldNewArray[1];
                 }
             }
         }
         $ownTemplate = true;
         $own = true;
     } else {
         $jcco['type'] = Input::get('templateType');
         if ($jcco['type'] == null) {
             return Redirect::back()->with('flashError', "You did not fill in the type of the template");
         }
         // Get selected, newest template
         $jcbase = $this->findNewestTemplate($jcco['type'], $batch->format);
         if ($jcbase == null) {
             Session::flash('flashError', "template not found");
             return Redirect::to("jobs2/submit");
         }
         if (!isset($jcbase['cml'])) {
             // Template must have CML field
             Session::flash('flashError', "No template details in this template");
             return Redirect::to("jobs2/submit");
         }
         $batchColumnsExtraChosenTemplate = Input::get('addMoreColumns');
         $fieldsInChosenTemplate = Input::get('tempFields');
         $arrayFields = explode(",", $fieldsInChosenTemplate);
         foreach ($arrayFields as $field) {
             $association = Input::get($field);
             array_push($associationsTemplBatch, $field . " - " . $association);
             if ($association == null || $association == "---") {
                 return Redirect::back()->with('flashError', "You did not fill in all the associations for the template fields");
             }
         }
         $jcco['cml'] = $jcbase['cml'];
         if (isset($jcbase['css'])) {
             $jcco['css'] = $jcbase['css'];
         }
         if (isset($jcbase['instructions'])) {
             $jcco['instructions'] = $jcbase['instructions'];
         }
         if (isset($jcbase['js'])) {
             $jcco['js'] = $jcbase['js'];
         }
         $jcco['template_id'] = $jcbase['_id'];
     }
     if (Input::has('titleOwn') && strlen(Input::get('titleOwn')) > 0) {
         $jcco['title'] = Input::get('titleOwn');
     } else {
         $jcco['title'] = Input::get('title');
     }
     if ($jcco['title'] == null) {
         return Redirect::back()->with('flashError', "You did not fill in the title of the template");
     }
     // TODO: dynamically load available platforms (in case more than cf are available).
     // At the moment this will work for CF2 and DrDetective but it is not flexible
     if (isset($jcbase['platform'])) {
         $jcco['platform'] = [$jcbase['platform']];
     } else {
         $jcco['platform'] = ["cf2"];
         $jcco['title'] = $jcco['title'] . "[[" . $jcco['type'] . "(" . $batch->_id . ", " . $batch->domain . ", " . $batch->format . ")]]";
     }
     $jcco['description'] = Input::get('description');
     ///////// PUT
     $jc->content = $jcco;
     if ($own) {
         $_tt = \MongoDB\Template::where('type', $jcco['type'])->where("format", $batch->format)->first();
         if (isset($_tt)) {
             Session::flash('flashError', "There is already a template of this type. Please rename (or select this template from dropdown list.");
             return Redirect::to("jobs2/submit");
         }
     }
     try {
         // Save activity
         $activity = new MongoDB\Activity();
         $activity->label = "Job is uploaded to crowdsourcing platform.";
         $activity->softwareAgent_id = 'jobcreator';
         // JOB softwareAgent_id = $platform. Does this need to be the same?
         $activity->save();
         // Save jobconf if necessary
         $hash = md5(serialize($jc->content));
         if ($existingid = JobConfiguration::where('hash', $hash)->pluck('_id')) {
             //[qq]
             $jcid = $existingid;
             // Don't save, it already exists.
         } else {
             $jc->format = $batch->format;
             $jc->domain = $batch->domain;
             $jc->project = $batch->project;
             $jc->user_id = $batch->user_id;
             $jc->softwareAgent_id = $activity->softwareAgent_id;
             $jc->hash = $hash;
             $jc->activity_id = $activity->_id;
             $jc->save();
             $jcid = $jc->_id;
         }
         // Software Agent ID which will perform the job
         // TODO: Fix this -- choose one of the possibly many platforms in $jcco
         // at the moment we are picking the first arbitrarily
         $job_sw_agent = $jcco['platform'][0];
         // Publish jobs
         $j = new Job();
         $j->format = $batch->format;
         $j->project = $batch->project;
         $j->domain = $batch->domain;
         $j->user_id = $batch->user_id;
         $j->type = $jc->content['type'];
         $j->batch_id = $batch->_id;
         $j->jobConf_id = $jcid;
         $j->softwareAgent_id = $job_sw_agent;
         $j->activity_id = $activity->_id;
         $j->iamemptyjob = "yes";
         $extraInfoBatch = array();
         $extraInfoBatch["batchColumnsNewTemplate"] = $batchColumnsNewTemplate;
         $extraInfoBatch["batchColumnsExtraChosenTemplate"] = $batchColumnsExtraChosenTemplate;
         $extraInfoBatch["associationsTemplBatch"] = $associationsTemplBatch;
         $extraInfoBatch["ownTemplate"] = $ownTemplate;
         $j->extraInfoBatch = $extraInfoBatch;
         $j->save();
         //convert to publish later
         $j->publish($ordersandbox == 'sandbox' ? true : false);
         $jobs[] = $j;
         $successmessage = "Created job with jobConf :-)";
         $platform = App::make($job_sw_agent);
         $platform->refreshJob($j->_id);
         Session::flash('flashSuccess', $successmessage);
         return Redirect::to("jobs");
     } catch (Exception $e) {
         // Undo creation and delete jobs
         // if(isset($jobs))
         // foreach($jobs as $j){
         // 	if(isset($j->platformJobId))
         // 		$j->undoCreation($j->platformJobId);
         // 	$j->forceDelete();
         // }
         //delete activity
         if (isset($j)) {
             $j->forceDelete();
         }
         if (isset($jc)) {
             $jc->forceDelete();
         }
         if ($activity) {
             $activity->forceDelete();
         }
         throw $e;
         //for debugging
         Session::flash('flashError', $e->getMessage());
         return Redirect::to("jobs2/submit");
     }
 }
Esempio n. 3
0
 public function postSubmitFinal($ordersandbox = 'order')
 {
     $jc = unserialize(Session::get('jobconf'));
     $template = Session::get('template');
     $batch = unserialize(Session::get('batch'));
     $questiontemplateid = Session::get('questiontemplateid');
     $jobs = array();
     if (!$jc->validate()) {
         $msg = '';
         foreach ($jc->getErrors()->all() as $message) {
             $msg .= "<li>{$message}</li>";
         }
         Session::flash('flashError', "<ul>{$msg}</ul>");
         return Redirect::to("jobs/submit");
     }
     try {
         // Save activity
         $activity = new MongoDB\Activity();
         $activity->label = "Job is uploaded to crowdsourcing platform.";
         $activity->softwareAgent_id = 'jobcreator';
         // JOB softwareAgent_id = $platform. Does this need to be the same?
         $activity->save();
         // Save jobconf if necessary
         $hash = md5(serialize($jc->content));
         if ($existingid = JobConfiguration::where('hash', $hash)->pluck('_id')) {
             $jcid = $existingid;
         } else {
             $jc->format = $batch->format;
             $jc->domain = $batch->domain;
             $jc->hash = $hash;
             $jc->activity_id = $activity->_id;
             $jc->save();
             $jcid = $jc->_id;
         }
         // Publish jobs
         foreach ($jc->content['platform'] as $platformstring) {
             $j = new Job();
             $j->format = $batch->format;
             $j->domain = $batch->domain;
             $j->type = explode('/', $template)[1];
             $j->template = $template;
             // TODO: remove
             $j->batch_id = $batch->_id;
             $j->questionTemplate_id = $questiontemplateid;
             $j->jobConf_id = $jcid;
             $j->softwareAgent_id = $platformstring;
             $j->activity_id = $activity->_id;
             $j->publish($ordersandbox == 'sandbox' ? true : false);
             $jobs[] = $j;
         }
         // Success.
         //Session::flash('flashSuccess', "Created " . ($ordersandbox == 'sandbox' ? 'but didn\'t order' : 'and ordered') . " job(s) on " .
         //				strtoupper(implode(', ', $jc->content['platform'])) . '.');
         $successmessage = "Created job" . (count($jc->content['platform']) > 1 ? 's' : '') . " on " . strtoupper(implode(', ', $jc->content['platform'])) . '. Order it by pressing the button under \'Actions\'. Demo jobs are published on the sandbox or internal channels only.';
         // TODO: this only takes the first job of potentially two
         if (!empty($jobs[0]->url)) {
             $successmessage .= ". After that, you can view it <a href='{$jobs[0]->url}' target='blank'>here</a>.";
         }
         Session::flash('flashSuccess', $successmessage);
         return Redirect::to("jobs/");
         //(Auth::user()->role == 'demo' ? '. Because this is a demo account, you can not order it. Please take a look at our finished jobs!' : '. Click on \'actions\' on the job to order it.')
     } catch (Exception $e) {
         // Undo creation and delete jobs
         if (isset($jobs)) {
             foreach ($jobs as $j) {
                 if (isset($j->platformJobId)) {
                     $j->undoCreation($j->platformJobId);
                 }
                 $j->forceDelete();
             }
         }
         //delete activity
         if ($activity) {
             $activity->forceDelete();
         }
         throw $e;
         //for debugging
         Session::flash('flashError', $e->getMessage());
         return Redirect::to("jobs/submit");
     }
 }