public function test_certificate_get_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);
     certificate_get_issue($this->course, $studentuser, $certificate, $coursemodule);
     $certificateteacherarray = certificate_get_teachers(null, $studentuser, $coursemodule, $coursemodule);
     // Acquire the ids (not all attributes are equal considering db transaction can have auto values).
     $teacheruserids = array_map(create_function('$t', 'return $t->id;'), $teacheruserarray);
     $certificateteacherids = array_map(create_function('$c', 'return $c->id;'), $certificateteacherarray);
     /**
      * Ensure that two arrays have one-to-one correspondence, that is each
      * is a subset of each other.
      */
     $emptyarray = array();
     $this->assertEquals(array_diff($teacheruserids, $certificateteacherids), $emptyarray);
 }
Example #2
0
/**
 * Alerts teachers by email of received certificates. First checks
 * whether the option to email teachers is set for this certificate.
 *
 * @param stdClass $course
 * @param stdClass $certificate
 * @param stdClass $certrecord
 * @param stdClass $cm course module
 * @return null
 */
function certificate_email_teachers($course, $certificate, $certrecord, $cm)
{
    global $USER, $CFG, $DB;
    if ($certificate->emailteachers == 0) {
        // No need to do anything
        return;
    }
    $student = $certrecord->studentname;
    $user = $DB->get_record('user', array('id' => $certrecord->userid));
    if ($teachers = certificate_get_teachers($certificate, $user, $course, $cm)) {
        $strawarded = get_string('awarded', 'certificate');
        foreach ($teachers as $teacher) {
            $info = new stdClass();
            $info->student = $student;
            $info->course = format_string($course->fullname, true);
            $info->certificate = format_string($certificate->name, true);
            $info->url = $CFG->wwwroot . '/mod/certificate/report.php?id=' . $cm->id;
            $from = $student;
            $postsubject = $strawarded . ': ' . $info->student . ' -> ' . $certificate->name;
            $posttext = certificate_email_teachers_text($info);
            $posthtml = $teacher->mailformat == 1 ? certificate_email_teachers_html($info) : '';
            @email_to_user($teacher, $from, $postsubject, $posttext, $posthtml);
            // If it fails, oh well, too bad.
        }
    }
}