/package/app/app/alpha/apps/kaltura/lib/myStatisticsMgr.class.php

https://github.com/richhl/kalturaCE · PHP · 417 lines · 321 code · 49 blank · 47 comment · 58 complexity · 6a8e11115282a889a04fc0a672506438 MD5 · raw file

  1. <?php
  2. class myStatisticsMgr
  3. {
  4. private static $s_dirty_objects = array();
  5. public static function incKuserViews ( kuser $kuser , $delta = 1 )
  6. {
  7. $v = $kuser->getViews();
  8. if ( self::shouldModify ( $kuser , kuserPeer::VIEWS ) );
  9. {
  10. self::inc ( $v , $delta);
  11. $kuser->setViews( $v );
  12. }
  13. return $v;
  14. }
  15. // will increment either fans or favorites for kshow or entry according to favorite.subject_type
  16. /**
  17. const SUBJECT_TYPE_KSHOW = '1';
  18. const SUBJECT_TYPE_ENTRY = '2';
  19. const SUBJECT_TYPE_USER = '3';
  20. */
  21. public static function addFavorite ( favorite $favorite )
  22. {
  23. self::add ( $favorite );
  24. $type = $favorite->getSubjectType();
  25. $id = $favorite->getSubjectId();
  26. if ( $type == favorite::SUBJECT_TYPE_ENTRY )
  27. {
  28. $obj = entryPeer::retrieveByPK( $id );
  29. if ( $obj )
  30. {
  31. $v = $obj->getFavorites () ;
  32. self::inc ( $v );
  33. $obj->setFavorites ( $v );
  34. }
  35. }
  36. elseif ( $type == favorite::SUBJECT_TYPE_KSHOW )
  37. {
  38. $obj = kshowPeer::retrieveByPK( $id );
  39. if ( $obj )
  40. {
  41. $v = $obj->getFavorites () ;
  42. self::inc ( $v );
  43. $obj->setFavorites ( $v );
  44. }
  45. }
  46. elseif ( $type == favorite::SUBJECT_TYPE_USER )
  47. {
  48. $obj = kuserPeer::retrieveByPK( $id );
  49. if ( $obj )
  50. {
  51. $v = $obj->getFans () ;
  52. self::inc ( $v );
  53. $obj->setFans ( $v );
  54. }
  55. }
  56. // don't forget to save the modified object
  57. self::add ( $obj );
  58. }
  59. //- will increment kuser.entries, kshow.entries & kshow.contributors
  60. public static function addEntry ( entry $entry )
  61. {
  62. $kshow = $entry->getkshow();
  63. if ( $kshow )
  64. {
  65. $v = $kshow->getEntries();
  66. self::inc ( $v );
  67. $kshow->setEntries ( $v );
  68. }
  69. $c = new Criteria();
  70. myCriteria::addComment( $c , __METHOD__ );
  71. $c->add ( entryPeer::KSHOW_ID , $entry->getKshowId() );
  72. $c->add ( entryPeer::KUSER_ID , $entry->getKuserId() );
  73. $c->setLimit ( 2 );
  74. $res = entryPeer::doCount( $c );
  75. if ( $res < 1 && $kshow != null )
  76. {
  77. // kuser didn't contribute to this kshow - increment
  78. $v = $kshow->getContributors();
  79. self::inc ( $v );
  80. $kshow->setContributors( $v );
  81. }
  82. $kuser = $entry->getkuser();
  83. if ( $kuser )
  84. {
  85. $v = $kuser->getEntries();
  86. self::inc ( $v );
  87. $kuser->setEntries ( $v );
  88. }
  89. self::add ( $kshow );
  90. self::add ( $kuser );
  91. }
  92. //- will increment kuser.entries, kshow.entries & kshow.contributors
  93. public static function deleteEntry ( entry $entry )
  94. {
  95. $kshow = $entry->getkshow();
  96. if ($kshow)
  97. {
  98. $v = $kshow->getEntries();
  99. self::dec ( $v );
  100. $kshow->setEntries ( $v );
  101. $c = new Criteria();
  102. myCriteria::addComment( $c , __METHOD__ );
  103. $c->add ( entryPeer::KSHOW_ID , $entry->getKshowId() );
  104. $c->add ( entryPeer::KUSER_ID , $entry->getKuserId() );
  105. $c->setLimit ( 2 );
  106. $res = entryPeer::doCount( $c );
  107. if ( $res == 1 )
  108. {
  109. // if $res > 1 - this kuser contributed more than one entry, deleting this one should still leave him a contributor
  110. // if $res < 1 - this kuser never contributed - strange! but no need to dec the contributors
  111. // kuser did contribute to this kshow - decrement
  112. $v = $kshow->getContributors();
  113. self::dec ( $v );
  114. $kshow->setContributors( $v );
  115. }
  116. $kuser = $entry->getkuser();
  117. if ( $kuser )
  118. {
  119. $v = $kuser->getEntries();
  120. self::dec ( $v );
  121. $kuser->setEntries ( $v );
  122. }
  123. self::add ( $kshow );
  124. self::add ( $kuser );
  125. }
  126. }
  127. //- will increment kuser.produced_kshows
  128. public static function addKshow ( kshow $kshow )
  129. {
  130. $kuser = $kshow->getKuser();
  131. // this might happen when creating a temp kshow without setting its producer
  132. if ( $kuser == NULL ) return;
  133. $v = $kuser->getProducedKshows ();
  134. self::inc ( $v );
  135. $kuser->setProducedKshows ( $v );
  136. self::add ( $kuser );
  137. }
  138. //- will decrement kuser.produced_kshows
  139. public static function deleteKshow ( kshow $kshow )
  140. {
  141. $kuser = $kshow->getKuser();
  142. // this might happen when creating a temp kshow without setting its producer
  143. if ( $kuser == NULL ) return;
  144. $v = $kuser->getProducedKshows ();
  145. self::dec( $v );
  146. $kuser->setProducedKshows ( $v );
  147. self::add ( $kuser );
  148. }
  149. public static function incKshowViews ( kshow $kshow , $delta = 1 )
  150. {
  151. $v = $kshow->getViews();
  152. if ( self::shouldModify ( $kshow , kshowPeer::VIEWS ) );
  153. {
  154. self::inc ( $v , $delta);
  155. $kshow->setViews( $v );
  156. }
  157. return $v;
  158. }
  159. public static function incKshowPlays ( kshow $kshow , $delta = 1 )
  160. {
  161. $v = $kshow->getPlays();
  162. KalturaLog::log ( __METHOD__ . ": " . $kshow->getId() . " plays: $v");
  163. if ( self::shouldModify ( $kshow , kshowPeer::PLAYS ) );
  164. {
  165. self::inc ( $v , $delta);
  166. $kshow->setPlays( $v );
  167. }
  168. KalturaLog::log ( __METHOD__ . ": " . $kshow->getId() . " plays: $v");
  169. return $v;
  170. }
  171. /*
  172. // - do we vote for kshows ??? - this should be derived from the roughcut
  173. public static function incKshowVotes ( kshow $kshow )
  174. {
  175. }
  176. */
  177. // - will increment kshow.comments or entry.comments according to comment_type
  178. /**
  179. * const COMMENT_TYPE_KSHOW = 1;
  180. const COMMENT_TYPE_DISCUSSION = 2;
  181. const COMMENT_TYPE_USER = 3;
  182. const COMMENT_TYPE_SHOUTOUT = 4;
  183. *
  184. */
  185. public static function addComment ( comment $comment )
  186. {
  187. $obj = NULL;
  188. $type = $comment->getCommentType();
  189. $id = $comment->getSubjectId();
  190. if ( $type == comment::COMMENT_TYPE_KSHOW ||
  191. $type == comment::COMMENT_TYPE_SHOUTOUT ||
  192. $type == comment::COMMENT_TYPE_DISCUSSION )
  193. {
  194. $obj = kshowPeer::retrieveByPK( $id );
  195. if ( $obj )
  196. {
  197. $v = $obj->getComments () ;
  198. self::inc ( $v );
  199. $obj->setComments ( $v );
  200. }
  201. }
  202. elseif ( $type == comment::COMMENT_TYPE_USER )
  203. {
  204. /* $obj = kuserPeer::retrieveByPK( $id );
  205. $v = $obj->getComments () ;
  206. self::inc ( $v );
  207. $obj->setComments ( $v );
  208. */
  209. }
  210. // TODO - what about the other types ?
  211. if ( $obj != NULL ) self::add ( $obj );
  212. }
  213. public static function addSubscriber ( KshowKuser $kushow_kuser )
  214. {
  215. $type = $kushow_kuser->getAlertType();
  216. if ( $type == KshowKuser::KSHOW_SUBSCRIPTION_NORMAL )
  217. {
  218. $kshow = $kushow_kuser->getkshow();
  219. if ( $kshow )
  220. {
  221. $v = $kshow->getSubscribers() ;
  222. self::inc ( $v );
  223. $kshow->setSubscribers ( $v );
  224. }
  225. self::add ( $kshow );
  226. }
  227. }
  228. // - will increment kshow.number_of_updates
  229. public static function incKshowUpdates ( kshow $kshow, $delta = 1 )
  230. {
  231. $v = $kshow->getNumberOfUpdates();
  232. if ( self::shouldModify( $kshow , kshowPeer::NUMBER_OF_UPDATES ) )
  233. {
  234. self::inc ( $v , $delta);
  235. $kshow->setNumberOfUpdates( $v );
  236. }
  237. return $v;
  238. }
  239. public static function incEntryViews ( entry $entry , $delta = 1 )
  240. {
  241. $v = $entry->getViews();
  242. if ( $delta == 0 ) return $v;
  243. if ( self::shouldModify ( $entry , entryPeer::VIEWS ) );
  244. {
  245. self::inc ( $v , $delta);
  246. $entry->setViews( $v );
  247. }
  248. if ( $entry->getType() == entryType::MIX )
  249. {
  250. $enclosing_kshow = $entry->getKshow();
  251. if ( $enclosing_kshow )
  252. {
  253. $kshow_views = $enclosing_kshow->getViews() ;
  254. $enclosing_kshow->setViews ( ++$kshow_views );
  255. self::add( $enclosing_kshow );
  256. }
  257. }
  258. return $v;
  259. }
  260. public static function incEntryPlays ( entry $entry , $delta = 1 )
  261. {
  262. $v = $entry->getPlays();
  263. if ( $delta == 0 ) return $v;
  264. if ( self::shouldModify ( $entry , entryPeer::PLAYS ) );
  265. {
  266. self::inc ( $v , $delta);
  267. $entry->setPlays( $v );
  268. }
  269. if ( $entry->getType() == entryType::MIX )
  270. {
  271. $enclosing_kshow = $entry->getKshow();
  272. if ( $enclosing_kshow )
  273. {
  274. $kshow_views = $enclosing_kshow->getPlays() ;
  275. $enclosing_kshow->setPlays ( ++$kshow_views );
  276. self::add( $enclosing_kshow );
  277. }
  278. }
  279. return $v;
  280. }
  281. public static function addKvote ( kvote $kvote , $delta_rank )
  282. {
  283. $entry = $kvote->getEntry();
  284. $res = self::incEntryVotes ( $entry , $delta_rank );
  285. return $res;
  286. }
  287. // - will update votes , total_rank & rank
  288. // if the ebtry is of type roughcut -0 will update the kshow's rank too
  289. private static function incEntryVotes ( entry $entry , $delta_rank )
  290. {
  291. $res = array();
  292. $votes = $entry->getVotes();
  293. if ( self::shouldModify ( $entry , entryPeer::VOTES ) );
  294. {
  295. self::inc ( $votes );
  296. $entry->setVotes( $votes );
  297. $total_rank = $entry->getTotalRank();
  298. self::inc ( $total_rank , $delta_rank );
  299. $entry->setTotalRank( $total_rank );
  300. $res ["entry"] = $entry;
  301. // can assume $votes > 0
  302. $rank = $entry->setRank ( ( $total_rank / $votes ) * 1000 );
  303. // if rouhcut - update the kshow's rank too
  304. if ( $entry->getType() == entryType::MIX )
  305. {
  306. $enclosing_kshow = $entry->getKshow();
  307. if ( $enclosing_kshow )
  308. {
  309. $kshow_votes = $enclosing_kshow->getVotes() ;
  310. $enclosing_kshow->setVotes ( ++$kshow_votes );
  311. if ( true ) //if ( $enclosing_kshow->getRank() < $entry->getRank() ) // rank the show
  312. {
  313. $enclosing_kshow->setRank ( $entry->getRank() );
  314. self::add( $enclosing_kshow );
  315. $res ["kshow"] = $enclosing_kshow;
  316. }
  317. }
  318. }
  319. }
  320. return $res;
  321. }
  322. // TODO - might be duplicates in the list- try to avoid redundant saves
  323. // (although won't commit to DB because there will be no internal dirty flags)
  324. public static function saveAllModified ()
  325. {
  326. foreach ( self::$s_dirty_objects as $id => $dirty_obj )
  327. {
  328. self::log ( "saving: [$id]" );
  329. $dirty_obj->save();
  330. }
  331. // free all the object - create a new empty array
  332. self::$s_dirty_objects = array();
  333. }
  334. private static function shouldModify ( BaseObject $baseObject , $col )
  335. {
  336. if ( ! $baseObject->isColumnModified($col ) )
  337. {
  338. self::add ( $baseObject );
  339. return true;
  340. }
  341. // this object should not be updated twice
  342. return false;
  343. }
  344. private static function add ( /*BaseObject*/ $baseObject )
  345. {
  346. if ( $baseObject != null )
  347. {
  348. $id = get_class ( $baseObject ) . $baseObject->getId();
  349. self::log ( "adding: [$id]" );
  350. self::$s_dirty_objects[$id] = $baseObject;
  351. }
  352. }
  353. private static function inc ( &$num , $delta = 1 )
  354. {
  355. if ( ! is_numeric ( $num )) $num = 0;
  356. $num += $delta;
  357. }
  358. private static function dec ( &$num , $delta = 1 )
  359. {
  360. if ( ! is_numeric ( $num )) $num = 0;
  361. $num -= $delta;
  362. }
  363. private static function log ( $str )
  364. {
  365. }
  366. }
  367. ?>