Install


Source

File: classes/Install.php

class Install {

	public static function instance() {
		static $instance = null;

		// Only run these methods if they haven't been run previously
		if ( null === $instance ) {
			$instance = new self();
			$instance->create_databases();
			$instance->create_pages();
			$instance->roles_and_permissions();
			$instance->set_default_options();
		}

		// Always return the instance
		return $instance;
	}


	/**
	 * Create databases required by DozentLMS
	 *
	 * This method fires during DozentLMS activation
	 *
	 * @since DozentLMS 1.0.0
	 *
	 * @see create_databases();
	 */
	public function create_databases() {
		global $wpdb;

		$prefix          = $wpdb->prefix;
		$charset_collate = $wpdb->get_charset_collate();

		if ( ! function_exists( 'dbDelta' ) ) {
			require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
		}

		/**
		 * Database Table for Course Complete
		 *
		 * @since DozentLMS 1.0.0
		 */

		$completes_table = "CREATE TABLE IF NOT EXISTS {$prefix}dozent_completes (
            complete_id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
            user_id int(11) DEFAULT NULL,
            completed_course_id int(11) DEFAULT NULL,
            course_id int(11) DEFAULT NULL,
            content_id int(11) DEFAULT NULL,
            completed_at datetime DEFAULT NULL,
            KEY user_id (user_id),
            KEY completed_course_id (completed_course_id),
            KEY course_id (course_id),
            KEY content_id (content_id)
		) {$charset_collate};";

		dbDelta( $completes_table );

		/**
		 * Database Table for Ratings and Reviews
		 *
		 * @since DozentLMS 1.0.0
		 */

