DISCUZ架构:积分系统代码分析三
3.下载附件积分增减
这一部分主要用到的就是attachment.php这个文件了,下面就来分析这个文件中与积分中有关系的代码:
以下为引用的内容:
if(!$isimage) {
$forum['getattachcredits'] = $forum['getattachcredits'] ? unserialize($forum['getattachcredits']) : array();
$getattachcredits = $forum['getattachcredits'] ? $forum['getattachcredits'] : $creditspolicy['getattach'];
updatecredits($discuz_uid, $getattachcredits, -1);
}
这一段的意思是:当一个附件不是图像的时候,取出该论坛对于下载附件的积分设置,比方说是-5分,那么就调用updatecredits(global.func中定义)这个函数更新一下积分。
P.S.:相关函数分析:
updatepostcredits函数,定义于./include/post.func.php
以下为引用的内容:
function updatepostcredits($operator, $uidarray, $creditsarray) {
global $db, $tablepre, $discuz_uid, $timestamp;
$membersarray = $postsarray = array();
foreach((is_array($uidarray) ? $uidarray : array($uidarray)) as $id) {
$membersarray[intval(trim($id))]++;
}
foreach($membersarray as $uid => $posts) {
$postsarray[$posts][] = $uid;
}
$lastpostadd = $uidarray == $discuz_uid ? ", lastpost='$timestamp'" : '';
$creditsadd1 = '';
if(is_array($creditsarray)) {
foreach($creditsarray as $id => $addcredits) {
$creditsadd1 .= ", extcredits$id=extcredits$id$operator$addcredits*\$posts";
}
}
foreach($postsarray as $posts => $uidarray) {
$uids = implode(',', $uidarray);
eval("\$creditsadd2 = \"$creditsadd1\";");
$db->query("UPDATE {$tablepre}members SET posts=posts+('$operator$posts') $lastpostadd $creditsadd2 WHERE uid IN ($uids)", 'UNBUFFERED');
}
}
可以看到这个函数有三个传入参数,$operator表示加还是减,$uidarray表示会员的id数组,$creditsarray表示要加减的积分数组。调用方法如:
以下为引用的内容:
updatepostcredits('+', 1, 1);
表示给uid为1的会员加1个积分