1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program is free software; you can redistribute it and/or modify it under
7: * the terms of the GNU Affero General Public License version 3 as published by the
8: * Free Software Foundation with the addition of the following permission added
9: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16: * details.
17: *
18: * You should have received a copy of the GNU Affero General Public License along with
19: * this program; if not, see http://www.gnu.org/licenses or write to the Free
20: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21: * 02110-1301 USA.
22: *
23: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address [email protected].
25: *
26: * The interactive user interfaces in modified source and object code versions
27: * of this program must display Appropriate Legal Notices, as required under
28: * Section 5 of the GNU Affero General Public License version 3.
29: *
30: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31: * these Appropriate Legal Notices must retain the display of the "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37:
38: class ServiceRoutingBehavior extends CBehavior {
39:
40: /**
41: * Picks the next asignee based on the routing type
42: *
43: * @return string Username that should be assigned the next lead
44: */
45: public function getNextAssignee() {
46: $admin = &Yii::app()->settings;
47: $type = $admin->serviceDistribution;
48: if ($type == "") {
49: return "Anyone";
50: } elseif ($type == "evenDistro") {
51: return $this->evenDistro();
52: } elseif ($type == "trueRoundRobin") {
53: return $this->roundRobin();
54: } elseif($type=='singleUser') {
55: $user=User::model()->findByPk($admin->srrId);
56: if(isset($user)){
57: return $user->username;
58: }else{
59: return "Anyone";
60: }
61: } elseif($type=='singleGroup') {
62: $group=Groups::model()->findByPk($admin->sgrrId);
63: if(isset($group)){
64: return $group->id;
65: }else{
66: return "Anyone";
67: }
68: }
69: }
70:
71: /**
72: * Picks the next asignee such that the resulting routing distribution
73: * would be even.
74: *
75: * @return mixed
76: */
77: public function evenDistro() {
78: $admin = &Yii::app()->settings;
79: $online = $admin->serviceOnlineOnly;
80: Session::cleanUpSessions();
81: $usernames = array();
82: $sessions = Session::getOnlineUsers();
83: $users = X2Model::model('User')->findAll();
84: foreach ($users as $user) {
85: $usernames[] = $user->username;
86: }
87:
88: if ($online == 1) {
89: foreach ($usernames as $user) {
90: if (in_array($user, $sessions))
91: $users[] = $user;
92: }
93: }else {
94: $users = $usernames;
95: }
96:
97: $numbers = array();
98: foreach ($users as $user) {
99: if ($user != 'admin' && $user!='api') {
100: $actions = X2Model::model('Actions')
101: ->findAllByAttributes(array('assignedTo' => $user, 'complete' => 'No'));
102: if (isset($actions))
103: $numbers[$user] = count($actions);
104: else
105: $numbers[$user] = 0;
106: }
107: }
108: asort($numbers);
109: reset($numbers);
110: return key($numbers);
111: }
112:
113: /**
114: * Picks the next assignee in a round-robin manner.
115: *
116: * Users get a chance to be picked in this manner only if online. In the
117: * round-robin distribution of leads, the last person who was picked for
118: * a lead assignment is stored using {@link updateRoundRobin()}. If no
119: * one is online, the lead will be assigned to "Anyone".
120: * @return mixed
121: */
122: public function roundRobin() {
123: $admin = &Yii::app()->settings;
124: $online = $admin->serviceOnlineOnly;
125: Session::cleanUpSessions();
126: $usernames = array();
127: $sessions = Session::getOnlineUsers();
128: $users = X2Model::model('User')->findAll();
129: foreach ($users as $userRecord) {
130: //exclude admin from candidates
131: if ($userRecord->username != 'admin' && $userRecord->username!='api') $usernames[] = $userRecord->username;
132: }
133: if ($online == 1) {
134: $userList = array();
135: foreach ($usernames as $user) {
136: if (in_array($user, $sessions))
137: $userList[] = $user;
138: }
139: }else {
140: $userList = $usernames;
141: }
142: $srrId = $this->getRoundRobin();
143: if(count($userList)>0){
144: $i = $srrId % count($userList);
145: $this->updateRoundRobin();
146: return $userList[$i];
147: }else{
148: return "Anyone";
149: }
150: }
151:
152: /**
153: * Returns the round-robin state
154: * @return integer
155: */
156: public function getRoundRobin() {
157: $admin = &Yii::app()->settings;
158: $srrId = $admin->srrId;
159: return $srrId;
160: }
161:
162: /**
163: * Stores the round-robin state.
164: */
165: public function updateRoundRobin() {
166: $admin = &Yii::app()->settings;
167: $admin->srrId = $admin->srrId + 1;
168: $admin->save();
169: }
170:
171:
172: }
173: