<commit_before>{% extends "helios/templates/cryptobase.html" %}

{% block title %}Questions for {{election.name}}{% endblock %}
{% block content %}
  <h3 class="title">{{election.name}} &mdash; Questions <span style="font-size:0.7em;">[<a href="{% url "helios.views.one_election_view" election.uuid %}">back to election</a>]</span></h3>

  <script language="javascript">
{% if election.questions %}
  QUESTIONS = JSON.parse("{{questions_json|escapejs}}");
{% else %}
  QUESTIONS = [];
{% endif %}
{% if admin_p and not election.frozen_at %}
  ADMIN_P = true;
{% else %}
  ADMIN_P = false;
{% endif %}

  CSRF_TOKEN = '{{csrf_token}}';

  function refresh() {
      $('#questions').processTemplate({'questions' : QUESTIONS, 'admin_p' : ADMIN_P}); 

      add_answers($('#answer_table'),DEFAULT_NUM_ANSWERS);
 }
  
  function save_questions(callback) {
      $.post('./save_questions', {'questions_json' : $.toJSON(QUESTIONS), 'csrf_token': CSRF_TOKEN}, function(result) {
          if (result == "FAILURE") {
            alert("The questions could not be saved. Please check that the URLs you entered are either http:// or https://");
          } else {
            callback();
          }
      });      
  }
  
  function get_question_from_form(form) {
      var max = null;
      if (form.max.value != "")
        max = parseInt(form.max.value);
        
      var new_q = {'question' : form.question.value, 'min': parseInt(form.min.value), 'max': max, 'short_name': form.question.value, 'answers': [], 'answer_urls': [], 'choice_type': form.choice_type.value, 'tally_type': 'homomorphic', 'result_type': form.result_type.value};
      var i = 0;
      while(true) {
          i += 1;
          if (!form['answer_' + i] || !form['answer_' + i].value)
            break;
          new_q.answers[i-1] = form['answer_' + i].value;
          new_q.answer_urls[i-1] = form['answer_url_' +i].value;
      }
      
     return new_q;
  }

  function question_add(form) {
      var new_q = get_question_from_form(form);
      QUESTIONS.push(new_q);
      save_questions(refresh);
  }
  
  function question_remove(q_num) {
      var new_questions = [];
      $(QUESTIONS).each(function(i, q) {
          if (i == q_num)
            return;
          new_questions.push(q);
      });
      QUESTIONS = new_questions;
      save_questions(refresh);
  }

  function question_edit(q_num) {
    $('#q_view_'+q_num).hide();
    $('#q_edit_'+q_num).show();

    // set the default values for this question
    var form = $('#question_edit_' + q_num + '_form');
    var q_data = QUESTIONS[q_num];
    form[0].min.value = q_data.min;<commit_msg>prevent adding question with no answers<commit_after>{% extends "helios/templates/cryptobase.html" %}

{% block title %}Questions for {{election.name}}{% endblock %}
{% block content %}
  <h3 class="title">{{election.name}} &mdash; Questions <span style="font-size:0.7em;">[<a href="{% url "helios.views.one_election_view" election.uuid %}">back to election</a>]</span></h3>

  <script language="javascript">
{% if election.questions %}
  QUESTIONS = JSON.parse("{{questions_json|escapejs}}");
{% else %}
  QUESTIONS = [];
{% endif %}
{% if admin_p and not election.frozen_at %}
  ADMIN_P = true;
{% else %}
  ADMIN_P = false;
{% endif %}

  CSRF_TOKEN = '{{csrf_token}}';

  function refresh() {
      $('#questions').processTemplate({'questions' : QUESTIONS, 'admin_p' : ADMIN_P}); 

      add_answers($('#answer_table'),DEFAULT_NUM_ANSWERS);
 }
  
  function save_questions(callback) {
      $.post('./save_questions', {'questions_json' : $.toJSON(QUESTIONS), 'csrf_token': CSRF_TOKEN}, function(result) {
          if (result == "FAILURE") {
            alert("The questions could not be saved. Please check that the URLs you entered are either http:// or https://");
          } else {
            callback();
          }
      });      
  }
  
  function get_question_from_form(form) {
      var max = null;
      if (form.max.value != "")
        max = parseInt(form.max.value);
        
      var new_q = {'question' : form.question.value, 'min': parseInt(form.min.value), 'max': max, 'short_name': form.question.value, 'answers': [], 'answer_urls': [], 'choice_type': form.choice_type.value, 'tally_type': 'homomorphic', 'result_type': form.result_type.value};
      var i = 0;
      while(true) {
          i += 1;
          if (!form['answer_' + i] || !form['answer_' + i].value)
            break;
          new_q.answers[i-1] = form['answer_' + i].value;
          new_q.answer_urls[i-1] = form['answer_url_' +i].value;
      }
      
      if (new_q.answers.length == 0) {
        return null;
      }
      
      return new_q;
  }

  function question_add(form) {
      var new_q = get_question_from_form(form);
      if (new_q) {
        QUESTIONS.push(new_q);
        save_questions(refresh);
      }
  }
  
  function question_remove(q_num) {
      var new_questions = [];
      $(QUESTIONS).each(function(i, q) {
          if (i == q_num)
            return;
          new_questions.push(q);
      });
      QUESTIONS = new_questions;
      save_questions(refresh);
  }

  function question_edit(q_num) {
    $('#q_view_'+q_num).hide();
    $('#q_edit_'+q_num).show();

    // set the default values for this question
    var form = $('#question_edit_' + q_num + '_form');
    var q_data = QUESTIONS[q_num];
    form[0].min.value = q_data.min;
{'fraction_non_alphanumeric': 0.1700352308548118, 'fraction_numerical': 0.0035230854811793065, 'mean_word_length': 2.8861671469740635, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 2, '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': '1546837', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1792, 'n_words': 357}