Commit e0b54700 by Justin Riley

randomize: fix off-by-1 issue with staff controls

* Require staff in randomize xmodule's handle_ajax!!!
* Use .val() instead of .text() in rand_jump JS
* Disable all alert() calls when using 'next' or 'jump' controls
parent ffaea78d
......@@ -48,7 +48,6 @@ class RandomizeModule(RandomizeFields, XModule):
# NOTE: calling self.get_children() creates a circular reference --
# it calls get_child_descriptors() internally, but that doesn't work
# until we've picked a choice
# import pudb; pudb.set_trace()
xml_attrs = self.descriptor.xml_attributes or []
use_randrange = 'use_randrange' in xml_attrs
# TODO: use this attr to toggle storing history and never showing the
......@@ -140,16 +139,18 @@ class RandomizeModule(RandomizeFields, XModule):
return child_html
def handle_ajax(self, dispatch, data):
choices = self.get_choices().keys()
index = self.get_choice_index(choices=choices)
if dispatch == 'next':
self.choice = choices[index + 1]
elif dispatch == 'jump':
log.debug('jump, data=%s' % data)
self.choice = choices[int(data['choice'])]
#num_choices = len(choices)
#if self.choice >= num_choices:
#self.choice = 0
if self.runtime.user_is_staff:
choices = self.get_choices().keys()
index = self.get_choice_index(choices=choices)
try:
if dispatch == 'next':
self.choice = choices[index + 1]
elif dispatch == 'jump':
log.debug('jump, data=%s' % data)
self.choice = choices[int(data['choice'])]
except (IndexError, ValueError):
# log the error in this case and return the current choice
log.exception("error in randomize next/jump (IGNORED):")
result = {'ret': "choice=%s" % self.choice}
return json.dumps(result)
......
% if is_staff:
<div id="randomize_${element_id}_control">
<p>
<input id="randomize_next" type="button" value="This is randomized module ${choice} of ${num_choices}: Go to next"/>
<input id="randomize_next" type="button" value="This is randomized module ${choice + 1} of ${num_choices}: Go to next"/>
or jump to:
<select id="randomize_jump">
% for kchoice in range(num_choices):
<% selected = "selected" if kchoice==choice else "" %>
<option value="${kchoice}" ${selected}>${kchoice}</option>
<option value="${kchoice}" ${selected}>${kchoice + 1}</option>
% endfor
</select>
</p>
......@@ -17,18 +17,18 @@
do_next = function(){
$.get( "${ajax_url}/next", function( data ) {
$( "#randomize_next" ).html( data );
alert( "next successful" );
// alert( "next successful" );
location.reload();
});
}
$('#randomize_next').click(do_next);
rand_jump = function(){
var choice = $('#randomize_jump option:selected').text();
var choice = $('#randomize_jump option:selected').val();
$.post( "${ajax_url}/jump", { 'choice': choice},
function( data ) {
$( "#randomize_next" ).html( data );
alert( "jump to " + choice + " successful" );
// alert( "jump to " + choice + " successful" );
location.reload();
});
}
......
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