changed git call from https to git readonly
[atutor.git] / mods / scorm_packages / scorm-1.2 / java / ATutorApiAdapterApplet.java
1 /*
2  * SCORM-1.2 API-Adapter Java Applet for ATutor
3  * Copyright (c) Matthai Kurian
4  *
5  * Made for ATutor, the same license terms as for ATutor itself apply.
6  *
7  * This Applet handles communication between ATutor and SCORM-1.2
8  * Sharable Content Objects (SCOs). Most communication is via Liveconnect.
9  * CMI (Computer Managed Instruction) data is sent to ATutor through http POST
10  * to an ATutor server side PHP script. SCORM-1.2 runtime behavior and CMI
11  * datamodel management is done by the PfPLMS SCORM-1.2 API-Adapter Core. 
12  */
13
14 import java.util.Hashtable;
15 import java.util.Enumeration;
16 import java.net.*;
17 import java.io.*;
18
19 public  class ATutorApiAdapterApplet
20         extends java.applet.Applet
21         implements ch.ethz.pfplms.scorm.api.ApiAdapterInterface
22 {
23         private ch.ethz.pfplms.scorm.api.ApiAdapter core;
24
25         private Hashtable ATutorScoCmi  = new Hashtable();
26
27         private String  ATutorStudentId;
28         private String  ATutorStudentName;
29
30         private String  ATutorScoId;
31         private String  ATutorPreparedScoId;
32
33         private boolean isVerbose   = false;
34
35         public ATutorApiAdapterApplet () {
36                 core = new ch.ethz.pfplms.scorm.api.ApiAdapter ();
37         }
38
39         public  final void init () {
40                 if (getParameter("verbose") != null) isVerbose = true;
41                 ATutorStudentId     = getParameter ("student_id");
42                 ATutorStudentName   = getParameter ("student_name");
43                 say ("cmi.core.student_id=" +ATutorStudentId);
44                 say ("cmi.core.student_name=" +ATutorStudentName);
45         }
46
47         private final void say (String s) {
48                 if (isVerbose) System.out.println (s);
49         }
50
51         private final static String decode (String es) {
52
53                 String s = es.replace('+', ' ');
54                 int l = s.length();
55
56                 int a = 0; 
57
58                 StringBuffer rv = new StringBuffer(l);
59                 byte[] b = null;
60
61                 try {
62                         int i;
63                         while ((i = s.indexOf('%', a)) >= 0) {
64                                 rv.append(s.substring(a, i));
65                                 a = i;
66
67                                 while (i+2 < l && s.charAt(i) == '%') i+=3;
68
69                                 if (b == null || b.length < (i-a)/3) {
70                                         b = new byte[((i-a)/3)];
71                                 }
72
73                                 int x = 0;
74                                 for (; a < i; a+=3) {
75                                         b[x++] = (byte) Integer.parseInt(
76                                                 s.substring(a+1, a+3), 16
77                                         );
78                                 }
79                                 rv.append(new String(b, 0, x, "utf-8"));
80                         }
81
82                         if (a < l) rv.append(s.substring(a));
83                         return rv.toString();
84
85                 } catch (Exception e) {
86                         return "";
87                 }
88         }
89
90         /*
91          * Methods for ATutor to call via Liveconnect
92          */
93
94         public  final void ATutorPrepare (String sco_id) {
95                 URLConnection po;
96                 ATutorScoCmi.clear();
97                 StringBuffer P = new StringBuffer();
98                 P.append ("&sco_id="+sco_id);
99                 say ("Retreiving cmi for sco="+sco_id+" from ATutor server");
100                 try {
101                         //po = (HttpURLConnection) ( new java.net.URL (
102                         po = (URLConnection) ( new java.net.URL (
103                                 getCodeBase().toString() + "read.php"
104                         )).openConnection();
105
106                         po.setRequestProperty (
107                                 "Content-Type",
108                                 "application/x-www-form-urlencoded"
109                         );
110                         po.setRequestProperty (
111                                 "Content-Length",
112                                 Integer.toString (P.length())
113                         );
114                         po.setDoOutput (true);
115                         po.setUseCaches (false);
116                         //po.setRequestMethod ("POST");
117                         po.setAllowUserInteraction (false);
118
119                         OutputStream os = po.getOutputStream();
120                         os.write (P.toString().getBytes());
121                         os.flush ();
122                         os.close ();
123
124                         BufferedReader br = new BufferedReader(
125                                         new InputStreamReader(
126                                                 po.getInputStream ()
127                                         )
128                         );
129                         String s, l, r;
130                         while ((s=br.readLine())!=null) {
131                                 if (s.indexOf('=') == -1) continue;
132                                 l = s.substring (0, s.indexOf('='));
133                                 r = s.substring (s.indexOf('=')+1, s.length());
134                                 r = decode (r);
135                                 say (" "+l + "="+r);
136                                 ATutorScoCmi.put (l,r);
137                         }
138                 } catch (Exception e) {
139                         say ("ATutor cmi retrieval failed.");
140                         say (e.toString());
141                 }
142                 ATutorPreparedScoId = sco_id;
143                 say ("Done. Note: this was cmi for the next sco ("+sco_id+") to launch.");
144         }
145
146         public  final void ATutorReset (String s) {
147                 if (s != null && s.equals(ATutorScoId)) {
148                         ATutorScoId = null;
149                         core.reset();
150                         say ("Reset by ATutor client.");
151                 } 
152         }
153
154         private final String ATutorCommit (boolean fin) {
155
156                 if (ATutorScoId == null) return "false"; 
157
158                 core.transEnd();
159                 StringBuffer P = new StringBuffer();
160                 Hashtable ins = core.getTransNew ();
161                 Hashtable mod = core.getTransMod ();
162                 if (fin) {
163                         Object o = core.sysGet ("cmi.core.entry");
164                         if (o != null && o.toString().equals ("ab-initio")) {
165                                 mod.put ("cmi.core.entry", "");
166                         }
167                 } else {
168                         core.transBegin();
169                 }
170
171                 P.append ("&sco_id=" +ATutorScoId);
172
173                 int i=0;
174                 for (Enumeration e = ins.keys(); e.hasMoreElements(); i++) {
175                         Object l = e.nextElement();
176                         Object r = ins.get(l);
177                         P.append("&iL["+i+"]="+l.toString());
178                         P.append("&iR["+i+"]="+URLEncoder.encode(r.toString()));
179                 }
180
181                 int u=0;
182                 for (Enumeration e = mod.keys(); e.hasMoreElements(); u++) {
183                         Object l = e.nextElement();
184                         Object r = mod.get(l);
185                         P.append("&uL["+u+"]="+l.toString());
186                         P.append("&uR["+u+"]="+URLEncoder.encode(r.toString()));
187                 }
188
189                 if (i == 0 && u == 0) {
190                         say ("Nothing to commit.");
191                         return "true";
192                 }
193
194                 //HttpURLConnection po;
195                 URLConnection po;
196
197                 try {
198                         //po = (HttpURLConnection) ( new java.net.URL (
199                         po = (URLConnection) ( new java.net.URL (
200                                 getCodeBase().toString()
201                                 + "write.php"
202                         )).openConnection();
203
204                         po.setRequestProperty (
205                                 "Content-Type",
206                                 "application/x-www-form-urlencoded"
207                         );
208                         po.setRequestProperty (
209                                 "Content-Length",
210                                 Integer.toString (P.length())
211                         );
212                         po.setDoOutput (true);
213                         po.setUseCaches (false);
214                         //po.setRequestMethod ("POST");
215                         po.setAllowUserInteraction (false);
216
217                         OutputStream os = po.getOutputStream();
218                         os.write (P.toString().getBytes());
219                         os.flush ();
220                         os.close ();
221
222                         BufferedReader r = new BufferedReader(
223                                         new InputStreamReader(
224                                                 po.getInputStream ()
225                                         )
226                         );
227                         try {
228                                 String s;
229                                 while ((s=r.readLine())!=null) {
230                                         say(s);
231                                 }
232                         } catch (EOFException ok) {}
233                         return "true";
234
235                 } catch (Exception e) {
236                         say ("ATutor cmi storage failed.");
237                         say (e.toString());
238                         return "false";
239                 }
240         }
241
242         public  final String ATutorGetValue (String l) {
243                 String rv = core.LMSGetValue (l);
244                 say ("ATutorGetValue("+l+")="+rv);
245                 return rv;
246         }
247
248         /*
249          * Liveconnect interface methods for SCO
250          */
251
252         public  final String LMSInitialize (String s) { 
253                 String rv = core.LMSInitialize(s);
254                 say ("LMSInitialize("+s+")="+rv);
255                 if (rv.equals("false")) return rv;
256                 core.reset();
257                 rv = core.LMSInitialize(s);
258                 ATutorScoId = ATutorPreparedScoId;
259                 core.sysPut ("cmi.core.student_id",   ATutorStudentId);
260                 core.sysPut ("cmi.core.student_name", ATutorStudentName);
261                 core.sysPut (ATutorScoCmi);
262                 core.transBegin();
263                 return rv;
264         }
265
266         public  final String LMSCommit (String s) {
267                 String rv = core.LMSCommit(s);
268                 if (rv.equals("false")) return rv;
269                 rv = ATutorCommit(false); 
270                 say ("LMSCommit("+s+")="+rv);
271                 return rv;
272         }
273
274         public  final String LMSFinish (String s) {
275                 String rv = core.LMSFinish(s);
276                 say ("LMSFinish("+s+")="+rv);
277                 if (rv.equals("false")) return rv;
278                 rv = ATutorCommit(true);
279                 ATutorScoId = null;
280                 core.reset();
281                 return rv;
282         }
283
284         public  final String LMSGetDiagnostic (String e) {
285                 String rv = core.LMSGetDiagnostic (e);
286                 say ("LMSGetDiagnostic("+e+")="+rv);
287                 return rv;
288         }
289
290         public  final String LMSGetErrorString (String e) {
291                 String rv = core.LMSGetErrorString (e);
292                 say ("LMSGetErrorString("+e+")="+rv);
293                 return rv;
294         }
295
296         public  final String LMSGetLastError () {
297                 String rv = core.LMSGetLastError ();
298                 say ("LMSLastError()="+rv);
299                 return rv;
300         }
301
302         public  final String LMSGetValue (String l) {
303                 String rv = core.LMSGetValue (l);
304                 say ("LMSGetValue("+l+")="+rv);
305                 return rv;
306         }
307
308         public  final String LMSSetValue (String l, String r) {
309                 String rv = core.LMSSetValue (l, r);
310                 say ("LMSSetValue("+l+"="+r+")="+rv);
311                 return rv;
312         }
313 }