protected function getJSONResponse()
 {
     // remember per-question timestamps to handle race conditions
     if (!isset($this->app->session->quiz_question_timestamp)) {
         $this->app->session->quiz_question_timestamp = new ArrayObject();
     }
     $quiz_question_timestamp = $this->app->session->quiz_question_timestamp;
     $transaction = new SwatDBTransaction($this->app->db);
     try {
         if (!$this->app->session->isLoggedIn()) {
             return $this->getErrorResponse('Not logged in.');
         }
         $account = $this->app->session->account;
         $quiz = $this->getQuiz($this->getProgress($this->getCredits()));
         if (!$quiz instanceof CMEQuiz) {
             return $this->getErrorResponse('Quiz not found.');
         }
         $binding_id = SiteApplication::initVar('binding_id', null, SiteApplication::VAR_POST);
         if ($binding_id === null) {
             return $this->getErrorResponse('Question binding not specified.');
         }
         $binding = $this->getQuestionBinding($quiz, $binding_id);
         $timestamp = SiteApplication::initVar('timestamp', null, SiteApplication::VAR_POST);
         if ($timestamp === null) {
             return $this->getErrorResponse('Timestamp not specified.');
         }
         if (isset($quiz_question_timestamp[$binding_id]) && $timestamp < $quiz_question_timestamp[$binding_id]) {
             return $this->getErrorResponse('Request is out of sequence.');
         }
         $quiz_question_timestamp[$binding_id] = $timestamp;
         $option_id = SiteApplication::initVar('option_id', null, SiteApplication::VAR_POST);
         if ($option_id === null) {
             return $this->getErrorResponse('Response option id not specified.');
         }
         $response = $this->getResponse($quiz);
         $response_value = $this->getResponseValue($quiz, $response, $binding, $option_id);
         if ($response_value === null) {
             return $this->getErrorResponse('Response option id not valid for the specified question.');
         }
         $this->saveResponseValue($response, $response_value);
         $transaction->commit();
     } catch (Exception $e) {
         $transaction->rollback();
         throw $e;
     }
     return array('status' => array('code' => 'ok', 'message' => ''), 'timestamp' => time());
 }
 protected function getJSONResponse()
 {
     $transaction = new SwatDBTransaction($this->app->db);
     try {
         if (!$this->app->session->isLoggedIn()) {
             return $this->getErrorResponse('Not logged in.');
         }
         $account = $this->app->session->account;
         $front_matter = $this->getFrontMatter();
         if (!$front_matter instanceof CMEFrontMatter) {
             return $this->getErrorResponse('CME front matter not found.');
         }
         // only save on a POST request
         if ($_SERVER['REQUEST_METHOD'] === 'POST') {
             $this->saveAccountAttestedCMEFrontMatter($account, $front_matter);
         }
         $transaction->commit();
     } catch (Exception $e) {
         $transaction->rollback();
         throw $e;
     }
     return array('status' => array('code' => 'ok', 'message' => ''));
 }
示例#3
0
文件: Edit.php 项目: GervaisdeM/blorg
 protected function saveBlorgFeedLogo()
 {
     $transaction = new SwatDBTransaction($this->app->db);
     try {
         $file_id = $this->processImage('feed_logo');
         $this->app->config->blorg->feed_logo = $file_id;
         $transaction->commit();
     } catch (SwatDBException $e) {
         $transaction->rollback();
         $message = new SwatMessage(Blorg::_('A database error has occured. The header image was not ' . 'saved.'), 'system-error');
         $this->app->messages->add($message);
         $e->process();
         return false;
     } catch (SwatException $e) {
         $message = new SwatMessage(Blorg::_('An error has occured. The header image was not saved.'), 'system-error');
         $this->app->messages->add($message);
         $e->process();
         return false;
     }
     return true;
 }
