|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00027 require_once 'Zend/Http/Client.php'; 00028 00037 class Zend_Service_Simpy 00038 { 00044 protected $_baseUri = 'http://simpy.com/simpy/api/rest/'; 00045 00051 protected $_http; 00052 00060 public function __construct($username, $password) 00061 { 00062 $this->_http = new Zend_Http_Client; 00063 $this->_http->setAuth($username, $password); 00064 } 00065 00072 public function getHttpClient() 00073 { 00074 return $this->_http; 00075 } 00076 00086 protected function _makeRequest($op, $query = null) 00087 { 00088 if ($query != null) { 00089 $query = array_diff($query, array_filter($query, 'is_null')); 00090 $query = '?' . http_build_query($query); 00091 } 00092 00093 $this->_http->setUri($this->_baseUri . $op . '.do' . $query); 00094 $response = $this->_http->request('GET'); 00095 00096 if ($response->isSuccessful()) { 00097 $doc = new DOMDocument(); 00098 $doc->loadXML($response->getBody()); 00099 $xpath = new DOMXPath($doc); 00100 $list = $xpath->query('/status/code'); 00101 00102 if ($list->length > 0) { 00103 $code = $list->item(0)->nodeValue; 00104 00105 if ($code != 0) { 00106 $list = $xpath->query('/status/message'); 00107 $message = $list->item(0)->nodeValue; 00111 require_once 'Zend/Service/Exception.php'; 00112 throw new Zend_Service_Exception($message, $code); 00113 } 00114 } 00115 00116 return $doc; 00117 } 00118 00122 require_once 'Zend/Service/Exception.php'; 00123 throw new Zend_Service_Exception($response->getMessage(), $response->getStatus()); 00124 } 00125 00135 public function getTags($limit = null) 00136 { 00137 $query = array( 00138 'limit' => $limit 00139 ); 00140 00141 $doc = $this->_makeRequest('GetTags', $query); 00142 00146 require_once 'Zend/Service/Simpy/TagSet.php'; 00147 return new Zend_Service_Simpy_TagSet($doc); 00148 } 00149 00157 public function removeTag($tag) 00158 { 00159 $query = array( 00160 'tag' => $tag 00161 ); 00162 00163 $this->_makeRequest('RemoveTag', $query); 00164 00165 return $this; 00166 } 00167 00176 public function renameTag($fromTag, $toTag) 00177 { 00178 $query = array( 00179 'fromTag' => $fromTag, 00180 'toTag' => $toTag 00181 ); 00182 00183 $this->_makeRequest('RenameTag', $query); 00184 00185 return $this; 00186 } 00187 00197 public function mergeTags($fromTag1, $fromTag2, $toTag) 00198 { 00199 $query = array( 00200 'fromTag1' => $fromTag1, 00201 'fromTag2' => $fromTag2, 00202 'toTag' => $toTag 00203 ); 00204 00205 $this->_makeRequest('MergeTags', $query); 00206 00207 return $this; 00208 } 00209 00219 public function splitTag($tag, $toTag1, $toTag2) 00220 { 00221 $query = array( 00222 'tag' => $tag, 00223 'toTag1' => $toTag1, 00224 'toTag2' => $toTag2 00225 ); 00226 00227 $this->_makeRequest('SplitTag', $query); 00228 00229 return $this; 00230 } 00231 00240 public function getLinks(Zend_Service_Simpy_LinkQuery $q = null) 00241 { 00242 if ($q != null) { 00243 $query = array( 00244 'q' => $q->getQueryString(), 00245 'limit' => $q->getLimit(), 00246 'date' => $q->getDate(), 00247 'afterDate' => $q->getAfterDate(), 00248 'beforeDate' => $q->getBeforeDate() 00249 ); 00250 00251 $doc = $this->_makeRequest('GetLinks', $query); 00252 } else { 00253 $doc = $this->_makeRequest('GetLinks'); 00254 } 00255 00259 require_once 'Zend/Service/Simpy/LinkSet.php'; 00260 return new Zend_Service_Simpy_LinkSet($doc); 00261 } 00262 00279 public function saveLink($title, $href, $accessType, $tags = null, $urlNickname = null, $note = null) 00280 { 00281 if (is_array($tags)) { 00282 $tags = implode(',', $tags); 00283 } 00284 00285 $query = array( 00286 'title' => $title, 00287 'href' => $href, 00288 'accessType' => $accessType, 00289 'tags' => $tags, 00290 'urlNickname' => $urlNickname, 00291 'note' => $note 00292 ); 00293 00294 $this->_makeRequest('SaveLink', $query); 00295 00296 return $this; 00297 } 00298 00306 public function deleteLink($href) 00307 { 00308 $query = array( 00309 'href' => $href 00310 ); 00311 00312 $this->_makeRequest('DeleteLink', $query); 00313 00314 return $this; 00315 } 00316 00324 public function getWatchlists() 00325 { 00326 $doc = $this->_makeRequest('GetWatchlists'); 00327 00331 require_once 'Zend/Service/Simpy/WatchlistSet.php'; 00332 return new Zend_Service_Simpy_WatchlistSet($doc); 00333 } 00334 00342 public function getWatchlist($watchlistId) 00343 { 00344 $query = array( 00345 'watchlistId' => $watchlistId 00346 ); 00347 00348 $doc = $this->_makeRequest('GetWatchlist', $query); 00349 00353 require_once 'Zend/Service/Simpy/Watchlist.php'; 00354 return new Zend_Service_Simpy_Watchlist($doc->documentElement); 00355 } 00356 00369 public function getNotes($q = null, $limit = null) 00370 { 00371 $query = array( 00372 'q' => $q, 00373 'limit' => $limit 00374 ); 00375 00376 $doc = $this->_makeRequest('GetNotes', $query); 00377 00381 require_once 'Zend/Service/Simpy/NoteSet.php'; 00382 return new Zend_Service_Simpy_NoteSet($doc); 00383 } 00384 00398 public function saveNote($title, $tags = null, $description = null, $noteId = null) 00399 { 00400 if (is_array($tags)) { 00401 $tags = implode(',', $tags); 00402 } 00403 00404 $query = array( 00405 'title' => $title, 00406 'tags' => $tags, 00407 'description' => $description, 00408 'noteId' => $noteId 00409 ); 00410 00411 $this->_makeRequest('SaveNote', $query); 00412 00413 return $this; 00414 } 00415 00423 public function deleteNote($noteId) 00424 { 00425 $query = array( 00426 'noteId' => $noteId 00427 ); 00428 00429 $this->_makeRequest('DeleteNote', $query); 00430 00431 return $this; 00432 } 00433 }