Ejemplo n.º 1
0
 function insert()
 {
     // hack: exclude the t field because its a timestamp. bit of a hack
     //dbRowObject::$debug = true;
     // todo: make this work
     //unset($this->nonidfields['t']);
     //echo '<pre>'.print_r($this->nonidfields, true) . '</pre>';
     parent::insert();
     //$this->nonidfields['t']='';
 }
 function __construct($db, $tablename, $fieldnames, $idname)
 {
     parent::__construct($db, $tablename, $fieldnames, $idname);
 }
 function submitAutomaticChallenge($userid, $shortname, &$code, $dontLog, $logid)
 {
     // create the cc object, then call the approval mechanism
     $evidence = 'Automatic!';
     require_once PATH_CORE . '/classes/user.class.php';
     $challengeTable = new ChallengeTable($db);
     $userTable = new UserTable($db);
     $userInfoTable = new UserInfoTable($db);
     $completedTable = $this;
     $user = $userTable->getRowObject();
     $userInfo = $userInfoTable->getRowObject();
     $challenge = $challengeTable->getRowObject();
     $completed = $completedTable->getRowObject();
     //dbRowObject::$debug =1;
     if (!($user->load($userid) && $userInfo->load($userid) && $challenge->loadWhere("shortName='{$shortname}'"))) {
         $code .= "Bad user: {$userid} or bad challenge: {$challengeid}. ";
         return false;
     }
     // validate challenge submission info
     // validate eligibility, date, membership
     if ($challenge->remainingCompletions <= 0 && $challenge->initialCompletions != 0) {
         $code = 'Insufficient completions.';
         return false;
     }
     if (!ChallengeTable::userIsEligible($challenge->eligibility, $user->eligibility)) {
         $code = 'User not eligible.';
         return false;
     }
     if (!$evidence != '') {
         $code = 'Evidence was blank';
         return false;
     }
     //if () //  TODO: now is between date start and end
     $now = time();
     $dateStart = strtotime($challenge->dateStart);
     $dateEnd = strtotime($challenge->dateEnd);
     if ($now > $dateEnd) {
         $code = 'Sorry, you are too late!';
         return false;
     }
     if ($now < $dateStart) {
         $code = 'Sorry, you are too early';
         return false;
     }
     // if () TODO: check user maximum by querying order histor
     // more...
     // everythings ok:
     $challenge->remainingCompletions--;
     $completed->userid = $user->userid;
     $completed->challengeid = $challenge->id;
     $completed->dateSubmitted = date('Y-m-d H:i:s', time());
     $completed->status = 'submitted';
     $completed->evidence = $evidence;
     $completed->logid = $logid;
     // have to have a completed id to attach to the media records...
     $completed->insert();
     $challenge->update();
     $code .= 'Automatic Challenge Application #' . $completed->id . ' submitted.';
     dbRowObject::$debug = 0;
     // now approve
     return $this->approveChallenge($completed->id, $challenge->pointValue, &$code, $dontLog);
 }
 static function processChallengeSubmit(&$code, &$passback)
 {
     //$debug = $_POST['debugSubmit']; // NEVER TURN ON FOR LIVE SITE
     echo '<h2>Processing, please wait...</h2>';
     if ($debug) {
         echo "POST<pre>" . print_r($_POST, true) . "</pre>";
     }
     // TODO: grab session keys from post, validate session
     $passback .= "&text={$_POST['text']}";
     $passback .= "&embedCode={$_POST['embedCode']}";
     if ($debug) {
         echo $passback;
     }
     if (isset($_POST['challengeid']) && $_POST['challengeid']) {
         $challengeid = $_POST['challengeid'];
     } else {
         $code = "There was no challenge id present in your submission";
         return false;
     }
     if (isset($_POST['text'])) {
         $evidence = $_POST['text'];
     } else {
         $evidence = '';
         /*$code = "Your text submission was empty."; return false;*/
     }
     if (isset($_POST['userid']) && $_POST['userid']) {
         $userid = $_POST['userid'];
     } else {
         $code = 'Either you aren\'t a registered user or your session is expired. Please return to the home page or sign in to facebook again.';
         return false;
     }
     require_once PATH_CORE . '/classes/db.class.php';
     $db = new cloudDatabase();
     // create a CompletedChallenges object
     //$userid = $this->page->session->userid;
     /*	if (!$userid)
     		{
     			echo "<pre>" . print_r($this->page->session, true) . "</pre>";	
     			
     			return "Could not get userid from session."; 
     		}*/
     require_once PATH_CORE . '/classes/user.class.php';
     require_once PATH_CORE . '/classes/challenges.class.php';
     $challengeTable = new ChallengeTable($db);
     $userTable = new UserTable($db);
     $userInfoTable = new UserInfoTable($db);
     $completedTable = new ChallengeCompletedTable($db);
     $user = $userTable->getRowObject();
     $userInfo = $userInfoTable->getRowObject();
     $challenge = $challengeTable->getRowObject();
     $completed = $completedTable->getRowObject();
     dbRowObject::$debug = $debug;
     $user->load($userid);
     $userInfo->load($userid);
     $challenge->load($challengeid);
     // validate challenge submission info
     // validate eligibility, date, membership
     if ($challenge->remainingCompletions <= 0 && $challenge->initialCompletions > 0) {
         $code = 'This challenge can no longer be completed for credit.';
         return false;
     }
     if (!ChallengeTable::userIsEligible($challenge->eligibility, $user->eligibility)) {
         $code = 'We\'re sorry, you are not eligible to receive credit for this challenge.';
         return false;
     }
     if (preg_match("/text/i", $challenge->requires) && !($evidence != '')) {
         $code = 'Sorry, you need to convince us you actually did this!';
         return false;
     }
     //if () //  TODO: now is between date start and end
     $now = time();
     $dateStart = strtotime($challenge->dateStart);
     $dateEnd = strtotime($challenge->dateEnd);
     if ($now > $dateEnd) {
         $code = 'Sorry, you are too late to receive credit for this challenge!';
         return false;
     }
     if ($now < $dateStart) {
         $code = 'Sorry, you can\'t receive credit for this challenge yet -- try again later!';
         return false;
     }
     // if () TODO: check user maximum by querying order histor
     // more...
     // everythings ok:
     $challenge->remainingCompletions--;
     $completed->userid = $user->userid;
     $completed->challengeid = $challenge->id;
     $phpnow = time();
     $completed->dateSubmitted = date('Y-m-d H:i:s', $phpnow);
     $completed->status = 'submitted';
     $completed->evidence = $evidence;
     $completed->comments = $evidence;
     // editors will review these later
     /*
      * The following code is a bit tricky. There are two things going on. The first is that photos
      * or videos are being checked for and their records are being created, but we have 
      * tentatively created a CompleteChallenge record first so they can back-reference it
      * 
      * If a required photo or video turns out not to have appeared, we have to then return an error 
      * and delete the CC record.
      * 
      * An extra wrinkle is that if both video and photo are in the requires field, we can accept 
      * one or the other.
      * 
      */
     // Create the completed to attach to the media records...
     if (!$completed->insert()) {
         $code = 'Internal error submitting your evidence, please try again.';
         return false;
     }
     if (preg_match("/photo/i", $challenge->requires) || preg_match("/optionalphoto/i", $challenge->requires)) {
         if ($photoFilename = self::processChallengeSubmitPhoto($userid, $completed->id)) {
             if (!preg_match("/\\.(jpg|png|gif|jpeg?)\$/i", $photoFilename)) {
                 $msg = 'Sorry, your photo did not appear to be of type jpg, png, or gif.';
                 $error = true;
             } else {
                 // create photo in our db
                 require_once PATH_CORE . '/classes/photo.class.php';
                 $photoTable = new PhotoTable($db);
                 $photoTable->createPhotoForCompletedChallenge($userid, $completed->id, $photoFilename, 'Photo submitted for ' . $challenge->title);
                 $photoSubmitted = true;
                 // indicate that a photo was found
             }
         } else {
             if (!preg_match("/optionalphoto/i", $challenge->requires)) {
                 $msg = 'No photo submitted';
                 $error = true;
             }
         }
     }
     //$passback .= "&photo1={$photoFilename}"; // wont be correct filename, actually quite complicated to make this work properly
     //echo $passback;
     if (preg_match("/video/i", $challenge->requires)) {
         if ($videoEmbedCode = self::processChallengeSubmitVideo($userid, $completed->id)) {
             // create photo in our db
             require_once PATH_CORE . '/classes/video.class.php';
             $videoTable = new VideoTable($db);
             $videoTable->createVideoForCompletedChallenge($userid, $completed->id, $videoEmbedCode, 'Video submitted for ' . $challenge->title);
             $videoSubmitted = true;
             // indicate video found
         } else {
             $msg = 'You must enter a YouTube video url.';
             $error = true;
         }
     }
     // HACK: now handle the case where both photo and video boxes appeared, and only one was entered
     if ($photoSubmitted or $videoSubmitted) {
         $error = false;
     }
     // set the $error flag set by the other one to false if one of them was sucessfully created
     if ($error) {
         $completed->delete();
         // delete the temporary CC record
         $code = $msg;
         return false;
     }
     $challenge->update();
     require_once PATH_CORE . '/classes/template.class.php';
     $code .= 'We have received your submission for the challenge <b>' . $challenge->title . '</b>  (reference number #' . $completed->id . ')';
     dbRowObject::$debug = 0;
     // NEVER TURN ON FOR LIVE SITE
     // for testing purposes -- approve free points right away
     if ($challenge->shortName == 'testPoints10k') {
         $code2 = '';
         if (!$completedTable->approveChallenge($completed->id, $challenge->pointValue, &$code2, false)) {
             $code = $code2;
             return false;
         } else {
             $code = 'Free points awarded!';
         }
     }
     return true;
 }
 function processFormUpdateDatabase($fdata)
 {
     // TODO: update data tables based on form data which is presumably now validated
     if ($debug) {
         echo 'Submitted form data ok: <pre>' . print_r($fdata, true) . '</pre>';
     }
     // if theres a problem, we can set $fdata->alert and return it
     // TODO: assume session valid and all that jazz....
     require_once PATH_CORE . '/classes/user.class.php';
     $userTable = new UserTable($this->db);
     $userInfoTable = new UserInfoTable($this->db);
     $user = $userTable->getRowObject();
     $userinfo = $userInfoTable->getRowObject();
     dbRowObject::$debug = 0;
     // NEVER TURN ON FOR LIVE SITE
     if (!$user->load($this->session->userid) || !$userinfo->load($this->session->userid)) {
         $fdata->alert = 'Fatal error: userid not found in database';
         $fdata->result = false;
         echo 'Error loading user table entries.';
         return $fdata;
     }
     $userinfo->gender = $fdata->gender;
     $userinfo->age = $fdata->age;
     $userinfo->city = $fdata->city;
     $userinfo->state = $fdata->state;
     $userinfo->country = $fdata->country;
     if ($fdata->zip != '') {
         $userinfo->zip = $fdata->zip;
     }
     // safe overwrite only if not empty string
     $userinfo->researchImportance = $fdata->researchImportance;
     $userinfo->noCommentNotify = $fdata->noCommentNotify;
     //$userinfo->birthdate = ''; // TODO
     $user->name = $fdata->name;
     $user->email = $fdata->email;
     $user->optInStudy = $fdata->optInStudy;
     $user->optInEmail = $fdata->optInEmail;
     // wrong?
     $user->acceptRules = $fdata->acceptRules;
     //		$user->optInSMS = $fdata->optInSMS;
     //		$user->optInProfile = $fdata->optInProfile;
     //		$user->optInFeed = $fdata->optInFeed;
     if (!$user->isMember) {
         $user->dateRegistered = date('Y-m-d H:i:s', time());
     }
     $user->isMember = 1;
     // by virtue of executing signup you are a member. not reversible?
     $user->eligibility = $this->checkEligibility($user, $userinfo);
     $user->update();
     $userinfo->update();
     return $fdata;
 }
 function debugFetchUserInfo($userid)
 {
     require_once PATH_CORE . '/classes/user.class.php';
     $userTable = new UserTable($this->db);
     $userInfoTable = new UserInfoTable($this->db);
     dbRowObject::$debug = 1;
     // NEVER TURN ON FOR LIVE SITE
     $user = $userTable->getRowObject();
     $userinfo = $userInfoTable->getRowObject();
     if (!$user->load($userid) || !$userinfo->load($userid)) {
         $code .= 'Couldnt load user or userinfo for userid=' . $userid;
         return;
     }
     $code .= 'User: '******'UserInfo:' . $userinfo->debugPrint() . '';
     return $code;
 }