/**
  * Call util to set remove stale records and set UUID, and then setup any
  * table-specific data.
  *
  * @param Doctrine_Event $event
  */
 function preInsert($event)
 {
     trec_make_new($this);
     // table-specific
     $this->out_headline = "Outcome Headline - " . $this->out_uuid;
     $this->out_teaser = "Outcome Teaser - " . $this->out_uuid;
     $this->out_dtim = air2_date();
     parent::preInsert($event);
 }
 /**
  * Create
  *
  * We have to override 'rec_create' instead of 'air_create' here, because
  * Doctrine sucks at pulling the correct data out of the SrcOutcome in the
  * post-insert hooks.
  *
  * @param array $data
  * @return string $uuid
  */
 protected function rec_create($data)
 {
     $this->require_data($data, array('out_headline', 'out_teaser'));
     $rec = new Outcome();
     // organization
     if (isset($data['org_uuid']) && $data['org_uuid']) {
         $o = AIR2_Record::find('Organization', $data['org_uuid']);
         if (!$o) {
             throw new Rframe_Exception(Rframe::BAD_DATA, 'Invalid org_uuid');
         }
         $rec->out_org_id = $o->org_id;
     }
     // project
     if (isset($data['prj_uuid']) && $data['prj_uuid']) {
         $p = AIR2_Record::find('Project', $data['prj_uuid']);
         if (!$p) {
             throw new Rframe_Exception(Rframe::BAD_DATA, 'Invalid prj_uuid');
         }
         $rec->PrjOutcome[]->pout_prj_id = $p->prj_id;
     }
     // inquiry
     if (isset($data['inq_uuid']) && $data['inq_uuid']) {
         $i = AIR2_Record::find('Inquiry', $data['inq_uuid']);
         if (!$i) {
             throw new Rframe_Exception(Rframe::BAD_DATA, 'Invalid inq_uuid');
         }
         $rec->InqOutcome[]->iout_inq_id = $i->inq_id;
     }
     $add_src_ids = array();
     if (isset($data['bin_uuid']) && $data['bin_uuid']) {
         $b = AIR2_Record::find('Bin', $data['bin_uuid']);
         if (!$b) {
             throw new Rframe_Exception(Rframe::BAD_DATA, 'Invalid bin_uuid');
         }
         $bsources = $b->get('BinSource');
         $type = $data['sout_type'];
         if ($bsources) {
             foreach ($bsources->toArray() as $key => $bsource) {
                 $src_id = $bsource['bsrc_src_id'];
                 $add_src_ids[$src_id] = $type;
             }
         }
     }
     // add the sources later, so the activity logging works
     if (isset($data['src_uuid']) && $data['src_uuid']) {
         $s = AIR2_Record::find('Source', $data['src_uuid']);
         if (!$s) {
             throw new Rframe_Exception(Rframe::BAD_DATA, 'Invalid src_uuid');
         }
         $add_src_ids[$s->src_id] = SrcOutcome::$TYPE_INFORMED;
     }
     // process email lists
     $em_tbl = Doctrine::getTable('SrcEmail');
     $list_keys = array('emails' => SrcOutcome::$TYPE_INFORMED, 'emails_cited' => SrcOutcome::$TYPE_CITED, 'emails_featured' => SrcOutcome::$TYPE_FEATURED);
     foreach ($list_keys as $key => $code) {
         if (isset($data[$key])) {
             $parts = explode(',', $data[$key]);
             foreach ($parts as $em) {
                 $em = trim($em);
                 if ($em && strlen(trim($em)) > 0) {
                     $eml_rec = $em_tbl->findOneBy('sem_email', $em);
                     if (!$eml_rec) {
                         throw new Rframe_Exception(Rframe::BAD_DATA, "Unknown email \"{$em}\"");
                     }
                     $add_src_ids[$eml_rec->sem_src_id] = $code;
                 }
             }
         }
     }
     // update and check authz
     foreach ($data as $key => $val) {
         if ($rec->getTable()->hasColumn($key)) {
             $rec->{$key} = $val;
         }
     }
     $this->check_authz($rec, 'write');
     // save and add the sources
     $this->air_save($rec);
     foreach ($add_src_ids as $srcid => $code) {
         $sout = new SrcOutcome();
         $sout->sout_out_id = $rec->out_id;
         $sout->sout_src_id = $srcid;
         $sout->sout_type = $code;
         $this->air_save($sout);
     }
     // send email alert, if set in profile
     if (defined('AIR2_EMAIL_ALERTS')) {
         $this->_send_notification_email($rec);
     }
     // return identifier
     return $rec[$this->ident];
 }
 public function addTask()
 {
     if ($_POST) {
         if (Request::has('id')) {
             $outcome = Outcome::find(Request::get('outcomeid'));
             if (Request::has('add')) {
                 $outcome->addTask(Request::get('id'));
             }
             if (Request::has('remove')) {
                 $outcome->removeTask(Request::get('id'));
             }
             if (Request::has('up')) {
                 $outcome->moveTaskUp(Request::get('id'));
             }
             if (Request::has('down')) {
                 $outcome->moveTaskDown(Request::get('id'));
             }
             if (Request::has('markfinal')) {
                 $outcome->addFinalTask(Request::get('id'));
             }
             if (Request::has('removefinal')) {
                 $outcome->finalTask()->delete(array('task_id' => Request::get('id')));
             }
             return Redirect::to('outcome/addtask')->with('addtask_outcome', serialize($outcome));
         }
     }
     // This is where we will add task/s to outcome
     $outcome = unserialize(Session::get('addtask_outcome'));
     if (is_null($outcome) || !isset($outcome->id)) {
         return Redirect::to('/outcomes');
     }
     $this->layout->content = View::make('outcome.addtask')->with('outcome', $outcome);
 }
 public function fetchPostByID()
 {
     $user = Auth::user();
     $input = Input::all();
     // search for post by ID
     //$searchForPost = DB::table('posts')->where('id', $input['postid'])->get();
     $post = Post::where('id', $input['postid'])->take(1)->get();
     //$jsonOut = json_decode($searchForPost[0]->status);
     $post->status = $post[0]->filterStatus();
     $post->image = ($post[0]->statustype = 'image') ? $post[0]->displayMediaApi() : '';
     //////////
     if ($post->image != '') {
         $doc = new DOMDocument();
         $doc->loadHTML($post->image);
         $xpath = new DOMXPath($doc);
         $src = $xpath->evaluate("string(//img/@src)");
         $post->image = $src;
     }
     /////////
     //$post->mission_completed = true;
     //print_r();
     // set display mission
     if ($post[0]->display_mission == 1) {
         $post->display_mission = true;
     }
     // get the groupid
     $groupid = GroupUser::where('user_id', '=', $user['id'])->pluck('group_id');
     // get the taskid
     $taskid = $post[0]->task_id;
     // get the outcome id
     $group = Group::find($groupid);
     $outcomeid = Outcome::find($group->outcome)->id;
     // execute query
     $userTaskArray = DB::table('user_tasks')->where('outcome_id', $outcomeid)->where('user_id', $user['id'])->where('group_id', $groupid)->where('task_id', $taskid)->where('complete', 1)->get();
     //print_r(count($userTaskArray));
     if (count($userTaskArray) > 0) {
         $post->mission_completed = true;
         $post->general = false;
     } else {
         $post->mission_completed = false;
         $post->general = true;
     }
     return json_encode($post);
 }
 /**
  * This will handle both updating and deleting group
  */
 public function updateGroup()
 {
     if ($_POST) {
         if (Input::has('delete')) {
             $group = Group::find(Input::get('id'));
             if (is_null($group)) {
                 return Redirect::to('groups/manage');
             }
             $group->delete();
             return Redirect::to('groups/manage');
         } elseif (Input::has('edit')) {
             $group = Group::find(Input::get('id'));
             if (is_null($group)) {
                 return Redirect::to('groups/manage');
             }
             return Redirect::to('group/edit')->with('update_group', $group);
         } elseif (Input::has('addremusers')) {
             $group = Group::find(Input::get('id'));
             if (is_null($group)) {
                 return Redirect::to('groups/manage');
             }
             return Redirect::action('GroupController@manageUsers', array('id' => $group->id));
         } elseif (Input::has('update')) {
             $group = Group::find(Input::get('id'));
             if (is_null($group)) {
                 return Redirect::to('groups/manage');
             }
             $group->name = Input::get('name');
             $group->description = Input::get('description');
             $group->timestart = strtotime(Input::get('timestart'));
             $group->outcome = Input::get('outcome');
             $group->survey = Input::get('survey');
             $group->postsurvey = Input::get('postsurvey');
             if (Input::hasFile('thumbnail')) {
                 $file = Input::file('thumbnail');
                 $pixpath = '/uploads/pix/group/';
                 $destinationPath = public_path() . $pixpath;
                 $filename = str_replace(" ", "_", $group->name) . '.' . $file->getClientOriginalExtension();
                 $file->move($destinationPath, $filename);
                 $group->thumbnail = base64_encode($pixpath . $filename);
             }
             $group->save();
             return Redirect::to('groups/manage');
         }
     }
     $group = Session::get('update_group');
     if (is_null($group)) {
         return Redirect::to('groups/manage');
     }
     //Outcome menu
     $outcomes = Outcome::all();
     $outcomes_menu = array();
     $outcomes_menu[0] = trans('master.choose');
     foreach ($outcomes as $outcome) {
         $outcomes_menu[$outcome->id] = trans($outcome->name);
     }
     //Survey menu
     $surveys = Survey::all();
     $surveys_menu = array();
     $surveys_menu[0] = trans('master.choose');
     foreach ($surveys as $survey) {
         $surveys_menu[$survey->id] = trans($survey->name);
     }
     return $this->layout->content = View::make('group.edit')->with('outcomes', $outcomes_menu)->with('surveys', $surveys_menu)->with('group', $group);
 }
