mirror of
				https://gitlab.com/animath/si/plateforme-corres2math.git
				synced 2025-11-04 13:12:20 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			99 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			99 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
class Document
 | 
						|
{
 | 
						|
	private $file_id;
 | 
						|
	private $user_id;
 | 
						|
	private $team_id;
 | 
						|
	private $problem;
 | 
						|
	private $type;
 | 
						|
	private $uploaded_at;
 | 
						|
	private $version;
 | 
						|
 | 
						|
	private function __construct() {}
 | 
						|
 | 
						|
	public static function fromId($id)
 | 
						|
	{
 | 
						|
		global $DB;
 | 
						|
		$req = $DB->prepare("SELECT * FROM `documents` WHERE `file_id` = ?;");
 | 
						|
		$req->execute([htmlspecialchars($id)]);
 | 
						|
		$data = $req->fetch();
 | 
						|
 | 
						|
		if ($data === false)
 | 
						|
			return null;
 | 
						|
 | 
						|
		return self::fromData($data);
 | 
						|
	}
 | 
						|
 | 
						|
	public static function fromData($data)
 | 
						|
	{
 | 
						|
		$doc = new Document();
 | 
						|
		$doc->fill($data);
 | 
						|
		return $doc;
 | 
						|
	}
 | 
						|
 | 
						|
	private function fill($data)
 | 
						|
	{
 | 
						|
		$this->file_id = $data["file_id"];
 | 
						|
		$this->user_id = $data["user"];
 | 
						|
		$this->team_id = $data["team"];
 | 
						|
		$this->problem = $data["problem"];
 | 
						|
		$this->type = DocumentType::fromName($data["type"]);
 | 
						|
		$this->uploaded_at = $data["uploaded_at"];
 | 
						|
		$this->version = isset($data["version"]) ? $data["version"] : 1;
 | 
						|
	}
 | 
						|
 | 
						|
	public function getFileId()
 | 
						|
	{
 | 
						|
		return $this->file_id;
 | 
						|
	}
 | 
						|
 | 
						|
	public function getUserId()
 | 
						|
	{
 | 
						|
		return $this->user_id;
 | 
						|
	}
 | 
						|
 | 
						|
	public function getTeamId()
 | 
						|
	{
 | 
						|
		return $this->team_id;
 | 
						|
	}
 | 
						|
 | 
						|
	public function getProblem()
 | 
						|
	{
 | 
						|
		return $this->problem;
 | 
						|
	}
 | 
						|
 | 
						|
	public function getType()
 | 
						|
	{
 | 
						|
		return $this->type;
 | 
						|
	}
 | 
						|
 | 
						|
	public function getUploadedAt()
 | 
						|
	{
 | 
						|
		return $this->uploaded_at;
 | 
						|
	}
 | 
						|
 | 
						|
	public function getVersion()
 | 
						|
	{
 | 
						|
		return $this->version;
 | 
						|
	}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
	public static function getAllDocuments($problem, $team_id = -1)
 | 
						|
	{
 | 
						|
		global $DB;
 | 
						|
		$req = $DB->query("SELECT * FROM `documents` AS `t1` "
 | 
						|
			. "INNER JOIN (SELECT `user`, `type`, `problem`, MAX(`uploaded_at`) AS `last_upload`, COUNT(`team`) AS `version` FROM `documents` GROUP BY `problem`, `type`, `user`) `t2` "
 | 
						|
			. "ON `t1`.`user` = `t2`.`user` AND `t1`.`type` = `t2`.`type` AND `t1`.`problem` = `t2`.`problem` "
 | 
						|
			. "WHERE `t1`.`uploaded_at` = `t2`.`last_upload` AND `t1`.`problem` = $problem " . ($team_id >= 0 ? "AND `team` = $team_id" : "") . " ORDER BY `t1`.`type`;");
 | 
						|
 | 
						|
		$docs = [];
 | 
						|
 | 
						|
		while (($data = $req->fetch()) !== false)
 | 
						|
			$docs[] = Document::fromData($data);
 | 
						|
 | 
						|
		return $docs;
 | 
						|
	}
 | 
						|
}
 |