I just finished writing the code that will email survey results to me every time somebody takes a survey.

I'm sure there are more elegant ways of doing this, but this one is simple and effective.

The following is the code that I inserted in classes/survey.class.php, right before the 'return' command at the end.

(Be sure to back up your original file so that you don't kill your survey system if this has any problems.)

I've added comments within the code to help you implement it. Remember, you insert this right before the final "return" in survey.class.php and then follow the instructions in the comments.
CODE
// BEGIN CODE FOR EMAILING RESULTS.
        include_once("/var/www/vhosts/mydomain.com/sqlstuff/ezsql.php");
                 // ezSQL is my favorite SQL wrapper. It's free and makes this stuff super-simple.
                 // You can download it here: http://www.woyano.com/jv/ezsql
                 // Once you install it, change the include line above to point to the right place for ezsql.php.
                 // Or, obviously, you can work without it - but you'll have to change the code below around accordingly.

        $emailbody = "<html><head><title>Survey Response</title></head><body>Survey Response:<br><br>";

        // In the following SQL, be sure to change all the "sv_" references to whatever your table prefix is.

        $responses = $db->get_results("SELECT GREATEST(rt.sequence, r.sequence) AS seq, GREATEST(rt.qid, r.qid) AS qid, from_unixtime(GREATEST(rt.entered, r.entered)) AS entered, q.question as question, av.numeric_value as numericvalue, GREATEST(av.value, rt.answer) as answer FROM sv_questions q LEFT JOIN sv_results r ON q.qid = r.qid LEFT JOIN sv_results_text rt ON q.qid = rt.qid LEFT JOIN sv_answer_values av ON r.avid = av.avid WHERE GREATEST(rt.sequence, r.sequence) = $id");

        foreach ( $responses as $response )
        {
            $qid = $response->qid;
            $question = $response->question;
            $answer = $response->answer;
            $emailbody = $emailbody . "$question<br>  <font color='red'>$answer</font><br>";

            // In my only survey, question id 221 asks for their email address. Change 221 in the following line to the qid of your question asking that. If you don't have one then omit this line. This just makes it so the email comes "from" your respondent.
            if ( $qid == 221 ) { $fromemail = $answer; }

        }
        $emailbody = $emailbody . "</body></html>";

        // If you don't have a question asking for an email, and so you omitted the 221 line above, then also omit this if statement, including the { ... } code block after it.
        if ( (!preg_match("/^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/", "$fromemail") ) )
        {
           $fromemail = "webmaster@yourdomain.com"; // If they gave a bad email format, it will use this instead.
        }

        // If you omitted all the stuff above that sets $fromemail to the respondent, then be sure to replace $fromemail in the following line with webmaster@yourdomain.com or something similar.
        mail("YourEmail@YourDomain.com", "Survey Response", "$emailbody", "From: $fromemail\nContent-Type: text/html; charset=iso-8859-1");
// END CODE FOR EMAILING RESULTS.

The beauty of this is that it's entirely self-contained. As a result, when UCCASS gets upgraded or patched and the survey.class.php gets replaced, you can just pop the code right back into the new file and it should work fine. For this reason, I suggest that once you have it working you make a copy of the working code and keep it somewhere that won't be messed up by an upgrade. That way you have easy access to it to just cut and paste it right back in.

One thing I might mention is that getting the email address from a question and using it for the "from" in the email does kind of make this code assume that you are using only a single survey. I like to be able to respond to people easily and I only have one survey, so this is nice feature for me. But if you have multiple surveys, then you should follow the directions above on omitting it because then the code will be much more generalized and will work equally well with all your surveys.

One last thing: I'm hoping people find this useful and worth tweaking on. There are a number of ways to generalize it and improve it, for example by replacing all the sv_ references with the proper variables to make it so that others don't have to manually change them, or grabbing the actual admin email address from config. And there are probably a million others. I just dashed this off in a short while and posting it for others was kind of an after thought. If you improve it, then I'm sure everybody (myself included) would appreciate it if you post your improvements to this thread.

Enjoy!

W