.
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 |
--------------------------------------------------------------------------------