MDL-12531, make the new member value available to all the affected plugins, thanks...
[moodle-linuxchix.git] / admin / mnet / adminlib.php
blobe4da39eca06b34eee7b387a0e14061cde0f5ed07
1 <?php
2 /**
3 * Library functions for mnet
5 * @author Donal McMullan donal@catalyst.net.nz
6 * @version 0.0.1
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package mnet
9 */
11 /**
12 * Parse a file to find out what functions/methods exist in it, and add entries
13 * for the remote-call-enabled functions to the database.
15 * The path to a file, e.g. auth/mnet/auth.php can be thought of as
16 * type/parentname/docname
18 * @param string $type mod, auth or enrol
19 * @param string $parentname Implementation of type, e.g. 'mnet' in the
20 * case of auth/mnet/auth.php
21 * @return bool True on success, else false
23 function mnet_get_functions($type, $parentname) {
24 global $CFG;
25 $dataobject = new stdClass();
26 $docname = $type.'.php';
27 $publishes = array();
28 if ('mod' == $type) {
29 $docname = 'rpclib.php';
30 $relname = '/mod/'.$parentname.'/'.$docname;
31 $filename = $CFG->dirroot.$relname;
32 if (file_exists($filename)) include_once $filename;
33 $mnet_publishes = $parentname.'_mnet_publishes';
34 if (function_exists($mnet_publishes)) {
35 (array)$publishes = $mnet_publishes();
37 } else {
38 // auth or enrol
39 $relname = '/'.$type.'/'.$parentname.'/'.$docname;
40 $filename = $CFG->dirroot.$relname;
41 if (file_exists($filename)) include_once $filename;
42 $class = $type.($type=='enrol'? 'ment':'').'_plugin_'.$parentname;
43 if (class_exists($class)) {
44 $object = new $class();
45 if (method_exists($object, 'mnet_publishes')) {
46 (array)$publishes = $object->mnet_publishes();
51 $methodServiceArray = array();
52 foreach($publishes as $service) {
53 if (is_array($service['methods'])) {
54 foreach($service['methods'] as $methodname) {
55 $methodServiceArray[$methodname][] = $service;
60 // Disable functions that don't exist (any more) in the source
61 // Should these be deleted? What about their permissions records?
62 $rpcrecords = get_records_select('mnet_rpc', ' parent=\''.$parentname.'\' AND parent_type=\''.$type.'\' ', 'function_name ASC ');
63 if (!empty($rpcrecords)) {
64 foreach($rpcrecords as $rpc) {
65 if (!array_key_exists($rpc->function_name, $methodServiceArray)) {
66 $rpc->enabled = 0;
67 update_record('mnet_rpc', $rpc);
72 if (!file_exists($filename)) return false;
74 if (extension_loaded('tokenizer')) {
75 include_once "$CFG->dirroot/$CFG->admin/mnet/MethodTable.php";
76 $functions = (array)MethodTable::create($filename,false);
79 foreach($methodServiceArray as $method => $servicearray) {
80 if (!empty($functions[$method])) {
81 $details = $functions[$method];
82 $profile = $details['arguments'];
83 if (!isset($details['returns'])) {
84 array_unshift($profile, array('type' => 'void', 'description' => 'No return value'));
85 } else {
86 array_unshift($profile, $details['returns']);
88 $dataobject->profile = serialize($profile);
89 $dataobject->help = addslashes($details['description']);
90 } else {
91 $dataobject->profile = serialize(array(array('type' => 'void', 'description' => 'No return value')));
92 $dataobject->help = '';
95 $dataobject->function_name = $method;
96 $dataobject->xmlrpc_path = $type.'/'.$parentname.'/'.$docname.'/'.$method;
97 $dataobject->parent_type = $type;
98 $dataobject->parent = $parentname;
99 $dataobject->enabled = '0';
101 if ($record_exists = get_record('mnet_rpc', 'xmlrpc_path', $dataobject->xmlrpc_path)) {
102 $dataobject->id = $record_exists->id;
103 $dataobject->enabled = $record_exists->enabled;
104 update_record('mnet_rpc', $dataobject);
105 } else {
106 $dataobject->id = insert_record('mnet_rpc', $dataobject, true);
109 foreach($servicearray as $service) {
110 $serviceobj = get_record('mnet_service', 'name', $service['name']);
111 if (false == $serviceobj) {
112 $serviceobj = new stdClass();
113 $serviceobj->name = $service['name'];
114 $serviceobj->apiversion = $service['apiversion'];
115 $serviceobj->offer = 1;
116 $serviceobj->id = insert_record('mnet_service', $serviceobj, true);
119 if (false == get_record('mnet_service2rpc', 'rpcid', $dataobject->id, 'serviceid', $serviceobj->id)) {
120 $obj = new stdClass();
121 $obj->rpcid = $dataobject->id;
122 $obj->serviceid = $serviceobj->id;
123 insert_record('mnet_service2rpc', $obj, true);
127 return true;
130 function upgrade_RPC_functions($returnurl) {
131 global $CFG;
133 $basedir = $CFG->dirroot.'/mod';
134 if (file_exists($basedir) && filetype($basedir) == 'dir') {
135 $dirhandle = opendir($basedir);
136 while (false !== ($dir = readdir($dirhandle))) {
137 $firstchar = substr($dir, 0, 1);
138 if ($firstchar == '.' or $dir == 'CVS' or $dir == '_vti_cnf') {
139 continue;
141 if (filetype($basedir .'/'. $dir) != 'dir') {
142 continue;
145 mnet_get_functions('mod', $dir);
150 $basedir = $CFG->dirroot.'/auth';
151 if (file_exists($basedir) && filetype($basedir) == 'dir') {
152 $dirhandle = opendir($basedir);
153 while (false !== ($dir = readdir($dirhandle))) {
154 $firstchar = substr($dir, 0, 1);
155 if ($firstchar == '.' or $dir == 'CVS' or $dir == '_vti_cnf') {
156 continue;
158 if (filetype($basedir .'/'. $dir) != 'dir') {
159 continue;
162 mnet_get_functions('auth', $dir);
166 $basedir = $CFG->dirroot.'/enrol';
167 if (file_exists($basedir) && filetype($basedir) == 'dir') {
168 $dirhandle = opendir($basedir);
169 while (false !== ($dir = readdir($dirhandle))) {
170 $firstchar = substr($dir, 0, 1);
171 if ($firstchar == '.' or $dir == 'CVS' or $dir == '_vti_cnf') {
172 continue;
174 if (filetype($basedir .'/'. $dir) != 'dir') {
175 continue;
178 mnet_get_functions('enrol', $dir);