|
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 require_once 'Store.php'; 00028 require_once 'Node.php'; 00029 require_once 'WebService/WebServiceFactory.php'; 00030 00031 class Session extends BaseObject 00032 { 00033 public $authenticationService; 00034 public $repositoryService; 00035 public $contentService; 00036 00037 private $_repository; 00038 private $_ticket; 00039 private $_stores; 00040 private $_namespaceMap; 00041 00042 private $nodeCache; 00043 private $idCount = 0; 00044 00051 public function __construct($repository, $ticket) 00052 { 00053 $this->nodeCache = array(); 00054 00055 $this->_repository = $repository; 00056 $this->_ticket = $ticket; 00057 00058 $this->repositoryService = WebServiceFactory::getRepositoryService($this->_repository->connectionUrl, $this->_ticket); 00059 $this->contentService = WebServiceFactory::getContentService($this->_repository->connectionUrl, $this->_ticket); 00060 } 00061 00069 public function createStore($address, $scheme="workspace") 00070 { 00071 // Create the store 00072 $result = $this->repositoryService->createStore(array( 00073 "scheme" => $scheme, 00074 "address" => $address)); 00075 $store = new Store($this, $result->createStoreReturn->address, $result->createStoreReturn->scheme); 00076 00077 // Add to the cached list if its been populated 00078 if (isset($this->_stores) == true) 00079 { 00080 $this->_stores[] = $store; 00081 } 00082 00083 // Return the newly created store 00084 return $store; 00085 } 00086 00094 public function getStore($address, $scheme="workspace") 00095 { 00096 return new Store($this, $address, $scheme); 00097 } 00098 00105 public function getStoreFromString($value) 00106 { 00107 list($scheme, $address) = explode("://", $value); 00108 return new Store($this, $address, $scheme); 00109 } 00110 00111 public function getNode($store, $id) 00112 { 00113 $node = $this->getNodeImpl($store, $id); 00114 if ($node == null) 00115 { 00116 $node = new Node($this, $store, $id); 00117 $this->addNode($node); 00118 } 00119 return $node; 00120 } 00121 00122 public function getNodeFromString($value) 00123 { 00124 // TODO 00125 throw new Exception("getNode($value) not yet implemented"); 00126 } 00127 00131 public function addNode($node) 00132 { 00133 $this->nodeCache[$node->__toString()] = $node; 00134 } 00135 00136 private function getNodeImpl($store, $id) 00137 { 00138 $result = null; 00139 $nodeRef = $store->scheme . "://" . $store->address . "/" . $id; 00140 if (array_key_exists($nodeRef, $this->nodeCache) == true) 00141 { 00142 $result = $this->nodeCache[$nodeRef]; 00143 } 00144 return $result; 00145 } 00146 00150 public function save($debug=false) 00151 { 00152 // Build the update statements from the node cache 00153 $statements = array(); 00154 foreach ($this->nodeCache as $node) 00155 { 00156 $node->onBeforeSave($statements); 00157 } 00158 00159 if ($debug == true) 00160 { 00161 var_dump($statements); 00162 echo ("<br><br>"); 00163 } 00164 00165 if (count($statements) > 0) 00166 { 00167 // Make the web service call 00168 $result = $this->repositoryService->update(array("statements" => $statements)); 00169 //var_dump($result); 00170 00171 // Update the state of the updated nodes 00172 foreach ($this->nodeCache as $node) 00173 { 00174 $node->onAfterSave($this->getIdMap($result)); 00175 } 00176 } 00177 } 00178 00184 public function clear() 00185 { 00186 // Clear the node cache 00187 $this->nodeCache = array(); 00188 } 00189 00190 private function getIdMap($result) 00191 { 00192 $return = array(); 00193 $statements = $result->updateReturn; 00194 if (is_array($statements) == true) 00195 { 00196 foreach ($statements as $statement) 00197 { 00198 if ($statement->statement == "create") 00199 { 00200 $id = $statement->sourceId; 00201 $uuid = $statement->destination->uuid; 00202 $return[$id] = $uuid; 00203 } 00204 } 00205 } 00206 else 00207 { 00208 if ($statements->statement == "create") 00209 { 00210 $id = $statements->sourceId; 00211 $uuid = $statements->destination->uuid; 00212 $return[$id] = $uuid; 00213 } 00214 } 00215 return $return; 00216 } 00217 00218 public function query($store, $query, $language='lucene') 00219 { 00220 // TODO need to support paged queries 00221 $result = $this->repositoryService->query(array( 00222 "store" => $store->__toArray(), 00223 "query" => array( 00224 "language" => $language, 00225 "statement" => $query), 00226 "includeMetaData" => false)); 00227 00228 // TODO for now do nothing with the score and the returned data 00229 $resultSet = $result->queryReturn->resultSet; 00230 return $this->resultSetToNodes($this, $store, $resultSet); 00231 } 00232 00233 public function getTicket() 00234 { 00235 return $this->_ticket; 00236 } 00237 00238 public function getRepository() 00239 { 00240 return $this->_repository; 00241 } 00242 00243 public function getNamespaceMap() 00244 { 00245 if ($this->_namespaceMap == null) 00246 { 00247 $this->_namespaceMap = new NamespaceMap(); 00248 } 00249 return $this->_namespaceMap; 00250 } 00251 00252 public function getStores() 00253 { 00254 if (isset ($this->_stores) == false) 00255 { 00256 $this->_stores = array (); 00257 $results = $this->repositoryService->getStores(); 00258 00259 foreach ($results->getStoresReturn as $result) 00260 { 00261 $this->_stores[] = new Store($this, $result->address, $result->scheme); 00262 } 00263 } 00264 00265 return $this->_stores; 00266 } 00267 00270 public function nextSessionId() 00271 { 00272 $sessionId = "session".$this->_ticket.$this->idCount; 00273 $this->idCount ++; 00274 return $sessionId; 00275 } 00276 } 00277 ?>