Commit fe42ca51 by Sean Lip

Add display_name field and author_view method; remove width/height fields; add…

Add display_name field and author_view method; remove width/height fields; add event tracking; include a local copy of the embedding script with an appropriate title for the iframe.
parent 6b251371
*.pyc
oppia_xblock.egg-info/*
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
import pkg_resources import pkg_resources
from eventtracking import tracker
from xblock.core import XBlock from xblock.core import XBlock
from xblock.fields import Scope, Integer, String from xblock.fields import Scope, Integer, String
from xblock.fragment import Fragment from xblock.fragment import Fragment
...@@ -27,26 +29,28 @@ class OppiaXBlock(XBlock): ...@@ -27,26 +29,28 @@ class OppiaXBlock(XBlock):
""" """
An XBlock providing an embedded Oppia exploration. An XBlock providing an embedded Oppia exploration.
""" """
_EVENT_NAME_EXPLORATION_LOADED = 'oppia.exploration.loaded'
_EVENT_NAME_EXPLORATION_COMPLETED = 'oppia.exploration.completed'
_EVENT_NAME_STATE_TRANSITION = 'oppia.exploration.state.changed'
# The display name of the component. Note that this is not editable in
# Studio.
display_name = String(
help="Display name of the component",
default="Oppia Exploration",
scope=Scope.settings)
# Note: These fields are defined on the class, and can be accessed in the # Note: These fields are defined on the class, and can be accessed in the
# code as self.<fieldname>. # code as self.<fieldname>.
oppiaid = String( oppiaid = String(
help="ID of the Oppia exploration to embed", help="ID of the Oppia exploration to embed",
default=None, default="4",
scope=Scope.content) scope=Scope.content)
src = String( src = String(
help="Source URL of the site", help="Source URL of the site",
default="https://www.oppia.org", default="https://www.oppia.org",
scope=Scope.content) scope=Scope.content)
width = Integer(
help="Width of the embedded exploration",
default=700,
scope=Scope.content)
height = Integer(
help="Height of the embedded exploration",
default=500,
scope=Scope.content)
def resource_string(self, path): def resource_string(self, path):
"""Handy helper for getting resources from our kit.""" """Handy helper for getting resources from our kit."""
...@@ -60,35 +64,50 @@ class OppiaXBlock(XBlock): ...@@ -60,35 +64,50 @@ class OppiaXBlock(XBlock):
""" """
html = self.resource_string("static/html/oppia.html") html = self.resource_string("static/html/oppia.html")
frag = Fragment(html.format(self=self)) frag = Fragment(html.format(self=self))
frag.add_javascript_url( frag.add_javascript(
"//cdn.jsdelivr.net/oppia/0.0.1/oppia-player.min.js") self.resource_string('static/lib/oppia-player-0.0.1-modified.js'))
frag.add_javascript(self.resource_string("static/js/oppia.js")) frag.add_javascript(self.resource_string("static/js/oppia.js"))
frag.initialize_js('OppiaXBlock') frag.initialize_js('OppiaXBlock')
return frag return frag
def _log(self, message): def author_view(self, context=None):
"""
A view of the XBlock to show within the Studio preview. For some
reason, the student_view() does not display, so we show a placeholder
instead.
"""
html = self.resource_string("static/html/oppia_preview.html")
frag = Fragment(html.format(self=self))
return frag
def _log(self, event_name, payload):
""" """
Logger for load, state transition and completion events. Logger for load, state transition and completion events.
""" """
pass tracker.emit(event_name, payload)
@XBlock.json_handler @XBlock.json_handler
def on_exploration_loaded(self, data, suffix=''): def on_exploration_loaded(self, data, suffix=''):
"""Called when an exploration has loaded.""" """Called when an exploration has loaded."""
self._log('Exploration %s was loaded.' % self.oppiaid) self._log(self._EVENT_NAME_EXPLORATION_LOADED, {
'exploration_id': self.oppiaid,
})
@XBlock.json_handler @XBlock.json_handler
def on_state_transition(self, data, suffix=''): def on_state_transition(self, data, suffix=''):
"""Called when a state transition in the exploration has occurred.""" """Called when a state transition in the exploration has occurred."""
self._log( self._log(self._EVENT_NAME_STATE_TRANSITION, {
"Recording the following state transition for exploration %s: " 'exploration_id': self.oppiaid,
"'%s' to '%s'" % ( 'old_state_name': data['oldStateName'],
self.oppiaid, data['oldStateName'], data['newStateName'])) 'new_state_name': data['newStateName'],
})
@XBlock.json_handler @XBlock.json_handler
def on_exploration_completed(self, data, suffix=''): def on_exploration_completed(self, data, suffix=''):
"""Called when the exploration has been completed.""" """Called when the exploration has been completed."""
self._log('Exploration %s has been completed.' % self.oppiaid) self._log(self._EVENT_NAME_EXPLORATION_COMPLETED, {
'exploration_id': self.oppiaid
})
def studio_view(self, context): def studio_view(self, context):
""" """
...@@ -98,8 +117,7 @@ class OppiaXBlock(XBlock): ...@@ -98,8 +117,7 @@ class OppiaXBlock(XBlock):
__name__, "static/html/oppia_edit.html") __name__, "static/html/oppia_edit.html")
oppiaid = self.oppiaid or '' oppiaid = self.oppiaid or ''
frag = Fragment(unicode(html_str).format( frag = Fragment(unicode(html_str).format(
oppiaid=oppiaid, src=self.src, width=self.width, oppiaid=oppiaid, src=self.src))
height=self.height))
js_str = pkg_resources.resource_string( js_str = pkg_resources.resource_string(
__name__, "static/js/oppia_edit.js") __name__, "static/js/oppia_edit.js")
...@@ -115,8 +133,6 @@ class OppiaXBlock(XBlock): ...@@ -115,8 +133,6 @@ class OppiaXBlock(XBlock):
""" """
self.oppiaid = data.get('oppiaid') self.oppiaid = data.get('oppiaid')
self.src = data.get('src') self.src = data.get('src')
self.width = data.get('width')
self.height = data.get('height')
return {'result': 'success'} return {'result': 'success'}
...@@ -126,7 +142,7 @@ class OppiaXBlock(XBlock): ...@@ -126,7 +142,7 @@ class OppiaXBlock(XBlock):
return [ return [
("Oppia Embedding", ("Oppia Embedding",
"""<vertical_demo> """<vertical_demo>
<oppia oppiaid="0" src="https://www.oppia.org" width="700" /> <oppia oppiaid="0" src="https://www.oppia.org"/>
</vertical_demo> </vertical_demo>
"""), """),
] ]
<div class="oppia_block"> <div class="oppia_block"
style="margin-left: auto; margin-right: auto; width: 700px;">
<oppia oppia-id="{self.oppiaid}" src="{self.src}" <oppia oppia-id="{self.oppiaid}" src="{self.src}"
width="{self.width}" height="{self.height}"></oppia> width="700" height="900"></oppia>
</div> </div>
...@@ -14,20 +14,6 @@ ...@@ -14,20 +14,6 @@
</div> </div>
<span class="tip setting-help">Example: https://www.oppia.org</span> <span class="tip setting-help">Example: https://www.oppia.org</span>
</li> </li>
<li class="field comp-setting-entry is-set">
<div class="wrapper-comp-setting">
<label class="label setting-label" for="width">Width</label>
<input class="input setting-input" name="width" id="width" value="{width}" type="text" />
</div>
<span class="tip setting-help">Width of the embedded exploration</span>
</li>
<li class="field comp-setting-entry is-set">
<div class="wrapper-comp-setting">
<label class="label setting-label" for="height">Height</label>
<input class="input setting-input" name="height" id="height" value="{height}" type="text" />
</div>
<span class="tip setting-help">Height of the embedded exploration</span>
</li>
</ul> </ul>
<div class="xblock-actions"> <div class="xblock-actions">
<ul> <ul>
......
<div style="opacity: 0.7;">
<p>
This XBlock does not have an inline Studio preview.
</p>
<p>
It is an embedded version of the Oppia exploration with id
<strong>"{self.oppiaid}"</strong> located at
<strong>{self.src}</strong>.
</p>
</div>
...@@ -21,9 +21,7 @@ function OppiaXBlockEditor(runtime, element) { ...@@ -21,9 +21,7 @@ function OppiaXBlockEditor(runtime, element) {
var handlerUrl = runtime.handlerUrl(element, 'studio_submit'); var handlerUrl = runtime.handlerUrl(element, 'studio_submit');
var data = { var data = {
oppiaid: $(element).find('input[name=oppiaid]').val(), oppiaid: $(element).find('input[name=oppiaid]').val(),
src: $(element).find('input[name=src]').val(), src: $(element).find('input[name=src]').val()
width: $(element).find('input[name=width]').val(),
height: $(element).find('input[name=height]').val()
}; };
runtime.notify('save', {state: 'start'}); runtime.notify('save', {state: 'start'});
$.post(handlerUrl, JSON.stringify(data)).done(function(response) { $.post(handlerUrl, JSON.stringify(data)).done(function(response) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment