Welcome to mirror list, hosted at ThFree Co, Russian Federation.

member.php « db « lib - github.com/nextcloud/richdocuments.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d44f420aa99d3a5b22c68048a142e7b02c70021f (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php

/**
 * ownCloud - Richdocuments App
 *
 * @author Victor Dubiniuk
 * @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 */

namespace OCA\Richdocuments\Db;

/**
 * @method boolean getIsGuest()
 * @method string getEsId()
 * @method string getToken()
 * @method int getStatus()
 */

class Member extends \OCA\Richdocuments\Db{

	const DB_TABLE = '`*PREFIX*richdocuments_member`';

	const ACTIVITY_THRESHOLD = 90; // 1.5 Minutes
	
	const MEMBER_STATUS_ACTIVE = 1;
	const MEMBER_STATUS_INACTIVE = 2;
	
	protected $tableName  = '`*PREFIX*richdocuments_member`';

	protected $insertStatement  = 'INSERT INTO `*PREFIX*richdocuments_member` (`es_id`, `uid`, `color`, `last_activity`, `is_guest`, `token`)
			VALUES (?, ?, ?, ?, ?, ?)';
	
	protected $loadStatement = 'SELECT * FROM `*PREFIX*richdocuments_member` WHERE `member_id`= ?';

	public static function getGuestPostfix(){
		return '(' . \OC::$server->getL10n('richdocuments')->t('guest') . ')';
	}


	public function updateActivity($memberId){
		return $this->execute(
				'UPDATE ' . $this->tableName . ' SET `last_activity`=?, `status`=? WHERE `member_id`=?',
				array(
					time(),
					self::MEMBER_STATUS_ACTIVE,
					$memberId
				)
		);
	}
	
	
	public function getActiveCollection($esId){
		$result = $this->execute('
			SELECT `es_id`, `member_id`
			FROM ' . self::DB_TABLE . '
			WHERE `es_id`= ?
				AND `status`=?
			',
			array(
				$esId,
				self::MEMBER_STATUS_ACTIVE
			)
		);
		$members = $result->fetchAll();
		if (!is_array($members)){
			$members = array();
		}
		return $members;
		
	}
	
	/**
	 * Mark members as inactive
	 * @param string $esId - session Id
	 * @return array - list of memberId that were marked as inactive
	 */
	public function updateByTimeout($esId){
		$time = $this->getInactivityPeriod();

		$result = $this->execute('
			SELECT `member_id`
			FROM ' . self::DB_TABLE . '
			WHERE `es_id`= ?
				AND `last_activity`<?
				AND `status`=?
			',
			array(
				$esId,
				$time,
				self::MEMBER_STATUS_ACTIVE
			)
		);
		
		$deactivated = $result->fetchAll();
		if (is_array($deactivated) && count($deactivated)){
			$deactivated = array_map(
				function($x){
					return ($x['member_id']);
				}, $deactivated
			);
			$this->deactivate($deactivated);
		} else {
			$deactivated = array();
		}

		return $deactivated;
	}

	/**
	 * Update members to inactive state
	 * @param array $memberIds
	 */
	public function deactivate($memberIds){
		$stmt = $this->buildInQuery('member_id', $memberIds);
		array_unshift($memberIds, self::MEMBER_STATUS_INACTIVE);
		$this->execute('
			UPDATE ' . $this->tableName . '
			SET `status`=?
			WHERE ' . $stmt,
			$memberIds
		);
	}
	
	protected function getInactivityPeriod(){
		return time() - self::ACTIVITY_THRESHOLD;
	}
}