├── .gitattributes ├── db ├── install.xml └── access.php ├── version.php ├── lang └── en │ └── block_inactive_user_cleanup.php ├── README.txt ├── edit_form.php ├── .gitignore └── block_inactive_user_cleanup.php /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /db/install.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 |
17 |
18 | 19 | -------------------------------------------------------------------------------- /version.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Version details 19 | * 20 | * @package block inactive user cleanup 21 | * @copyright 2014 Dualcube {@link http://dualcube.com/} 22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 | */ 24 | 25 | defined('MOODLE_INTERNAL') || die(); 26 | 27 | $plugin->version = 2014080501; // The current plugin version (Date: YYYYMMDDXX). 28 | $plugin->requires = 2012112900; 29 | $plugin->component = 'block_inactive_user_cleanup'; // Full name of the plugin (used for diagnostics). 30 | $plugin->maturity = MATURITY_STABLE; 31 | $plugin->cron = 60; 32 | $plugin->release = '2.4 (Build: 2014080501)'; 33 | 34 | -------------------------------------------------------------------------------- /lang/en/block_inactive_user_cleanup.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Strings for component 'block_inactive_user_cleanup', language 'en', branch 'MOODLE_24_STABLE' 19 | * @package block_inactive_user_cleanup 20 | * @copyright 2014 Dualcube {@link http://dualcube.com/} 21 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 22 | */ 23 | $string['pluginname'] = 'Inactive User Cleanup'; 24 | $string['setting'] = 'Setting Panel'; 25 | $string['daysofinactivity'] = 'Days Of Inactivity'; 26 | $string['daysbeforedeletion'] = 'Days Before Deletion'; 27 | $string['emailsetting'] = 'Email Setting'; 28 | $string['emailsubject'] = 'Subject'; 29 | $string['emailbody'] = 'Body'; 30 | $string['runcron'] = 'Run Cron Manually'; 31 | 32 | 33 | -------------------------------------------------------------------------------- /db/access.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * inactive user cleanup block caps. 19 | * 20 | * @package block_inactive_user_cleanup 21 | * @copyright 2014 Dualcube {@link http://dualcube.com/} 22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 | */ 24 | 25 | defined('MOODLE_INTERNAL') || die(); 26 | 27 | $capabilities = array( 28 | 'block/inactive_user_cleanup:addinstance' => array( 29 | 'riskbitmask' => RISK_SPAM | RISK_XSS, 30 | 'captype' => 'write', 31 | 'contextlevel' => CONTEXT_BLOCK, 32 | 'archetypes' => array( 33 | 'editingteacher' => CAP_ALLOW, 34 | 'manager' => CAP_ALLOW 35 | ), 36 | 'clonepermissionsfrom' => 'moodle/site:manageblocks' 37 | ), 38 | ); 39 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | Inactive User Clean-up for Moodle 2 | 3 | Inactive User Cleanup plugin deletes inactive user accounts. Its cleanup process runs on Moodle cron job. 4 | 5 | The plugin works in two steps: 6 | 7 | Step 1: The admin sets up days of inactivity and drafts notification mails for all users from the 8 | ‘Configuring A Inactive User’ block. 9 | If inactive users are found, a notification mail is sent to them. 10 | 11 | Step 2: If the user still remains inactive from the Moodle site within the time span mentioned in the notification mail, 12 | the deletion process starts. 13 | With the next run of the cleanup process, the particular inactive user account entry gets removed. 14 | 15 | Inactive User Cleanup plugin deletes inactive user accounts. Its cleanup process runs on Moodle cron job. 16 | 17 | The plugin works in two steps: 18 | 19 | Step 1: The admin sets up days of inactivity and drafts notification mails for all users from the 20 | ‘Configuring A Inactive User’ block. 21 | If inactive users are found, a notification mail is sent to them. 22 | 23 | Step 2: If the user still remains inactive from the Moodle site within the time span mentioned in the notification mail, 24 | the deletion process starts. 25 | With the next run of the cleanup process, the particular inactive user account entry gets removed. 26 | 27 | 28 | To install, place all files in /blocks/inactive_user_cleanup and visit /admin/index.php in your browser. 29 | 30 | This block is written by Dualcube . 31 | -------------------------------------------------------------------------------- /edit_form.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Form for inactive user cleanup block instances. 19 | * @package block_inactive_user_cleanup 20 | * @copyright 2014 Dualcube {@link http://dualcube.com/} 21 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 22 | */ 23 | class block_inactive_user_cleanup_edit_form extends block_edit_form { 24 | protected function specific_definition($mform) { 25 | global $CFG, $DB; 26 | $mform->addElement('header', 'configheader', get_string('setting', 'block_inactive_user_cleanup')); 27 | $mform->addElement('text', 'config_daysofinactivity', get_string('daysofinactivity', 'block_inactive_user_cleanup')); 28 | $mform->addElement('text', 'config_daysbeforedeletion', get_string('daysbeforedeletion', 'block_inactive_user_cleanup')); 29 | $mform->setDefault('config_daysofinactivity', '365'); 30 | $mform->setType('config_daysofinactivity', PARAM_TEXT); 31 | $mform->setDefault('config_daysbeforedeletion', '10'); 32 | $mform->setType('config_daysbeforedeletion', PARAM_TEXT); 33 | $mform->addElement('header', 'config_headeremail', get_string('emailsetting', 'block_inactive_user_cleanup')); 34 | $mform->addElement('text', 'config_subjectemail', get_string('emailsubject', 'block_inactive_user_cleanup')); 35 | $editoroptions = array('trusttext' => true, 'subdirs' => true, 'maxfiles' => 1, 36 | 'maxbytes' => 1024); 37 | $mform->addElement('editor', 'config_bodyemail', get_string('emailbody', 'block_inactive_user_cleanup'), $editoroptions); 38 | $mform->setType('config_subjectemail', PARAM_TEXT); 39 | $mform->setDefault('config_subjectemail', 'subject'); 40 | $mform->setType('config_bodyemail', PARAM_TEXT); 41 | $mform->setDefault('config_bodyemail', 'body'); 42 | } 43 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /block_inactive_user_cleanup.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Inactive User Cleanup block 19 | * @package block inactive user cleanup 20 | * @copyright 2014 dualcube {@link http://dualcube.com/} 21 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 22 | */ 23 | 24 | class block_inactive_user_cleanup extends block_base { 25 | public function init() { 26 | if (is_siteadmin()) { 27 | $this->title = get_string('pluginname', 'block_inactive_user_cleanup'); 28 | } 29 | } 30 | public function get_content() { 31 | if (is_siteadmin()) { 32 | global $USER, $CFG, $DB; 33 | $this->content = new stdClass; 34 | $settingpanelinsert = new stdClass; 35 | $settingpanelupdate = new stdClass; 36 | $this->content->text .= '

Today is ' . date("d-m-y") . '

'; 37 | $this->content->text .= '

Run cron script :'; 38 | $cronurl = new moodle_url('/admin/cron.php'); 39 | $this->content->text .= html_writer::link($cronurl, get_string('runcron', 'block_inactive_user_cleanup')); 40 | $this->content->text .= '

'; 41 | $interval = date("d-m-y", $USER->lastlogin) - date("d-m-y"); 42 | if (!empty($this->config->daysofinactivity)) { 43 | $settingpanelinsert->daysbeforedeletion = $this->config->daysbeforedeletion; 44 | $settingpanelinsert->daysofinactivity = $this->config->daysofinactivity; 45 | $settingpanelinsert->emailsubject = $this->config->subjectemail; 46 | $settingpanelinsert->emailbody = $this->config->bodyemail['text']; 47 | $count = $DB->count_records('block_inactive_user_cleanup'); 48 | $id = $DB->get_records_sql('select id as ids from {block_inactive_user_cleanup}'); 49 | foreach ($id as $updateid) { 50 | $settingpanelupdate->id = $updateid->ids; 51 | } 52 | $settingpanelupdate->daysbeforedeletion = $this->config->daysbeforedeletion; 53 | $settingpanelupdate->daysofinactivity = $this->config->daysofinactivity; 54 | $settingpanelupdate->emailsubject = $this->config->subjectemail; 55 | $settingpanelupdate->emailbody = $this->config->bodyemail['text']; 56 | if ($count == 0) { 57 | $DB->insert_record('block_inactive_user_cleanup', $settingpanelinsert); 58 | } else { 59 | $DB->update_record('block_inactive_user_cleanup', $settingpanelupdate); 60 | } 61 | } 62 | return $this->content; 63 | } 64 | } 65 | public function cron() { 66 | if (is_siteadmin()) { 67 | global $DB, $CFG; 68 | mtrace("Hey, my inactive user cleanup is running"); 69 | $emailsetting = $DB->get_records('block_inactive_user_cleanup'); 70 | foreach ($emailsetting as $emailsettingdetails) { 71 | $inactivity = $emailsettingdetails->daysofinactivity; 72 | $beforedelete = $emailsettingdetails->daysbeforedeletion; 73 | $subject = $emailsettingdetails->emailsubject; 74 | $body = $emailsettingdetails->emailbody; 75 | } 76 | $users = $DB->get_records('user'); 77 | foreach ($users as $usersdetails) { 78 | $subject = $emailsettingdetails->emailsubject; 79 | $messagetext = html_to_text($emailsettingdetails->emailbody); 80 | if (date("d-m-y", $usersdetails->lastlogin) - date("d-m-y") > $inactivity) { 81 | if ($mailresults = email_to_user($usersdetails, $users, $subject, $messagetext)) { 82 | mtrace('email sent'); 83 | } 84 | } 85 | if ($beforedelete != 0) { 86 | $deleted = 1; 87 | $username = $usersdetails->email . '.' . time(); 88 | $letters = 'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; 89 | $deleteemail = substr(str_shuffle($letters), 0, 32); 90 | if ((date("d-m-y", $usersdetails->lastlogin) - date("d-m-y")) >= ($inactivity + $beforedelete)) { 91 | $sql = 'update {user} set deleted = ?, username = ?, email = ? where id = ?'; 92 | $DB->execute($sql, array($deleted, $username, $deleteemail, $usersdetails->id)); 93 | mtrace('delete user' . $usersdetails->id); 94 | } 95 | } 96 | } 97 | return true; 98 | } 99 | } 100 | } 101 | 102 | 103 | --------------------------------------------------------------------------------