Skip to content

Commit

Permalink
Version 6.3 add support for video history
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel authored and daniel committed Nov 28, 2018
1 parent 32177e8 commit fe3c80c
Show file tree
Hide file tree
Showing 19 changed files with 409 additions and 175 deletions.
1 change: 0 additions & 1 deletion .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ Options All -Indexes
RewriteRule ^setCategoryVideo$ objects/videoCategory.json.php [NC,L]
RewriteRule ^reencodeVideo$ objects/videoReencode.json.php [NC,L]
RewriteRule ^rotateVideo$ objects/videoRotate.json.php [NC,L]
RewriteRule ^addViewCountVideo$ objects/videoAddViewCount.json.php [NC,L]


# Subscribes
Expand Down
1 change: 0 additions & 1 deletion classic.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@
RewriteRule ^setCategoryVideo$ objects/videoCategory.json.php [NC,L]
RewriteRule ^reencodeVideo$ objects/videoReencode.json.php [NC,L]
RewriteRule ^rotateVideo$ objects/videoRotate.json.php [NC,L]
RewriteRule ^addViewCountVideo$ objects/videoAddViewCount.json.php [NC,L]


# Subscribes
Expand Down
2 changes: 1 addition & 1 deletion install/checkConfiguration.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

$installationVersion = "6.2";
$installationVersion = "6.3";


header('Content-Type: application/json');
Expand Down
3 changes: 3 additions & 0 deletions install/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ CREATE TABLE IF NOT EXISTS `videos_statistics` (
`ip` VARCHAR(45) NULL,
`users_id` INT NULL,
`videos_id` INT NOT NULL,
`created` DATETIME NULL DEFAULT NULL,
`modified` DATETIME NULL DEFAULT NULL,
`lastVideoTime` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `fk_videos_statistics_users1_idx` (`users_id` ASC),
INDEX `fk_videos_statistics_videos1_idx` (`videos_id` ASC),
Expand Down
100 changes: 72 additions & 28 deletions objects/video.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,22 @@ function __construct($title = "", $filename = "", $id = 0) {
}
}

