PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/SocialProfile/UserGifts/UserGiftsClass.php

http://semantic-social-profile.googlecode.com/
PHP | 435 lines | 293 code | 37 blank | 105 comment | 22 complexity | d8c37ceea5f864c2d80f54eea53c135a MD5 | raw file
  1. <?php
  2. /**
  3. * UserGifts class
  4. * @todo document
  5. */
  6. class UserGifts {
  7. /**
  8. * All member variables should be considered private
  9. * Please use the accessor functions
  10. */
  11. /**#@+
  12. * @private
  13. */
  14. var $user_id; # Text form (spaces not underscores) of the main part
  15. var $user_name; # Text form (spaces not underscores) of the main part
  16. /**
  17. * Constructor
  18. * @private
  19. */
  20. /* private */ function __construct( $username ) {
  21. $title1 = Title::newFromDBkey( $username );
  22. $this->user_name = $title1->getText();
  23. $this->user_id = User::idFromName( $this->user_name );
  24. }
  25. /**
  26. * Sends a gift to the specified user.
  27. *
  28. * @param $user_to Integer: user ID of the recipient
  29. * @param $gift_id Integer: gift ID number
  30. * @param $type Integer: gift type
  31. * @param $message Mixed: message as supplied by the sender
  32. */
  33. public function sendGift( $user_to, $gift_id, $type, $message ) {
  34. $user_id_to = User::idFromName( $user_to );
  35. $dbw = wfGetDB( DB_MASTER );
  36. $dbw->insert(
  37. 'user_gift',
  38. array(
  39. 'ug_gift_id' => $gift_id,
  40. 'ug_user_id_from' => $this->user_id,
  41. 'ug_user_name_from' => $this->user_name,
  42. 'ug_user_id_to' => $user_id_to,
  43. 'ug_user_name_to' => $user_to,
  44. 'ug_type' => $type,
  45. 'ug_status' => 1,
  46. 'ug_message' => $message,
  47. 'ug_date' => date( 'Y-m-d H:i:s' ),
  48. ), __METHOD__
  49. );
  50. $ug_gift_id = $dbw->insertId();
  51. $this->incGiftGivenCount( $gift_id );
  52. $this->sendGiftNotificationEmail( $user_id_to, $this->user_name, $gift_id, $type );
  53. // Add to new gift count cache for receiving user
  54. $this->incNewGiftCount( $user_id_to );
  55. $stats = new UserStatsTrack( $user_id_to, $user_to );
  56. $stats->incStatField( 'gift_rec' );
  57. $stats = new UserStatsTrack( $this->user_id, $this->user_name );
  58. $stats->incStatField( 'gift_sent' );
  59. return $ug_gift_id;
  60. }
  61. /**
  62. * Sends the notification about a new gift to the user who received the
  63. * gift, if the user wants notifications about new gifts and their e-mail
  64. * is confirmed.
  65. *
  66. * @param $user_id_to Integer: user ID of the receiver of the gift
  67. * @param $user_from Mixed: name of the user who sent the gift
  68. * @param $gift_id Integer: ID number of the given gift
  69. * @param $type Integer: gift type; unused
  70. */
  71. public function sendGiftNotificationEmail( $user_id_to, $user_from, $gift_id, $type ) {
  72. $gift = Gifts::getGift( $gift_id );
  73. $user = User::newFromId( $user_id_to );
  74. $user->loadFromDatabase();
  75. if ( $user->isEmailConfirmed() && $user->getIntOption( 'notifygift', 1 ) ) {
  76. $giftsLink = SpecialPage::getTitleFor( 'ViewGifts' );
  77. $updateProfileLink = SpecialPage::getTitleFor( 'UpdateProfile' );
  78. if ( trim( $user->getRealName() ) ) {
  79. $name = $user->getRealName();
  80. } else {
  81. $name = $user->getName();
  82. }
  83. $subject = wfMsgExt( 'gift_received_subject', 'parsemag',
  84. $user_from,
  85. $gift['gift_name']
  86. );
  87. $body = wfMsgExt( 'gift_received_body', 'parsemag',
  88. $name,
  89. $user_from,
  90. $gift['gift_name'],
  91. $giftsLink->getFullURL(),
  92. $updateProfileLink->getFullURL()
  93. );
  94. $user->sendMail( $subject, $body );
  95. }
  96. }
  97. public function clearAllUserGiftStatus() {
  98. $dbw = wfGetDB( DB_MASTER );
  99. $dbw->update( 'user_gift',
  100. /* SET */array( 'ug_status' => 0 ),
  101. /* WHERE */array( 'ug_user_id_to' => $this->user_id ),
  102. __METHOD__
  103. );
  104. $this->clearNewGiftCountCache( $this->user_id );
  105. }
  106. static function clearUserGiftStatus( $id ) {
  107. $dbw = wfGetDB( DB_MASTER );
  108. $dbw->update( 'user_gift',
  109. /* SET */array( 'ug_status' => 0 ),
  110. /* WHERE */array( 'ug_id' => $id ),
  111. __METHOD__
  112. );
  113. }
  114. /**
  115. * Checks if a given user owns the gift, which is specified by its ID.
  116. *
  117. * @param $user_id Integer: user ID of the given user
  118. * @param $ug_id Integer: ID number of the gift that we're checking
  119. * @return Boolean: true if the user owns the gift, otherwise false
  120. */
  121. public function doesUserOwnGift( $user_id, $ug_id ) {
  122. $dbr = wfGetDB( DB_SLAVE );
  123. $s = $dbr->selectRow(
  124. 'user_gift',
  125. array( 'ug_user_id_to' ),
  126. array( 'ug_id' => $ug_id ),
  127. __METHOD__
  128. );
  129. if ( $s !== false ) {
  130. if ( $user_id == $s->ug_user_id_to ) {
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. /**
  137. * Deletes a gift from the user_gift table.
  138. *
  139. * @param $ug_id Integer: ID number of the gift to delete
  140. */
  141. static function deleteGift( $ug_id ) {
  142. $dbw = wfGetDB( DB_MASTER );
  143. $dbw->delete( 'user_gift', array( 'ug_id' => $ug_id ), __METHOD__ );
  144. }
  145. /**
  146. * Gets the user gift with the ID = $id.
  147. *
  148. * @param $id Integer: gift ID number
  149. * @return Array: array containing gift info, such as its ID, sender, etc.
  150. */
  151. static function getUserGift( $id ) {
  152. if ( !is_numeric( $id ) ) {
  153. return '';
  154. }
  155. $dbr = wfGetDB( DB_SLAVE );
  156. $res = $dbr->select(
  157. array( 'user_gift', 'gift' ),
  158. array(
  159. 'ug_id', 'ug_user_id_from', 'ug_user_name_from',
  160. 'ug_user_id_to', 'ug_user_name_to', 'ug_message', 'gift_id',
  161. 'ug_date', 'ug_status', 'gift_name', 'gift_description',
  162. 'gift_given_count'
  163. ),
  164. array( "ug_id = {$id}" ),
  165. __METHOD__,
  166. array( 'LIMIT' => 1, 'OFFSET' => 0 ),
  167. array( 'gift' => array( 'INNER JOIN', 'ug_gift_id = gift_id' ) )
  168. );
  169. $row = $dbr->fetchObject( $res );
  170. if ( $row ) {
  171. $gift['id'] = $row->ug_id;
  172. $gift['user_id_from'] = $row->ug_user_id_from;
  173. $gift['user_name_from'] = $row->ug_user_name_from;
  174. $gift['user_id_to'] = $row->ug_user_id_to;
  175. $gift['user_name_to'] = $row->ug_user_name_to;
  176. $gift['message'] = $row->ug_message;
  177. $gift['gift_count'] = $row->gift_given_count;
  178. $gift['timestamp'] = $row->ug_date;
  179. $gift['gift_id'] = $row->gift_id;
  180. $gift['name'] = $row->gift_name;
  181. $gift['description'] = $row->gift_description;
  182. $gift['status'] = $row->ug_status;
  183. }
  184. return $gift;
  185. }
  186. /**
  187. * Increase the amount of new gifts for the user with ID = $user_id.
  188. *
  189. * @param $user_id Integer: user ID for the user whose gift count we're
  190. * going to increase.
  191. */
  192. public function incNewGiftCount( $user_id ) {
  193. global $wgMemc;
  194. $key = wfMemcKey( 'user_gifts', 'new_count', $user_id );
  195. $wgMemc->incr( $key );
  196. }
  197. /**
  198. * Decrease the amount of new gifts for the user with ID = $user_id.
  199. *
  200. * @param $user_id Integer: user ID for the user whose gift count we're
  201. * going to decrease.
  202. */
  203. public function decNewGiftCount( $user_id ) {
  204. global $wgMemc;
  205. $key = wfMemcKey( 'user_gifts', 'new_count', $user_id );
  206. $wgMemc->decr( $key );
  207. }
  208. /**
  209. * Clear the new gift counter for the user with ID = $user_id.
  210. * This is done by setting the value of the memcached key to 0.
  211. */
  212. public function clearNewGiftCountCache() {
  213. global $wgMemc;
  214. $key = wfMemcKey( 'user_gifts', 'new_count', $this->user_id );
  215. $wgMemc->set( $key, 0 );
  216. }
  217. /**
  218. * Get the amount of new gifts for the user with ID = $user_id
  219. * from memcached. If successful, returns the amount of new gifts.
  220. *
  221. * @param $user_id Integer: user ID for the user whose gifts we're going to
  222. * fetch.
  223. * @return Integer: amount of new gifts
  224. */
  225. static function getNewGiftCountCache( $user_id ) {
  226. global $wgMemc;
  227. $key = wfMemcKey( 'user_gifts', 'new_count', $user_id );
  228. $data = $wgMemc->get( $key );
  229. if ( $data != '' ) {
  230. wfDebug( "Got new gift count of $data for id $user_id from cache\n" );
  231. return $data;
  232. }
  233. }
  234. /**
  235. * Get the amount of new gifts for the user with ID = $user_id.
  236. * First tries cache (memcached) and if that succeeds, returns the cached
  237. * data. If that fails, the count is fetched from the database.
  238. * UserWelcome.php calls this function.
  239. *
  240. * @param $user_id Integer: user ID for the user whose gifts we're going to
  241. * fetch.
  242. * @return Integer: amount of new gifts
  243. */
  244. static function getNewGiftCount( $user_id ) {
  245. $data = self::getNewGiftCountCache( $user_id );
  246. if ( $data != '' ) {
  247. $count = $data;
  248. } else {
  249. $count = self::getNewGiftCountDB( $user_id );
  250. }
  251. return $count;
  252. }
  253. /**
  254. * Get the amount of new gifts for the user with ID = $user_id from the
  255. * database and stores it in memcached.
  256. *
  257. * @param $user_id Integer: user ID for the user whose gifts we're going to
  258. * fetch.
  259. * @return Integer: amount of new gifts
  260. */
  261. static function getNewGiftCountDB( $user_id ) {
  262. wfDebug( "Got new gift count for id $user_id from DB\n" );
  263. global $wgMemc;
  264. $key = wfMemcKey( 'user_gifts', 'new_count', $user_id );
  265. $dbr = wfGetDB( DB_SLAVE );
  266. $newGiftCount = 0;
  267. $s = $dbr->selectRow(
  268. 'user_gift',
  269. array( 'COUNT(*) AS count' ),
  270. array( 'ug_user_id_to' => $user_id, 'ug_status' => 1 ),
  271. __METHOD__
  272. );
  273. if ( $s !== false ) {
  274. $newGiftCount = $s->count;
  275. }
  276. $wgMemc->set( $key, $newGiftCount );
  277. return $newGiftCount;
  278. }
  279. public function getUserGiftList( $type, $limit = 0, $page = 0 ) {
  280. $dbr = wfGetDB( DB_SLAVE );
  281. $params = array();
  282. if ( $limit > 0 ) {
  283. $limitvalue = 0;
  284. if ( $page ) {
  285. $limitvalue = $page * $limit - ( $limit );
  286. }
  287. $params['LIMIT'] = $limit;
  288. $params['OFFSET'] = $limitvalue;
  289. }
  290. $params['ORDER BY'] = 'ug_id DESC';
  291. $res = $dbr->select(
  292. array( 'user_gift', 'gift' ),
  293. array(
  294. 'ug_id', 'ug_user_id_from', 'ug_user_name_from', 'ug_gift_id',
  295. 'ug_date', 'ug_status', 'gift_name', 'gift_description',
  296. 'gift_given_count', 'UNIX_TIMESTAMP(ug_date) AS unix_time'
  297. ),
  298. array( "ug_user_id_to = {$this->user_id}" ),
  299. __METHOD__,
  300. $params,
  301. array( 'gift' => array( 'INNER JOIN', 'ug_gift_id = gift_id' ) )
  302. );
  303. $requests = array();
  304. foreach ( $res as $row ) {
  305. $requests[] = array(
  306. 'id' => $row->ug_id,
  307. 'gift_id' => $row->ug_gift_id,
  308. 'timestamp' => ( $row->ug_date ),
  309. 'status' => $row->ug_status,
  310. 'user_id_from' => $row->ug_user_id_from,
  311. 'user_name_from' => $row->ug_user_name_from,
  312. 'gift_name' => $row->gift_name,
  313. 'gift_description' => $row->gift_description,
  314. 'gift_given_count' => $row->gift_given_count,
  315. 'unix_timestamp' => $row->unix_time
  316. );
  317. }
  318. return $requests;
  319. }
  320. public function getAllGiftList( $limit = 10, $page = 0 ) {
  321. $dbr = wfGetDB( DB_SLAVE );
  322. $params = array();
  323. $params['ORDER BY'] = 'ug_id DESC';
  324. if ( $limit > 0 ) {
  325. $limitvalue = 0;
  326. if ( $page ) {
  327. $limitvalue = $page * $limit - ( $limit );
  328. }
  329. $params['LIMIT'] = $limit;
  330. $params['OFFSET'] = $limitvalue;
  331. }
  332. $res = $dbr->select(
  333. array( 'user_gift', 'gift' ),
  334. array(
  335. 'ug_id', 'ug_user_id_from', 'ug_user_name_from', 'ug_gift_id',
  336. 'ug_date', 'ug_status', 'gift_name', 'gift_description',
  337. 'gift_given_count', 'UNIX_TIMESTAMP(ug_date) AS unix_time'
  338. ),
  339. array(),
  340. __METHOD__,
  341. $params,
  342. array( 'gift' => array( 'INNER JOIN', 'ug_gift_id = gift_id' ) )
  343. );
  344. $requests = array();
  345. foreach ( $res as $row ) {
  346. $requests[] = array(
  347. 'id' => $row->ug_id,
  348. 'gift_id' => $row->ug_gift_id,
  349. 'timestamp' => ( $row->ug_date ),
  350. 'status' => $row->ug_status,
  351. 'user_id_from' => $row->ug_user_id_from,
  352. 'user_name_from' => $row->ug_user_name_from,
  353. 'gift_name' => $row->gift_name,
  354. 'gift_description' => $row->gift_description,
  355. 'gift_given_count' => $row->gift_given_count,
  356. 'unix_timestamp' => $row->unix_time
  357. );
  358. }
  359. return $requests;
  360. }
  361. /**
  362. * Update the counter that tracks how many times a gift has been given out.
  363. *
  364. * @param $gift_id Integer: ID number of the gift that we're tracking
  365. */
  366. private function incGiftGivenCount( $gift_id ) {
  367. $dbw = wfGetDB( DB_MASTER );
  368. $dbw->update(
  369. 'gift',
  370. array( 'gift_given_count=gift_given_count+1' ),
  371. array( 'gift_id' => $gift_id ),
  372. __METHOD__
  373. );
  374. }
  375. /**
  376. * Gets the amount of gifts a user has.
  377. *
  378. * @param $userName Mixed: username whose gift count we're looking up
  379. * @return Integer: amount of gifts the specified user has
  380. */
  381. static function getGiftCountByUsername( $userName ) {
  382. $dbr = wfGetDB( DB_SLAVE );
  383. $userId = User::idFromName( $userName );
  384. $res = $dbr->select(
  385. 'user_gift',
  386. 'COUNT(*) AS count',
  387. array( "ug_user_id_to = {$userId}" ),
  388. __METHOD__,
  389. array( 'LIMIT' => 1, 'OFFSET' => 0 )
  390. );
  391. $row = $dbr->fetchObject( $res );
  392. $giftCount = 0;
  393. if ( $row ) {
  394. $giftCount = $row->count;
  395. }
  396. return $giftCount;
  397. }
  398. }