<?php
if (!defined('ABSPATH')) exit();
if (!class_exists('DbIndeedAbstract')) require_once ULP_PATH . 'classes/Abstracts/DbIndeedAbstract.class.php';
if (class_exists('DbNotificationsUlp')) return;
class DbNotificationsUlp extends DbIndeedAbstract{
	/**
	 * @var string
	 */
	protected $table = '';

	/**
	 * @param none
	 * @return none
	 */
	public function __construct(){
		global $wpdb;
		$this->table = $wpdb->prefix . 'ulp_notifications';
	}

  public function getByTypeAndCourseId($course_id=0, $type=''){
			if ($course_id==0) $course_id = -1;
			$course_id = esc_sql($course_id);
			$type = esc_sql($type);
			$data = $this->getRow('subject, message', " type='$type' AND course_id=$course_id AND status=1 ORDER BY id DESC LIMIT 1");
			if (empty($data)){
					$course_id = -1;
					$data = $this->getRow('subject, message', " type='$type' AND course_id=$course_id AND status=1 ORDER BY id DESC LIMIT 1");
			}
			if (isset($data['message'])){
					$data['message'] = indeed_format_str_like_wp( stripslashes( $data['message'] ) );
			}
			if (isset($data['subject'])){
					$data['subject'] = stripslashes( $data['subject'] );
			}
			return $data;
  }

	public function getPushoverByCourseIdAndType($course_id=0, $type=''){
			if ($course_id==0) $course_id = -1;
			$course_id = esc_sql($course_id);
			$type = esc_sql($type);
			$data = $this->getRow('subject, pushover_message', " type='$type' AND course_id=$course_id AND pushover_status=1 ORDER BY id DESC LIMIT 1");
			if (empty($data)){
					$course_id = -1;
					$data = $this->getRow('subject, pushover_message', " type='$type' AND course_id=$course_id AND pushover_status=1 ORDER BY id DESC LIMIT 1");
			}
			return $data;
	}

	public function getNotificationById($id=0){
			$id = esc_sql($id);
			$array = $this->getRow( " `id`, `type`, `course_id`, `subject`, `message`, `pushover_message`, `pushover_status`, `status` ", "id=$id");
			if (empty($array)){
					/// DEFAULTS
					$array = array(
													'type' => '',
													'subject' => '',
													'message' => '',
													'pushover_message' => '',
													'pushover_status' => 0,
													'course_id' => 0,
													'status' => 0,
													'id' => 0,
					);
			}
			$array['message'] = indeed_format_str_like_wp( stripslashes( $array['message'] ) );
			$array['subject'] = stripslashes( $array['subject'] );
			return $array;
	}

