Commit fd333a56 by Piotr Mitros

Cleanups for code review

parent da93ba6e
...@@ -4,26 +4,25 @@ import pkg_resources ...@@ -4,26 +4,25 @@ import pkg_resources
import uuid import uuid
from xblock.core import XBlock from xblock.core import XBlock
from xblock.fields import Scope, Integer, String, Boolean, DateTime, Float from xblock.fields import Scope, String, Boolean, DateTime, Float
from xblock.fragment import Fragment from xblock.fragment import Fragment
class DoneXBlock(XBlock): class DoneXBlock(XBlock):
""" """
Show a toggle which lets students mark things as done. Show a toggle which lets students mark things as done.
""" """
# Fields are defined on the class. You can access them in your code as
# self.<fieldname>.
done = Boolean( done = Boolean(
scope = Scope.user_state, scope=Scope.user_state,
help = "Is the student done?", help="Is the student done?",
default = False default=False
) )
align = String( align = String(
scope = Scope.settings, scope=Scope.settings,
help = "Align left/right/center", help="Align left/right/center",
default = "left" default="left"
) )
has_score = True has_score = True
...@@ -35,45 +34,39 @@ class DoneXBlock(XBlock): ...@@ -35,45 +34,39 @@ class DoneXBlock(XBlock):
@XBlock.json_handler @XBlock.json_handler
def toggle_button(self, data, suffix=''): def toggle_button(self, data, suffix=''):
"""
Ajax call when the button is clicked. Input is a JSON dictionary
with one boolean field: `done`. This will save this in the
XBlock field, and then issue an appropriate grade.
"""
self.done = data['done'] self.done = data['done']
if data['done']: if data['done']:
grade = 1 grade = 1
else: else:
grade = 0 grade = 0
self.runtime.publish(self, 'grade', {'value':grade, 'max_value': 1}) self.runtime.publish(self, 'grade', {'value': grade, 'max_value': 1})
return {} return {}
def student_view(self, context=None): def student_view(self, context=None):
""" """
The primary view of the DoneXBlock, shown to students The primary view of the DoneXBlock, shown to students
when viewing courses. when viewing courses.
""" """
html = self.resource_string("static/html/done.html").format(done=self.done, html_resource = self.resource_string("static/html/done.html")
id=uuid.uuid1(0)) html = html_resource.format(done=self.done,
unchecked_png = self.runtime.local_resource_url(self, 'public/check-empty.png'); id=uuid.uuid1(0))
checked_png = self.runtime.local_resource_url(self, 'public/check-full.png'); unchecked_png = self.runtime.local_resource_url(self, 'public/check-empty.png')
frag = Fragment(html)#.format(uid=self.scope_ids.usage_id)) checked_png = self.runtime.local_resource_url(self, 'public/check-full.png')
frag.add_css_url("//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css") frag = Fragment(html)
#frag.add_javascript_url("//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js")
frag.add_javascript_url("//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js")
frag.add_css(self.resource_string("static/css/done.css")) frag.add_css(self.resource_string("static/css/done.css"))
grow_left = 1
grow_right = 1
if self.align.lower() == "left":
grow_left = 0
if self.align.lower() == "right":
grow_right = 0
frag.add_css(".done_left_spacer {{ flex-grow:{l}; }} .done_right_spacer {{ flex-grow:{r}; }}".format(r=grow_right, l=grow_left))
frag.add_javascript(self.resource_string("static/js/src/done.js")) frag.add_javascript(self.resource_string("static/js/src/done.js"))
frag.initialize_js("DoneXBlock", {'state':self.done, frag.initialize_js("DoneXBlock", {'state': self.done,
'unchecked':unchecked_png, 'unchecked': unchecked_png,
'checked':checked_png}) 'checked': checked_png,
'align': self.align.lower()})
return frag return frag
# TO-DO: change this to create the scenarios you'd like to see in the
# workbench while developing your XBlock.
@staticmethod @staticmethod
def workbench_scenarios(): def workbench_scenarios():
"""A canned scenario for display in the workbench.""" """A canned scenario for display in the workbench."""
...@@ -81,14 +74,14 @@ class DoneXBlock(XBlock): ...@@ -81,14 +74,14 @@ class DoneXBlock(XBlock):
("DoneXBlock", ("DoneXBlock",
"""<vertical_demo> """<vertical_demo>
<done align="left"> </done> <done align="left"> </done>
<done align="left"> </done> <done align="right"> </done>
</vertical_demo> </vertical_demo>
"""), """),
] ]
## Everything below is stolen from https://github.com/edx/edx-ora2/blob/master/apps/openassessment/xblock/lms_mixin.py ## Everything below is stolen from https://github.com/edx/edx-ora2/blob/master/apps/openassessment/xblock/lms_mixin.py
## It's needed to keep the LMS+Studio happy. ## It's needed to keep the LMS+Studio happy.
## It should be included as a mixin. ## It should be included as a mixin.
display_name = String( display_name = String(
default="Completion", scope=Scope.settings, default="Completion", scope=Scope.settings,
......
...@@ -8,10 +8,18 @@ ...@@ -8,10 +8,18 @@
width:1px; width:1px;
} }
.done_onoffswitch_wrapper {
display: flex;
flex-direction: row;
width: 100%;
}
.done_onoffswitch { .done_onoffswitch {
position: relative; width: 180px; position: relative; width: 180px;
width: 180px;
-webkit-user-select:none; -moz-user-select:none; -ms-user-select: none; -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
} }
.done_onoffswitch-checkbox { .done_onoffswitch-checkbox {
display: none; display: none;
} }
......
<div class="done_onoffswitch"> <div class="done_onoffswitch_wrapper">
<div class="done_left_spacer"></div>
<div class="done_onoffswitch">
<input type="checkbox" name="onoffswitch" class="done_onoffswitch-checkbox" id="{id}_myonoffswitch" checked="{done}"> </input> <input type="checkbox" name="onoffswitch" class="done_onoffswitch-checkbox" id="{id}_myonoffswitch" checked="{done}"> </input>
<label class="done_onoffswitch-label" for="{id}_myonoffswitch"> <label class="done_onoffswitch-label" for="{id}_myonoffswitch">
<span class="done_aria_hidden">I have completed this</span> <span class="done_aria_hidden">I have completed this</span>
<span class="done_onoffswitch-inner"></span> <span class="done_onoffswitch-inner"></span>
<span class="done_onoffswitch-switch"></span> <span class="done_onoffswitch-switch"></span>
</label> </label>
</div>
<div class="done_right_spacer"></div>
</div> </div>
...@@ -10,23 +10,30 @@ function update_knob(element, data) { ...@@ -10,23 +10,30 @@ function update_knob(element, data) {
function DoneXBlock(runtime, element, data) { function DoneXBlock(runtime, element, data) {
$('.done_onoffswitch-checkbox', element).prop("checked", data.state); $('.done_onoffswitch-checkbox', element).prop("checked", data.state);
var grow_left = 1;
var grow_right = 1;
if (data.align == "left") {
grow_left = 0;
}
if (data.align == "right") {
grow_right = 0;
}
$('.done_left_spacer', element).css("flex-grow", grow_left.toString());
$('.done_right_spacer', element).css("flex-grow", grow_right.toString());
update_knob(element, data); update_knob(element, data);
var handlerUrl = runtime.handlerUrl(element, 'toggle_button'); var handlerUrl = runtime.handlerUrl(element, 'toggle_button');
function updateCount(result) {}
$(function ($) { $(function ($) {
// Don't have animations on for above class changes. This is probably not necessary. I
// was seeing animations on page load. I did a few things to fix it. The line below
// wasn't the one that fixed it, but I decided to keep it anyways.
//$('.done_block', element).addClass("done_windshield_animated");
$('.done_onoffswitch', element).addClass("done_animated"); $('.done_onoffswitch', element).addClass("done_animated");
$('.done_onoffswitch-checkbox', element).change(function(){ $('.done_onoffswitch-checkbox', element).change(function(){
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: handlerUrl, url: handlerUrl,
data: JSON.stringify({"done":$('.done_onoffswitch-checkbox', element).prop("checked")}), data: JSON.stringify({"done":$('.done_onoffswitch-checkbox', element).prop("checked")}),
success: updateCount
}); });
update_knob(element, data); update_knob(element, data);
}); });
......
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