Skip to content

Commit

Permalink
Merge pull request #50 from Learnosity/LRN-39610/feature/add-signatur…
Browse files Browse the repository at this point in the history
…e-v2

Lrn 39610/feature/add signature v2
  • Loading branch information
bhavya-shukla-lrn authored Jun 29, 2023
2 parents 738394b + 02f26d7 commit 9cf70d6
Show file tree
Hide file tree
Showing 17 changed files with 421 additions and 86 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ target
Dist
.idea
learnositysdk.iml
.DS_Store
*/.DS_Store
docs/quickstart/assessment/webapps/
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,38 @@ From this point on, we'll assume that your web server is available at this local

http://localhost:9280/quickstart-1.0/

The page will load. This is a basic example of an assessment loaded into a web page with Learnosity's assessment player. You can interact with this demo assessment to try out the various Question types.
You can now access the APIs using the following URL [click here](http://localhost:9280/quickstart-1.0/)

<img width="50%" height="50%" src="docs/images/image-quickstart-index.png">

Following are the routes to access our APIs:

* Author API: http://localhost:9280/quickstart-1.0/AuthorApi.jsp
* Questions API: http://localhost:9280/quickstart-1.0/QuestionsApi.jsp
* Items API: http://localhost:9280/quickstart-1.0/ItemsApi.jsp
* Reports API: http://localhost:9280/quickstart-1.0/ReportsApi.jsp

Open these pages with your web browser. These are all basic examples of Learnosity's integration. You can interact with these demo pages to try out the various APIs. The Items API example is a basic example of an assessment loaded into a web page with Learnosity's assessment player. You can interact with this demo assessment to try out the various Question types.

<img width="50%" height="50%" src="docs/images/image-quickstart-examples-assessment.png">

[(Back to top)](#table-of-contents)

### **How it works**
Let's walk through the code for this standalone assessment example. The source files are included under the quickstart folder, in the following locations:

/learnosity-sdk-java/docs/quickstart/assessment/src/main/java/com/learnosity/quickstart/App.java
/learnosity-sdk-java/docs/quickstart/assessment/src/main/java/com/learnosity/quickstart/ItemsApp.java
/learnosity-sdk-java/docs/quickstart/assessment/src/main/webapp/index.jsp
/learnosity-sdk-java/docs/quickstart/assessment/src/main/resources/config.properties

The first section of code discussed (`App.java`) and is executed server-side. It constructs a set of configuration options for Items API, and securely signs them using the consumer key. The second section is HTML and JavaScript in a JSP page (`index.jsp`) and is executed server-side, and what it generates is loaded in the browser. It renders and runs the assessment functionality.
The first section of code discussed is (`ItemsApp.java`) and this is executed server-side. It constructs a set of configuration options for Items API, and securely signs them using the consumer key. The second section is HTML and JavaScript in a JSP page (`ItemsApi.jsp`) and is executed server-side, and what it generates is loaded in the browser. It renders and runs the assessment functionality.

[(Back to top)](#table-of-contents)

### **Server-side code**

Starting with `App.java`, we start by including some LearnositySDK helpers - they'll make it easy to generate and sign the config options. We also include the standard UUID library for generating unique user and session IDs.
Starting with `ItemsApp.java`, we start by including some LearnositySDK helpers - they'll make it easy to generate and sign the config options. We also include the standard UUID library for generating unique user and session IDs.

``` Java
import learnositysdk.request.Init;
Expand Down Expand Up @@ -161,7 +173,7 @@ private Map<String, String> createRequestObject() {
* `rendering_type`: selects a rendering mode, `assess` mode is a "standalone" mode (loading a complete assessment player for navigation, as opposed to `inline` for embedding without).
* `type`: selects the context for the student response storage. `submit_practice` mode means the student responses will be stored in the Learnosity cloud, allowing for grading and review.
* `name`: human-friendly display name to be shown in reporting, via Reports API and Data API.
* `state`: Optional. Can be set to `initial`, `resume` or `review`. `initial` is the default.
* `state`: optional. Can be set to `initial`, `resume` or `review`. `initial` is the default.

**Note**: you can submit the configuration options as a Java map as shown above, or alternatively as a JSON string, JSON object or JavaBean.

Expand Down Expand Up @@ -202,7 +214,7 @@ We've got our set of signed configuration parameters, so now we can set up our p

This example uses plain HTML in a JSP template, served by the servlet container.

The following example HTML/JSP code can be found near the bottom of the `index.jsp` file.
The following example HTML/JSP code can be found near the bottom of the `ItemsApi.jsp` file.

``` JSP
<html>
Expand All @@ -214,7 +226,7 @@ The following example HTML/JSP code can be found near the bottom of the `index.j
<div id="learnosity_assess"></div>
<!-- Load the Items API library. -->
<script src="https://items.learnosity.com/?v2021.2.LTS"></script>
<script src="https://items.learnosity.com/?latest-lts"></script>
<!-- Initiate Items API assessment rendering, using the JSON blob of signed params. -->
<script>
Expand All @@ -229,7 +241,7 @@ The following example HTML/JSP code can be found near the bottom of the `index.j
The important parts to be aware of in this HTML are:

* A div with `id="learnosity_assess"`. This is where the Learnosity assessment player will be rendered to deliver the assessment.
* The `<script src="https://items.learnosity.com/?v2021.2.LTS"></script>` tag, which includes Learnosity's Items API on the page and makes the global `LearnosityItems` object available. The version specified as `v2021.2.LTS` will retrieve that specific [Long Term Support (LTS) version](https://help.learnosity.com/hc/en-us/articles/360001268538-Release-Cadence-and-Version-Lifecycle). In production, you should always pin to a specific LTS version to ensure version compatibility.
* The `<script src="https://items.learnosity.com/?latest-lts"></script>` tag, which includes Learnosity's Items API on the page and makes the global `LearnosityItems` object available. The version specified as `latest-lts` will retrieve the latest version supported. To know more about switching to a specific LTS version, visit our [Long Term Support (LTS) page](https://help.learnosity.com/hc/en-us/articles/360001268538-Release-Cadence-and-Version-Lifecycle). In production, you should always pin to a specific LTS version to ensure version compatibility.
* The call to `LearnosityItems.init()`, which initiates Items API to inject the assessment player into the page.
* The line `<%= app.initOptions(request.getServerName()) %>` dynamically sends the contents of our init options to JavaScript, so it can be passed to `init()`.

Expand Down
2 changes: 1 addition & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,4 @@ Thanks for reading to the end! Find more information about developing an app wit
<li><a href="http://authorguide.learnosity.com">authorguide.learnosity.com</a> -- authoring documentation for content creators.
</ul>

Back to [README.md](README.md)
Back to [README.md](README.md)
Binary file added docs/images/image-quickstart-index.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
package com.learnosity.quickstart;

import learnositysdk.request.Init;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import org.json.JSONObject;

public class App
public abstract class App
{
private Properties config = new Properties();
private UUID user_id = UUID.randomUUID();
private UUID session_id = UUID.randomUUID();
private String domain;
private String put;
Properties config = new Properties();
UUID user_id = UUID.randomUUID();
UUID session_id = UUID.randomUUID();
String domain;
String put;

public App() {
loadConfig();
}

public String initOptions(String domain) {
Map<String, String> security = createSecurityObject(domain);
Map<String, String> request = createRequestObject();
String secret = config.getProperty("consumerSecret");
try {
Init init = new Init("items", security, secret, request);
return init.generate();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}

private void loadConfig() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream iStream = null;
Expand All @@ -53,23 +39,9 @@ private void loadConfig() {
}
}

private Map<String, String> createSecurityObject(String domain) {
var s = new HashMap();
s.put("domain", domain);
s.put("consumer_key", config.getProperty("consumer"));
return s;
}
abstract String initOptions(String domain);

private Map<String, String> createRequestObject() {
var r = new HashMap();
r.put("user_id", this.user_id);
r.put("activity_template_id", "quickstart_examples_activity_template_001");
r.put("session_id", this.session_id);
r.put("activity_id", "quickstart_examples_activity_001");
r.put("rendering_type", "assess");
r.put("type", "submit_practice");
r.put("name", "Items API Quickstart");
r.put("state", "initial");
return r;
}
abstract Map<String, String> createSecurityObject(String domain);

abstract JSONObject createRequestObject();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.learnosity.quickstart;

import learnositysdk.request.Init;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;

public class AuthorApp extends App
{
public String initOptions(String domain) {
Map<String, String> security = createSecurityObject(domain);
JSONObject request = createRequestObject();
String secret = config.getProperty("consumerSecret");

try {
Init init = new Init("author", security, secret, request);
return init.generate();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}

Map<String, String> createSecurityObject(String domain) {
var security = new HashMap();
security.put("domain", domain);
security.put("consumer_key", config.getProperty("consumer"));
return security;
}

JSONObject createRequestObject() {
JSONObject request = new JSONObject();
request.put("mode", "item_list");
request.put("reference", "my-item-reference");

JSONObject config = new JSONObject();
JSONObject config_item_edit = new JSONObject();
JSONObject config_item_edit_item = new JSONObject();
JSONObject config_item_edit_item_reference = new JSONObject();
config_item_edit_item_reference.put("show", true);
config_item_edit_item_reference.put("edit", true);
config_item_edit_item.put("reference", config_item_edit_item_reference);
config_item_edit_item.put("dynamic_content", true);
config_item_edit_item.put("shared_passage", true);
config_item_edit_item.put("enable_audio_recording", true);
config_item_edit.put("item", config_item_edit_item);
config.put("item_edit", config_item_edit);
request.put("config", config);

JSONObject user = new JSONObject();
user.put("id", "brianmoser");
user.put("firstname", "Test");
user.put("lastname", "Test");
user.put("email", "test@test.com");
request.put("user", user);

return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.learnosity.quickstart;

import learnositysdk.request.Init;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;

public class ItemsApp extends App
{
public String initOptions(String domain) {
Map<String, String> security = createSecurityObject(domain);
JSONObject request = createRequestObject();
String secret = config.getProperty("consumerSecret");
try {
Init init = new Init("items", security, secret, request);
return init.generate();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}

Map<String, String> createSecurityObject(String domain) {
var security = new HashMap();
security.put("domain", domain);
security.put("consumer_key", config.getProperty("consumer"));
return security;
}

JSONObject createRequestObject() {
JSONObject request = new JSONObject();
request.put("user_id", this.user_id);
request.put("activity_template_id", "quickstart_examples_activity_template_001");
request.put("session_id", this.session_id);
request.put("activity_id", "quickstart_examples_activity_001");
request.put("rendering_type", "assess");
request.put("type", "submit_practice");
request.put("name", "Items API Quickstart");
request.put("state", "initial");
return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.learnosity.quickstart;

import learnositysdk.request.Init;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import org.json.JSONArray;

public class QuestionsApp extends App
{
public String initOptions(String domain) {
Map<String, String> security = createSecurityObject(domain);
JSONObject request = createRequestObject();
String secret = config.getProperty("consumerSecret");

try {
Init init = new Init("questions", security, secret, request);
return init.generate();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}

Map<String, String> createSecurityObject(String domain) {
var security = new HashMap();
security.put("domain", domain);
security.put("consumer_key", config.getProperty("consumer"));
security.put("user_id", this.user_id);
return security;
}

JSONObject createRequestObject() {
JSONObject request = new JSONObject();
request.put("type", "local_practice");
request.put("state", "initial");
request.put("id", "questionsapi-demo");
request.put("name", "Questions API Demo");
request.put("course", "mycourse");

JSONArray arr_questions = new JSONArray();
JSONObject question = new JSONObject();
question.put("type", "association");
question.put("response_id", "60001");
question.put("stimulus", "Match the cities to the parent nation.");
question.put("instant_feedback", true);

JSONArray stimulust_list = new JSONArray();
stimulust_list.put("London");
stimulust_list.put("Dublin");
stimulust_list.put("Paris");
stimulust_list.put("Sydney");
question.put("stimulus_list", stimulust_list);

JSONArray possible_responses = new JSONArray();
possible_responses.put("Australia");
possible_responses.put("France");
possible_responses.put("Ireland");
possible_responses.put("England");
question.put("possible_responses", possible_responses);

JSONObject validation = new JSONObject();
JSONArray valid_responses = new JSONArray();
valid_responses.put(new JSONArray().put("England"));
valid_responses.put(new JSONArray().put("Ireland"));
valid_responses.put(new JSONArray().put("France"));
valid_responses.put(new JSONArray().put("Australia"));
validation.put("valid_responses", valid_responses);
question.put("validation", validation);

arr_questions.put(question);
request.put("questions", arr_questions);

return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.learnosity.quickstart;

import learnositysdk.request.Init;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import org.json.JSONArray;

public class ReportsApp extends App
{
public String initOptions(String domain) {
Map<String, String> security = createSecurityObject(domain);
JSONObject request = createRequestObject();
String secret = config.getProperty("consumerSecret");

try {
Init init = new Init("reports", security, secret, request);
return init.generate();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}

Map<String, String> createSecurityObject(String domain) {
var security = new HashMap();
security.put("domain", domain);
security.put("consumer_key", config.getProperty("consumer"));
return security;
}

JSONObject createRequestObject() {
JSONObject report = new JSONObject();
report.put("id", "session-detail");
report.put("type", "session-detail-by-item");
report.put("user_id", this.user_id);
report.put("session_id", this.session_id);

JSONArray reports = new JSONArray();
reports.put(report);

JSONObject request = new JSONObject();
request.put("reports", reports);

return request;
}
}
Loading

0 comments on commit 9cf70d6

Please sign in to comment.