예제 #1
0
/**
 * Invalidates browser caches and cached data in temp.
 *
 * IMPORTANT - If you are adding anything here to do with the cache directory you should also have a look at
 * {@link phpunit_util::reset_dataroot()}
 *
 * @return void
 */
function purge_all_caches()
{
    global $CFG, $DB;
    reset_text_filters_cache();
    js_reset_all_caches();
    theme_reset_all_caches();
    get_string_manager()->reset_caches();
    core_text::reset_caches();
    if (class_exists('core_plugin_manager')) {
        core_plugin_manager::reset_caches();
    }
    // Bump up cacherev field for all courses.
    try {
        increment_revision_number('course', 'cacherev', '');
    } catch (moodle_exception $e) {
        // Ignore exception since this function is also called before upgrade script when field course.cacherev does not exist yet.
    }
    $DB->reset_caches();
    cache_helper::purge_all();
    // Purge all other caches: rss, simplepie, etc.
    clearstatcache();
    remove_dir($CFG->cachedir . '', true);
    // Make sure cache dir is writable, throws exception if not.
    make_cache_directory('');
    // This is the only place where we purge local caches, we are only adding files there.
    // The $CFG->localcachedirpurged flag forces local directories to be purged on cluster nodes.
    remove_dir($CFG->localcachedir, true);
    set_config('localcachedirpurged', time());
    make_localcache_directory('', true);
    \core\task\manager::clear_static_caches();
}
 /**
  * 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();
 }