<commit_before># Methods added to this helper will be available to all templates in the application.

require_dependency File.join(Rails.root, 'vendor', 'plugins', 'annotations', 'lib', 'app', 'helpers', 'application_helper')

module ApplicationHelper
  #savage_beast
  include SavageBeast::ApplicationHelper
  
  include TagsHelper
  
  # ========================================
  # Code to help with remembering which tab
  # the user was in after redirects etc.
  # ----------------------------------------

  # Note: the implementation of this method means that when it is used
  # it also resets the param to "false", thus the remembering of a tab is
  # only done in the one (current) request.
  # If more control than that is required (ie: being able to configure how long tab is remembered for),
  # then split into 2 different methods.
  # def get_and_reset_use_tab_cookie_param_value
  #   #logger.info ""
  #   #logger.info "get_and_reset_use_tab_cookie_param_value called; before - session[:use_tab_cookie] = #{session[:use_tab_cookie]}"
  #   #logger.info ""
  # 
  #   value = session[:use_tab_cookie]
  #   value = value.blank? ? false : value
  # 
  #   session[:use_tab_cookie] = false
  # 
  #   #logger.info ""
  #   #logger.info "get_and_reset_use_tab_cookie_param_value called; after - session[:use_tab_cookie] = #{session[:use_tab_cookie]}"
  #   #logger.info ""
  # 
  #   return value
  # end

  # ========================================
  
  
  #savage_beast
    def admin?
      puts "checking admin"
            return current_user.is_admin?
    end

  def reload_flash_error
    page.replace "error_flash", :partial => 'layouts/flash_error'
  end

  def get_all_annotations_for_variable_with_current_user(variable)
    #    var = Variable.find(variable)
    logger.info("annotations for " + variable.to_s)
    person = Person.find(current_user.id)
    tag_array = Array.new
    person.owned_taggings.each do |tagging|
      if tagging.taggable_type== "Variable" && tagging.taggable_id == variable.id
        puts "pushing " + tagging.tag_id.to_s
        tag_array.push(tagging.tag_id)
      end
    end



    #  logger.info(person.to_s)
    #  obj = variable.annotations_with_attribute_and_by_source("tag",person)
    #  logger.info(obj.to_json)
    #    all_annotation_array = Array.new
    #    obj.each do |item|
    #      logger.info("id: " + item.id.to_s)
    #      all_annotation_array.push(Hash["name",item.value, "id",item.id])
    #    end<commit_msg>moved ukda registration check to the app helper<commit_after># Methods added to this helper will be available to all templates in the application.

require_dependency File.join(Rails.root, 'vendor', 'plugins', 'annotations', 'lib', 'app', 'helpers', 'application_helper')

module ApplicationHelper
  #savage_beast
  include SavageBeast::ApplicationHelper
  
  include TagsHelper
  
  # ========================================
  # Code to help with remembering which tab
  # the user was in after redirects etc.
  # ----------------------------------------

  # Note: the implementation of this method means that when it is used
  # it also resets the param to "false", thus the remembering of a tab is
  # only done in the one (current) request.
  # If more control than that is required (ie: being able to configure how long tab is remembered for),
  # then split into 2 different methods.
  # def get_and_reset_use_tab_cookie_param_value
  #   #logger.info ""
  #   #logger.info "get_and_reset_use_tab_cookie_param_value called; before - session[:use_tab_cookie] = #{session[:use_tab_cookie]}"
  #   #logger.info ""
  # 
  #   value = session[:use_tab_cookie]
  #   value = value.blank? ? false : value
  # 
  #   session[:use_tab_cookie] = false
  # 
  #   #logger.info ""
  #   #logger.info "get_and_reset_use_tab_cookie_param_value called; after - session[:use_tab_cookie] = #{session[:use_tab_cookie]}"
  #   #logger.info ""
  # 
  #   return value
  # end

  # ========================================
  
  #Check if someone is registered with the UKDA using their
  #web service (see UKDA_EMAIL_ADDRESS in environment_local.rb). It
  #takes a form with the persons email address and 'Login' submit button
  #params and returns some xml with a simple <registered>yes</registered>
  #or <registered>no</registered>
  def ukda_registration_check(person)
   params={'LoginName' => person.email, 'Login' => 'Login'}
   response= Net::HTTP.post_form(URI.parse(UKDA_EMAIL_ADDRESS),params)
   xml_parser = XML::Parser.string(response.body)
   xml = xml_parser.parse
   node = xml.find('child::registered')
   return node.first.content == "yes"
  end
  
  
  #savage_beast
    def admin?
      puts "checking admin"
            return current_user.is_admin?
    end
    
#if the flash is for an ajax rendered page then use this to flash 
#something
  def reload_flash_error
    page.replace "error_flash", :partial => 'layouts/flash_error'
  end

  def get_all_annotations_for_variable_with_current_user(variable)
    #    var = Variable.find(variable)
    logger.info("annotations for " + variable.to_s)
    person = Person.find(current_user.id)
    tag_array = Array.new
    person.owned_taggings.each do |tagging|
      if tagging.taggable_type== "Variable" && tagging.taggable_id == variable.id
        puts "pushing " + tagging.tag_id.to_s
        tag_array.push(tagging.tag_id)
      end
    end



    #  logger.info(person.to_s)
    #  obj = variable.annotations_with_attribute_and_by_source("tag",person)
    #  logger.info(obj.to_json)
    #    all_annotation_array = Array.new
    #    obj.each do |item|
    #      logger.info("id: " + item.id.to_s)
    #      all_annotation_array.push(Hash["name",item.value, "id",item.id])
    #    end
{'fraction_non_alphanumeric': 0.13982239247779907, 'fraction_numerical': 0.00034825004353125546, 'mean_word_length': 3.3780487804878048, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, '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': '122610', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1786, 'n_words': 451}