PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/source/class/class_task.php

https://github.com/jinbo51/DiscuzX
PHP | 649 lines | 573 code | 70 blank | 6 comment | 191 complexity | c451a182ee33e47177148bb3608e13f5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: class_task.php 30363 2012-05-24 07:16:47Z monkey $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class task {
  12. var $task;
  13. var $taskvars;
  14. var $message;
  15. var $multipage;
  16. var $listdata;
  17. function task() {}
  18. function &instance() {
  19. static $object;
  20. if(empty($object)) {
  21. $object = new task();
  22. }
  23. return $object;
  24. }
  25. function tasklist($item) {
  26. global $_G;
  27. $multipage = '';
  28. $page = max(1, intval($_GET['page']));
  29. $start_limit = ($page - 1) * $_G['tpp'];
  30. $tasklist = $endtaskids = $magicids = $medalids = $groupids = array();
  31. $updated = FALSE;
  32. $num = 0;
  33. foreach(C::t('common_task')->fetch_all_by_status($_G['uid'], $item) as $task) {
  34. if($item == 'new' || $item == 'canapply') {
  35. list($task['allowapply'], $task['t']) = $this->checknextperiod($task);
  36. if($task['allowapply'] < 0) {
  37. continue;
  38. }
  39. $task['noperm'] = $task['applyperm'] && $task['applyperm'] != 'all' && !(($task['applyperm'] == 'member'&& $_G['adminid'] == '0') || ($task['applyperm'] == 'admin' && $_G['adminid'] > '0') || forumperm($task['applyperm']));
  40. $task['appliesfull'] = $task['tasklimits'] && $task['achievers'] >= $task['tasklimits'];
  41. if($item == 'canapply' && ($task['noperm'] || $task['appliesfull'])) {
  42. continue;
  43. }
  44. }
  45. $num++;
  46. if($task['reward'] == 'magic') {
  47. $magicids[] = $task['prize'];
  48. } elseif($task['reward'] == 'medal') {
  49. $medalids[] = $task['prize'];
  50. } elseif($task['reward'] == 'invite') {
  51. $invitenum = $task['prize'];
  52. } elseif($task['reward'] == 'group') {
  53. $groupids[] = $task['prize'];
  54. }
  55. if($task['available'] == '2' && ($task['starttime'] > TIMESTAMP || ($task['endtime'] && $task['endtime'] <= TIMESTAMP))) {
  56. $endtaskids[] = $task['taskid'];
  57. }
  58. $csc = explode("\t", $task['csc']);
  59. $task['csc'] = floatval($csc[0]);
  60. $task['lastupdate'] = intval($csc[1]);
  61. if(!$updated && $item == 'doing' && $task['csc'] < 100) {
  62. $updated = TRUE;
  63. $escript = explode(':', $task['scriptname']);
  64. if(count($escript) > 1) {
  65. include_once DISCUZ_ROOT.'./source/plugin/'.$escript[0].'/task/task_'.$escript[1].'.php';
  66. $taskclassname = 'task_'.$escript[1];
  67. } else {
  68. require_once libfile('task/'.$task['scriptname'], 'class');
  69. $taskclassname = 'task_'.$task['scriptname'];
  70. }
  71. $taskclass = new $taskclassname;
  72. $task['applytime'] = $task['dateline'];
  73. if(method_exists($taskclass, 'csc')) {
  74. $result = $taskclass->csc($task);
  75. } else {
  76. showmessage('task_not_found', '', array('taskclassname' => $taskclassname));
  77. }
  78. if($result === TRUE) {
  79. $task['csc'] = '100';
  80. C::t('common_mytask')->update($_G['uid'], $task['taskid'], array('csc' => $task['csc']));
  81. } elseif($result === FALSE) {
  82. C::t('common_mytask')->update($_G['uid'], $task['taskid'], array('status' => -1));
  83. } else {
  84. $task['csc'] = floatval($result['csc']);
  85. C::t('common_mytask')->update($_G['uid'], $task['taskid'], array('csc' => $task['csc']."\t".$_G['timestamp']));
  86. }
  87. }
  88. if(in_array($item, array('done', 'failed')) && $task['period']) {
  89. list($task['allowapply'], $task['t']) = $this->checknextperiod($task);
  90. $task['allowapply'] = $task['allowapply'] > 0 ? 1 : 0;
  91. }
  92. $task['icon'] = $task['icon'] ? $task['icon'] : 'task.gif';
  93. if(strtolower(substr($task['icon'], 0, 7)) != 'http://') {
  94. $escript = explode(':', $task['scriptname']);
  95. if(count($escript) > 1 && file_exists(DISCUZ_ROOT.'./source/plugin/'.$escript[0].'/task/task_'.$escript[1].'.gif')) {
  96. $task['icon'] = 'source/plugin/'.$escript[0].'/task/task_'.$escript[1].'.gif';
  97. } else {
  98. $task['icon'] = 'static/image/task/'.$task['icon'];
  99. }
  100. }
  101. $task['dateline'] = $task['dateline'] ? dgmdate($task['dateline'], 'u') : '';
  102. $tasklist[] = $task;
  103. }
  104. if($magicids) {
  105. foreach(C::t('common_magic')->fetch_all($magicids) as $magic) {
  106. $this->listdata[$magic['magicid']] = $magic['name'];
  107. }
  108. }
  109. if($medalids) {
  110. foreach(C::t('forum_medal')->fetch_all($medalids) as $medal) {
  111. $this->listdata[$medal['medalid']] = $medal['name'];
  112. }
  113. }
  114. if($groupids) {
  115. foreach(C::t('common_usergroup')->fetch_all($groupids) as $group) {
  116. $this->listdata[$group['groupid']] = $group['grouptitle'];
  117. }
  118. }
  119. if($invitenum) {
  120. $this->listdata[$invitenum] = $_G['lang']['invite_code'];
  121. }
  122. if($endtaskids) {
  123. }
  124. return $tasklist;
  125. }
  126. function view($id) {
  127. global $_G;
  128. $this->task = C::t('common_task')->fetch_by_uid($_G['uid'], $id);
  129. if(!$this->task) {
  130. showmessage('task_nonexistence');
  131. }
  132. switch($this->task['reward']) {
  133. case 'magic':
  134. $this->task['rewardtext'] = C::t('common_magic')->fetch($this->task['prize']);
  135. $this->task['rewardtext'] = $this->task['rewardtext']['name'];
  136. break;
  137. case 'medal':
  138. $this->task['rewardtext'] = C::t('forum_medal')->fetch($this->task['prize']);
  139. $this->task['rewardtext'] = $this->task['rewardtext']['name'];
  140. break;
  141. case 'group':
  142. $group = C::t('common_usergroup')->fetch($this->task['prize']);
  143. $this->task['rewardtext'] = $group['grouptitle'];
  144. break;
  145. }
  146. $this->task['icon'] = $this->task['icon'] ? $this->task['icon'] : 'task.gif';
  147. if(strtolower(substr($this->task['icon'], 0, 7)) != 'http://') {
  148. $escript = explode(':', $this->task['scriptname']);
  149. if(count($escript) > 1 && file_exists(DISCUZ_ROOT.'./source/plugin/'.$escript[0].'/task/task_'.$escript[1].'.gif')) {
  150. $this->task['icon'] = 'source/plugin/'.$escript[0].'/task/task_'.$escript[1].'.gif';
  151. } else {
  152. $this->task['icon'] = 'static/image/task/'.$this->task['icon'];
  153. }
  154. }
  155. $this->task['endtime'] = $this->task['endtime'] ? dgmdate($this->task['endtime'], 'u') : '';
  156. $this->task['description'] = nl2br($this->task['description']);
  157. $this->taskvars = array();
  158. foreach(C::t('common_taskvar')->fetch_all_by_taskid($id) as $taskvar) {
  159. if(!$taskvar['variable'] || $taskvar['value']) {
  160. if(!$taskvar['variable']) {
  161. $taskvar['value'] = $taskvar['description'];
  162. }
  163. if($taskvar['sort'] == 'apply') {
  164. $this->taskvars['apply'][] = $taskvar;
  165. } elseif($taskvar['sort'] == 'complete') {
  166. $this->taskvars['complete'][$taskvar['variable']] = $taskvar;
  167. } elseif($taskvar['sort'] == 'setting') {
  168. $this->taskvars['setting'][$taskvar['variable']] = $taskvar;
  169. }
  170. }
  171. }
  172. $this->task['grouprequired'] = $comma = '';
  173. $this->task['applyperm'] = $this->task['applyperm'] == 'all' ? '' : $this->task['applyperm'];
  174. if(!in_array($this->task['applyperm'], array('', 'member', 'admin'))) {
  175. $query = C::t('common_usergroup')->fetch_all(explode(',', str_replace("\t", ',', $this->task['applyperm'])));
  176. foreach($query as $group) {
  177. $this->task['grouprequired'] .= $comma.$group[grouptitle];
  178. $comma = ', ';
  179. }
  180. }
  181. if($this->task['relatedtaskid']) {
  182. $task = C::t('common_task')->fetch($this->task['relatedtaskid']);
  183. $_G['taskrequired'] = $task['name'];
  184. }
  185. $escript = explode(':', $this->task['scriptname']);
  186. if(count($escript) > 1) {
  187. include_once DISCUZ_ROOT.'./source/plugin/'.$escript[0].'/task/task_'.$escript[1].'.php';
  188. $taskclassname = 'task_'.$escript[1];
  189. } else {
  190. require_once libfile('task/'.$this->task['scriptname'], 'class');
  191. $taskclassname = 'task_'.$this->task['scriptname'];
  192. }
  193. $taskclass = new $taskclassname;
  194. if($this->task['status'] == '-1') {
  195. if($this->task['period']) {
  196. list($allowapply, $this->task['t']) = $this->checknextperiod($this->task);
  197. } else {
  198. $allowapply = -4;
  199. }
  200. } elseif($this->task['status'] == '0') {
  201. $allowapply = -1;
  202. $csc = explode("\t", $this->task['csc']);
  203. $this->task['csc'] = floatval($csc[0]);
  204. $this->task['lastupdate'] = intval($csc[1]);
  205. if($this->task['csc'] < 100) {
  206. if(method_exists($taskclass, 'csc')) {
  207. $result = $taskclass->csc($this->task);
  208. }
  209. if($result === TRUE) {
  210. $this->task['csc'] = '100';
  211. C::t('common_mytask')->update($_G['uid'], $id, array('csc' => $this->task['csc']));
  212. } elseif($result === FALSE) {
  213. C::t('common_mytask')->update($_G['uid'], $id, array('status' => -1));
  214. dheader("Location: home.php?mod=task&do=view&id=$id");
  215. } else {
  216. $this->task['csc'] = floatval($result['csc']);
  217. C::t('common_mytask')->update($_G['uid'], $id, array('csc' => $this->task['csc']."\t".$_G['timestamp']));
  218. }
  219. }
  220. } elseif($this->task['status'] == '1') {
  221. if($this->task['period']) {
  222. list($allowapply, $this->task['t']) = $this->checknextperiod($this->task);
  223. } else {
  224. $allowapply = -5;
  225. }
  226. } else {
  227. $allowapply = 1;
  228. }
  229. if(method_exists($taskclass, 'view')) {
  230. $this->task['viewmessage'] = $taskclass->view($this->task, $this->taskvars);
  231. } else {
  232. $this->task['viewmessage'] = '';
  233. }
  234. if($allowapply > 0) {
  235. if($this->task['applyperm'] && $this->task['applyperm'] != 'all' && !(($this->task['applyperm'] == 'member' && $_G['adminid'] == '0') || ($this->task['applyperm'] == 'admin' && $_G['adminid'] > '0') || preg_match("/(^|\t)(".$_G['groupid'].")(\t|$)/", $this->task['applyperm']))) {
  236. $allowapply = -2;
  237. } elseif($this->task['tasklimits'] && $this->task['achievers'] >= $this->task['tasklimits']) {
  238. $allowapply = -3;
  239. }
  240. }
  241. $this->task['dateline'] = dgmdate($this->task['dateline'], 'u');
  242. return $allowapply;
  243. }
  244. function checknextperiod($task) {
  245. global $_G;
  246. $allowapply = false;
  247. $nextapplytime = '';
  248. if($task['periodtype'] == 0) {
  249. $allowapply = TIMESTAMP - $task['dateline'] >= $task['period'] * 3600 ? 2 : -6;
  250. $nextapplytime = tasktimeformat($task['period'] * 3600 - TIMESTAMP + $task['dateline']);
  251. } elseif($task['periodtype'] == 1) {
  252. $todaytimestamp = TIMESTAMP - (TIMESTAMP + $_G['setting']['timeoffset'] * 3600) % 86400;
  253. $allowapply = $task['dateline'] < $todaytimestamp - ($task['period'] - 1) * 86400 ? 2 : -6;
  254. $nextapplytime = ($task['dateline'] - ($task['dateline'] + $_G['setting']['timeoffset'] * 3600) % 86400) + $task['period'] * 86400;
  255. $nextapplytime = dgmdate($nextapplytime);
  256. } elseif($task['periodtype'] == 2 && $task['period'] > 0 && $task['period'] <= 7) {
  257. $task['period'] = $task['period'] != 7 ? $task['period'] : 0;
  258. $todayweek = dgmdate(TIMESTAMP, 'w');
  259. $weektimestamp = TIMESTAMP - ($todayweek - $task['period']) * 86400;
  260. $weekstart = $weektimestamp - ($weektimestamp + $_G['setting']['timeoffset'] * 3600) % 86400;
  261. $weekfirstday = $weekstart - $task['period'] * 86400;
  262. if($task['dateline'] && ($task['dateline'] > $weekstart || $task['dateline'] > $weekfirstday)) {
  263. $allowapply = -6;
  264. if($task['dateline'] > $weekfirstday) {
  265. $weekstart += 604800;
  266. }
  267. $nextapplytime = dgmdate($weekstart);
  268. } else {
  269. $allowapply = 2;
  270. }
  271. } elseif($task['periodtype'] == 3 && $task['period'] > 0) {
  272. list($year, $month) = explode('/', dgmdate(TIMESTAMP, 'Y/n'));
  273. $monthstart = mktime(0, 0, 0, $month, $task['period'], $year);
  274. $monthfirstday = mktime(0, 0, 0, $month, 1, $year);
  275. if($task['dateline'] && ($task['dateline'] > $monthstart || $task['dateline'] > $monthfirstday)) {
  276. $allowapply = -6;
  277. if($task['dateline'] > $monthfirstday) {
  278. $monthstart = mktime(0, 0, 0, $month + 1, $task['period'], $year);
  279. }
  280. $nextapplytime = dgmdate($monthstart);
  281. } else {
  282. $allowapply = 2;
  283. }
  284. }
  285. return array($allowapply, $nextapplytime);
  286. }
  287. function apply($id) {
  288. global $_G;
  289. $this->task = C::t('common_task')->fetch($id);
  290. if($this->task['available'] != 2) {
  291. showmessage('task_nonexistence');
  292. } elseif(($this->task['starttime'] && $this->task['starttime'] > TIMESTAMP) || ($this->task['endtime'] && $this->task['endtime'] <= TIMESTAMP)) {
  293. showmessage('task_offline');
  294. } elseif($this->task['tasklimits'] && $this->task['achievers'] >= $this->task['tasklimits']) {
  295. showmessage('task_full');
  296. }
  297. if($this->task['relatedtaskid'] && !C::t('common_mytask')->count($_G['uid'], $this->task['relatedtaskid'], 1)) {
  298. return -1;
  299. } elseif($this->task['applyperm'] && $this->task['applyperm'] != 'all' && !(($this->task['applyperm'] == 'member' && $_G['adminid'] == '0') || ($this->task['applyperm'] == 'admin' && $_G['adminid'] > '0') || preg_match("/(^|\t)(".$_G['groupid'].")(\t|$)/", $this->task['applyperm']))) {
  300. return -2;
  301. } elseif(!$this->task['period'] && C::t('common_mytask')->count($_G['uid'], $id)) {
  302. return -3;
  303. } elseif($this->task['period']) {
  304. $mytask = C::t('common_mytask')->fetch($_G['uid'], $id);
  305. $task = C::t('common_task')->fetch($id);
  306. $mytask['period'] = $task['period'];
  307. $mytask['periodtype'] = $task['periodtype'];
  308. unset($task);
  309. list($allowapply) = $this->checknextperiod($mytask);
  310. if($allowapply < 0) {
  311. return -4;
  312. }
  313. }
  314. $escript = explode(':', $this->task['scriptname']);
  315. if(count($escript) > 1) {
  316. include_once DISCUZ_ROOT.'./source/plugin/'.$escript[0].'/task/task_'.$escript[1].'.php';
  317. $taskclassname = 'task_'.$escript[1];
  318. } else {
  319. require_once libfile('task/'.$this->task['scriptname'], 'class');
  320. $taskclassname = 'task_'.$this->task['scriptname'];
  321. }
  322. $taskclass = new $taskclassname;
  323. if(method_exists($taskclass, 'condition')) {
  324. $taskclass->condition();
  325. }
  326. C::t('common_mytask')->insert(array(
  327. 'uid' => $_G['uid'],
  328. 'username' => $_G['username'],
  329. 'taskid' => $this->task['taskid'],
  330. 'csc' => '0\t'.$_G['timestamp'],
  331. 'dateline' => $_G['timestamp']
  332. ), false, true);
  333. C::t('common_task')->update_applicants($this->task['taskid'], 1);
  334. if(method_exists($taskclass, 'preprocess')) {
  335. $taskclass->preprocess($this->task);
  336. }
  337. return true;
  338. }
  339. function draw($id) {
  340. global $_G;
  341. if(!($this->task = C::t('common_task')->fetch_by_uid($_G['uid'], $id))) {
  342. showmessage('task_nonexistence');
  343. } elseif($this->task['status'] != 0) {
  344. showmessage('task_not_underway');
  345. } elseif($this->task['tasklimits'] && $this->task['achievers'] >= $this->task['tasklimits']) {
  346. return -1;
  347. }
  348. $escript = explode(':', $this->task['scriptname']);
  349. if(count($escript) > 1) {
  350. include_once DISCUZ_ROOT.'./source/plugin/'.$escript[0].'/task/task_'.$escript[1].'.php';
  351. $taskclassname = 'task_'.$escript[1];
  352. } else {
  353. require_once libfile('task/'.$this->task['scriptname'], 'class');
  354. $taskclassname = 'task_'.$this->task['scriptname'];
  355. }
  356. $taskclass = new $taskclassname;
  357. if(method_exists($taskclass, 'csc')) {
  358. $result = $taskclass->csc($this->task);
  359. } else {
  360. showmessage('task_not_found', '', array('taskclassname' => $taskclassname));
  361. }
  362. if($result === TRUE) {
  363. if($this->task['reward']) {
  364. $rewards = $this->reward();
  365. $notification = $this->task['reward'];
  366. if($this->task['reward'] == 'magic') {
  367. $rewardtext = C::t('common_magic')->fetch($this->task['prize']);
  368. $rewardtext = $rewardtext['name'];
  369. } elseif($this->task['reward'] == 'medal') {
  370. $rewardtext = C::t('forum_medal')->fetch($this->task['prize']);
  371. $rewardtext = $rewardtext['name'];
  372. if(!$this->task['bonus']) {
  373. $notification = 'medal_forever';
  374. }
  375. } elseif($this->task['reward'] == 'group') {
  376. $group = C::t('common_usergroup')->fetch($this->task['prize']);
  377. $rewardtext = $group['grouptitle'];
  378. } elseif($this->task['reward'] == 'invite') {
  379. $rewardtext = $this->task['prize'];
  380. }
  381. notification_add($_G[uid], 'task', 'task_reward_'.$notification, array(
  382. 'taskid' => $this->task['taskid'],
  383. 'name' => $this->task['name'],
  384. 'creditbonus' => $_G['setting']['extcredits'][$this->task['prize']]['title'].' '.$this->task['bonus'].' '.$_G['setting']['extcredits'][$this->task['prize']]['unit'],
  385. 'rewardtext' => $rewardtext,
  386. 'bonus' => $this->task['bonus'],
  387. 'prize' => $this->task['prize'],
  388. ));
  389. }
  390. if(method_exists($taskclass, 'sufprocess')) {
  391. $taskclass->sufprocess($this->task);
  392. }
  393. C::t('common_mytask')->update($_G['uid'], $id, array('status' => 1, 'csc' => 100, 'dateline' => $_G['timestamp']));
  394. C::t('common_task')->update_achievers($id, 1);
  395. if($_G['inajax']) {
  396. $this->message('100', $this->task['reward'] ? 'task_reward_'.$this->task['reward'] : 'task_completed', array(
  397. 'creditbonus' => $_G['setting']['extcredits'][$this->task['prize']]['title'].' '.$this->task['bonus'].' '.$_G['setting']['extcredits'][$this->task['prize']]['unit'],
  398. 'rewardtext' => $rewardtext,
  399. 'bonus' => $this->task['bonus'],
  400. 'prize' => $this->task['prize']
  401. )
  402. );
  403. } else {
  404. return true;
  405. }
  406. } elseif($result === FALSE) {
  407. C::t('common_mytask')->update($_G['uid'], $id, array('status' => -1));
  408. if($_G['inajax']) {
  409. $this->message('-1', 'task_failed');
  410. } else {
  411. return -2;
  412. }
  413. } else {
  414. $result['t'] = $this->timeformat($result['remaintime']);
  415. $this->messagevalues['values'] = array('csc' => $result['csc'], 't' => $result['t']);
  416. if($result['csc']) {
  417. C::t('common_mytask')->update($_G['uid'], $id, array('csc' => $result['csc']."\t".$_G['timestamp']));
  418. $this->messagevalues['msg'] = $result['t'] ? 'task_doing_rt' : 'task_doing';
  419. } else {
  420. $this->messagevalues['msg'] = $result['t'] ? 'task_waiting_rt' : 'task_waiting';
  421. }
  422. if($_G['inajax']) {
  423. $this->message($result['csc'], $this->messagevalues['msg'], $this->messagevalues['values']);
  424. } else {
  425. return -3;
  426. }
  427. }
  428. }
  429. function giveup($id) {
  430. global $_G;
  431. if($_GET['formhash'] != FORMHASH) {
  432. showmessage('undefined_action');
  433. } elseif(!($this->task = C::t('common_task')->fetch_by_uid($_G['uid'], $id))) {
  434. showmessage('task_nonexistence');
  435. } elseif($this->task['status'] != '0') {
  436. showmessage('task_not_underway');
  437. }
  438. C::t('common_mytask')->delete($_G['uid'], $id);
  439. C::t('common_task')->update_applicants($id, -1);
  440. }
  441. function parter($id) {
  442. $parterlist = array();
  443. foreach(C::t('common_mytask')->fetch_all_by_taskid($id, 8) as $parter) {
  444. $parter['avatar'] = avatar($parter['uid'], 'small');
  445. $csc = explode("\t", $parter['csc']);
  446. $parter['csc'] = floatval($csc[0]);
  447. $parterlist[] = $parter;
  448. }
  449. return $parterlist;
  450. }
  451. function delete($id) {
  452. global $_G;
  453. $mytask = C::t('common_mytask')->fetch($_G['uid'], $id);
  454. $this->task = C::t('common_task')->fetch($id);
  455. if($this->task['available'] != 2 || empty($mytask) || $mytask['status'] == 1) {
  456. showmessage('task_nonexistence');
  457. }
  458. if(method_exists($taskclass, 'delete')) {
  459. $taskclass->delete($this->task);
  460. }
  461. C::t('common_mytask')->delete($_G['uid'], $id);
  462. C::t('common_task')->update_applicants($id, -1);
  463. return true;
  464. }
  465. function reward() {
  466. switch($this->task['reward']) {
  467. case 'credit': return $this->reward_credit($this->task['prize'], $this->task['bonus']); break;
  468. case 'magic': return $this->reward_magic($this->task['prize'], $this->task['bonus']); break;
  469. case 'medal': return $this->reward_medal($this->task['prize'], $this->task['bonus']); break;
  470. case 'invite': return $this->reward_invite($this->task['prize'], $this->task['bonus']); break;
  471. case 'group': return $this->reward_group($this->task['prize'], $this->task['bonus']); break;
  472. }
  473. }
  474. function reward_credit($extcreditid, $credits) {
  475. global $_G;
  476. $creditsarray[$extcreditid] = $credits;
  477. updatemembercount($_G['uid'], $creditsarray, 1, 'TRC', $this->task['taskid']);
  478. }
  479. function reward_magic($magicid, $num) {
  480. global $_G;
  481. if(C::t('common_member_magic')->count($_G['uid'], $magicid)) {
  482. C::t('common_member_magic')->increase($_G['uid'], $magicid, array('num' => $num), false, true);
  483. } else {
  484. C::t('common_member_magic')->insert(array(
  485. 'uid' => $_G['uid'],
  486. 'magicid' => $magicid,
  487. 'num' => $num
  488. ));
  489. }
  490. }
  491. function reward_medal($medalid, $day) {
  492. global $_G;
  493. $memberfieldforum = C::t('common_member_field_forum')->fetch($_G['uid']);
  494. $medals = $memberfieldforum['medals'];
  495. unset($memberfieldforum);
  496. if(empty($medals) || !in_array($medalid, explode("\t", $medals))) {
  497. $medalsnew = $medals ? $medals."\t".$medalid : $medalid;
  498. C::t('common_member_field_forum')->update($_G['uid'], array('medals' => $medalsnew), 'UNBUFFERED');
  499. $data = array(
  500. 'uid' => $_G['uid'],
  501. 'medalid' => $medalid,
  502. 'type' => 0,
  503. 'dateline' => TIMESTAMP,
  504. 'expiration' => $day ? TIMESTAMP + $day * 86400 : '',
  505. 'status' => 1,
  506. );
  507. C::t('forum_medallog')->insert($data);
  508. }
  509. }
  510. function reward_invite($num, $day) {
  511. global $_G;
  512. $day = empty($day) ? 5 : $day;
  513. $expiration = $_G['timestamp'] + $day * 86400;
  514. $codes = array();
  515. for ($i=0; $i < $num; $i++) {
  516. $code = strtolower(random(6));
  517. $codes[] = "('$_G[uid]', '$code', '$_G[timestamp]', '$expiration', '$_G[clientip]')";
  518. $invitedata = array(
  519. 'uid' => $_G['uid'],
  520. 'code' => $code,
  521. 'dateline' => $_G['timestamp'],
  522. 'endtime' => $expiration,
  523. 'inviteip' => $_G['clientip']
  524. );
  525. C::t('common_invite')->insert($invitedata);
  526. }
  527. }
  528. function reward_group($gid, $day = 0) {
  529. global $_G;
  530. $exists = FALSE;
  531. if($_G['forum_extgroupids']) {
  532. $_G['forum_extgroupids'] = explode("\t", $_G['forum_extgroupids']);
  533. if(in_array($gid, $_G['forum_extgroupids'])) {
  534. $exists = TRUE;
  535. } else {
  536. $_G['forum_extgroupids'][] = $gid;
  537. }
  538. $_G['forum_extgroupids'] = implode("\t", $_G['forum_extgroupids']);
  539. } else {
  540. $_G['forum_extgroupids'] = $gid;
  541. }
  542. C::t('common_member')->update($_G['uid'], array('extgroupids' => $_G['forum_extgroupids']), 'UNBUFFERED');
  543. if($day) {
  544. $memberfieldforum = C::t('common_member_field_forum')->fetch($_G['uid']);
  545. $groupterms = !empty($memberfieldforum['groupterms']) ? dunserialize($memberfieldforum['groupterms']) : array();
  546. unset($memberfieldforum);
  547. $groupterms['ext'][$gid] = $exists && $groupterms['ext'][$gid] ? max($groupterms['ext'][$gid], TIMESTAMP + $day * 86400) : TIMESTAMP + $day * 86400;
  548. C::t('common_member_field_forum')->update($_G['uid'], array('groupterms' => serialize($groupterms)), 'UNBUFFERED');
  549. }
  550. }
  551. function message($csc, $msg, $values = array()) {
  552. include template('common/header_ajax');
  553. $msg = lang('message', $msg, $values);
  554. echo "$csc|$msg";
  555. include template('common/footer_ajax');
  556. exit;
  557. }
  558. function timeformat($t) {
  559. global $_G;
  560. if($t) {
  561. $h = floor($t / 3600);
  562. $m = floor(($t - $h * 3600) / 60);
  563. $s = floor($t - $h * 3600 - $m * 60);
  564. return ($h ? "$h{$_G['setting']['dlang'][date][4]}" : '').($m ? "$m{$_G[setting][dlang][date][6]}" : '').($h || !$s ? '' : "$s{$_G[setting][dlang][date][7]}");
  565. }
  566. return '';
  567. }
  568. }
  569. function tasktimeformat($t) {
  570. global $_G;
  571. if($t) {
  572. $h = floor($t / 3600);
  573. $m = floor(($t - $h * 3600) / 60);
  574. $s = floor($t - $h * 3600 - $m * 60);
  575. return ($h ? "$h{$_G['lang']['core']['date']['hour']}" : '').($m ? "$m{$_G['lang']['core']['date']['min']}" : '').($h || !$s ? '' : "$s{$_G['lang']['core']['date']['sec']}");
  576. }
  577. return '';
  578. }
  579. ?>