/** * Execute task * * @global \moodle_database $DB * @global \stdClass $CFG * @return void * @throws \moodle_exception */ public function execute() { global $DB, $CFG; // Not going to work if we're missing settings. if (!isset($CFG->block_mailchimp_apicode) || !isset($CFG->block_mailchimp_listid) || !isset($CFG->block_mailchimp_linked_profile_field)) { return; } echo '== Beginning synchronization of MailChimp subscribers ==', "\n"; // Get all users in MailChimp and synchronize. echo 'Getting list of users in MailChimp.', "\n"; $listusers = \block_mailchimp\helper::getMembersSync(); if (!$listusers) { debugging("ERROR: Failed to get list of all members. Unable to synchronize users."); return; } // If there is an interest specified, filter out users who do not have this interest marked. if (isset($CFG->block_mailchimp_interest) && !$CFG->block_mailchimp_interest == "0") { foreach ($listusers['members'] as $key => $externaluser) { if ($externaluser['interests'][$CFG->block_mailchimp_interest] == false) { unset($listusers['members'][$key]); } } // Reindex the array $listusers['members'] = array_values($listusers['members']); } $listuserscount = count($listusers['members']); // Get list of users in Moodle echo 'Getting list of users in Moodle.', "\n"; $moodleusers = $DB->get_records('user'); $moodleuserscount = count($moodleusers); // Convert Moodle email addresses to lower case. Mailchimp stores emails in lower case and calculates the MD5 hash on the lower case email. foreach ($moodleusers as $moodleuser) { $moodleuser->email = strtolower($moodleuser->email); } // Sort MailChimp users list echo 'Sorting list of MailChimp users.', "\n"; foreach ($listusers['members'] as $key => $row) { $emails[$key] = $row['email_address']; } array_multisort($emails, SORT_ASC, $listusers['members']); unset($emails); // Sort Moodle users list echo 'Sorting list of Moodle users.', "\n"; foreach ($moodleusers as $key => $row) { $emails[$key] = $row->email; } array_multisort($emails, SORT_ASC, $moodleusers); unset($emails); // Syncronize the list of users in Moodle with those in Mailchimp echo '== Starting sync of Moodle users with users in MailChimp ==', "\n"; foreach ($moodleusers as $moodleusersynckey => $moodleuser) { $statuspercent = round($moodleusersynckey / $moodleuserscount * 100, 1, PHP_ROUND_HALF_UP); echo $statuspercent, "% \r"; if (isguestuser($moodleuser)) { continue; } $this->synchronize_user($moodleuser, $listusers); } echo 'Done.', "\n"; //Iterate through mailchimp list and compare to moodle users' emails. If the email is not found in moodle, delete from mailchimp list. echo '== Starting MailChimp list cleanup ==', "\n"; foreach ($listusers['members'] as $listuserskey => $externaluser) { $statuspercent = round($listuserskey / $listuserscount * 100, 1, PHP_ROUND_HALF_UP); echo $statuspercent, "% \r"; $this->synchronize_mcuser($externaluser, $moodleusers); } echo 'Done.', "\n"; echo '== Finished MailChimp syncronization ==', "\n"; // Clean up static caches, since this process runs for a long time and (potentially) changes many DB records. See https://docs.moodle.org/dev/Task_API#Caches \core\task\manager::clear_static_caches(); }
/** * Execute task * * @global \moodle_database $DB * @global \stdClass $CFG * @return void * @throws \moodle_exception */ public function execute_interestsync() { global $DB, $CFG; // Not going to work if we're missing settings. if (!isset($CFG->block_mailchimp_apicode) || !isset($CFG->block_mailchimp_listid) || !isset($CFG->block_mailchimp_linked_profile_field)) { cli_error("No API code or list selected. Aborting interest sync.", 1); return; } if (empty($CFG->block_mailchimp_interest) || $CFG->block_mailchimp_interest == "0") { cli_error("No interest selected. Aborting interest sync.", 1); return; } $listid = $CFG->block_mailchimp_listid; // Get all users in MailChimp. $listusers = \block_mailchimp\helper::getMembersSync(); if (!$listusers) { cli_error("ERROR: Failed to get list of all members. Unable to synchronize users.", 1); return; } $listuserscount = count($listusers['members']); // Get list of users in Moodle cli_heading('Getting list of users in Moodle'); $moodleusers = $DB->get_records('user'); cli_heading('Sorting user lists'); // Sort MailChimp users list // foreach ($listusers['members'] as $key => $row) { // $emails[$key] = $row['email_address']; // } // array_multisort($emails, SORT_ASC, $listusers['members']); // unset($emails); // Sort Moodle users list foreach ($moodleusers as $key => $row) { $emails[$key] = $row->email; } array_multisort($emails, SORT_ASC, $moodleusers); unset($emails); // // Update all the users that are present in moodle so they have the interest added. // cli_heading('Adding interest to MailChimp users that are present in Moodle'); // foreach ($moodleusers as $moodleuser) { // foreach ($listusers['members'] as $listuser) { // // Search through the moodleusers to find the user with corresponding email address // $maxkey = count($moodleusers) - 1; // $minkey = 0; // $searchkey = round((($maxkey + $minkey)/2), 0, PHP_ROUND_HALF_UP); // $moodleuser = false; // $listuseremail = strtowlower($listuser['email_address']); // while($minkey <= $maxkey) { // $moodleuseremail = strtolower($moodleusers[$searchkey]->email); // if ($listuseremail == $moodleuseremail) { // $moodleuser = $moodleusers[$searchkey]; // break; // } // else if ($listuseremail > $moodleuseremail) { // $minkey = $searchkey + 1; // $searchkey = round((($maxkey + $minkey)/2), 0, PHP_ROUND_HALF_UP); // } // else if ($listuseremail < $moodleuseremail) { // $maxkey = $searchkey - 1; // $searchkey = round((($maxkey + $minkey)/2), 0, PHP_ROUND_HALF_UP); // } // } // // No corresponding moodleuser for the user in mailchimp // if ($moodleuser == false) { // // Do nothing for now // break; // } // // Maybe add some error handling here // } // Update subscription status info cli_heading('Applying interest label and updating subscription status'); foreach ($listusers['members'] as $listuserkey => $listuser) { $statuspercent = round($listuserkey / $listuserscount * 100, 1, PHP_ROUND_HALF_UP); echo $statuspercent, "% \r"; // Search through the mailchimp users to find the user with corresponding email address $maxkey = count($moodleusers) - 1; $minkey = 0; $searchkey = round(($maxkey + $minkey) / 2, 0, PHP_ROUND_HALF_UP); $moodleuser = false; $listuseremail = strtolower($listuser['email_address']); while ($minkey <= $maxkey) { $moodleuseremail = strtolower($moodleusers[$searchkey]->email); if ($listuseremail == $moodleuseremail) { $moodleuser = $moodleusers[$searchkey]; break; } else { if ($listuseremail > $moodleuseremail) { $minkey = $searchkey + 1; $searchkey = round(($maxkey + $minkey) / 2, 0, PHP_ROUND_HALF_UP); } else { if ($listuseremail < $moodleuseremail) { $maxkey = $searchkey - 1; $searchkey = round(($maxkey + $minkey) / 2, 0, PHP_ROUND_HALF_UP); } } } } // No corresponding moodleuser for the user in mailchimp if ($moodleuser == false) { // Do nothing for now break; } // Apply interest label to the user in MailChimp (First and last names are updated from Moodle) $args['EMAIL'] = strtolower($moodleuser->email); $args['FNAME'] = $moodleuser->firstname; $args['LNAME'] = $moodleuser->lastname; $updatestatus = \block_mailchimp\helper::listUpdateMember($listid, $moodleuser->email, $args, 'html'); // Get the profile data for this moodleuser $moodleuserprofiledata = $this->mc_get_profile_data($moodleuser); // Check subscription status in MC if ($listuser['status'] == 'unsubscribed' && $moodleuserprofiledata->data == '1') { // User is unsubscribed in mailchimp but marked as subscribed in moodle. Mailchimp is the source of truth for this sync. We'll unsubscribe in moodle $this->mc_update_profile_subscription($moodleuser, false); } if ($listuser['status'] == 'subscribed' && $moodleuserprofiledata->data == '0') { // User is subscribed in mailchimp but marked as unsubscribed in moodle. Subscribe the user in moodle $this->mc_update_profile_subscription($moodleuser, true); } } // New line for next output echo 'Done.', "\n"; }