Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add link to research survey #6

Merged
merged 4 commits into from
Apr 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions assets/javascripts/survey-link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
(function() {

// Percentage of users to display the survey for
var percentage = 0.6,
cookie_time = null,
cookie_array = document.cookie.split(';'),
i, cookie, time, site_time, site_registered, link, data, query_string;

for(i=0;i < cookie_array.length;i++) {
cookie = cookie_array[i];
cookie = cookie.replace(/^\s+/, '')
if (cookie.indexOf('ms_srv_t=') == 0) {
// Cookie for time...
cookie_time = cookie.substring('ms_srv_t='.length,cookie.length);
}
if (cookie.indexOf('ms_srv_r=') == 0) {
// Cookie for referrer...
cookie_referrer = cookie.substring('ms_srv_r='.length,cookie.length);
}
}

if (cookie_time == null) {
// No cookie!
if (Math.random() < percentage) {
// Chosen to survey
// Set the cookie to current timestamp
time = Math.round(new Date().getTime() / 1000);
document.cookie = 'ms_srv_t='+time+'; path=/';
document.cookie = 'ms_srv_r='+document.referrer+'; path=/';
cookie_time = time;
cookie_referrer = document.referrer;

} else {
// Not chosen to survey
// Set cookie to X
document.cookie = 'ms_srv_t=X; path=/';
cookie_time = 'X'
}
}

// Only bother to do this if the cookie is set and the link exists
if (cookie_time != 'X' && document.getElementById('ms_srv_wrapper')) {

// Find the time on site thus far
site_time = Math.round(new Date().getTime() / 1000) - cookie_time;

// Find the URL on the page
link = document.getElementById('ms_srv_link');

// Is the user signed in?
site_registered = !!document.getElementById("logged_in_bar");

data = {
'ms_time': site_time,
'ms_referrer': cookie_referrer || null,
'ms_registered': site_registered,
'ms_transaction': link.getAttribute("data-transaction")
}

// Assemble the query string
query_string = [];
for (d in data) {
query_string.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
}

// Append query string to the URL
link.href = link.href + '?' + query_string.join('&');

// Show the survey link element
document.getElementById('ms_srv_wrapper').style.display = '';

// Bind a click event to the link so we can de-survey the user
link.onclick = function() { document.cookie = 'ms_srv_t=X; path=/'; }

}

})();
1 change: 1 addition & 0 deletions lib/alavetelitheme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ def set_view_paths
end

Rails.application.config.assets.precompile.push 'asktheeu.css'
Rails.application.config.assets.precompile.push 'survey-link.js'
132 changes: 132 additions & 0 deletions lib/controller_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,136 @@ def right_to_know
render :template => "help/right_to_know"
end
end

RequestController.class_eval do
# Page new form posts to
def new
# All new requests are of normal_sort
if !params[:outgoing_message].nil?
params[:outgoing_message][:what_doing] = 'normal_sort'
end

# If we've just got here (so no writing to lose), and we're already
# logged in, force the user to describe any undescribed requests. Allow
# margin of 1 undescribed so it isn't too annoying - the function
# get_undescribed_requests also allows one day since the response
# arrived.
if [email protected]? && params[:submitted_new_request].nil? && [email protected]_leave_requests_undescribed?
@undescribed_requests = @user.get_undescribed_requests
if @undescribed_requests.size > 1
render :action => 'new_please_describe'
return
end
end

# Banned from making new requests?
user_exceeded_limit = false
if !authenticated_user.nil? && !authenticated_user.can_file_requests?
# If the reason the user cannot make new requests is that they are
# rate-limited, it’s possible they composed a request before they
# logged in and we want to include the text of the request so they
# can squirrel it away for tomorrow, so we detect this later after
# we have constructed the InfoRequest.
user_exceeded_limit = authenticated_user.exceeded_limit?
if !user_exceeded_limit
@details = authenticated_user.can_fail_html
render :template => 'user/banned'
return
end
# User did exceed limit
@next_request_permitted_at = authenticated_user.next_request_permitted_at
end

# First time we get to the page, just display it
if params[:submitted_new_request].nil? || params[:reedit]
if user_exceeded_limit
render :template => 'user/rate_limited'
return
end
return render_new_compose(batch=false)
end

# Check we have :public_body_id - spammers seem to be using :public_body
# erroneously instead
if params[:info_request][:public_body_id].blank?
redirect_to frontpage_path and return
end

# See if the exact same request has already been submitted
# TODO: this check should theoretically be a validation rule in the
# model, except we really want to pass @existing_request to the view so
# it can link to it.
@existing_request = InfoRequest.find_existing(params[:info_request][:title], params[:info_request][:public_body_id], params[:outgoing_message][:body])

# Create both FOI request and the first request message
@info_request = InfoRequest.create_from_attributes(params[:info_request],
params[:outgoing_message])
@outgoing_message = @info_request.outgoing_messages.first

# Maybe we lost the address while they're writing it
if !@info_request.public_body.is_requestable?
render :action => 'new_' + @info_request.public_body.not_requestable_reason
return
end

# See if values were valid or not
if !@existing_request.nil? || !@info_request.valid?
# We don't want the error "Outgoing messages is invalid", as in this
# case the list of errors will also contain a more specific error
# describing the reason it is invalid.
@info_request.errors.delete(:outgoing_messages)