		$reviews_table = "CREATE TABLE IF NOT EXISTS {$prefix}dozent_reviews (
            ID bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
            user_id int(11) DEFAULT NULL,
            course_id int(11) DEFAULT NULL,
            review_id int(11) DEFAULT '0',
            review text DEFAULT NULL,
            rating tinyint(4) DEFAULT NULL,
            status tinyint(1) DEFAULT '1',
            created_at datetime DEFAULT NULL,
            updated_at datetime DEFAULT NULL,
            KEY user_id (user_id),
			KEY course_id (course_id),
			KEY review_id (review_id),
			KEY rating (rating) 
        ) {$charset_collate};";

		dbDelta( $reviews_table );

		/**
		 * Database Table for Instructor courses permission
		 *
		 * @since DozentLMS 1.0.0
		 */

		$instructor_courses_table = "CREATE TABLE IF NOT EXISTS {$prefix}dozent_instructor_courses (
            ID bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
			course_id int(11) DEFAULT NULL,
			instructor_id int(11) DEFAULT NULL,
			permissions text DEFAULT NULL,
			added_at datetime NULL DEFAULT NULL,
			KEY course_id (course_id),
			KEY instructor_id (instructor_id)
        ) {$charset_collate};";

		dbDelta( $instructor_courses_table );

		/**
		 * Create database for enrolments
		 */

		$enrols_table = "CREATE TABLE IF NOT EXISTS {$prefix}dozent_enrols (
            ID bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
			course_id int(11) unsigned DEFAULT NULL,
			user_id int(11) unsigned DEFAULT NULL,
			order_id int(11) unsigned DEFAULT NULL,
			order_by_product_id int(11) unsigned DEFAULT NULL,
			status varchar(30) DEFAULT NULL,
			expired_at datetime NULL DEFAULT NULL,
			created_at datetime NULL DEFAULT NULL,
            KEY  course_id (course_id),
            KEY  user_id (user_id),
            KEY  order_id (order_id),
            KEY  order_by_product_id (order_by_product_id),
            KEY  status (status),
            KEY  expired_at (expired_at),
            KEY  created_at (created_at)
        ) {$charset_collate};";

		dbDelta( $enrols_table );

		/**
		 * Instructors Earnings table
		 */

		$earning_table = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}dozent_earnings (
			ID bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
			user_id int(11) unsigned DEFAULT NULL,
			course_id int(11) unsigned DEFAULT NULL,
			order_id int(11) unsigned DEFAULT NULL,
			order_status varchar(50) DEFAULT NULL,
			total_courses_under_order int(11) DEFAULT NULL,
			course_price_total decimal(16,2) DEFAULT NULL,
			per_course_price decimal(16,2) DEFAULT NULL,
			instructor_amount decimal(16,2) DEFAULT NULL,
			instructor_rate decimal(16,2) DEFAULT NULL,
			admin_amount decimal(16,2) DEFAULT NULL,
			admin_rate decimal(16,2) DEFAULT NULL,
			commission_type varchar(20) DEFAULT NULL,
			deduct_fees_rate decimal(16,2) DEFAULT NULL,
			deduct_fees_amount decimal(16,2) DEFAULT NULL,
			deduct_fees_name varchar(250) DEFAULT NULL,
			deduct_fees_type varchar(20) DEFAULT NULL,
			process_by varchar(20) DEFAULT NULL,
			raw_data text DEFAULT NULL,
			created_at datetime DEFAULT NULL,
            KEY  user_id (user_id),
            KEY  course_id (course_id),
            KEY  order_id (order_id),
            KEY  order_status (order_status),
            KEY  created_at (created_at)
		) {$charset_collate};";

		dbDelta( $earning_table );

		/**
		 * Withdraw by Instructor
		 */

		$withdraws_table = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}dozent_withdrawal (
			withdraw_id bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
			user_id int(11) DEFAULT NULL,
			amount decimal(16,2) DEFAULT NULL,
			method_data text DEFAULT NULL,
			status varchar(50) DEFAULT NULL,
			updated_at datetime DEFAULT NULL,
			created_at datetime DEFAULT NULL,
            KEY  user_id (user_id),
            KEY  amount (amount),
            KEY  status (status),
            KEY  created_at (created_at)
		) {$charset_collate};";

		dbDelta( $withdraws_table );


		/**
		 * Dozent Quiz Questions
		 */

		$quiz_questions_table = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}dozent_quiz_questions (
			question_id bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
			user_id int(11) DEFAULT NULL,
			quiz_id int(11) DEFAULT NULL,
			title varchar(191) DEFAULT NULL,
			image_id int(11) DEFAULT NULL,  
			question_type varchar(20) DEFAULT NULL,
			mixed_value text DEFAULT NULL,
			score decimal(8,2) DEFAULT NULL,
			sort_order int(11) DEFAULT NULL,
            KEY  quiz_id (quiz_id)
		) {$charset_collate};";

		dbDelta( $quiz_questions_table );

		/**
		 * Dozent Quiz Question Options
		 */

		$quiz_options_table = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}dozent_quiz_question_options (
			option_id bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
			question_id int(11) DEFAULT NULL,
			title varchar(191) DEFAULT NULL,
			image_id int(11) DEFAULT NULL,  
			display_preference varchar(20) DEFAULT NULL,
			is_correct int(11) DEFAULT NULL,
			sort_order int(11) DEFAULT NULL,
            KEY  question_id (question_id)
		) {$charset_collate};";

		dbDelta( $quiz_options_table );

		/**
		 * Dozent Quiz Attempt
		 */

		$quiz_options_table = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}dozent_quiz_attempts (
			id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
			course_id int(11) DEFAULT NULL,
			quiz_id int(11) DEFAULT NULL,
			user_id int(11) DEFAULT NULL,
			reviewer_id int(11) DEFAULT NULL,
			questions_limit int(11) DEFAULT NULL,
			total_answered int(11) DEFAULT NULL,
			total_scores decimal(6,2) DEFAULT NULL,
			earned_scores decimal(6,2) DEFAULT NULL,
			passing_percent int(11) DEFAULT NULL,
			earned_percent int(11) DEFAULT NULL,
			status varchar(191) DEFAULT NULL,
			quiz_gradable tinyint(1) DEFAULT '0',
			is_reviewed tinyint(1) DEFAULT '0',
			ended_at timestamp NULL DEFAULT NULL,
			reviewed_at timestamp NULL DEFAULT NULL,
			passed tinyint(4) DEFAULT NULL,
			quiz_option text DEFAULT NULL,
			created_at datetime DEFAULT NULL,
			updated_at datetime DEFAULT NULL,
			KEY  course_id (course_id),
			KEY  quiz_id (quiz_id),
			KEY  user_id (user_id),
			KEY  total_scores (total_scores),
			KEY  earned_scores (earned_scores),
			KEY  status (status)
		) {$charset_collate};";

		dbDelta( $quiz_options_table );

		$quiz_options_table = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}dozent_quiz_answers (
		  	id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
			quiz_id int(11) DEFAULT NULL,
			question_id int(11) DEFAULT NULL,
			user_id int(11) DEFAULT NULL,
			attempt_id int(11) DEFAULT NULL,
			answer text DEFAULT NULL,
			q_type varchar(20) DEFAULT NULL,
			q_score decimal(6,2) DEFAULT NULL,
			earned_scores decimal(6,2) DEFAULT NULL,
			is_correct tinyint(1) DEFAULT '0',
			KEY  quiz_id (quiz_id),
			KEY  question_id (question_id),
			KEY  user_id (user_id),
			KEY  attempt_id (attempt_id),
			KEY  q_score (q_score),
			KEY  earned_scores (earned_scores)
		) {$charset_collate};";

		dbDelta( $quiz_options_table );
	}

	/**
	 * Create Default pages
	 *
	 *
	 * Example usage:
	 *
	 *     create_pages();
	 *
	 * @since DozentLMS 1.0.0
	 *
	 * @see create_pages();
	 *
	 */
	public function create_pages() {
		$dashboard_args    = array(
			'post_title'   => __( 'Dashboard', 'dozent' ),
			'post_content' => '',
			'post_type'    => 'page',
			'post_status'  => 'publish',
		);
		$dashboard_page_id = wp_insert_post( $dashboard_args );
		dozent_update_option( 'dozent_dashboard_page_id', $dashboard_page_id );

		$user_signup_page_args = array(
			'post_title'   => __( 'User Signup', 'dozent' ),
			'post_content' => '',
			'post_type'    => 'page',
			'post_status'  => 'publish',
		);
		$user_signup_page_id   = wp_insert_post( $user_signup_page_args );
		dozent_update_option( 'dozent_user_signup_page_id', $user_signup_page_id );
	}

	/**
	 * Set Default roles and permission during DozentLMS activation
	 *
	 *
	 * Example usage:
	 *
	 *     roles_and_permissions();
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 * @see roles_and_permissions();
	 *
	 */

	public static function roles_and_permissions() {

		$instructor_role = DOZENT_INSTRUCTOR_ROLE;
		$permissions     = [
			//Manage Instructor
			'dozent_instructor',

			//Posts Type Permission
			'edit_course',
			'read_course',
			'delete_course',
			'delete_courses',
			'edit_courses',
			'edit_others_courses',
			'read_private_courses',
		];

		remove_role( $instructor_role );
		add_role( $instructor_role, DOZENT_INSTRUCTOR_ROLE_NAME, $permissions );

		$instructor = get_role( $instructor_role );
		if ( $instructor ) {

			$instructor_cap = array(
				'edit_posts',
				'read',
				'upload_files',
			);

			$instructor_cap = array_merge( $instructor_cap, $permissions );

			$can_publish_course = (bool) dozent_get_option( 'instructor_can_publish_course' );
			if ( $can_publish_course ) {
				$instructor_cap[] = 'publish_courses';
			}

			foreach ( $instructor_cap as $cap ) {
				$instructor->add_cap( $cap );
			}
		}

		$administrator = get_role( 'administrator' );
		if ( $administrator ) {

			$permissions[] = 'publish_courses';
			foreach ( $permissions as $cap ) {
				$administrator->add_cap( $cap );
			}
		}

		/**
		 * Add Instructor role to administrator
		 */
		if ( current_user_can( 'administrator' ) ) {
			dozent_instructor_approve( get_current_user_id() );
		}

	}


	/**
	 * Set default option during DozentLMS activation.
	 *
	 *
	 * Example usage:
	 *
	 *     set_default_options();
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 * @see set_default_options();
	 */

	public function set_default_options() {
		$options = [
			'redirect_back_after_logout' => '1',
			'enable_focus_mode' => '1',
			'enable_discussion' => '1',
			'pagination_per_page' => '20',
			'show_accept_terms_checkbox' => '1',
			'login_error_message' => __( 'Incorrect username or password.', 'dozent' ),
			'site_logo_id' => '',
			'course_base_slug' => 'courses',
			'course_category_base_slug' => 'course-category',
			'lecture_base_slug' => 'lecture',
			'assignment_base_slug' => 'assignment',
			'quiz_base_slug' => 'quiz',
			'dozent_dashboard_page_id' => '',
			'dozent_user_signup_page_id' => '',
			'course_archive_page_id' => '',
			'terms_and_condition_page_id' => '',
			'privacy_policy_page_id' => '',
			'course_archive_page' => '-1',
			'courses_col_per_row' => '3',
			'courses_per_page' => '20',
			'enable_dozent_earning' => '1',
			'earning_admin_commission' => '40',
			'earning_instructor_commission' => '60',
			'dozent_earning_fees' => [
				'enable_fees_deducting' => '1',
				'fees_name' => 'Platform Charge',
				'fees_amount' => '20',
				'fees_type' => 'percent',
			],
			'statement_show_per_page' => '20',
			'min_withdraw_amount' => '10',
		];

		update_option('dozent_options', $options);
	}


}

Methods