Skip to content

Commit 3d79d8f

Browse files
committed
Added doc blocks for the files that were missing and did some
code quality changes
1 parent 71a0fba commit 3d79d8f

9 files changed

+527
-255
lines changed

classes/task/cleanup_students.php

+47-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,58 @@
11
<?php
2-
namespace tool_disable_delete_students\task;
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Scheduled task for managing student accounts lifecycle.
19+
*
20+
* This task handles the automated process of disabling and/or deleting student accounts
21+
* based on configured criteria within the tool_disable_delete_students plugin.
22+
*
23+
* @package tool_disable_delete_students
24+
* @copyright 2024 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
25+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26+
* @author Waleed ul hassan <[email protected]>
27+
*/
328

4-
defined('MOODLE_INTERNAL') || die();
29+
namespace tool_disable_delete_students\task;
530

31+
/**
32+
* Scheduled task class for cleaning up student accounts.
33+
*
34+
* @package tool_disable_delete_students
35+
*/
636
class cleanup_students extends \core\task\scheduled_task {
37+
38+
/**
39+
* Returns the name of the scheduled task.
40+
*
41+
* @return string The name of the task
42+
* @throws \coding_exception
43+
*/
744
public function get_name() {
845
return get_string('taskname', 'tool_disable_delete_students');
946
}
1047

48+
/**
49+
* Executes the scheduled task to process student accounts.
50+
*
51+
* This method triggers the account processing utility which handles
52+
* the disabling and/or deletion of student accounts based on configured rules.
53+
*
54+
* @return void
55+
*/
1156
public function execute() {
1257
\tool_disable_delete_students\util::process_student_accounts();
1358
}

classes/util.php

+86-38
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,156 @@
11
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Utility class for managing student account lifecycle operations.
19+
*
20+
* This class provides functionality for managing student accounts including:
21+
* - Identifying accounts for cleanup
22+
* - Processing account disabling and deletion based on configured rules
23+
* - Handling role-based exclusions
24+
*
25+
* @package core_admin
26+
* @copyright 2024 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
* @author Waleed ul hassan <[email protected]>
29+
*/
30+
231
namespace tool_disable_delete_students;
332

433
defined('MOODLE_INTERNAL') || die();
534

35+
/**
36+
* Utility class containing static methods for student account management.
37+
*/
638
class util {
739
/**
8-
* Get roles that should be excluded from cleanup
9-
* @return array
40+
* Get the list of roles that should be excluded from the cleanup process.
41+
*
42+
* Users with these roles will not be subject to automatic disabling or deletion,
43+
* even if they also have a student role.
44+
*
45+
* @return array Array of role shortnames that are excluded from processing
1046
*/
1147
public static function get_excluded_roles(): array {
1248
return ['coursecreator', 'editingteacher', 'teacher', 'manager', 'admin'];
1349
}
1450

1551
/**
16-
* Check if user has any excluded roles
17-
* @param int $userid
18-
* @return bool
52+
* Check if a user has any roles that exclude them from cleanup.
53+
*
54+
* @param int $userid The ID of the user to check
55+
* @return bool True if the user has any excluded roles, false otherwise
1956
*/
2057
public static function has_excluded_roles(int $userid): bool {
2158
global $DB;
22-
23-
$excluded_roles = self::get_excluded_roles();
59+
60+
$excludedroles = self::get_excluded_roles();
2461
$roles = get_user_roles(\context_system::instance(), $userid);
25-
62+
2663
foreach ($roles as $role) {
27-
if (in_array($role->shortname, $excluded_roles)) {
64+
if (in_array($role->shortname, $excludedroles)) {
2865
return true;
2966
}
3067
}
31-
68+
3269
return false;
3370
}
3471

3572
/**
36-
* Process student accounts
73+
* Process all student accounts for potential disabling or deletion.
74+
*
75+
* This method implements the main business logic for account management:
76+
* - Identifies active student accounts
77+
* - Checks each account against configured timeframes
78+
* - Disables accounts that meet the disability criteria
79+
* - Deletes accounts that meet the deletion criteria
80+
* - Respects role-based exclusions
3781
*/
3882
public static function process_student_accounts() {
3983
global $DB, $CFG;
4084

41-
$disable_after_course_end = get_config('tool_disable_delete_students', 'disable_after_course_end');
42-
$disable_after_creation = get_config('tool_disable_delete_students', 'disable_after_creation');
43-
$delete_after_months = get_config('tool_disable_delete_students', 'delete_after_months');
85+
$disableaftercourseend = get_config('tool_disable_delete_students', 'disable_after_course_end');
86+
$disableaftercreation = get_config('tool_disable_delete_students', 'disable_after_creation');
87+
$deleteaftermonths = get_config('tool_disable_delete_students', 'delete_after_months');
4488

45-
// Get all active student accounts
89+
// Get all active student accounts.
4690
$sql = "SELECT DISTINCT u.*
4791
FROM {user} u
4892
JOIN {role_assignments} ra ON ra.userid = u.id
4993
JOIN {role} r ON r.id = ra.roleid
5094
WHERE u.deleted = 0
5195
AND r.shortname = 'student'";
52-
96+
5397
$students = $DB->get_records_sql($sql);
5498

5599
foreach ($students as $student) {
56-
// Skip if user has excluded roles
100+
// Skip if user has excluded roles.
57101
if (self::has_excluded_roles($student->id)) {
58102
continue;
59103
}
60104

61-
$should_disable = false;
62-
$should_delete = false;
105+
$shoulddisable = false;
106+
$shoulddelete = false;
63107

64-
// Check course end date condition
65-
$latest_course_end = self::get_latest_course_end_date($student->id);
66-
if ($latest_course_end) {
67-
$days_since_course_end = (time() - $latest_course_end) / DAYSECS;
68-
if ($days_since_course_end > $disable_after_course_end) {
69-
$should_disable = true;
108+
// Check course end date condition.
109+
$latestcourseend = self::get_latest_course_end_date($student->id);
110+
if ($latestcourseend) {
111+
$dayssincecourseend = (time() - $latestcourseend) / DAYSECS;
112+
if ($dayssincecourseend > $disableaftercourseend) {
113+
$shoulddisable = true;
70114
}
71-
if ($days_since_course_end > ($delete_after_months * 30)) {
72-
$should_delete = true;
115+
if ($dayssincecourseend > ($deleteaftermonths * 30)) {
116+
$shoulddelete = true;
73117
}
74118
}
75119

76-
// Check account creation date condition
77-
$days_since_creation = (time() - $student->timecreated) / DAYSECS;
78-
if ($days_since_creation > $disable_after_creation) {
79-
$should_disable = true;
120+
// Check account creation date condition.
121+
$dayssincecreation = (time() - $student->timecreated) / DAYSECS;
122+
if ($dayssincecreation > $disableaftercreation) {
123+
$shoulddisable = true;
80124
}
81125

82-
if ($should_delete) {
126+
if ($shoulddelete) {
83127
delete_user($student);
84128
mtrace("Deleted user: " . $student->username);
85-
} elseif ($should_disable && !$student->suspended) {
129+
} else if ($shoulddisable && !$student->suspended) {
86130
$DB->set_field('user', 'suspended', 1, ['id' => $student->id]);
87131
mtrace("Disabled user: " . $student->username);
88132
}
89133
}
90134
}
91135

92136
/**
93-
* Get the latest course end date for a user
94-
* @param int $userid
95-
* @return int|null
137+
* Get the latest course end date for a specific user.
138+
*
139+
* Retrieves the most recent end date among all courses the user is enrolled in.
140+
* Only considers courses with valid end dates (enddate > 0).
141+
*
142+
* @param int $userid The ID of the user to check
143+
* @return int|null The timestamp of the latest course end date, or null if no valid end dates found
96144
*/
97145
private static function get_latest_course_end_date(int $userid): ?int {
98146
global $DB;
99-
147+
100148
$sql = "SELECT MAX(c.enddate) as latest_end
101149
FROM {course} c
102150
JOIN {enrol} e ON e.courseid = c.id
103151
JOIN {user_enrolments} ue ON ue.enrolid = e.id
104152
WHERE ue.userid = :userid AND c.enddate > 0";
105-
153+
106154
$result = $DB->get_field_sql($sql, ['userid' => $userid]);
107155
return $result ?: null;
108156
}

db/tasks.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Scheduled task definitions for the tool_disable_delete_students plugin.
19+
*
20+
* This file contains the configuration for scheduled tasks related to
21+
* the management of student accounts, specifically for disabling accounts
22+
* based on defined criteria.
23+
*
24+
* @package tool_disable_delete_students
25+
* @copyright 2024 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
26+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27+
* @author Waleed ul hassan <[email protected]>
28+
*/
29+
230
defined('MOODLE_INTERNAL') || die();
331

32+
// Define the scheduled tasks for the tool_disable_delete_students plugin.
433
$tasks = [
534
[
635
'classname' => 'tool_disable_delete_students\task\cleanup_students',
@@ -10,5 +39,5 @@
1039
'day' => '*',
1140
'month' => '*',
1241
'dayofweek' => '0',
13-
]
42+
],
1443
];

lang/en/tool_disable_delete_students.php

+28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
11
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Language strings for the tool_disable_delete_students plugin.
19+
*
20+
* This file contains the language strings used in the plugin for managing
21+
* student account lifecycle operations, including disabling and deleting
22+
* accounts based on defined criteria.
23+
*
24+
* @package tool_disable_delete_students
25+
* @copyright 2024 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
26+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27+
* @author Waleed ul hassan <[email protected]>
28+
*/
29+
230
$string['pluginname'] = 'Student Account Cleanup';
331
$string['taskname'] = 'Clean up student accounts';
432
$string['disable_after_course_end'] = 'Days after course end to disable';

settings.php

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Settings for the tool_disable_delete_students plugin.
19+
*
20+
* @package tool_disable_delete_students
21+
* @copyright 2024 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
* @author Waleed ul hassan <[email protected]>
24+
*/
25+
226
defined('MOODLE_INTERNAL') || die;
327

428
if ($hassiteconfig) {
529
$settings = new admin_settingpage('tool_disable_delete_students', get_string('pluginname', 'tool_disable_delete_students'));
630
$ADMIN->add('tools', $settings);
731

8-
// Days after course end to disable account
32+
// Days after course end to disable account.
933
$settings->add(new admin_setting_configtext(
1034
'tool_disable_delete_students/disable_after_course_end',
1135
get_string('disable_after_course_end', 'tool_disable_delete_students'),
@@ -14,7 +38,7 @@
1438
PARAM_INT
1539
));
1640

17-
// Days after creation to disable account
41+
// Days after creation to disable account.
1842
$settings->add(new admin_setting_configtext(
1943
'tool_disable_delete_students/disable_after_creation',
2044
get_string('disable_after_creation', 'tool_disable_delete_students'),
@@ -23,7 +47,7 @@
2347
PARAM_INT
2448
));
2549

26-
// Months after course end to delete account
50+
// Months after course end to delete account.
2751
$settings->add(new admin_setting_configtext(
2852
'tool_disable_delete_students/delete_after_months',
2953
get_string('delete_after_months', 'tool_disable_delete_students'),

0 commit comments

Comments
 (0)