  public function save($input_data=array()){
    $do_update = false;
		foreach ($input_data as $k=>$v){
				$input_data [$k] = esc_sql($v);
		}
    if (!empty($input_data['id'])){
      $do_update = $this->getById($input_data['id']);
    }

		do_action( 'ulp_save_notification_action', $input_data );

    if ($do_update){
        return $this->update("type='{$input_data['type']}',
															course_id={$input_data['course_id']},
															subject='{$input_data['subject']}',
															message='{$input_data['message']}',
															pushover_message='{$input_data['pushover_message']}',
															pushover_status='{$input_data['pushover_status']}',
															status={$input_data['status']}",
															"id={$input_data['id']}");
    } else {
        return $this->insert("null,
															'{$input_data['type']}',
															{$input_data['course_id']},
															'{$input_data['subject']}',
															'{$input_data['message']}',
															'{$input_data['pushover_message']}',
															'{$input_data['pushover_status']}',
															{$input_data['status']}");
    }
  }

  public function delete($id=0){
		$id = esc_sql($id);
    return parent::delete("id=$id");
  }

  public function getAll(){
      return $this->getResults( " `id`, `type`, `course_id`, `subject`, `message`, `pushover_message`, `pushover_status`, `status` ", '1=1');
  }

  public function getById($id=0){
			$id = esc_sql($id);
      return $this->getRow( " `id`, `type`, `course_id`, `subject`, `message`, `pushover_message`, `pushover_status`, `status` ", "id=$id");
  }

	public function getActionTypes($type='admin'){
		/// todo: admin_before_course_expires && admin_scheduled_lesson_user
		$data = array(
				'admin' => [
								'admin_user_become_instructor' 			=> __('User wants to be Instructor', 'ulp'),
								'admin_user_enroll_course' 					=> __('User enrolls into a course', 'ulp'),
								'admin_user_complete_course' 				=> __('User completes a course', 'ulp'),
								'admin_user_complete_lesson' 				=> __('User completes a lesson', 'ulp'),
								/// 'admin_scheduled_lesson_user' => __('A scheduled lesson is available to a user', 'ulp'),
								'admin_user_pass_quiz' 							=> __('User passes a quiz', 'ulp'),
								'admin_user_fails_quiz' 						=> __('User fails a quiz', 'ulp'),
								'admin_user_complete_quiz' 					=> __('User completes a quiz', 'ulp'),
								'admin_instructor_create_course'		=> __('Instructor create a course', 'ulp'),
								'admin_instructor_edit_course'			=> __('Instructor edit a course', 'ulp'),
								/// 'admin_before_course_expires' => __("'X' days before a course expires", 'ulp'),
					],
				'student' => [
								'user_enroll_course' 				=> __('User enrolls into a course', 'ulp'),
								'user_complete_course' 			=> __('User completes a course', 'ulp'),
								'user_complete_lesson' 			=> __('User completes a lesson', 'ulp'),
								'user_pass_quiz' 						=> __('User passes a quiz', 'ulp'),
								'user_fails_quiz' 					=> __('User fails a quiz', 'ulp'),
								'user_complete_quiz' 				=> __('User completes a quiz', 'ulp'),
								'user_bank_transfer' 				=> __('Bank Transfer Payment Details', 'ulp'),
				],
				'announcements' => [
								'admin_user_comment_on_announcement' 	=> __('Author: User commentted on Announcement', 'ulp'),
								'user_new_announcement' 							=> __('Student: New Announcement submitted', 'ulp'),
				],
				'qanda' => [
								'student_ask_question' 										=> __('Author: Student ask a new Question', 'ulp'),
								'new_reply_on_question_from_instructor' 	=> __('Student: Instructor reply to student Question', 'ulp'),
								'new_reply_on_question' 									=> __('Author: Student comment on Question', 'ulp'),
				],
				'others'	=> [
								'instructor_user_enroll_course'								=> __('Instructor: Student enrolls into a course', 'ulp'),
								'instructor_admin_has_publish_your_course'		=> __('Instructor: Your course has been published', 'ulp'),
				],
		);
		return $data[$type];
	}

	public function get_standard_by_type($type=''){
			$template = ['subject' => '', 'content' => ''];
			switch ($type){
				case 'admin_user_become_instructor':
					$template = [
						'subject' => '{blogname}: User {username} wants to become an Instructor',
						'content' => '<html><head></head><body>
														 <p>Hello,</p>
<p></p>
														 <p>User: {username} wants to become an instructor.</p>
<p></p>
														 <p>To make him an instructor, please go to {admin_user_url_page} and set his Wp Role to Instructor.</p>
<p></p>
														<p> Have a nice day!</p>
					</body></html>'
					];
					break;
				case 'admin_user_enroll_course':
					$template = [
								'subject' => ' {blogname}: User {username} has just enrolled in the {course_name} course ',
								'content' => '
														<p>Hello,</p>
<p></p>
														<p>User: {username} has just enrolled in the following course: {course_name}.</p>
<p></p>
														<p>Have a nice day!</p>
					'
					];
					break;
				case 'admin_user_complete_course':
					$template = [
							'subject' => '{blogname}: User {username} has just completed the {course_name} course',
							'content' => '
										<p>Hello,</p>
										<p></p>
										<p>User: {username} has just finished the {course_name} course.</p>
										<p></p>
										<p>Have a nice day!</p>
							',
					];
					break;
				case 'admin_user_complete_lesson':
					$template = [
							'subject' => '{blogname}: User {username} has just completed the {lesson_title} lesson',
							'content' => '
									<p>Hello,</p>
									<p></p>
									<p>User: {username} has just completed the {lesson_title} lesson from the {course_name} course.</p>
									<p></p>
									<p>Have a nice day!</p>
							',
					];
					break;
				case 'admin_scheduled_lesson_user':
					$template = [
							'subject' => '{blogname}: {lesson_title} lesson has just become available for user {username}',
							'content' => '
										<p>Hello,</p>
										<p></p>
										<p>Lesson {shortcode} has become available for user: {username}.</p>
										<p></p>
										<p>Have a nice day!</p>
							',
					];
					break;
					case 'admin_user_pass_quiz':
						$template = [
								'subject' => '{blogname}: User {username} has just passed the {quiz_title} quiz',
								'content' => '<p>Hello,</p>
<p></p>
<p>User {username} has just passed the {quiz_title} quiz with {quiz_grade} grade.</p>
<p></p>
<p>Have a nice day!</p>
',
						];
						break;
						case 'admin_user_fails_quiz':
							$template = [
									'subject' => '{blogname}: User {username} has not passed the {quiz_title} quiz',
									'content' => '
											<p>Hello,</p>
											<p></p>
											<p>User {username} scored {quiz_grade} grade on the quiz {quiz_title}, but it wasn’t enough to pass.</p>
											<p></p>
											<p>Have a nice day!</p>
									',
							];
							break;
						case 'admin_user_complete_quiz':
							$template = [
										'subject' => '{blogname}: User {username} has just completed the {quiz_title} quiz',
										'content' => '<p>Hello,</p>
<p></p>
<p>User {username} has just finished completing the quiz {quiz_title}.</p>
<p></p>
<p>Have a nice day!</p>
',
							];
							break;
						case 'admin_before_course_expires':
							$template = [
										'subject' => '{blogname}: {course_name} course will expire soon',
										'content' => '
											<p>Hello,</p>
											<p></p>
											<p>The course {course_name} will expire in {shortcode} for user {username}.<p>
											<p></p>
											<p>Have a nice day!</p>
										',
							];
							break;
						case 'admin_instructor_create_course':
							$template = [
										'subject' => '{blogname}: {username} has created a new course',
										'content' => '
											<p>Hello,</p>
											<p></p>
											<p>{username} has created the following course: {course_name}.<p>
											<p></p>
											<p>Have a nice day!</p>
										',
							];
							break;
						case 'admin_instructor_edit_course':
							$template = [
										'subject' => '{blogname}: {username} has updated a {course_name}',
										'content' => '
											<p>Hello,</p>
											<p></p>
											<p>{username} has updated a {course_name}.<p>
											<p></p>
											<p>Have a nice day!</p>
										',
							];
							break;

						case 'user_enroll_course':
							$template = [
										'subject' => '{blogname}: Successfully enrolled in the course',
										'content' => '
											<p>Hello {username},</p>
											<p></p>
											<p>You have successfully enrolled in the {course_name} course.</p>
											<p></p>
											<p>Have a nice day,</p>
											<p>{blogname}</p>
										',
							];
							break;
						case 'user_complete_course':
							$template = [
											'subject' => ' {blogname}: Finished course',
											'content' => '
												<p>Congratulations {username},</p>
												<p></p>
												<p>You have just finished the {course_name} course.</p>
												<p></p>
												<p>Have a nice day,</p>
												<p>{blogname}</p>
											',
							];
							break;
						case 'user_complete_lesson':
							$template = [
											'subject' => '{blogname}: Completed lesson',
											'content' => '
												<p>Hello {username},</p>
												<p></p>
												<p>You have just finished the {lesson_title} lesson part of the {course_name} course.</p>
												<p></p>
												<p>Have a nice day,</p>
												<p>{blogname}</p>
											',
							];
							break;
						case 'user_pass_quiz':
							$template = [
												'subject' => '{blogname}: Quiz results',
												'content' => '
														<p>Congratulations {username},</p>
														<p></p>
														<p>You have passed the quiz {quiz_title} with {quiz_grade} grade.</p>
														<p></p>
														<p>Keep up the good work,</p>
														<p>{blogname}</p>
												',
							];
							break;
						case 'user_fails_quiz':
							$template = [
													'subject' => ' {blogname}: Quiz results',
													'content' => '
															<p>Hello {username},</p>
															<p></p>
															<p>Sadly, your {quiz_grade} grade for the {quiz_title} quiz was not enough to pass.</p>
															<p></p>
															<p>Study some more and give it another try.</p>
															<p></p>
															<p>Have a nice day,</p>
															<p>{blogname}</p>
													',
							];
							break;
						case 'user_complete_quiz':
							$template = [
													'subject' => 'Subject: {blogname}: Quiz completed',
													'content' => '
															<p>Hello {username},
															<p>
															<p>You have just finished the {quiz_title} quiz.</p>
															<p></p>
															<p>Have a nice day,</p>
															<p>{blogname}</p>
													',
							];
							break;
						case 'user_bank_transfer':
							$template = [
													'subject' => 'Subject: {blogname}: Payment Inform',
													'content' => '
															<p>Hi {username},</p>
															<br/> <br/>
															<p>Please proceed the bank transfer payment for: {currency}{amount}</p>
															<p><strong>Payment Details:</strong> Subscription {course_name} for {username} with Identification: {user_id}_{course_id}</p><br/> <p><strong>Bank Details:</strong> IBAN:xxxxxxxxxxxxxxxxxxxx Bank NAME</p>
													',
							];
							break;
						case 'admin_user_comment_on_announcement':
							$template = [
													'subject' => 'Subject: {blogname} - Student commentted on your announcement',
													'content' => '<p>Student <strong>{student_name}</strong> post a new comment on your announcement:<p>
<br/>
<p><a href="{announcement_link}">{announcement_title}</a></p>
<br/><br/>
<p>Comment:</p>																				<p><i>{comment_content}</i></p>',
							];
							break;
						case 'user_new_announcement':
							$template = [
													'subject' => 'Subject: {blogname} - New announcement: {announcement_title}',
													'content' => '<p>Hi {first_name} {last_name},</p>
<br/>																				<p><strong>{author_name}</strong> made a new announcement on <a href="{course_link}">{course_name}</a>.</p>
<br/>													 							<p><a href="{announcement_link}"><strong>{announcement_title}</strong></a></p>
																				<p>{announcement_content}</p>',
							];
							break;
						case 'student_ask_question':
							$template = [
													'subject' => 'Subject: {blogname} - Student ask new question: {qanda_title}',
													'content' => '<p>Hi,</p>
<br/>																				<p><strong>{student_name}</strong> ask a new question on <a href="{course_link}">{course_name}</a>.</p>
<br/>													 							<p><a href="{qanda_link}"><strong>{qanda_title}</strong></a></p>
																				<p>{qanda_content}</p>',
							];
							break;
						case 'new_reply_on_question_from_instructor':
							$template = [
													'subject' => 'Subject: {blogname} - New reply on question: {qanda_title}',
													'content' => '<p>Hi {user_full_name},</p>
<br/>																				<p>Instructor reply on your question from <a href="{course_link}">{course_name}</a>.</p>
<br/>													 							<p><a href="{qanda_link}"><strong>{qanda_title}</strong></a></p>
<br/>
<p>Reply:</p>																				<p>{comment_content}</p>',
							];
							break;
						case 'new_reply_on_question':
							$template = [
													'subject' => 'Subject: {blogname} - New reply on question: {qanda_title}',
													'content' => '<p>Hi,</p>
<br/>																				<p>Student <strong>{user_full_name}</strong> reply on his question from <a href="{course_link}">{course_name}</a>.</p>
<br/>													 							<p><a href="{qanda_link}"><strong>{qanda_title}</strong></a></p>
<br/>
<p>Reply:</p>																				<p>{comment_content}</p>',
							];
							break;
						case 'instructor_user_enroll_course':
							$template = [
										'subject' => ' {blogname}: User {username} has just enrolled in the {course_name} course ',
										'content' => '
																<p>Hello,</p>
		<p></p>
																<p>User: {username} has just enrolled in the following course: {course_name}.</p>
		<p></p>
																<p>Have a nice day!</p>
							'
							];
							break;
						case 'instructor_admin_has_publish_your_course':
							$template = [
										'subject' => ' {blogname}: User {username} your course has been published ',
										'content' => '
																<p>Hello,</p>
		<p></p>
																<p>{username}, your course ({course_name}) has been published .</p>
		<p></p>
																<p>Have a nice day!</p>
							'
							];
							break;
		}
		return $template;
	}
}
