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.='
'; $output.='
'.$this->getFriendlyStatus(false, true).'
'; $output.='
'; $output.='
'.$this->entryName.'
'; $output.='
'; $output.=' '; $output.='
'; $output.='
'; $output.='
'; $output.=' '; $output.='
'; $output.='
'; $output.='

'.$this->entryDescription.'

'; $output.='
'; if($isAdmin) { $output.='
'; } else { global $user; if ($user->role=="Judge") { global $CURRENT_ROUND; global $rankeval; if($CURRENT_ROUND==2) { if($this->finalist>0 && $rankeval<6) { $judgement = SJDAJudgement::getCurrentJudgement($this->entryId,$CURRENT_ROUND,$user->userId); if($judgement) { $output.=''; $output.='
'; $rankeval=(7-$judgement->score); } else { $output.='
'; } } } } } $output.='
'; return $output; } public function emailhtml() { $output=""; $output.='
'; $output.='
'; $output.='
'.$this->entryName.'
'; $output.='
'; $output.=' '; $output.='
'; $output.='
'; $output.='
'; $output.=' '; $output.='
'; return $output; } } ?>feedbackId = $row['FeedbackID']; $fb->entryId = $row['EntryID']; $fb->fromId = $row['FromID']; $fb->messageDate = $row['MessageDate']; $fb->messageText = $row['MessageText']; $fb->messageRead = $row['MessageRead']; $feedback[]=$fb; } } return $feedback; } public function save() { if ($this->feedbackId) { logtoconsole("Updating EntryFeedback ".$this->feedbackId); $sql = 'UPDATE EntryFeedback SET '. ' EntryID="'. JDMIS_DB_Escape($this->entryId) .'", '. ' FromID="'. JDMIS_DB_Escape($this->entryDescription) .'", '. ' MessageDate="'. JDMIS_DB_Escape($this->messageDate) .'", '. ' MessageText="'. JDMIS_DB_Escape($this->messageText) .'", '. ' MessageRead="'. JDMIS_DB_Escape($this->messageRead) .'" '. ' WHERE FeedbackID='.$this->feedbackId; $result = JDMIS_DB_ExecuteQuery($sql, true); logtoconsole($sql,$result); if($result) return $this->feedbackId; } else { logtoconsole("Creating EntryFeedback "); $sql = "INSERT INTO EntryFeedback (EntryID, FromID, MessageDate, MessageText) ". 'VALUES ( '.JDMIS_DB_Escape($this->entryId).', '. JDMIS_DB_Escape($this->fromId).', '. 'NOW(), '. '"'.JDMIS_DB_Escape($this->messageText).'" )'; $result = JDMIS_DB_ExecuteQuery($sql, true); $this->feedbackId = $result; logtoconsole($sql,$result); return $this->feedbackId; } } public function delete() { $sql = "DELETE FROM EntryFeedback WHERE FeedbackID=".$this->feedbackId; $result = JDMIS_DB_ExecuteQuery($sql, true); unset($this->feedbackId); unset($this->messageText); unset($this->fromId); return $result; } public function getFromUser() { return SJDAUser::getUserByID($this->fromId); } public function getEntry() { return SJDAEntry::getEntryByID($this->entryId); } } judgementId = $row['JudgementID']; $entry->userId = $row['UserID']; $entry->entryId = $row['EntryID']; $entry->round = $row['Round']; $entry->score = $row['Score']; $entry->judgementDate = $row['JudgementDate']; $entry->judgeName = $row['JudgeName']; $entries[]=$entry; } } return $entries; } public function save() { if ($this->judgementId) { logtoconsole("Updating Judgement ".$this->judgementId); $sql = ' UPDATE Judgement SET '. ' UserID= "'. JDMIS_DB_Escape($this->userId) .'", '. ' EntryID= "'. JDMIS_DB_Escape($this->entryId) .'", '. ' Round= "'. JDMIS_DB_Escape($this->round) .'", '. ' Score= "'. JDMIS_DB_Escape($this->score) .'", '. ' JudgementDate="'. JDMIS_DB_Escape($this->judgementDate) .'" '. ' WHERE JudgementID='.$this->judgementId; $result = JDMIS_DB_ExecuteQuery($sql, true); logtoconsole($sql,$result); if($result) return $this->entryId; } else { logtoconsole("Creating Entry "); $sql = "INSERT INTO Judgement (UserID, EntryID, Round, Score, JudgementDate) ". 'VALUES ( "'. JDMIS_DB_Escape($this->userId) .'", '. ' "'. JDMIS_DB_Escape($this->entryId) .'", '. ' "'. JDMIS_DB_Escape($this->round) .'", '. ' "'. JDMIS_DB_Escape($this->score) .'", '. ' NOW() '. ' )'; $result = JDMIS_DB_ExecuteQuery($sql, true); $this->judgementId = $result; logtoconsole($sql,$result); return $this->judgementId; } } public function getUser() { $user=SJDAUser::getUserByID($this->userId); } public function getEntry() { $entry=SJDAEntry::getEntryByID($this->entryId); } public static function getTotalScore($entryId) { $sql = "SELECT SUM(Score) AS TotalScore FROM Judgement WHERE EntryID=$entryId"; $result = JDMIS_DB_ExecuteQuery($sql, false); if($result){ return $result[0]['TotalScore']; } else { return -2; } } public static function getRoundScore($entryId, $round) { $sql = "SELECT SUM(Score) AS RoundScore FROM Judgement WHERE EntryID=$entryId AND Round=$round"; $result = JDMIS_DB_ExecuteQuery($sql, false); if($result){ return $result[0]['RoundScore']; } else { return -2; } } public static function getJudgement($entryId, $round, $userId) { $result = SJDAJudgement::search($entryId, $round, $userId); if($result){ return $result[0]; } else { //return a new justdement $judgement = new SJDAJudgement(); $judgement->entryId = $entryId; $judgement->round = $round; $judgement->userId = $userId; $judgement->score = 0; return $judgement; } } public static function getJudgeScore($entryId, $round, $userId) { $judgement=SJDAJudgement::getJudgement($entryId, $round, $userId); return $judgement->score; } public function delete() { if ($this->judgementId) { logtoconsole("Deleting Judgement ".$this->judgementId); $sql = ' DELETE FROM Judgement WHERE JudgementID='.$this->judgementId; $result = JDMIS_DB_ExecuteQuery($sql, true); logtoconsole($sql,$result); return $result; } } }