/**
  * 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.
                         }
                     }
                 }
             }
         }
     }
 }