Discussions started by mohan reddy

To download the quiz files containing questions and answers using web services follow the steps mentioned below.

1. You need below mentioned parameters before calling webservice function moodle_file_get_files.

a)contextid

b)component

c)filearea

d)itemid

e)filepath

f)filename 

You will find values for above mentioned parameters from Moodle Table mdl_files.

SELECT * FROM moodle_apple.mdl_files;

 

2. Use the code to get information about the files.(below code helps to call the appropriate web service ,

you need add extra code for converting response  in to a file)

package com.exilant.moodle;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

/**
*
* @author Mohan Kumar B M
*/
public class Moodle {

public static void main(String[] args) throws ProtocolException,
IOException, ParserConfigurationException, SAXException {

final String token = "";
final String domainName = "";

String functionName = "moodle_file_get_files";
String urlParameters = "contextid=153&component=user&filearea=draft&itemid=803619384&filepath=/&filename=Quiz.txt";

String serverurl = domainName + "/webservice/rest/server.php"
+ "?wstoken=" + token + "&wsfunction=" + functionName;
HttpURLConnection con = (HttpURLConnection) new URL(serverurl)
.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
con.setRequestProperty("Content-Language", "en-US");
con.setDoOutput(true);
con.setUseCaches(false);
con.setDoInput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

// Get Response
InputStream is = con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuilder response = new StringBuilder();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
System.out.println(response.toString());

}

}

**Sorry i didn't get time to complete code for downloading file.

For any queries please email me at mkbmbattu@gmail.com.

Thanks,

Mohan Kumar B M

Average of ratings: -

To access noodle web services soap/rest using java ,please follow below steps.

http://docs.moodle.org/dev/External_services_security#Steps_needed_to_configure_simple_web_service_access

1. Generate Token for any user in Moodle.

Token which is necessary for validating user against accessing webservices.

2. Enable any of the SOAP/REST/XML-RPC  Webservices in your Moodle setup.

3. Create a web service and add all the available web service functions to the newly created webservice.

4. Web service function name to be called and parameters to be passed .

Example:

String functionName = "moodle_user_get_users_by_id";
String urlParameters ="userids[0]=1";

String functionName = "moodle_group_get_course_groups";
String urlParameters ="courseid=3";

String functionName = "moodle_enrol_get_enrolled_users";
String urlParameters ="courseid=25";

String functionName = "moodle_group_get_groups";
String urlParameters ="groupids[0]=1";

String functionName = "moodle_group_get_course_groups";
String urlParameters ="courseid=1";

String functionName = "moodle_group_get_groupmembers";
String urlParameters ="groupids[0]=2";

String functionName = "moodle_course_get_courses";
String urlParameters ="";

5. Please find the Java code to access Moodle Rest service

package com.exilant.moodle;

 

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.ProtocolException;

import java.net.URL;

 

import javax.xml.parsers.ParserConfigurationException;

 

import org.xml.sax.SAXException;

 

/**

 * 

 * @author Mohan Kumar B M

 */

publicclass Moodle {

 

public static void main(String[] args) throws ProtocolException,

IOException, ParserConfigurationException, SAXException {

 

String token = "";

String domainName = "";

 

String functionName = "moodle_user_get_users_by_id";

String urlParameters = "userids[0]=1";

 

String serverurl = domainName + "/webservice/rest/server.php"

+ "?wstoken=" + token + "&wsfunction=" + functionName;

HttpURLConnection con = (HttpURLConnection) new URL(serverurl)

.openConnection();

con.setRequestMethod("POST");

con.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

con.setRequestProperty("Content-Language", "en-US");

con.setDoOutput(true);

con.setUseCaches(false);

con.setDoInput(true);

DataOutputStream wr = new DataOutputStream(con.getOutputStream());

wr.writeBytes(urlParameters);

wr.flush();

wr.close();

 

// Get Response

InputStream is = con.getInputStream();

BufferedReader rd = new BufferedReader(new InputStreamReader(is));

String line;

StringBuilder response = new StringBuilder();

while ((line = rd.readLine()) != null) {

response.append(line);

response.append('\r');

}

rd.close();

System.out.println(response.toString());

 

}

 

}

For any queries please email me at mkbmbattu@gmail.com.

Thanks,

Mohan Kumar B M

 

Average of ratings: -