/
Token.php
173 lines (160 loc) · 4.7 KB
/
Token.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
*
* For code completion we add the available scenario's here
* Attributes
* @property int $tid
* @property string $firstname
* @property string $lastname
* @property string $email
* @property string $emailstatus
* @property string $token
* @property string $language
* @property string $blacklisted
* @property string $sent
* @property string $remindersent
* @property int $remindercount
* @property string $completed
* @property int $usesleft
* @property DateTime $validfrom
* @property DateTime $validuntil
*
* Relations
* @property Survey $survey The survey this token belongs to.
*
* Scopes
* @method Token incomplete() incomplete() Select only uncompleted tokens
* @method Token usable() usable() Select usable tokens: valid daterange and userleft > 0
*
*/
abstract class Token extends Dynamic
{
public function attributeLabels() {
$labels = array(
'tid' => gT('Token ID'),
'partcipant' => gt('Participant ID'),
'firstname' => gT('First name'),
'lastname' => gT('Last name'),
'email' => gT('Email address'),
'emailstatus' => gT('Email status'),
'token' => gT('Token'),
'language' => gT('Language code'),
'blacklisted' => gT('Blacklisted'),
'sent' => gT('Invitation sent date'),
'remindersent' => gT('Last reminder sent date'),
'remindercount' =>gT('Total numbers of sent reminders'),
'completed' => gT('Completed'),
'usesleft' => gT('Uses left'),
'validfrom' => gT('Valid from'),
'validuntil' => gT('Valid until'),
);
// Check if we have custom attributes.
if ($this->hasAttribute('attribute_1'))
{
foreach (unserialize($this->survey->attributedescriptions) as $key => $info)
{
$labels[$key] = $info['description'];
}
}
return $labels;
}
public function beforeDelete() {
$result = parent::beforeDelete();
if ($result && isset($this->surveylink))
{
if (!$this->surveylink->delete())
{
throw new CException('Could not delete survey link. Token was not deleted.');
}
return true;
}
return $result;
}
public function findByToken($token)
{
return $this->findByAttributes(array(
'token' => $token
));
}
public function generateToken()
{
$length = $this->survey->tokenlength;
$this->token = randomChars($length);
$counter = 0;
while (!$this->validate('token'))
{
$this->token = randomChars($length);
$counter++;
// This is extremely unlikely.
if ($counter > 10)
{
throw new CHttpException(500, 'Failed to create unique token in 10 attempts.');
}
}
}
/**
*
* @param mixed $className Either the classname or the survey id.
* @return Token
*/
public static function model($className = null) {
return parent::model($className);
}
/**
*
* @param int $surveyId
* @param string $scenario
* @return Token Description
*/
public static function create($surveyId, $scenario = 'insert') {
return parent::create($surveyId, $scenario);
}
public function relations()
{
$result = array(
'responses' => array(self::HAS_MANY, 'Response_' . $this->id, array('token' => 'token')),
'survey' => array(self::BELONGS_TO, 'Survey', '', 'on' => "sid = {$this->id}"),
'surveylink' => array(self::BELONGS_TO, 'SurveyLink', array('participant_id' => 'participant_id'), 'on' => "survey_id = {$this->id}")
);
return $result;
}
public function rules()
{
return array(
array('token', 'unique', 'allowEmpty' => true),
array(implode(',', $this->tableSchema->columnNames), 'safe')
);
}
public function scopes()
{
return array(
'incomplete' => array(
'condition' => 'completed = "N"'
),
'usable' => array(
'condition' => 'usesleft > 0 AND COALESCE(validuntil, NOW()) >= NOW() AND COALESCE(validfrom, NOW()) <= NOW()'
),
'editable' => array(
'condition' => 'COALESCE(validuntil, NOW()) >= NOW() AND COALESCE(validfrom, NOW()) <= NOW()'
)
);
}
public function summary()
{
$criteria = $this->getDbCriteria();
$criteria->select = array(
"COUNT(*) as count",
"COUNT(CASE WHEN (token IS NULL OR token='') THEN 1 ELSE NULL END) as invalid",
"COUNT(CASE WHEN (sent!='N' AND sent<>'') THEN 1 ELSE NULL END) as sent",
"COUNT(CASE WHEN (emailstatus LIKE 'OptOut%') THEN 1 ELSE NULL END) as optout",
"COUNT(CASE WHEN (completed!='N' and completed<>'') THEN 1 ELSE NULL END) as completed"
);
$command = $this->getCommandBuilder()->createFindCommand($this->getTableSchema(),$criteria);
return $command->queryRow();
}
public function tableName()
{
return '{{tokens_' . $this->id . '}}';
}
}
?>