class SJDAEntry { public $entryId; public $userId; public $category; public $entryName; public $entryDescription; public $entryMaterials; public $entryValue; public $entryDate; public $entryStatus; public $finalist; public $winner; private $country; private $experience; private $school; private $education; public $aiReview; public $internalComments; private $files_folder = '/resources/2024/'; private $thumbs_folder = '/resources/2024/thumbnails/'; public function getFriendlyStatus($nospaces=false, $short=false) { $status = $this->entryStatus; if($status=="Accepted") { if($this->finalist>0) { if($short) { $status = "Finalist"; } else { $status = "Finalist"; } } else { if($short) { $status = "In Review"; } else { $status = "In Review"; } } } else if($status=="Disqualified") { $status = "Not Selected"; } if($nospaces) { $status = strtolower(str_replace(" ","_",$status)); } return $status; } public function getCountry() { if(!$this->country) { $user = SJDAUser::getUserByID($this->userId); $this->country = $user->country; } return $this->country; } public function getExperience() { if(!$this->experience) { $user = SJDAUser::getUserByID($this->userId); $this->experience = $user->experience; } return $this->experience; } public function getSchool() { if(!$this->school) { $user = SJDAUser::getUserByID($this->userId); $this->school = $user->school; } return $this->school; } public function getEducation() { if(!$this->education) { $user = SJDAUser::getUserByID($this->userId); $this->education = $user->education; } return $this->education; } public static function getEntryByID($entryId) { $sql = "SELECT * FROM Entries WHERE EntryID=".$entryId; $result = self::getEntriesBySQL($sql); if($result){ return $result[0]; } return null; } public static function getAll($round=-1) { $sql = 'SELECT Entries.*, Users.* FROM Entries INNER JOIN Users ON Entries.UserID=Users.UserID'; if($round==0) { //no filter } else if($round==1) { $sql.=" WHERE Entries.EntryStatus='Accepted'"; } else if($round==2) { $sql.=" WHERE Entries.EntryStatus='Accepted' AND Entries.Finalist>0"; } else if($round==3) { //TBD for this round } return self::getEntriesBySQL($sql); } public static function getEntriesByCategory($aCategory, $userID=null) { global $CURRENT_ROUND; $sql = 'SELECT Entries.EntryID AS EntryID, Entries.UserID AS UserID, Category, EntryName, EntryDescription, '. ' EntryMaterials, EntryValue, EntryDate, EntryStatus, Finalist, Winner, '. ' (Judgement.JudgementID IS NOT NULL) AS IsJudged, '. ' SUM(CASE WHEN Judgement.Round = '.$CURRENT_ROUND; global $user; if($user->role=="Judge") { $sql.=' AND Judgement.UserID='.$user->userId; } $sql .=' THEN Score ELSE 0 END) AS RoundScore, '. ' SUM(Score) - SUM(CASE WHEN Judgement.Round = '.$CURRENT_ROUND.' THEN Score ELSE 0 END) AS PreviousRoundScore, '. ' SUM(Score) as TotalScore '. ' FROM `Entries` LEFT JOIN `Judgement` ON Judgement.EntryID=Entries.EntryID '. ' WHERE Category="'.JDMIS_DB_Escape($aCategory).'" '; if($userID){ $sql.=" AND Entries.UserID=".$userID; } else { //look for sort options in the URL and apply them if(isset($_GET['finalist'])) { $sql.=" AND Finalist=>0"; } else if(isset($_GET['winner'])) { $sql.=" AND Winner>0"; } else if(isset($_GET['unjudged'])) { $sql.=" AND (Judgement.JudgementID IS NULL) AND EntryStatus='Accepted'"; } else if(!isset($_GET['all'])) { if ($CURRENT_ROUND==1) { $sql.=" AND EntryStatus='Accepted'"; } else if($CURRENT_ROUND==2) { $sql.=" AND Finalist>0"; //IsJudged=true"; } else if($CURRENT_ROUND==3) { $sql.=" AND Finalist>0"; } else { //default to showing all entries } } } $sql .= " GROUP BY Entries.EntryID,Entries.UserID,Category,EntryName,EntryDescription,EntryMaterials,EntryValue,EntryDate,EntryStatus,Finalist,Winner"; if(isset($_GET['recent'])) { $sql.=" ORDER BY EntryDate DESC"; } else if(isset($_GET['popular'])) { $sql.=" ORDER BY RoundScore DESC"; } else if(isset($_GET['all'])) { if(isset($_GET['random'])) { $sql.=" ORDER BY RAND()"; } else { $sql.=" ORDER BY EntryDate DESC"; } } else if(isset($_GET['random'])) { $sql.=" ORDER BY RAND()"; } else if($CURRENT_ROUND==2) { $sql.=" ORDER BY RoundScore DESC";//, PreviousRoundScore DESC"; } global $user; //if ($user->role=="Administrator") echo $sql; $result = self::getEntriesBySQL($sql); return $result; } public static function getEntriesBySQL($sql) { $result = JDMIS_DB_ExecuteQuery($sql, false); global $user; if ($user->role=="Administrator") logtoconsole($sql, $result); $entries=array(); if($result){ foreach($result as $row) { $entry = new SJDAEntry(); $entry->entryId = $row['EntryID']; $entry->entryName = $row['EntryName']; $entry->entryDescription = $row['EntryDescription']; $entry->entryStatus = $row['EntryStatus']; $entry->entryMaterials = $row['EntryMaterials']; $entry->entryValue = $row['EntryValue']; $entry->entryDate = $row['EntryDate']; $entry->category = $row['Category']; $entry->userId = $row['UserID']; $entry->finalist = $row['Finalist']; $entry->winner = $row['Winner']; if (isset($row['School'])) $entry->school = $row['School']; if (isset($row['Experience'])) $entry->experience = $row['Experience']; if (isset($row['Education'])) $entry->education = $row['Education']; if (isset($row['Country'])) $entry->country = $row['Country']; if (isset($row['AIReview'])) $entry->aiReview = $row['AIReview']; if (isset($row['InternalComments'])) $entry->internalComments = $row['InternalComments']; $entries[]=$entry; } } return $entries; } public function save() { if ($this->entryId) { logtoconsole("Updating Entry ".$this->entryId); $sql = 'UPDATE Entries SET '. ' EntryName="'. JDMIS_DB_Escape($this->entryName) .'", '. ' EntryDescription="'. JDMIS_DB_Escape($this->entryDescription) .'", '. ' EntryMaterials="'. JDMIS_DB_Escape($this->entryMaterials) .'", '. ' EntryValue="'. JDMIS_DB_Escape($this->entryValue) .'", '. ' EntryStatus="'. JDMIS_DB_Escape($this->entryStatus) .'", '. ' EntryDate="'. JDMIS_DB_Escape($this->entryDate) .'", '. ' Category="'. JDMIS_DB_Escape($this->category) .'", '. ' UserID="'. JDMIS_DB_Escape($this->userId) .'", '. ' Finalist="'. JDMIS_DB_Escape($this->finalist) .'", '. ' Winner="'. JDMIS_DB_Escape($this->winner) .'", '. ' AIReview="'. JDMIS_DB_Escape($this->aiReview) .'", '. ' InternalComments="'. JDMIS_DB_Escape($this->internalComments) .'" '. ' WHERE EntryID='.$this->entryId; $result = JDMIS_DB_ExecuteQuery($sql, true); logtoconsole($sql,$result); if($result) return $this->entryId; } else { logtoconsole("Creating Entry "); $sql = "INSERT INTO Entries (EntryName, EntryDescription, EntryMaterials, EntryValue, EntryStatus, EntryDate, Category, UserID, Finalist, Winner, AIReview, InternalComments) ". 'VALUES ( "'.JDMIS_DB_Escape($this->entryName).'", '. '"'.JDMIS_DB_Escape($this->entryDescription).'", '. '"'.JDMIS_DB_Escape($this->entryMaterials).'", '. '"'.JDMIS_DB_Escape($this->entryValue).'", '. '"'.JDMIS_DB_Escape($this->entryStatus).'", '. 'NOW(), '. '"'.JDMIS_DB_Escape($this->category).'", '. ''.JDMIS_DB_Escape($this->userId).', '. '"'.JDMIS_DB_Escape($this->finalist).'", '. '"'.JDMIS_DB_Escape($this->winner).'", '; '"'.JDMIS_DB_Escape($this->aiReview).'", '; '"'.JDMIS_DB_Escape($this->InternalComments).'" )'; $result = JDMIS_DB_ExecuteQuery($sql, true); $this->entryId = $result; logtoconsole($sql,$result); return $this->userId; } } public function delete() { $files=[]; //Delete thumbnails $files=$this->getThumbnails(); foreach($files as $file) { if (file_exists($_SERVER['DOCUMENT_ROOT'].$file)) { unlink($_SERVER['DOCUMENT_ROOT'].$file); } else { return "ERROR: Thumbnail $file could not be located."; } } //Delete files $files=$this->getFiles(); foreach($files as $file){ if (file_exists($_SERVER['DOCUMENT_ROOT'].$file)) { unlink($_SERVER['DOCUMENT_ROOT'].$file); } else { return "ERROR: Thumbnail $file could not be located."; } } //Delete any feedback //Delete Database entry $sql = "DELETE FROM EntryFeedback WHERE EntryID=".$this->entryId; $result = JDMIS_DB_ExecuteQuery($sql, true); //Delete Database entry $sql = "DELETE FROM Entries WHERE EntryID=".$this->entryId; $result = JDMIS_DB_ExecuteQuery($sql, true); unset($this->entryId); unset($this->entryName); unset($this->entryStatus); return $result; } public function getUser() { return SJDAUser::getUserByID($this->userId); } public function getFeedback() { $feedback=SJDAFeedback::getFeedbackByEntry($this->entryId); return $feedback; } public function getJudgements() { return SJDAJudgement::getJudgementsByEntry($this->entryId); } public function getRoundScore($round) { return SJDAJudgement::getRoundScore($this->entryId, $round); } public function getTotalScore() { return SJDAJudgement::getTotalScore($this->entryId); } public function getVotes() { $votes=array(); logtoconsole("getVotes in ENTRY class has not yet been implemented"); return $votes; } public function entryNameSanitised() { //todo: limit length, remove special chars return str_replace(" ","_",$this->entryName); } public function getFiles() { $output=array(); // Get all matching files $files = glob($_SERVER['DOCUMENT_ROOT'].$this->files_folder.$this->entryId.'_*'); foreach($files as $file) { $info = pathinfo($file); $output[]=$this->files_folder . $info['filename'] . "." . $info['extension']; } return $output; } public function getThumbnails() { $output=array(); $files = $this->getFiles(); foreach($files as $file) { $info = pathinfo($_SERVER['DOCUMENT_ROOT'].$file); $thumbFile = $this->thumbs_folder . $info['filename'] . '.jpg'; if(!file_exists($_SERVER['DOCUMENT_ROOT'].$thumbFile)) { // Create 600x600 thumbnail try { $image = new Imagick($_SERVER['DOCUMENT_ROOT'].$file); $image->thumbnailImage(600, 0); $image->setImageCompressionQuality(80); $image->writeImage($_SERVER['DOCUMENT_ROOT'].$thumbFile); $image->clear(); $image->destroy(); logtoconsole("created thumbnail at ".$thumbFile); } catch (exception $e){ logtoconsole("Unable to make Thumbnail:",$e); } } $output[]=$thumbFile; } return $output; } public function html($isAdmin=false) { $output=""; $output.='
'.$this->entryDescription.'
'; $output.=''.$this->entryDescription.'
'; $output.='