diff --git a/includes/chamilo-functions.php b/includes/chamilo-functions.php index b68ab01..b16df5c 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,226 @@ 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 ); + + $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() + ); + + // 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 ); + } + } + + 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); + }