*/ /** * @class pointController * @author NAVER (developers@xpressengine.com) * @brief Controller class of point modules */ class pointController extends point { /** * @brief Initialization */ public function init() { } /** * @brief Membership point application trigger */ public function triggerInsertMember($obj) { $member_srl = $obj->member_srl; if (!$member_srl) { return; } $config = $this->getConfig(); $point = intval($config->signup_point); if (!$point) { return; } $cur_point = getModel('point')->getPoint($member_srl, true); $this->setPoint($member_srl, $cur_point + $point, 'signup'); } /** * @brief A trigger to add points to the member for login */ public function triggerAfterLogin($obj) { $member_srl = $obj->member_srl; if (!$member_srl) { return; } // Points are given only once a day. if (substr($obj->last_login, 0, 8) === date('Ymd')) { return; } $config = $this->getConfig(); $point = intval($config->login_point); if (!$point) { return; } $cur_point = getModel('point')->getPoint($member_srl, true); $this->setPoint($member_srl, $cur_point + $point); } /** * @brief Member group deletion trigger */ public function triggerDeleteGroup($obj) { $group_srl = $obj->group_srl; $config = $this->getConfig(); // Exclude deleted group from point/level/group integration if($config->point_group && isset($config->point_group[$group_srl])) { unset($config->point_group[$group_srl]); getController('module')->insertModuleConfig('point', $config); } } /** * @brief A trigger to add points to the member for creating a post */ public function triggerInsertDocument($obj) { $module_srl = $obj->module_srl; $member_srl = abs($obj->member_srl); if (!$module_srl || !$member_srl) { return; } // The fix to disable giving points for saving the document temporarily if ($module_srl == $member_srl) { return; } if ($obj->status === getModel('document')->getConfigStatus('temp')) { return; } // Get the points of the member $cur_point = getModel('point')->getPoint($member_srl, true); // Add points for the document. $document_point = $this->_getModulePointConfig($module_srl, 'insert_document'); $cur_point += $document_point; // Add points for attached files. if ($obj->uploaded_count > 0) { $attached_files_point = $this->_getModulePointConfig($module_srl, 'upload_file'); $cur_point += $attached_files_point * $obj->uploaded_count; } // Increase the point. $this->setPoint($member_srl, $cur_point); } /** * @brief The trigger to give points for normal saving the temporarily saved document * Temporary storage at the point in 1.2.3 changed to avoid payment */ public function triggerUpdateDocument($obj) { $oDocumentModel = getModel('document'); $oDocument = $oDocumentModel->getDocument($obj->document_srl); $module_srl = $oDocument->get('module_srl'); $member_srl = abs($oDocument->get('member_srl')); if (!$module_srl || !$member_srl) { return; } // Only give points if the document is being updated from TEMP to another status such as PUBLIC. if ($obj->status === $oDocumentModel->getConfigStatus('temp') || $oDocument->get('status') !== $oDocumentModel->getConfigStatus('temp')) { if ($obj->uploaded_count > $oDocument->get('uploaded_count')) { $cur_point = getModel('point')->getPoint($member_srl, true); $attached_files_point = $this->_getModulePointConfig($module_srl, 'upload_file'); $cur_point += $attached_files_point * ($obj->uploaded_count - $oDocument->get('uploaded_count')); $this->setPoint($member_srl, $cur_point); } return; } // Get the points of the member $cur_point = getModel('point')->getPoint($member_srl, true); // Add points for the document. $document_point = $this->_getModulePointConfig($module_srl, 'insert_document'); $cur_point += $document_point; // Add points for attached files. if ($obj->uploaded_count > 0) { $attached_files_point = $this->_getModulePointConfig($module_srl, 'upload_file'); $cur_point += $attached_files_point * $obj->uploaded_count; } // Increase the point. $this->setPoint($member_srl, $cur_point); } /** * @brief The trigger which deducts the points related to post comments before deleting the post itself */ public function triggerBeforeDeleteDocument($obj) { } /** * @brief A trigger to give points for deleting the post */ public function triggerDeleteDocument($obj) { $module_srl = $obj->module_srl; $member_srl = abs($obj->member_srl); if (!$module_srl || !$member_srl) { return; } // The fix to disable giving points for saving the document temporarily if ($module_srl == $member_srl) { return; } if ($obj->status === getModel('document')->getConfigStatus('temp')) { return; } // Get the points of the member $cur_point = getModel('point')->getPoint($member_srl, true); // Subtract points for the document. $document_point = $this->_getModulePointConfig($module_srl, 'insert_document'); if ($document_point > 0) { $cur_point -= $document_point; } // Increase the point. $this->setPoint($member_srl, $cur_point); } /** * @brief A trigger which gives points for entering a comment */ public function triggerInsertComment($obj) { $module_srl = $obj->module_srl; $member_srl = abs($obj->member_srl); if (!$module_srl || !$member_srl) { return; } // Abort if the comment and the document have the same author. $oDocument = getModel('document')->getDocument($obj->document_srl); if (!$oDocument->isExists() || abs($oDocument->get('member_srl')) == $member_srl) { return; } // Abort if the document is older than a configured limit. $config = $this->getConfig(); if ($config->no_point_date > 0 && ztime($oDocument->get('regdate')) < time() - ($config->no_point_date * 86400)) { return; } // Get the points of the member $cur_point = getModel('point')->getPoint($member_srl, true); // Add points for the comment. $comment_point = $this->_getModulePointConfig($module_srl, 'insert_comment'); $cur_point += $comment_point; // Add points for attached files. if ($obj->uploaded_count > 0) { $attached_files_point = $this->_getModulePointConfig($module_srl, 'upload_file'); $cur_point += $attached_files_point * $obj->uploaded_count; } // Increase the point. $this->setPoint($member_srl, $cur_point); } /** * @brief A trigger which gives points for uploaded file changes to a comment */ public function triggerUpdateComment($obj) { } /** * @brief A trigger which gives points for deleting a comment */ public function triggerDeleteComment($obj) { $module_srl = $obj->module_srl; $member_srl = abs($obj->member_srl); if (!$module_srl || !$member_srl) { return; } // Abort if the comment and the document have the same author. $oDocument = getModel('document')->getDocument($obj->document_srl); if (!$oDocument->isExists() || abs($oDocument->get('member_srl')) == $member_srl) { return; } // Abort if the document is older than a configured limit. $config = $this->getConfig(); if ($config->no_point_date > 0 && ztime($oDocument->get('regdate')) < ztime($obj->regdate) - ($config->no_point_date * 86400)) { return; } // Get the points of the member $cur_point = getModel('point')->getPoint($member_srl, true); // Add points for the comment. $comment_point = $this->_getModulePointConfig($module_srl, 'insert_comment'); $cur_point -= $comment_point; // Increase the point. $this->setPoint($member_srl, $cur_point); } /** * @brief Add the file registration trigger * To prevent taking points for invalid file registration this method wlil return a null object */ public function triggerInsertFile($obj) { } /** * @brief A trigger to give points for deleting a file * Remove points only in case an invalid file is being deleted */ public function triggerDeleteFile($obj) { $module_srl = $obj->module_srl; $member_srl = abs($obj->member_srl); if (!$module_srl || !$member_srl || $obj->isvalid !== 'Y') { return; } // Get the points of the member $cur_point = getModel('point')->getPoint($member_srl, true); // Subtract points for the file. $file_point = $this->_getModulePointConfig($module_srl, 'upload_file'); $cur_point -= $file_point; // Update the point. $this->setPoint($member_srl, $cur_point); } /** * @brief The trigger called before a file is downloaded */ public function triggerBeforeDownloadFile($obj) { $logged_info = Context::get('logged_info'); $logged_member_srl = $logged_info->member_srl; $author_member_srl = abs($obj->member_srl); $module_srl = $obj->module_srl; if ($logged_member_srl && $logged_member_srl == $author_member_srl) { return; } $point = $this->_getModulePointConfig($module_srl, 'download_file'); if (!$point) { return; } // Get current points. $cur_point = $logged_member_srl ? getModel('point')->getPoint($logged_member_srl) : 0; // If the user (member or guest) does not have enough points, deny access. $config = $this->getConfig(); if ($config->disable_download == 'Y' && $cur_point + $point < 0) { return new BaseObject(-1, 'msg_cannot_download'); } // Points will be adjusted after downloading (triggerDownloadFile). } /** * @brief The trigger to give or take points for downloading the file */ public function triggerDownloadFile($obj) { $logged_info = Context::get('logged_info'); $logged_member_srl = $logged_info->member_srl; $author_member_srl = abs($obj->member_srl); $module_srl = $obj->module_srl; if ($logged_member_srl && $logged_member_srl == $author_member_srl) { return; } // Adjust points of the downloader. if ($logged_member_srl) { $point = $this->_getModulePointConfig($module_srl, 'download_file'); if ($point) { $cur_point = getModel('point')->getPoint($logged_member_srl); $this->setPoint($logged_member_srl, $cur_point + $point); } } // Adjust points of the uploader. if ($author_member_srl) { $point = $this->_getModulePointConfig($module_srl, 'download_file_author'); if ($point) { $cur_point = getModel('point')->getPoint($author_member_srl); $this->setPoint($author_member_srl, $cur_point + $point); } } } /** * @brief Give points for hits increase * Run it even if there are no points */ public function triggerUpdateReadedCount($obj) { $logged_info = Context::get('logged_info'); $logged_member_srl = $logged_info->member_srl; $author_member_srl = abs($obj->get('member_srl')); $module_srl = $obj->get('module_srl'); if ($logged_member_srl && $logged_member_srl == $author_member_srl) { return; } // Load configuration for reader and author points. $reader_point = $this->_getModulePointConfig($module_srl, 'read_document'); $author_point = $this->_getModulePointConfig($module_srl, 'read_document_author'); if (!$reader_point && !$author_point) { return; } // If the reader has already read this document, do not adjust points again. if ($logged_member_srl) { $args = new stdClass; $args->member_srl = $logged_member_srl; $args->document_srl = $obj->document_srl; $output = executeQuery('document.getDocumentReadedLogInfo', $args); if ($output->data->count) { return; } } // Adjust points of the reader. if ($reader_point) { // Get current points. $cur_point = $logged_member_srl ? getModel('point')->getPoint($logged_member_srl) : 0; // If the reader does not have enough points, deny access. if ($cur_point + $reader_point < 0 && $this->getConfig()->disable_read_document == 'Y') { $message = sprintf(lang('msg_disallow_by_point'), abs($reader_point), $cur_point); $obj->add('content', $message); $_SESSION['banned_document'][$obj->document_srl] = true; return new BaseObject(-1, $message); } else { $_SESSION['banned_document'][$obj->document_srl] = false; } // Record the fact that this member has already read this document. if ($logged_member_srl) { $args = new stdClass(); $args->member_srl = $logged_member_srl; $args->document_srl = $obj->document_srl; $output = executeQuery('document.insertDocumentReadedLog', $args); $this->setPoint($logged_member_srl, $cur_point + $reader_point); } } // Adjust points of the person who wrote the document. if ($author_point && $author_member_srl) { $cur_point = getModel('point')->getPoint($author_member_srl); $this->setPoint($author_member_srl, $cur_point + $author_point); } } /** * @brief Points for voting up or down */ public function triggerUpdateVotedCount($obj) { $logged_info = Context::get('logged_info'); $logged_member_srl = $logged_info->member_srl; $target_member_srl = abs($obj->member_srl); if ($logged_member_srl && $logged_member_srl == $target_member_srl) { return; } // Document or comment? $is_comment = isset($obj->comment_srl) && $obj->comment_srl; // Adjust points of the voter. if ($logged_member_srl) { $config_key = ($obj->point > 0) ? ($is_comment ? 'voter_comment' : 'voter') : ($is_comment ? 'blamer_comment' : 'blamer'); $point = $this->_getModulePointConfig($obj->module_srl, $config_key); if ($point) { if (isset($obj->cancel) && $obj->cancel) { $point = -1 * $point; } $cur_point = getModel('point')->getPoint($logged_member_srl, true); $this->setPoint($logged_member_srl, $cur_point + $point); } } // Adjust points of the person who wrote the document or comment. if ($target_member_srl) { $config_key = ($obj->point > 0) ? ($is_comment ? 'voted_comment' : 'voted') : ($is_comment ? 'blamed_comment' : 'blamed'); $point = $this->_getModulePointConfig($obj->module_srl, $config_key); if ($point) { if (isset($obj->cancel) && $obj->cancel) { $point = -1 * $point; } $cur_point = getModel('point')->getPoint($target_member_srl, true); $this->setPoint($target_member_srl, $cur_point + $point); } } } /** * @brief Copy point settings when copying module */ public function triggerCopyModule($obj) { $oModuleModel = getModel('module'); $pointConfig = $oModuleModel->getModulePartConfig('point', $obj->originModuleSrl); $oModuleController = getController('module'); if(is_array($obj->moduleSrlList)) { foreach($obj->moduleSrlList AS $key=>$moduleSrl) { $oModuleController->insertModulePartConfig('point', $moduleSrl, $pointConfig); } } } /** * @brief Set points */ public function setPoint($member_srl, $point, $mode = null) { $member_srl = abs($member_srl); $mode_arr = array('add', 'minus', 'update', 'signup'); if(!$mode || !in_array($mode,$mode_arr)) $mode = 'update'; // Get configuration information $oMemberModel = getModel('member'); $oModuleModel = getModel('module'); $oPointModel = getModel('point'); $config = $oModuleModel->getModuleConfig('point'); // Get the default configuration information $current_point = $oPointModel->getPoint($member_srl, true); $current_level = $oPointModel->getLevel($current_point, $config->level_step); // Change points $args = new stdClass(); $args->member_srl = $member_srl; $args->point = $current_point; switch($mode) { case 'add' : $args->point += $point; break; case 'minus' : $args->point -= $point; break; case 'update' : case 'signup' : $args->point = $point; break; } if($args->point < 0) $args->point = 0; $point = $args->point; // Call a trigger (before) $trigger_obj = new stdClass(); $trigger_obj->member_srl = $args->member_srl; $trigger_obj->mode = $mode; $trigger_obj->current_point = $current_point; $trigger_obj->current_level = $current_level; $trigger_obj->set_point = $point; $trigger_output = ModuleHandler::triggerCall('point.setPoint', 'before', $trigger_obj); if(!$trigger_output->toBool()) { return $trigger_output; } // begin transaction $oDB = &DB::getInstance(); $oDB->begin(); // If there are points, update, if no, insert $oPointModel = getModel('point'); if($oPointModel->isExistsPoint($member_srl)) executeQuery("point.updatePoint", $args); else executeQuery("point.insertPoint", $args); // Get a new level $level = $oPointModel->getLevel($point, $config->level_step); // If existing level and a new one are different attempt to set a point group $new_group_list = array(); $del_group_list = array(); if ($config->group_ratchet === 'Y') { $change_group = ($level > $current_level); } else { $change_group = ($level != $current_level); } if ($change_group) { // Check if the level, for which the current points are prepared, is calculate and set the correct group $point_group = $config->point_group; // If the point group exists if($point_group && is_array($point_group) && count($point_group) ) { // Get the default group $default_group = $oMemberModel->getDefaultGroup(); asort($point_group); // Reset group after initialization if($config->group_reset != 'N') { // If the new level is in the right group if(in_array($level, $point_group)) { // Delete all groups except the one which the current level belongs to foreach($point_group as $group_srl => $target_level) { $del_group_list[] = $group_srl; if($target_level == $level) $new_group_list[] = $group_srl; } } // Otherwise, in case the level is reduced, add the recent group else { $i = $level; while($i > 0) { if(in_array($i, $point_group)) { foreach($point_group as $group_srl => $target_level) { if($target_level == $i) { $new_group_list[] = $group_srl; } } $i = 0; } $i--; } } // Delete the group of a level which is higher than the current level foreach($point_group as $group_srl => $target_level) { if($target_level > $level) $del_group_list[] = $group_srl; } $del_group_list[] = $default_group->group_srl; } // Grant a new group else { // Check until the current level by rotating setting the configurations of the point groups foreach($point_group as $group_srl => $target_level) { $del_group_list[] = $group_srl; if($target_level <= $level) $new_group_list[] = $group_srl; } } // If there is no a new group, granted the default group if(!$new_group_list[0]) $new_group_list[0] = $default_group->group_srl; // Remove linkage group if($del_group_list && count($del_group_list)) { $del_group_args = new stdClass; $del_group_args->member_srl = $member_srl; $del_group_args->group_srl = implode(',', $del_group_list); $del_group_output = executeQuery('point.deleteMemberGroup', $del_group_args); } // Grant a new group foreach($new_group_list as $group_srl) { $new_group_args = new stdClass; $new_group_args->member_srl = $member_srl; $new_group_args->group_srl = $group_srl; executeQuery('member.addMemberToGroup', $new_group_args); } } } // Call a trigger (after) $trigger_obj->new_group_list = $new_group_list; $trigger_obj->del_group_list = $del_group_list; $trigger_obj->new_level = $level; ModuleHandler::triggerCall('point.setPoint', 'after', $trigger_obj); $oDB->commit(); // Cache Settings $cache_key = sprintf('member:point:%d', $member_srl); $cache_path = sprintf('./files/member_extra_info/point/%s/', getNumberingPath($member_srl)); $cache_filename = sprintf('%s%d.cache.txt', $cache_path, $member_srl); if (Rhymix\Framework\Cache::getDriverName() !== 'dummy') { Rhymix\Framework\Cache::set($cache_key, $point); Rhymix\Framework\Storage::delete($cache_filename); } else { Rhymix\Framework\Storage::write($cache_filename, $point); } getController('member')->_clearMemberCache($member_srl); unset(self::$_member_point_cache[$member_srl]); return $output; } /** * Get point configuration for module, falling back to defaults if not set. * * @param int $module_srl * @param string $config_key * @return int */ protected function _getModulePointConfig($module_srl, $config_key) { $module_srl = intval($module_srl); $config_key = strval($config_key); if (!$config_key) { return 0; } $oModuleModel = getModel('module'); if ($module_srl) { if (!isset(self::$_module_config_cache[$module_srl])) { self::$_module_config_cache[$module_srl] = $oModuleModel->getModulePartConfig('point', $module_srl); } $module_config = self::$_module_config_cache[$module_srl]; } else { $module_config = array(); } if (isset($module_config[$config_key]) && $module_config[$config_key] !== '') { $point = $module_config[$config_key]; } else { $default_config = $this->getConfig(); $point = $default_config->{$config_key}; } return intval($point); } } /* End of file point.controller.php */ /* Location: ./modules/point/point.controller.php */