$userparams->timecreated = time(); $userparams->timemodified = time(); $DB->insert_record('block_mailchimp_users', $userparams); $usertoprocess = $DB->get_record('block_mailchimp_users', array('userid' => $USER->id)); } else { $usertoprocess->registered = $actionregister; $DB->update_record('block_mailchimp_users', $usertoprocess); } if (!$profilefieldtoprocess) { $profilefieldparams = new stdClass(); $profilefieldparams->userid = $USER->id; $profilefieldparams->fieldid = $CFG->block_mailchimp_linked_profile_field; $profilefieldparams->data = $actionregister ? 1 : 0; $profilefieldparams->dataformat = 0; $DB->insert_record('user_info_data', $profilefieldparams); $profilefieldtoprocess = $DB->get_record('user_info_data', array('userid' => $USER->id)); } else { $profilefieldtoprocess->data = $actionregister; $DB->update_record('user_info_data', $profilefieldtoprocess); } // Mailchimp. if ($actionregister) { $mergevars = array('EMAIL' => $USER->email, 'FNAME' => $USER->firstname, 'LNAME' => $USER->lastname); // We can already have a subscription, in which case we'll get an error code returned. \block_mailchimp\helper::listSubscribe($CFG->block_mailchimp_listid, $USER->email, $mergevars); redirect($CFG->wwwroot, get_string('subscribed_to_mailchimp', 'block_mailchimp')); } else { // We can have already been unsubscribed, in which case we'll get an error code returned. \block_mailchimp\helper::listUnsubscribe($CFG->block_mailchimp_listid, $USER->email); redirect($CFG->wwwroot, get_string('unsubscribed_to_mailchimp', 'block_mailchimp')); }
/** * Synchronise Mailchimp account/subscription for single moodle user * * @param \stdClass record from user table $moodleuser * */ private function synchronize_user($moodleuser, $listusers) { global $CFG, $DB; // First collect appropriate data. $mailchimpprofiledata = $this->mc_get_profile_data($moodleuser); $mailchimpinternaluser = $this->mc_get_internal_user($moodleuser); $externaluservars = array('EMAIL' => $moodleuser->email, 'FNAME' => $moodleuser->firstname, 'LNAME' => $moodleuser->lastname); // We might want to update the email. if ($mailchimpinternaluser->email != $moodleuser->email) { $this->mc_update_email_internal($moodleuser); } // Load external mailchimp userdata. $listmemberinfo = \block_mailchimp\helper::listMemberInfoSync($mailchimpinternaluser->email, $listusers); // In case of an error, the external user does not yet exist. if (!$listmemberinfo) { $externaluserregistered = false; } else { $externaluserregistered = true; } // If there's no subscription and we have no external user, abandon. // External: NA if (!$externaluserregistered) { if ($mailchimpprofiledata->data == '0') { // Add user to the mailchimp list, just as unsubscribed (we want the list to match moodle users) // Internal: U \block_mailchimp\helper::listUnsubscribe($CFG->block_mailchimp_listid, $moodleuser->email, $externaluservars, 'html', $listusers); } else { if ($mailchimpprofiledata->data == '1') { // If there's no external user but a profile setting to subscribe, handle it. // Internal: S \block_mailchimp\helper::listSubscribe($CFG->block_mailchimp_listid, $moodleuser->email, $externaluservars, 'html', $listusers); // Update the internal status if for some reason not synced if (!(bool) $mailchimpinternaluser->registered) { $this->mc_update_subscription_internal($moodleuser, true); } } } } else { if ($externaluserregistered === true) { $externaluserinfo = $listmemberinfo; // User is externally known. if ($externaluserinfo['status'] == 'pending') { // We do absolutely nothing while statusses are pending. return; } else { if ($externaluserinfo['status'] == 'unsubscribed') { // Handle unsubscription sync. // External: U $comparison = $this->compareModified($mailchimpinternaluser->timemodified, $externaluserinfo['last_changed']); if (!$comparison) { // Error in comparison. Do something clever } else { if ((int) $comparison == 1) { // Internal subscription status is newer, change mailchimp subscription status if needed. if ($mailchimpprofiledata->data == '1') { // Internal: S // Subscribe the user in mailchimp \block_mailchimp\helper::listSubscribe($CFG->block_mailchimp_listid, $moodleuser->email, $externaluservars, 'html', $listusers); } // If data == 0, internal and external subscription status match. } else { if ((int) $comparison == 2) { // External subscription status is newer, change the internal status to unsubscribed if needed. if ($mailchimpprofiledata->data == '1') { // Internal: S // Unsubscribe the user in Moodle since MailChimp is last modified. $this->mc_update_profile_subscription($moodleuser, false); $this->mc_update_subscription_internal($moodleuser, false); } } } } } else { if ($externaluserinfo['status'] == 'subscribed') { // Handle subscription sync. // External: S $comparison = $this->compareModified($mailchimpinternaluser->timemodified, $externaluserinfo['last_changed']); if (!$comparison) { // Error in comparison. Do something clever. Get a fez. } else { if ((int) $comparison == 1) { // Internal subscription status is newer, change mailchimp subscription status if needed. if ($mailchimpprofiledata->data == '0') { // Internal: U // Unsubscribe the user from mailchimp \block_mailchimp\helper::listUnsubscribe($CFG->block_mailchimp_listid, $moodleuser->email, $externaluservars, 'html', $listusers); } // If data == 1, internal and external subscription status match. } else { if ((int) $comparison == 2) { // External subscription status is newer, change the internal status to subscribed if needed. if ($mailchimpprofiledata->data == '0') { // Internal: U // Subscribe the user in Moodle since MailChimp is last modified. $this->mc_update_profile_subscription($moodleuser, true); $this->mc_update_subscription_internal($moodleuser, true); } } } } } else { if ($externaluserinfo['status'] == 'cleaned') { // No idea what to do here. } } } } } } }