staff_debug_actions.js 4.11 KB
Newer Older
Carson Gee committed
1
// Build StaffDebug object
2
var StaffDebug = (function (){
3 4 5

  get_current_url = function() {
    return window.location.pathname;
6
  };
7 8 9

  get_url = function(action){
    var pathname = this.get_current_url();
10
    var url = pathname.substr(0,pathname.indexOf('/courseware')) + '/instructor/api/' + action;
11
    return url;
12
  };
13

14 15
  sanitized_string = function(string) {
    return string.replace(/[.*+?^:${}()|[\]\\]/g, "\\$&");
16
  };
17

18
  get_user = function(locname){
19
    locname = sanitized_string(locname);
20
    var uname = $('#sd_fu_' + locname).val();
21
    if (uname===""){
22 23 24
        uname =  $('#sd_fu_' + locname).attr('placeholder');
    }
    return uname;
25
  };
26 27 28 29 30 31

  do_idash_action = function(action){
    var pdata = {
        'problem_to_reset': action.location,
        'unique_student_identifier': get_user(action.locationName),
        'delete_module': action.delete_module
32
    };
33 34 35 36 37
    $.ajax({
        type: "GET",
        url: get_url(action.method),
        data: pdata,
        success: function(data){
38 39
            var text = _.template(action.success_msg, {interpolate: /\{(.+?)\}/g})(
                {user: data.student}
40
            );
41 42
            var html = _.template('<p id="idash_msg" class="success">{text}</p>', {interpolate: /\{(.+?)\}/g})(
                {text: text}
43 44
            );
            $("#result_"+sanitized_string(action.locationName)).html(html);
45 46 47 48 49 50 51 52
        },
        error: function(request, status, error) {
            var response_json;
            try {
                response_json = $.parseJSON(request.responseText);
            } catch(e) {
                response_json = { error: gettext('Unknown Error Occurred.') };
            }
53
            var text = _.template('{error_msg} {error}', {interpolate: /\{(.+?)\}/g})(
54 55 56
                {
                    error_msg: action.error_msg,
                    error: response_json.error
57
                }
58
            );
59 60
            var html = _.template('<p id="idash_msg" class="error">{text}</p>', {interpolate: /\{(.+?)\}/g})(
                {text: text}
61 62 63
            );
            $("#result_"+sanitized_string(action.locationName)).html(html);

64 65 66
        },
        dataType: 'json'
    });
67
  };
68 69 70 71 72 73 74 75 76 77

  reset = function(locname, location){
    this.do_idash_action({
        locationName: locname,
        location: location,
        method: 'reset_student_attempts',
        success_msg: gettext('Successfully reset the attempts for user {user}'),
        error_msg: gettext('Failed to reset attempts.'),
        delete_module: false
    });
78
  };
79

80 81 82 83 84 85 86 87 88
  sdelete = function(locname, location){
    this.do_idash_action({
        locationName: locname,
        location: location,
        method: 'reset_student_attempts',
        success_msg: gettext('Successfully deleted student state for user {user}'),
        error_msg: gettext('Failed to delete student state.'),
        delete_module: true
    });
89
  };
90

91 92 93 94 95 96 97 98 99
  rescore = function(locname, location){
    this.do_idash_action({
        locationName: locname,
        location: location,
        method: 'rescore_problem',
        success_msg: gettext('Successfully rescored problem for user {user}'),
        error_msg: gettext('Failed to rescore problem.'),
        delete_module: false
    });
100
  };
101

Carson Gee committed
102
  return {
103 104 105 106
      reset: reset,
      sdelete: sdelete,
      rescore: rescore,
      do_idash_action: do_idash_action,
Carson Gee committed
107 108
      get_current_url: get_current_url,
      get_url: get_url,
109 110
      get_user: get_user,
      sanitized_string:sanitized_string
111
  }; })();
Carson Gee committed
112 113 114

// Register click handlers
$(document).ready(function() {
115

116
    var $courseContent = $('.course-content');
117 118 119 120 121 122 123 124 125 126
    $courseContent.on("click", '.staff-debug-reset', function() {
        StaffDebug.reset($(this).parent().data('location-name'), $(this).parent().data('location'));
        return false;
    });
    $courseContent.on("click", '.staff-debug-sdelete', function() {
        StaffDebug.sdelete($(this).parent().data('location-name'), $(this).parent().data('location'));
        return false;
    });
    $courseContent.on("click", '.staff-debug-rescore', function() {
        StaffDebug.rescore($(this).parent().data('location-name'), $(this).parent().data('location'));
127 128
        return false;
    });
Carson Gee committed
129
});