-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathteamroles.php
148 lines (125 loc) · 4.86 KB
/
teamroles.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
147
148
<?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(__DIR__.'/TeamRolesUpdater.php');
require_once(__DIR__.'/TeamRolesUserInfo.php');
require_once(__DIR__.'/TeamRolesAdminTab.php');
class plgUserTeamRoles extends JPlugin
{
private $teamRolesUpdater = null;
public function onUserBeforeSave($oldUser, $isNew, $newUser)
{
$activeUser = (!$oldUser['activation'] && !$oldUser['block']);
$userInfo = new TeamRolesUserInfo($this->params, $oldUser['id'], $oldUser['username'], $activeUser);
$this->teamRolesUpdater = new TeamRolesUpdater();
$this->teamRolesUpdater->setUserInfo($userInfo);
}
public function onUserAfterSave($user, $isnew, $success, $msg)
{
if (!$success) {
return;
}
$activeUser = (!$user['activation'] && !$user['block']);
JAccess::clearStatics();
$comparitor = new TeamRolesUserInfo($this->params, $user['id'], $user['username'], $activeUser);
$this->teamRolesUpdater->setComparitorUserInfo($comparitor);
$this->teamRolesUpdater->updateRoles();
}
/**
* Display parent/child info on the user management form, plus a button to re-sync them with Moodle.
*/
public function onContentPrepareForm($form, $data)
{
if (!($form instanceof JForm)) {
$this->_subject->setError('JERROR_NOT_A_FORM');
return false;
}
$userID = isset($data->id) ? $data->id : 0;
// Only show on the admin panel and for existing users.
$formName = $form->getName();
if (!in_array($formName, ['com_users.user', 'com_users.profile']) || !$userID) {
return true;
}
$userInfo = new TeamRolesUserInfo($this->params, $data->id, $data->username);
$teamRolesAdminTab = new TeamRolesAdminTab($userInfo, $formName === 'com_users.user');
$teamRolesAdminTab->addTeamTabToAdminForm($form);
$this->addAjaxJS();
return true;
}
private function addAjaxJS()
{
$token = JSession::getFormToken();
$js = <<<eof
jQuery(function() {
jQuery(".teamrole-toggle").click(function() {
var \$this = jQuery(this);
var \$recordInfo = \$this.data('info');
jQuery.ajax({
type: "POST",
data: \$recordInfo,
url: 'index.php?option=com_ajax&group=user&plugin=TeamRolesToggleUser&format=json&{$token}=1',
success: function(results) {
console.log(results);
\$this.toggle();
\$this.siblings().toggle();
}
});
});
jQuery(".teamrole-resync").click(function() {
var \$this = jQuery(this);
var \$recordInfo = \$this.data('info');
jQuery.ajax({
type: "POST",
data: \$recordInfo,
url: 'index.php?option=com_ajax&group=user&plugin=TeamRolesResync&format=json&{$token}=1',
success: function(results) {
console.log(results);
alert(results.data[0].synced ? "Resync completed successfully." : "An error occurred while resyncing.");
}
});
});
});
eof;
$doc = JFactory::getDocument();
$doc->addScriptDeclaration($js);
}
public function onAjaxTeamRolesResync()
{
if (!JSession::checkToken('get')) {
return ['error'=>'Invalid token'];
}
$input = JFactory::getApplication()->input;
$teamLeader = JFactory::getUser($input->getInt('teamLeaderID',0));
$userInfo = new TeamRolesUserInfo($this->params, $teamLeader->id, $teamLeader->username);
$this->teamRolesUpdater = new TeamRolesUpdater();
$this->teamRolesUpdater->setUserInfo($userInfo);
$this->teamRolesUpdater->resync();
return ['synced'=>true];
}
public function onAjaxTeamRolesToggleUser()
{
if (!JSession::checkToken('get')) {
return ['error'=>'Invalid token'];
}
$input = JFactory::getApplication()->input;
$on = ($input->getString('on','true') === 'true');
$teamLeader = JFactory::getUser($input->getInt('teamLeaderID',0));
$teamMember = JFactory::getUser($input->getInt('teamMemberID',0));
$userInfo = new TeamRolesUserInfo($this->params, $teamLeader->id, $teamLeader->username);
$result = $userInfo->saveTeamRoleToggleToProfile($teamMember->id, $on);
if ($result) {
TeamRolesUpdater::joomdleAddParentRole($teamMember->username, $teamLeader->username);
} else {
TeamRolesUpdater::joomdleRemoveParentRole($teamMember->username, $teamLeader->username);
}
return ['toggled'=>$result];
}
}