From 63e74f14f4d49e9a340ebfbdb68ae998f55c4a3b Mon Sep 17 00:00:00 2001 From: Patrick Donelan Date: Fri, 9 Jan 2009 07:13:15 +0000 Subject: [PATCH] Added "Preview" button for survey builders --- lib/WebGUI/Asset/Wobject/Survey.pm | 48 +++++++++++++++++++ www/extras/wobject/Survey/editsurvey.js | 2 +- .../wobject/Survey/editsurvey/object.js | 17 +++++-- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/lib/WebGUI/Asset/Wobject/Survey.pm b/lib/WebGUI/Asset/Wobject/Survey.pm index 7be6c74f3..f123820cf 100644 --- a/lib/WebGUI/Asset/Wobject/Survey.pm +++ b/lib/WebGUI/Asset/Wobject/Survey.pm @@ -298,6 +298,54 @@ sub www_submitObjectEdit { return $self->www_loadSurvey( { address => \@address } ); } ## end sub www_submitObjectEdit +#------------------------------------------------------------------- +=head2 Allow survey editors to "jump to" a particular section of question in a +Survey by tricking Survey into thinking they've completed the survey up to that +point. Useful for survey builders. +Note that calling this method will delete any existing survey responses for the +current user (although only survey builders can call this method so that shouldn't be +a problem +=cut + +sub www_jumpTo { + my $self = shift; + + return $self->session->privilege->insufficient() + unless ( $self->session->user->isInGroup( $self->get('groupToEditSurvey') ) ); + + my $data = $self->session->form->paramsHashRef(); + + $self->session->log->debug("jumpTo to $data->{id}"); + + # Remove existing responses for current user + $self->session->db->write( 'delete from Survey_response where assetId = ? and userId = ?', + [ $self->getId, $self->session->user->userId() ] ); + my $responseId = $self->getResponseId(); + + $self->loadBothJSON(); + + # iterate over surveyOrder looking for the jumpTo target + for my $i ( 0 .. $#{ $self->response->surveyOrder() } ) { + my $address = $self->response->surveyOrder()->[$i]; + + my @possibilities = ( + $self->survey->section($address), + $self->survey->question($address), + ); + foreach my $possibilty (@possibilities) { + if ( ref $possibilty eq 'HASH' && $possibilty->{id} eq $data->{id} ) { + $self->session->log->debug("Found jumpTo target"); + $self->response->lastResponse( $i - 1 ); + $self->saveResponseJSON(); + last; + } + } + } + $self->session->log->debug("Unable to find jumpTo target"); + + return $self->www_takeSurvey; +} + #------------------------------------------------------------------- sub copyObject { my ( $self, $address ) = @_; diff --git a/www/extras/wobject/Survey/editsurvey.js b/www/extras/wobject/Survey/editsurvey.js index a1fbcbebf..f2cac509e 100644 --- a/www/extras/wobject/Survey/editsurvey.js +++ b/www/extras/wobject/Survey/editsurvey.js @@ -133,7 +133,7 @@ Survey.OnLoad = function() { initHandler: function(){ new YAHOO.util.DDTarget("sections","sections"); Survey.Comm.loadSurvey(); - }, + } } }(); diff --git a/www/extras/wobject/Survey/editsurvey/object.js b/www/extras/wobject/Survey/editsurvey/object.js index 43fc3f4b7..e356c2326 100644 --- a/www/extras/wobject/Survey/editsurvey/object.js +++ b/www/extras/wobject/Survey/editsurvey/object.js @@ -21,8 +21,14 @@ Survey.ObjectTemplate = new function(){ { text:"Cancel", handler:function(){this.cancel();}}, { text:"Delete", handler:function(){document.getElementById('delete').value = 1; this.submit();}} ]; + if (type !== 'answer') { + butts.push({ + text: "Preview", + handler: jumpTo + }); + } - var form = new YAHOO.widget.Dialog(type, + var dialog = new YAHOO.widget.Dialog(type, { width : "600px", context: [document.body, 'tr', 'tr'], @@ -31,8 +37,11 @@ Survey.ObjectTemplate = new function(){ buttons : butts } ); - form.callback = Survey.Comm.callback; - form.render(); + dialog.callback = Survey.Comm.callback; + dialog.render(); + function jumpTo() { + window.location.search = 'func=jumpTo;id=' + dialog.form.id.value; + } var textareaId = type+'Text'; var textarea = YAHOO.util.Dom.get(textareaId); @@ -52,7 +61,7 @@ Survey.ObjectTemplate = new function(){ } myTextarea.render(); - form.show(); + dialog.show(); } }();