SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

Using SharePoint Surveys for a Quiz - Part 1

Our company recently had a fundraising drive for a charity.  The people on the fundraising committee came up with an idea to raise some money and have a little fun - we would have a trivia contest for employees.  You would pay a small fee to enter, with the best score winning a prize, and the proceeds would go to the charity.

I suppose there are web sites out there that can host something like this, but my immediate throught was that we could use SharePoint's survey function to do this.  And with a little customization, it can handle the job.

Some of the requirements we had are:

  • You can only take the quiz once.
  • You can't see the questions before you take the quiz.
  • You can't go back and change your answers once you submit them.
  • You only have 10 minutes to take the quiz.
  • Prevent "cheating" as much as possible.
  • Need a way to find the best score (shown in Part 2)

Some of these things are handled out-of-the-box by SharePoint.  Others require customizations.  Others can't be overcome 100% and require a little trust...

If you know of other ways to do some of these things without customizations, I would love to hear about them.

Requirement: You can only take the quiz once.

This can be handled by setting the survey option "Allow multiple responses?" to No in the survey settings in SharePoint (set it when creating the survey or later using the link "Title, description and navigation" in the Survey Settings.

Requirement: You can't see the questions before you take the quiz.

This one is somewhat difficult to overcome.  It depends how secure you want things to be and how savvy your users are.

First, in Survey Settings -> Advanced settings, set the Item-level Permissions to:

Read access: Only their own
Edit access: Only their own

This prevents non-admin users from seeing the responses from any other user who has already responded to the survey.  So they won't be able to see the questions using "Show a graphical summary of responses" or "Show all responses" when clicking on the survey.  Those two links on the survey's "overview.aspx" page will only show the user's responses, and until the user has taken the survey there will be nothing to show.

However, there is still a hole... On the "overview.aspx" page (which is the 'landing page" you see when you click on the list in "View All Site Content"), there is an option under the Actions menu for "Export to Spreadsheet".  That will create an Excel spreadsheet with the survey questions in the headings of the columns even if the user has not responded to the survey yet.  The contents of the spreadsheet will be empty, but the questions will be in the column headings. So a savvy user could use this to see the questions prior to taking the survey.

The only way I have found to avoid this is to drop the survey as a web part on a site or web part page, and set the Toolbar Type of the web part to Summary Toolbar and change the Chrome Type to None.  Then there will be no toolbar at the top with the Actions method.  You need to change the Chrome Type to None, or users will be able to click on the header of the web part and get to the survey's landing page (overview.aspx). Point users to this site/page as the method to take the survey.

This does not entirely solve the problem however. The user can still click on "View All Site Content" and get to the overview.aspx page from there by clicking on the link to it in the Surveys section on the viewlists.aspx page.

One way to solve that is to hide the "View All Site Content" link.  You can do that by dropping a Content Editor Web Part (CEWP) on the page and use the Source Editor button to put this in the web part:

<style>
.ms-quicklaunchheader { display: none }
</style>

Change the Chrome Type of the CEWP to none and save it.  The "View All Site Content" link will now be gone!  (When you need to get to it, you need to add "_layouts/viewlsts.aspx" to the site's URL.

Of course, if you have users who really know their SharePoint, they will be able to still get to the View All Site Content page (viewlsts.aspx) or the survey's overview.aspx page by finding the URL to the site or survey and changing the URL to navigate to it.

In my company's case, we do not really have any non-admin users who know SharePoint well enough to do this, so hiding the "View All Site Content" link is sufficient for us.  But if you do have advanced SharePoint users, my recommendation is to rename overview.aspx for the survey to another name, like "overview_admin.aspx" using SPD.  Then if you wish, create a new overview.aspx page with nothing on it or some kind of message for anyone navigating to it.  Then the only way a user could get to the survey's original overview.aspx page is if the user knew the new name you gave it.  (You also need to remember it because you will need it to get the survey results.)

There is yet another way the user would be able to see the answers without taking the survey, and that is to use the Respond link to take the survey and then click the Cancel button after reading the questions.  SharePoint doesn't record the response to the survey until the user clicks the Finish button.  So we need a way to disable or hide the Cancel buttons (there are two).

This can be accomplished using SPD to edit the survey's NewForm.aspx page, which is the page the user sees when they respond to a survey.  Add this Javascript to the page, just after the closing </table> tag after the closing </WebPartPages:WebPartZone> tag:

<script type="text/javascript">
function hideCancel()
{
    var e = document.getElementsByTagName("input");

    for(var i = 0; i < e.length; i++)
        if (e[i].type == 'button' && e[i].value == 'Cancel')
            e[i].style.visibility = "hidden";
}
hideCancel();
</script>

This code looks for all input tags that are buttons and have the value (button name) of "Cancel", and sets their visibility to hidden.  This will find both Cancel buttons and hide them, so the user now cannot cancel out of the survey once they start it. 

There is still one more way the user can see the questions without taking the survey - if the user clicks Respond link to take the survey, then clicks the Back button or closes the browser, SharePoint does not record that the survey was taken.  So a user could click Respond, read the questions, click Back or close the browser, do some research, then take the quiz again with their new knowledge.

This can be prevented by submitting the survey results automatically when the "onbeforeunload" event fires in Internet Explorer.  Note that this technique may not work with some browsers; some do not support this event.  But we are using IE only, so it works in our case.

By submitting the results when "onbeforeunload" fires, clicking the back button or closing the browser will be trapped and the user won't be able to take the survey again.  We explain this in the "rules of the game" - we tell them they must finish the quiz once they start it.

Trapping and submitting the results with "onbeforeunload" is tricky.  First, the survey form does not have a "submit" button, therefore the form "submit()" function cannot simply be called to submit the results.  We need to "click" the Finish button. In order to "click" the Finish button, we need to find it on the page.

Second, "onbeforeunload" will fire any time the page is left, including when the user really clicks the Finish button.  If the user navigates off the page without clicking Finish, we want to display a message to let the user know that they screwed up, but we don't want that message to display when they correctly click the Finish button.  To do this, we need to insert some Javascript code to disable the onbeforeunload event (set it to null) when the Finish button is really clicked.  And there are two Finish buttons on the page, one at the top and one at the bottom.  We need to do this to both of them.

This Javascript code will accomplish that.

<script type="text/javascript">

function findFinish(which)
{
   var btn = null;
   var count = 1;
   var e = document.getElementsByTagName("input");

   for(var i = 0; i < e.length; i++)
   {
      if (e[i].type == 'button' && e[i].value == 'Finish')
      {
         if (count == which)
         {
            btn = e[i];
            break;
         }
         else
         {
            count++;
         }
      }
   }
 
   return btn;
}
function insertSubmitEvent()
{
   var btn = findFinish(1);
   if (btn != null)
   {
      if (typeof btn.onclick == "function")
      {
         var oldClick = btn.onclick;
         btn.onclick = function() { window.onbeforeunload = null; oldClick(); }
      }
   }
 
   var btn2 = findFinish(2);
   if (btn2 != null)
   {
     if (typeof btn2.onclick == "function")
     {
        var oldClick = btn2.onclick;
        btn2.onclick = function() { window.onbeforeunload = null; oldClick(); }
     }
   }

}
function forceSubmit()
{
   alert('Since you clicked off the page, your answers will now be recorded - you were warned!');
   var btn = findFinish(1);
   if (btn != null)
     btn.click();
}
insertSubmitEvent();
window.onbeforeunload = forceSubmit;
</script>

This code finds both Finish buttons and inserts code in each button's "click" event to disable the "onbeforeunload" event.  This is so a "real" click of the Finish button does not execute our code.  We only want our code to execute when the user tries to leave the page in some way other than the Finish button.

Next it sets the window's "onbeforeunload" event to our function, which tells the user that their answers are now going to be recorded since they tried to leave the page.  Then it finds the first Finish button and "clicks" it.  (The "onbeforeunload" event won't fire again because the "click" event disables it when it executes.)

Requirement: You can't go back and change your answers once you submit them.

I have found no way to do this in the out-of-the-box surveys in SharePoint.  At first I thought I could create a new Permission Level that gave only Add and View permission (no Edit or Delete permission).  I did this and assigned that Add-and-View permission level to the appropriate group for the survey.  But it always results in an Access Denied error when a user tries to respond to the survey.  It would not allow the users to respond to the survey unless they had Edit permission.

That makes no sense to me - the user is adding a response - why do they need Edit permission?

Since that was out as an option, there was only one thing I could think of - insert some Javascript code in the survey's EditForm.aspx to redirect them out of that form if they tried to change their answers after submission.

Using SPD, I opened the survey's EditForm.aspx and after the </style> tag, I added this code:

<script type="text/javascript">
alert("Sorry, you cannot change your answers once you have finished the quiz.");
window.location = "/SomeLocation/In/SharePoint";
</script>

All this does is display a message, then redirect somewhere else.  No editing allowed.

Requirement: You only have 10 minutes to take the quiz.

We want to put a time limit on the amount of time a user had to take the quiz.  This should discourage them from taking the time to "get a lifeline" by calling or asking someone else, or from firing up a laptop to look up an answer on the web.

Of course, there is nothing built into SharePoint surveys to handle this, but it can be done with a little Javascript code insert into the survey's NewForm.aspx, which is the page the user sees when they respond to a survey.

We can use the Javascript setTimeout() function to fire off an event after a certain amount of time. In our case, we want to fire an event after 10 minutes.  But do what?  We want to submit the survey results - but the Finish button is not a "submit" button, so we can't use the submit() function of the form.  We need to "click" the Finish button.  We can do that with some Javascript - we need to first find the Finish button, then click it.  So add this code to NewForm.aspx, just after the closing </table> tag after the closing </WebPartPages:WebPartZone> tag:

<script type="text/javascript">

function findFinish()
{
    var btn = null;
    var e = document.getElementsByTagName("input");

    for(var i = 0; i < e.length; i++)
    {
        if (e[i].type == 'button' && e[i].value == 'Finish')
        {
           btn = e[i];
           break;
        }
    }
 
    return btn;
}
function endQuiz()
{
    alert('Sorry, your time is up!  Your answers will now be recorded.');
 
    var btn = findFinish();
    if (btn != null)
      btn.click();
}
setTimeout("endQuiz()",60000*10);

</script>

This code will execute the endQuiz() function after 10 minutes (60000 milliseconds = 1 minute, times 10).  endQuiz() displays a message with alert(), then finds the Finish button by looking for an input tag that is a button with the value (label) "Finish", then clicks it, submitting the survey results.

NOTE: You must be sure that no questions are set up as requiring an answer.  If you have any that are required, it will kick the form back open and wait for the user to answer the required questions, defeating the purpose of the timeout.

Requirement: Prevent "cheating" as much as possible.

As explained at the beginning of this post, this is a trivia quiz for fun, but there is a prize involved.  So we want to take some measures to prevent "cheating", but we aren't talking about doing the SAT's, bar exam, or medical boards here.... We just want to make it somewhat difficult, and there are a few easy steps we can take.

First, we can prevent the user from printing the questions on the browser page (perhaps to give them to a partner in crime).  Insert this just after the closing </table> tag after the closing </WebPartPages:WebPartZone> tag:

<style type="text/css" media="print">
BODY { display:none; visibility:hidden; }
</style>

This uses CSS to set the entire body's content to hidden for the media type "print".  So if the print button in the browser is clicked, the page will be blank. (There is no way I can see to prevent the PrintScreen key from being used to capture a screen image, so there is still that option - again, we are just trying to make things difficult, it's not foolproof.)

Second, we can disable the right-click context menu to prevent Select All and Copy, so these can't be used to copy the questions to the clipboard.  We can also capture the "SelectStart" event and short circuit it, which should stop Control-A being used to select everything on the page (to then copy with Control-C).  These measures work in Internet Explorer, but may not work with other browsers.  In our case, we use IE only.  So add this Javascript code to NewForm.aspx:

document.body.oncontextmenu = function() {return false;}
document.body.onselectstart = function() {return false;}

The third and final measure we can take is to prevent users from trying to "Google" some answers.  In a way, the time limit helps with this.  If you have enough questions in the quiz and an aggressive time limit, trying to look up answers will end up taking too much time and you will have a few right answers, but many will be left unanswered.

We can't prevent the user from opening other browser windows or tabs, but we can detect whether our survey page loses focus, meaning the user possibly tried to look something up.  It could be they were doing something innocent like reading an e-mail, but as part of our rules, we are telling the users that once they start, they must stay on that page and finish the entire quiz.  Clicking off the page will automatically submit what they have answered so far.

The loss of focus on the page can be detected in Internet Explorer with the "onblur" event.  It's a strange name, but it's defined in the documentation as "when the object loses the input focus".  If we trap this event on the "window" object, we can tell when the current browser window loses focus, meaning the user clicked off the page and tried to do something else.  If this happens, we want to submit the current answers by "clicking" the Finish button.  This works exactly the same way as how we trapped the "onbeforeunload" event above.  Just do the same thing for "onblur".

Summary

If you want all of the measures described above to be taken, do the following: 

1. Set "Allow multiple responses" to No.

2. Set Item-level Permissions for Read and Edit to "Only their own".

3. Put the survey on a page as a web part, and set the web part to use the Summary toolbar and Chrome Type of None.

4. Use the Content Editor Web Part on that page to hide "View All Site Content" and/or rename the overview.aspx page for the survey.

5. Edit the survey's EditForm.aspx page and add the Javascript given above to redirect away from it so answers cannot be changed.

6. Add the following code to the NewForm.aspx page, after the closing </table> tag after the closing </WebPartPages:WebPartZone> tag, to time the response, prevent navigation away from the page, and prevent copying or printing:

<style type="text/css" media="print">
BODY { display:none; visibility:hidden; }
</style>
<script type="text/javascript">

function findFinish(which)
{
    var btn = null;
    var count = 1;
    var e = document.getElementsByTagName("input");

    for(var i = 0; i < e.length; i++)
    {
        if (e[i].type == 'button' && e[i].value == 'Finish')
        {
            if (count == which)
            {
                btn = e[i];
                break;
            }
            else
            {
                count++;
            }
        }
    }

    return btn;
}
function hideCancel()
{
    var e = document.getElementsByTagName("input");

    for(var i = 0; i < e.length; i++)
        if (e[i].type == 'button' && e[i].value == 'Cancel')
            e[i].style.visibility = "hidden";

}
function insertSubmitEvent()
{
    var btn = findFinish(1);
    if (btn != null)
    {
        if (typeof btn.onclick == "function")
        {
            var oldClick = btn.onclick;
            btn.onclick = function() { window.onbeforeunload = null; window.onblur = null; oldClick(); }
        }
    }

    var btn2 = findFinish(2);
    if (btn2 != null)
    {
        if (typeof btn2.onclick == "function")
        {
            var oldClick = btn2.onclick;
            btn2.onclick = function() { window.onbeforeunload = null; window.onblur = null; oldClick(); }
        }
    }
}
function endQuiz()
{
    alert('Sorry, your time is up! Your answers will now be recorded.');

    window.onbeforeunload = null;
    window.onblur = null;
    var btn = findFinish(1);
    if (btn != null)
        btn.click();
}
function forceSubmit()
{
    alert('Since you clicked off the page, your answers will now be recorded - you were warned!');
    var btn = findFinish(1);
    if (btn != null)
        btn.click();
}

setTimeout("endQuiz()",60000*10);
insertSubmitEvent();
hideCancel();
window.onbeforeunload = forceSubmit;
window.onblur = forceSubmit;
document.body.oncontextmenu = function() {return false;}
document.body.onselectstart = function() {return false;}

</script>

In part 2, we will look at how to easily "grade" the quiz and find out who got the best score.

 


Posted 09-22-2008 4:00 PM by joed

Comments

bazztrap wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 09-22-2008 4:14 PM

usually all quiz have right and wrong answer displayed at some point of emailed in the.

Survey doesnt really show a dynamic indicator is the answer selected was right or wrong before moving on.  Or even email the right answer after quiz is over. I can write an event handler to do it but then I would have to for each new quiz i create..  

joed wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 09-23-2008 7:52 AM

Right, it doesn't do anything about giving the correct answers.  After I post part 2 about grading the quiz, I'll take a look at using a similar technique (using Excel) to send out an e-mail with the response and correct answers.

Links (9/23/2008) « Steve Pietrek - Everything SharePoint wrote Links (9/23/2008) &laquo; Steve Pietrek - Everything SharePoint
on 09-23-2008 7:25 PM

Pingback from  Links (9/23/2008) &laquo; Steve Pietrek - Everything SharePoint

Need Help Creating a Quiz for work Intranet Site - Fires of Heaven Guild Message Board wrote Need Help Creating a Quiz for work Intranet Site - Fires of Heaven Guild Message Board
on 11-03-2008 9:31 PM

Pingback from  Need Help Creating a Quiz for work Intranet Site - Fires of Heaven Guild Message Board

KJ wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 11-17-2008 5:39 AM

How can i create the waring message before

setTimeout("endQuiz()

joed wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 11-17-2008 7:36 PM

KJ, you could add something like this:

function endQuizWarning()

{

   alert('One more minute left...');

}

setTimeout("endQuizWarning()",60000*9);

This will display "One more minute left..." at nine minutes (60000*9).  It will require the user to click OK.

However, I have not tried this - I don't know if this will trigger the "onbeforeunload" event.  I don't think it will, but you will have to try it.

KJ wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 11-17-2008 10:01 PM

Thank you Joed. it's working perfectly. i have one more question if you don't mind.

in the survey , i have modify the newform.aspx  to show a page that i add into content editor web part when user click Finish. is it possible that write the script to close browser or sharepoint when user click Finish?    thank you  

joed wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 11-17-2008 11:24 PM

I don't think it is easily done.  The "Finish" button has to do a postback to the server to write the survey answers to the database.

So if you  did a "window.close()" Javascript call in endQuiz() for example (or hooked into the onclick event for the Finish button), it would close the window and Sharepoint would never get the answers.

I think the best you could do is use the &Source= parameter in the URL.  When you use "&Source=xxx" at the end of the URL to your survey, SharePoint will redirect to the page "xxx" after the postback.  (At least it does with lists, I have not tried it with surveys although it should be the same.)

See this for more details: sharepointsherpa.com/.../sharepoint-2007-using-the-source-query-string-parameter

You might be able to create a page with a Content Editor Web Part (CEWP) and insert some Javascript in the web part like this:

window.opener = self;

window.close();

Or try:

window.opener = 'x';

window.close();

Sometimes window.close() will cause the browser to give the "page is trying to close the browser" warning - setting window.opener is supposed to bypass this but I don't know if it always works.

Anyway, put that in the CEWP on a page, then use the &Source= in the URL to redirect to that page and see if that works.

Brendan wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 11-19-2008 1:23 PM

I enjoyed the post, just wanted to let you know that you can keep people from changing their answers by going to Settings-Survey Settings-Advanced Settings and set the Edit items permission to "None".  This way, they won't be able to edit their responses at all and works on lists too ^_^.

joed wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 11-20-2008 7:28 AM

Brendan, that does not work for me.  If I give a user Contribute permission on the survey and in Advanced Settings set Edit Items to None, the user can take the survey, but when they click Finish they get "Access Denied" and their response is not recorded.

Quiz in MOSS 2007 | keyongtech wrote Quiz in MOSS 2007 | keyongtech
on 01-18-2009 11:02 AM

Pingback from  Quiz in MOSS 2007 | keyongtech

kunkura wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 01-29-2009 1:22 AM

hi im currently using wss 2.0. i have tried using the above code regarding set timeout , it works in such a way that i terminates the time to answer the quiz after 10 minutes but it doesnt save the answers. in wss 2.0 i cannot find the finish button. what i see here is just save and close button. any help? thanks

joed wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 01-29-2009 10:10 PM

kunkura, I do not have a WSS 2.0 system set up to look at it specifically, but take a look at the javascript function in my code called "findFinish".  That's what you need to modify.

In my code, that function simply loops through all of the controls on the page and finds the control that has the type "button" and the label "Finish".  It's a brute force way to find the Finish button, but it's the only way I could come up with since there is no unique ID.

If WSS 2.0 calls the button "Save" instead of "Finish", then it is likely all you need to do is change the word "Finish" in the if statement to "Save" and it should work.

If it does not work, then I doing a web search for the "IE Developer Bar", which is a free tool from MS that installs in your browser and allows you to examine attributes of the controls on a page.

After installing it, load up your WSS 2.0 survey page, then activate the IE Developer bar, use "Find", then "Select Element by Click" and click on the Save button and look at the attributes of it.  Find some things unique about it and change the "findFinish" function to look for a control with those unique attributes.

yatinpurohit wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 02-04-2009 12:41 AM

Very useful post! Some suggestions in MOSS 2007 - Default view Overview.aspx has - "View All Responses" which links to AllItems.aspx. Users can see their own responses and delete their own post using context menu and resubmit a new response. This is even if you have selected "No" to "Allow multiple responses" . To change default Overview page - go to "Site Action -> Edit Page" then "add a new web part" select the Quiz (Survey), then delete original web part. Also, rename AllItems.aspx to a secret name like "AllItemsMyAllItems.aspx" using SPD.

Sam wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 02-04-2009 3:09 AM

Very nice Joed! Thnx.

One question though: Is it possible to link a picture besides the questions??

joed wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 02-08-2009 4:34 PM

Sam - not easily.  There is nothing built into surveys to do that.  You might be able to inject the pictures into the survey page using javascript (and jQuery to make it easier).

beuferd wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 02-09-2009 7:18 AM

your article was very interesting and helpful to me, and I thank you very much for the scripting help.

I have two comments for you that might help other readers.

1.  IE 7 seems to have an issue with the onbeforeunload function.  I run IE 7 and can't get it to work.  When I used the newest version of Firefox, the function works, kind of.  I have to do a screen refresh on the 'overiew' page after I press the back button to see the result of my quiz.

2.  As for the edit.aspx page and the problem you had with users being able to make changes.  You had us put in a script, which was a good work around, but you can use Sharepoint to help you with this problem.  You can make a new USER PERMISSION LEVEL....I use WSS 3.0..I get to this place by....

Clicking People and Groups --> Site Permissions-->(last two steps, links are on the menu bar on the left side of a generic page)-->you should be at a screen listing all the 'groups' on your site-->Click the Settings subheader above the list of Groups-->Add a new Permission level.

Here I made a new permission level (QuizTakers) to 'view' and 'add' records.  

After I made a new Permission Level, I went back to my Quiz Permission Settings and changed it from the Site Settings to Custom and added my new permission to the Groups allowed to view that list.

I hope this information helps you out.  Please re-write what I've written if it's hard to read.

joed wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 02-17-2009 9:33 PM

bueferd,

We use IE7 here on about 500 PC's and did not have any problems with the javascript or onbeforeunload.  I am not sure why it would be different for you.

Also I am unable to get the permission level setup you described to work - I had originally tried that before resorting to script.  Perhaps there is something else different in our security setup, but MOSS would not allow a user to finish their survey unless they had permission to edit.  GIving them View and Add would allow them to take the survey, but when they clicked Finish they would get an access denied error.

I think the way you did it with permission levels is the "right" way to do it, but it is just not working that way for me.

sat wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 02-26-2009 12:35 AM

hi joed,

Your article for setting timeout for a survey quiz is quiet good and nice  and i have a query regarding this as :

If i create a survey quiz with some 20 questions which i kept it in 2 pages, so now how to record the answers with setting some time limit, if in case of first page there will be next button and i dont want to finish or by clicking next button with in the time limit, and how can we store those questions with out submitting..

Thanks in Advance...

kunkura wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 03-01-2009 8:43 PM

hi again joed,

unfortunately, I still cannot automatically save and close after the time is up. i have edited the source as shown below:

<script type="text/javascript">

function findSaveandClose()

{

   var btn = null;

   var e = document.getElementsByTagName("input");

   for(var i = 0; i < e.length; i++)

   {

       if (eIdea.type == 'button' && eIdea.value == 'Save and Close')

       {

          btn = eIdea;

          break;

       }

   }

   return btn;

}

function endQuiz()

{

   alert('Sorry, your time is up!  Your answers will now be recorded.');

   alert('Please click the Save and Close button to save your answers.');

   var btn = findSaveandClose();

   if (btn != null)

     btn.SaveandClose();

}

setTimeout("endQuiz()",60000*10);

since i still cannot execute this automatically, can u instead provide me with codes where in the user can no longer answer or continue answering the exam when time is up. that's why you would notice i've included another alert saying "pls. click the save and close button to save your answers" . hope to hear from you asap. thank you.

Guilefox wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 03-10-2009 4:20 AM

This is fanstastic Joe, we decided to use this rather than any third party quiz webpart for some informal assessments/tests here. I am in your debt big time.

I have it 90% working now after some teething troubles, as I have a problem with navigating away from the 'survey' (ie using the back button on the browser). Or clicking on another page.

Rather than submiting the survey it takes me back a page and nothing has been submitted. I have not set my answers to be compulsory, so not sure why it does not submit the page. The time limit, hiding the cancel button and changing the editform.aspx works great though.

Any ideas?

tony wu wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 04-10-2009 2:45 PM

kunkura,

In WSS 2.0 there is no finish button. Instead there is an anchor (<a>) tag with ID diidIOSaveItem. In NewForm.aspx, place the following HTML below the </WebPartPages:WebPartZone> tag:

<input type=submit value="Submit" onclick="clickFinish()" class="ms-toolbar" >

Place the following javascript before the </body> tag:

<script>

function clickFinish()

{

 document.getElementById("diidIOSaveItem").click();

}

</script>

I am not sure if this will work in Firefox but it works in IE for me.

joed wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 04-22-2009 8:13 AM

Sorry for the delay in publishing/responding to these last remarks.  The sharepointblogs.com moved to a new platform and I just started getting notification emails of responses.

sat, it would be very tricky to get this method to work with a two page survey.  I am not sure that it is possible.  Off the top of my head, without even looking at it, you would need to find the Next button and have it execute your javascript function first, which would need to turn off the things that trap the page exits, then call whatever Sharepoint would call to proceed to the next page.  As for timers, if you wanted to track total time across both page,  you would need to somehow store the elapsed time (perhaps in a cookie), then have the second page pull that out of the cookie and reduce the timeout by the already elapsed time.  It all sounds quite complex.

Guilefox, are you using IE7?

Do you get the alert message when you try to navigate off the page or does it just go to a blank page?

It almost sounds like it is not finding the submit button.

As a test, change the last lines in the forceSubmit() function to:

if (btn != null)

       btn.click();

else

  alert('Did not find submit button');

See if it displays that "Did not find submit button" message.  If it does, that's the cause - then we need to see why it is not finding the button.

Stan Dickervitz wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 05-12-2009 10:37 AM

A much easier solution....

Just rename the summary.aspx and the allitems.aspx in the lists folder. When a user clicks on either 'Show' link, it will show a http404 error page. Administrator can see the 'Show' pages by simply using the changed file name in the browser.

yiitt wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 06-15-2009 7:19 AM

Hi Joe,

I have the same problem with Guilefox. Everything works fine except for the "Back" button. When the user clicks Back, warning message appears on the screen but it doesn't save the records. I couldn't get where the problem is.

Btw really great post,

Thanks.

jennyvera08 wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 06-22-2009 3:09 AM

hi tonywu,

thank you for you response. i havent visited this site for a while. but i have tried ur recommendation, it worked  it displayed a submit button after the time i have set for the exam, the only problem is that it still not automatically save when time's up. my idea is if time is up, the user can no longer answer the other questions and have no choice but to click the submit button. thanks. hope to hear from u again.

joed wrote re: Using SharePoint Surveys for a Quiz - Part 1
on 06-22-2009 10:56 PM

For those of you having problems where it is not forcing the submit, is your survey contained entirely on one page?  I don't think this code will work if it is multiple pages.

If it is only one page, you need to see if it is finding the submit button.  Edit the javascript code and where ever it calles findFinish(), add code to display a message if the return value is null (meaning it did not find the submit button).  That's the only reason I can think of that it would not save it - if it can't find the button to click.  For example:

var btn = findFinish(1);

if (btn == null)

 alert('Did not find the submit button');

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.