PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/UserStats.class.php

https://github.com/BrianPrz/worklist
PHP | 704 lines | 606 code | 81 blank | 17 comment | 63 complexity | 6b01174616d90f24407f4ae23c2712f2 MD5 | raw file
  1. <?php
  2. if (!defined('SL_OK')) {
  3. define ('SL_OK', 'ok');
  4. }
  5. if (!defined('SL_ERROR')) {
  6. define ('SL_ERROR', 'error');
  7. }
  8. class UserStats{
  9. protected $userId;
  10. protected $itemsPerPage = 20;
  11. public function __construct($userId){
  12. $this->setUserId($userId);
  13. }
  14. public function setUserId($userId){
  15. $this->userId = $userId;
  16. }
  17. public function setItemsPerPage($number){
  18. $this->itemsPerPage = $number;
  19. }
  20. public function getItemsPerPage(){
  21. return $this->itemsPerPage;
  22. }
  23. public function getTotalJobsCount(){
  24. $sql = "SELECT COUNT(*) FROM `" . WORKLIST . "` "
  25. ."WHERE (`mechanic_id` = {$this->userId} OR `creator_id` = {$this->userId})"
  26. ."AND `status` IN ('Working', 'Functional', 'SvnHold', 'Review', 'Completed', 'Done')";
  27. $res = mysql_query($sql);
  28. if($res && $row = mysql_fetch_row($res)){
  29. return $row[0];
  30. }
  31. return false;
  32. }
  33. // wrapper for getJobsCount to get number of jobs in an active status
  34. public function getActiveJobsCount(){
  35. $sql = "SELECT COUNT(*) FROM `" . WORKLIST . "` "
  36. . "WHERE (`mechanic_id` = {$this->userId} OR `runner_id` = {$this->userId}) "
  37. . " AND `status` IN ('Working', 'Review', 'SvnHold', 'Functional')";
  38. $res = mysql_query($sql);
  39. if($res && $row = mysql_fetch_row($res)){
  40. return $row[0];
  41. }
  42. return false;
  43. }
  44. public function getFollowingJobs($page = 1) {
  45. $count = $this->getTotalJobsFollowingCount();
  46. $sql = "SELECT `" . WORKLIST . "`.`id`, `summary`, `status`, `mn`.`nickname` AS `mechanic_nickname`,
  47. `cn`.`nickname` AS `creator_nickname`,`rn`.`nickname` AS `runner_nickname`,
  48. DATE_FORMAT(`created`, '%m/%d/%Y') AS `created`
  49. FROM `" . WORKLIST . "`
  50. LEFT JOIN `" . USERS . "` AS `mn` ON `mechanic_id` = `mn`.`id`
  51. LEFT JOIN `" . USERS . "` AS `rn` ON `runner_id` = `rn`.`id`
  52. LEFT JOIN `" . USERS . "` AS `cn` ON `creator_id` = `cn`.`id`
  53. JOIN `" . TASK_FOLLOWERS . "` AS `tf` ON `tf`.`workitem_id` = `" . WORKLIST . "`.`id` AND `tf`.`user_id` = {$this->userId}
  54. ORDER BY `id` DESC
  55. LIMIT " . ($page-1)*$this->itemsPerPage . ", {$this->itemsPerPage}";
  56. $res = mysql_query($sql);
  57. $itemsArray = array();
  58. if ($res ) {
  59. while ($row = mysql_fetch_assoc($res)) {
  60. $itemsArray[] = $row;
  61. }
  62. return array(
  63. 'count' => $count,
  64. 'pages' => ceil($count/$this->itemsPerPage),
  65. 'page' => $page,
  66. 'joblist' => $itemsArray
  67. );
  68. }
  69. return false;
  70. }
  71. public function getTotalJobsFollowingCount(){
  72. $sql = "SELECT COUNT(*) FROM `" . TASK_FOLLOWERS . "` "
  73. ."WHERE (`user_id` = {$this->userId})";
  74. $res = mysql_query($sql);
  75. if ($res && $row = mysql_fetch_row($res)) {
  76. return $row[0];
  77. }
  78. return false;
  79. }
  80. public function getRunnerTotalJobsCount(){
  81. $sql = "SELECT COUNT(*) FROM `" . WORKLIST . "` "
  82. ."WHERE (`runner_id` = {$this->userId})"
  83. ."AND `status` IN ('Working', 'Functional', 'SvnHold', 'Review', 'Completed', 'Done')";
  84. $res = mysql_query($sql);
  85. if($res && $row = mysql_fetch_row($res)){
  86. return $row[0];
  87. }
  88. return false;
  89. }
  90. public function getTotalRunnerItems($page = 1){
  91. $count = $this->getRunnerTotalJobsCount();
  92. $sql = "SELECT `" . WORKLIST . "`.`id`, `summary`, `cn`.`nickname` AS `creator_nickname`,
  93. `rn`.`nickname` AS `runner_nickname`,
  94. DATE_FORMAT(`created`, '%m/%d/%Y') AS `created`
  95. FROM `" . WORKLIST . "`
  96. LEFT JOIN `" . USERS . "` AS `cn` ON `creator_id` = `cn`.`id`
  97. LEFT JOIN `" . USERS . "` AS `rn` ON `runner_id` = `rn`.`id`
  98. WHERE (`runner_id` = {$this->userId})
  99. AND `status` IN ('Working', 'Functional', 'SvnHold', 'Review', 'Completed', 'Done') ORDER BY `id` DESC
  100. LIMIT " . ($page-1)*$this->itemsPerPage . ", {$this->itemsPerPage}";
  101. $itemsArray = array();
  102. $res = mysql_query($sql);
  103. if($res ){
  104. while($row = mysql_fetch_assoc($res)){
  105. $itemsArray[] = $row;
  106. }
  107. return array(
  108. 'count' => $count,
  109. 'pages' => ceil($count/$this->itemsPerPage),
  110. 'page' => $page,
  111. 'joblist' => $itemsArray);
  112. }
  113. return false;
  114. }
  115. public function getRunnerActiveJobsCount(){
  116. $sql = "SELECT COUNT(*) FROM `" . WORKLIST . "` "
  117. . "WHERE (`runner_id` = {$this->userId}) AND `status` IN ('Working', 'Review', 'SvnHold', 'Functional')";
  118. $res = mysql_query($sql);
  119. if($res && $row = mysql_fetch_row($res)){
  120. return $row[0];
  121. }
  122. return false;
  123. }
  124. public function getActiveRunnerItems($page = 1){
  125. $count = $this->getRunnerActiveJobsCount();
  126. $sql = "SELECT `" . WORKLIST . "`.`id`, `summary`, `status`, `mn`.`nickname` AS `mechanic_nickname`, `cn`.`nickname` AS `creator_nickname`,
  127. `rn`.`nickname` AS `runner_nickname`,
  128. DATE_FORMAT(`created`, '%m/%d/%Y') AS `created`
  129. FROM `" . WORKLIST . "`
  130. LEFT JOIN `" . USERS . "` AS `mn` ON `mechanic_id` = `mn`.`id`
  131. LEFT JOIN `" . USERS . "` AS `rn` ON `runner_id` = `rn`.`id`
  132. LEFT JOIN `" . USERS . "` AS `cn` ON `creator_id` = `cn`.`id`
  133. WHERE (`runner_id` = {$this->userId}) AND `status` IN ('Working', 'Functional', 'SvnHold', 'Review') ORDER BY `id` DESC "
  134. . "LIMIT " . ($page-1)*$this->itemsPerPage . ", {$this->itemsPerPage}";
  135. $itemsArray = array();
  136. $res = mysql_query($sql);
  137. if ($res ) {
  138. while($row = mysql_fetch_assoc($res)) {
  139. $itemsArray[] = $row;
  140. }
  141. return array(
  142. 'count' => $count,
  143. 'pages' => ceil($count/$this->itemsPerPage),
  144. 'page' => $page,
  145. 'joblist' => $itemsArray
  146. );
  147. }
  148. return false;
  149. }
  150. public function getAvgJobRunTime() {
  151. $query = "SELECT AVG(TIME_TO_SEC(TIMEDIFF(doneDate, workingDate))) as avgJobRunTime FROM
  152. (SELECT w.id, s.change_date AS doneDate,
  153. ( SELECT MAX(`date`) AS workingDate FROM fees
  154. WHERE worklist_id = w.id AND `desc` = 'Accepted Bid') as workingDate
  155. FROM status_log s
  156. LEFT JOIN worklist w ON s.worklist_id = w.id
  157. WHERE s.status = 'Done' AND w.runner_id = " . $this->userId . ") AS x";
  158. if($result = mysql_query($query)) {
  159. $row = mysql_fetch_array($result);
  160. return ($row['avgJobRunTime'] > 0) ? relativeTime($row['avgJobRunTime'], false, true, false) : '';
  161. } else {
  162. return false;
  163. }
  164. }
  165. public function getDevelopersForRunner() {
  166. $query = "SELECT u.id, u.nickname, count(w.id) AS totalJobCount, sum(f.amount) AS totalEarnings FROM users u
  167. LEFT OUTER JOIN fees f ON f.user_id = u.id
  168. LEFT OUTER JOIN worklist w ON f.worklist_id = w.id
  169. WHERE f.paid =1 AND f.withdrawn = 0 AND f.expense = 0
  170. AND w.runner_id = {$this->userId} AND u.id <> w.runner_id
  171. GROUP BY u.id
  172. ORDER BY totalEarnings DESC";
  173. if($result = mysql_query($query)) {
  174. if(mysql_num_rows($result) > 0) {
  175. while($row = mysql_fetch_assoc($result)) {
  176. $developers[$row['id']] = $row;
  177. }
  178. } else {
  179. return false;
  180. }
  181. } else {
  182. return false;
  183. }
  184. return $developers;
  185. }
  186. public function getProjectsForRunner() {
  187. $query = "SELECT p.project_id, p.name, count(distinct w.id) AS totalJobCount, sum(f.amount) AS totalEarnings FROM " . PROJECTS . " p
  188. LEFT OUTER JOIN " . WORKLIST . " w ON w.project_id = p.project_id
  189. LEFT OUTER JOIN " . FEES . " f ON f.worklist_id = w.id
  190. WHERE w.runner_id = {$this->userId}
  191. AND w.status IN ('Working', 'Functional', 'SvnHold', 'Review', 'Completed', 'Done')
  192. AND f.paid = 1 AND f.withdrawn = 0 AND f.expense = 0
  193. GROUP BY p.project_id order by totalEarnings DESC";
  194. if($result = mysql_query($query)) {
  195. if(mysql_num_rows($result) > 0) {
  196. while($row = mysql_fetch_assoc($result)) {
  197. $projects[$row['project_id']] = $row;
  198. }
  199. } else {
  200. return false;
  201. }
  202. } else {
  203. return false;
  204. }
  205. return $projects;
  206. }
  207. public function getRunJobsCount() {
  208. $sql = "
  209. SELECT
  210. COUNT(*)
  211. FROM " . WORKLIST . "
  212. WHERE runner_id = {$this->userId}";
  213. $res = mysql_query($sql);
  214. if ($res && $row = mysql_fetch_row($res)) {
  215. return $row[0];
  216. }
  217. return false;
  218. }
  219. public function getMechanicJobCount() {
  220. $sql = "
  221. SELECT
  222. COUNT(*)
  223. FROM " . WORKLIST . "
  224. WHERE mechanic_id = {$this->userId}";
  225. $res = mysql_query($sql);
  226. if ($res && $row = mysql_fetch_row($res)) {
  227. return $row[0];
  228. }
  229. return false;
  230. }
  231. public function getTimeToPayAvg() {
  232. $sql = "
  233. SELECT ROUND(AVG(diff)) AS average FROM (
  234. SELECT TIME_TO_SEC(TIMEDIFF(dateDone, dateCompleted)) AS diff FROM (
  235. SELECT
  236. (SELECT MAX(change_date) FROM " . STATUS_LOG . " WHERE worklist_id = w.id AND status = 'Completed') AS dateCompleted,
  237. (SELECT MAX(change_date) FROM " . STATUS_LOG . " WHERE worklist_id = w.id AND status = 'Done') AS dateDone
  238. FROM " . STATUS_LOG . " sl
  239. LEFT JOIN " . WORKLIST . " w ON w.id = sl.worklist_id
  240. WHERE
  241. w.runner_id = {$this->userId}
  242. AND w.status = 'Done'
  243. GROUP BY worklist_id) AS dates
  244. WHERE dateCompleted IS NOT null) AS diffs";
  245. $res = mysql_query($sql);
  246. if ($res && $row = mysql_fetch_assoc($res)) {
  247. return $row['average'];
  248. }
  249. return false;
  250. }
  251. public function getTimeBidAcceptedAvg() {
  252. $sql = "
  253. SELECT ROUND(AVG(diff)) AS average FROM (
  254. SELECT TIME_TO_SEC(TIMEDIFF(dateWorking, firstBid)) AS diff FROM (
  255. SELECT
  256. (SELECT MAX(`date`) FROM " . FEES . " WHERE worklist_id = w.id AND `desc` = 'Accepted Bid') AS dateWorking,
  257. (SELECT MIN(bid_created) FROM " . BIDS . " WHERE worklist_id = w.id) AS firstBid
  258. FROM " . STATUS_LOG . " sl
  259. LEFT JOIN " . WORKLIST . " w ON w.id = sl.worklist_id
  260. WHERE
  261. w.runner_id = {$this->userId}
  262. GROUP BY worklist_id) AS dates
  263. WHERE dateWorking IS NOT null) AS diffs";
  264. $res = mysql_query($sql);
  265. if ($res && $row = mysql_fetch_assoc($res)) {
  266. return $row['average'];
  267. }
  268. return false;
  269. }
  270. public function getDoneOnTimePercentage() {
  271. $sql = "
  272. SELECT
  273. b.worklist_id,
  274. b.bid_done,
  275. ( CASE sl.status WHEN 'Functional' THEN MAX(sl.change_date) END) AS endTime
  276. FROM bids b, status_log sl
  277. WHERE
  278. sl.worklist_id = b.worklist_id
  279. AND sl.user_id = {$this->userId}
  280. AND b.bidder_id = sl.user_id
  281. GROUP BY sl.worklist_id
  282. HAVING endTime <= b.bid_done
  283. ";
  284. $res = mysql_query($sql);
  285. $functional_onTime_count = mysql_num_rows($res);
  286. $sql = "
  287. SELECT
  288. ( CASE sl.status WHEN 'Functional' THEN MAX(sl.change_date) END) AS endTime
  289. FROM bids b, status_log sl
  290. WHERE
  291. sl.worklist_id = b.worklist_id
  292. AND sl.user_id = {$this->userId}
  293. AND b.bidder_id = sl.user_id
  294. GROUP BY sl.worklist_id
  295. HAVING endTime IS NOT NULL
  296. ";
  297. $res = mysql_query($sql);
  298. $functional_all_count = mysql_num_rows($res);
  299. $onTimePercentage = $functional_all_count <> 0 ? round(($functional_onTime_count / $functional_all_count) * 100, 2) : 0;
  300. return $onTimePercentage;
  301. }
  302. public function getTimeCompletedAvg() {
  303. $sql = "
  304. SELECT ROUND(AVG(bidTime - realTime)) AS average FROM (
  305. SELECT
  306. TIME_TO_SEC(TIMEDIFF(dateCompleted, dateAccepted)) AS realTime,
  307. TIME_TO_SEC(TIMEDIFF(dateCompleted, dateToBeDone)) AS bidTime
  308. FROM (
  309. SELECT
  310. (SELECT MAX(change_date) FROM status_log WHERE worklist_id = w.id AND status = 'Completed') AS dateCompleted,
  311. (SELECT MAX(`date`) FROM " . FEES . " WHERE worklist_id = w.id AND `desc` = 'Accepted Bid') AS dateAccepted,
  312. (SELECT bid_done FROM " . BIDS . " WHERE worklist_id = w.id AND accepted = 1) AS dateToBeDone
  313. FROM " . STATUS_LOG . " sl
  314. LEFT JOIN " . WORKLIST . " w ON w.id = sl.worklist_id
  315. WHERE
  316. w.mechanic_id = {$this->userId}
  317. AND w.status IN ('Completed', 'Done')
  318. GROUP BY worklist_id) AS dates
  319. WHERE dateCompleted IS NOT null) AS diffs";
  320. $res = mysql_query($sql);
  321. if ($res && $row = mysql_fetch_assoc($res)) {
  322. return $row['average'];
  323. }
  324. return false;
  325. }
  326. public function getTotalEarnings(){
  327. $sql = "SELECT SUM(amount) FROM `fees` "
  328. . "WHERE `paid` = 1 AND `withdrawn`=0 AND `expense`=0 "
  329. . "AND `user_id` = {$this->userId}";
  330. $res = mysql_query($sql);
  331. if($res && $row = mysql_fetch_row($res)){
  332. return (int) $row[0];
  333. }
  334. return false;
  335. }
  336. // gets sum of paid earnings between specified period
  337. // start date and end date are included
  338. public function getEarningsForPeriod($startDate, $endDate){
  339. $startDate = date("Y-m-d", $startDate);
  340. $endDate = date("Y-m-d", $endDate);
  341. $sql = "SELECT SUM(amount) FROM `" . FEES . "` "
  342. . "WHERE `paid` = 1 AND `withdrawn`=0 AND `expense`=0 "
  343. . "AND `paid_date` >= '$startDate' AND `paid_date` <= '$endDate'"
  344. . "AND `user_id` = {$this->userId}";
  345. $res = mysql_query($sql);
  346. if($res && $row = mysql_fetch_row($res)){
  347. return (int) $row[0];
  348. }
  349. return false;
  350. }
  351. // gets earning for a number of days back
  352. // getLatestEarnings(30) will give earnings (paid) for last 30 days
  353. public function getLatestEarnings($daysCount){
  354. return $this->getEarningsForPeriod(strtotime("- $daysCount days"), time());
  355. }
  356. // gets list of fees and jobs associated with them for the preiod
  357. // start date and end date are included
  358. public function getEarningsJobsForPeriod($startDate, $endDate, $page = 1){
  359. $startDate = date("Y-m-d", $startDate);
  360. $endDate = date("Y-m-d", $endDate);
  361. $count = 0;
  362. $sql = "SELECT COUNT(*) FROM `" . FEES . "` "
  363. . "WHERE `paid` = 1 AND `withdrawn`=0 AND `expense`=0
  364. AND `paid_date` >= '$startDate' AND `paid_date` <= '$endDate'
  365. AND `user_id` = {$this->userId}";
  366. $res = mysql_query($sql);
  367. if($res && $row = mysql_fetch_row($res)){
  368. $count = $row[0];
  369. }
  370. $sql = "SELECT DISTINCT `worklist_id`, `amount`, `summary`, `paid_date`,
  371. DATE_FORMAT(`paid_date`, '%m/%d/%Y') AS `paid_formatted`,
  372. `cn`.`nickname` AS `creator_nickname`, `rn`.`nickname` AS `runner_nickname`
  373. FROM `" . FEES . "`
  374. LEFT JOIN `" . WORKLIST . "` ON `worklist_id` = `worklist`.`id`
  375. LEFT JOIN `" . USERS . "` AS `cn` ON `creator_id` = `cn`.`id`
  376. LEFT JOIN `" . USERS . "` AS `rn` ON `runner_id` = `rn`.`id`
  377. WHERE `" . FEES . "`.`paid` = 1 AND `withdrawn`=0 AND `expense`=0
  378. AND `paid_date` >= '$startDate' AND `paid_date` <= '$endDate'
  379. AND `user_id` = {$this->userId} ORDER BY `paid_date` DESC
  380. LIMIT " . ($page-1)*$this->itemsPerPage . ", {$this->itemsPerPage}";
  381. $itemsArray = array();
  382. $res = mysql_query($sql);
  383. if($res ){
  384. while($row = mysql_fetch_assoc($res)){
  385. $itemsArray[] = $row;
  386. }
  387. return array(
  388. 'count' => $count,
  389. 'pages' => ceil($count/$this->itemsPerPage),
  390. 'page' => $page,
  391. 'joblist' => $itemsArray);
  392. }
  393. return false;
  394. }
  395. // gets list of fees and jobs associated with them for a number of days back
  396. // works similar to getLatestEarnings(30) - will give earnings with jobs (paid) for last 30 days
  397. public function getLatestEarningsJobs($daysCount, $page = 1){
  398. return $this->getEarningsJobsForPeriod(strtotime("- $daysCount days"), time(), $page);
  399. }
  400. // get number of total love received by user using sendlove api
  401. public function getLoveCount(){
  402. $data = $this->sendloveApiRequest('getcount');
  403. if($data){
  404. return (int) $data['count'];
  405. }
  406. return false;
  407. }
  408. public function getUniqueLoveCount(){
  409. $data = $this->sendloveApiRequest('getuniquecount');
  410. if($data){
  411. return (int) $data['count'];
  412. }
  413. return false;
  414. }
  415. // get total love received by user using sendlove api
  416. public function getTotalLove($page = 1){
  417. $data = $this->sendloveApiRequest('getlove', $page);
  418. if($data){
  419. return $data;
  420. }
  421. return false;
  422. }
  423. // sends a request to sendlove api and returns data in case of success
  424. // false - if something went wrong
  425. public function sendloveApiRequest($action, $page = 1){
  426. $user = new User();
  427. $user->findUserById($this->userId);
  428. $params = array (
  429. 'action' => $action,
  430. 'api_key' => SENDLOVE_API_KEY,
  431. 'page' => $page,
  432. 'perpage' => $this->itemsPerPage,
  433. 'username' => $user->getUsername());
  434. $referer = (empty($_SERVER['HTTPS'])?'http://':'https://').$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
  435. $sendlove_rsp = postRequest (SENDLOVE_API_URL, $params, array(CURLOPT_REFERER => $referer));
  436. $rsp = json_decode ($sendlove_rsp, true);
  437. if($rsp['status'] == SL_OK){
  438. return $rsp['data'];
  439. }else{
  440. return false;
  441. }
  442. }
  443. public function getJobsCount(){
  444. $count = 0;
  445. $sql = "SELECT COUNT(*) FROM `" . WORKLIST . "` "
  446. . "WHERE `mechanic_id` = {$this->userId} OR `creator_id` = {$this->userId} "
  447. ."AND `status` IN ('Working', 'Functional', 'Review', 'Completed', 'Done')
  448. AND `creator_id` != `mechanic_id` ";
  449. $res = mysql_query($sql);
  450. if($res && $row = mysql_fetch_row($res)){
  451. $count = $row[0];
  452. }
  453. return $count;
  454. }
  455. public function getJobsCountForASpecificProject($project){
  456. $count = 0;
  457. $sql = "SELECT COUNT(*) FROM `" . WORKLIST . "` "
  458. . "WHERE `mechanic_id` = {$this->userId} OR `creator_id` = {$this->userId} "
  459. ."AND `status` IN ('Working', 'Functional', 'Review', 'Completed', 'Done')
  460. AND `creator_id` != `mechanic_id` AND project_id = " . $project;
  461. $res = mysql_query($sql);
  462. if ($res && $row = mysql_fetch_row($res)){
  463. $count = $row[0];
  464. }
  465. return $count;
  466. }
  467. public function getCompletedJobsWithStats($page = 1) {
  468. $sql = "
  469. SELECT w.id, w.summary, f.cost, DATEDIFF(d2.change_date, b.date) days
  470. FROM " . WORKLIST . " w
  471. # Code is commented out as there is not a reliable 'Working' status date due to issues
  472. # in the acceptdBid method. This method has been updated.
  473. # LEFT JOIN " . STATUS_LOG . " d1 ON d1.worklist_id = w.id AND d1.status = 'Working'
  474. LEFT JOIN " . STATUS_LOG . " d2 ON d2.worklist_id = w.id AND d2.status = 'Completed'
  475. # And temporarily using the creation date of the Accepted Bid
  476. LEFT JOIN " . FEES . " b ON b.worklist_id = w.id AND b.desc = 'Accepted Bid'
  477. LEFT JOIN (
  478. SELECT SUM(amount) cost, worklist_id
  479. FROM " . FEES . "
  480. WHERE withdrawn = 0
  481. GROUP BY worklist_id
  482. ) f ON f.worklist_id = w.id
  483. WHERE
  484. (`mechanic_id` = {$this->userId} OR `creator_id` = {$this->userId}) AND
  485. w.`status` IN ('Completed', 'Done')
  486. GROUP BY w.`id`
  487. ORDER BY w.`id` DESC
  488. LIMIT 5";
  489. $res = mysql_query($sql);
  490. if ($res) {
  491. $itemsArray = array();
  492. while ($row = mysql_fetch_assoc($res)) {
  493. $itemsArray[] = $row;
  494. }
  495. return array(
  496. 'joblist' => $itemsArray,
  497. 'pages' => 1,
  498. 'page' => $page
  499. );
  500. }
  501. return false;
  502. }
  503. public function getUserItems($status, $page = 1){
  504. $count = $this->getJobsCount($status);
  505. $sql = "SELECT `" . WORKLIST . "`.`id`, `summary`, `cn`.`nickname` AS `creator_nickname`,
  506. `rn`.`nickname` AS `runner_nickname`,
  507. DATE_FORMAT(`created`, '%m/%d/%Y') AS `created`
  508. FROM `" . WORKLIST . "`
  509. LEFT JOIN `" . USERS . "` AS `cn` ON `creator_id` = `cn`.`id`
  510. LEFT JOIN `" . USERS . "` AS `rn` ON `runner_id` = `rn`.`id`
  511. WHERE (`mechanic_id` = {$this->userId} OR `creator_id` = {$this->userId})
  512. AND `status` IN ('Working', 'Functional', 'SvnHold', 'Review', 'Completed', 'Done') ORDER BY `id` DESC
  513. LIMIT " . ($page-1)*$this->itemsPerPage . ", {$this->itemsPerPage}";
  514. $itemsArray = array();
  515. $res = mysql_query($sql);
  516. if($res ){
  517. while($row = mysql_fetch_assoc($res)){
  518. $itemsArray[] = $row;
  519. }
  520. return array(
  521. 'count' => $count,
  522. 'pages' => ceil($count/$this->itemsPerPage),
  523. 'page' => $page,
  524. 'joblist' => $itemsArray);
  525. }
  526. return false;
  527. }
  528. public function getUserItemsForASpecificProject($status, $project, $page = 1){
  529. $count = $this->getJobsCountForASpecificProject($project);
  530. $sql = "SELECT `" . WORKLIST . "`.`id`, `summary`, `cn`.`nickname` AS `creator_nickname`,
  531. `rn`.`nickname` AS `runner_nickname`,
  532. DATE_FORMAT(`created`, '%m/%d/%Y') AS `created`
  533. FROM `" . WORKLIST . "`
  534. LEFT JOIN `" . USERS . "` AS `cn` ON `creator_id` = `cn`.`id`
  535. LEFT JOIN `" . USERS . "` AS `rn` ON `runner_id` = `rn`.`id`
  536. WHERE (`mechanic_id` = {$this->userId} OR `creator_id` = {$this->userId})
  537. AND `status` IN ('Working', 'Functional', 'Review', 'Completed', 'Done')
  538. AND project_id = ". $project . " ORDER BY `id` DESC
  539. LIMIT " . ($page-1)*$this->itemsPerPage . ", {$this->itemsPerPage}";
  540. $itemsArray = array();
  541. $res = mysql_query($sql);
  542. if ($res){
  543. while($row = mysql_fetch_assoc($res)){
  544. $itemsArray[] = $row;
  545. }
  546. return array(
  547. 'count' => $count,
  548. 'pages' => ceil($count/$this->itemsPerPage),
  549. 'page' => $page,
  550. 'joblist' => $itemsArray);
  551. }
  552. return false;
  553. }
  554. public function getActiveUserItems($status, $page = 1){
  555. $count = $this->getActiveJobsCount($status);
  556. $sql = "SELECT `" . WORKLIST . "`.`id`, `summary`, `status`, `mn`.`nickname` AS `mechanic_nickname`, `cn`.`nickname` AS `creator_nickname`,
  557. `rn`.`nickname` AS `runner_nickname`,
  558. `" . WORKLIST . "`.`sandbox` AS `sandbox`,
  559. DATE_FORMAT(`created`, '%m/%d/%Y') AS `created`
  560. FROM `" . WORKLIST . "`
  561. LEFT JOIN `" . USERS . "` AS `mn` ON `mechanic_id` = `mn`.`id`
  562. LEFT JOIN `" . USERS . "` AS `rn` ON `runner_id` = `rn`.`id`
  563. LEFT JOIN `" . USERS . "` AS `cn` ON `creator_id` = `cn`.`id`
  564. WHERE (`mechanic_id` = {$this->userId} OR `runner_id` = {$this->userId})
  565. AND `status` IN ('Working', 'Functional', 'SvnHold', 'Review')
  566. ORDER BY `id` DESC "
  567. . "LIMIT " . ($page-1)*$this->itemsPerPage . ", {$this->itemsPerPage}";
  568. $itemsArray = array();
  569. $res = mysql_query($sql);
  570. if ($res) {
  571. while($row = mysql_fetch_assoc($res)) {
  572. $itemsArray[] = $row;
  573. }
  574. return array(
  575. 'count' => $count,
  576. 'pages' => ceil($count/$this->itemsPerPage),
  577. 'page' => $page,
  578. 'joblist' => $itemsArray
  579. );
  580. }
  581. return false;
  582. }
  583. public function getBonusPaymentsTotal() {
  584. $sql = "
  585. SELECT
  586. IFNULL(`rewarder`.`sum`,0) AS `bonus_tot`
  587. FROM `".USERS."`
  588. LEFT JOIN (SELECT `user_id`, SUM(amount) AS `sum` FROM `".FEES."` WHERE (`withdrawn`=0 AND `paid` = 1 AND `user_id` = {$this->userId}) AND (`rewarder`=1 OR `bonus`=1) GROUP BY `user_id`) AS `rewarder` ON `".USERS."`.`id` = `rewarder`.`user_id`
  589. WHERE `id` = {$this->userId}";
  590. $res = mysql_query($sql);
  591. if($res && $row = mysql_fetch_row($res)){
  592. return (int) $row[0];
  593. }
  594. return false;
  595. }
  596. public static function getNewUserStats() {
  597. $sql = "
  598. SELECT (
  599. SELECT COUNT(DISTINCT(users.id))
  600. FROM " . USERS . "
  601. INNER JOIN " . FEES . " ON users.id = fees.user_id AND users.added > DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
  602. ) AS newUsersWithFees, (
  603. SELECT COUNT(DISTINCT(users.id))
  604. FROM " . USERS . "
  605. INNER JOIN " . BIDS . " ON users.id = bids.bidder_id AND users.added > DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
  606. ) AS newUsersWithBids, (
  607. SELECT COUNT(*)
  608. FROM " . USERS . "
  609. WHERE added > DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
  610. ) AS newUsers, (
  611. SELECT COUNT(*)
  612. FROM " . USERS . "
  613. WHERE
  614. added > DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
  615. AND last_seen > added
  616. ) AS newUsersLoggedIn";
  617. $res = mysql_query($sql);
  618. if ($res && $row = mysql_fetch_assoc($res)) {
  619. return $row;
  620. }
  621. return false;
  622. }
  623. }