Commit 1abe15d4 by Prem Sichanugrist

Rewrite Problem module

parent 8e7bf4c1
...@@ -61,12 +61,6 @@ class Module(XModule): ...@@ -61,12 +61,6 @@ class Module(XModule):
'ajax_url':self.ajax_url, 'ajax_url':self.ajax_url,
}) })
def get_init_js(self):
return render_to_string('problem.js',
{'id':self.item_id,
'ajax_url':self.ajax_url,
})
def get_problem_html(self, encapsulate=True): def get_problem_html(self, encapsulate=True):
html = self.lcp.get_html() html = self.lcp.get_html()
content={'name':self.name, content={'name':self.name,
...@@ -130,7 +124,7 @@ class Module(XModule): ...@@ -130,7 +124,7 @@ class Module(XModule):
html=render_to_string('problem.html', context) html=render_to_string('problem.html', context)
if encapsulate: if encapsulate:
html = '<div id="main_{id}">'.format(id=self.item_id)+html+"</div>" html = '<div id="problem_{id}" class="problem" data-url="{ajax_url}">'.format(id=self.item_id)+html+"</div>"
return html return html
......
...@@ -27,7 +27,7 @@ describe 'Courseware', -> ...@@ -27,7 +27,7 @@ describe 'Courseware', ->
it 'binds the sequential content change event', -> it 'binds the sequential content change event', ->
@courseware.bind() @courseware.bind()
expect($('#seq_content')).toHandleWith 'change', @courseware.render expect($('#seq_content')).toHandleWith 'contentChanged', @courseware.render
describe 'render', -> describe 'render', ->
beforeEach -> beforeEach ->
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
}); });
return it('binds the sequential content change event', function() { return it('binds the sequential content change event', function() {
this.courseware.bind(); this.courseware.bind();
return expect($('#seq_content')).toHandleWith('change', this.courseware.render); return expect($('#seq_content')).toHandleWith('contentChanged', this.courseware.render);
}); });
}); });
return describe('render', function() { return describe('render', function() {
......
...@@ -12,9 +12,12 @@ class @Courseware ...@@ -12,9 +12,12 @@ class @Courseware
bind: -> bind: ->
if $('#seq_content').length if $('#seq_content').length
$('#seq_content').change @render $('#seq_content').bind 'contentChanged', @render
render: -> render: ->
$('.course-content .video').each -> $('.course-content .video').each ->
id = $(this).attr('id').replace(/video_/, '') id = $(this).attr('id').replace(/video_/, '')
new Video id, $(this).data('streams') new Video id, $(this).data('streams')
$('.course-content .problems-wrapper').each ->
id = $(this).attr('id').replace(/problem_/, '')
new Problem id, $(this).data('url')
...@@ -54,6 +54,13 @@ div.course-wrapper { ...@@ -54,6 +54,13 @@ div.course-wrapper {
display: table-cell; display: table-cell;
vertical-align: top; vertical-align: top;
&.problem-header {
section.staff {
margin-top: 30px;
font-size: 80%;
}
}
@media screen and (max-width:1120px) { @media screen and (max-width:1120px) {
display: block; display: block;
width: auto; width: auto;
......
class @Problem
constructor: (@id, url) ->
@element = $("#problem_#{id}")
@content_url = "#{url}problem_get?id=#{@id}"
@render()
$: (selector) ->
$(selector, @element)
bind: =>
MathJax.Hub.Queue ["Typeset",MathJax.Hub]
window.update_schematics()
@$('section.action input:button').click @refreshAnswers
@$('section.action input.check').click @check
@$('section.action input.reset').click @reset
@$('section.action input.show').click @show
@$('section.action input.save').click @save
render: (content) ->
if content
@element.html(content)
@bind()
else
@element.load @content_url, @bind
check: =>
Logger.log 'problem_check', @answers
$.post "/modx/problem/#{@id}/problem_check", @answers, (response) =>
switch response.success
when 'incorrect', 'correct'
@render(response.contents)
else
alert(response.success)
reset: =>
Logger.log 'problem_reset', @answers
$.post "/modx/problem/#{@id}/problem_reset", id: @id, (content) =>
@render(content)
show: =>
Logger.log 'problem_show', problem: @id
$.post "/modx/problem/#{@id}/problem_show", (response) =>
$.each response, (key, value) =>
@$("#answer_#{key}").text(value)
save: =>
Logger.log 'problem_save', @answers
$.post "/modx/problem/#{@id}/problem_save", @answers, (response) =>
if response.success
alert 'Saved'
refreshAnswers: =>
@answers = {}
@$('input.schematic').each (index, element) ->
element.schematic.update_value()
$.each @$("[id^=input_#{@id}_]"), (index, input) =>
@answers[$(input).attr('id')] = $(input).val()
...@@ -39,7 +39,7 @@ class window.Sequence ...@@ -39,7 +39,7 @@ class window.Sequence
MathJax.Hub.Queue(["Typeset",MathJax.Hub]) MathJax.Hub.Queue(["Typeset",MathJax.Hub])
@position = new_position @position = new_position
$('#seq_content').change() $('#seq_content').trigger 'contentChanged'
goto: (event) => goto: (event) =>
event.preventDefault() event.preventDefault()
......
<%namespace name='static' file='static_content.html'/> <%namespace name='static' file='static_content.html'/>
<h2 class="problem-header">${ problem['name'] } <h2 class="problem-header">
% if problem['weight']: ${ problem['name'] }
: ${ problem['weight'] } points % if problem['weight']:
% endif : ${ problem['weight'] } points
% if settings.QUICKEDIT: % endif
<span class="staff">
<br/> % if settings.QUICKEDIT:
<br/> <section class="staff">
<br/> <a href=${MITX_ROOT_URL}/quickedit/${id}>Quick Edit Problem</a>
<br/> </section>
<font size=-2><a href=${MITX_ROOT_URL}/quickedit/${id}>Quick % endif
Edit Problem</a></font></span>
% endif
</h2> </h2>
<section class="problem"> <section class="problem">
...@@ -21,16 +19,16 @@ Edit Problem</a></font></span> ...@@ -21,16 +19,16 @@ Edit Problem</a></font></span>
<input type="hidden" name="problem_id" value="${ problem['name'] }"> <input type="hidden" name="problem_id" value="${ problem['name'] }">
% if check_button: % if check_button:
<input id="check_${ id }" type="button" value="${ check_button }" > <input class="check" type="button" value="${ check_button }">
% endif % endif
% if reset_button: % if reset_button:
<input id="reset_${ id }" type="button" value="Reset" > <input class="reset" type="button" value="Reset">
% endif % endif
% if save_button: % if save_button:
<input id="save_${ id }" type="button" value="Save" > <input class="save" type="button" value="Save">
% endif % endif
% if answer_available: % if answer_available:
<input id="show_${ id }" type="button" value="Show Answer" > <input class="show" type="button" value="Show Answer">
% endif % endif
% if explain : % if explain :
<a href="/courseware/6.002_Spring_2012/${ explain }" class="new-page">Explanation</a> <a href="/courseware/6.002_Spring_2012/${ explain }" class="new-page">Explanation</a>
...@@ -42,3 +40,4 @@ Edit Problem</a></font></span> ...@@ -42,3 +40,4 @@ Edit Problem</a></font></span>
% endif % endif
</section> </section>
</section> </section>
function ${ id }_content_updated() {
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
update_schematics();
$('#check_${ id }').unbind('click').click(function() {
$("input.schematic").each(function(index,element){ element.schematic.update_value(); });
$(".CodeMirror").each(function(index,element){ if (element.CodeMirror.save) element.CodeMirror.save(); });
var submit_data={};
$.each($("[id^=input_${ id }_]"), function(index,value){
if (value.type==="checkbox"){
if (value.checked) {
if (typeof submit_data[value.name] == 'undefined'){
submit_data[value.name]=[];
}
submit_data[value.name].push(value.value);
}
}
if (value.type==="radio"){
if (value.checked) {
submit_data[value.name]= value.value;
}
}
else{
submit_data[value.id]=value.value;
}
});
postJSON('${ MITX_ROOT_URL }/modx/problem/${ id }/problem_check',
submit_data,
function(json) {
switch(json.success) {
case 'incorrect': // Worked, but answer not
case 'correct':
$('#main_${ id }').html(json.contents);
${ id }_content_updated();
break;
default:
alert(json.success);
}}
);
log_event('problem_check', submit_data);
});
$('#reset_${ id }').unbind('click').click(function() {
var submit_data={};
$.each($("[id^=input_${ id }_]"), function(index,value){
submit_data[value.id]=value.value;
});
postJSON('${ MITX_ROOT_URL }/modx/problem/${ id }/problem_reset', {'id':'${ id }'}, function(html_as_json) {
$('#main_${ id }').html(html_as_json);
${ id }_content_updated();
});
log_event('problem_reset', submit_data);
});
// show answer button
// TODO: the button should turn into "hide answer" afterwards
$('#show_${ id }').unbind('click').click(function() {
postJSON('${ MITX_ROOT_URL }/modx/problem/${ id }/problem_show', {}, function(data) {
for (var key in data) {
if ($.isArray(data[key])){
for (var ans_index in data[key]){
var choice_id = 'input_'+key+'_'+data[key][ans_index];
$("label[for="+choice_id+"]").attr("correct_answer", "true");
}
}
$("#answer_"+key).text(data[key]);
}
});
log_event('problem_show', {'problem':'${ id }'});
});
$('#save_${ id }').unbind('click').click(function() {
$("input.schematic").each(function(index,element){ element.schematic.update_value(); });
var submit_data={};
$.each($("[id^=input_${ id }_]"), function(index,value) {
submit_data[value.id]=value.value;
});
postJSON('${ MITX_ROOT_URL }/modx/problem/${ id }/problem_save',
submit_data,
function(data) {
if(data.success) {
alert('Saved');
}});
log_event('problem_save', submit_data);
});
}
function ${ id }_load() {
$('#main_${ id }').load('${ ajax_url }problem_get?id=${ id }', ${ id }_content_updated);
}
$(function() {
${ id }_load();
});
<section id="main_${id}" class="problems-wrapper"></section> <section id="problem_${id}" class="problems-wrapper" data-url="${ajax_url}"></section>
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