Last active
          August 29, 2015 14:14 
        
      - 
      
 - 
        
Save xymostech/2630e4e6ee1654a34210 to your computer and use it in GitHub Desktop.  
    Gists for uploading items
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | javascript: $.ajax({ | |
| /* Removes all the assessment items in an exercise. Set `window.exercise` to the name of | |
| * the exercise (like `"addition_1"`). */ | |
| type: "GET", | |
| url: "/api/v1/exercises/" + window.exercise, | |
| success: function(data) { | |
| var id = data.id; | |
| var sha = data.sha; | |
| console.log("Got id", id, "and sha", sha); | |
| $.ajax({ | |
| url: "/api/v1/exercises/" + id, | |
| type: "PUT", | |
| data: JSON.stringify({ | |
| live: true, | |
| problem_types: [{ | |
| items: [], | |
| name: "Type 1" | |
| }], | |
| sha: sha | |
| }), | |
| contentType: "application/json", | |
| async: false, | |
| error: function(data, status, error) { | |
| console.error("Error clearing items", data, status, error); | |
| }, | |
| complete: function(data, status) { | |
| console.log("Success!", status, data); | |
| } | |
| }); | |
| }, | |
| error: function(data, status, error) { | |
| console.log("Error getting id for exercise '"+ window.exercise + "':", data, status, error); | |
| } | |
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | javascript: $.ajax({ | |
| /* Adds a list of items to an exercise. Set `window.exercise` to the name of the exercise | |
| * (like `"addition_1"`) and `window.items` to a list of items to upload. */ | |
| type: "GET", | |
| url: "/api/v1/exercises/" + window.exercise, | |
| success: function(data) { | |
| var id = data.id; | |
| console.log("Got id", id); | |
| _.each(window.items, function(item, i) { | |
| $.ajax({ | |
| url: "/api/v1/exercises/" + id + "/assessment_item", | |
| type: "POST", | |
| data: JSON.stringify({ | |
| item_data: JSON.stringify(item), | |
| tags: [], | |
| author_names: ["Emily"] | |
| }), | |
| contentType: "application/json", | |
| async: false, | |
| error: function(data, status, error) { | |
| console.error("Error uploading item:", data, status, error); | |
| }, | |
| complete: function(data, status) { | |
| console.log(status, (i + 1) + "/" + window.items.length, data); | |
| } | |
| }); | |
| }); | |
| console.log("Done!"); | |
| }, | |
| error: function(data, status, error) { | |
| console.log("Error getting id for exercise '" + window.exercise + "'!", data, status, error); | |
| } | |
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | javascript: $.ajax({ | |
| /* Sets the assessment items in an exercise to the list of shas provided. Set `window.exercise` | |
| to the name of the exercise (like `"addition_1"`) and `window.shas` to a list of shas | |
| (like `["xa0eb24b10b654f84", "x0a0f6d88867597e3"]`) to use. */ | |
| type: "GET", | |
| url: "/api/v1/exercises/" + window.exercise, | |
| success: function(data) { | |
| var id = data.id; | |
| var sha = data.sha; | |
| console.log("Got id", id, "and sha", sha); | |
| var items = []; | |
| _.each(window.shas, function(item_id) { | |
| $.ajax({ | |
| url: "/api/internal/assessment_items/" + item_id, | |
| type: "GET", | |
| async: false, | |
| success: function(item_data) { | |
| console.log("Found item with id:", item_id); | |
| items.push({ | |
| id: item_data.id, | |
| sha: item_data.sha, | |
| live: true | |
| }); | |
| }, | |
| error: function(data, status, error) { | |
| console.log("Couldn't find item with id:", item_id, data, status, error); | |
| } | |
| }); | |
| }); | |
| $.ajax({ | |
| url: "/api/v1/exercises/" + id, | |
| type: "PUT", | |
| data: JSON.stringify({ | |
| sha: sha, | |
| live: true, | |
| problem_types: [{ | |
| items: items, | |
| name: "Type 1" | |
| }] | |
| }), | |
| contentType: "application/json", | |
| complete: function(data, status) { | |
| console.log("Success!"); | |
| }, | |
| error: function(data, status, error) { | |
| console.error("Error setting problem_types:", data, status, error); | |
| } | |
| }); | |
| }, | |
| error: function(data, status, error) { | |
| console.log("Error getting id for exercise '"+ window.exercise + "':", data, status, error); | |
| } | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment