]> git.mjollnir.org Git - moodle.git/commitdiff
web service MDL-12886 few TODO comments
authorjerome <jerome>
Thu, 22 Jan 2009 05:22:05 +0000 (05:22 +0000)
committerjerome <jerome>
Thu, 22 Jan 2009 05:22:05 +0000 (05:22 +0000)
user/external.php
webservice/rest/locallib.php [new file with mode: 0644]
webservice/rest/server.php
webservice/soap/server.php

index d7f08b791ff9ce4606a25b7db1f8fd9e093d1386..fcd488a10b1e5caa8056f735503e4e8a94d4c6c2 100644 (file)
@@ -81,12 +81,12 @@ final class user_external extends moodle_external {
         global $DB;
         $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$mnethostid));
     /// PLEASE UNCOMMENT HERE ONCE AUTHENTICATION IS IMPLEMENTED - $USER/context need to be set here
-        //if (require_capability('moodle/user:delete', get_context_instance(CONTEXT_SYSTEM))) {
+//        if (has_capability('moodle/user:delete', get_context_instance(CONTEXT_SYSTEM))) {
             return delete_user($user); //this function is in moodlelib.php
-        //}
-        //else {
-        //    throw new moodle_exception('couldnotdeleteuser');
-        //}
+//        }
+//        else {
+//            throw new moodle_exception('couldnotdeleteuser');
+//        }
     }
 
     /**
diff --git a/webservice/rest/locallib.php b/webservice/rest/locallib.php
new file mode 100644 (file)
index 0000000..d584813
--- /dev/null
@@ -0,0 +1,110 @@
+<?php
+/**
+ *
+ * Rest library
+ *
+ * @author Jerome Mouneyrac, Ferran Recio, David Castro Garcia
+ */
+
+/**
+ *
+ * @author Jerome Mouneyrac
+ * @global object $CFG
+ * @param string $rest_arguments example: /mod/forum/get_discussion
+ * @return string xml object
+ */
+function call_moodle_function ($rest_arguments) {
+    global $CFG;
+///REST params conversion
+    $functionname = substr($rest_arguments,strrpos($rest_arguments,"/")+1); //retrieve the function name (it's located after the last '/') in $rest_arguments
+                                                                            //$rest_argument
+    $apipath = substr($rest_arguments,0, strlen($rest_arguments) - strlen($functionname)); //api path is the other part of $rest_arguments
+
+    $classname = str_replace('/', '_', $apipath); // convert '/' into '_' (e.g. /mod/forum/ => _mod_forum_)
+    $classname = substr($classname,1, strlen($classname) - 1); //remove first _ (e.g. _mod_forum => mod_forum)
+    $classname .= 'external';
+
+    require_once($CFG->dirroot.$apipath.'external.php');
+    $wsapi = new $classname();
+    $description = $wsapi->get_function_webservice_description($functionname); //retrieve the web service description for this function
+
+///This following line is only REST protocol
+    $params = retrieve_params ($description); //retrieve the REST params
+
+///Generic part to any protocols
+    if ($params === false) {
+        //return an error message, the REST params doesn't match with the web service description
+    }
+    $res = call_user_func_array  ( $classname.'::'.$functionname, $params);
+    
+///Transform result into xml in order to send the REST response
+    $return =  mdl_conn_rest_object_to_xml ($res,key($description['return']));
+
+       return "<Result>$return</Result>";
+}
+
+
+/**
+ *
+ * @author Jerome Mouneyrac
+ * @param <type> $description
+ * @return <type>
+ */
+function retrieve_params ($description) {
+    //retrieve REST param matching the description (warning: PHP assign the first instanciation as the first position in the table)
+    foreach ($description['wsparams'] as $paramname => $paramtype) {
+        $value = optional_param($paramname,null,$paramtype);
+        if (!empty($value)) {
+                $params[$paramname] = $value;
+            }
+        }
+    
+    return $params;
+}
+
+/**
+ * auxiliar function for simplexml_object_to_xml
+ * @author Ferran Recio, David Castro Garcia
+ * @param $obj
+ * @param $tag
+ * @param $atts assoc array (key => value)
+ * @return string
+ */
+function mdl_conn_rest_object_to_xml ($obj, $tag,$atts=false) {
+       $res = '';
+       $tag_atts = '';
+       if ($atts) {
+               $main_atts = array();
+               foreach ($atts as $att=>$val) {
+                       $main_atts[] = "$att=\"".urlencode($val)."\"";
+               }
+               if (count($main_atts)) $tag_atts = ' '.implode(' ',$main_atts);
+       }
+
+       //if is an object
+       if (is_object($obj)) {
+               $parts = get_object_vars($obj);
+               foreach ($parts as $tag2 => $val) {
+                       $res.= mdl_conn_rest_object_to_xml ($val, $tag2);
+               }
+               return "<$tag$tag_atts>\n$res</$tag>\n";
+       }
+       //if it's an array all elements will be inside te same tag but with a new atribute key
+       if (is_array($obj)){
+               if (!$atts) $atts = array();
+               //we came from another array
+               if (isset($atts['keys'])) $atts = array();
+               foreach ($obj as $key=>$val) {
+                       $array_atts = $atts;
+                       $array_atts['key'] = $key;
+                       $res.= mdl_conn_rest_object_to_xml ($val, $tag,$array_atts);
+               }
+               return $res;
+       }
+       //any other type, just encapsule it
+       $obj = htmlentities($obj);
+       return  "<$tag$tag_atts>$obj</$tag>\n";
+
+}
+
+?>
\ No newline at end of file
index 20eb8afbff39e4920a5567770cb0bd258ce7f4eb..a07085464d7b5e0d789612d8bf5e852710cefb25 100644 (file)
@@ -13,7 +13,7 @@
  */
 
 require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
-require_once('lib.php');
+require_once('locallib.php');
 
 if (empty($CFG->enablewebservices)) {
     die;
@@ -23,6 +23,6 @@ if (empty($CFG->enablewebservices)) {
 $rest_arguments = get_file_argument('server.php');
 
 header ("Content-type: text/xml");
-
+//TODO implement authentication (probably in the locallib.php)
 echo call_moodle_function($rest_arguments);
 ?>
\ No newline at end of file
index 703cca879a56bde51d4c3d61f8b352bf8e03e5b4..03371edcfeb402f266018382f733c1a1d64f2c8b 100644 (file)
@@ -20,9 +20,14 @@ if (empty($CFG->enablewebservices)) {
 $classpath = optional_param(classpath,null,PARAM_ALPHA);
 require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php');
 
+//TODO retrieve the token from the url
+//     if the token doesn't exist create a server with a connection.wsdl
+//     and set a class containing only get_token() (need to create connection.wsdl and class soapiniconnection)
+//     if token exist, do the authentication here
+
 /// run the server
-$server = new SoapServer("moodle.wsdl");
-$server->setClass($classpath."_external");
+$server = new SoapServer("moodle.wsdl"); //TODO: need to call the wsdl generation on the fly
+$server->setClass($classpath."_external"); //TODO: pass $user as parameter
 $server->handle();
 
 ?>
\ No newline at end of file