function addView() {
function addView($currentTime = 0) {
global $global;
if (empty($this->id)) {
return false;
}
$sql = "UPDATE videos SET views_count = views_count+1, modified = now() WHERE id = ?";


$insert_row = sqlDAL::writeSql($sql, "i", array($this->id));

if ($insert_row) {
VideoStatistic::save($this->id);
$obj = new stdClass();
$obj->videos_statistics_id = VideoStatistic::create($this->id, $currentTime);
$obj->videos_id = $this->id;
$this->views_count++;
YouPHPTubePlugin::addView($this->id, $this->views_count);
return $this->id;
return $obj;
} else {
die($sql . ' Error : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
}
Expand Down Expand Up @@ -161,7 +162,7 @@ function save($updateVideoGroups = false, $allowOfflineUser = false) {
if (empty($this->next_videos_id)) {
$this->next_videos_id = 'NULL';
}

$this->rate = floatval($this->rate);
if (!empty($this->id)) {
if (!$this->userCanManageVideo()) {
Expand Down Expand Up @@ -417,7 +418,7 @@ function setStatus($status) {
if (!empty($this->id)) {
global $global;
$sql = "UPDATE videos SET status = ?, modified = now() WHERE id = ? ";
$res = sqlDAL::writeSql($sql,'si',array($status, $this->id));
$res = sqlDAL::writeSql($sql, 'si', array($status, $this->id));
if ($global['mysqli']->errno != 0) {
die('Error on update Status: (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
}
Expand Down Expand Up @@ -553,16 +554,16 @@ static function getVideo($id = "", $status = "viewable", $ignoreGroup = false, $

if ($status == "viewable") {
$sql .= " AND v.status IN ('" . implode("','", Video::getViewableStatus($showUnlisted)) . "')";
} elseif ($status == "viewableNotUnlisted") {
} elseif ($status == "viewableNotUnlisted") {
$sql .= " AND v.status IN ('" . implode("','", Video::getViewableStatus(false)) . "')";
}elseif (!empty($status)) {
} elseif (!empty($status)) {
$sql .= " AND v.status = '{$status}'";
}

if (!empty($_GET['catName'])) {
$sql .= " AND (c.clean_name = '{$_GET['catName']}' OR c.parentId IN (SELECT cs.id from categories cs where cs.clean_name = '{$_GET['catName']}' ))";
}

if (!empty($_GET['channelName'])) {
$user = User::getChannelOwner($_GET['channelName']);
$sql .= " AND v.users_id = {$user['id']} ";
Expand Down Expand Up @@ -600,6 +601,7 @@ static function getVideo($id = "", $status = "viewable", $ignoreGroup = false, $
$video['groups'] = UserGroups::getVideoGroups($video['id']);
$video['title'] = UTF8encode($video['title']);
$video['description'] = UTF8encode($video['description']);
$video['progress'] = self::getVideoPogressPercent($video['id']);
}
} else {
$video = false;
Expand Down Expand Up @@ -704,7 +706,7 @@ static function getAllVideos($status = "viewable", $showOnlyLoggedUserVideos = f
if (!empty($_GET['catName'])) {
$sql .= " AND (c.clean_name = '{$_GET['catName']}' OR c.parentId IN (SELECT cs.id from categories cs where cs.clean_name = '{$_GET['catName']}' ))";
}

if (!empty($_GET['channelName'])) {
$user = User::getChannelOwner($_GET['channelName']);
$sql .= " AND v.users_id = {$user['id']} ";
Expand Down Expand Up @@ -742,6 +744,7 @@ static function getAllVideos($status = "viewable", $showOnlyLoggedUserVideos = f
$row['statistc_month'] = VideoStatistic::getStatisticTotalViews($row['id'], false, $previewsMonth, $today);
$row['statistc_unique_user'] = VideoStatistic::getStatisticTotalViews($row['id'], true);
}
$row['progress'] = self::getVideoPogressPercent($row['id']);
$row['category'] = xss_esc_back($row['category']);
$row['groups'] = UserGroups::getVideoGroups($row['id']);
$row['tags'] = self::getTags($row['id']);
Expand Down Expand Up @@ -784,18 +787,18 @@ static function getAllVideosLight($status = "viewable", $showOnlyLoggedUserVideo
} else {
$sql .= " AND v.status IN ('" . implode("','", Video::getViewableStatus($showUnlisted)) . "')";
}
}elseif ($status == "viewableNotUnlisted") {
} elseif ($status == "viewableNotUnlisted") {
$sql .= " AND v.status IN ('" . implode("','", Video::getViewableStatus(false)) . "')";
} elseif (!empty($status)) {
$sql .= " AND v.status = '{$status}'";
}


if (!empty($_GET['channelName'])) {
$user = User::getChannelOwner($_GET['channelName']);
$sql .= " AND v.users_id = {$user['id']} ";
}

$res = sqlDAL::readSql($sql);
$fullData = sqlDAL::fetchAllAssoc($res);
sqlDAL::close($res);
Expand Down Expand Up @@ -853,7 +856,7 @@ static function getTotalVideos($status = "viewable", $showOnlyLoggedUserVideos =
$sql .= " AND v.type = '{$_SESSION['type']}' ";
}
}

if (!empty($_GET['channelName'])) {
$user = User::getChannelOwner($_GET['channelName']);
$sql .= " AND v.users_id = {$user['id']} ";
Expand Down Expand Up @@ -1027,14 +1030,14 @@ function delete() {
}
return $resp;
}

private function removeTrailerReference($videos_id) {
if (!$this->userCanManageVideo()) {
return false;
}

global $global;

if (!empty($videos_id)) {
$videoURL = self::getLink($videos_id, '', true);
$sql = "UPDATE videos SET trailer1 = '' WHERE trailer1 = ?";
Expand Down Expand Up @@ -1079,10 +1082,10 @@ function setDescription($description) {
}

function setCategories_id($categories_id) {
if(!Category::userCanAddInCategory($categories_id)){
if (!Category::userCanAddInCategory($categories_id)) {
return false;
}

// to update old cat as well when auto..
if (!empty($this->categories_id)) {
$this->old_categories_id = $this->categories_id;
Expand Down Expand Up @@ -1854,9 +1857,9 @@ static function get_id_from_clean_title($clean_title) {
static function getLinkToVideo($videos_id, $clean_title = "", $embed = false, $type = "URLFriendly", $get = array()) {
global $global;
$get_http = http_build_query($get);
if(empty($get_http)){
if (empty($get_http)) {
$get_http = "";
}else{
} else {
$get_http = "?{$get_http}";
}
if ($type == "URLFriendly") {
Expand Down Expand Up @@ -1897,7 +1900,7 @@ static function getPermaLinkFromCleanTitle($clean_title, $embed = false, $get =
return self::getLinkToVideo("", $clean_title, $embed, "permalink", $get);
}

static function getURLFriendlyFromCleanTitle($clean_title, $embed = false, $get = array()) {
static function getURLFriendlyFromCleanTitle($clean_title, $embed = false, $get = array()) {
return self::getLinkToVideo("", $clean_title, $embed, "URLFriendly", $get);
}

Expand Down Expand Up @@ -1930,38 +1933,38 @@ static function getTotalVideosThumbsUpFromUser($users_id, $startDate, $endDate)
$sql = "SELECT id from likes WHERE videos_id = ? AND `like` = 1 ";
if (!empty($startDate)) {
$sql .= " AND `created` >= ? ";
$format .="s";
$format .= "s";
$values[] = $startDate;
}

if (!empty($endDate)) {
$sql .= " AND `created` <= ? ";
$format .="s";
$format .= "s";
$values[] = $endDate;
}
$res = sqlDAL::readSql($sql, $format, $values);
$countRow = sqlDAL::num_rows($res);
sqlDAL::close($res);
$r['thumbsUp']+=$countRow;
$r['thumbsUp'] += $countRow;

$format = "";
$values = array();
$sql = "SELECT id from likes WHERE videos_id = {$row['id']} AND `like` = -1 ";
if (!empty($startDate)) {
$sql .= " AND `created` >= ? ";
$format .="s";
$format .= "s";
$values[] = $startDate;
}

if (!empty($endDate)) {
$sql .= " AND `created` <= ? ";
$format .="s";
$format .= "s";
$values[] = $endDate;
}
$res = sqlDAL::readSql($sql, $format, $values);
$countRow = sqlDAL::num_rows($res);
sqlDAL::close($res);
$r['thumbsDown']+=$countRow;
$r['thumbsDown'] += $countRow;
}
}

Expand All @@ -1983,6 +1986,47 @@ static function deleteThumbs($filename) {
}
}

static function getVideoPogress($videos_id, $users_id = 0) {
if (empty($users_id)) {
if (!User::isLogged()) {
return 0;
}
$users_id = User::getId();
}

return VideoStatistic::getLastVideoTimeFromVideo($videos_id, $users_id);
}

static function getVideoPogressPercent($videos_id, $users_id = 0) {
$lastVideoTime = self::getVideoPogress($videos_id, $users_id);

if (empty($lastVideoTime)) {
return array('percent' => 0, 'lastVideoTime' => 0);
}

// start incremental search and save
$sql = "SELECT duration FROM `videos` WHERE id = ? LIMIT 1";
$res = sqlDAL::readSql($sql, "i", array($videos_id));
$row = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);

if (empty($row) || empty($row['duration'])) {
return array('percent' => 0, 'lastVideoTime' => 0);
}

$duration = parseDurationToSeconds($row['duration']);

if (empty($duration)) {
return array('percent' => 0, 'lastVideoTime' => 0);
}

if ($lastVideoTime > $duration) {
return array('percent' => 100, 'lastVideoTime' => $lastVideoTime);
}

return array('percent' => ($lastVideoTime / $duration) * 100, 'lastVideoTime' => $lastVideoTime);
}

}

}
Expand Down
10 changes: 9 additions & 1 deletion objects/videoAddViewCount.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@
$resp = $obj->addView();
$seconds = parseDurationToSeconds($obj->getDuration());
$_SESSION['addViewCount'][$_POST['id']] = strtotime("+{$seconds} seconds");
}else if(!empty ($_POST['currentTime'])){
$resp = VideoStatistic::updateStatistic($obj->getId(), User::getId(), intval($_POST['currentTime']));
}else{
$resp = 0;
}
$count = $obj->getViews_count();
echo '{"status":"'.!empty($resp).'","count":"'.$count.'"}';

$obj2 = new stdClass();
$obj2->status = !empty($resp);
$obj2->count = $count;
$obj2->resp = $resp;

echo json_encode($obj2);
Loading

0 comments on commit fe3c80c

Please sign in to comment.