saralem.com

יוסף

get_charset_collate();

// Students table
$table_students = $wpdb->prefix . 'passover_students';
$sql1 = "CREATE TABLE $table_students (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
classroom varchar(50) NOT NULL,
personal_code varchar(10) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";

// Tasks table
$table_tasks = $wpdb->prefix . 'passover_tasks';
$sql2 = "CREATE TABLE $table_tasks (
id mediumint(9) NOT NULL AUTO_INCREMENT,
task_description text NOT NULL,
classroom varchar(50) NOT NULL,
is_required tinyint(1) DEFAULT 0,
active_date date DEFAULT NULL,
PRIMARY KEY (id)
) $charset_collate;";

// Submissions table
$table_submissions = $wpdb->prefix . 'passover_submissions';
$sql3 = "CREATE TABLE $table_submissions (
id mediumint(9) NOT NULL AUTO_INCREMENT,
student_id mediumint(9) NOT NULL,
task_ids text NOT NULL,
submission_date datetime DEFAULT CURRENT_TIMESTAMP,
won_prize tinyint(1) DEFAULT 0,
prize_name varchar(255) DEFAULT ",
PRIMARY KEY (id)
) $charset_collate;";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql1);
dbDelta($sql2);
dbDelta($sql3);
}

/**
* Enqueue necessary scripts and styles
*/
public function enqueue_scripts() {
// Register and enqueue CSS
wp_enqueue_style('passover-tasks-style', plugin_dir_url(__FILE__) . 'css/passover-tasks.css', array(), '1.0.0');

// Register and enqueue Font Awesome
wp_enqueue_style('font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css');

// Register and enqueue Google Font
wp_enqueue_style('google-font-heebo', 'https://fonts.googleapis.com/css2?family=Heebo:wght@400;500;700&display=swap');

// Register and enqueue main JavaScript
wp_enqueue_script('passover-tasks-script', plugin_dir_url(__FILE__) . 'js/passover-tasks.js', array('jquery'), '1.0.0', true);

// Pass AJAX URL to JavaScript
wp_localize_script('passover-tasks-script', 'passover_tasks_ajax', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('passover_tasks_nonce')
));
}

/**
* Render the task form via shortcode
*/
public function render_task_form() {
ob_start();
include(plugin_dir_path(__FILE__) . 'templates/task-form.php');
return ob_get_clean();
}

/**
* AJAX handler to verify a student by personal code
*/
public function ajax_verify_student() {
check_ajax_referer('passover_tasks_nonce', 'nonce');

$code = isset($_POST['code']) ? sanitize_text_field($_POST['code']) : ";

if (!$code || strlen($code) !== 3 || !is_numeric($code)) {
wp_send_json_error(array('message' => 'Invalid code format'));
wp_die();
}

global $wpdb;
$table_students = $wpdb->prefix . 'passover_students';
$table_submissions = $wpdb->prefix . 'passover_submissions';

// Get student data
$student = $wpdb->get_row(
$wpdb->prepare("SELECT * FROM $table_students WHERE personal_code = %s", $code),
ARRAY_A
);

if (!$student) {
wp_send_json_error(array('message' => 'Student not found'));
wp_die();
}

// Check if student already completed tasks today
$today = date('Y-m-d');
$completed_today = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM $table_submissions
WHERE student_id = %d AND DATE(submission_date) = %s",
$student['id'], $today
)
);

wp_send_json_success(array(
'success' => true,
'student' => array(
'id' => $student['id'],
'name' => $student['name'],
'classroom' => $student['classroom'],
'code' => $student['personal_code']
),
'completedToday' => $completed_today > 0
));

wp_die();
}

/**
* AJAX handler to get tasks for a classroom
*/
public function ajax_get_tasks() {
check_ajax_referer('passover_tasks_nonce', 'nonce');

$classroom = isset($_POST['classroom']) ? sanitize_text_field($_POST['classroom']) : ";

if (!$classroom) {
wp_send_json_error(array('message' => 'Classroom is required'));
wp_die();
}

global $wpdb;
$table_tasks = $wpdb->prefix . 'passover_tasks';
$today = date('Y-m-d');

// Get today's tasks for the classroom
$tasks = $wpdb->get_results(
$wpdb->prepare(
"SELECT id, task_description as task, IF(is_required=1, 'כן', 'לא') as required
FROM $table_tasks
WHERE (classroom = %s OR classroom = 'all')
AND (active_date = %s OR active_date IS NULL)
AND active_date IS NOT NULL
ORDER BY is_required DESC, id ASC",
$classroom, $today
),
ARRAY_A
);

wp_send_json_success(array(
'success' => true,
'tasks' => $tasks
));

wp_die();
}

/**
* AJAX handler to submit completed tasks
*/
public function ajax_submit_tasks() {
check_ajax_referer('passover_tasks_nonce', 'nonce');

$student_id = isset($_POST['student_id']) ? intval($_POST['student_id']) : 0;
$completed_tasks = isset($_POST['completed_tasks']) ? sanitize_text_field($_POST['completed_tasks']) : ";

if (!$student_id || !$completed_tasks) {
wp_send_json_error(array('message' => 'Missing required data'));
wp_die();
}

global $wpdb;
$table_submissions = $wpdb->prefix . 'passover_submissions';

// Check if won a prize (simplified logic – 10% chance of winning)
$won = (rand(1, 10) === 1);
$prize_name = ";

if ($won) {
// List of sample prizes
$prizes = array(
'חבילת ממתקים',
'כרטיס לקולנוע',
'ספר קריאה',
'משחק קופסה',
'שובר מתנה'
);
$prize_name = $prizes[array_rand($prizes)];
}

// Insert submission
$result = $wpdb->insert(
$table_submissions,
array(
'student_id' => $student_id,
'task_ids' => $completed_tasks,
'won_prize' => $won ? 1 : 0,
'prize_name' => $prize_name
)
);

if ($result === false) {
wp_send_json_error(array('message' => 'Failed to save submission'));
wp_die();
}

wp_send_json_success(array(
'success' => true,
'won' => $won,
'prizeName' => $prize_name
));

wp_die();
}
}

// Initialize the plugin
$passover_tasks = new PassoverTasks();

אני כאן בשבילכם!

ממריאים!!