<?php
/**
 * This email will be sent to a teacher when a course is assigned to them.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

if ( class_exists( 'Sensei_Email_Teacher_New_Course_Assignment', false ) ) {
	return;
}

/**
 * Teacher Started Course
 *
 * An email sent to the teacher when one of their students starts a course.
 *
 * @package Users
 * @author Automattic
 *
 * @since       1.6.0
 */
class Sensei_Email_Teacher_New_Course_Assignment {

	var $template;
	var $subject;
	var $heading;
	var $recipient;
	var $learner;
	var $teacher;

	/**
	 * Constructor
	 *
	 * @since 1.8.0
	 * @access public
	 */
	function __construct() {

		$this->template = 'teacher-new-course-assignment';
		return;
	}

	/**
	 * Trigger email sending.
	 *
	 * @param int $teacher_id The teacher ID.
	 * @param int $course_id The course ID.
	 */
	function trigger( $teacher_id = 0, $course_id = 0 ) {
		global $sensei_email_data;

		$this->teacher   = new WP_User( $teacher_id );
		$this->recipient = stripslashes( $this->teacher->user_email );

		/**
		 * Fires before the mail is sent.
		 *
		 * @hook sensei_before_mail
		 *
		 * @param {string} $recipient The recipient email.
		 */
		do_action( 'sensei_before_mail', $this->recipient );

		$this->heading = apply_filters( 'sensei_email_heading', __( 'Course assigned to you', 'sensei-lms' ), $this->template );
		$this->subject = apply_filters( 'sensei_email_subject', __( 'New course assigned to you', 'sensei-lms' ), $this->template );

		// course edit link
		$course_edit_link = admin_url( 'post.php?post=' . $course_id . '&action=edit' );

		// Course name
		$course = get_post( $course_id );
		// Construct data array
		$sensei_email_data = apply_filters(
			'sensei_email_data',
			array(
				'template'         => $this->template,
				'heading'          => $this->heading,
				'teacher_id'       => $teacher_id,
				'course_id'        => $course_id,
				'course_name'      => $course->post_title,
				'course_edit_link' => $course_edit_link,
			),
			$this->template
		);

		// Send mail
		Sensei()->emails->send( $this->recipient, $this->subject, Sensei()->emails->get_content( $this->template ) );

		/**
		 * Fires after the mail is sent.
		 *
		 * @hook sensei_after_sending_email
		 */
		do_action( 'sensei_after_sending_email' );
	}
}
