<commit_before># Copyright (C) 2007, 2008, 2009, 2010, 2011 The Collaborative Software Foundation
#
# This file is part of TriSano.
#
# TriSano is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the
# Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# TriSano is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with TriSano. If not, see http://www.gnu.org/licenses/agpl-3.0.txt.
require 'ostruct'

class ExtendedFormBuilder < ActionView::Helpers::FormBuilder
  include ActionView::Helpers::SanitizeHelper
  include ActionController::UrlWriter
  extend  ActionView::Helpers::SanitizeHelper::ClassMethods

  def core_text_field(attribute, options = {}, event =nil)
    core_follow_up(attribute, options, event) do |attribute, options|
      text_field(attribute, options)
    end
  end

  def core_calendar_date_select(attribute, options = {}, event =nil)
    core_follow_up(attribute, options, event) do |attribute, options|
      calendar_date_select(attribute, options)
    end
  end

  def dropdown_code_field(attribute, code_name, options ={}, html_options ={}, event =nil)
    core_follow_up(attribute, html_options, event) do |attribute, html_options|
      options[:include_blank] = true unless options[:include_blank] == false
      self.collection_select(attribute, codes(code_name.to_s), :id, :code_description, options, html_options)
    end
  end

  def core_dropdown_field(attribute, collection, value_method, text_method, options={}, html_options={}, event=nil)
    core_follow_up(attribute, html_options, event) do |attribute, html_options|
      options[:include_blank] = true unless options[:include_blank] == false
      self.collection_select(attribute, collection, value_method, text_method, options, html_options)
    end
  end

  def multi_select_code_field(attribute, code_name, options, html_options)
    html_options[:multiple] = true
    self.collection_select(attribute, codes(code_name), :id, :code_description, options, html_options)
  end

  def codes(code_name)
    @template.codes_for_select(code_name)
  end

  # TODO: refactor me!
  def dynamic_question(form_elements_cache, question_element, event, index, html_options = {})
    id = html_options[:id]
    result = ""
    question = question_element.question

    if question.is_multi_valued?
      if form_elements_cache.children(question_element).empty?
        return ""
      else
        value_set = form_elements_cache.children_by_type("ValueSetElement", question_element).first
        if (value_set.nil? || form_elements_cache.children(value_set).empty?)
          return ""
        end
      end
    end

    index = @object.id.nil? ? index : @object.id
    html_options[:index] = index

    follow_ups = form_elements_cache.children_by_type("FollowUpElement", question_element)

    if(follow_ups.size > 0)
      conditions = []
      follow_ups.each { |follow_up| conditions << "#{follow_up.condition},#{follow_up.id}"}
      conditions = conditions.join(",")
      text_answer_event = "sendConditionRequest('#{process_condition_path}', this, '#{event.id}', '#{question_element.id}');"
      select_answer_event = "sendConditionRequest('#{process_condition_path}', this, '#{event.id}', '#{question_element.id}');"
    end

    cdc_attributes = []
    codes = []
    input_element = case question.data_type
    when :single_line_text
      unless (question.size.nil?)
        html_options[:maxlength] = question.size
        html_options[:size] = question.size
      end
      html_options[:onchange] = text_answer_event if follow_ups
      text_field(:text_answer, html_options)
    when :multi_line_text
      html_options[:rows] = 3
      html_options[:onchange] = text_answer_event if follow_ups
      text_area(:text_answer, html_options)
    when :drop_down
      field_name = @object_name
      field_index = @object.new_record? ? "" : index.to_s
      field_id = @object.new_record? ? question.id.to_s : index.to_s

      select_list = ""
      selected_code = ""

      html_options[:onclick] = select_answer_event if follow_ups
      select_values = []
      get_values(form_elements_cache, question_element).each do |value_hash|
        codes << {:value => value_hash[:value], :code => value_hash[:code]}
        if @object.text_answer
          selected_code = @object.text_answer == value_hash[:value] ? value_hash[:code] : "" unless !selected_code.blank?
        end<commit_msg>Fix iPad support for follow up drop downs<commit_after># Copyright (C) 2007, 2008, 2009, 2010, 2011 The Collaborative Software Foundation
