Пример #1
0
 /**
  * Unvote a node
  *
  * @param int $nodeid Node ID.
  * @return array New Node info.
  * @see vB_Api_Node::getNode()
  * @throws vB_Exception_Api
  */
 public function unvote($nodeid)
 {
     $node = vB_Api::instanceInternal('node')->getNodeFullContent($nodeid);
     $node = $node[$nodeid];
     $this->checkCanUseRep($node);
     $loginuser =& vB::getCurrentSession()->fetch_userinfo();
     if ($node['userid'] == $loginuser['userid']) {
         // Can't vote own node
         throw new vB_Exception_Api('reputationownpost');
     }
     // Check if the user has already reputation this node
     $existingreputation = $this->assertor->getRow('vBForum:reputation', array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT, 'nodeid' => $node['nodeid'], 'whoadded' => $loginuser['userid']));
     if (!$existingreputation) {
         throw new vB_Exception_Api('reputationnovote');
     }
     $userinfo = vB_Api::instanceInternal('user')->fetchUserinfo($node['userid']);
     if (!$userinfo['userid']) {
         throw new vB_Exception_Api('invalidid', 'User');
     }
     $usergroupcache = vB::getDatastore()->getValue('usergroupcache');
     $bf_ugp_genericoptions = vB::getDatastore()->getValue('bf_ugp_genericoptions');
     if (!($usergroupcache["{$userinfo['usergroupid']}"]['genericoptions'] & $bf_ugp_genericoptions['isnotbannedgroup'])) {
         throw new vB_Exception_Api('reputationbanned');
     }
     $userinfo['reputation'] -= $existingreputation['reputation'];
     // Determine this user's reputationlevelid.
     $reputationlevelid = $this->assertor->getField('vBForum:reputation_userreputationlevel', array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_STORED, 'reputation' => $userinfo['reputation']));
     // init user data manager
     $userdata = new vB_Datamanager_User(NULL, vB_DataManager_Constants::ERRTYPE_STANDARD);
     $userdata->set_existing($userinfo);
     $userdata->set('reputation', $userinfo['reputation']);
     $userdata->set('reputationlevelid', intval($reputationlevelid));
     $userdata->pre_save();
     // Delete existing vote
     $this->assertor->assertQuery('vBForum:reputation', array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_DELETE, 'reputationid' => $existingreputation['reputationid']));
     $userdata->save();
     $condition = array('nodeid' => $nodeid);
     $this->assertor->assertQuery('vBForum:updateNodeVotes', $condition);
     $votesCount = $this->assertor->getField('vBForum:node', array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT, vB_dB_Query::COLUMNS_KEY => array('votes'), vB_dB_Query::CONDITIONS_KEY => $condition));
     if ($votesCount == 0) {
         /*
         					TODO: add test for below code
         */
         /*
         				TODO Add a new delete event for LikedNode and replace below with a dismiss event!
         */
         // we need to remove the notification
         $typesByTypename = vB_Library::instance('notification')->getNotificationTypes();
         $typeid = $typesByTypename[vB_Notification_LikedNode::TYPENAME]['typeid'];
         $notificationInfo = array('recipient' => $node['userid'], 'sentbynodeid' => $nodeid, 'typeid' => $typeid);
         $notification = $this->assertor->getRow('vBForum:notification', $notificationInfo);
         if (isset($notification['notificationid'])) {
             vB_Library::instance('notification')->deleteNotification($notification['notificationid']);
         }
     }
     // Expire node cache so this like displays correctly
     vB_Cache::instance()->allCacheEvent('nodeChg_' . $nodeid);
     return array('nodeid' => $nodeid, 'votes' => $votesCount);
 }