|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /* 00003 * Copyright (C) 2005 Alfresco, Inc. 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * as published by the Free Software Foundation; either version 2 00008 * of the License, or (at your option) any later version. 00009 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 00019 * As a special exception to the terms and conditions of version 2.0 of 00020 * the GPL, you may redistribute this Program in connection with Free/Libre 00021 * and Open Source Software ("FLOSS") applications as described in Alfresco's 00022 * FLOSS exception. You should have recieved a copy of the text describing 00023 * the FLOSS exception, and it is also available here: 00024 * http://www.alfresco.com/legal/licensing" 00025 */ 00026 00027 // change by moodle 00028 require_once($CFG->libdir."/alfresco/Service/Functions.php"); 00029 00030 class ContentData extends BaseObject 00031 { 00032 private $_isPopulated = false; 00033 private $_isDirty = false; 00034 00035 private $_node; 00036 private $_property; 00037 00038 private $_mimetype; 00039 private $_size; 00040 private $_encoding; 00041 private $_url; 00042 private $_newContent; 00043 private $_newFileContent; 00044 00045 public function __construct($node, $property, $mimetype=null, $encoding=null, $size=-1) 00046 { 00047 $this->_node = $node; 00048 $this->_property = $property; 00049 $this->_mimetype = $mimetype; 00050 $this->_encoding = $encoding; 00051 if ($size != -1) 00052 { 00053 $this->size = $size; 00054 } 00055 $this->_isPopulated = false; 00056 } 00057 00058 public function setPropertyDetails($node, $property) 00059 { 00060 $this->_node = $node; 00061 $this->_property = $property; 00062 } 00063 00064 public function __toString() 00065 { 00066 $this->populateContentData(); 00067 return "mimetype=".$this->mimetype."|encoding=".$this->encoding."|size=".$this->size; 00068 } 00069 00070 public function getNode() 00071 { 00072 return $this->_node; 00073 } 00074 00075 public function getProperty() 00076 { 00077 return $this->_property; 00078 } 00079 00080 public function getIsDirty() 00081 { 00082 return $this->_isDirty; 00083 } 00084 00085 public function getMimetype() 00086 { 00087 $this->populateContentData(); 00088 return $this->_mimetype; 00089 } 00090 00091 public function setMimetype($mimetype) 00092 { 00093 $this->populateContentData(); 00094 $this->_mimetype = $mimetype; 00095 } 00096 00097 public function getSize() 00098 { 00099 $this->populateContentData(); 00100 return $this->_size; 00101 } 00102 00103 public function getEncoding() 00104 { 00105 $this->populateContentData(); 00106 return $this->_encoding; 00107 } 00108 00109 public function setEncoding($encoding) 00110 { 00111 $this->populateContentData(); 00112 $this->_encoding = $encoding; 00113 } 00114 00115 public function getUrl() 00116 { 00117 // TODO what should be returned if the content has been updated?? 00118 00119 $this->populateContentData(); 00120 $result = null; 00121 if ($this->_url != null) 00122 { 00123 $result = $this->_url."?ticket=".$this->_node->session->ticket; 00124 } 00125 return $result; 00126 } 00127 00128 public function getGuestUrl() 00129 { 00130 // TODO what should be returned if the content has been updated?? 00131 00132 $this->populateContentData(); 00133 $result = null; 00134 if ($this->_url != null) 00135 { 00136 $result = $this->_url."?guest=true"; 00137 } 00138 return $result; 00139 } 00140 00141 public function getContent() 00142 { 00143 $this->populateContentData(); 00144 00145 $result = null; 00146 if ($this->_isDirty == true) 00147 { 00148 if ($this->_newFileContent != null) 00149 { 00150 $handle = fopen($this->_newFileContent, "rb"); 00151 $result = stream_get_contents($handle); 00152 fclose($handle); 00153 } 00154 else if ($this->_newContent != null) 00155 { 00156 $result = $this->_newContent; 00157 } 00158 } 00159 else 00160 { 00161 if ($this->getUrl() != null) 00162 { 00163 $handle = fopen($this->getUrl(), "rb"); 00164 $result = stream_get_contents($handle); 00165 fclose($handle); 00166 } 00167 } 00168 return $result; 00169 } 00170 00171 public function setContent($content) 00172 { 00173 $this->populateContentData(); 00174 $this->_isDirty = true; 00175 $this->_newContent = $content; 00176 } 00177 00178 public function writeContentFromFile($fileName) 00179 { 00180 $this->populateContentData(); 00181 $this->_isDirty = true; 00182 $this->_newFileContent = $fileName; 00183 } 00184 00185 public function readContentToFile($fileName) 00186 { 00187 $handle = fopen($fileName, "wb"); 00188 fwrite($handle, $this->getContent()); 00189 fclose($handle); 00190 } 00191 00192 public function onBeforeSave(&$statements, $where) 00193 { 00194 if ($this->_isDirty == true) 00195 { 00196 // Check mimetype has been set 00197 if ($this->_mimetype == null) 00198 { 00199 throw new Exception("A mime type for the content property ".$this->_property." on node ".$this->_node->__toString()." must be set"); 00200 } 00201 00202 // If a file has been specified then read content from there 00203 //$content = null; 00204 if ($this->_newFileContent != null) 00205 { 00206 // Upload the content to the repository 00207 $contentData = upload_file($this->node->session, $this->_newFileContent, $this->_mimetype, $this->_encoding); 00208 00209 // Set the content property value 00210 $this->addStatement( 00211 $statements, 00212 "update", 00213 array("property" => array( 00214 "name" => $this->property, 00215 "isMultiValue" => false, 00216 "value" => $contentData)) + $where); 00217 } 00218 else 00219 { 00220 // Add the writeContent statement 00221 $this->addStatement( 00222 $statements, 00223 "writeContent", 00224 array( 00225 "property" => $this->_property, 00226 "content" => $this->_newContent, 00227 "format" => array( 00228 "mimetype" => $this->_mimetype, 00229 "encoding" => $this->_encoding)) + 00230 $where); 00231 } 00232 } 00233 } 00234 00235 public function onAfterSave() 00236 { 00237 $this->_isDirty = false; 00238 $this->_isPopulated = false; 00239 $this->_mimetype = null; 00240 $this->__size = null; 00241 $this->__encoding = null; 00242 $this->__url = null; 00243 $this->__newContent = null; 00244 } 00245 00246 private function populateContentData() 00247 { 00248 //echo "isPopulated:".$this->_isPopulated."; node:".$this->_node."; property:".$this->_property."<br>"; 00249 if ($this->_isPopulated == false && $this->_node != null && $this->_property != null && $this->_node->isNewNode == false) 00250 { 00251 $result = $this->_node->session->contentService->read( array( 00252 "items" => array( 00253 "nodes" => array( 00254 "store" => $this->_node->store->__toArray(), 00255 "uuid" => $this->_node->id)), 00256 "property" => $this->_property) ); 00257 if (isset($result->content) == true) 00258 { 00259 if (isset($result->content->length) == true) 00260 { 00261 $this->_size = $result->content->length; 00262 } 00263 if (isset($result->content->format->mimetype) == true) 00264 { 00265 $this->_mimetype = $result->content->format->mimetype; 00266 } 00267 if (isset($result->content->format->encoding) == true) 00268 { 00269 $this->_encoding = $result->content->format->encoding; 00270 } 00271 if (isset($result->content->url) == true) 00272 { 00273 $this->_url = $result->content->url; 00274 } 00275 } 00276 00277 $this->_isPopulated = true; 00278 } 00279 } 00280 00281 private function addStatement(&$statements, $statement, $body) 00282 { 00283 $result = array(); 00284 if (array_key_exists($statement, $statements) == true) 00285 { 00286 $result = $statements[$statement]; 00287 } 00288 $result[] = $body; 00289 $statements[$statement] = $result; 00290 } 00291 } 00292 ?>