#
# This file is part of TriSano.
#
# TriSano is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the
# Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# TriSano is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with TriSano. If not, see http://www.gnu.org/licenses/agpl-3.0.txt.
require 'ostruct'

class ExtendedFormBuilder < ActionView::Helpers::FormBuilder
  include ActionView::Helpers::SanitizeHelper
  include ActionController::UrlWriter
  extend  ActionView::Helpers::SanitizeHelper::ClassMethods

  def core_text_field(attribute, options = {}, event =nil)
    core_follow_up(attribute, options, event) do |attribute, options|
      text_field(attribute, options)
    end
  end

  def core_calendar_date_select(attribute, options = {}, event =nil)
    core_follow_up(attribute, options, event) do |attribute, options|
      calendar_date_select(attribute, options)
    end
  end

  def dropdown_code_field(attribute, code_name, options ={}, html_options ={}, event =nil)
    core_follow_up(attribute, html_options, event) do |attribute, html_options|
      options[:include_blank] = true unless options[:include_blank] == false
      self.collection_select(attribute, codes(code_name.to_s), :id, :code_description, options, html_options)
    end
  end

  def core_dropdown_field(attribute, collection, value_method, text_method, options={}, html_options={}, event=nil)
    core_follow_up(attribute, html_options, event) do |attribute, html_options|
      options[:include_blank] = true unless options[:include_blank] == false
      self.collection_select(attribute, collection, value_method, text_method, options, html_options)
    end
  end

  def multi_select_code_field(attribute, code_name, options, html_options)
    html_options[:multiple] = true
    self.collection_select(attribute, codes(code_name), :id, :code_description, options, html_options)
  end

  def codes(code_name)
    @template.codes_for_select(code_name)
  end

  # TODO: refactor me!
  def dynamic_question(form_elements_cache, question_element, event, index, html_options = {})
    id = html_options[:id]
    result = ""
    question = question_element.question

    if question.is_multi_valued?
      if form_elements_cache.children(question_element).empty?
        return ""
      else
        value_set = form_elements_cache.children_by_type("ValueSetElement", question_element).first
        if (value_set.nil? || form_elements_cache.children(value_set).empty?)
          return ""
        end
      end
    end

    index = @object.id.nil? ? index : @object.id
    html_options[:index] = index

    follow_ups = form_elements_cache.children_by_type("FollowUpElement", question_element)

    if(follow_ups.size > 0)
      conditions = []
      follow_ups.each { |follow_up| conditions << "#{follow_up.condition},#{follow_up.id}"}
      conditions = conditions.join(",")
      text_answer_event = "sendConditionRequest('#{process_condition_path}', this, '#{event.id}', '#{question_element.id}');"
      select_answer_event = "sendConditionRequest('#{process_condition_path}', this, '#{event.id}', '#{question_element.id}');"
    end

    cdc_attributes = []
    codes = []
    input_element = case question.data_type
    when :single_line_text
      unless (question.size.nil?)
        html_options[:maxlength] = question.size
        html_options[:size] = question.size
      end
      html_options[:onchange] = text_answer_event if follow_ups
      text_field(:text_answer, html_options)
    when :multi_line_text
      html_options[:rows] = 3
      html_options[:onchange] = text_answer_event if follow_ups
      text_area(:text_answer, html_options)
    when :drop_down
      field_name = @object_name
      field_index = @object.new_record? ? "" : index.to_s
      field_id = @object.new_record? ? question.id.to_s : index.to_s

      select_list = ""
      selected_code = ""

      html_options[:onchange] = select_answer_event if follow_ups
      select_values = []
      get_values(form_elements_cache, question_element).each do |value_hash|
        codes << {:value => value_hash[:value], :code => value_hash[:code]}
        if @object.text_answer
          selected_code = @object.text_answer == value_hash[:value] ? value_hash[:code] : "" unless !selected_code.blank?
        end
{'fraction_non_alphanumeric': 0.10198771984597772, 'fraction_numerical': 0.005203455094182537, 'mean_word_length': 4.20021645021645, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6429151', 'n_tokens_mistral': 2869, 'n_tokens_neox': 2807, 'n_words': 714}