/** The feedback creation methods for StudentsManager. */
Ext.define('devilry.extjshelpers.studentsmanager.StudentsManagerCreateFeedback', {

    requires: [
        'devilry.gradeeditors.RestfulRegistryItem'
    ],

    /**
     * @private
     */
    loadGradeEditorConfigModel: function() {
        this.gradeeditor_config_model.load(this.assignmentid, {
            scope: this,
            success: function(record) {
                this.gradeeditor_config_recordcontainer.setRecord(record);
                this.loadRegistryItem();
            },
            failure: function() {
                // TODO: Handle errors
            }
        });
    },

    /**
     * @private
     */
    loadRegistryItem: function() {
        var registryitem_model = Ext.ModelManager.getModel('devilry.gradeeditors.RestfulRegistryItem');
        registryitem_model.load(this.gradeeditor_config_recordcontainer.record.data.gradeeditorid, {
            scope: this,
            success: function(record) {
                this.registryitem_recordcontainer.setRecord(record);
            }
        });
    },

    /**
     * @private
     */
    onLoadRegistryItem: function() {
        if(this.giveFeedbackButton.rendered) {
            this.giveFeedbackButton.getEl().unmask();
        }
    },

    /**
     * @private
     */
    onGiveFeedbackToSelected: function(button) {
        if(this.noneSelected()) {
            this.onSelectNone();
            return;
        }
        var draftEditor = Ext.create('devilry.gradeeditors.EditManyDraftEditorWindow', {
            isAdministrator: this.isAdministrator,
            gradeeditor_config: this.gradeeditor_config_recordcontainer.record.data,
            registryitem: this.registryitem_recordcontainer.record.data,
            listeners: {
                scope: this,
                createNewDraft: this.onPublishFeedback
            }
        });
        draftEditor.show();
    },


    /**
     * @private
     */
    onPublishFeedback: function(feedbackdraftModelName, draftstring) {
        //this.down('studentsmanager_studentsgrid').selModel.selectAll();
        this.down('studentsmanager_studentsgrid').performActionOnSelected({
            scope: this,
            callback: this.giveFeedbackToSelected,
            extraArgs: [feedbackdraftModelName, draftstring]
        });
    },

    /**
     * @private
     */
    giveFeedbackToSelected: function(record, index, total, feedbackdraftModelName, draftstring) {
        var msg = Ext.String.format('Setting feedback on group {0}/{1}', index, total);
        this.getEl().mask(msg);

        if(record.data.latest_delivery_id != null) {
            var draftrecord = Ext.create(feedbackdraftModelName, {
                draft: draftstring,
                published: true,
                delivery: record.data.latest_delivery_id
            });
            draftrecord.save({
                scope: this,
                failure: function() {
                    console.error('Failed to save a draft');
                    console.error(draftrecord);
                }
            });
        }

        if(index == total) {
            this.loadFirstPage();
            this.getEl().unmask();
        }
    }
});

@@ -398,33 +398,39 @@
 ccess: function(
-r
+configR
 ecord) %7B%0A       
@@ -480,33 +480,39 @@
 ainer.setRecord(
-r
+configR
 ecord);%0A        
@@ -991,24 +991,36 @@
  function(re
+gistryItemRe
 cord) %7B%0A    
@@ -1077,16 +1077,28 @@
 ecord(re
+gistryItemRe
 cord);%0A 
@@ -2139,24 +2139,128 @@
 electAll();%0A
+        this.progressWindow.start('Give feedback to many');%0A        this._finishedSavingGroupCount = 0;%0A
         this
@@ -2549,17 +2549,32 @@
 unction(
-r
+assignmentGroupR
 ecord, i
@@ -2584,16 +2584,30 @@
 x, total
+SelectedGroups
 , feedba
@@ -2724,16 +2724,30 @@
 x, total
+SelectedGroups
 );%0A     
@@ -2785,17 +2785,32 @@
      if(
-r
+assignmentGroupR
 ecord.da
@@ -2835,16 +2835,282 @@
 _id 
-!
+=
 = null)
+  %7B%0A            this.progressWindow.addWarning(assignmentGroupRecord, 'Has no deliveries, and therefore we can not add any feedback.');%0A            this._finishedSavingGroupCount ++;%0A            this.checkIfFinishedGivingFeedback(totalSelectedGroups);%0A        %7D else
  %7B%0A 
@@ -3270,17 +3270,32 @@
 livery: 
-r
+assignmentGroupR
 ecord.da
@@ -3400,39 +3400,40 @@
                 
-failure
+callback
 : function() %7B%0A 
@@ -3419,32 +3419,44 @@
 lback: function(
+r, operation
 ) %7B%0A            
@@ -3467,167 +3467,696 @@
     
-console.error('Failed to save a draft');%0A                    console.error(draftrecord);%0A                %7D%0A            %7D);%0A        %7D%0A%0A        if(index == total
+if(operation.success) %7B%0A                        this.progressWindow.addSuccess(assignmentGroupRecord, 'Feedback successfully created.');%0A                    %7D else %7B%0A                        this.progressWindow.addErrorFromOperation(%0A                            assignmentGroupRecord, 'Failed to create feedback', operation%0A                        );%0A                    %7D%0A%0A                    this._finishedSavingGroupCount ++;%0A                    this.checkIfFinishedGivingFeedback(totalSelectedGroups);%0A                %7D%0A            %7D);%0A        %7D%0A    %7D,%0A%0A    checkIfFinishedGivingFeedback: function(totalSelectedGroups) %7B%0A        if(this._finishedSavingGroupCount == totalSelectedGroups
 ) %7B%0A
@@ -4216,32 +4216,74 @@
 tEl().unmask();%0A
+            this.progressWindow.finish();%0A
         %7D%0A    %7D%0A

Use MultiResultWindow.