This repository was archived by the owner on Jul 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentBalancesEmailParents.php
298 lines (237 loc) · 7.71 KB
/
StudentBalancesEmailParents.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
* Email Studnt Balances to Parents
*
* @package Email module
*/
require_once 'ProgramFunctions/SendEmail.fnc.php';
if ( file_exists( 'ProgramFunctions/Template.fnc.php' ) )
{
// @since 3.6.
require_once 'ProgramFunctions/Template.fnc.php';
}
else
{
// @deprecated.
require_once 'modules/Email/includes/Template.fnc.php';
}
DrawHeader( ProgramTitle() );
// Send emails.
if ( isset( $_REQUEST['modfunc'] )
&& $_REQUEST['modfunc'] === 'save'
&& AllowEdit() )
{
if ( isset( $_POST['student'] ) )
{
SaveTemplate( $_REQUEST['inputstudentbalancesemailtext'] );
$message = str_replace( "''", "'", $_REQUEST['inputstudentbalancesemailtext'] );
$st_list = '\'' . implode( '\',\'', $_REQUEST['student'] ) . '\'';
// SELECT Staff details.
$extra['SELECT'] .= ",(SELECT st.FIRST_NAME||' '||st.LAST_NAME
FROM STAFF st,STUDENTS_JOIN_USERS sju
WHERE sju.STAFF_ID=st.STAFF_ID
AND s.STUDENT_ID=sju.STUDENT_ID
AND st.SYEAR='" . UserSyear() . "' LIMIT 1) AS PARENT_NAME";
$extra['SELECT'] .= ",(SELECT st.EMAIL
FROM STAFF st,STUDENTS_JOIN_USERS sju
WHERE sju.STAFF_ID=st.STAFF_ID
AND s.STUDENT_ID=sju.STUDENT_ID
AND st.SYEAR='" . UserSyear() . "' LIMIT 1) AS PARENT_EMAIL";
// SELECT Student Balance.
$extra['SELECT'] .= ',(COALESCE((SELECT SUM(f.AMOUNT)
FROM BILLING_FEES f
WHERE f.STUDENT_ID=ssm.STUDENT_ID
AND f.SYEAR=ssm.SYEAR),0)
-COALESCE((SELECT SUM(p.AMOUNT)
FROM BILLING_PAYMENTS p
WHERE p.STUDENT_ID=ssm.STUDENT_ID
AND p.SYEAR=ssm.SYEAR),0)) AS BALANCE';
$extra['WHERE'] = " AND s.STUDENT_ID IN (" . $st_list . ")";
// Call functions to format Columns.
$extra['functions'] = array( 'BALANCE' => '_makeCurrency' );
$student_RET = GetStuList( $extra );
// echo '<pre>'; var_dump($student_RET); echo '</pre>';
$error_email_list = array();
foreach ( (array) $student_RET as $student )
{
$to = $student['PARENT_EMAIL'];
$reply_to = null;
if ( filter_var( User( 'EMAIL' ), FILTER_VALIDATE_EMAIL ) )
{
$reply_to = User( 'NAME' ) . ' <' . User( 'EMAIL' ) . '>';
}
$subject = dgettext( 'Email', 'Student Balance' ) .
' - ' . $student['FIRST_NAME'] . ' ' . $student['LAST_NAME'];
// Substitutions.
$msg = str_replace(
array(
'__FIRST_NAME__',
'__LAST_NAME__',
'__SCHOOL_ID__',
'__PARENT_NAME__',
'__BALANCE__',
),
array(
$student['FIRST_NAME'],
$student['LAST_NAME'],
SchoolInfo( 'TITLE' ),
$student['PARENT_NAME'],
strip_tags( $student['BALANCE'] ), // Remove <!-- 0 --> comment.
),
$message
);
$result = SendEmail( $to, $subject, $msg, $reply_to );
if ( ! $result )
{
$error_email_list[] = $student['PARENT_NAME'] .
' (' . $student['PARENT_EMAIL'] . ')';
}
}
if ( ! empty( $error_email_list ) )
{
$error_email_list = implode( ', ', $error_email_list );
$error[] = sprintf(
dgettext( 'Email', 'Email not sent to: %s' ),
$error_email_list
);
}
$note[] = dgettext( 'Email', 'The student balances have been sent.' );
}
// No Users selected.
else
$error[] = _( 'You must choose at least one student.' );
unset( $_SESSION['_REQUEST_vars']['modfunc'] );
unset( $_REQUEST['modfunc'] );
}
// Display errors if any.
if ( isset( $error ) )
{
echo ErrorMessage( $error );
}
// Display notes if any.
if ( isset( $note ) )
{
echo ErrorMessage( $note, 'note' );
}
// Display Search screen or Student list.
if ( empty( $_REQUEST['modfunc'] )
|| $_REQUEST['search_modfunc'] === 'list' )
{
// Open Form & Display Email options.
if ( $_REQUEST['search_modfunc'] === 'list' )
{
echo '<form action="Modules.php?modname=' . $_REQUEST['modname'] . '&modfunc=save" method="POST">';
$extra['header_right'] = SubmitButton( dgettext( 'Email', 'Send Balances to Selected Parents' ) );
$extra['extra_header_left'] = '<table>';
$template = GetTemplate();
// Email Template Textarea.
$extra['extra_header_left'] .= '<tr class="st"><td>
<label><textarea name="inputstudentbalancesemailtext" cols="97" rows="5">' . $template . '</textarea>
<span class="legend-gray">' . dgettext( 'Email', 'Student Balance' ) . ' - ' . _( 'Email Text' ) . '</span></label>
</td></tr>';
// Spacing.
$extra['extra_header_left'] .= '<tr><td> </td></tr>';
// Substitutions.
$extra['extra_header_left'] .= '<tr class="st">
<td><table><tr class="st">';
$extra['extra_header_left'] .= '<td>__PARENT_NAME__</td>
<td>= ' . _( 'Parent Name' ) . '</td>
<td colspan="3"> </td>';
$extra['extra_header_left'] .= '</tr><tr class="st">';
$extra['extra_header_left'] .= '<td>__FIRST_NAME__</td>
<td>= ' . _( 'First Name' ) . '</td><td> </td>';
$extra['extra_header_left'] .= '<td>__LAST_NAME__</td>
<td>= ' . _( 'Last Name' ) . '</td>';
$extra['extra_header_left'] .= '</tr><tr class="st">';
$extra['extra_header_left'] .= '<td>__SCHOOL_ID__</td>
<td>= ' . _( 'School' ) . '</td>
<td> </td>';
$extra['extra_header_left'] .= '<td>__BALANCE__</td>
<td>= ' . dgettext( 'Email', 'Student Balance' ) . '</td>';
$extra['extra_header_left'] .= '</tr></table>
<span class="legend-gray">' . _( 'Substitutions' ) . '</span></td></tr>';
$extra['extra_header_left'] .= '</table>';
}
$extra['SELECT'] = ",s.STUDENT_ID AS CHECKBOX";
// SELECT Staff details.
$extra['SELECT'] .= ",(SELECT st.FIRST_NAME||' '||st.LAST_NAME
FROM STAFF st,STUDENTS_JOIN_USERS sju
WHERE sju.STAFF_ID=st.STAFF_ID
AND s.STUDENT_ID=sju.STUDENT_ID
AND st.SYEAR='" . UserSyear() . "' LIMIT 1) AS PARENT_NAME";
$extra['SELECT'] .= ",(SELECT st.EMAIL
FROM STAFF st,STUDENTS_JOIN_USERS sju
WHERE sju.STAFF_ID=st.STAFF_ID
AND s.STUDENT_ID=sju.STUDENT_ID
AND st.SYEAR='" . UserSyear() . "' LIMIT 1) AS PARENT_EMAIL";
// SELECT Student Balance.
$extra['SELECT'] .= ',(COALESCE((SELECT SUM(f.AMOUNT)
FROM BILLING_FEES f
WHERE f.STUDENT_ID=ssm.STUDENT_ID
AND f.SYEAR=ssm.SYEAR),0)
-COALESCE((SELECT SUM(p.AMOUNT)
FROM BILLING_PAYMENTS p
WHERE p.STUDENT_ID=ssm.STUDENT_ID
AND p.SYEAR=ssm.SYEAR),0)) AS BALANCE';
// ORDER BY Balance, Name.
$extra['ORDER_BY'] = 'BALANCE DESC, FULL_NAME';
// Call functions to format Columns.
$extra['functions'] = array( 'CHECKBOX' => '_makeChooseCheckbox', 'BALANCE' => '_makeCurrency' );
// Columns Titles.
$extra['columns_before'] = array(
'CHECKBOX' => '</a><input type="checkbox" value="Y" name="controller" onclick="checkAll(this.form,this.form.controller.checked,\'student\');" /><A>',
);
$extra['columns_after'] = array(
'BALANCE' => _( 'Balance' ),
'PARENT_NAME' => _( 'Parent Name' ),
'PARENT_EMAIL' => _( 'Email' ),
);
// No link for Student's name.
$extra['link'] = array( 'FULL_NAME' => false );
// Remove Current Student if any.
$extra['new'] = true;
// Display Search screen or Search Students.
Search( 'student_id', $extra );
// Submit & Close Form.
if ( $_REQUEST['search_modfunc'] === 'list' )
{
echo '<br /><div class="center">' .
SubmitButton( dgettext( 'Email', 'Send Balances to Selected Parents' ) ) . '</div>';
echo '</form>';
}
}
/**
* Make Choose Checkbox
*
* Local function
*
* @param string $value STUDENT_ID value.
* @param string $column 'CHECKBOX'.
*
* @return string Checkbox or empty string if no Email
*/
function _makeChooseCheckbox( $value, $column )
{
global $THIS_RET;
// If valid email.
if ( filter_var( $THIS_RET['PARENT_EMAIL'], FILTER_VALIDATE_EMAIL ) )
{
return '<input type="checkbox" name="student[' . $value . ']" value="' . $value . '" />';
}
else
return '';
}
/**
* Make Currency
*
* Local function
*
* @param string $value Balance value.
* @param string $column 'BALANCE'.
*
* @return string Formatted Balance value with Currency
*/
function _makeCurrency( $value, $column )
{
return Currency( $value * -1 );
}