LoginSignup
0
0

More than 5 years have passed since last update.

caliper-jsのサンプルプログラム

Last updated at Posted at 2018-08-06

caliper-jsのインストールのつづき。

ビルドしたcaliper-jsを使う。

<script src="caliperSensor-1.1.3.js"></script>

最新ではないがimsglobal.orgにライブラリがある。

<script src="http://purl.imsglobal.org/caliper/caliperSensor-1.1.1.js"></script>

以下の内容はcaliper-jsのサンプルを書き変えたものである。まず,EntityFactoryとEventFactoryで,entityとeventを記述していく。IMS Caliper 1.1の仕様によると,AssessmentEventではid,type,actor,action,obj,eventTimeが必須である。

var actor = Caliper.Entities.EntityFactory().create(Caliper.Entities.Person, {id: "https://example.edu/users/554433"});
var action = Caliper.Actions.started.term
var obj = Caliper.Entities.EntityFactory().create(Caliper.Entities.Assessment, {
  id: "https://example.edu/terms/201801/courses/7/sections/1/assess/1",
  dateToStartOn: "2018-11-14T05:00:00.000Z",
  dateToSubmit": "2018-11-18T11:59:59.000Z",
  maxAttempts: 1,
  maxScore: 25.0
  // ... add additional optional property assignments
});

// Create Event
var event = Caliper.Events.EventFactory().create(Caliper.Events.AssessmentEvent, {
  id: id,
  actor: actor,
  action: action,
  object: obj,
  eventTime: new Date().toISOString(),
});

作成したeventをエンベロープにおさめて,エンドポイントに送信する。

// Initialize Caliper sensor
var sensor = Caliper.Sensor;
sensor.initialize("http://example.org/sensors/1");

// Override default HTTP options
var options = Caliper.Clients.HttpOptions;
options.uri = "https://example.edu/caliper/target/endpoint";
options.headers["Authorization"] = "40dI6P62Q_qrWxpTk95z8w";

// Initialize and register client
var client = Caliper.Clients.HttpClient;
client.initialize("http://example.org/sensors/1/clients/2", options);
sensor.registerClient(client);

// Create data payload
var payload = [];
payload.push(event);

// Envelope options
var opts = {
  sensor: sensor.id,
  sendTime: new Date().toISOString(),
  dataVersion: "http://purl.imsglobal.org/ctx/caliper/v1p1",
  data: payload
};

// Create envelope with data payload
var envelope = sensor.createEnvelope(opts);

// Delegate transmission responsibilities to client
sensor.sendToClient(client, envelope);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0