├── db ├── install.xml ├── tasks.php └── access.php ├── settings.php ├── version.php ├── lang └── en │ └── tool_inactive_user_cleanup.php ├── index.php ├── Readme.txt ├── settings_form.php └── classes ├── privacy └── provider.php └── task └── tool_inactive_user_cleanup_task.php /db/install.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 |
19 |
20 | -------------------------------------------------------------------------------- /settings.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Link to inactive user cleanup 19 | * 20 | * @package tool_inactive_user_cleanup 21 | * @author DualCube 22 | * @copyright DualCube (https://dualcube.com) 23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 | */ 25 | 26 | defined('MOODLE_INTERNAL') || die; 27 | if ($hassiteconfig) { 28 | $ADMIN->add('reports', 29 | new admin_externalpage('toolinactive_user_cleanup', get_string('pluginname', 'tool_inactive_user_cleanup'), 30 | "$CFG->wwwroot/$CFG->admin/tool/inactive_user_cleanup/index.php", 'moodle/site:config')); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /db/tasks.php: -------------------------------------------------------------------------------- 1 | . 16 | /** 17 | * installation tool_inactive_user_cleanup tool caps. 18 | * 19 | * @package tool_inactive_user_cleanup 20 | * @author DualCube 21 | * @copyright DualCube (https://dualcube.com) 22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 | */ 24 | defined('MOODLE_INTERNAL') || die(); 25 | $tasks = array( 26 | array( 27 | 'classname' => 'tool_inactive_user_cleanup\task\tool_inactive_user_cleanup_task', 28 | 'blocking' => 0, 29 | 'minute' => '59', 30 | 'hour' => '23', 31 | 'day' => '*', 32 | 'dayofweek' => '*', 33 | 'month' => '*' 34 | ) 35 | ); -------------------------------------------------------------------------------- /version.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Plugin version info 19 | * 20 | * @package tool_inactive_user_cleanup 21 | * @author DualCube 22 | * @copyright DualCube (https://dualcube.com) 23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 | */ 25 | 26 | defined('MOODLE_INTERNAL') || die(); 27 | 28 | $plugin->version = 2023101000; // The current plugin version (Date: YYYYMMDDXX). 29 | $plugin->requires = 2014051200; // Requires this Moodle version. 30 | $plugin->component = 'tool_inactive_user_cleanup'; // Full name of the plugin (used for diagnostics). 31 | $plugin->maturity = MATURITY_STABLE; 32 | $plugin->release = '2.7.2 (Build: 2023101000)'; 33 | 34 | 35 | -------------------------------------------------------------------------------- /db/access.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Defines the capabilities of admin to use admin tool 19 | * 20 | * @package tool_inactive_user_cleanup 21 | * @author DualCube 22 | * @copyright DualCube (https://dualcube.com) 23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 | */ 25 | 26 | defined('MOODLE_INTERNAL') || die(); 27 | 28 | $capabilities = array( 29 | 'tool/inactive_user_cleanup:inactive_user_cleanup' => array( 30 | 'riskbitmask' => RISK_SPAM, 31 | 'captype' => 'write', 32 | 'contextlevel' => CONTEXT_SYSTEM, 33 | 'archetypes' => array( 34 | 'manager' => CAP_ALLOW 35 | ), 36 | 'clonepermissionsfrom' => 'moodle/site:inactive_user_cleanups', 37 | ), 38 | ); 39 | -------------------------------------------------------------------------------- /lang/en/tool_inactive_user_cleanup.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Strings for component 'tool_inactive_user_cleanup', language 'en', branch 'MOODLE_22_STABLE' 19 | * 20 | * @package tool_inactive_user_cleanup 21 | * @author DualCube 22 | * @copyright DualCube (https://dualcube.com) 23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 | */ 25 | 26 | $string['pluginname'] = 'Inactive User Cleanup'; 27 | $string['setting'] = 'Setting Panel'; 28 | $string['daysofinactivity'] = 'Days Of Inactivity'; 29 | $string['daysbeforedeletion'] = 'Days Before Deletion'; 30 | $string['deletiondescription'] = 'Put "0" to disable the cleanup option'; 31 | $string['emailsetting'] = 'Email Setting'; 32 | $string['emailsubject'] = 'Subject'; 33 | $string['emailbody'] = 'Body'; 34 | $string['runcron'] = 'Run Cron Manually'; 35 | $string['invalaliddayofinactivity'] = 'tool_inactive_user_cleanup disabled for putting invalid value in "Days Of Inactivity", the value should be greater than "0"'; 36 | $string['taskstart'] = 'Hey, admin tool inactive user cleanup is running'; 37 | $string['taskend'] = 'tool_inactive_user_cleanup task finished'; 38 | $string['detetsuccess'] = 'User_Delete Success'; 39 | $string['deleteduser'] = 'Deleted user '; 40 | $string['emailsent'] = 'Email sent'; 41 | $string['userinactivtime'] = 'User is inactive for past day '; 42 | $string['userid'] = 'USER ID '; 43 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * From for Inactive user cleanup email setting 19 | * 20 | * @package tool_inactive_user_cleanup 21 | * @author DualCube 22 | * @copyright DualCube (https://dualcube.com) 23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 | */ 25 | require_once('../../../config.php'); 26 | require_once($CFG->libdir.'/adminlib.php'); 27 | require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/inactive_user_cleanup/settings_form.php'); 28 | require_login(); 29 | admin_externalpage_setup('toolinactive_user_cleanup'); 30 | echo $OUTPUT->header(); 31 | $settingsform = new tool_inactive_user_cleanup_config_form(); 32 | $fromdata = $settingsform->get_data(); 33 | $configdata = get_config('tool_inactive_user_cleanup'); 34 | if (!empty($configdata->daysbeforedeletion)) { 35 | $data = new stdClass(); 36 | $data->config_daysbeforedeletion = $configdata->daysbeforedeletion; 37 | $data->config_daysofinactivity = $configdata->daysofinactivity; 38 | $data->config_subjectemail = $configdata->emailsubject; 39 | $data->config_bodyemail['text'] = $configdata->emailbody; 40 | $settingsform->set_data($data); 41 | } 42 | $settingsform->display(); 43 | 44 | if ($settingsform->is_submitted()) { 45 | set_config('daysbeforedeletion', $fromdata->config_daysbeforedeletion, 'tool_inactive_user_cleanup'); 46 | set_config('daysofinactivity', $fromdata->config_daysofinactivity, 'tool_inactive_user_cleanup'); 47 | set_config('emailsubject', $fromdata->config_subjectemail, 'tool_inactive_user_cleanup'); 48 | set_config('emailbody', $fromdata->config_bodyemail['text'], 'tool_inactive_user_cleanup'); 49 | } 50 | 51 | echo $OUTPUT->footer(); -------------------------------------------------------------------------------- /Readme.txt: -------------------------------------------------------------------------------- 1 | Admin Tool Inactive User Cleanup 2 | ================================ 3 | 4 | Installation 5 | ------------ 6 | Go to [ Site administration > Plugins(Plugins) > Install plugins ] and just upload or drag & drop downloaed ZIP file. 7 | To install, place all downloaded files in /admin/tool/inactive_user_cleanup and visit /admin/index.php in your browser. 8 | This block is written by DualCube. 9 | 10 | Overview 11 | -------- 12 | This plugin deletes inactive user accounts after the days which admin decided on setting and send a mail to user for login before deletion date. This cleanup process runs with Moodle cron job. 13 | 14 | Setup 15 | ----- 16 | In the first step admin user of the site setup days of inactivity, which decided the users can get mail after that days of inactivity.(if it set to "0" the clean up process will be disable) 17 | Next set "Days Before Deletion" which is the notice period for inactive user before deletion.(if it set to "0" then deletion will be disable) 18 | Then drafting notification mail for all users from the Site administration > Reports > Inactive User Cleanup. 19 | If an inactive user is found he/she gets the notification mail. 20 | 21 | Clean up 22 | -------- 23 | After getting notification mail, if the user still has not accessed the moodle site,then the deletion process starts. 24 | The particular inactive user account entry is removed with next run of this cleanup process. 25 | This is automatically or manually run by cron process. 26 | 27 | Setting Panel 28 | ------------ 29 | [www.yoursitename/admin/tool/inactive_user_cleanup/index.php] / [Site administration > Reports(Reports) > Inactive User Cleanup 30 | If setting is require for this cleanup process. 31 | Days of Inactivity is set by the admin user. 32 | Days Before Deletion is set with zero when admin just wants to notify the inactive user for access the site i.e. in first step. 33 | After that when user wants to run cleanup process then Days Before Deletion will set by the admin user. 34 | 35 | Email setting 36 | ------------- 37 | Admin user must set the subject and body text of the email. Which the notified inactive user can get the mail with correct words. 38 | 39 | Cron process 40 | ------------ 41 | Admin user run cron job manually after set password for manual cron on [ Administration > Security > Site security settings > Cron password for remote access ] from https://site.example.com/admin/cron.php?password=opensesame (replace "opensesame" with your cron password) 42 | 43 | Uninstall 44 | --------- 45 | Admin can uninstall this admin tool from- Site administration > Plugins > Admin tools > Manage Admin Tools [ Inactive User Cleanup ] 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /settings_form.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * From for Inactive user cleanup email setting 19 | * 20 | * @package tool_inactive_user_cleanup 21 | * @author DualCube 22 | * @copyright DualCube (https://dualcube.com) 23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 | */ 25 | 26 | defined('MOODLE_INTERNAL') || die(); 27 | require_once($CFG->libdir.'/formslib.php'); 28 | require_once($CFG->dirroot . '/user/editlib.php'); 29 | /** 30 | * Email Cofiguration Form for Inactive user cleanup. 31 | * 32 | * @copyright DualCube (https://dualcube.com) 33 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 | */ 35 | class tool_inactive_user_cleanup_config_form extends moodleform { 36 | /** 37 | * Definition. 38 | */ 39 | public function definition () { 40 | $mform = $this->_form; 41 | $mform->addElement('header', 'configheader', get_string('setting', 'tool_inactive_user_cleanup')); 42 | $mform->addElement('text', 'config_daysofinactivity', get_string('daysofinactivity', 'tool_inactive_user_cleanup')); 43 | $mform->addElement('text', 'config_daysbeforedeletion', get_string('daysbeforedeletion', 'tool_inactive_user_cleanup')); 44 | $mform->addElement('static', 'description','' ,get_string('deletiondescription', 'tool_inactive_user_cleanup')); 45 | $mform->setDefault('config_daysofinactivity', '365'); 46 | $mform->setType('config_daysofinactivity', PARAM_INT); 47 | $mform->setDefault('config_daysbeforedeletion', '10'); 48 | $mform->setType('config_daysbeforedeletion', PARAM_INT); 49 | $mform->addElement('header', 'config_headeremail', get_string('emailsetting', 'tool_inactive_user_cleanup')); 50 | $mform->addElement('text', 'config_subjectemail', get_string('emailsubject', 'tool_inactive_user_cleanup')); 51 | $editoroptions = array('trusttext' => true, 'subdirs' => true, 'maxfiles' => 1, 52 | 'maxbytes' => 1024); 53 | $mform->addElement('editor', 'config_bodyemail', get_string('emailbody', 'tool_inactive_user_cleanup'), $editoroptions); 54 | $mform->setType('config_subjectemail', PARAM_TEXT); 55 | $mform->setDefault('config_subjectemail', 'subject'); 56 | $mform->setType('config_bodyemail', PARAM_RAW); 57 | $mform->setDefault('config_bodyemail', 'body'); 58 | $this->add_action_buttons(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /classes/privacy/provider.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Inactive user cleanup library 19 | * 20 | * @package tool_inactive_user_cleanup 21 | * @author DualCube 22 | * @copyright DualCube (https://dualcube.com) 23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 | */ 25 | defined('MOODLE_INTERNAL') || die(); 26 | /* 27 | * Standard cron function 28 | */ 29 | namespace tool_inactive_user_cleanup\privacy; 30 | use core_privacy\local\metadata\collection; 31 | /** 32 | * Privacy Subsystem implementation for tool_inactive_user_cleanup. 33 | * 34 | * @copyright DualCube (https://dualcube.com) 35 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 | */ 37 | class provider implements 38 | \core_privacy\local\metadata\provider, 39 | 40 | // The Enrolled Courses Block plugin contains user's enrolled courses. 41 | \core_privacy\local\request\plugin\provider, 42 | 43 | \core_privacy\local\request\core_userlist_provider { 44 | /** 45 | * Returns meta data about this system. 46 | * 47 | * @param collection $collection The initialised collection to add items to. 48 | * @return collection A listing of user data stored through this system. 49 | */ 50 | public static function get_metadata(collection $collection) : collection { 51 | $collection->add_database_table( 52 | 'tool_inactive_user_cleanup', 53 | [ 54 | 'userid' => 'privacy:metadata:tool_inactive_user_cleanup:userid', 55 | 'emailsent' => 'privacy:metadata:tool_inactive_user_cleanup:emailsent', 56 | 'date' => 'privacy:metadata:tool_inactive_user_cleanup:date', 57 | ], 58 | 'privacy:metadata:tool_inactive_user_cleanup' 59 | ); 60 | return $collection; 61 | } 62 | /** 63 | * Delete all data for all users in the specified context. 64 | * 65 | * @param \context $context The specific context to delete data for. 66 | */ 67 | public static function delete_data_for_all_users_in_context(\context $context) { 68 | global $DB; 69 | if (empty($contextlist->count())) { 70 | return; 71 | } 72 | $userid = $context->get_user()->id; 73 | $DB->delete_records('tool_inactive_user_cleanup', array('userid' => $userid)); 74 | } 75 | 76 | /** 77 | * Delete all user data for the specified user, in the specified contexts. 78 | * 79 | * @param approved_contextlist $contextlist The approved contexts and user information to delete information for. 80 | */ 81 | public static function delete_data_for_user(approved_contextlist $contextlist) { 82 | global $DB; 83 | 84 | if (empty($contextlist->count())) { 85 | return; 86 | } 87 | $userid = $contextlist->get_user()->id; 88 | foreach ($contextlist->get_contexts() as $context) { 89 | $DB->delete_records('tool_inactive_user_cleanup', ['userid' => $userid]); 90 | } 91 | } 92 | /** 93 | * Delete all data for all users in the specified userlist. 94 | * 95 | * @param approved_userlist $userlist The specific userlist to delete data for. 96 | */ 97 | public static function delete_data_for_users(approved_userlist $userlist) { 98 | global $DB; 99 | 100 | $context = $userlist->get_context(); 101 | list($userinsql, $userinparams) = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED); 102 | $params = $userinparams; 103 | $sql = "userid {$userinsql}"; 104 | $DB->delete_records_select('tool_inactive_user_cleanup', $sql, $params); 105 | } 106 | 107 | } -------------------------------------------------------------------------------- /classes/task/tool_inactive_user_cleanup_task.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * The Inactive user cleanup library 19 | * 20 | * @package tool_inactive_user_cleanup 21 | * @author DualCube 22 | * @copyright DualCube (https://dualcube.com) 23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 | * tool_inactive_user_cleanup is standard cron function 25 | */ 26 | namespace tool_inactive_user_cleanup\task; 27 | defined('MOODLE_INTERNAL') || die(); 28 | 29 | 30 | /** 31 | * Scheduled task for Inactive user cleanup. 32 | * 33 | * @copyright DualCube (https://dualcube.com) 34 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 | */ 36 | class tool_inactive_user_cleanup_task extends \core\task\scheduled_task { 37 | /** 38 | * Get a descriptive name for this task (shown to admins). 39 | * 40 | * @return string 41 | */ 42 | public function get_name() { 43 | return get_string('pluginname', 'tool_inactive_user_cleanup'); 44 | } 45 | /** 46 | * Execute. 47 | */ 48 | public function execute() { 49 | global $DB, $CFG; 50 | mtrace(get_string('taskstart','tool_inactive_user_cleanup')); 51 | $beforedelete = get_config('tool_inactive_user_cleanup', 'daysbeforedeletion'); 52 | $inactivity = get_config('tool_inactive_user_cleanup', 'daysofinactivity'); 53 | if($inactivity>0){ 54 | $subject = get_config('tool_inactive_user_cleanup', 'emailsubject'); 55 | $body = get_config('tool_inactive_user_cleanup', 'emailbody'); 56 | $users = $DB->get_records('user', array('deleted' => '0')); 57 | $messagetext = html_to_text($body); 58 | $mainadminuser = get_admin(); 59 | foreach ($users as $usersdetails) { 60 | $minus = round((time() - $usersdetails->lastaccess) / 60 / 60 / 24); 61 | if ($minus > $inactivity) { 62 | $ischeck = $DB->get_record('tool_inactive_user_cleanup', array('userid' => $usersdetails->id)); 63 | if (!$ischeck && $usersdetails->lastaccess != 0) { 64 | $record = new \stdClass(); 65 | $record->userid = $usersdetails->id; 66 | if (email_to_user($usersdetails, $mainadminuser, $subject, $messagetext)) { 67 | mtrace(get_string('userid','tool_inactive_user_cleanup')); 68 | mtrace($usersdetails->id. '---' .$usersdetails->email); 69 | mtrace(get_string('userinactivtime','tool_inactive_user_cleanup') . $minus); 70 | mtrace(); 71 | $record->emailsent = 1; 72 | $record->date = time(); 73 | $lastinsertid = $DB->insert_record('tool_inactive_user_cleanup', $record, false); 74 | } 75 | } 76 | } 77 | if ($beforedelete != 0 && $usersdetails->lastaccess != 0) { 78 | $deleteuserafternotify = $DB->get_record('tool_inactive_user_cleanup', array('userid' => $usersdetails->id)); 79 | if (!empty($deleteuserafternotify)) { 80 | $mailssent = $deleteuserafternotify->date; 81 | $diff = round((time() - $mailssent) / 60 / 60 / 24); 82 | if ($diff > $beforedelete) { 83 | if (!isguestuser($usersdetails->id)) { 84 | delete_user($usersdetails); 85 | mtrace(get_string('deleteduser','tool_inactive_user_cleanup') . $usersdetails->id); 86 | mtrace(get_string('detetsuccess','tool_inactive_user_cleanup')); 87 | } 88 | } 89 | } 90 | } 91 | } 92 | }else{ 93 | mtrace(get_string('invalaliddayofinactivity','tool_inactive_user_cleanup')); 94 | } 95 | 96 | 97 | mtrace(get_string('taskend','tool_inactive_user_cleanup')); 98 | }//end of function execute() 99 | }// End of class 100 | --------------------------------------------------------------------------------