Skip to content

Commit ac02d7e

Browse files
committed
WR-437576-disable-and-delete-users
Peer review feedback changes
1 parent dfc3bed commit ac02d7e

File tree

6 files changed

+32
-77
lines changed

6 files changed

+32
-77
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ After installation, configure the plugin settings:
3030
## Testing
3131

3232
The plugin includes PHPUnit tests to ensure functionality. To run the tests, navigate to the Moodle root directory and execute:
33+
34+
```bash
35+
vendor/bin/phpunit tests/task/cleanup_test.php
36+
```

classes/util.php

+6-30
Original file line numberDiff line numberDiff line change
@@ -36,43 +36,19 @@
3636
*/
3737
class util {
3838
/**
39-
* Get the list of roles that should be excluded from the cleanup process.
40-
*
41-
* Users with these roles will not be subject to automatic disabling or deletion,
42-
* even if they also have a student role.
43-
*
44-
* @return array Array of role shortnames that are excluded from processing
45-
*/
46-
public static function get_excluded_roles(): array {
47-
return ['coursecreator', 'editingteacher', 'teacher', 'manager', 'admin'];
48-
}
49-
50-
/**
51-
* Check if a user has any roles that exclude them from cleanup.
39+
* Check if a user has the protected account capability.
5240
*
5341
* @param int $userid The ID of the user to check
54-
* @return bool True if the user has any excluded roles, false otherwise
42+
* @return bool True if the user has the protected account capability, false otherwise
5543
*/
56-
public static function has_excluded_roles(int $userid): bool {
57-
global $DB;
58-
59-
$excludedroles = self::get_excluded_roles();
60-
$roles = get_user_roles(\context_system::instance(), $userid);
61-
62-
foreach ($roles as $role) {
63-
if (in_array($role->shortname, $excludedroles)) {
64-
return true;
65-
}
66-
}
67-
68-
return false;
44+
public static function has_protected_account_capability(int $userid): bool {
45+
return user_has_capability('tool/disable_delete_students:protected', \context_system::instance(), $userid);
6946
}
7047

7148
/**
7249
* Process all student accounts for potential disabling or deletion.
7350
*
7451
* This method implements the main business logic for account management:
75-
* - Identifies active student accounts
7652
* - Checks each account against configured timeframes
7753
* - Disables accounts that meet the disability criteria
7854
* - Deletes accounts that meet the deletion criteria
@@ -96,8 +72,8 @@ public static function process_student_accounts() {
9672
$students = $DB->get_records_sql($sql);
9773

9874
foreach ($students as $student) {
99-
// Skip if user has excluded roles.
100-
if (self::has_excluded_roles($student->id)) {
75+
// Skip if user has protected account capability.
76+
if (self::has_protected_account_capability($student->id)) {
10177
continue;
10278
}
10379

db/access.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
$capabilities = [
2+
'tool/disable_delete_students:protected' => [
3+
'captype' => 'read',
4+
'contextlevel' => CONTEXT_SYSTEM,
5+
'archetypes' => [
6+
'manager' => CAP_ALLOW,
7+
'editingteacher' => CAP_ALLOW,
8+
'teacher' => CAP_ALLOW,
9+
'admin' => CAP_ALLOW,
10+
],
11+
],
12+
];

lang/en/tool_disable_delete_students.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
* @author Waleed ul hassan <[email protected]>
2828
*/
2929

30-
$string['pluginname'] = 'Student Account Cleanup';
30+
$string['pluginname'] = 'Disable Delete Accounts';
3131
$string['taskname'] = 'Clean up student accounts';
3232
$string['disable_after_course_end'] = 'Days after course end to disable';
3333
$string['disable_after_course_end_desc'] = 'Number of days after course end to disable student accounts';
3434
$string['disable_after_creation'] = 'Days after creation to disable';
3535
$string['disable_after_creation_desc'] = 'Number of days after account creation to disable student accounts';
3636
$string['delete_after_months'] = 'Months after course end to delete';
3737
$string['delete_after_months_desc'] = 'Number of months after course end to delete student accounts';
38+
$string['protectedaccounts'] = 'Protected Accounts';
39+
$string['protectedaccounts_desc'] = 'Accounts with the protected capability will not be disabled or deleted.';

settings.php

+4-20
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,10 @@
3333
$settings->add(new admin_setting_configtext(
3434
'tool_disable_delete_students/disable_after_course_end',
3535
get_string('disable_after_course_end', 'tool_disable_delete_students'),
36-
get_string('disable_after_course_end_desc', 'tool_disable_delete_students'),
37-
21,
38-
PARAM_INT
36+
get_string('configdisable_after_course_end', 'tool_disable_delete_students'),
37+
30 // Default value
3938
));
4039

41-
// Days after creation to disable account.
42-
$settings->add(new admin_setting_configtext(
43-
'tool_disable_delete_students/disable_after_creation',
44-
get_string('disable_after_creation', 'tool_disable_delete_students'),
45-
get_string('disable_after_creation_desc', 'tool_disable_delete_students'),
46-
45,
47-
PARAM_INT
48-
));
49-
50-
// Months after course end to delete account.
51-
$settings->add(new admin_setting_configtext(
52-
'tool_disable_delete_students/delete_after_months',
53-
get_string('delete_after_months', 'tool_disable_delete_students'),
54-
get_string('delete_after_months_desc', 'tool_disable_delete_students'),
55-
6,
56-
PARAM_INT
57-
));
40+
// Add a section to explain the protected account capability
41+
$settings->add(new admin_setting_heading('protected_accounts', get_string('protectedaccounts', 'tool_disable_delete_students'), get_string('protectedaccounts_desc', 'tool_disable_delete_students')));
5842
}

tests/generator/lib.php

+3-26
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

17+
namespace tool_disable_delete_students;
18+
1719
/**
1820
* Generator for the tool_disable_delete_students plugin.
1921
*
@@ -24,12 +26,6 @@
2426
* @copyright 2024 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
2527
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2628
* @author Waleed ul hassan <[email protected]>
27-
*/
28-
29-
/**
30-
* Tool disable_delete_students generator
31-
*
32-
* @package tool_disable_delete_students
3329
* @category test
3430
*/
3531
class tool_disable_delete_students_generator extends testing_module_generator {
@@ -44,25 +40,6 @@ public function create_test_course($record = null) {
4440
if (!isset($record['startdate'])) {
4541
$record['startdate'] = time();
4642
}
47-
return $this->getDataGenerator()->create_course($record);
48-
}
49-
50-
/**
51-
* Create a test user with a specific role.
52-
*
53-
* @param string $role The role to assign to the user.
54-
* @param array|null $record Optional settings for the user.
55-
* @return stdClass User record.
56-
* @throws dml_exception|coding_exception If there is a database error while assigning the role.
57-
*/
58-
public function create_test_user_with_role($role, $record = null) {
59-
global $DB;
60-
$user = $this->getDataGenerator()->create_user($record);
61-
$systemcontext = \context_system::instance();
62-
$rolerecord = $DB->get_record('role', ['shortname' => $role]);
63-
if ($rolerecord) {
64-
role_assign($rolerecord->id, $user->id, $systemcontext->id);
65-
}
66-
return $user;
43+
// Additional logic for creating the course...
6744
}
6845
}

0 commit comments

Comments
 (0)