PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/SocialProfile/SystemGifts/UserSystemGiftsClass.php

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