-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.php
106 lines (99 loc) · 3.14 KB
/
init.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
session_start();
//init database
require("libs/FluentPDO/FluentPDO.php");
$pdo = new PDO("mysql:host=176.10.154.200;dbname=eyeofsweden", "root", "reftec1234");
$fpdo = new FluentPDO($pdo);
//to get access inside functions
function db(){
global $fpdo;
return $fpdo;
}
function pdodb(){
global $pdo;
return $pdo;
}
//prepare session places
if(!isset($_SESSION['rating'])) $_SESSION['rating'] = array();
if(!isset($_SESSION['history'])) $_SESSION['history'] = array();
function updateRating($videoId,$rating){
$rating = (int)$rating;
//add to history
$_SESSION['history'][$videoId] = $rating;
//get video tags
$videoTags = db()->from('video_tags')->where('video_id = ?',$videoId)->fetchAll();
if(is_array($videoTags)){
foreach($videoTags as $videoTag){
if(!isset($_SESSION['rating']['tag'.$videoTag['tag_id']])){
//set a new
$_SESSION['rating']['tag'.$videoTag['tag_id']] = 0;
}
//update
$_SESSION['rating']['tag'.$videoTag['tag_id']] += ((int)$rating*$videoTag['relevance']);
}
return true;
}
return false;
}
function nextVideosQuery($howMany,$favorites=array(),$watchedIds=array()){
//create query
$query = 'SELECT video_id, SUM(relevance) AS video_rel FROM video_tags ';//select video_id and counter of founded
if(count($favorites)>0 || count($watchedIds)>0) $query.= 'WHERE ';
if(count($favorites)>0) $query.= 'tag_id IN ('.implode(',',$favorites).') ';//check tags selected by user
if(count($favorites)>0 and count($watchedIds)>0) $query.= 'AND ';
if(count($watchedIds)>0) $query.= 'video_id NOT IN ('.implode(',',$watchedIds).') ';//check for not seen videos
$query.= 'GROUP BY video_id ORDER BY video_rel DESC LIMIT '.$howMany;//order by relevance and limit to 1 row
//pepare pdo query
$stmt = pdodb()->prepare($query);
//execute query
$queryOk = $stmt->execute();
//if db error - return false
if(!$queryOk) return false;
//fetch videos from result
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
/**
* Get next video information
* @TODO: returning only not seen videos - what if we run out of videos?
* meybe not seen in latest
* @return boolean|array false if not video found, array if there is a next video
*/
function getNextVideo($markedAsWatched=true,$howMany=1){
//sort
asort($_SESSION['rating']);
//flip
$favorites = array_reverse($_SESSION['rating']);
//best 6
$favorites = array();
foreach(array_slice($favorites,0,6) as $favKey=>$favValue){
$favorites[] = str_replace('tag', '', $favKey);
}
//get next video
$watchedIds = array_keys($_SESSION['history']);
//get videos
$videos = false;
$i=5;
while(!$videos && $i>0){
array_pop($favorites);
$videos = nextVideosQuery($howMany,$favorites,$watchedIds);
$i--;
}
//check if videos are not empty
if(count($videos)>0){
$result = array();
foreach($videos as $videoItem){
//get first video. fuck the rest
$v = db()->from('videos',$videoItem['video_id'])->fetch();
//save as watched with 0 score to not show again
if($markedAsWatched && !isset($_SESSION['history'][$v['id']])){
$_SESSION['history'][$v['id']] = 0;
}
$result[] = $v;
}
//return video
return ($howMany===1) ? $v : $result;
}else{
//no videos found
return false;
}
}