| Server IP : 188.114.97.2 / Your IP : 104.23.197.230 Web Server : Apache/2.4.59 (Debian) System : Linux EDL-STRETCH 4.19.0-27-amd64 #1 SMP Debian 4.19.316-1 (2024-06-25) x86_64 User : edlftp ( 1002) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/nicolasj/www/sms.formationlangues.be/application/controllers/BO/ |
Upload File : |
<?php
/*********************
*********************
Profile Controller : "Profile.php"
Profile Template : "profile.php"
Aernout Guillaume
http://Codes.Solutions
*********************
*********************/
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Profile extends NS_Controller
{
public function __construct()
{
parent::__construct(array('no_cache' => 1));
$this->load->helper('generator');
$this->load->model('users_model');
$this->load->model('student_model');
$this->load->model('teacher_model');
$this->load->model('customer_model');
$this->load->model('address_model');
$this->load->model('event_model');
$this->load->model('course_model');
$this->load->model('profile_model');
$this->load->model('product_transaction_model');
$this->load->model('payment_model');
$this->load->model('setting_model');
$this->load->database();
}
public function index($arg = '')
{
$this->load->helper('url');
$param = $this->uri->segment(3);
$user_id = (int) $this->session->userdata('userID');
$user = $this->users_model->get_entry((int) $user_id);
//if paid and registred
if ($this->users_model->is_paid($user_id)) {
$pending = $this->session->userdata('profile_pending');
if ($pending != null) {
if ($this->switch_profile($pending)) {
if ($this->session->userdata('parent_login') != 1 && $this->session->userdata('student_login') != 1) {
header("Refresh:0");
}
}
}
}
if ($_SESSION['parent_login'] == "1" && $_SESSION['login_type'] == "invited") {
$_SESSION['login_type'] = 'parent';
header("Refresh:0");
}
$data = [
'page_name' => 'profile',
'page_title' => get_phrase('Profile'),
'role' => $this->session->userdata('login_type'),
'language' => $this->profile_model->language(),
'profile' => $this->profile_model->data_profile(),
'products' => $this->customer_model->get_cart(),
'firstname' => $user->first_name,
'lastname' => $user->last_name,
'user' => $user,
'fullfilled_user' => $this->users_model->fullfilled_user((array) $user),
'pdf_uniq_name' => $this->users_model->uniq_user_hash((int) $user->user_id),
'dynamic_assets' => ['intlTelInput']
];
if (isset($param)) {
$data['reset'] = "true";
}
if ($this->session->userdata("login_type") === 'teacher') {
$data['teacher'] = $this->teacher_model->get_entry($user_id);
$data['teacher_can_full_edit'] = ((int) $data['teacher']->locked_profile === 0);
$data['teacher_available_to_generate_contract'] = $this->teacher_model->teacher_available_to_generate_contract($user_id);
$data['teacher_available_to_sign_contract'] = $this->teacher_model->teacher_available_to_sign_contract($user_id);
$data['teacher_book_url'] = $this->setting_model->get_value('teacher_book_url');
} elseif ($this->session->userdata("login_type") === 'parent') {
$data['childrens'] = $this->student_model->get_from_parent_id((int) $user_id);
$data['courses_unassigned'] = $this->customer_model->get_paid_unattribued_courses((int) $user_id);
}
if ($this->session->userdata('new_purchase') && $this->session->userdata('order_id') && $this->session->userdata('order_amount')) {
$data['new_purchase'] = (bool) $this->session->userdata('new_purchase');
$data['order_id'] = (int) $this->session->userdata('order_id');
$data['order_amount'] = (float) $this->session->userdata('order_amount');
}
$this->load->view('backend/index', $data);
}
public function reset()
{
$pass = $this->input->post("pass");
if ($this->profile_model->password_reset($pass)) {
echo "1";
} else {
echo "reset failed";
}
return true;
}
public function edit()
{
$this->load->helper('validator');
$this->continueIfAllowed(['allowed' => ['admin', 'student', 'teacher', 'parent']]);
$data = $this->input->post([
'pic','phone','dob','line_1','line_2','country','state',
'location','postcode','lang','pass','type','firstname','lastname',
'nationality', 'register_number','school','phone_sos', 'teacher_statut',
]);
$errors = [];
$role = $this->session->userdata('login_type');
$userId = (int) $this->session->userdata('userID');
if (empty($userId)) {
echo json_encode(['success' => false, 'errors' => ['Not allowed to edit this profile.']]);
}
$user = $this->users_model->get_entry((int) $userId);
$user_address_id = $this->users_model->get_profile_address();
if ($this->users_model->check_phone($data['phone']) != true || ($data['phone'] == null || $data['phone'] == "")) {
array_push($errors, "Please enter a correct phone number");
}
$update_user_data = [];
// Firstname and lastname can be changed only if empty
if (!empty($user->firstname) && !empty($data['firstname'])) {
$data['firstname'] = '';
}
if (!empty($user->lastname) && !empty($data['lastname'])) {
$data['lastname'] = '';
}
if (!empty($data['nationality']) && in_array($data['nationality'], ['be', 'fr', 'nl', 'de', 'lu', '*'])) {
$update_user_data['nationality'] = $data['nationality'];
}
if ($role !== 'admin') {
//admin dont need those forms
if ($this->users_model->check_address($data['line_1'], $data['line_2'], $data['country'], $data['state'], $data['postcode']) != true) {
array_push($errors, "Please enter a correct address");
}
$dob = str_replace('/', '-', $data['dob']);
$dob = date('Y-m-d', strtotime($dob));
if (empty($data['register_number'])) {
array_push($errors, "Please enter a valid national register number");
}
if ($update_user_data['nationality'] == 'be' && register_number_validity($data['register_number']) === false) {
array_push($errors, "Invalid register national number. Please enter a valid national register number");
}
if ($this->users_model->check_dob($dob) != true || ($data['dob'] == null || $data['dob'] == "") && $role !== 'parent') {
array_push($errors, "Please enter a correct date of birth");
}
}
if ($data['pass'] != '') {
if (!$this->users_model->check_pass($data['pass'])) {
array_push($errors, "Please enter a correct password");
}
}
/* Teacher can change their profile themselves only before contract generation (or sign) */
if ($role === "teacher") {
$teacher = $this->teacher_model->get_entry($user->user_id);
if ($teacher && (int) $teacher->locked_profile === 1) {
$data = array_diff_key($data, ['firstname' => true, 'lastname' => true, 'phone' => true]);
}
}
if (empty($errors)) {
//update user + address
if (!$this->users_model->update_user($user->user_id, $user->email, $data['pass'], $data['firstname'], $data['lastname'], $data['phone'], $data['lang'], '', $dob)) {
return false;
}
if (!empty($update_user_data)) {
$this->users_model->update_entry((int) $user->user_id, $update_user_data); // TODO: regroup all fields in this update request (instead use update_user function)
}
$this->profile_model->set_session_lang(); // set language
$this->db->set(array("first_connection" => "0"));
$this->db->where('user_id', $userId);
$this->db->update('user');
if ($role == "teacher") {
if (!empty($teacher) && (int) $teacher->locked_profile === 0) {
if (!empty($user_address_id) && !empty($this->address_model->get_entry((int) $user_address_id))) {
$this->users_model->update_user_address($user_address_id, $data['line_1'], $data['line_2'], $data['country'], $data['state'], $data['location'], $data['postcode']);
} elseif ($role === "teacher") {
$address_id = $this->users_model->create_user_address($data['line_1'], $data['line_2'], $data['country'], $data['state'], $data['location'], $data['postcode']);
$this->teacher_model->update_entry($user->user_id, ['address_id' => (int) $address_id]);
}
}
return $this->edit_teacher_profile();
} elseif ($role == "student") {
$this->profile_model->update_student_profile($data, $userId);
if (!empty($user_address_id) && !empty($this->address_model->get_entry((int) $user_address_id))) {
$this->users_model->update_user_address($user_address_id, $data['line_1'], $data['line_2'], $data['country'], $data['state'], $data['location'], $data['postcode']);
}
} elseif ($role == "parent") {
$this->profile_model->update_parent_profile($data, $userId);
if (!empty($user_address_id) && !empty($this->address_model->get_entry((int) $user_address_id))) {
$this->users_model->update_user_address($user_address_id, $data['line_1'], $data['line_2'], $data['country'], $data['state'], $data['location'], $data['postcode']);
}
}
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'errors' => $errors]);
}
}
private function edit_teacher_profile()
{
$teacher_id = (int) $this->session->userdata('userID');
$teacher = $this->teacher_model->get_entry($teacher_id);
if (empty($teacher)) {
echo json_encode(['success' => false]);
return;
}
$form_data = $this->input->post([
'dob', 'register_number', 'teacher_statut', 'teacher_iban'
]);
$errors = [];
$dob = DateTime::createFromFormat('Y-m-d', str_replace('/', '-', $form_data['dob']));
if (empty($dob) || $dob > (new DateTime()) || $dob->diff(new DateTime())->y < 16) {
$errors[] = "Please enter a correct date of birth";
}
$update_data = [];
// Allow edition for somes fields only before final validation
if ((int) $teacher->locked_profile === 0) {
if (empty($form_data['teacher_iban']) || strlen($form_data['teacher_iban']) > 34) {
$errors[] = "IBAN must be between 14 and 34 characters";
} else {
$update_data['iban'] = $form_data['teacher_iban'];
}
if (in_array((int) $form_data['teacher_statut'], [1, 3]) === false) {
$errors[] = "Please choose a teacher's contract type";
} else {
$update_data['admin_contract'] = (int) $form_data['teacher_statut'];
}
$update_data['register_number'] = $form_data['register_number'];
$update_data['dob'] = $dob->format('Y-m-d');
}
if (!empty($errors)) {
echo json_encode(['success' => false, 'errors' => $errors]);
return;
}
// If teacher is already in status "order", lock profile
if ((int) $teacher->admin_state === 1) {
$update_data['locked_profile'] = true;
}
$this->teacher_model->update_entry($teacher_id, $update_data);
echo json_encode(['success' => true]);
}
public function edit_student()
{
//update from parent profile (fonction added for register number)
$this->continueIfAllowed(['allowed' => ['admin', 'student', 'parent']]);
$data = $this->input->post([
'pic', 'phone', 'dob', 'line_1', 'line_2', 'country',
'state', 'location', 'postcode', 'lang', 'pass',
'type', 'name', 'surname', 'register_number', 'school', 'phone_sos', 'student'
]);
$error = array();
// Admin can edit all students, otherwise, we need to check if the user have the right to change student data
$userId = (int) $data['student'];
$actual_userid = (int) $this->session->userdata('userID');
if ($this->session->userdata('login_type') != 'admin') {
if ($userId !== $actual_userid && in_array($userId, $this->crud_model->get_parent_child($actual_userid)) === false) {
echo json_encode(['success' => false, 'errors' => ['Not allowed to edit this student.']]);
return;
}
}
$userEmail = $this->crud_model->get_user_email($userId);
$userAddressId = $this->users_model->get_profile_address();
if ($this->users_model->check_phone($data['phone']) != true || ($data['phone'] == null || $data['phone'] == "")) {
array_push($error, "Please enter a correct phone number");
}
if ($this->session->userdata('login_type') != 'admin') {
//admin dont need those forms
if ($this->users_model->check_address($data['line_1'], $data['line_2'], $data['country'], $data['state'], $data['postcode']) != true) {
array_push($error, "Please enter a correct address");
}
$dob = str_replace('/', '-', $data['dob']);
$dob = date('Y-m-d', strtotime($dob));
if ($this->users_model->check_dob($dob) != true || ($data['dob'] == null || $data['dob'] == "")) {
array_push($error, "Please enter a correct date of birth");
}
if (empty($data['register_number'])) {
array_push($error, "Please enter a valid national register number");
}
}
if ($data['pass'] != '') {
if (!$this->users_model->check_pass($data['pass'])) {
array_push($error, "Please enter a correct password");
}
}
$error = array_filter($error);
if (empty($error)) {
$this->profile_model->update_student_profile($data, $userId); //new procedure
$this->profile_model->set_session_lang(); // set language
$this->db->set(array("first_connection" => "0"));
$this->db->where('user_id', $userId);
$this->db->update('user');
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'errors' => $error]);
}
}
public function check_email()
{
$this->continueIfAllowed(array('allowed' => array('invited','student','parent','teacher','admin')));
$email = $this->input->post('email');
if ($this->users_model->email_exist($email) == true) {
echo "true";
} else {
echo "false";
}
}
public function edit_invited()
{
$this->continueIfAllowed(['allowed' => ['invited','student','parent','teacher','admin']]);
$data = $this->input->post(array('pic','phone','dob','line_1','line_2','country','state','location','postcode','lang','pass','type','name','surname','email','repartition','courses'));
$isoLang = $this->profile_model->intToIso($data['lang']);
if ($isoLang == "") {
$isoLang = "fr";
}
$error = [];
$userId = $this->session->userdata('userID');
$userEmail = $this->crud_model->get_user_email($userId);
$userAddressId = $this->users_model->get_profile_address();
$is_paid = true;
$password;
$this->session->set_userdata(array('login_type' => "invited"));
if ($this->session->userdata('login_type') === 'invited') {
if ($data['type'] === "child") {
//verifications
$dob = str_replace('/', '-', $data['dob']);
$dob = date('Y-m-d', strtotime($dob));
if ($this->users_model->check_dob($dob) != true || ($data['dob'] == null || $data['dob'] == "")) {
array_push($error, "Please enter a correct date of birth");
}
//check spam click
$last = $this->crud_model->get_last_user();
if ($last['first_name'] == $data['name'] && $last['last_name'] == $data['surname']) {
array_push($error, "user already created : spam prevent");
}
$error = array_filter($error);
if (empty($error)) {
//create user
$IDmail = uniqid()."[email protected]";
$password = $this->users_model->generate_pass('8');
$student_id = $this->users_model->create_user($IDmail, $password, $data['name'], $data['surname'], $data['phone'], '4', $isoLang, '3', '1', $is_paid);
$student_address_id = $this->users_model->create_user_address($data['line_1'], $data['line_2'], $data['country'], $data['state'], $data['location'], $data['postcode']);
if (!is_numeric($student_address_id) || !is_numeric($student_id)) {
return false;
};
$this->users_model->create_student($student_id, $student_address_id, "shop user :".$this->payment_model->get_payment_user_date($userId), $dob, $userId);
//everithing is done : Parent > Child √ -> redirect to parent profile
if ($is_paid) {
//if paid switch profile
if ($this->switch_profile('parent')) {
//set balance update;
$this->customer_model->update_balance_new($student_id, $data['courses'], $data['repartition']);
//assign course
$courses = json_decode($data['courses'], true);
foreach ($courses as $course_id) {
$course = $this->course_model->get_entry((int) $course_id);
$this->assignAll($student_id, (int) $course_id, $isoLang);
$product_transaction = $this->product_transaction_model->get_entry_unassigned((int) $userId, (int) $course_id);
if ($product_transaction) {
$this->product_transaction_model->update_entry($product_transaction->id, [
'state' => Product_transaction_model::STATES['assigned'],
'assigned_to' => $student_id,
'assigned_at' => date('Y-m-d H:i:s')
]);
if ($course) {
$transaction = $this->wallet_model->get_transaction_by_id((int) $product_transaction->transaction_id);
try {
$this->event_model->notify_admin('new_customer', $isoLang, $userId, [
'student_enrolled' => ucfirst($data['name']) . " " . ucfirst($data['surname']),
'product_buy' => $course->CourseName,
'payment_amount' => $transaction->Payment_Amount,
'payment_date' => date("d/m/Y"),
]);
} catch (Exception $e) {
echo "mail error :".$e->getMessage();
}
}
}
}
}
}
echo json_encode(['success' => true]);
return;
}
} elseif ($data['type'] === "student") {
//verifications
$dob = str_replace('/', '-', $data['dob']);
$dob = date('Y-m-d', strtotime($dob));
if ($this->users_model->check_dob($dob) != true || ($data['dob'] == null || $data['dob'] == "")) {
array_push($error, "Please enter a correct date of birth");
}
$error = array_filter($error);
if (empty($error)) {
$this->users_model->update_user($userId, $userEmail, $data['pass'], '', '', '', $data['lang']);
if ($userAddressId != "0" && $userAddressId != null) {
$this->users_model->update_user_address($userAddressId, $data['line_1'], $data['line_2'], $data['country'], $data['state'], $data['location'], $data['postcode']);
} else {
$id_address = $this->users_model->create_user_address($data['line_1'], $data['line_2'], $data['country'], $data['state'], $data['location'], $data['postcode']);
$this->users_model->add_student_address_id($id_address, $userId);
}
if (!$this->users_model->student_exist($userId)) {
$this->users_model->create_student($userId, $userAddressId, "shop user :".$this->payment_model->get_payment_user_date($userId), $dob, '');
$stud_name = $this->crud_model->get_user_name($userId);
$stud_mail = $this->crud_model->get_user_email($userId);
$this->event_model->trigger_user('account_creation_student', $isoLang, $userId, $data['pass']);
}
$this->profile_model->set_session_lang(); // set language
$this->session->set_userdata('profile_pending', 'student');
if ($is_paid) {
$sql = array(
"role_id" => '4',
"first_connection" => '0',
);
$this->db->set($sql);
$this->db->where('user_id', $userId);
$this->db->update('user');
if ($this->switch_profile('student')) {
//update balance
$this->customer_model->update_balance($userId, '1');
//auto assign
$courses = json_decode($data['courses'], true);
foreach ($courses as $course_id) {
$course = $this->course_model->get_entry((int) $course_id);
$product_transaction = $this->product_transaction_model->get_entry_unassigned((int) $userId, (int) $course_id);
$this->assignAll($userId, $course_id, $isoLang);
if ($product_transaction) {
$this->product_transaction_model->update_entry($product_transaction->id, [
'state' => Product_transaction_model::STATES['assigned'],
'assigned_to' => (int) $userId,
'assigned_at' => date('Y-m-d H:i:s')
]);
if ($course) {
$transaction = $this->wallet_model->get_transaction_by_id((int) $product_transaction->transaction_id);
try {
$this->event_model->notify_user('new_customer', $isoLang, $userId, [
'student_enrolled' => $data['name'] . " " . $data['surname'],
'product_buy' => $course->CourseName,
'payment_amount' => $transaction->Payment_Amount,
'payment_date' => date("d/m/Y"),
]);
} catch (Exception $e) {
echo "mail error :".$e->getMessage();
}
}
}
}
}
}
echo json_encode(['success' => true]);
return;
}
} elseif ($data['type'] === "parent") {
// Parent profile here
//verifications + check email
$dob = str_replace('/', '-', $data['dob']);
$dob = date('Y-m-d', strtotime($dob));
$id_address = 0;
$error = array_filter($error);
if (!$this->users_model->update_user($userId, $userEmail, $data['pass'], '', '', $data['phone'], $data['lang'])) {
return false;
}
if ($userAddressId != "0" && $userAddressId != null) {
$this->users_model->update_user_address($userAddressId, $data['line_1'], $data['line_2'], $data['country'], $data['state'], $data['location'], $data['postcode']);
} else {
$userAddressId = $this->users_model->create_user_address($data['line_1'], $data['line_2'], $data['country'], $data['state'], $data['location'], $data['postcode']);
}
if ($this->users_model->parent_exist($userId)) {
$this->users_model->update_parent_bis($userId, $userAddressId, "shop user :".$this->payment_model->get_payment_user_date($userId), $dob);
} else {
$this->users_model->create_parent($userId, $userAddressId, "shop user :".$this->payment_model->get_payment_user_date($userId), $dob, '');
}
$this->profile_model->set_session_lang(); // set language
$this->session->set_userdata('profile_pending', 'parent');
if ($is_paid) {
//if paid switch profile
// chronology : parent > child > redirect
$sql = array(
"role_id" => '3',
"first_connection" => "0"
);
$this->db->set($sql);
$this->db->where('user_id', $userId);
$this->db->update('user');
}
echo json_encode(['success' => true]);
return;
}
}
echo json_encode(['success' => false]);
return;
}
public function switch_profile($type)
{
//if paid switch else display error
$this->session->unset_userdata('invited_login');
$this->session->unset_userdata('invited_id');
$this->session->unset_userdata('login_type');
$this->session->set_userdata(array(
$type.'_login' => '1',
$type.'_id' => '1',
'login_type' => $type,
'notification' => "Votre profil ".$type." a été créé. Dès que votre achat aura été validé, vous serez averti par mail"
));
$this->customer_model->update_user_state('is_final', '1', $this->session->userdata('userID'));
return true;
}
// original from Student
public function assignAll($studentId, $courseId, $isoLang)
{
//keep in mind to avoid duplicate content :√
$last;
$lessonsId = $this->crud_model->get_course_lessons($courseId);
if (count($lessonsId) == 0) {
$this->customer_model->set_preference($studentId, $courseId);
return;
}
foreach ($lessonsId as $key => $lessonId) {
$assigned = $this->users_model->checkAssigned($studentId, $courseId, $lessonId["ID"]);
if (isset($studentId) && isset($courseId) && isset($lessonId) && ($assigned == 'false')) {
$sql = array(
'course_id' => "$courseId",
'lesson_id' => $lessonId["ID"],
'student_id' => "$studentId",
'author' => 'From Customer :'.$this->session->userdata('userID')
);
$this->db->insert('course_student_new', $this->crud_model->cleanArray($sql));
$this->event_model->trigger_first_lesson('Course_Student_First_Assignation', $isoLang, $studentId, $lessonId["ID"]);
}
}
$this->customer_model->update_user_state('is_final', '1', $studentId);
$this->customer_model->reset_preference($studentId, $courseId);
}
public function auto_complete_city()
{
$zip = $this->input->post('zip');
$cities = $this->users_model->get_city_by_code($zip);
echo json_encode($cities);
}
}