|
| 1 | +# An example script showing the functionality of the TinCan Python Library |
| 2 | + |
| 3 | +import uuid |
| 4 | +from resources import lrs_properties |
| 5 | +from tincan.remote_lrs import RemoteLRS |
| 6 | +from tincan.statement import Statement |
| 7 | +from tincan.agent import Agent |
| 8 | +from tincan.verb import Verb |
| 9 | +from tincan.activity import Activity |
| 10 | +from tincan.context import Context |
| 11 | +from tincan.language_map import LanguageMap |
| 12 | +from tincan.activity_definition import ActivityDefinition |
| 13 | +from tincan.documents import StateDocument |
| 14 | + |
| 15 | +# construct an LRS |
| 16 | +print "constructing the LRS..." |
| 17 | +lrs = RemoteLRS( |
| 18 | + version=lrs_properties.version, |
| 19 | + endpoint=lrs_properties.endpoint, |
| 20 | + username=lrs_properties.username, |
| 21 | + password=lrs_properties.password, |
| 22 | +) |
| 23 | +print "...done" |
| 24 | + |
| 25 | +# construct the actor of the statement |
| 26 | +print "constructing the Actor..." |
| 27 | +actor = Agent( |
| 28 | + name='UserMan', |
| 29 | + mbox='mailto:tincanpython@tincanapi.com', |
| 30 | +) |
| 31 | +print "...done" |
| 32 | + |
| 33 | +# construct the verb of the statement |
| 34 | +print "constructing the Verb..." |
| 35 | +verb = Verb( |
| 36 | + id='http://adlnet.gov/expapi/verbs/experienced', |
| 37 | + display=LanguageMap({'en-US': 'experienced'}), |
| 38 | +) |
| 39 | +print "...done" |
| 40 | + |
| 41 | +# construct the object of the statement |
| 42 | +print "constructing the Object..." |
| 43 | +object = Activity( |
| 44 | + id='http://tincanapi.com/TinCanPython/Test/Unit/0', |
| 45 | + definition=ActivityDefinition( |
| 46 | + name=LanguageMap({'en-US': 'TinCanPython Library'}), |
| 47 | + description=LanguageMap({'en-US': 'Use of, or interaction with, the TinCanPython Library'}), |
| 48 | + ), |
| 49 | +) |
| 50 | +print "...done" |
| 51 | + |
| 52 | +# construct a context for the statement |
| 53 | +print "constructing the Context..." |
| 54 | +context = Context( |
| 55 | + registration=uuid.uuid4(), |
| 56 | + instructor=Agent( |
| 57 | + name='Lord TinCan', |
| 58 | + mbox='mailto:tincanpython@tincanapi.com', |
| 59 | + ), |
| 60 | + #language='en-US', |
| 61 | +) |
| 62 | +print "...done" |
| 63 | + |
| 64 | +# construct the actual statement |
| 65 | +print "constructing the Statement..." |
| 66 | +statement = Statement( |
| 67 | + actor=actor, |
| 68 | + verb=verb, |
| 69 | + object=object, |
| 70 | + context=context, |
| 71 | +) |
| 72 | +print "...done" |
| 73 | + |
| 74 | +# save our statement to the remote_lrs and store the response in 'response' |
| 75 | +print "saving the Statement..." |
| 76 | +response = lrs.save_statement(statement) |
| 77 | + |
| 78 | +if not response: |
| 79 | + raise ValueError("statement failed to save") |
| 80 | +print "...done" |
| 81 | + |
| 82 | +# retrieve our statement from the remote_lrs using the id returned in the response |
| 83 | +print "Now, retrieving statement..." |
| 84 | +response = lrs.retrieve_statement(response.content.id) |
| 85 | + |
| 86 | +if not response.success: |
| 87 | + raise ValueError("statement could not be retrieved") |
| 88 | +print "...done" |
| 89 | + |
| 90 | +print "constructing new Statement from retrieved statement data..." |
| 91 | +ret_statement = Statement.from_json(response.data) |
| 92 | +print "...done" |
| 93 | + |
| 94 | +# now, using our old statement and our returned statement, we can send multiple statements |
| 95 | +# note: these statements are logically identical, but are 2 separate objects |
| 96 | +print "saving both Statements" |
| 97 | +response = lrs.save_statements([statement, ret_statement]) |
| 98 | + |
| 99 | +if not response: |
| 100 | + raise ValueError("statements failed to save") |
| 101 | +print "...done" |
| 102 | + |
| 103 | +# we can query our statements using an object |
| 104 | +# constructing the query object with common fields |
| 105 | +# note: more information about queries can be found in the xAPI spec |
| 106 | +# https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#723-getstatements |
| 107 | +query = { |
| 108 | + "agent": actor, |
| 109 | + "verb": verb, |
| 110 | + "activity": object, |
| 111 | + "related_activities": True, |
| 112 | + "related_agents": True, |
| 113 | + "limit": 2, |
| 114 | +} |
| 115 | + |
| 116 | +print "querying statements..." |
| 117 | +response = lrs.query_statements(query) |
| 118 | + |
| 119 | +if not response: |
| 120 | + raise ValueError("statements could not be queried") |
| 121 | +print "...done" |
| 122 | + |
| 123 | +# now we will explore saving a document, e.g. a state document |
| 124 | +print "constructing a state document..." |
| 125 | +state_document = StateDocument( |
| 126 | + activity=object, |
| 127 | + agent=actor, |
| 128 | + id='stateDoc', |
| 129 | + content=bytearray('stateDocValue', encoding='utf-8'), |
| 130 | +) |
| 131 | +print "...done" |
| 132 | + |
| 133 | +print "saving state document..." |
| 134 | +response = lrs.save_state(state_document) |
| 135 | + |
| 136 | +if not response.success: |
| 137 | + raise ValueError("could not save state document") |
| 138 | +print "...done" |
0 commit comments