Conflicts:
	htdocs/api/v2/index.php
This commit is contained in:
yang 2015-02-05 15:26:56 +08:00
commit a7b8ab964b
10 changed files with 170 additions and 23 deletions

View File

@ -89,7 +89,7 @@ class DBRecommand extends Database
/**
* 以目前的學習點編號查詢下一個學習點的資訊
* @param string $currentPoint 目前的學習點編號
* @return array 查詢結果
* @return array
*/
public function queryEdgeByID($currentPoint)
{

View File

@ -266,7 +266,7 @@ class DBStudy extends Database {
* @param string $study_id 此記錄編號
* @since 2.0.0
*/
public function toOutTaeget($study_id)
public function toOutTarget($study_id)
{
// 寫入
@ -285,7 +285,7 @@ class DBStudy extends Database {
* @param string $activity_id 活動編號
* @since 2.0.0
*/
public function allToOutTaeget($activity_id)
public function alltoOutTarget($activity_id)
{
// 寫入

View File

@ -51,17 +51,17 @@ class DBTarget extends Database {
foreach($queryResultAll as $key => $thisResult) {
array_push($result,
array( 'target_id' => $thisResult['TID'],
'area_id' => $thisResult['AID'],
'hall_id' => $thisResult['HID'],
'target_number' => $thisResult['TNum'],
array( 'target_id' => (int)$thisResult['TID'],
'area_id' => (int)$thisResult['AID'],
'hall_id' => (int)$thisResult['HID'],
'target_number' => (int)$thisResult['TNum'],
'name' => $thisResult['TName'],
'map_url' => $thisResult['TMapID'],
'learn_time' => $thisResult['TLearnTime'],
'PLj' => $thisResult['PLj'],
'Mj' => $thisResult['Mj'],
'S' => $thisResult['S'],
'Fj' => $thisResult['Fj']
'learn_time' => (int)$thisResult['TLearnTime'],
'PLj' => (int)$thisResult['PLj'],
'Mj' => (int)$thisResult['Mj'],
'S' => (int)$thisResult['S'],
'Fj' => (int)$thisResult['Fj']
));
}
return $result;

View File

@ -1,4 +1,7 @@
<?php
/**
* RecommandPoint.php
*/
namespace UElearning\Recommand;
@ -14,13 +17,17 @@ use UElearning\Database;
/**
* 推薦學習點
*
* Usage:
* $recommand = new RecommandPoint();
*
* @version 2.0.0
* @package UElearning
* @subpackage Study
*/
class RecommandPoint
{
/**
/**
* 正規化參數
*
* @access private
@ -37,6 +44,8 @@ class RecommandPoint
const ALPHA=0.5;
private $recommand;
private $theme;
private $activity;
public function __construct()
@ -52,18 +61,17 @@ class RecommandPoint
private function computeNormalizationParameter($theme_number)
{
$normal = 0; //正規化之後的GAMMA值
$EntitySum = 0; //實體學習點分別算重之後的值
$VirtualSum = 0; //虛擬學習點分別算重之後的值
$EntitySum = 0; //實體學習點分別算重之後的值
$VirtualSum = 0; //虛擬學習點分別算重之後的值
$edge = $this->recommand->queryEdgeByID('0');
$theme = new Study\Theme($theme_number);
for($i=0;$i<count($edge);$i++)
{
$next_point = $edge[$i]["next_point"];
$move_time = $edge[$i]["move_time"];
$next_target = new Target\Target($next_point);
$belong = $this->recommand->queryBelongByID($next_point,$theme->getId());
$belong = $this->recommand->queryBelongByID($next_point,$theme_number);
$weight = $belong["weight"];
$VirtualSum += $weight / $next_target->getLearnTime();
@ -76,12 +84,91 @@ class RecommandPoint
return $EntitySum/$VirtualSum;
}
/**
* 是否為可以學習的標的
* @param $target_id 標的編號
* @return bool enable or not
*/
private function isEnableTargetId($activityObj,$target_id)
{
$target = new Target\Target($target_id);
if(!$activityObj->isTargetLearned($target_id)) return true;
else return false;
}
/**
* 過濾非法的標的
*
* @param array $point_list 全部的標的清單
* @param StudyActivity $activityObj 學習活動物件
* @return array 合法的學習點
*/
private function excludeIrregularTarget($point_list,$activityObj)
{
$regularTarget = array();
for($i=0;$i<count($point_list);$i++)
{
$nextPoint = $point_list[$i]['next_point'];
if($this->isEnableTargetId($activityObj,$nextPoint)) {
array_push($regularTarget,$point_list[$i]);
}
}
return $regularTarget;
}
/**
* 推薦學習點
* @param int $current_point 目前的標的編號
* @param int $theme_number 主題編號
* @param int activity_number 學習活動編號
* @return array 學習點清單
*/
public function recommand($current_point,$theme_number,$activity_number)
public function recommand($current_point,$activity_number)
{
$this->activity = new Study\StudyActivity($activity_number);
$themeID = $this->activity->getThemeId();
$this->theme = new Study\Theme($themeID);
$this->gamma = $this->computeNormalizationParameter($themeID);
$pointList = $this->recommand->queryEdgeByID($current_point);
$targetList = $this->excludeIrregularTarget($pointList,$this->activity);
//計算路徑的權重值
$pathCost = 0;
$VirtualPathCost = 0;
$recommand = array();
$isEntity = true;
for($i=0;$i<count($targetList);$i++)
{
$next_point = $targetList[$i]["next_point"];
$moveTime = $targetList[$i]["move_time"];
$nextPoint = new Target\Target($next_point);
$weight = $this->theme->getWeightByNextTarget($next_point);
if($nextPoint->isFullPeople())
{
$pastCost = 0;
$virtualCost = RecommandPoint::ALPHA * $this->gamma * ($wright/$nextPoint->getLearnTime());
$isEntity=false;
}
else
{
if($nextPoint->isNumberOfPeopleZero()) $Rj = 0;
else $Rj = $nextPoint->getMj()/$nextPoint->getPLj();
$pathCost = RecommandPoint::ALPHA * $this->gamma * ($weight * ($nextPoint->getS()-$Rj+1)/($moveTime + $nextPoint->getLearnTime()));
$virtualCost = RecommandPoint::ALPHA * $this->gamma * ($weight/$nextPoint->getLearnTime());
}
array_push($recommand,array("nextPoint"=>$next_point,
"isEntity" => $isEntity,
"PathCost"=>$pathCost,
"VirtualCost"=>$virtualCost));
}
foreach($recommand as $key=>$value)
{
$tmp[$key] = $value["PathCost"];
}
array_multisort($tmp,SORT_DESC,$recommand,SORT_DESC);
return $recommand;
}
}

View File

@ -7,7 +7,9 @@ namespace UElearning\Study;
require_once UELEARNING_LIB_ROOT.'/Database/DBStudy.php';
require_once UELEARNING_LIB_ROOT.'/Study/Exception.php';
require_once UELEARNING_LIB_ROOT.'/Target/Target.php';
use UElearning\Database;
use UElearning\Target;
use UElearning\Exception;
/**
@ -173,7 +175,15 @@ class Study {
* @since 2.0.0
*/
public function toOut() {
// 將資料庫內容標示已離開
$db = new Database\DBStudy();
$db->toOutTaeget($this->id);
$db->toOutTarget($this->id);
// 將標的目前人數-1
if($this->isEntity()) {
$target = new Target\Target($this->getTargetId);
$target->addMj(-1);
}
}
}

View File

@ -438,6 +438,20 @@ class StudyActivity {
// ========================================================================
/**
* 取得目前已進入的學習點
*
* @return int 標的編號若無則null
*/
public function getCurrentInTarget() {
// 活動編號
$saId = $this->id;
$sct = new StudyManager();
return $sct->getCurrentInTargetId($saId);
}
/**
* 此標的是否已學習過
*

View File

@ -7,7 +7,10 @@ namespace UElearning\Study;
require_once UELEARNING_LIB_ROOT.'/Database/DBStudy.php';
require_once UELEARNING_LIB_ROOT.'/Study/Exception.php';
require_once UELEARNING_LIB_ROOT.'/Target/Target.php';
require_once UELEARNING_LIB_ROOT.'/Target/Exception.php';
use UElearning\Database;
use UElearning\Target;
use UElearning\Exception;
/**
@ -68,14 +71,24 @@ class StudyManager {
* @param int $activity_id 活動編號
* @param int $target_id 標的編號
* @param bool $is_entity 是否為現場學習
* @throw UElearning\Exception\TargetNoFoundException
* return int 進出紀錄編號
*/
public function toInTarget($activity_id, $target_id, $is_entity) {
// 若沒有任一個點正在學習中
if($this->getCurrentInTargetId($activity_id) == null) {
// 紀錄進資料庫
$db = new Database\DBStudy();
return $db->toInTaeget($activity_id, $target_id, $is_entity);
$id = $db->toInTaeget($activity_id, $target_id, $is_entity);
// 將標的目前人數+1
if($is_entity) {
$target = new Target\Target($target_id);
$target->addMj(1);
}
return $id;
}
else {
throw new Exception\InLearningException();
@ -93,12 +106,22 @@ class StudyManager {
// 從資料庫取得此活動此標的學習中資料
$db = new Database\DBStudy();
$learning_array = $db->getInStudyIdByTargetId($activity_id, $target_id);
$target = new Target\Target($target_id);
// 找到正在學習中的資料
if(isset($learning_array)) {
// 將所有此標的的進入紀錄全部標示
foreach($learning_array as $thisArray) {
$db->toOutTaeget($thisArray['study_id']);
// 將此紀錄標示為已離開
$db->toOutTarget($thisArray['study_id']);
// 將標的目前人數-1
if($thisArray['is_entity'] = true) {
$target = new Target\Target($target_id);
$target->addMj(-1);
}
}
}
}

View File

@ -6,6 +6,7 @@
namespace UElearning\Study;
require_once UELEARNING_LIB_ROOT.'/Database/DBTheme.php';
require_once UELEARNING_LIB_ROOT.'/Database/DBRecommand.php';
require_once UELEARNING_LIB_ROOT.'/Study/Exception.php';
use UElearning\Database;
use UElearning\Exception;
@ -176,4 +177,16 @@ class Theme {
return $this->queryResultArray['modify_time'];
}
/**
* 取得下一個標的所屬主題的權重
* @param string $next_point
* @return int weight 權重
* @since 2.0.0
*/
public function getWeightByNextTarget($next_point){
$belong = new Database\DBRecommand();
$weight = $belong->queryBelongByID($next_point,$this->id);
return $weight['weight'];
}
}

View File

@ -105,7 +105,7 @@ class Target {
* @since 2.0.0
*/
public function __construct($inputTID){
$this->tId = $inputTID;
$this->tId = (int)$inputTID;
$this->getQuery();
}