Changeset 25

Show
Ignore:
Timestamp:
12/21/08 12:36:04 (6 months ago)
Author:
matthew
Message:

Module now monitors space used by a site and emails users with site administrator role when space usage is exceeded. It repeats the email once a week until issue is resolved.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • drupal/modules/iutilities/trunk/iutilities.module

    r24 r25  
    1717                '#required' => TRUE, 
    1818        ); 
     19        $form['disk_space_warnings_role'] = array( 
     20                '#type' => 'textfield', 
     21                '#title' => t('Role to receive warning emails'), 
     22                '#default_value' => variable_get('disk_space_warnings_role', 'site administrator'), 
     23                '#size' => 20, 
     24                '#maxlength' => 25, 
     25                '#description' => t("The role entered here here will receive warning messages by email should the site exceed the space limitations."), 
     26                '#required' => TRUE, 
     27        ); 
    1928        return system_settings_form($form); 
    2029} 
     
    2635        $space_used = (int) trim($space_used_array[0]); 
    2736        variable_set('disk_space_used', $space_used);    
     37        $disk_space_limit = (int) variable_get('disk_space_limit',150); 
     38        //how long since a warning email was sent?? 
     39        $time_now = (int) time(); 
     40        $time_last_email = (int) variable_get('disk_space_last_emailed',0); 
     41        $time_since_email = (int) $time_now - $time_last_email; 
     42        //email if space allowance has been exceeded 
     43        // but only email if not yet emailed 
     44        // or it's more than a week since an email was sent 
     45        if ( $space_used > $disk_space_limit && ($time_last_email==0 || $time_since_email > 604799) ) { 
     46                $site_default_language = language_default(); 
     47                $disk_space_warnings_role = variable_get('disk_space_warnings_role', 'site administrator'); 
     48                $users_with_role_query = "select u.mail from {users} u inner join {users_roles} ur on u.uid=ur.uid inner join {role} r on r.rid=ur.rid where r.name='%s'"; 
     49                $result = db_query(db_rewrite_sql($users_with_role_query),$disk_space_warnings_role); 
     50                while ($data = db_fetch_object($result)) { 
     51                        drupal_mail('iutilities', 'warning', $data->mail, $site_default_language, $params); 
     52                } 
     53                //set a variable which notes that an email has been sent. 
     54                $time_stamp = (int) time(); 
     55                variable_set('disk_space_last_emailed',$time_stamp); 
     56        } elseif ($space_used <= $disk_space_limit) { 
     57                variable_set('disk_space_last_emailed',0); 
     58        } 
     59} 
     60 
     61function iutilities_mail($key, &$message, $params) { 
     62        global $base_url; 
     63        $space_used = variable_get('disk_space_used',150); 
     64        $space_limit = variable_get('disk_space_limit',150); 
     65        switch($key) { 
     66        case 'warning': 
     67                $message['subject'] = t('Space usage warning from ' . $base_url); 
     68                $message['body'] = t("An information message from your website host, ILLUMINATE ICT\n------------------------------------------------\n\nYou are listed as being a site administrator for $base_url.\n\nThis email is to inform you that you are currently exceeding your space allocation on our webserver. You have been allocated " . $space_limit . "MB and you are using " . $space_used . "MB.\n\nWe are not about to discontinue your service, but we would appreciate it if you would get in touch to discuss how we could help you. Contact us at http://www.illuminateict.org.uk.\n\nYou will receive this email weekly until we can help you to reduce the amount of space you are using, or increase your space allowance on the server."); 
     69                break; 
     70        } 
    2871} 
    2972 
     
    3679        ); 
    3780        $items['admin/settings/diskusage'] = array( 
    38                 'title' => 'Set Disk Usage Limit', 
     81                'title' => 'Set Disk Usage Defaults', 
    3982                'description' => 'The default 150MB space allowance set by illuminate ICT can be increased here.', 
    4083                'access callback' => 'access_disk_usage_settings', 
     
    4588        return $items; 
    4689} // illuminate_menu() 
    47  
    48 function stuff() { 
    49         return "Hello"; 
    50 } 
    5190 
    5291function access_disk_usage_settings() {