Пример #1
0
/**
 * Get fields values of a virtual platform.
 * @param string $user The calling user.
 * @param string $table The table to read.
 * @param string $fields The fileds to retrieve.
 * @param mixed $select The value of id or alternative field.
 */
function mnetadmin_rpc_get_fields($user, $table, $fields, $select)
{
    global $CFG, $USER, $DB;
    // Invoke local user and check his rights.
    invoke_local_user($user, 'local/vmoodle:execute');
    // Creating response.
    $response = new StdClass();
    $response->status = RPC_SUCCESS;
    // Getting record.
    ob_clean();
    ob_start();
    // Used to prevent HTML output from dmllib methods and capture errors.
    $field = array_keys($select);
    $record = $DB->get_record($table, array($field[0] => $select[$field[0]], isset($select[1]) ? $field[1] : '' => isset($select[1]) ? $select[1] : '', isset($select[2]) ? $field[2] : '' => isset($select[2]) ? $select[2] : ''), implode(',', $fields));
    if (!$record) {
        $error = parse_wlerror();
        if (empty($error)) {
            $error = 'Unable to retrieve record.';
        }
        $response->status = RPC_FAILURE_RECORD;
        $response->errors[] = $error;
        return json_encode($response);
    }
    ob_end_clean();
    // Setting value.
    $response->value = $record;
    // Returning response.
    return json_encode($response);
}
Пример #2
0
/**
 * Enables or disables a plugin of a virtual platform.
 * @param string $user The calling user.
 * @param string $plugininfos a structure with info for each plugin to setup.
 */
function mnetadmin_rpc_set_plugins_states($user, $plugininfos, $json_response = true)
{
    global $CFG, $USER, $DB;
    // debug_trace("Plugin Set States: Entry point");
    // Creating response.
    $response = new Stdclass();
    $response->status = RPC_SUCCESS;
    $response->errors = array();
    $response->error = '';
    // Invoke local user and check his rights.
    if ($auth_response = invoke_local_user((array) $user, 'local/vmoodle:execute')) {
        if ($json_response) {
            // We could not have a credential.
            return $auth_response;
        } else {
            return json_decode($auth_response);
        }
    }
    // Getting plugin enable/disable method.
    if (!empty($plugininfos)) {
        foreach ($plugininfos as $plugin => $infos) {
            $actionclass = $infos['type'] . '_remote_control';
            // Ignore non implemented.
            if (!class_exists($actionclass)) {
                debug_trace("failing running remote action on {$actionclass}. Class not found");
                continue;
            }
            $control = new $actionclass($infos['type'], $plugin);
            $action = $infos['action'];
            $return = $control->action($action);
            if ($return !== 0) {
                $response->status = RPC_FAILURE;
                $response->errors[] = $return;
            }
            $response->value = 'done.';
        }
    }
    $response->error = implode(', ', $response->errors);
    // Returning response.
    if ($json_response) {
        return json_encode($response);
    } else {
        return $response;
    }
}
Пример #3
0
function mnetadmin_rpc_upgrade($user, $json_response = true)
{
    global $CFG, $USER;
    // Invoke local user and check his rights
    if ($auth_response = invoke_local_user((array) $user)) {
        if ($json_response) {
            return $auth_response;
        } else {
            return json_decode($auth_response);
        }
    }
    // Creating response
    $response = new stdclass();
    $response->status = RPC_SUCCESS;
    require "{$CFG->dirroot}/version.php";
    // defines $version, $release, $branch and $maturity
    $CFG->target_release = $release;
    // used during installation and upgrades
    if ($version < $CFG->version) {
        $response->status = RPC_FAILURE_RUN;
        $response->error = get_string('downgradedcore', 'error');
        $response->errors[] = get_string('downgradedcore', 'error');
        if ($json_response) {
            return json_encode($response);
        } else {
            return $response;
        }
    }
    $oldversion = "{$CFG->release} ({$CFG->version})";
    $newversion = "{$release} ({$version})";
    if (!moodle_needs_upgrading()) {
        $response->message = get_string('cliupgradenoneed', 'core_admin', $newversion);
        if ($json_response) {
            return json_encode($response);
        } else {
            return $response;
        }
    }
    // debug_trace('Remote Upgrade : Environment check');
    list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_NEWER);
    if (!$envstatus) {
        $response->status = RPC_FAILURE_RUN;
        $response->error = vmoodle_get_string('environmentissues', 'vmoodleadminset_upgrade');
        $response->errors[] = vmoodle_get_string('environmentissues', 'vmoodleadminset_upgrade');
        $response->detail = $environment_results;
        if ($json_response) {
            return json_encode($response);
        } else {
            return $response;
        }
    }
    // Test plugin dependencies.
    // debug_trace('Remote Upgrade : Plugins check');
    $failed = array();
    if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
        $response->status = RPC_FAILURE_RUN;
        $response->error = get_string('pluginschecktodo', 'admin');
        $response->errors[] = get_string('pluginschecktodo', 'admin');
        if ($json_response) {
            return json_encode($response);
        } else {
            return $response;
        }
    }
    ob_start();
    // debug_trace('Remote Upgrade : Upgrade core');
    if ($version > $CFG->version) {
        upgrade_core($version, false);
    }
    set_config('release', $release);
    set_config('branch', $branch);
    // unconditionally upgrade
    // debug_trace('Remote Upgrade : Upgrade other');
    upgrade_noncore(false);
    // log in as admin - we need doanything permission when applying defaults
    // debug_trace('Remote Upgrade : Turning ADMIN ');
    session_set_user(get_admin());
    // apply all default settings, just in case do it twice to fill all defaults
    // debug_trace('Remote Upgrade : Applying settings ');
    admin_apply_default_settings(NULL, false);
    admin_apply_default_settings(NULL, false);
    ob_end_clean();
    $response->message = vmoodle_get_string('upgradecomplete', 'vmoodleadminset_upgrade', $newversion);
    if ($json_response) {
        return json_encode($response);
    } else {
        return $response;
    }
}
Пример #4
0
/**
 * Purge internally all caches.
 * @param object $user The calling user, containing mnethostroot reference and hostroot reference.
 */