示例#4
0
 protected function processDBData()
 {
     parent::processDBData();
     $locale = SwatI18NLocale::get();
     $relocate = true;
     $message = null;
     $delete_count = 0;
     $error_count = 0;
     $newsletters = $this->getNewsletters();
     foreach ($newsletters as $newsletter) {
         // only allow deleting unsent newsletters. There is nothing
         // technically stopping us from deleting ones that have been sent,
         // but do this for the sake of stats until deleting sent newsletters
         // is required.
         if ($newsletter->isSent() == false) {
             $list = $this->getList($newsletter);
             $campaign_type = $newsletter->instance instanceof SiteInstance ? $newsletter->instance->shortname : null;
             $campaign = $newsletter->getCampaign($this->app, $campaign_type);
             $transaction = new SwatDBTransaction($this->app->db);
             try {
                 // If not a draft, remove the resources. Don't delete draft
                 // newsletter resources as they are shared across all
                 // drafts.
                 if ($newsletter->isScheduled()) {
                     DeliveranceCampaign::removeResources($this->app, $campaign);
                 }
                 $list->deleteCampaign($campaign);
                 $sql = 'delete from Newsletter where id = %s';
                 $sql = sprintf($sql, $this->app->db->quote($newsletter->id, 'integer'));
                 $delete_count += SwatDB::exec($this->app->db, $sql);
                 $transaction->commit();
             } catch (DeliveranceAPIConnectionException $e) {
                 $transaction->rollback();
                 $e->processAndContinue();
                 $error_count++;
                 $relocate = false;
             } catch (Exception $e) {
                 $transaction->rollback();
                 $e = new DeliveranceException($e);
                 $e->processAndContinue();
                 $error_count++;
                 $relocate = false;
             }
         }
     }
     if ($delete_count > 0) {
         $message = new SwatMessage(sprintf(Deliverance::ngettext('One newsletter has been deleted.', '%s newsletters have been deleted.', $delete_count), $locale->formatNumber($delete_count)), 'notice');
         $this->app->messages->add($message);
     }
     if ($error_count > 0) {
         $message = new SwatMessage(Deliverance::_('There was an issue connecting to the email service ' . 'provider.'), 'error');
         $message->content_type = 'text/xml';
         $message->secondary_content = sprintf('<strong>%s</strong><br />%s', sprintf(Deliverance::ngettext('One newsletter has not been deleted.', '%s newsletters have not been deleted.', $error_count), $locale->formatNumber($error_count)), Deliverance::ngettext('Connection issues are typically short-lived and ' . 'attempting to delete the newsletter again after a ' . 'delay  will usually be successful.', 'Connection issues are typically short-lived and ' . 'attempting to delete the newsletters again after a ' . 'delay will usually be successful.', $error_count));
     }
     return $relocate;
 }
示例#5
0
    /**
     * Applies this tag to a photo
     *
     * Any unsaved changes to the tag and photo are saved before this tag is
     * applied to the photo.
     *
     * @param PinholePhoto $photo the photo this tag is to be applied to.
     */
    public function applyToPhoto(PinholePhoto $photo)
    {
        $transaction = new SwatDBTransaction($this->db);
        try {
            // save photo and tag
            $photo->save();
            $this->save();
            // save binding
            $sql = sprintf('insert into PhotoPhotoTagBinding (photo, tag)
				values (%s, %s)', $this->db->quote($photo->id, 'integer'), $this->db->quote($this->id, 'integer'));
            SwatDB::exec($this->db, $sql);
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollback();
            throw $e;
        }
        $this->photos->add($photo);
    }
示例#6
0
    protected function publishPosts(SwatViewSelection $post_ids)
    {
        $num = 0;
        $transaction = new SwatDBTransaction($this->app->db);
        try {
            $instance_id = $this->app->getInstanceId();
            $post_ids = SwatDB::implodeSelection($this->app->db, $post_ids);
            $sql = sprintf('select id, shortname, title, bodytext from BlorgPost
				where instance %s %s and id in (%s) and enabled = %s', SwatDB::equalityOperator($instance_id), $this->app->db->quote($instance_id, 'integer'), $post_ids, $this->app->db->quote(false, 'boolean'));
            $posts = SwatDB::query($this->app->db, $sql, SwatDBClassMap::get('BlorgPostWrapper'));
            foreach ($posts as $post) {
                if ($post->shortname === null) {
                    $post->shortname = $this->getPostShortname($post);
                }
                $post->enabled = true;
                $post->save();
                $num++;
            }
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollback();
            throw $e;
        }
        if ($num > 0 && isset($this->app->memcache)) {
            $this->app->memcache->flushNs('posts');
        }
        $message = new SwatMessage(sprintf(Blorg::ngettext('One post has been shown on site.', '%s posts have been shown on site.', $num), SwatString::numberFormat($num)));
    }
 /**
  * Enqueues a batch of subscription update requests for this list
  *
  * No duplicate updates are added to the queue. This prevents the queue
  * from growing exponentially if list updates are unavailable for a long
  * time.
  *
  * @param array $addresses
  *
  * @return integer status code for a queued response.
  */
 public function queueBatchUpdate(array $addresses)
 {
     $transaction = new SwatDBTransaction($this->app->db);
     try {
         foreach ($addresses as $info) {
             $this->queueUpdate($info['email'], $info);
         }
         $transaction->commit();
     } catch (SwatDBException $e) {
         $transaction->rollback();
         throw $e;
     }
     return DeliveranceList::QUEUED;
 }