public function test_certificate_email_teachers()
 {
     global $DB;
     $certificate = $this->generator->create_instance(array('course' => $this->course->id));
     $coursemodule = get_coursemodule_from_instance('certificate', $certificate->id);
     $studentroleid = $DB->get_record('role', array('shortname' => 'student'), 'id')->id;
     $teacherroleid = $DB->get_record('role', array('shortname' => 'editingteacher'), 'id')->id;
     $teacheruserarray = array();
     for ($i = 0; $i < 10; $i++) {
         $teacheruserarray[] = $this->getDataGenerator()->create_user(array('email' => "teacherdoge{$i}@dogeversity.doge", 'username' => "Dr. doge{$i}"));
         // Enrol the user as a teacher.
         $this->getDataGenerator()->enrol_user(end($teacheruserarray)->id, $this->course->id, $teacherroleid);
     }
     // Enrol a single student and issue his/her a certificate.
     $studentuser = $this->getDataGenerator()->create_user(array('email' => "*****@*****.**", 'username' => "dogemanorwomen"));
     $this->getDataGenerator()->enrol_user($studentuser->id, $this->course->id, $studentroleid);
     $certificateissues = certificate_get_issue($this->course, $studentuser, $certificate, $coursemodule);
     $this->setUser($studentuser);
     unset_config('noemailever');
     $sink = $this->redirectEmails();
     $certificate->emailteachers = true;
     // Temporarily make it true.
     certificate_email_teachers($this->course, $certificate, $certificateissues, $coursemodule);
     $messages = $sink->get_messages();
     $this->assertEquals(count($teacheruserarray), count($messages));
     // Verify to/from emails
     foreach ($messages as $message) {
         $this->assertEquals($studentuser->email, $message->from);
     }
     foreach ($teacheruserarray as $user) {
         $useremailisrecipient = false;
         foreach ($messages as $message) {
             if ($useremailisrecipient = $message->to == $user->email) {
                 break;
             }
         }
         $this->assertTrue($useremailisrecipient);
     }
 }
Example #2
0
/**
 * Inserts final user data when a certificate is created.
 *
 * @param stdClass $course
 * @param stdClass $certificate
 * @param stdClass $certrecord
 * @param stdClass $cm
 */
function certificate_issue($course, $certificate, $certrecord, $cm)
{
    global $USER, $DB;
    if ($certificate->printgrade) {
        if ($certificate->printgrade == 1) {
            $grade = certificate_print_course_grade($course);
        } else {
            if ($certificate->printgrade > 1) {
                $grade = certificate_print_mod_grade($course, $certificate->printgrade);
            }
        }
        if ($certificate->gradefmt == 1) {
            $certrecord->reportgrade = $grade->percentage;
        }
        if ($certificate->gradefmt == 2) {
            $certrecord->reportgrade = $grade->points;
        }
        if ($certificate->gradefmt == 3) {
            $certrecord->reportgrade = $grade->letter;
        }
    }
    $date = certificate_generate_date($certificate, $course);
    $certrecord->certdate = $date;
    $DB->update_record('certificate_issues', $certrecord);
    certificate_email_teachers($course, $certificate, $certrecord, $cm);
    certificate_email_others($course, $certificate, $certrecord, $cm);
}
function certificate_prepare_issue($course, $user)
{
    global $certificate;
    if (record_exists("certificate_issues", "certificateid", $certificate->id, "userid", $user->id)) {
        return get_record("certificate_issues", "certificateid", $certificate->id, "userid", $user->id);
    } else {
        if (certificate_grade_condition()) {
            $timecreated = time();
            $certdate = certificate_generate_date($certificate, $course);
            if (!$certdate) {
                $certdate = $timecreated;
            }
            $code = certificate_generate_code();
            if ($certificate->printgrade == 1) {
                $coursegrade = get_course_grade($course->id);
            } else {
                if ($certificate->printgrade > 1) {
                    $coursegrade = certificate_mod_grade($course, $certificate->printgrade);
                }
            }
            if ($certificate->gradefmt == 1) {
                $gradeinput = $coursegrade->percentage . '%';
            } else {
                if ($certificate->gradefmt == 2) {
                    $gradeinput = $coursegrade->points . ' %%P%%';
                } else {
                    if ($certificate->gradefmt == 3) {
                        $gradeinput = certificate_get_gradeletter($coursegrade->percentage);
                    }
                }
            }
            $studentname = str_replace('\'', '\\\'', certificate_generate_studentname($course, $user));
            insert_record("certificate_issues", array("certificateid" => $certificate->id, "userid" => $user->id, "timecreated" => $timecreated, "studentname" => $studentname, "code" => $code, "classname" => str_replace('\'', '\\\'', $course->fullname), "certdate" => $certdate, "credits" => $certificate->credithours, "grade" => $gradeinput), false);
            certificate_email_teachers($certificate);
        }
    }
}
Example #4
0
/**
 * Inserts preliminary user data when a certificate is viewed.
 * Prevents form from issuing a certificate upon browser refresh.
 *
 * @param stdClass $course
 * @param stdClass $user
 * @param stdClass $certificate
 * @param stdClass $cm
 * @return stdClass the newly created certificate issue
 */
function certificate_get_issue($course, $user, $certificate, $cm)
{
    global $DB;
    // Check if there is an issue already, should only ever be one
    if ($certissue = $DB->get_record('certificate_issues', array('userid' => $user->id, 'certificateid' => $certificate->id))) {
        return $certissue;
    }
    // Create new certificate issue record
    $certissue = new stdClass();
    $certissue->certificateid = $certificate->id;
    $certissue->userid = $user->id;
    $certissue->code = certificate_generate_code();
    $certissue->timecreated = time();
    $certissue->id = $DB->insert_record('certificate_issues', $certissue);
    // Email to the teachers and anyone else
    certificate_email_teachers($course, $certificate, $certissue, $cm);
    certificate_email_others($course, $certificate, $certissue, $cm);
    return $certissue;
}
Example #5
0
function certificate_issue($course, $certificate, $certrecord, $cm)
{
    if ($certificate->printgrade > 0) {
        if ($certificate->printgrade == 1) {
            $grade = certificate_print_course_grade($course);
        } else {
            if ($certificate->printgrade > 1) {
                $grade = certificate_print_mod_grade($course, $certificate->printgrade);
            }
        }
        if ($certificate->gradefmt == 1) {
            $certrecord->reportgrade = addslashes($grade->percentage);
        }
        if ($certificate->gradefmt == 2) {
            $certrecord->reportgrade = addslashes($grade->points);
        }
        if ($certificate->gradefmt == 3) {
            $certrecord->reportgrade = addslashes($grade->letter);
        }
    }
    $date = certificate_generate_date($certificate, $course);
    $certrecord->certdate = $date;
    update_record('certificate_issues', addslashes_object($certrecord));
    certificate_email_teachers($course, $certificate, $certrecord, $cm);
    certificate_email_others($course, $certificate, $certrecord, $cm);
}