| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Vinzenz Rosenkranz <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author René Gieling <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @license GNU AGPL version 3 or any later version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  *  This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  *  it under the terms of the GNU Affero General Public License as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  *  published by the Free Software Foundation, either version 3 of the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  *  License, or (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  *  This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  *  GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *  You should have received a copy of the GNU Affero General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *  along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | namespace OCA\Polls\Db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use OCP\DB\QueryBuilder\IQueryBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use OCP\IDBConnection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use OCP\AppFramework\Db\QBMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use Doctrine\DBAL\Exception\TableNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @template-extends QBMapper<Log> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  */ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  | class LogMapper extends QBMapper { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 	public function __construct(IDBConnection $db) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 		parent::__construct($db, 'polls_log', '\OCA\Polls\Db\Log'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * @return Log | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	public function find($id): Log { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		$qb = $this->db->getQueryBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		$qb->select('*') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		   ->from($this->getTableName()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		   ->where( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			   $qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		   ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		return $this->findEntity($qb); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * @return Log[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 * @psalm-return array<array-key, Log> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	public function findByPollId($pollId): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$qb = $this->db->getQueryBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		$qb->select('*') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			->from($this->getTableName()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			->where( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		   ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		return $this->findEntities($qb); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * @return Log[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * @psalm-return array<array-key, Log> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	public function findUnprocessed(): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		$qb = $this->db->getQueryBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		$qb->select('*') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			->from($this->getTableName()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			->where( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			   $qb->expr()->eq('processed', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		   ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		return $this->findEntities($qb); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * @return Log[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * @psalm-return array<array-key, Log> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	public function findUnprocessedPolls(): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		$qb = $this->db->getQueryBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$qb->selectDistinct('poll_id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			->from($this->getTableName()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 			->where($qb->expr()->eq('processed', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		return $this->findEntities($qb); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	 * @return Log | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	public function getLastRecord(int $pollId): Log { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		$qb = $this->db->getQueryBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		$qb->select('*') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 			->from($this->getTableName()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			->setMaxResults(1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			->orderBy('id', 'DESC'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		return $this->findEntity($qb); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	public function removeDuplicates() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 			// remove duplicates from oc_polls_log | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			// preserve the first entry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 			$query = $this->db->getQueryBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 			$query->select('id', 'processed', 'poll_id', 'user_id', 'message_id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 				->from($this->getTableName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 			$foundEntries = $query->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 			$delete = $this->db->getQueryBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			$delete->delete($this->getTableName()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 				->where('id = :id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 			$entries2Keep = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			while ($row = $foundEntries->fetch()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 				$currentRecord = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 					$row['processed'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 					$row['poll_id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 					$row['user_id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 					$row['message_id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 				]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 				if (in_array($currentRecord, $entries2Keep)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 					$delete->setParameter('id', $row['id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 					$delete->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 				} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 					$entries2Keep[] = $currentRecord; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		} catch (TableNotFoundException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 			// ignore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	public function deleteByUserId(string $userId): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 		$query = $this->db->getQueryBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 		$query->delete($this->getTableName()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 			->where('user_id = :userId') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 			->setParameter('userId', $userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		$query->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 161 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |  |