<?php

/*
 * Let's just first assume that a user will onely have one group
 * with one and only one outcome
 */
/**
 * These are the variables that have been supplied in the request
 */
$groupid = Request::get('groupid') ?: 1;
$userid = Request::get('userid') ?: 1;
$group = Group::find($groupid);
$outcome = Outcome::find($group->outcome);
$availArray = array_fetch($outcome->tasks->toArray(), 'id');
$currentDay = round((time() - $group->timestart) / 86400, 0);
$doneArray = array();
$doneArray = DB::table('user_tasks')->where('group_id', $groupid)->where('user_id', $userid)->where('complete', 1)->lists('task_id');
$doneArray[] = $outcome->getFinalTask();
$array = array_diff($availArray, $doneArray);
if (empty($array)) {
    if ($currentDay >= 21) {
        $array[20] = $outcome->getFinalTask();
    } else {
        $array = $availArray;
    }
} elseif ($currentDay >= 21) {
    $array = array();
    $array[20] = $outcome->getFinalTask();
}
echo '<pre>';
print_r($array);
 /**
  * This function will be called from the dice page. It will accept the 
  * task that the user has agreed to and will send that to the database
  */
 public function acceptTask()
 {
     if (!$_POST) {
         App::abort(404);
     }
     $user = User::find(Input::get('userid'));
     $group = Group::find(Input::get('groupid'));
     $outcome = Outcome::find($group->outcome);
     $task = Task::find(Input::get('taskid'));
     // TODO: Check if the record already exists in the DB
     DB::table('user_tasks')->insert(array('user_id' => $user->id, 'group_id' => $group->id, 'outcome_id' => $outcome->id, 'task_id' => $task->id, 'created_at' => new DateTime(), 'updated_at' => new DateTime()));
     return Redirect::action('PlayController@showStatusPage', array('id' => $group->id));
 }
 public function getNextTask()
 {
     // Get the current user
     $user = Auth::user();
     // Get the group that the user belongs to
     $group = $user->getGroup();
     // Get the outcome from group
     $outcome = Outcome::find($group->outcome);
     // Now get the remaining task/s for this user in the specified group
     $tasks = $user->getRemainingTasks();
 }