<commit_before><script type="text/javascript">
    function addQuestionnaireTableRow(questionnaire_type,round_no,questionnaire, assignment_questionnaire, questionnaire_options) {
        var questionnaire_table = jQuery('#assignment_questionnaire_table>tbody>tr');
        var row_id = 'questionnaire_table_' + questionnaire_type;
        var i = 0;
        var html;

        html += '<tr id="' + row_id + '">' +
        '<input name="assignment_form[assignment_questionnaire][][assignment_id]" type="hidden" value="';
        html += '<%= @assignment_form.assignment.id %>';
	      html += '">';


        //Rubric name
        if (round_no ==null) {
            html += '<td><label for="questionnaire_id">' + questionnaire.display_type + ':</label></td>';
        }
        else {
            html += '<td><label for="questionnaire_id">' + questionnaire.display_type + ' Round '+ round_no + ':</label></td>';
        }
        //Rubric options
        html +=     '<td align="center">' +
        '<select id="questionnaire_id" name="assignment_form[assignment_questionnaire][][questionnaire_id]" style="width:300px" class="form-control">' +
        '<option value="">--None--</option>';
        for (i = 0; i < questionnaire_options.length; i++) {
            html += '<option value="' + questionnaire_options[i][1] + '">' + questionnaire_options[i][0] + '</option>';
        }
        html += '</select></td>';
        //Use dropdown instead
        html += '<td align="center"><input type="checkbox" name="dropdown" id="dropdown" value="true"></td>';
        //Scored question display type (Dropdown or Scale)
        html += '<td align="center"><select id="scored_question_display_type", name="assignment_form[assignment_questionnaire][][dropdown]" class="form-control">';
          if (assignment_questionnaire.dropdown == true) {
            html += '<option value=true selected="selected">Dropdown</option>';
            html += '<option value=false>Scale</option>';
          } else {
            html += '<option value=true>Dropdown</option>';
            html += '<option value=false selected="selected">Scale</option>';
          }
        html += '</select></td>';
        //Weight
        html += '<td align="center"><input name="assignment_form[assignment_questionnaire][][questionnaire_weight]" style="width:40px" type="text" value="' + assignment_questionnaire.questionnaire_weight + '">% </td>' +
        '<td align="center"><input name="assignment_form[assignment_questionnaire][][notification_limit]" style="width:40px" type="text" value="' + assignment_questionnaire.notification_limit + '">% </td>';
        //Notification Limit 
        // if (round_no ==null) {
        //     html += '<td align="center"><input name="assignment_form[assignment_questionnaire][][used_in_round]" style="width:30px" type="hidden" value=" "' + '"> </td>';
        // }
        // else{
        //     html += '<td align="center"><input name="assignment_form[assignment_questionnaire][][used_in_round]" style="width:30px" type="hidden" value="' + round_no + '"> </td>';
        // }
        //insert menu for adding & selecting tag prompts
        <%if @assignment_form.assignment.is_answer_tagging_allowed %>
        tag_dropdown_index = 0
        placeholder_id = 'tag_prompt_placeholder_' + questionnaire.id
        deleted_input_id = getIdPrefix(questionnaire.id) + '[deleted][]'
        html += "<tr>"
        html += "<td>Add tag prompts</td>"
        html += "<td colspan='6'><div id='" + placeholder_id + "'></div>"
        html += "<button type='button' class='btn btn-default' onClick='addTagPromptDropdown(\"" + placeholder_id + "\", undefined, " + questionnaire.id + ", " + null + ", " + 0 + ", " + 0 +")'>+Tag prompt+</button>"
        html += "<button type='button' class='btn btn-default' onClick='removeLastTagPromptDropdown(\"" + placeholder_id + "\",\"" + deleted_input_id + "\")'>-Tag prompt-</button>"
        html += "</td></tr>"
        <% end %>
        questionnaire_table.last().after(html);
        jQuery('#questionnaire_id').val(questionnaire.id).attr('id', '');
    }
    // start used for tag prompt
    var tag_prompts = null
    var q_types = null
    function getIdPrefix(qs_id){
      prefix = 'assignment_form[tag_prompt_deployments]['
      if(qs_id!=null)
        return prefix + qs_id + ']'
      else
        return prefix + ']'
    }
    async function addTagPromptDropdown(placeholder_id, tag_dep_id, questionnaire_id, tag_prompt_id, question_type, text_len){
      $( "#" + placeholder_id ).append("<div id='loading_image'><img src='/assets/loading.gif' /></div>")
      q_types_filled = false
      tag_prompts_filled = false
      if(!q_types){
        $.ajax({
          dataType: "json",
          url: "../../questions/types",
          data: "",
          success: function(types){
            q_types = types
            q_types_filled = true
          }
        });
      }else
        q_types_filled = true
      if(!tag_prompts){
        $.ajax({
          dataType: "json",
          url: "../../tag_prompts",
          data: "",
          success: function(tp){
            tag_prompts = tp
            tag_prompts_filled = true
          }});
      }else
        tag_prompts_filled = true
      while(!(tag_prompts_filled || q_types_filled)){
        await sleep(2000);
      }
      $('#loading_image').remove()
      var id_prefix = getIdPrefix(questionnaire_id)
      var html = "<div id='container_" + id_prefix + "'>"
      html += "<input type='hidden' name='" + id_prefix + "[id][]' value='" + tag_dep_id + "'>"
      html += "Tag prompt "
      html += "<img src='/assets/info.png' title='Tag label that will be shown below the feedback' />"
      html += ": <select name='" + id_prefix + "[tag_prompt][]' onchange='if (this.selectedIndex == 1)openAddNewTagPopup()' onfocus='if (this.selectedIndex == 1)this.selectedIndex = 0' class='form-control'>"
      html += "<option value='none'>---</option>"
      html += "<option value='new'>add new..</option>"
      for (i=0; i<tag_prompts.length; i++) {
        html += "<option value=" + tag_prompts[i].id
        if (tag_prompt_id == tag_prompts[i].id)
          html += " selected "
        html += ">" + tag_prompts[i].prompt + "</option>"
      }
      html += "</select>"
      html += "&nbsp;&nbsp; apply to question type "
      html += "<img src='/assets/info.png' title='The tag prompt will only be shown below the answers of this particular question type. It is useful to exclude types of question that the tags are not relevant to' />"
      html += ": <select name='" + id_prefix + "[question_type][]' class='form-control'>"
      for (i=0; i<q_types.length; i++) {
        html += "<option value=" + q_types[i]
        if (q_types[i] == question_type)
          html += " selected "
        html += ">" + q_types[i] + "</option>"
      }
      html += "</select>"
      html += "&nbsp;&nbsp; comment length threshold "
      html += "<img src='/assets/info.png' title='This is only applicable to textual comments. The tag prompt will only be shown below textual comments whose length exceed the threshold. it is useful to exclude short comments that are not relevant' />"
      html += ": <input type='text' name='" + id_prefix + "[answer_length_threshold][]' size='4' value='" + text_len + "'/>"
      html += "</div>"
      $( "#" + placeholder_id ).append(html)
    }
    function sleep(ms) {
      return new Promise(resolve => setTimeout(resolve, ms));
    }
    function removeLastTagPromptDropdown(placeholder_id, deleted_input_id){
      //var id_prefix = 'assignment_form[tag_prompt_deployments]'
      deleted_tag_dep_id = $("#"+placeholder_id+" div:last").find('input[type="hidden"]')[0].value
      html = "<input type='hidden' name='" + deleted_input_id +"' value='"+ deleted_tag_dep_id +"' />"
      $("#" + placeholder_id + " div:last").remove()
      $("#" + placeholder_id).append(html)
    }
    function openAddNewTagPopup(){
      $.colorbox({
        iframe: true,
        href: "../../tag_prompts/view?popup=true",
        opacity: 0.8,
        innerWidth: 800,
        innerHeight: 500,
        transition:"fade",
        onClosed:function(){
          $.ajax({
            dataType: "json",
            url: "../../tag_prompts",
            data: "",
            success: function(tag_prompts){
              option = "<option value='none'>---</option><option value='new'>add new..</option>";
              //put all prompts including the newly added as options, find the highest id to be selected by default
              //This won't work if it's just an update
              last_added_id = 0;
              last_added_index = 0;
              for(i=0; i<tag_prompts.length; i++){
                if(tag_prompts[i].id>last_added_id){
                  last_added_id = tag_prompts[i].id;
                  last_added_index = i + 2;
                }
                option += "<option value='" + tag_prompts[i].id + "'";
                option += ">" + tag_prompts[i].prompt + "</option>";
              }
              //update all dropdown with the option, restore the selected option, unless for the dropdown that calls the popup
              $('select[name*="\\[tag_prompt\\]"]').each( function() {
                s = jQuery(this)
                s_value = s[0].options[s[0].selectedIndex].value;
                jQuery(this).empty().append(option);
                for(i=0; i<tag_prompts.length; i++){
                  if(s_value == "new")
                    s[0].selectedIndex = last_added_index;
                  else if(s_value == tag_prompts[i].id.toString())
                    s[0].selectedIndex = i + 2
                }
              });
            }});
        }
      });
    }
    <% unless @assignment_form.tag_prompt_deployments == nil %>
    	<% @assignment_form.tag_prompt_deployments.each do |tag_dep| %>
    		addTagPromptDropdown('tag_prompt_placeholder_' + <%= tag_dep.questionnaire_id %>, <%= tag_dep.id %>, <%= tag_dep.questionnaire_id %>, <%= tag_dep.tag_prompt_id %>, '<%= tag_dep.question_type.to_s %>', <%= tag_dep.answer_length_threshold %>)
    	<% end %>
    <%end%>
    // end tag prompt code
    function removeQuestionnaireTableRow(questionnaire_type) {
      var row_id = 'questionnaire_table_' + questionnaire_type;
      jQuery('#' + row_id).remove();
    }
    function metareview_due_date() {
      var metareview = document.getElementById("metareview_due_date_label");
        var dropdownIndex = document.getElementById('metareview_quest').selectedIndex;
        var dropdownValue = document.getElementById('metareview_quest')[dropdownIndex].value;
        if (dropdownValue != "0") {
          metareview.style.display = "";
        } else {
          metareview.style.display = "none";
        }
    }
    //zhewei: varying rubric by round still not add 'BookmarkRatingQuestionnaire' yet.
    function handleCheckReviewVary(checkvalue) {
        var state = checkvalue.checked;
        var round_count = <%= @assignment_form.assignment.rounds_of_reviews%>;
        if (state == true && round_count >1){
            //Make it display by rounds
            var element_id;
            element_id = 'questionnaire_table_' + 'ReviewQuestionnaire';
            jQuery('#' + element_id).remove();
            element_id = 'questionnaire_table_' + 'MetareviewQuestionnaire';
            jQuery('#' + element_id).remove();
            element_id = 'questionnaire_table_' + 'AuthorFeedbackQuestionnaire';
            jQuery('#' + element_id).remove();
            element_id = 'questionnaire_table_' + 'TeammateReviewQuestionnaire';
            jQuery('#' + element_id).remove();
            for (i = 1; i <= round_count; i++) {
                addQuestionnaireTableRow(
                        'ReviewQuestionnaire',
                        i,
                        <%= questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>,
                        <%= assignment_questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>,
                        <%= questionnaire_options(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>
                );
            }
            addQuestionnaireTableRow(
                    'MetareviewQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>
            );
            addQuestionnaireTableRow(
                    'AuthorFeedbackQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>
            );
            addQuestionnaireTableRow(
                    'TeammateReviewQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>
            );
            <%@avoidrepeatsign=1%>;
        }
        if (state == false && round_count>1){
            //Make it display as usual
            var element_id;
            for (i=1;i<=round_count+1;i++) {
                element_id = 'questionnaire_table_' + 'ReviewQuestionnaire';
                jQuery('#' + element_id).remove();
            }
            element_id = 'questionnaire_table_' + 'MetareviewQuestionnaire';
            jQuery('#' + element_id).remove();
            element_id = 'questionnaire_table_' + 'AuthorFeedbackQuestionnaire';
            jQuery('#' + element_id).remove();
            element_id = 'questionnaire_table_' + 'TeammateReviewQuestionnaire';
            jQuery('#' + element_id).remove();
            //And display original ones
            addQuestionnaireTableRow(
                    'ReviewQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>
            );
            addQuestionnaireTableRow(
                    'MetareviewQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>
            );
            addQuestionnaireTableRow(
                    'AuthorFeedbackQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>
            );
            addQuestionnaireTableRow(
                    'TeammateReviewQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>
            );
        }
    }
  </script>

<!--Add a review varies by round checkbox. -->
<input name="assignment_questionnaire[used_in_round]" type="hidden" value="false" />
<%= check_box_tag('assignment_questionnaire[used_in_round]', 'true', @reviewvarycheck,:onclick=>"handleCheckReviewVary(this)" ) %>
<%= label_tag('assignment_questionnaire[used_in_round]', 'Review rubric varies by round?') %>
<div>
<table class='exp table table-striped' id='assignment_questionnaire_table' style='padding:20px; font-size: 15px; width: 100%'>
  <tr class='heading'>
    <th width="10%"></th>
    <th style="text-align: center;" width="30%">Questionnaire</th>
    <th style="text-align: center;" width="15%">Use dropdown<br/>instead
      <img src="/assets/info.png" title="If instructor choose 'Use dropdown instead', a simple dropdown will be used for student to do peer review instead of the corresponding questionnare.">
    </th>
    <th style="text-align: center;" width="15%">Scored-question<br/>display style
      <img src="/assets/info.png" title="Instructor can decide for scored questions, should they be displayed as scaled questions (the scores are displayed in the next line as a set of radio buttons) or criterion questions (the scores displayed as dropdown).">
    </th>
    <th style="text-align: center;" width="15%">Weight</th>
    <th style="text-align: center;" width="15%">Notification Limit
      <img src="/assets/info.png" title="If a new review differ from existing reviews on the same artifact by more than this percentage, the instructor will be notified by e-mail.">
    </th>
  </tr>
  <script>
      jQuery(document).ready(function () {
          <% if @reviewvarycheck%>
          <% for i in 1..@assignment_form.assignment.rounds_of_reviews %>
          addQuestionnaireTableRow(
                  'ReviewQuestionnaire',
                  <%=i%>,
                  <%= questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',i).to_json.html_safe %>,
                  <%= assignment_questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',i).to_json.html_safe %>,
                  <%= questionnaire_options(@assignment_form.assignment, 'ReviewQuestionnaire',i).to_json.html_safe %>
          );
          <% end %>
          <% else %>
          addQuestionnaireTableRow(
                  'ReviewQuestionnaire',
                  null,
                  <%= questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>,
                  <%= assignment_questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>,
                  <%= questionnaire_options(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>
          );
          <% end %>
          var metareview_allowed_checkbox = jQuery('#metareviewAllowed');
          if (metareview_allowed_checkbox.is(':checked')) {
            addQuestionnaireTableRow(
                    'MetareviewQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>
            );
          }
          addQuestionnaireTableRow(
                  'AuthorFeedbackQuestionnaire',
                  null,
                  <%= questionnaire(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>,
                  <%= assignment_questionnaire(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>,
                  <%= questionnaire_options(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>
          );
          var team_assignment_checkbox = jQuery('#team_assignment');
          if (team_assignment_checkbox.is(':checked')) {
            addQuestionnaireTableRow(
                    'TeammateReviewQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>
            );
          }
          var bookmark_checkbox = jQuery('#assignment_form_assignment_use_bookmark');
          if (bookmark_checkbox.is(':checked')) {
            addQuestionnaireTableRow(
                    'BookmarkRatingQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'BookmarkRatingQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'BookmarkRatingQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'BookmarkRatingQuestionnaire',nil).to_json.html_safe %>
            );
          }
      });
  </script>
</table>
</div>
<commit_msg>fix bugs varying my rubric couldn't be saved<commit_after><script type="text/javascript">
    function addQuestionnaireTableRow(questionnaire_type,round_no,questionnaire, assignment_questionnaire, questionnaire_options) {
        var questionnaire_table = jQuery('#assignment_questionnaire_table>tbody>tr');
        var row_id = 'questionnaire_table_' + questionnaire_type;
        var i = 0;
        var html;

        html += '<tr id="' + row_id + '">' +
        '<input name="assignment_form[assignment_questionnaire][][assignment_id]" type="hidden" value="';
        html += '<%= @assignment_form.assignment.id %>';
	      html += '">';


        //Rubric name
        if (round_no ==null) {
            html += '<td><label for="questionnaire_id">' + questionnaire.display_type + ':</label></td>';
        }
        else {
            html += '<td><label for="questionnaire_id">' + questionnaire.display_type + ' Round '+ round_no + ':</label></td>';
        }
        //Rubric options
        html +=     '<td align="center">' +
        '<select id="questionnaire_id" name="assignment_form[assignment_questionnaire][][questionnaire_id]" style="width:300px" class="form-control">' +
        '<option value="">--None--</option>';
        for (i = 0; i < questionnaire_options.length; i++) {
            html += '<option value="' + questionnaire_options[i][1] + '">' + questionnaire_options[i][0] + '</option>';
        }
        html += '</select></td>';
        //Use dropdown instead
        html += '<td align="center"><input type="checkbox" name="dropdown" id="dropdown" value="true"></td>';
        //Scored question display type (Dropdown or Scale)
        html += '<td align="center"><select id="scored_question_display_type", name="assignment_form[assignment_questionnaire][][dropdown]" class="form-control">';
          if (assignment_questionnaire.dropdown == true) {
            html += '<option value=true selected="selected">Dropdown</option>';
            html += '<option value=false>Scale</option>';
          } else {
            html += '<option value=true>Dropdown</option>';
            html += '<option value=false selected="selected">Scale</option>';
          }
        html += '</select></td>';
        //Weight
        html += '<td align="center"><input name="assignment_form[assignment_questionnaire][][questionnaire_weight]" style="width:40px" type="text" value="' + assignment_questionnaire.questionnaire_weight + '">% </td>' +
        '<td align="center"><input name="assignment_form[assignment_questionnaire][][notification_limit]" style="width:40px" type="text" value="' + assignment_questionnaire.notification_limit + '">% </td>';
        //Notification Limit 
        if (round_no ==null) {
             html += '<td align="center"><input name="assignment_form[assignment_questionnaire][][used_in_round]" style="width:30px" type="hidden" value=" "' + '"> </td>';
        }
        else{
             html += '<td align="center"><input name="assignment_form[assignment_questionnaire][][used_in_round]" style="width:30px" type="hidden" value="' + round_no + '"> </td>';
        }
        //insert menu for adding & selecting tag prompts
        <%if @assignment_form.assignment.is_answer_tagging_allowed %>
        tag_dropdown_index = 0
        placeholder_id = 'tag_prompt_placeholder_' + questionnaire.id
        deleted_input_id = getIdPrefix(questionnaire.id) + '[deleted][]'
        html += "<tr>"
        html += "<td>Add tag prompts</td>"
        html += "<td colspan='6'><div id='" + placeholder_id + "'></div>"
        html += "<button type='button' class='btn btn-default' onClick='addTagPromptDropdown(\"" + placeholder_id + "\", undefined, " + questionnaire.id + ", " + null + ", " + 0 + ", " + 0 +")'>+Tag prompt+</button>"
        html += "<button type='button' class='btn btn-default' onClick='removeLastTagPromptDropdown(\"" + placeholder_id + "\",\"" + deleted_input_id + "\")'>-Tag prompt-</button>"
        html += "</td></tr>"
        <% end %>
        questionnaire_table.last().after(html);
        jQuery('#questionnaire_id').val(questionnaire.id).attr('id', '');
    }
    // start used for tag prompt
    var tag_prompts = null
    var q_types = null
    function getIdPrefix(qs_id){
      prefix = 'assignment_form[tag_prompt_deployments]['
      if(qs_id!=null)
        return prefix + qs_id + ']'
      else
        return prefix + ']'
    }
    async function addTagPromptDropdown(placeholder_id, tag_dep_id, questionnaire_id, tag_prompt_id, question_type, text_len){
      $( "#" + placeholder_id ).append("<div id='loading_image'><img src='/assets/loading.gif' /></div>")
      q_types_filled = false
      tag_prompts_filled = false
      if(!q_types){
        $.ajax({
          dataType: "json",
          url: "../../questions/types",
          data: "",
          success: function(types){
            q_types = types
            q_types_filled = true
          }
        });
      }else
        q_types_filled = true
      if(!tag_prompts){
        $.ajax({
          dataType: "json",
          url: "../../tag_prompts",
          data: "",
          success: function(tp){
            tag_prompts = tp
            tag_prompts_filled = true
          }});
      }else
        tag_prompts_filled = true
      while(!(tag_prompts_filled || q_types_filled)){
        await sleep(2000);
      }
      $('#loading_image').remove()
      var id_prefix = getIdPrefix(questionnaire_id)
      var html = "<div id='container_" + id_prefix + "'>"
      html += "<input type='hidden' name='" + id_prefix + "[id][]' value='" + tag_dep_id + "'>"
      html += "Tag prompt "
      html += "<img src='/assets/info.png' title='Tag label that will be shown below the feedback' />"
      html += ": <select name='" + id_prefix + "[tag_prompt][]' onchange='if (this.selectedIndex == 1)openAddNewTagPopup()' onfocus='if (this.selectedIndex == 1)this.selectedIndex = 0' class='form-control'>"
      html += "<option value='none'>---</option>"
      html += "<option value='new'>add new..</option>"
      for (i=0; i<tag_prompts.length; i++) {
        html += "<option value=" + tag_prompts[i].id
        if (tag_prompt_id == tag_prompts[i].id)
          html += " selected "
        html += ">" + tag_prompts[i].prompt + "</option>"
      }
      html += "</select>"
      html += "&nbsp;&nbsp; apply to question type "
      html += "<img src='/assets/info.png' title='The tag prompt will only be shown below the answers of this particular question type. It is useful to exclude types of question that the tags are not relevant to' />"
      html += ": <select name='" + id_prefix + "[question_type][]' class='form-control'>"
      for (i=0; i<q_types.length; i++) {
        html += "<option value=" + q_types[i]
        if (q_types[i] == question_type)
          html += " selected "
        html += ">" + q_types[i] + "</option>"
      }
      html += "</select>"
      html += "&nbsp;&nbsp; comment length threshold "
      html += "<img src='/assets/info.png' title='This is only applicable to textual comments. The tag prompt will only be shown below textual comments whose length exceed the threshold. it is useful to exclude short comments that are not relevant' />"
      html += ": <input type='text' name='" + id_prefix + "[answer_length_threshold][]' size='4' value='" + text_len + "'/>"
      html += "</div>"
      $( "#" + placeholder_id ).append(html)
    }
    function sleep(ms) {
      return new Promise(resolve => setTimeout(resolve, ms));
    }
    function removeLastTagPromptDropdown(placeholder_id, deleted_input_id){
      //var id_prefix = 'assignment_form[tag_prompt_deployments]'
      deleted_tag_dep_id = $("#"+placeholder_id+" div:last").find('input[type="hidden"]')[0].value
      html = "<input type='hidden' name='" + deleted_input_id +"' value='"+ deleted_tag_dep_id +"' />"
      $("#" + placeholder_id + " div:last").remove()
      $("#" + placeholder_id).append(html)
    }
    function openAddNewTagPopup(){
      $.colorbox({
        iframe: true,
        href: "../../tag_prompts/view?popup=true",
        opacity: 0.8,
        innerWidth: 800,
        innerHeight: 500,
        transition:"fade",
        onClosed:function(){
          $.ajax({
            dataType: "json",
            url: "../../tag_prompts",
            data: "",
            success: function(tag_prompts){
              option = "<option value='none'>---</option><option value='new'>add new..</option>";
              //put all prompts including the newly added as options, find the highest id to be selected by default
              //This won't work if it's just an update
              last_added_id = 0;
              last_added_index = 0;
              for(i=0; i<tag_prompts.length; i++){
                if(tag_prompts[i].id>last_added_id){
                  last_added_id = tag_prompts[i].id;
                  last_added_index = i + 2;
                }
                option += "<option value='" + tag_prompts[i].id + "'";
                option += ">" + tag_prompts[i].prompt + "</option>";
              }
              //update all dropdown with the option, restore the selected option, unless for the dropdown that calls the popup
              $('select[name*="\\[tag_prompt\\]"]').each( function() {
                s = jQuery(this)
                s_value = s[0].options[s[0].selectedIndex].value;
                jQuery(this).empty().append(option);
                for(i=0; i<tag_prompts.length; i++){
                  if(s_value == "new")
                    s[0].selectedIndex = last_added_index;
                  else if(s_value == tag_prompts[i].id.toString())
                    s[0].selectedIndex = i + 2
                }
              });
            }});
        }
      });
    }
    <% unless @assignment_form.tag_prompt_deployments == nil %>
    	<% @assignment_form.tag_prompt_deployments.each do |tag_dep| %>
    		addTagPromptDropdown('tag_prompt_placeholder_' + <%= tag_dep.questionnaire_id %>, <%= tag_dep.id %>, <%= tag_dep.questionnaire_id %>, <%= tag_dep.tag_prompt_id %>, '<%= tag_dep.question_type.to_s %>', <%= tag_dep.answer_length_threshold %>)
    	<% end %>
    <%end%>
    // end tag prompt code
    function removeQuestionnaireTableRow(questionnaire_type) {
      var row_id = 'questionnaire_table_' + questionnaire_type;
      jQuery('#' + row_id).remove();
    }
    function metareview_due_date() {
      var metareview = document.getElementById("metareview_due_date_label");
        var dropdownIndex = document.getElementById('metareview_quest').selectedIndex;
        var dropdownValue = document.getElementById('metareview_quest')[dropdownIndex].value;
        if (dropdownValue != "0") {
          metareview.style.display = "";
        } else {
          metareview.style.display = "none";
        }
    }
    //zhewei: varying rubric by round still not add 'BookmarkRatingQuestionnaire' yet.
    function handleCheckReviewVary(checkvalue) {
        var state = checkvalue.checked;
        var round_count = <%= @assignment_form.assignment.rounds_of_reviews%>;
        if (state == true && round_count >1){
            //Make it display by rounds
            var element_id;
            element_id = 'questionnaire_table_' + 'ReviewQuestionnaire';
            jQuery('#' + element_id).remove();
            element_id = 'questionnaire_table_' + 'MetareviewQuestionnaire';
            jQuery('#' + element_id).remove();
            element_id = 'questionnaire_table_' + 'AuthorFeedbackQuestionnaire';
            jQuery('#' + element_id).remove();
            element_id = 'questionnaire_table_' + 'TeammateReviewQuestionnaire';
            jQuery('#' + element_id).remove();
            for (i = 1; i <= round_count; i++) {
                addQuestionnaireTableRow(
                        'ReviewQuestionnaire',
                        i,
                        <%= questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>,
                        <%= assignment_questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>,
                        <%= questionnaire_options(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>
                );
            }
            addQuestionnaireTableRow(
                    'MetareviewQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>
            );
            addQuestionnaireTableRow(
                    'AuthorFeedbackQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>
            );
            addQuestionnaireTableRow(
                    'TeammateReviewQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>
            );
            <%@avoidrepeatsign=1%>;
        }
        if (state == false && round_count>1){
            //Make it display as usual
            var element_id;
            for (i=1;i<=round_count+1;i++) {
                element_id = 'questionnaire_table_' + 'ReviewQuestionnaire';
                jQuery('#' + element_id).remove();
            }
            element_id = 'questionnaire_table_' + 'MetareviewQuestionnaire';
            jQuery('#' + element_id).remove();
            element_id = 'questionnaire_table_' + 'AuthorFeedbackQuestionnaire';
            jQuery('#' + element_id).remove();
            element_id = 'questionnaire_table_' + 'TeammateReviewQuestionnaire';
            jQuery('#' + element_id).remove();
            //And display original ones
            addQuestionnaireTableRow(
                    'ReviewQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>
            );
            addQuestionnaireTableRow(
                    'MetareviewQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>
            );
            addQuestionnaireTableRow(
                    'AuthorFeedbackQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>
            );
            addQuestionnaireTableRow(
                    'TeammateReviewQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>
            );
        }
    }
  </script>

<!--Add a review varies by round checkbox. -->
<input name="assignment_questionnaire[used_in_round]" type="hidden" value="false" />
<%= check_box_tag('assignment_questionnaire[used_in_round]', 'true', @reviewvarycheck,:onclick=>"handleCheckReviewVary(this)" ) %>
<%= label_tag('assignment_questionnaire[used_in_round]', 'Review rubric varies by round?') %>
<div>
<table class='exp table table-striped' id='assignment_questionnaire_table' style='padding:20px; font-size: 15px; width: 100%'>
  <tr class='heading'>
    <th width="10%"></th>
    <th style="text-align: center;" width="30%">Questionnaire</th>
    <th style="text-align: center;" width="15%">Use dropdown<br/>instead
      <img src="/assets/info.png" title="If instructor choose 'Use dropdown instead', a simple dropdown will be used for student to do peer review instead of the corresponding questionnare.">
    </th>
    <th style="text-align: center;" width="15%">Scored-question<br/>display style
      <img src="/assets/info.png" title="Instructor can decide for scored questions, should they be displayed as scaled questions (the scores are displayed in the next line as a set of radio buttons) or criterion questions (the scores displayed as dropdown).">
    </th>
    <th style="text-align: center;" width="15%">Weight</th>
    <th style="text-align: center;" width="15%">Notification Limit
      <img src="/assets/info.png" title="If a new review differ from existing reviews on the same artifact by more than this percentage, the instructor will be notified by e-mail.">
    </th>
  </tr>
  <script>
      jQuery(document).ready(function () {
          <% if @reviewvarycheck%>
          <% for i in 1..@assignment_form.assignment.rounds_of_reviews %>
          addQuestionnaireTableRow(
                  'ReviewQuestionnaire',
                  <%=i%>,
                  <%= questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',i).to_json.html_safe %>,
                  <%= assignment_questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',i).to_json.html_safe %>,
                  <%= questionnaire_options(@assignment_form.assignment, 'ReviewQuestionnaire',i).to_json.html_safe %>
          );
          <% end %>
          <% else %>
          addQuestionnaireTableRow(
                  'ReviewQuestionnaire',
                  null,
                  <%= questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>,
                  <%= assignment_questionnaire(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>,
                  <%= questionnaire_options(@assignment_form.assignment, 'ReviewQuestionnaire',nil).to_json.html_safe %>
          );
          <% end %>
          var metareview_allowed_checkbox = jQuery('#metareviewAllowed');
          if (metareview_allowed_checkbox.is(':checked')) {
            addQuestionnaireTableRow(
                    'MetareviewQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'MetareviewQuestionnaire',nil).to_json.html_safe %>
            );
          }
          addQuestionnaireTableRow(
                  'AuthorFeedbackQuestionnaire',
                  null,
                  <%= questionnaire(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>,
                  <%= assignment_questionnaire(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>,
                  <%= questionnaire_options(@assignment_form.assignment, 'AuthorFeedbackQuestionnaire',nil).to_json.html_safe %>
          );
          var team_assignment_checkbox = jQuery('#team_assignment');
          if (team_assignment_checkbox.is(':checked')) {
            addQuestionnaireTableRow(
                    'TeammateReviewQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'TeammateReviewQuestionnaire',nil).to_json.html_safe %>
            );
          }
          var bookmark_checkbox = jQuery('#assignment_form_assignment_use_bookmark');
          if (bookmark_checkbox.is(':checked')) {
            addQuestionnaireTableRow(
                    'BookmarkRatingQuestionnaire',
                    null,
                    <%= questionnaire(@assignment_form.assignment, 'BookmarkRatingQuestionnaire',nil).to_json.html_safe %>,
                    <%= assignment_questionnaire(@assignment_form.assignment, 'BookmarkRatingQuestionnaire',nil).to_json.html_safe %>,
                    <%= questionnaire_options(@assignment_form.assignment, 'BookmarkRatingQuestionnaire',nil).to_json.html_safe %>
            );
          }
      });
  </script>
</table>
</div>

{'fraction_non_alphanumeric': 0.1405796770436014, 'fraction_numerical': 0.0034259872553274104, 'mean_word_length': 2.6221045665122436, 'pattern_counts': {'":': 6, '<': 377, '<?xml version=': 0, '>': 377, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4430825', 'n_tokens_mistral': 12621, 'n_tokens_neox': 11953, 'n_words': 2127}