mirror of
https://github.com/chenasraf/nextcloud-forum.git
synced 2026-05-18 01:28:58 +00:00
118 lines
3.0 KiB
PHP
118 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
// SPDX-FileCopyrightText: Chen Asraf <contact@casraf.dev>
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
namespace OCA\Forum\Db;
|
|
|
|
use OCA\Forum\AppInfo\Application;
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
use OCP\AppFramework\Db\QBMapper;
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
use OCP\IDBConnection;
|
|
|
|
/**
|
|
* @template-extends QBMapper<ForumUser>
|
|
*/
|
|
class ForumUserMapper extends QBMapper {
|
|
public function __construct(
|
|
IDBConnection $db,
|
|
) {
|
|
parent::__construct($db, Application::tableName('forum_users'), ForumUser::class);
|
|
}
|
|
|
|
/**
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
|
|
* @throws DoesNotExistException
|
|
*/
|
|
public function find(int $id): ForumUser {
|
|
/* @var $qb IQueryBuilder */
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->getTableName())
|
|
->where(
|
|
$qb->expr()
|
|
->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
|
|
);
|
|
return $this->findEntity($qb);
|
|
}
|
|
|
|
/**
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
|
|
* @throws DoesNotExistException
|
|
*/
|
|
public function findByUserId(string $userId): ForumUser {
|
|
/* @var $qb IQueryBuilder */
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->getTableName())
|
|
->where(
|
|
$qb->expr()
|
|
->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
|
|
);
|
|
return $this->findEntity($qb);
|
|
}
|
|
|
|
/**
|
|
* @return array<ForumUser>
|
|
*/
|
|
public function findAll(): array {
|
|
/* @var $qb IQueryBuilder */
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')->from($this->getTableName());
|
|
return $this->findEntities($qb);
|
|
}
|
|
|
|
/**
|
|
* Count all forum users
|
|
*/
|
|
public function countAll(): int {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select($qb->func()->count('*', 'count'))
|
|
->from($this->getTableName());
|
|
$result = $qb->executeQuery();
|
|
$row = $result->fetch();
|
|
$result->closeCursor();
|
|
return (int)($row['count'] ?? 0);
|
|
}
|
|
|
|
/**
|
|
* Count users created since a timestamp
|
|
*/
|
|
public function countSince(int $timestamp): int {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select($qb->func()->count('*', 'count'))
|
|
->from($this->getTableName())
|
|
->where($qb->expr()->gte('created_at', $qb->createNamedParameter($timestamp, IQueryBuilder::PARAM_INT)));
|
|
$result = $qb->executeQuery();
|
|
$row = $result->fetch();
|
|
$result->closeCursor();
|
|
return (int)($row['count'] ?? 0);
|
|
}
|
|
|
|
/**
|
|
* Get top contributors by post count
|
|
*
|
|
* @return array<array<string, mixed>>
|
|
*/
|
|
public function getTopContributors(int $limit = 5): array {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('user_id', 'post_count')
|
|
->from($this->getTableName())
|
|
->orderBy('post_count', 'DESC')
|
|
->setMaxResults($limit);
|
|
$result = $qb->executeQuery();
|
|
$contributors = [];
|
|
while ($row = $result->fetch()) {
|
|
$contributors[] = [
|
|
'userId' => $row['user_id'],
|
|
'postCount' => (int)$row['post_count'],
|
|
];
|
|
}
|
|
$result->closeCursor();
|
|
return $contributors;
|
|
}
|
|
}
|