public static function status($feedid, $status, $op)
 {
     // get $db instance
     $db = DB::getInstance();
     $db->lock('feed', 'WRITE');
     // grab the feed
     $feedResult = Mapper::getStatic('Feed', $feedid);
     if (count($feedResult['Feed']) == 0) {
         $db->unlock();
         die('Invalid feed id.');
     }
     # grab old status
     $old_status = $feedResult['Feed'][0]->status;
     $type = gettype($status);
     echo "Performing '{$op}' with value '{$status}' on current status '{$old_status}'\n";
     if ($op == 'inc') {
         // increasing mode
         echo "Increasing status of feed {$feedid} by {$status}... ";
         # increase status
         $status = $old_status + $status;
     }
     if ($op == 'set') {
         // set mode
         if ($old_status >= $status || $status > 100) {
             $db->unlock();
             die("Ignoring setting the status since the old status {$old_status} >= the new status {$status} or the old status >= 100.\n");
         } else {
             echo "Setting status of feed {$feedid} to {$status}... ";
         }
     }
     echo "status now {$status}.\n";
     # clamp the addition
     if ($status >= 100) {
         $status = 100;
         $startTime = $feedResult['Feed'][0]->time;
         $endTime = microtime(true);
         $duration = $endTime - $startTime;
         $feedResult['Feed'][0]->time = $endTime;
         $feedResult['Feed'][0]->duration = $duration;
     }
     # push to database
     $feedResult['Feed'][0]->status = $status;
     Mapper::update($feedResult['Feed'][0], $feedid);
     $db->unlock();
     # update related shared feeds
     $relatedMapper = new Mapper('Feed');
     $relatedMapper->join('Meta', 'Meta.target_id = Feed.id')->filter('Meta.name = (?)', 'root_id')->filter('Meta.value = (?)', $feedResult['Feed'][0]->id)->filter('Feed.id != (?)', $feedResult['Feed'][0]->id);
     $relatedResult = $relatedMapper->get();
     foreach ($relatedResult['Feed'] as $key => $value) {
         $relatedResult['Feed'][$key]->time = $feedResult['Feed'][0]->time;
         $relatedResult['Feed'][$key]->duration = $feedResult['Feed'][0]->duration;
         $relatedResult['Feed'][$key]->status = $feedResult['Feed'][0]->status;
         Mapper::update($relatedResult['Feed'][$key], $relatedResult['Feed'][$key]->id);
     }
     # send email if status == 100
     if ($status == 100) {
         // user's email
         $userMapper = new Mapper('User');
         $userMapper->filter('user.id = (?)', $feedResult['Feed'][0]->user_id);
         $userResult = $userMapper->get();
         // if nothing in DB yet, return -1
         if (count($userResult['User']) > 0) {
             $subject = "ChRIS2 - " . $feedResult['Feed'][0]->plugin . " plugin finished";
             $message = "Hello " . $userResult['User'][0]->username . "," . PHP_EOL . PHP_EOL;
             $message .= "Your results are available at:" . PHP_EOL . PHP_EOL;
             $dirRoot = joinPaths(CHRIS_USERS, $userResult['User'][0]->username, $feedResult['Feed'][0]->plugin, $feedResult['Feed'][0]->name . '-' . $feedResult['Feed'][0]->id);
             $dataDir = array_diff(scandir($dirRoot), array('..', '.'));
             foreach ($dataDir as $dir) {
                 $mailFilePath = $dirRoot . '/' . $dir . '/_chrisRun_/' . 'chris.mail';
                 if (file_exists($mailFilePath)) {
                     $mailContents = file_get_contents($mailFilePath);
                     $message .= $dirRoot . '/' . $dir . PHP_EOL . $mailContents . PHP_EOL . PHP_EOL;
                 } else {
                     $message .= $dirRoot . '/' . $dir . PHP_EOL . PHP_EOL;
                 }
             }
             $message .= "Thank you for using ChRIS.";
             echo "Sending email to " . $userResult['User'][0]->email . " since the status is '{$status}'%.\n";
             // get user email address
             email(CHRIS_PLUGIN_EMAIL_FROM, $userResult['User'][0]->email, $subject, $message);
         }
     }
 }
 public function testUpdate()
 {
     // get a patient by id
     $patientObject = new Patient();
     $patientObject->name = 'PLN2';
     $patientObject->dob = '2002-01-01';
     $patientObject->sex = 'F';
     $patientObject->uid = 'PID2;';
     $patientID = Mapper::add($patientObject);
     // Modify one field
     $patientObject->name = 'PLN3';
     // Update database and get object
     Mapper::update($patientObject, $patientID);
     $patientResult = Mapper::getStatic('Patient', $patientID);
     // compared object we just added with its "base" object
     // we make sure id match
     $patientObject->id = $patientID;
     $this->assertTrue($patientResult['Patient'][0]->equals($patientObject) == True);
     // update "silly" object to create one object which alread exists
     $existingID = Mapper::update($patientObject, -1);
     // should return the id of the object which already exists
     $this->assertTrue($patientID == $existingID);
     //update object that does not exist
     $patientObject->name = 'PLN4';
     $existingID = Mapper::update($patientObject, -1);
     // update should return 0 if object does not exist
     $this->assertTrue($existingID == 0);
     // clean the DB
     Mapper::delete('Patient', $patientID);
 }