forked from SFWLtd/plg_user_teamroles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeamRolesUpdater.php
146 lines (130 loc) · 5.53 KB
/
TeamRolesUpdater.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* @version 1.3
* @package plg_user_teamroles
* @author Simon Champion
* @copyright SFW Ltd
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPL
* @requires Joomdle >= 1.0.6
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
require_once(JPATH_SITE.'/components/com_joomdle/helpers/content.php');
class TeamRolesUpdater
{
private $userInfo = null;
public function setUserInfo(TeamRolesUserInfo $userInfo)
{
$this->userInfo = $userInfo;
}
public function setComparitorUserInfo(TeamRolesUserInfo $userInfo)
{
$this->userInfo->addComparitor($userInfo);
}
public function updateRoles()
{
if ($this->userInfo->sameAsComparitor()) {
//nothing changed, so bail out early.
return;
}
foreach ($this->userInfo->groupsAdded() as $group) {
if ($this->userInfo->updated->iAmAParent) {
$this->giveMeParentRoleForGroup($group);
} else {
$this->giveMeChildRoleForGroup($group);
}
}
foreach ($this->userInfo->groupsRemoved() as $group) {
if ($this->userInfo->iAmAParent) {
$this->removeMyParentRoleForGroup($group);
} else {
$this->removeMyChildRoleForGroup($group);
}
}
if ($this->userInfo->isParentRoleChanging()) {
foreach ($this->userInfo->groupsUnchanged() as $group) {
if ($this->userInfo->isBecomingAParent()) {
$this->giveMeParentRoleForGroup($group);
}
if ($this->userInfo->isNoLongerAParent()) {
$this->removeMyParentRoleForGroup($group);
}
}
}
}
public function resync()
{
$comparitor = clone $this->userInfo;
$this->setComparitorUserInfo($comparitor);
$currentParentSetting = $this->userInfo->iAmAParent;
//switch off their team leader flag and update the Moodle team...
$this->userInfo->updated->iAmAParent = !$currentParentSetting;
$this->updateRoles();
//...then switch it back on and update again.
$this->userInfo->iAmAParent = !$currentParentSetting;
$this->userInfo->updated->iAmAParent = $currentParentSetting;
$this->updateRoles();
//we're done so don't really need to set this back to original value, but be polite to the calling method.
$this->userInfo->iAmAParent = $currentParentSetting;
}
/**
* User has been been given group leader rights, so need to give him parent roles for everyone in his group.
* So look at all the other users in the group and grant him parent connection to them.
*/
private function giveMeParentRoleForGroup($group)
{
foreach (TeamRolesUserInfo::usersInGroup($group) as $user) {
if ($this->userInfo->loadTeamRoleToggleFromProfile($user)) {
self::joomdleAddParentRole(JFactory::getUser($user)->get('username'), $this->userInfo->username);
}
}
}
/**
* User has been had his group leader rights revoked, so need to unpick all of his parent roles.
* So look at all the other users in the group and remove his parent connection to them.
*/
private function removeMyParentRoleForGroup($group)
{
foreach (TeamRolesUserInfo::usersInGroup($group) as $user) {
self::joomdleRemoveParentRole(JFactory::getUser($user)->get('username'), $this->userInfo->username);
}
}
/**
* User has been added to a group, so need to implement the parent roles for that group to point to him.
* So look at the other users in the group, from them, anyone who is marked as having the privs need to gain parent connection with this user.
*/
private function giveMeChildRoleForGroup($group)
{
foreach (TeamRolesUserInfo::usersInGroup($group) as $user) {
if (in_array($this->userInfo->configAccess, JAccess::getAuthorisedViewLevels($user))) {
$username = JFactory::getUser($user)->get('username');
$teamLeaderInfo = new TeamRolesUserInfo([], $user, $username); //config params arg not needed in this context.
$toggle = $teamLeaderInfo->loadTeamRoleToggleFromProfile($this->userInfo->userID);
if ($toggle) {
self::joomdleAddParentRole($this->userInfo->username, $username);
}
}
}
}
/**
* User has been removed from a group, so need to unpick the parent roles pointing to him that are associated with that group.
* So look at the other users in the group, from them, anyone who is marked as having the privs need to lose the parent connection with this user.
*/
private function removeMyChildRoleForGroup($group)
{
foreach (TeamRolesUserInfo::usersInGroup($group) as $user) {
if (in_array($this->userInfo->configAccess, JAccess::getAuthorisedViewLevels($user))) {
self::joomdleRemoveParentRole($this->userInfo->username, JFactory::getUser($user)->get('username'));
}
}
}
public static function joomdleAddParentRole($child, $parent)
{
JoomdleHelperContent::call_method('add_parent_role', $child, $parent);
}
public static function joomdleRemoveParentRole($child, $parent)
{
//NB: remove_parent_role was not part of the standard Joomdle package until 1.0.6.
JoomdleHelperContent::call_method('remove_parent_role', $child, $parent);
}
}