Email quiz results to specific person, eg teacher

Email quiz results to specific person, eg teacher

by Patrick Jeitler -
Number of replies: 24

I have searched the forums left and right for detailed information on this subject and found the following:

Quote:

In the file moodle/mod/quiz/attempt.php around line 224 you have:

if ($quiz->grade) {
print_heading("$strscore: $result->sumgrades/$quiz->sumgrades ($result->percentage %)");
print_heading("$strgrade: $result->grade/$quiz->grade");
}
End Quote
around this line i am supposed to call the mail function, but here is my problem:
I cannot find any of that code in my version of quiz:
$module->version  = 2005060301;   // The (date) version of this module
$module->requires = 2005021600;   // Requires this Moodle version
where did it go? any insight on this would be great as i need to be able to add email notification after a person has completed their quiz.
Thank you,
Patrick
Average of ratings: -
In reply to Patrick Jeitler

Re: Email quiz results to specific person, eg teacher

by Gustav W Delius -
You could add it to /mod/quiz/review.php in the

/// Print infobox

section. If students are allowed to review their quiz immediately after closing their attempt then they are brought to this page. So you can send the email from here. But you need to make sure to send the email only once because students can revisit this page later (depending on the options you have set).
In reply to Patrick Jeitler

Re: Email quiz results to specific person, eg teacher

by Tony Ruggiero -
Patrick:

Were you able to get this working? If so, would you be willing to share your code changes?

Do you e-mail when a particular grade level has been reached?

Thank you in advnaced for your help,
Tony
In reply to Tony Ruggiero

Re: Email quiz results to specific person, eg teacher

by Patrick Jeitler -
I have not been able to get it to work just yet. I will post an update when I do.
In reply to Patrick Jeitler

Re: Email quiz results to specific person, eg teacher

by Patrick Jeitler -

I finally had some time to play around with this:

Here is the code that I use to generate an email to all the teachers of a course. The red text is the existing code and the black text is what I have added.

Here is an example of the email that was sent out:

  • User Information:
  • Name: Test User
  • TechID:
  • Organization:
  • Course: Course Fullname 101
  • Quiz: test quiz
  • Score: 100

Here is the code:

