kdb = $kdb; //mida kuradit!!! if ( is_numeric($rid)) { $commData = $kdb->getCommonData($rid); $this->setData($commData); } else { $this->newObject(); } } function setData($commData) { $this->setResourceID($commData['resourceID']); $this->setDeleted($commData['deleted']); $this->setFolderid($commData['folderid']); $this->setTitle($commData['title']); $this->setUserID((int)$commData['userID']); $this->setCreated($commData['created']); $this->setModified($commData['modified']); $this->setDescription($commData['description']); $this->setKeywords($commData['keywords']); $this->setAuthors($commData['authors']); $this->setRating($commData['rating']); $this->setState($commData['state']); $this->setSecurity($commData['security']); $this->waramuUID = $commData['waramuUID']; } function newObject() { global $user; $this->setResourceID("new"); $this->setDeleted(0); if ( isset($_GET['folderid']) && is_numeric($_GET['folderid']) ) { $this->setFolderid($_GET['folderid']); } else if ( isset($_SESSION['folderid']) && is_numeric($_SESSION['folderid']) ) { $this->setFolderid($_SESSION['folderid']); } $this->setTitle(""); $this->setUserID((int)$user->getId()); $this->setCreated('0000-00-00 00:00:00'); $this->setModified('0000-00-00 00:00:00'); $this->setDescription(""); $this->setKeywords(""); $this->setAuthors($this->getFullname($this->getUserID())); $this->setRating(0); } function updateData($commData) { $this->setTitle($commData['title']); $this->setDescription($commData['description']); $this->setKeywords($commData['keywords']); $this->setAuthors($commData['authors']); } public function getImages() { //return $this->kdb->getResourcesByType("Image", array("userID"=>$this->user->getId())); return $this->kdb->getResourcesByType("Image", array("security"=>"'public'")); } public function getVAuthor() { global $user; $ud = $user->getDetails(); $fval = "BEGIN:VCARD\n"; $fval .= "VERSION:3.0\n"; $fval .= "N:".$ud->getLastname().";".$ud->getFirstname().";;;\n"; $fval .= "FN:".$ud->getFullname()."\n"; $fval .= "ORG:".$user->getSchool()."\n"; $fval .= "EMAIL;TYPE=internet,pref:".$ud->getEmail()."\n"; $fval .= "END:VCARD\n"; return $fval; } protected function save() { // save if ( $this->getResourceID() == 'new' ) { $sql = "INSERT INTO resources (type, folderid, title, description, keywords, authors, userID) values ('%s', %s, '%s', '%s', '%s', '%s', %s)"; $sql = sprintf($sql, $this->getType(), $this->getFolderid() ? $this->getFolderid() : 'NULL', $this->getTitle(), $this->getDescription(), $this->keywords, $this->getFullname($this->getUserID()), $this->getUserID()); $this->kdb->query($sql); $dbuid = mysql_insert_id(); $this->setResourceID($dbuid); $this->kdb->query("INSERT INTO language (resourceID, language) values (".$dbuid.", 'et')"); } else { $sql = "UPDATE resources SET title='%s', description='%s', keywords='%s', authors='%s', modified=NOW() WHERE resourceID=%s"; $sql = sprintf($sql, $this->getTitle(), $this->getDescription(), $this->keywords, $this->authors, $this->getResourceID() ); $this->kdb->query($sql); } if (isset($_POST)) { $this->setMetaData($_POST); } $this->updateFulltext(); } protected function copy() { global $user; $sql = "INSERT INTO resources (type, folderid, title, description, keywords, authors, userID) values ('%s', %s, '%s', '%s', '%s', '%s', %s)"; $fid = 'NULL'; if ( isset($_POST['folderid']) && is_numeric($_POST['folderid']) && $_POST['folderid']>0) { $fid = $_POST['folderid']; } $sql = sprintf($sql, $this->getType(), $fid, $this->getTitle(), $this->getDescription(), $this->keywords, $this->getAuthors(), $user->getID()); $this->kdb->query($sql); $dbuid = mysql_insert_id(); $this->setResourceID($dbuid); $this->updateFulltext(); } protected function makeResourceFromXML($xml) { global $user; $sql = "INSERT INTO resources (type, folderid, title, description, keywords, authors, userID) values ('%s', %s, '%s', '%s', '%s', '%s', %s)"; $fid = 'NULL'; if ( isset($_POST['folderid']) && is_numeric($_POST['folderid']) && $_POST['folderid']>0) { $fid = $_POST['folderid']; } $sql = sprintf($sql, $xml['type'], $fid, $xml['title'], $xml['description'], $xml['keywords'], $xml['authors'], $user->getID()); $this->kdb->query($sql); $dbuid = mysql_insert_id(); $this->setResourceID($dbuid); $this->setMetaData($xml, true); $this->updateFulltext(); } protected function updateFulltext() { $sql_del = "DELETE FROM fti WHERE resourceID=".$this->getResourceID(); $this->kdb->query($sql_del); $sql_fti = "INSERT INTO fti values (%s, '%s')"; $fti = $this->getTitle()." ".$this->getDescription()." ".$this->getKeywords(); if ( method_exists($this, "fulltext") ) { $fti .= " ".$this->fulltext(); } $this->kdb->query(sprintf($sql_fti, $this->getResourceID(), $fti)); } protected function isAnswerer() { $aobj = $this->getAnswrObject(); if ($aobj->getUserID()==$this->user->getID()) { return true; } return false; } public function addJS() { foreach ($this->js as $j) { $this->krihvel->out("head", ''); } if ( $this->mode == "assignments" ) { $header = ''; $this->krihvel->out("head", $header); } } public function addCSS() { foreach ($this->css as $c) { $this->krihvel->out("head", ''); } } public function getFullname($uid=false) { if ($uid) { $userid = $uid; } else { if ($this->mode=='answer') { $aob = $this->getAnswerObject(); $userid = $aob->getUserID(); } else { $userid = $this->getUserID(); } } $user = new User((int)$userid); return $user->getDetails()->getFullnameDisplay(); } protected function setResourceID($rid) { $this->resourceID = $rid; } public function getResourceID() { return $this->resourceID; } protected function setDeleted($d) { $this->deleted = $d; } public function getDeleted() { return $this->deleted; } public function getType() { return $this->dtype; } protected function setFolderid($f) { $this->folderid = $f; } public function getFolderid() { return $this->folderid; } protected function setTitle($t) { $this->title = $t; } public function getTitle() { return $this->title; } protected function setUserID($u) { $this->userID = $u; } public function getUserID() { return $this->userID; } public function getState() { return $this->state; } public function setState($val) { if ( $val != 'draft' && $val != 'final' ) { throw new Exception('hack hack'); } $this->state = $val; } public function validIsDraft() { if ($this->getState() == 'draft' ) { return 0; } return 1; } public function validIsFinal() { if ($this->getState() == 'final' ) { return 0; } return 1; } public function validIsMyDraft() { if (!$this->isOwner()) { return 0; } if (!$this->validIsDraft()){ return 1; } return 0; } public function validIsMyPrivate() { if (!$this->isOwner()) { return 0; } if (!$this->validIsPrivate()){ return 1; } return 0; } public function validIsSecured() { if (!$this->validIsMyDraft() && !$this->validIsMyPrivate()){ return 0; } return 1; } public function validIsPrivate() { if ($this->getSecurity() == 'private' ) { return 0; } return 1; } public function validHasSettings() { if (!in_array($this->getType(), array('gallery'))) { return 0; } return 1; } public function validIsPublic() { global $user, $krihvel; $roles = $user->getRoles(); if ( $roles["authenticated"] > 0 ) { return 0; } else { $aob = $this->getAnswerObject(); if ( $aob->getExponate()) { return 0; } } return 1; } public function issetAid() { if ( isset($_GET['aid']) ) { return 0; } return 1; } public function getSecurity() { return $this->security; } public function setSecurity($val) { if ( $val != 'public' && $val != 'private' ) { throw new Exception('hack hack'); } $this->security = $val; } public function setWaramuUID($val) { $this->waramuUID = $val; $this->kdb->query("UPDATE resources SET waramuUID='".$val."' WHERE resourceID=".$this->getResourceID()); } public function getWaramuUID() { return $this->waramuUID; } protected function setCreated($c) { $this->created = $c; } public function getCreated() { return $this->created; } public function getFormattedCreated() { date_default_timezone_set('UTC'); if ($this->mode=='answer') { $aob = $this->getAnswerObject(); $d = strtotime($aob->getCreated()); }if ($this->getResourceID()=="new") { $d = strtotime("NOW"); }else { $d = strtotime($this->created); } return strftime("%e %B %G", $d); } public function getFormattedToday() { date_default_timezone_set('UTC'); $d = strtotime("NOW"); return strftime("%e %B %G", $d); } protected function setModified($m) { $this->modified = $m; } public function getModified() { return $this->modified; } protected function setDescription($d) { $this->description = $d; } public function getDescription() { return $this->description; } protected function setKeywords($kws) { $kws_raw = preg_split("/,/", $kws); $kws = ","; foreach ($kws_raw as $kw_raw){ $kw = trim($kw_raw); $kws .= mb_strtolower($kw, "UTF-8").","; } $this->keywords = $kws; } public function getKeywords() { $k = trim($this->keywords, ","); $kws_raw = preg_split("/,/", $k); $kws = implode(", ", $kws_raw); return $kws; } protected function setAuthors($kws) { $kws_raw = preg_split("/,/", $kws); $kws = ","; foreach ($kws_raw as $kw_raw){ $kw = trim($kw_raw); $kws .= $kw.","; } $this->authors = $kws; } public function getAuthors() { $k = trim($this->authors, ","); $kws_raw = preg_split("/,/", $k); $kws = implode(", ", $kws_raw); return $kws; } public function getAnswerObject() { if ($this->answer_object != NULL) { return $this->answer_object; } $at = $this->getType()."Answer"; $aob = NULL; if (isset($_GET['aid']) && is_numeric($_GET['aid'])) { $aob = $this->kdb->getAnswerByID($_GET['aid'], $at); } else if (isset($_POST['aid']) && is_numeric($_POST['aid'])) { $aob = $this->kdb->getAnswerByID($_POST['aid'], $at); } else { $aob = $this->kdb->getAnswerByLatest($this->getResourceID(), $this->user->getId(), $at); } $this->answer_object = $aob; return $aob; } public function getAnswers() { $assid = false; $at = $this->getType()."Answer"; if (isset($_GET['assignmentid'])) { $assid = $_GET['assignmentid']; return $this->kdb->getAnswerByRID($this->getResourceId(), array('assignmentID'=>$assid, 'order'=>array('by'=>'created', 'order'=>'ASC')), $at); } return $this->kdb->getAnswerByRID($this->getResourceId(), array(), $at); } public function getNumberOfAnswers() { return $this->kdb->hasAnswers($this->getResourceId()); } public function getOverallRating() { $rating = $this->getRating(); return $this->getRatingStars($rating); } public function getMyRating() { $rating = $this->kdb->getRatingByResource($this->getResourceId()); if ($rating) { return $rating; } return false; } public function getRComments() { return $this->kdb->getCommentsByResource($this->getResourceId()); } public function getRatingStars($rating) { $stars = array(0,0,0,0,0); for ($r=0; $r<$rating; $r++) { if ($rating>$r) { $stars[$r] = 1; if ($rating-$r<1) { $stars[$r] = 2; } } } return $stars; } public function getRating() { return $this->rating; } protected function setRating($r) { $this->rating = $r; } public function getFolders() { global $kdb, $user; $ret = $kdb->query("SELECT * FROM folders WHERE userID=".$user->getId()); $res = array(); while ( $row = mysql_fetch_array($ret) ) { $res[] = $row; } return $res; } function getCurrentFolderID() { return $this->getFolderid(); } function getMyExhibitions() { global $kdb, $user; $q = "SELECT * FROM exhibitions WHERE security='public' AND userID=".$user->getId(); $ret = $kdb->query($q); $exs = array(); while ( $row = mysql_fetch_array($ret) ) { $exs []= $row; } return $exs; } function getNumberOfExponates($exhib) { $ret = $this->kdb->query("SELECT exponateID FROM exponates WHERE exhibitionID=".$exhib); return mysql_num_rows($ret); } function getIsExponated($exhib) { if ( isset($_GET['aid']) ) { $ret = $this->kdb->query("SELECT * FROM exponates WHERE answerID=".$_GET['aid']." AND exhibitionID=".$exhib); while ( $row = mysql_fetch_array($ret) ) { return true; } } return false; } /** * @Secured('roles' = {'member'}, 'valid' = {}) * */ function actionSetExponate() { $expids = array(); if ( isset($_POST['exponate']) && is_array($_POST['exponate']) ) { $expids = array_keys($_POST['exponate']); } $aid = $_POST['aid']; $ret = $this->kdb->query("SELECT * FROM exponates WHERE answerID=".$aid); $exhibs = array(); while ( $row = mysql_fetch_array($ret) ) { if (in_array($row['exhibitionID'], $expids)) { $exhibs []= $row['exhibitionID']; } else { $this->kdb->query("DELETE from exponates WHERE exhibitionID=".$row['exhibitionID']." AND answerID=".$aid); $this->kdb->query("UPDATE answers SET exponate=0 WHERE answerID=".$aid); } } foreach ($expids as $expid) { if (!in_array($expid, $exhibs)) { $this->kdb->query("INSERT INTO exponates (exhibitionID, answerID) VALUES (".$expid.", ".$aid.")"); $this->kdb->query("UPDATE answers SET exponate=1 WHERE answerID=".$aid); } } $_SESSION['krihvel_notice'] = gettext("Exponates set"); return array('rid' => $this->getResourceID(), 'mode' => "answer", 'aid' => $_POST['aid']); } /** * @Secured('roles' = {'member'}, 'valid' = {}) * */ public function actionGrade() { if (isset($_POST['grade'])) { $sql = "UPDATE answers SET grade='%s', comment='%s' WHERE answerID=%s"; $this->kdb->query(sprintf($sql, $_POST['grade'], $_POST['comment'], $_POST['aid'])); } $_SESSION['krihvel_notice'] = gettext("Grade saved"); return array('rid' => $this->getResourceID(), 'mode' => "answer", 'aid' => $_POST['aid']); } /** * @Secured('roles' = {'member'}, 'valid' = {}) * */ public function actionExhibitions() { return array('rid' => $this->getResourceID(), 'mode' => "exhibitions", 'aid' => $_POST['aid']); } /** * @Secured('roles' = {'member'}, 'valid' = {}) * */ public function actionAddToExponates() { $this->kdb->query(sprintf("UPDATE answers SET exponate=1 WHERE answerID=".$_POST['aid'])); $_SESSION['krihvel_notice'] = gettext("Answer is now in exhibition"); return array('rid' => $this->getResourceID(), 'mode' => "answer", 'aid' => $_POST['aid']); } /** * @Secured('roles' = {'member'}, 'valid' = {}) * */ public function actionRemoveFromExponates() { $this->kdb->query(sprintf("UPDATE answers SET exponate=0 WHERE answerID=".$_POST['aid'])); $_SESSION['krihvel_notice'] = gettext("Answer removed from exhibition"); return array('rid' => $this->getResourceID(), 'mode' => "answer", 'aid' => $_POST['aid']); } public function isOwner() { if ( isset($_GET['debug'])) { $this->krihvel->out("debug", "Object owner: ".$this->getUserID()." user id:". $this->user->getId()); } if ( $this->getUserID() == $this->user->getId() ) { return 0; } return 1; } public function getMDFDB($md) { $tgs = array(); if (is_numeric($this->getResourceID())) { $ret = $this->kdb->query("SELECT ".$md." FROM ".$md." WHERE resourceID=".$this->getResourceID()); while ( $row = mysql_fetch_array($ret) ) { $tgs []= $row[$md]; } } return $tgs; } protected function setMetaData($pd, $xml=false) { foreach (array('targetgroup','language','resourcetype','lre','difficulty') as $md) { if (isset($pd[$md])) { if (!$xml) { $this->kdb->query("DELETE from ".$md." WHERE resourceID=".$this->getResourceID()); $mda = $pd[$md]; } else { $mda = unserialize($pd[$md]); } foreach ($mda as $tg) { $this->kdb->query("INSERT INTO ".$md." (resourceID, ".$md.") values (".$this->getResourceID().", '".$tg."')"); } } } } public function useImage($iid) { if ($iid) { $q = "INSERT INTO usedimages (resourceID, imageUID) SELECT ".$this->getResourceID().", '".$iid."' FROM dual WHERE not exists (SELECT * FROM usedimages WHERE usedimages.resourceID = ".$this->getResourceID()." AND usedimages.imageUID = '".$iid."')"; $this->kdb->query($q); } } public function removeUsedImages() { $q = "DELETE FROM usedimages WHERE resourceID=".$this->getResourceID(); $this->kdb->query($q); } function getUsedImages() { $q = "SELECT imageUID FROM usedimages WHERE resourceID=".$this->getResourceID(); $ret = $this->kdb->query($q); $uids = array(); while ( $row = mysql_fetch_array($ret) ) { $uids []= $row['imageUID']; } return $uids; } /** * @Secured('roles' = {'authenticated'}, 'valid' = {'isOwner'}) * */ public function actionSaveSettings() { if (isset($_POST['status'])) { $this->setState($_POST['status']); } $this->setSecurity($_POST['security']); $this->kdb->query("UPDATE resources SET state='".$this->getState()."', security='".$this->getSecurity()."' WHERE resourceID=".$this->getResourceID()); $_SESSION['krihvel_notice'] = gettext("Saved"); if ( !strcmp($this->getState(), 'final') && !strcmp($this->getSecurity(), 'public') && !in_array($this->getType(),array('link'))) { return array('rid' => $this->getResourceID(), 'mode' => 'assignments'); } return array('rid' => $this->getResourceID(), 'mode' => 'view'); } /** * @Secured('roles' = {'member','manager'}, 'valid' = {'isOwner'}) * */ public function actionDeleteFromWaramu() { $_SESSION['krihvel_notice'] = gettext("Deletion from Waramu failed"); if ( $this->getWaramuUID() ) { global $user; $au = ''.$user->getUsername().''; require_once("Waramu.php"); $ws = new Waramu(); $ws->connect(); $r = $ws->deleteResource($au, $this->getWaramuUID()); $ws->disconnect(); $this->setWaramuUID(NULL); $_SESSION['krihvel_notice'] = gettext("Resource deleted from Waramu"); } else { $_SESSION['krihvel_notice'] = gettext("Resource not in Waramu"); } return array('rid' => $this->getResourceID()); } /** * @Secured('roles' = {'member','manager'}, 'valid' = {'isOwner'}) * */ public function actionExportToWaramu() { $_SESSION['krihvel_notice'] = gettext("Export to Waramu failed"); if ( !$this->getWaramuUID() ) { global $user; $au = ''.$user->getUsername().''; require_once("Waramu.php"); $ws = new Waramu(); $ws->connect(); $r = $ws->newResource($au, $this->toWaramu()); if ( is_int($r) ) { // error } else { $this->setWaramuUID($r); } $zipcontent = $this->makeThisAsZipFile(); $ares = $ws->addAttachment($au, $this->getWaramuUID(), $zipcontent, $this->getTitle().'.zip'); $ws->disconnect(); $_SESSION['krihvel_notice'] = gettext("Resource exported to Waramu"); } else { $_SESSION['krihvel_notice'] = gettext("Resource already in Waramu"); } return array('rid' => $this->getResourceID()); } public function toWaramu() { $xml = ''; $xml .= ''; $xml .= ''.$this->dtype.''; $xml .= ''.$this->getTitle().''; $xml .= ''.$this->getDescription().''; $xml .= ''.$this->getKeywords().''; $xml .= ''.$this->getVAuthor().''; //$xml .= '0'; // agerange minimum //$xml .= '99'; // agerange maximum $xml .= ''.$this->getMDFDB('targetgroup').''; $xml .= ''.$this->getMDFDB('resourcetype').''; $xml .= 'http://creativecommons.org/licenses/by-sa/3.0/'; $xml .= ''.$this->getMDFDB('language').''; $xml .= ''.$this->getMDFDB('difficulty').''; $xml .= ''.$this->getMDFDB('lre').''; $xml .= ''; return $xml; } public function makeBaseXML() { $xml = 'dtype.']]>\n'; $xml .= 'cname.']]>\n'; $xml .= '<![CDATA['.$this->getTitle().']]>\n'; $xml .= 'getDescription().']]>\n'; $xml .= 'authors.']]>\n'; $xml .= 'keywords.']]>\n'; $xml .= 'getFullname().']]>\n'; $xml .= 'getCreated().']]>\n'; $xml .= 'getMDFDB('targetgroup')).']]>\n'; $xml .= 'getMDFDB('language')).']]>\n'; $xml .= 'getMDFDB('lre')).']]>\n'; $xml .= 'getMDFDB('difficulty')).']]>\n'; $xml .= 'getMDFDB('resourcetype')).']]>\n'; $xml .= '\n'; return $xml; } public function makeImageObjectsXml($rid) { $xml = '\n'; $xml .= '\n'; $iobs = $this->kdb->getResourcesByType("image", array('resourceID'=>$rid), 1); if ($iobs) { $iob = $iobs[0]; $xml .= 'dtype.']]>\n'; $xml .= 'cname.']]>\n'; $xml .= '<![CDATA['.$iob->getTitle().']]>\n'; $xml .= 'getDescription().']]>\n'; $xml .= 'authors.']]>\n'; $xml .= 'keywords.']]>\n'; $xml .= 'getFullname().']]>\n'; $xml .= 'getCreated().']]>\n'; $xml .= 'getMDFDB('targetgroup')).']]>\n'; $xml .= 'getMDFDB('language')).']]>\n'; $xml .= 'getMDFDB('lre')).']]>\n'; $xml .= 'getMDFDB('difficulty')).']]>\n'; $xml .= 'getMDFDB('resourcetype')).']]>\n'; $xml .= '\n'; $xml .= $iob->makeXML(); } $xml .= '\n'; return $xml; } public function makeThisAsZipFile() { $xml = '\n'; $xml .= '\n'; $xml .= $this->makeBaseXML(); $xml .= $this->makeXML(); $xml .= '\n'; $contains_images = $this->getUsedImages(); $zip = new ZipArchive(); $filename = tempnam(sys_get_temp_dir(), 'Krihvel'); if ( $zip->open($filename, ZIPARCHIVE::OVERWRITE) !== TRUE ) { $_SESSION['krihvel_notice'] = gettext("Cannot open"). " <$filename>\n"; return array('rid' => $this->getResourceID(), 'mode' => 'settings'); } $zip->addFromString("resource_data.xml", $xml); if ($contains_images) { foreach ($contains_images as $imid) { $res = $this->kdb->query("SELECT * FROM images WHERE imageUID='".$imid."'"); $im = mysql_fetch_array($res); $image = $zip->addFile(IMAGES_PATH.$im['resourceID'], $im['imageName']); $zip->addFromString($im['imageName'].".xml", $this->makeImageObjectsXml($im['resourceID'])); } } $zip->close(); $zc = file_get_contents($filename); unlink($filename); return $zc; } /** * @Secured('roles' = {'authenticated'}, 'valid' = {'isOwner'}) * */ public function actionExportResource() { $xml = '\n'; $xml .= '\n'; $xml .= $this->makeBaseXML(); $xml .= $this->makeXML(); $xml .= '\n'; $contains_images = $this->getUsedImages(); $zip = new ZipArchive(); $filename = tempnam(sys_get_temp_dir(), 'Krihvel'); if ( $zip->open($filename, ZIPARCHIVE::OVERWRITE) !== TRUE ) { $_SESSION['krihvel_notice'] = gettext("Cannot open"). " <$filename>\n"; return array('rid' => $this->getResourceID(), 'mode' => 'settings'); } $zip->addFromString("resource_data.xml", $xml); if ($contains_images) { foreach ($contains_images as $imid) { $res = $this->kdb->query("SELECT * FROM images WHERE imageUID='".$imid."'"); $im = mysql_fetch_array($res); $image = $zip->addFile(IMAGES_PATH.$im['resourceID'], $im['imageName']); $zip->addFromString($im['imageName'].".xml", $this->makeImageObjectsXml($im['resourceID'])); } } $zip->close(); header("Content-Type: application/zip"); header("Content-Length: " . filesize($filename)); header("Content-Disposition: attachment; filename=\"".$this->getTitle().".zip\""); unlink($filename); } /** * @Secured(roles={'authenticated'}) * */ function actionCancel() { $_SESSION['krihvel_notice'] = gettext("Action cancelled!"); if ( in_array($this->getType(), array('link', 'image')) ) { if (isset($_SESSION['folderid']) && $_SESSION['folderid']!="NULL") { return array('pid' => 'Folder', 'fid' => $_SESSION['folderid']); } return array('pid' => 'MyFolder'); } return array('pid' => 'TaskCreator'); } } ?>