From b7874e768e5bd463c8aff4166e694887495cad24 Mon Sep 17 00:00:00 2001 From: fabiowoj <36703444+fabiowoj@users.noreply.github.com> Date: Tue, 10 Nov 2020 06:21:42 -0300 Subject: [PATCH 1/2] Functions to integrate with woocommerce Create a user in chamilo when the order is complete. If the user already in chamilo, then just subscribe in course or session. --- includes/chamilo-functions.php | 246 ++++++++++++++++++++++++++++++++- 1 file changed, 244 insertions(+), 2 deletions(-) diff --git a/includes/chamilo-functions.php b/includes/chamilo-functions.php index b68ab01..42f006c 100644 --- a/includes/chamilo-functions.php +++ b/includes/chamilo-functions.php @@ -91,7 +91,17 @@ function chamilo_get_courses($visibilities = array()) { if (empty($visibilites)) { $visibilities = 'public,public-registered'; } - $courses = chamilo_soap_call( 'courses_list', 'WSCourseList', 'admin', $signature, $visibilities ); + $courses = chamilo_soap_call( 'courses_list', 'WSCourseList', $username, $signature, $visibilities ); + return $courses; +} + +function chamilo_get_courses_by_user() { + $signature = chamilo_get_signature(CHAMILO_SECRET_KEY); + $current_user = wp_get_current_user(); + $username = $current_user->user_login.$current_user->ID; + + $courses = chamilo_soap_call( 'user_info', 'WSCourseListOfUser', $username, $signature); + return $courses; } @@ -181,4 +191,236 @@ function chamilo_display_courses_list($courses) { $output .= ''; } echo $output; -} \ No newline at end of file +} + +function chamilo_order_complete( $order_id ) { + + $finalKey = chamilo_get_signature(CHAMILO_SECRET_KEY); + //global $items; + $order = new WC_Order($order_id); + //$order = wc_get_order( $order_id ); + $items = $order->get_items(); + + foreach ( $items as $item ) { + $product_name = $item->get_name(); + $product_id = $item->get_product_id(); + $product_variation_id = $item->get_variation_id(); + + // Get a product instance. I could pass in an ID here. + // I'm leaving empty to get the current product. + $product = wc_get_product($product_id); + + + if (!empty($product->get_attribute( 'CHAMILOCODE' ))) { + $courseCodeList[] = $product->get_attribute( 'CHAMILOCODE' ); + }; + if (!empty($product->get_attribute( 'CHAMILOSESSIONID' ))) { + $sessionList[] = $product->get_attribute( 'CHAMILOSESSIONID' ); + }; + + } + + if (empty($courseCodeList) && empty($sessionList)) { + + //write_log( "Course code and sessionlist are empty, nothing to create" ); + return true; + } + + + // Get the user ID from an Order ID + $user_id = get_post_meta( $order_id, '_customer_user', true ); + + // Get an instance of the WC_Customer Object from the user ID + $customer = new WC_Customer( $user_id ); + + $username = $customer->get_username(); // Get username + $user_email = $customer->get_email(); // Get account email + $first_name = $customer->get_first_name(); + $last_name = $customer->get_last_name(); + $display_name = $customer->get_display_name(); + + + // Check if the PS customer have already an account in Chamilo + $chamilo_params = array( + 'original_user_id_name' => 'external_user_id', + //required, field name about original user id + 'original_user_id_value' => $user_id, + //required, field value about original user id + 'secret_key' => $finalKey + //required, secret key ("your IP address and security key from chamilo") encrypted with sha1 + ); + + $chamilo_user_data = chamilo_soap_call( 'registration', 'WSGetUser', $chamilo_params ); + + //write_log($chamilo_user_data); + $chamilo_user_id = null; + if (!empty($chamilo_user_data)) { + $chamilo_user_id = $chamilo_user_data->user_id; + } + + + // Login generation - firstname (30 len char) + PS customer id + + $login = substr(strtolower($username),0,30).$user_id; + // User does not have a Chamilo account we proceed to create it + if (empty($chamilo_user_id)) { + + //write_log( "Wordpress Customer does not exist in Chamilo proceed the creation of the Chamilo user" ); + // Password generation + $password = $clean_password = generate_password(); + $encryption = 'sha1'; + switch($encryption) { + case 'md5': + $password = md5($password); + break; + case 'sha1': + $password = sha1($password); + break; + } + + // Default account validity in chamilo. + $expirationDate = date('Y-m-d H:i:s', strtotime("+3660 days")); + + // Setting params + $chamilo_params = + array( + 'firstname' => $first_name, // required + 'lastname' => $last_name, // required + 'status' => '5', // required, (1 => teacher, 5 => learner) + 'email' => $user_email, // optional, follow the same format (example@domain.com) + 'loginname' => $login, // required + 'official_code'=> $user_id, + 'password' => $password, // required, it's important to define the salt into an extra field param + 'encrypt_method' => $encryption, // required, check if the encrypt is the same than dokeos configuration + 'language' => 'brazilian', // optional + 'phone' => '', // optional + 'expiration_date' => $expirationDate, // optional, follow the same format + 'original_user_id_name' => 'external_user_id', //required, field name about original user id + 'original_user_id_value' => $user_id, //required, field value about original user id + 'secret_key' => $finalKey, //required, secret key ("your IP address and security key from chamilo") encrypted with sha1 + 'extra' => array() + ); + //write_log($chamilo_params); + // Creating a Chamilo user, calling the webservice + $chamilo_user_id = chamilo_soap_call( 'registration', 'WSCreateUserPasswordCrypted', $chamilo_params); + + + if (!empty($chamilo_user_id)) { + //write_log( "User is subscribed" ); + global $cookie; + + /* Email generation */ + $subject = get_bloginfo( 'name' ).' [Campus - Chamilo]'; + $templateVars = array( + '{firstname}' => $first_name, + '{lastname}' => $last_name, + '{email}' => $user_email, + '{login}' => $login, + '{password}' => $clean_password, + '{chamilo_url}' => get_option('chamilo_setting_url'), + '{site}' => get_bloginfo( 'name' ), + ); + + /* Email sending */ + + $to = $user_email; + $body = $templateVars; + $headers[] = 'Content-Type: text/html; charset=UTF-8'; + $headers[] = 'From: CBEPJUR <'.get_bloginfo( "admin_email" ).'>'; + wp_mail( $to, $subject, $body, $headers ); + + + + }else { + + //write_log("Error to create user"); + + } + + //write_log( "WSCreateUserPasswordCrypted was called this is the result: {$chamilo_user_id}" ); + } else { + //write_log("User have already a chamilo account associated with the current Wordpress customer. Chamilo user_id = {$chamilo_user_id} "); + + if (!empty($chamilo_user_id)) { + //write_log('User is subscribed'); + global $cookie; + + /* Email generation */ + $subject = get_bloginfo( 'name' ).' [Campus - Chamilo]'; + $templateVars = array( + '{firstname}' => $first_name, + '{lastname}' => $last_name, + '{chamilo_url}' => get_option('chamilo_setting_url'), + '{site}' => get_bloginfo( 'name' ), + ); + + + /* Email sending */ + //write_log('Sending message already registered'); + + $to = $user_email; + $body = $templateVars; + $headers[] = 'Content-Type: text/html; charset=UTF-8'; + $headers[] = 'From: CBEPJUR <'.get_bloginfo( "admin_email" ).'>'; + $mailResult = wp_mail( $to, $subject, $body, $headers ); + //write_log($mailResult); + } + } + + if (!empty($chamilo_user_id)) { + foreach ($courseCodeList as $course_code) { + write_log("Subscribing user to the course: {$course_code} "); + //if ($this->debug) error_log('Chamilo user was registered with user_id = '.$chamilo_user_id); + $chamilo_params = array( + 'course' => trim($course_code), + 'user_id' => $chamilo_user_id, + 'status' => STUDENT, + 'secret_key' => $finalKey + //required, secret key ("your IP address and security key from chamilo") encrypted with sha1 + ); + + $result = chamilo_soap_call( 'registration', 'WSSubscribeUserToCourseSimple', $chamilo_params ); + + + + } + + foreach ($sessionList as $sessionId) { + write_log("Subscribing user to the session: {$sessionId}"); + + $params = array( + 'session' => trim($sessionId), + 'user_id' => $chamilo_user_id, + 'status' => STUDENT, + 'secret_key' => $finalKey + ); + $result = chamilo_soap_call( 'registration', 'WSSubscribeUserToSessionSimple',$params); + + } + } else { + write_log("Error while trying to create a Chamilo user : ".print_r($chamilo_params, 1)."."); + } + + return true; + + +} + +function generate_password($length = 8) + { + $characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'; + if ($length < 2) { + $length = 2; + } + $password = ''; + for ($i = 0; $i < $length; $i ++) { + $password .= $characters[rand() % strlen($characters)]; + } + return $password; + } + +function chamilo_get_courses_by_user_display() + { + $courses = chamilo_get_courses_by_user(); + chamilo_display_courses_list($courses); + } From e5cc978f83c7a191c634f8ae35a53172a2761dc5 Mon Sep 17 00:00:00 2001 From: fabiowoj <36703444+fabiowoj@users.noreply.github.com> Date: Tue, 29 Dec 2020 18:55:37 -0300 Subject: [PATCH 2/2] Update chamilo-functions.php Fix idents and let blank optional language to create user in chamilo --- includes/chamilo-functions.php | 312 ++++++++++++++++----------------- 1 file changed, 151 insertions(+), 161 deletions(-) diff --git a/includes/chamilo-functions.php b/includes/chamilo-functions.php index 42f006c..b16df5c 100644 --- a/includes/chamilo-functions.php +++ b/includes/chamilo-functions.php @@ -220,10 +220,9 @@ function chamilo_order_complete( $order_id ) { } - if (empty($courseCodeList) && empty($sessionList)) { - - //write_log( "Course code and sessionlist are empty, nothing to create" ); - return true; + if (empty($courseCodeList) && empty($sessionList)) { + //write_log( "Course code and sessionlist are empty, nothing to create" ); + return true; } @@ -241,169 +240,160 @@ function chamilo_order_complete( $order_id ) { // Check if the PS customer have already an account in Chamilo - $chamilo_params = array( - 'original_user_id_name' => 'external_user_id', - //required, field name about original user id - 'original_user_id_value' => $user_id, - //required, field value about original user id - 'secret_key' => $finalKey - //required, secret key ("your IP address and security key from chamilo") encrypted with sha1 - ); - - $chamilo_user_data = chamilo_soap_call( 'registration', 'WSGetUser', $chamilo_params ); - - //write_log($chamilo_user_data); - $chamilo_user_id = null; - if (!empty($chamilo_user_data)) { - $chamilo_user_id = $chamilo_user_data->user_id; - } - - - // Login generation - firstname (30 len char) + PS customer id - - $login = substr(strtolower($username),0,30).$user_id; - // User does not have a Chamilo account we proceed to create it - if (empty($chamilo_user_id)) { - - //write_log( "Wordpress Customer does not exist in Chamilo proceed the creation of the Chamilo user" ); - // Password generation - $password = $clean_password = generate_password(); - $encryption = 'sha1'; - switch($encryption) { - case 'md5': - $password = md5($password); - break; - case 'sha1': - $password = sha1($password); - break; - } - - // Default account validity in chamilo. - $expirationDate = date('Y-m-d H:i:s', strtotime("+3660 days")); - - // Setting params - $chamilo_params = - array( - 'firstname' => $first_name, // required - 'lastname' => $last_name, // required - 'status' => '5', // required, (1 => teacher, 5 => learner) - 'email' => $user_email, // optional, follow the same format (example@domain.com) - 'loginname' => $login, // required - 'official_code'=> $user_id, - 'password' => $password, // required, it's important to define the salt into an extra field param - 'encrypt_method' => $encryption, // required, check if the encrypt is the same than dokeos configuration - 'language' => 'brazilian', // optional - 'phone' => '', // optional - 'expiration_date' => $expirationDate, // optional, follow the same format - 'original_user_id_name' => 'external_user_id', //required, field name about original user id - 'original_user_id_value' => $user_id, //required, field value about original user id - 'secret_key' => $finalKey, //required, secret key ("your IP address and security key from chamilo") encrypted with sha1 + $chamilo_params = array( + 'original_user_id_name' => 'external_user_id', + //required, field name about original user id + 'original_user_id_value' => $user_id, + //required, field value about original user id + 'secret_key' => $finalKey + //required, secret key ("your IP address and security key from chamilo") encrypted with sha1 + ); + + $chamilo_user_data = chamilo_soap_call( 'registration', 'WSGetUser', $chamilo_params ); + + $chamilo_user_id = null; + if (!empty($chamilo_user_data)) { + $chamilo_user_id = $chamilo_user_data->user_id; + } + + + // Login generation - firstname (30 len char) + WP customer id + + $login = substr(strtolower($username),0,30).$user_id; + // User does not have a Chamilo account we proceed to create it + if (empty($chamilo_user_id)) { + + // write_log( "Wordpress Customer does not exist in Chamilo proceed the creation of the Chamilo user" ); + + // Password generation + $password = $clean_password = generate_password(); + $encryption = 'sha1'; // Set the encryption of your chamilo platform + switch($encryption) { + case 'md5': + $password = md5($password); + break; + case 'sha1': + $password = sha1($password); + break; + } + + // Default account validity in chamilo. + $expirationDate = date('Y-m-d H:i:s', strtotime("+3660 days")); + + // Setting params + $chamilo_params = + array( + 'firstname' => $first_name, // required + 'lastname' => $last_name, // required + 'status' => '5', // required, (1 => teacher, 5 => learner) + 'email' => $user_email, // optional, follow the same format (example@domain.com) + 'loginname' => $login, // required + 'official_code'=> $user_id, + 'password' => $password, // required, it's important to define the salt into an extra field param + 'encrypt_method' => $encryption, // required, check if the encrypt is the same than dokeos configuration + 'language' => '', // optional + 'phone' => '', // optional + 'expiration_date' => $expirationDate, // optional, follow the same format + 'original_user_id_name' => 'external_user_id', //required, field name about original user id + 'original_user_id_value' => $user_id, //required, field value about original user id + 'secret_key' => $finalKey, //required, secret key ("your IP address and security key from chamilo") encrypted with sha1 'extra' => array() - ); - //write_log($chamilo_params); - // Creating a Chamilo user, calling the webservice - $chamilo_user_id = chamilo_soap_call( 'registration', 'WSCreateUserPasswordCrypted', $chamilo_params); - - - if (!empty($chamilo_user_id)) { - //write_log( "User is subscribed" ); - global $cookie; - - /* Email generation */ - $subject = get_bloginfo( 'name' ).' [Campus - Chamilo]'; - $templateVars = array( - '{firstname}' => $first_name, - '{lastname}' => $last_name, - '{email}' => $user_email, - '{login}' => $login, - '{password}' => $clean_password, - '{chamilo_url}' => get_option('chamilo_setting_url'), - '{site}' => get_bloginfo( 'name' ), - ); - - /* Email sending */ - - $to = $user_email; - $body = $templateVars; - $headers[] = 'Content-Type: text/html; charset=UTF-8'; - $headers[] = 'From: CBEPJUR <'.get_bloginfo( "admin_email" ).'>'; - wp_mail( $to, $subject, $body, $headers ); - - - - }else { - - //write_log("Error to create user"); - - } - - //write_log( "WSCreateUserPasswordCrypted was called this is the result: {$chamilo_user_id}" ); - } else { - //write_log("User have already a chamilo account associated with the current Wordpress customer. Chamilo user_id = {$chamilo_user_id} "); - - if (!empty($chamilo_user_id)) { - //write_log('User is subscribed'); - global $cookie; - - /* Email generation */ - $subject = get_bloginfo( 'name' ).' [Campus - Chamilo]'; - $templateVars = array( - '{firstname}' => $first_name, - '{lastname}' => $last_name, - '{chamilo_url}' => get_option('chamilo_setting_url'), - '{site}' => get_bloginfo( 'name' ), - ); - - - /* Email sending */ - //write_log('Sending message already registered'); - - $to = $user_email; - $body = $templateVars; - $headers[] = 'Content-Type: text/html; charset=UTF-8'; - $headers[] = 'From: CBEPJUR <'.get_bloginfo( "admin_email" ).'>'; - $mailResult = wp_mail( $to, $subject, $body, $headers ); - //write_log($mailResult); - } - } + ); + + // Creating a Chamilo user, calling the webservice + $chamilo_user_id = chamilo_soap_call( 'registration', 'WSCreateUserPasswordCrypted', $chamilo_params); if (!empty($chamilo_user_id)) { - foreach ($courseCodeList as $course_code) { - write_log("Subscribing user to the course: {$course_code} "); - //if ($this->debug) error_log('Chamilo user was registered with user_id = '.$chamilo_user_id); - $chamilo_params = array( - 'course' => trim($course_code), - 'user_id' => $chamilo_user_id, - 'status' => STUDENT, - 'secret_key' => $finalKey - //required, secret key ("your IP address and security key from chamilo") encrypted with sha1 - ); - - $result = chamilo_soap_call( 'registration', 'WSSubscribeUserToCourseSimple', $chamilo_params ); - - - - } - - foreach ($sessionList as $sessionId) { - write_log("Subscribing user to the session: {$sessionId}"); - - $params = array( - 'session' => trim($sessionId), - 'user_id' => $chamilo_user_id, - 'status' => STUDENT, - 'secret_key' => $finalKey - ); - $result = chamilo_soap_call( 'registration', 'WSSubscribeUserToSessionSimple',$params); - - } + // write_log( "User is subscribed" ); + global $cookie; + + /* Email generation */ + $subject = get_bloginfo( 'name' ).' [Campus - Chamilo]'; + $templateVars = array( + '{firstname}' => $first_name, + '{lastname}' => $last_name, + '{email}' => $user_email, + '{login}' => $login, + '{password}' => $clean_password, + '{chamilo_url}' => get_option('chamilo_setting_url'), + '{site}' => get_bloginfo( 'name' ), + ); + + /* Email sending */ + + $to = $user_email; + $body = $templateVars; + $headers[] = 'Content-Type: text/html; charset=UTF-8'; + $headers[] = 'From: CBEPJUR <'.get_bloginfo( "admin_email" ).'>'; + wp_mail( $to, $subject, $body, $headers ); + } else { - write_log("Error while trying to create a Chamilo user : ".print_r($chamilo_params, 1)."."); + + // write_log("Error to create user"); + } - - return true; - + // write_log( "WSCreateUserPasswordCrypted was called this is the result: {$chamilo_user_id}" ); + } else { + //write_log("User have already a chamilo account associated with the current Wordpress customer. Chamilo user_id = {$chamilo_user_id} "); + + if (!empty($chamilo_user_id)) { + //write_log('User is subscribed'); + global $cookie; + + /* Email generation */ + $subject = get_bloginfo( 'name' ).' [Campus - Chamilo]'; + $templateVars = array( + '{firstname}' => $first_name, + '{lastname}' => $last_name, + '{chamilo_url}' => get_option('chamilo_setting_url'), + '{site}' => get_bloginfo( 'name' ), + ); + + + /* Email sending */ + //write_log('Sending message already registered'); + + $to = $user_email; + $body = $templateVars; + $headers[] = 'Content-Type: text/html; charset=UTF-8'; + $headers[] = 'From: CBEPJUR <'.get_bloginfo( "admin_email" ).'>'; + $mailResult = wp_mail( $to, $subject, $body, $headers ); + } + } + + if (!empty($chamilo_user_id)) { + foreach ($courseCodeList as $course_code) { + write_log("Subscribing user to the course: {$course_code} "); + //if ($this->debug) error_log('Chamilo user was registered with user_id = '.$chamilo_user_id); + $chamilo_params = array( + 'course' => trim($course_code), + 'user_id' => $chamilo_user_id, + 'status' => STUDENT, + 'secret_key' => $finalKey + //required, secret key ("your IP address and security key from chamilo") encrypted with sha1 + ); + + $result = chamilo_soap_call( 'registration', 'WSSubscribeUserToCourseSimple', $chamilo_params ); + } + + foreach ($sessionList as $sessionId) { + // write_log("Subscribing user to the session: {$sessionId}"); + + $params = array( + 'session' => trim($sessionId), + 'user_id' => $chamilo_user_id, + 'status' => STUDENT, + 'secret_key' => $finalKey + ); + $result = chamilo_soap_call( 'registration', 'WSSubscribeUserToSessionSimple',$params); + + } + } else { + // write_log("Error while trying to create a Chamilo user : ".print_r($chamilo_params, 1)."."); + } + +return true; } function generate_password($length = 8)