/** * Returns a configured mobileWorksApi */ public function getMobileWorksApi() { App::uses('MobileWorks', 'Lib'); $mw = new MobileWorks(); // provide your username/password $mw->username = '******'; $mw->password = '******'; $mw->sandbox(); return $mw; }
/** * Pushes a given task to MobileWorks. The resource is set according * to the evalution type. * * Expects the evaluation id to be the current active record id. * * @param MobileWorks $mobileWorksApi A configured mobileWorksApi * @return void */ public function pushTask($mobileWorksApi, $redundancy, $workflowType, $blocked_users) { $this->read(null, $this->id); // create the callback url dependend of type value if ($this->data['Evaluation']['type'] == Evaluation::$TYPE_ARTICLE_TOPIC) { $callback = Configure::read('domain') . '/evaluations/returnstepone/' . $this->data['Evaluation']['id'] . '.json'; } else { $callback = Configure::read('domain') . '/evaluations/returnsteptwo/' . $this->data['Evaluation']['id'] . '.json'; } // create a project (to get an instant callback, we need a new project for every task) $p = $mobileWorksApi->Project(array('projectid' => Configure::read('version') . $this->data['Evaluation']['id'], 'webhooks' => $callback)); $daysSinceCreation = 0; $price = $this->getPrice(0); //calculate price since initial task $now = time(); $your_date = strtotime($this->data['Evaluation']['created']); $datediff = $now - $your_date; $daysSinceCreation = floor($datediff / (60 * 60 * 24)); $price = $this->getPrice($daysSinceCreation); // create the tasks for ($i = 0; $i < 3; $i++) { $t = $mobileWorksApi->Task(array('taskid' => $this->getMWTaskId($i), 'instructions' => $this->data['Evaluation']['question'], 'workflow' => $workflowType, 'blocked' => $blocked_users, 'payment' => $price)); // build choices if ($this->data['Evaluation']['type'] == Evaluation::$TYPE_ARTICLE_TOPIC) { $t->add_field('result', 'm', array("choices" => "Yes,No")); } else { // titlesentiment or paragraphsentiment $t->add_field('rating', 'm', array("choices" => "-5 (Very Bad),-4,-3,-2,-1,0 (balanced),1,2,3,4,5 (Very positive)")); } // add to project $p->add_task($t); } //otherwise reject the task if ($daysSinceCreation < 15) { // push the project $project_url = $p->post(); // add the task url to the evaluation record $this->saveField('task_url', $project_url); } else { $this->saveField('task_url', "rejected"); } }