From c4825127cc9faca1e9f989d2bc8759ed10fb18f4 Mon Sep 17 00:00:00 2001
From: Richard Mansfield <richardm@catalyst.net.nz>
Date: Wed, 12 Aug 2009 12:10:10 +1200
Subject: [PATCH] Allow mnet functions to have parent_type independent of xmlrpc_path

---
 admin/mnet/adminlib.php |   43 ++++++++++++++++++++++---------------------
 1 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/admin/mnet/adminlib.php b/admin/mnet/adminlib.php
index 731f37c..66b99f1 100644
--- a/admin/mnet/adminlib.php
+++ b/admin/mnet/adminlib.php
@@ -15,37 +15,38 @@
  * The path to a file, e.g. auth/mnet/auth.php can be thought of as
  * type/parentname/docname
  *
- * @param  string   $type           mod, auth or enrol
+ * @param  string   $parenttype     mod, auth or enrol
  * @param  string   $parentname     Implementation of type, e.g. 'mnet' in the
  *                                  case of auth/mnet/auth.php
+ * @param  string   $parenttypedir  path to $parenttype directory, if != $parenttype
  * @return bool                     True on success, else false
  */
-function mnet_get_functions($type, $parentname) {
+function mnet_get_functions($parenttype, $parentname, $parenttypedir=null) {
     global $CFG;
     $dataobject = new stdClass();
-    $docname = $type.'.php';
     $publishes = array();
-    if ('mod' == $type) {
-        $docname = 'rpclib.php';
-        $relname  = '/mod/'.$parentname.'/'.$docname;
-        $filename = $CFG->dirroot.$relname;
+    if ($parenttype == 'auth' || $parenttype == 'enrol') {
+        $doc = $parenttype . '/' . $parentname . '/' . $parenttype . '.php';
+        $filename = $CFG->dirroot . '/' . $doc;
         if (file_exists($filename)) include_once $filename;
-        $mnet_publishes = $parentname.'_mnet_publishes';
-        if (function_exists($mnet_publishes)) {
-            (array)$publishes = $mnet_publishes();
-        }
-    } else {
-        // auth or enrol
-        $relname  = '/'.$type.'/'.$parentname.'/'.$docname;
-        $filename = $CFG->dirroot.$relname;
-        if (file_exists($filename)) include_once $filename;
-        $class = $type.($type=='enrol'? 'ment':'').'_plugin_'.$parentname;
+        $class = $parenttype . ($parenttype == 'enrol' ? 'ment' : '') . '_plugin_' . $parentname;
         if (class_exists($class)) {
             $object = new $class();
             if (method_exists($object, 'mnet_publishes')) {
                 (array)$publishes = $object->mnet_publishes();
             }
         }
+    } else {
+        // mod
+        $parentdir = (is_null($parenttypedir) ? $parenttype : $parenttypedir) . '/' . $parentname;
+        $doc = $parentdir . '/rpclib.php';
+        $filename = $CFG->dirroot . '/' . $doc;
+        if (file_exists($filename)) include_once $filename;
+        $prefix = str_replace('/', '_', substr($parentdir, strlen('mod/')));
+        $mnet_publishes = $prefix . '_mnet_publishes';
+        if (function_exists($mnet_publishes)) {
+            (array)$publishes = $mnet_publishes();
+        }
     }
 
     $methodServiceArray = array();
@@ -59,7 +60,7 @@ function mnet_get_functions($type, $parentname) {
 
     // Disable functions that don't exist (any more) in the source
     // Should these be deleted? What about their permissions records?
-    $rpcrecords = get_records_select('mnet_rpc', ' parent=\''.$parentname.'\' AND parent_type=\''.$type.'\' ', 'function_name ASC ');
+    $rpcrecords = get_records_select('mnet_rpc', " parent='$parentname' AND parent_type='$parenttype' ", 'function_name ASC ');
     if (!empty($rpcrecords)) {
         foreach($rpcrecords as $rpc) {
             if (!array_key_exists($rpc->function_name, $methodServiceArray)) {
@@ -93,8 +94,8 @@ function mnet_get_functions($type, $parentname) {
         }
 
         $dataobject->function_name = $method;
-        $dataobject->xmlrpc_path   = $type.'/'.$parentname.'/'.$docname.'/'.$method;
-        $dataobject->parent_type   = $type;
+        $dataobject->xmlrpc_path   = $doc . '/' . $method;
+        $dataobject->parent_type   = $parenttype;
         $dataobject->parent        = $parentname;
         $dataobject->enabled       = '0';
 
@@ -145,7 +146,7 @@ function upgrade_RPC_functions($returnurl) {
                     continue;
                 }
 
-                mnet_get_functions($parenttype, $dir);
+                mnet_get_functions($parenttype, $dir, $parenttype);
             }
         }
     }
-- 
1.5.4.3