$percentage = round(($attempt->sumgrades/$quiz->sumgrades)*100, 0);
        $grade = round(($attempt->sumgrades/$quiz->sumgrades)*$quiz->grade, $CFG->quiz_decimalpoints);
        $table->data[] = array("$strscore:", "$attempt->sumgrades/$quiz->sumgrades ($percentage %)");
        $table->data[] = array("$strgrade:", $grade.get_string('outof', 'quiz').$quiz->grade);

  
  
  // Added automatic emailing if a user completes an exam with a certrain score
  
  if ($teachers = get_course_teachers($course->id)) {
         foreach ($teachers as $teacher) {            
               echo $teacher->id.'<br>';
               echo $teacher->email.'<br>';
                $to .= "$teacher->email,";
         }
     }
  
  $to = rtrim($to, ",");
  $subject = $USER->firstname.' '.$USER->lastname.' has completed the quiz "'.$quiz->name.'" in  the course "'.$course->fullname.'" with a passing grade.';
  //$message = $USER->firstname.', (ID='.$USER->id
  $message = "User Information:\n"
     ."Name: ".$USER->firstname." ".$USER->lastname."\n"
     ."TechID: ".$USER->idnumber."\n"
     ."Organization: ".$USER->department."\n"
     ."Course: ".$course->fullname."\n"
     ."Quiz: ".$quiz->name."\n"
     ."Score: ".$percentage."\n";

  $message = wordwrap($message, 70);
  if ($percentage>80){              // set minimum required passing score
       mail($to,$subject,$message);
  }
    

 

 

 }
    if ($isteacher and $attempt->userid == $USER->id) {
        // the teacher is at the end of a preview. Print button to start new preview
        unset($buttonoptions);
        $buttonoptions['q'] = $quiz->id;

 This is just a first draft. I still need to address this problem: everytime a user reviews a quiz attempt an email is sent out. My thoughts on solving this problem:

  • Add a field 'sent_email' to the mdl_quiz_attempts table.
  • Once an email has been sent out, an entry is being made in the table.
  • everytime a user reviews a quiz attempt, I'll check the sent_email field for the user's attempts. If nothing has been sent and the minimum score has been reached, I'll send an email. Otherwise no email is sent.

It wouldn't be that hard, but I don't know how Moodle accesses the database and I would much rather use the built in functions than create new ones.

Any help or ideas are much appreciated.

 

In reply to Patrick Jeitler

Re: Email quiz results to specific person, eg teacher

by frank weissman -

I like the idea.

I have some extra ones.

  • Make it possible to sent an e-mail to the candidate as well
  • Put the candidates e-mail adress in the "senders" field, so it would be possible to reply an reflect.
  • Make it possible to turn on and off. ( If I have 500 students taking a test I would not like 500 mails in my mailbox)

Frank

In reply to frank weissman

Re: Email quiz results to specific person, eg teacher

by Tony Ruggiero -
Patrick:

Ok, loaded the changes in quiz/review.php and I get the following error:

Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing in D:\inetpub\rnfa\moodle\mod\quiz\review.php on line 201

I am working on a WIndows box and I know the mail server requires a from address.

Any ideas?

Tony

Using Moodle 1.53+
In reply to Patrick Jeitler

Re: Email quiz results to specific person, eg teacher

by Tony Ruggiero -
Awesome Patrick!!!!!

Thanks...I think your solution for the mulitple e-mails makes perfect sense.

Tony
In reply to Patrick Jeitler

Re: Email quiz results to specific person, eg teacher

by Tony Ruggiero -
Folks:

If you have a Windows box and restrictive SMTP policies you might need this in front of Patrick's code:

/* Specify your SMTP Server, Port and Valid From Address */
  ini_set("SMTP","mail.yourdomain.com");
  ini_set("smtp_port","25");
  ini_set("sendmail_from","you@yourdomain.com");

The from e-mail must be a valid account on the server.

Works great, thanks Patrick.

Tony
In reply to Tony Ruggiero

Re: Email quiz results to specific person, eg teacher

by Patrick Jeitler -

Tony, I am glad you got it worked out.

I have not been able to include any updates as of yet. I have been working on the certificate module: it now includes an option to display the entire course grade instead of just one graded module. Here is the link if you are interested.

http://moodle.org/mod/forum/discuss.php?d=35709

Patrick

In reply to Patrick Jeitler

Re: Email quiz results to specific person, eg teacher

by Tony Ruggiero -
Patrick:

I will download it and check it out.

I am working up some scripting for the one-time e-mail function but I am pretty bad when it comes to scripting from scratch. I can troubleshoot pretty good so it's going slow and it isn't going to look pretty.

Tony
In reply to Patrick Jeitler

Re: Email quiz results to specific person, eg teacher

by Tony Ruggiero -
Patrick:

Found a tiny problem in the code. If you access the page as an Teacher it sends the result out as if you took the exam. I have eliminated some of the code for my use, but the important changes are in bold.

This code correction fixes that:

$to = rtrim($to, ",");
$subject = $student->firstname.' '.$student->lastname.' has completed the quiz/homework "'.$quiz->name.'" in "'.$course->fullname.'" with a passing grade.';
//$message = $USER->firstname.', (ID='.$USER->id
$message = "User Information:\n"
."Name: ".$student->firstname." ".$student->lastname."\n"
."TechID: ".$student->idnumber."\n"
."Course: ".$course->fullname."\n"
."Quiz: ".$quiz->name."\n"
."Score: ".$percentage."\n";

Thanks,
Tony
In reply to Tony Ruggiero

Re: Email quiz results to specific person, eg teacher

by Gustav W Delius -
Take a look at lib/moodlelib.php where you will find a function email_to_user() which you should use instead.
In reply to Gustav W Delius

Re: Email quiz results to specific person, eg teacher

by Chris Fry -

These lines appear to include the code for change of email address change/login failure and confirmation....but nothing to do with the quiz module.

Do you agree?

In reply to Chris Fry

Re: Email quiz results to specific person, eg teacher

by Gustav W Delius -
Chris, sorry for my brief and confusing post. I was just giving a hibt regarding the problem reported in an earlier post with the email function which would be solved by using Moodle's emailing function.
In reply to Gustav W Delius

Re: Email quiz results to specific person, eg teacher

by Chris Fry -
No problem .... but if you would; can you please explain to me .... the code that you have posted will work for sending mail to the Teacher so they can see the grade?
In reply to Chris Fry

Re: Email quiz results to specific person, eg teacher

by Gustav W Delius -
Yes, the function email_to_user() will send an email to a specified user. Take a look at the documentation of the function in moodlelib.php. This function is the recommended way of sending emails from Moodle. It is the function used by the forum module to send you a copy of this post, for example.


In reply to Gustav W Delius

Re: Email quiz results to specific person, eg teacher

by Chris Fry -

This is what I have:

        $percentage = round(($attempt->sumgrades/$quiz->sumgrades)*100, 0);
        $grade = round(($attempt->sumgrades/$quiz->sumgrades)*$quiz->grade, $CFG->quiz_decimalpoints);
        $table->data[] = array("$strscore:", "$attempt->sumgrades/$quiz->sumgrades ($percentage %)");
        $table->data[] = array("$strgrade:", $grade.get_string('outof', 'quiz').$quiz->grade);


        // Added automatic emailing if a user completes an exam with a certrain score

  if ($teachers = get_course_teachers($course->id)) {
         foreach ($teachers as $teacher) {
               echo $teacher->id.'<br>';
               echo $teacher->email.'<br>';
                $to .= "$teacher->email,";
         }
     }

  $to = rtrim($to, ",");
  $subject = $USER->firstname.' '.$USER->lastname.' has completed the quiz "'.$quiz->name.'" in  the course "'.$course->fullname.'" with a passing grade.';
  //$message = $USER->firstname.', (ID='.$USER->id
  $message = "User Information:\n"
     ."Name: ".$USER->firstname." ".$USER->lastname."\n"
     ."TechID: ".$USER->idnumber."\n"
     ."Organization: ".$USER->department."\n"
     ."Course: ".$course->fullname."\n"
     ."Quiz: ".$quiz->name."\n"
     ."Score: ".$percentage."\n";

  $message = wordwrap($message, 70);
  if ($percentage>80){              // set minimum required passing score
       mail($to,$subject,$message);
  }


    }
    if ($isteacher and $attempt->userid == $USER->id) {
        // the teacher is at the end of a preview. Print button to start new preview

within my \mod\quiz\review.php ...  however I am not getting an email and I am not getting an error....any thoughts?

In reply to Chris Fry

Re: Email quiz results to specific person, eg teacher

by Tony Ruggiero -
Are you using a Windows server?

I have modified the code for my use but change the areas in bold:

if ($isteacher) {
/* Specify your SMTP Server, Port and Valid From Address */
ini_set("SMTP","mail.yourdomain.com");
ini_set("smtp_port","25");
ini_set("sendmail_from","webmaster@yourdomain.com");
if ($teachers = get_course_teachers($course->id)) {
foreach ($teachers as $teacher) {
$to .= "$teacher->email,";
}
}
$to = rtrim($to, ",");
$subject = $student->firstname.' '.$student->lastname.' has completed the exam "'.$quiz->name.'" in "'.$course->fullname.'" with a passing grade.';
//$message = $USER->firstname.', (ID='.$USER->id
$message = "User Information:\n"
."Name: ".$student->firstname." ".$student->lastname."\n"
."Program: TDL-CCSN\n"
."Course: ".$course->fullname."\n"
."Quiz: ".$quiz->name."\n"
."Score: ".$percentage."\n";
$message = wordwrap($message, 70);
if ($percentage>85){ // set minimum required passing score
mail($to,$subject,$message);
}
} else {
/* Specify your SMTP Server, Port and Valid From Address */
ini_set("SMTP","mail.yourdomain.com");
ini_set("smtp_port","25");
ini_set("sendmail_from","webmaster@yourdomain.com");
if ($teachers = get_course_teachers($course->id)) {
foreach ($teachers as $teacher) {
$to .= "$teacher->email,";
}
}
$to = rtrim($to, ",");
$subject = $USER->firstname.' '.$USER->lastname.' has completed the exam "'.$quiz->name.'" in "'.$course->fullname.'" with a passing grade.';
//$message = $USER->firstname.', (ID='.$USER->id
$message = "User Information:\n"
."Name: ".$USER->firstname." ".$USER->lastname."\n"
."Program: TDL-CCSN\n"
."Course: ".$course->fullname."\n"
."Quiz: ".$quiz->name."\n"
."Score: ".$percentage."\n";
$message = wordwrap($message, 70);
if ($percentage>85){ // set minimum required passing score
mail($to,$subject,$message);
}
}

This will allow a teacher to use the review page and even if it sends an e-mail, it will use the student's name instead of the teacher's.

If you are using a Windows server the "from" e-mail address must be an active account on that server. This code also eliminates the "echo" of the teacher's e-mail on the review page. I have this running on 4 installations of Moodle 1.53+

Hope this helps.
Tony
In reply to Tony Ruggiero

Re: Email quiz results to specific person, eg teacher

by Chris Fry -

Tony

First, thank you for sending me the code.

Yes I am using an a windows server with Exchange, and even though I am not getting an error when I submit the results I am also not getting the email with the results.

I have tried changing the script to my internal mail domain and my external mail domain and neither appears to work.

I am including the entire review.php file so that you may be able to advise me where I am going wrong .... thank you

In reply to Chris Fry

Re: Email quiz results to specific person, eg teacher

by Tony Ruggiero -
Chris:

I think you had it in the wrong place. Try this one (I replaced my information with yours).

Good luck,
Tony
In reply to Tony Ruggiero

Re: Email quiz results to specific person, eg teacher

by Chris Fry -
 

quiz1


Warning: mail(): Failed to connect to mailserver at "mail.cfryc.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in c:\easyphp1-8\www\mod\quiz\review.php on line 224

Is the error that I am getting with the update.....

In reply to Chris Fry

Re: Email quiz results to specific person, eg teacher

by Tony Ruggiero -
Chris:

Has your Moodle installation been able to send out other e-mails? You need to use the settings you put in the Admin>>>Variables page for Mail to get this to work. Your mail server may not be "mail.cfryc.com"

I had that error until I added the init_set lines to the code.

There were some pacing issues in the file I attached last night (although I don't think that's the problem) so I attached another one.

All this will have to change because I think this need to be an optional function in locallib.php and allow for only one mailing.

Hope it works,
Tony
In reply to Tony Ruggiero

Re: Email quiz results to specific person, eg teacher

by cure hope -
I like to send periodic reminder e-mails to students who have not passed the quiz yet, it that possible?
In reply to cure hope

This worked with an older version of Moodle

by brad winchester -

I had this working on an older version of Moodle.  However it's not working with the latest version. Hope it helps someone.

moodle/mod/quiz/review.php


<?php  // $Id: review.php,v 1.23.2.5 2005/07/07 14:53:15 gustav_delius Exp $
/**
* This page prints a review of a particular quiz attempt
*
* @version $Id: review.php,v 1.23.2.5 2005/07/07 14:53:15 gustav_delius Exp $
* @author Martin Dougiamas and many others. This has recently been completely
*         rewritten by Alex Smith, Julian Sedding and Gustav Delius as part of
*         the Serving Mathematics project
*        
{@link http://maths.york.ac.uk/serving_maths}
* @license
http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package quiz
*/

    require_once("../../config.php");
    require_once("locallib.php");

    $attempt = required_param('attempt', PARAM_INT);    // A particular attempt ID for review
    $page = optional_param('page', 0, PARAM_INT); // The required page
    $showall = optional_param('showall', 0, PARAM_BOOL);

    if (! $attempt = get_record("quiz_attempts", "id", $attempt)) {
        error("No such attempt ID exists");
    }
    if (! $quiz = get_record("quiz", "id", $attempt->quiz)) {
        error("The quiz with id $attempt->quiz belonging to attempt $attempt is missing");
    }
    if (! $course = get_record("course", "id", $quiz->course)) {
        error("The course with id $quiz->course that the quiz with id $quiz->id belongs to is missing");
    }
    if (! $cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) {
        error("The course module for the quiz with id $quiz->id is missing");
    }

    if (!count_records('quiz_newest_states', 'attemptid', $attempt->id)) {
        // this question has not yet been upgraded to the new model
        quiz_upgrade_states($attempt);
    }

    require_login($course->id, false, $cm);
    $isteacher = isteacher($course->id);
    $popup = $isteacher ? 0 : $quiz->popup; // Controls whether this is shown in a javascript-protected window.

    if (!$isteacher) {
        if (!$attempt->timefinish) {
            redirect('attempt.php?q='.$quiz->id);
        }
        // If not even responses are to be shown in review then we
        // don't allow any review
        if (!($quiz->review & QUIZ_REVIEW_RESPONSES)) {
            redirect('view.php?q='.$quiz->id);
        }
        if ((time() - $attempt->timefinish) > 120) { // always allow review right after attempt
            if (time() < $quiz->timeclose and !($quiz->review & QUIZ_REVIEW_OPEN)) {
                redirect('view.php?q='.$quiz->id, get_string("noreviewuntil", "quiz", userdate($quiz->timeclose)));
            }
            if (time() >= $quiz->timeclose and !($quiz->review & QUIZ_REVIEW_CLOSED)) {
                redirect('view.php?q='.$quiz->id, get_string("noreview", "quiz"));
            }
        }
        if ($attempt->userid != $USER->id) {
            error("This is not your attempt!", 'view.php?q='.$quiz->id);
        }
    }

    add_to_log($course->id, "quiz", "review", "review.php?id=$cm->id&amp;attempt=$attempt->id", "$quiz->id", "$cm->id");

/// Print the page header

    $strquizzes = get_string("modulenameplural", "quiz");
    $strreview  = get_string("review", "quiz");
    $strscore  = get_string("score", "quiz");
    $strgrade  = get_string("grade");
    $strbestgrade  = get_string("bestgrade", "quiz");
    $strtimetaken     = get_string("timetaken", "quiz");
    $strtimecompleted = get_string("completedon", "quiz");
    $stroverdue = get_string("overdue", "quiz");

    if (!empty($popup)) {
        define('MESSAGE_WINDOW', true);  // This prevents the message window coming up
        print_header($course->shortname.': '.format_string($quiz->name), '', '', '', '', false, '', '', false, '');
        /// Include Javascript protection for this page
        include('protect_js.php');
    } else {
        $strupdatemodule = isteacheredit($course->id)
                    ? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
                    : "";
        print_header_simple(format_string($quiz->name), "",
                 "<a href=\"index.php?id=$course->id\">$strquizzes</a>
                  -> <a href=\"view.php?id=$cm->id\">".format_string($quiz->name,true)."</a> -> $strreview",
                 "", "", true, $strupdatemodule);
    }
    echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib

/// Print heading and tabs if this is part of a preview
    if ($isteacher) {
        $currenttab = ($attempt->userid == $USER->id) ? 'preview' : '';
        include('tabs.php');
    } else {
        print_heading(format_string($quiz->name));
    }

/// Load all the questions and states needed by this script

    // load the questions needed by page
    $pagelist = $showall ? quiz_questions_in_quiz($attempt->layout) : quiz_questions_on_page($attempt->layout, $page);
    $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
           "  FROM {$CFG->prefix}quiz_questions q,".
           "       {$CFG->prefix}quiz_question_instances i".
           " WHERE i.quiz = '$quiz->id' AND q.id = i.question".
           "   AND q.id IN ($pagelist)";
    if (!$questions = get_records_sql($sql)) {
        error('No questions found');
    }

    // Load the question type specific information
    if (!quiz_get_question_options($questions)) {
        error('Could not load question options');
    }

    // Restore the question sessions to their most recent states
    // creating new sessions where required
    if (!$states = quiz_restore_question_sessions($questions, $quiz, $attempt)) {
        error('Could not restore question sessions');
    }

/// Print infobox

    $timelimit = (int)$quiz->timelimit * 60;
    $overtime = 0;

//    if ($attempt->timefinish) {
//        if ($timetaken = ($attempt->timefinish - $attempt->timestart)) {
//            if($timelimit && $timetaken > ($timelimit + 60)) {
//                $overtime = $timetaken - $timelimit;
//                $overtime = format_time($overtime);
//            }
//            $timetaken = format_time($timetaken);
//        } else {
//            $timetaken = "-";
//        }
//    } else {
//        $timetaken = get_string('unfinished', 'quiz');
//    }

    $table->align  = array("right", "left");
    if ($attempt->userid <> $USER->id) {
       $student = get_record('user', 'id', $attempt->userid);
       $picture = print_user_picture($student->id, $course->id, $student->picture, false, true);
       $table->data[] = array($picture, fullname($student, true));
    }
    if ($isteacher and count($attempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' AND userid = '$attempt->userid'", 'attempt ASC')) > 1) {
        // print list of attempts
        $attemptlist = '';
        foreach ($attempts as $at) {
            $attemptlist .= ($at->id == $attempt->id)
                ? '<b>'.$at->attempt.'</b>, '
                : '<a href="review.php?attempt='.$at->id.($showall?'&amp;showall=true':'').'">'.$at->attempt.'</a>, ';
        }
        $table->data[] = array(get_string('attempts', 'quiz').':', trim($attemptlist, ' ,'));
    }

    $table->data[] = array(get_string('startedon', 'quiz').':', userdate($attempt->timestart));
    if ($attempt->timefinish) {
        $table->data[] = array("$strtimecompleted:", userdate($attempt->timefinish));
        $table->data[] = array("$strtimetaken:", $timetaken);
    }
    if (!empty($overtime)) {
        $table->data[] = array("$stroverdue:", $overtime);
    }
    if ($quiz->grade) {
        if($overtime) {
            $result->sumgrades = "0";
            $result->grade = "0.0";
        }
        $percentage = round(($attempt->sumgrades/$quiz->sumgrades)*100, 0);
        $grade = round(($attempt->sumgrades/$quiz->sumgrades)*$quiz->grade, $CFG->quiz_decimalpoints);
        $table->data[] = array("$strscore:", "$attempt->sumgrades/$quiz->sumgrades ($percentage %)");
        $table->data[] = array("$strgrade:", $grade.get_string('outof', 'quiz').$quiz->grade);
       
// Added automatic emailing if a user completes an exam with a certrain score
    if ($isteacher) {
/* Specify your SMTP Server, Port and Valid From Address */
  ini_set("SMTP","mail.domain.com");
  ini_set("smtp_port","25");
  ini_set("sendmail_from","
<font color="#ff0000">noreply@domain.com</font>");
 if ($teachers = get_course_teachers($course->id)) {
  foreach ($teachers as $teacher) {
  $to .= "$teacher->email,";
  }
 }
  $to = rtrim($to, ",");
  $subject = $student->firstname.' '.$student->lastname.' has completed the exam "'.$quiz->name.'" in "'.$course->fullname.'" with a passing grade.';
  //$message = $USER->firstname.', (ID='.$USER->id
  $message = "User Information:\n"
  ."Name: ".$student->firstname." ".$student->lastname."\n"
  ."Program: TDL-CCSN\n"
  ."Course: ".$course->fullname."\n"
  ."Quiz: ".$quiz->name."\n"
  ."Score: ".$percentage."\n";
  $message = wordwrap($message, 70);
 if ($percentage>1){ // set minimum required passing score
  mail($to,$subject,$message);
  }
 } else {
/* Specify your SMTP Server, Port and Valid From Address */
  ini_set("SMTP","mail.domain.com");
  ini_set("smtp_port","25");
  ini_set("sendmail_from","
<font color="#ff0000">noreply@domain.com</font>");
 if ($teachers = get_course_teachers($course->id)) {
  foreach ($teachers as $teacher) {
  $to .= "$teacher->email,";
  }
 }
  $to = rtrim($to, ",");
  $subject = $USER->firstname.' '.$USER->lastname.' has completed the exam "'.$quiz->name.'" in "'.$course->fullname.'" with a passing grade.';
  //$message = $USER->firstname.', (ID='.$USER->id
  $message = "User Information:\n"
  ."Name: ".$USER->firstname." ".$USER->lastname."\n"
  ."Program: TDL-CCSN\n"
  ."Course: ".$course->fullname."\n"
  ."Quiz: ".$quiz->name."\n"
  ."Score: ".$percentage."\n";
  $message = wordwrap($message, 70);
 if ($percentage>1){ // set minimum required passing score
  mail($to,$subject,$message);
  }
 }
  }
 if ($isteacher and $attempt->userid == $USER->id) {
        // the teacher is at the end of a preview. Print button to start new preview
        unset($buttonoptions);
        $buttonoptions['q'] = $quiz->id;
        $buttonoptions['forcenew'] = true;
        echo '<center>';
        print_single_button($CFG->wwwroot.'/mod/quiz/attempt.php', $buttonoptions, get_string('startagain', 'quiz'));
        echo '</center>';
    } else { // print number of the attempt
        print_heading(get_string('reviewofattempt', 'quiz', $attempt->attempt));
    }
    print_table($table);

    // print javascript button to close the window, if necessary
    if (!$isteacher) {
        include('attempt_close_js.php');
    }

/// Print the navigation panel if required
    $numpages = quiz_number_of_pages($attempt->layout);
    if ($numpages > 1 and !$showall) {
        print_paging_bar($numpages, $page, 1, 'review.php?attempt='.$attempt->id.'&amp;');
        echo '<center><a href="review.php?attempt='.$attempt->id.'&amp;showall=true">';
        print_string('showall', 'quiz');
        echo '</a></center>';
    }

/// Print all the questions

    $pagequestions = explode(',', $pagelist);
    $number = quiz_first_questionnumber($attempt->layout, $pagelist);
    foreach ($pagequestions as $i) {
        if (!isset($questions[$i])) {
            print_simple_box_start('center', '90%');
            echo '<b><font size="+1">' . $number . '</font></b><br />';
            notify(get_string('errormissingquestion', 'quiz', $i));
            print_simple_box_end();
            $number++; // Just guessing that the missing question would have lenght 1
            continue;
        }
        $options = quiz_get_reviewoptions($quiz, $attempt, $isteacher);
        $options->validation = QUIZ_EVENTVALIDATE === $states[$i]->event;
        $options->history = ($isteacher and !$attempt->preview) ? 'all' : 'graded';
        // Print the question
        if ($i > 0) {
            echo "<br />\n";
        }
        quiz_print_quiz_question($questions[$i], $states[$i], $number, $quiz, $options);
        $number += $questions[$i]->length;
    }

    // Print the navigation panel if required
    if ($numpages > 1 and !$showall) {
        print_paging_bar($numpages, $page, 1, 'review.php?attempt='.$attempt->id.'&amp;');
    }

    // print javascript button to close the window, if necessary
    if (!$isteacher) {
        include('attempt_close_js.php');
    }

    if (empty($popup)) {
        print_footer($course);
    }
?>