function mnetadmin_rpc_purge_caches($user, $json_response = true)
{
    global $CFG, $USER;
    debug_trace('RPC ' . json_encode($user));
    if ($auth_response = invoke_local_user((array) $user)) {
        if ($json_response) {
            return $auth_response;
        } else {
            return json_decode($auth_response);
        }
    }
    // Creating response.
    $response = new stdClass();
    $response->status = RPC_SUCCESS;
    purge_all_caches();
    debug_trace('RPC Bind : Sending response');
    // Returns response (success or failure).
    return json_encode($response);
}
Пример #5
0
/**
 * Deletes a peer by unmarking it.
 * @param string $username The calling user.
 * @param string $userhost The calling user's host.
 * @param string $remotehost The calling host.
 * @param string $peer_wwwroot The peer's wwwroot to delete.
 */
function mnetadmin_rpc_unbind_peer($username, $userhost, $remotehost, $peer_wwwroot)
{
    global $CFG, $USER, $DB;
    // Invoke distant user who makes the call and checks his rights.
    $user['username'] = $username;
    $user['remoteuserhostroot'] = $userhost;
    $user['remotehostroot'] = $remotehost;
    invoke_local_user($user, 'local/vmoodle:managevmoodles');
    // Creating response.
    $response = new stdClass();
    $response->status = RPC_SUCCESS;
    // Retrieves the peer record, edits it and inserts it.
    if ($vmoodle_host = $DB->get_record('mnet_host', array('wwwroot' => $peer_wwwroot))) {
        $vmoodle_host->deleted = 1;
        if (!$DB->update_record('mnet_host', $vmoodle_host)) {
            $response->status = RPC_FAILURE_RECORD;
            $response->errors[] = 'Error when updating the host \'' . $vmoodle_host->name . '\'.';
            $response->error = 'Error when updating the host \'' . $vmoodle_host->name . '\'.';
        }
    } else {
        // If host cannot be find. LET IT SILENT, it is unbound !
        // $response->status = RPC_FAILURE_RECORD;
        // $response->errors[] = 'Host with \'wwwroot = '.$peer_wwwroot.'\' cannot be find.';
    }
    // Returns response (success or failure).
    return json_encode($response);
}
Пример #6
0
/**
 * require remote enrollement on a MNET satellite.
 * This XML-RPC call fetches for a remotely known course and enroll the user inside
 * This is essentially intended to use by foreign systems to slave the user management
 * in a MNET network.
 * @param string $callinguser The calling user.
 * @param string $targetuser The username or user identifier of the user to assign a role remotely.
 * @param string $useridfield The field used for identifying the user (id, idnumber or username).
 * @param string $courseidfield The identifying value of the remote course 
 * @param string $courseidentifier The identifying value of the remote course 
 * @param string $rolename The remote role name to be assigned as
 * @param string $starttime The starting date
 * @param string $endtime The enrollement ending date
 *
 */