render :action => 'new'
return
end

# Show preview page, if it is a preview
if params[:preview].to_i == 1
return render_new_preview
end

if user_exceeded_limit
render :template => 'user/rate_limited'
return
end

if !authenticated?(
:web => _("To send your FOI request").to_str,
:email => _("Then your FOI request to {{public_body_name}} will be sent.",:public_body_name=>@info_request.public_body.name),
:email_subject => _("Confirm your FOI request to {{public_body_name}}",:public_body_name=>@info_request.public_body.name)
)
# do nothing - as "authenticated?" has done the redirect to signin page for us
return
end

if params[:post_redirect_user]
# If an admin has clicked the confirmation link on a users behalf,
# we don’t want to reassign the request to the administrator.
@info_request.user = params[:post_redirect_user]
else
@info_request.user = authenticated_user
end
# This automatically saves dependent objects, such as @outgoing_message, in the same transaction
@info_request.save!
# TODO: send_message needs the database id, so we send after saving, which isn't ideal if the request broke here.
@outgoing_message.send_message
flash[:request_sent] = true
flash[:notice] = _("<p>Your {{law_used_full}} request has been <strong>sent on its way</strong>!</p>
<p><strong>We will email you</strong> when there is a response, or after {{late_number_of_days}} working days if the authority still hasn't
replied by then.</p>
<p>If you write about this request (for example in a forum or a blog) please link to this page, and add an
annotation below telling people about your writing.</p>",:law_used_full=>@info_request.law_used_full,
:late_number_of_days => AlaveteliConfiguration::reply_late_after_days)

flash[:notice] = flash[:notice] += _(%Q(
<div id="ms_srv_wrapper" style="display:none;">
<p><a href="https://www.surveygizmo.co.uk/s3/1979939/2014-Baselining-AskTheEU" id="ms_srv_link" data-transaction="report">
We're running a short survey to help us understand how well AskTheEU works. If you'd like to take it, click here.
</a></p>
</div>))

redirect_to show_new_request_path(:url_title => @info_request.url_title)
end
end

end
1 change: 1 addition & 0 deletions lib/views/general/_before_body_end.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= javascript_include_tag 'survey-link.js' %>
5 changes: 4 additions & 1 deletion locale-theme/de/app.po
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ msgid "Help Desk"
msgstr "Helpdesk"

msgid "This website is powered by <a href='http://www.alaveteli.org/'>Alaveteli</a>. Other original content was developed by Access Info Europe and is made public under a <a href='http://creativecommons.org/licenses/by-sa/3.0/'>Creative Commons Attribution, Share Alike licence</a>."
msgstr "Diese Seite wurde durch <a href='http://www.alaveteli.org/'>Alaveteli</a> ermöglicht. Weitere Inhalte wurden von Access Info Europe entwickelt und unter der <a href='http://creativecommons.org/licenses/by-sa/3.0/'>Creative Commons Attribution, Share Alike lizenziert</a>."
msgstr "Diese Seite wurde durch <a href='http://www.alaveteli.org/'>Alaveteli</a> ermöglicht. Weitere Inhalte wurden von Access Info Europe entwickelt und unter der <a href='http://creativecommons.org/licenses/by-sa/3.0/'>Creative Commons Attribution, Share Alike lizenziert</a>."

msgid "\\n <div id=\"ms_srv_wrapper\" style=\"display:none;\">\\n <p><a href=\"https://www.surveygizmo.co.uk/s3/1979939/2014-Baselining-AskTheEU\" id=\"ms_srv_link\" data-transaction=\"report\">\\n We're running a short survey to help us understand how well AskTheEU works. If you'd like to take it, click here.\\n </a></p>\\n </div>"
msgstr "\\n <div id=\"ms_srv_wrapper\" style=\"display:none;\">\\n <p><a href=\"https://www.surveygizmo.co.uk/s3/1979939/2014-Baselining-AskTheEU\" id=\"ms_srv_link\" data-transaction=\"report\">\\n Wir führen eine kurze Umfrage durch, die uns helfen soll zu erfahren, wie gut AskTheEU funktioniert. Wenn Sie daran teilnehmen möchten, klicken Sie hier\\n </a></p>\\n </div>"
5 changes: 4 additions & 1 deletion locale-theme/es/app.po
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ msgid "View all news"
msgstr "Todas las noticias"

msgid "Follow us"
msgstr "Síguenos"
msgstr "Síguenos"

msgid "\\n <div id=\"ms_srv_wrapper\" style=\"display:none;\">\\n <p><a href=\"https://www.surveygizmo.co.uk/s3/1979939/2014-Baselining-AskTheEU\" id=\"ms_srv_link\" data-transaction=\"report\">\\n We're running a short survey to help us understand how well AskTheEU works. If you'd like to take it, click here.\\n </a></p>\\n </div>"
msgstr "\\n <div id=\"ms_srv_wrapper\" style=\"display:none;\">\\n <p><a href=\"https://www.surveygizmo.co.uk/s3/1979939/2014-Baselining-AskTheEU\" id=\"ms_srv_link\" data-transaction=\"report\">\\n Estamos llevando a cabo una breve encuesta para entender mejor cómo funciona AskTheEU. Si te gustaría participar, haz clic aquí.\\n </a></p>\\n </div>"