-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Functions to integrate with woocommerce #2
Open
fabiowoj
wants to merge
2
commits into
chamilo:master
Choose a base branch
from
fabiowoj:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 .= '</ul>'; | ||
} | ||
echo $output; | ||
} | ||
} | ||
|
||
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 ([email protected]) | ||
'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); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
language shouldn't be a fixed value. Isn't there something we can use in WordPress to catch the current language?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes @ywarnier , but not getting entire name of language, just the code.
$lang = get_bloginfo("language");
echo $lang;
// output fr-FR, pt-BR, ....
So for this function, maybe the 'language' just let empty to chamilo made of default language in chamilo platform.