function mnetadmin_rpc_remote_enrol($callinguser, $targetuser, $rolename, $whereroot, $courseidfield, $courseidentifier, $starttime = 0, $endtime = 0, $json_response = true)
{
    global $CFG, $USER, $DB;
    if (function_exists('debug_trace')) {
        debug_trace($CFG->wwwroot . ' >> mnetadmin_rpc_remote_enrol(' . json_encode($callinguser) . ", {$targetuser}, {$rolename}, {$whereroot}, {$courseidfield}, {$courseidentifier}, {$starttime} = 0, {$endtime} = 0, {$json_response} = true) ");
    }
    $extresponse = new stdclass();
    $extresponse->status = RPC_SUCCESS;
    $extresponse->errors = array();
    $extresponse->error = '';
    // Invoke local user and check his rights.
    if ($auth_response = invoke_local_user((array) $callinguser, 'local/vmoodle:execute')) {
        if ($json_response) {
            return $auth_response;
        } else {
            return json_decode($auth_response);
        }
    }
    if ($whereroot == $CFG->wwwroot) {
        if (function_exists('debug_trace')) {
            debug_trace("local enrol process for {$targetuser} as {$rolename} in {$courseidentifier} by {$courseidfield} from {$starttime} to {$endtime}");
        }
        // Getting remote_course definition.
        switch ($courseidfield) {
            case 'id':
                $course = $DB->get_record('course', array('id' => $courseidentifier));
                break;
            case 'shortname':
                $course = $DB->get_record('course', array('shortname' => $courseidentifier));
                break;
            case 'idnumber':
                $course = $DB->get_record('course', array('idnumber' => $courseidentifier));
                break;
        }
        if (!$course) {
            $extresponse->status = RPC_FAILURE_RECORD;
            $extresponse->errors[] = "Unkown course {$courseidentifier} based on {$courseidfield}.";
            $extresponse->error = "Unkown course {$courseidentifier} based on {$courseidfield}.";
            if (function_exists('debug_trace')) {
                debug_trace("Unkown course based on {$courseidfield} with {$courseidentifier} ");
            }
            if ($json_response) {
                return json_encode($extresponse);
            } else {
                return $extresponse;
            }
        }
        // Getting role if default.
        if (empty($rolename)) {
            $rolename = $course->defaultrolename;
        }
        if (function_exists('debug_trace')) {
            debug_trace("Bounce to mnetadmin_rpc_assignrole");
        }
        $extresponse = mnetadmin_rpc_assign_role($callinguser, $targetuser, $rolename, 'id', CONTEXT_COURSE, $course->id, $starttime, $endtime, $json_response);
        if (!$json_response) {
            return json_decode($extresponse);
        } else {
            return $extresponse;
        }
    } else {
        if (function_exists('debug_trace')) {
            debug_trace('remote source process');
        }
        // Make remote call.
        $userhostroot = $DB->get_field_select('mnet_host', 'wwwroot', " id = {$USER->mnethostid} AND deleted = 0 ");
        if (!$userhostroot) {
            $extresponse->error = 'Unkown user host root (or deleted).';
            if ($json_response) {
                return json_encode($extresponse);
            } else {
                return $extresponse;
            }
        }
        if (!$DB->record_exists('mnet_host', array('wwwroot' => $whereroot, 'deleted' => 0))) {
            $extresponse->error = '$whereroot is unknown host or deleted.';
            if ($json_response) {
                return json_encode($extresponse);
            } else {
                return $extresponse;
            }
        }
        $rpcclient = new mnet_xmlrpc_client();
        $rpcclient->set_method('local/vmoodle/plugins/roles/rpclib.php/mnetadmin_rpc_remote_enrol');
        $caller = new StdClass();
        $caller->username = $USER->username;
        $caller->remoteuserhostroot = $userhostroot;
        $caller->remotehostroot = $CFG->wwwroot;
        $rpcclient->add_param($caller, 'struct');
        // caller user
        $rpcclient->add_param($targetuser, 'string');
        $rpcclient->add_param($rolename, 'string');
        $rpcclient->add_param($whereroot, 'string');
        $rpcclient->add_param($courseidfield, 'string');
        $rpcclient->add_param($courseidentifier, 'string');
        $rpcclient->add_param($starttime, 'int');
        $rpcclient->add_param($endtime, 'int');
        $mnet_host = new mnet_peer();
        $mnet_host->set_wwwroot($whereroot);
        if (!$rpcclient->send($mnet_host)) {
            $extresponse->status = RPC_FAILURE;
            $extresponse->errors[] = 'REMOTE : ' . implode("<br/>\n", @$rpcclient->errors);
            $extresponse->error = 'REMOTE : ' . implode("<br/>\n", @$rpcclient->errors);
            if ($json_response) {
                return json_encode($extresponse);
            } else {
                return $extresponse;
            }
        }
        $response = json_decode($rpcclient->response);
        if ($response->status == 200) {
            $extresponse->message = 'remote enrol success';
            if ($json_response) {
                return json_encode($extresponse);
            } else {
                return $extresponse;
            }
        } else {
            $extresponse->status = RPC_FAILURE;
            $extresponse->errors = array();
            $extresponse->errors[] = 'Remote application errors : ';
            $extresponse->errors = array_merge($extresponse->errors, $response->errors);
            $extresponse->error = 'Remote application error.';
            if ($json_response) {
                return json_encode($extresponse);
            } else {
                return $extresponse;
            }
        }
    }
}