コード例 #1
0
ファイル: PollAction.class.php プロジェクト: nick-strohm/WCF
 /**
  * Copies a poll from one object id to another.
  */
 public function copy()
 {
     $sourceObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.poll', $this->parameters['sourceObjectType']);
     $targetObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.poll', $this->parameters['targetObjectType']);
     //
     // step 1) get data
     //
     // get poll
     $sql = "SELECT\t*\n\t\t\tFROM\twcf" . WCF_N . "_poll\n\t\t\tWHERE\tobjectTypeID = ?\n\t\t\t\tAND objectID = ?";
     $statement = WCF::getDB()->prepareStatement($sql);
     $statement->execute(array($sourceObjectType->objectTypeID, $this->parameters['sourceObjectID']));
     $row = $statement->fetchArray();
     if ($row === false) {
         return array('pollID' => null);
     }
     // get options
     $pollOptionList = new PollOptionList();
     $pollOptionList->getConditionBuilder()->add("poll_option.pollID = ?", array($row['pollID']));
     $pollOptionList->readObjects();
     //
     // step 2) copy
     //
     // cretae poll
     $pollData = $row;
     $pollData['objectTypeID'] = $targetObjectType->objectTypeID;
     $pollData['objectID'] = $this->parameters['targetObjectID'];
     unset($pollData['pollID']);
     $newPoll = PollEditor::create($pollData);
     // create options
     $newOptionIDs = array();
     foreach ($pollOptionList as $pollOption) {
         $newOption = PollOptionEditor::create(array('pollID' => $newPoll->pollID, 'optionValue' => $pollOption->optionValue, 'votes' => $pollOption->votes, 'showOrder' => $pollOption->showOrder));
         $newOptionIDs[$pollOption->optionID] = $newOption->optionID;
     }
     // copy votes
     WCF::getDB()->beginTransaction();
     foreach ($newOptionIDs as $oldOptionID => $newOptionID) {
         $sql = "INSERT INTO\twcf" . WCF_N . "_poll_option_vote\n\t\t\t\t\t\t(pollID, optionID, userID)\n\t\t\t\tSELECT\t\t" . $newPoll->pollID . ", " . $newOptionID . ", userID\n\t\t\t\tFROM\t\twcf" . WCF_N . "_poll_option_vote\n\t\t\t\tWHERE\t\toptionID = ?";
         $statement = WCF::getDB()->prepareStatement($sql);
         $statement->execute(array($oldOptionID));
     }
     WCF::getDB()->commitTransaction();
     return array('pollID' => $newPoll->pollID);
 }
コード例 #2
0
ファイル: PollManager.class.php プロジェクト: nick-strohm/WCF
 /**
  * Returns a list of poll options with vote state for current user.
  * 
  * @param	array<integer>		$pollIDs
  * @return	\wcf\data\poll\option\PollOptionList
  */
 public function getPollOptions(array $pollIDs)
 {
     $optionList = new PollOptionList();
     $optionList->getConditionBuilder()->add("poll_option.pollID IN (?)", array($pollIDs));
     // check for user votes
     if (WCF::getUser()->userID) {
         $optionList->sqlSelects = "CASE WHEN poll_option_vote.optionID IS NULL THEN '0' ELSE '1' END AS voted";
         $optionList->sqlJoins = "LEFT JOIN wcf" . WCF_N . "_poll_option_vote poll_option_vote ON (poll_option_vote.optionID = poll_option.optionID AND poll_option_vote.userID = " . WCF::getUser()->userID . ")";
     } else {
         $optionList->sqlSelects = "'0' AS voted";
     }
     $optionList->readObjects();
     return $optionList;
 }