This repository has been 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
/
DisciplineLogEmailParents.php
304 lines (239 loc) · 7.55 KB
/
DisciplineLogEmailParents.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
299
300
301
302
303
304
<?php
/**
* Email Discipline Log to Parents
*
* @package Email module
*/
require_once 'modules/Discipline/includes/ReferralLog.fnc.php';
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['inputdisciplinelogemailtext'] );
$message = str_replace( "''", "'", $_REQUEST['inputdisciplinelogemailtext'] );
$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";
$extra['WHERE'] = " AND s.STUDENT_ID IN (" . $st_list . ")";
$student_RET = GetStuList( $extra );
// echo '<pre>'; var_dump($student_RET); echo '</pre>';
// Generate and get Discipline Logs.
$referral_logs = ReferralLogsGenerate( $extra );
$error_email_list = array();
$pdf_options = array(
'css' => true,
'margins' => array(),
'mode' => 3, // Save.
);
foreach ( (array) $student_RET as $student_id => $student )
{
$to = $student['PARENT_EMAIL'];
$reply_to = $cc = null;
if ( filter_var( User( 'EMAIL' ), FILTER_VALIDATE_EMAIL ) )
{
$reply_to = User( 'NAME' ) . ' <' . User( 'EMAIL' ) . '>';
}
$subject = _( 'Discpline Log' ) .
' - ' . $student['FULL_NAME'];
// Substitutions.
$msg = str_replace(
array(
'__FIRST_NAME__',
'__LAST_NAME__',
'__SCHOOL_ID__',
'__PARENT_NAME__',
),
array(
$student['FIRST_NAME'],
$student['LAST_NAME'],
SchoolInfo( 'TITLE' ),
$student['PARENT_NAME'],
),
$message
);
if ( isset( $referral_logs[ $student_id ] ) )
{
// Generate PDF.
$handle = PDFStart( $pdf_options );
echo $referral_logs[ $student_id ];
$pdf_file = PDFStop( $handle );
$pdf_name = $subject . '.pdf';
// Send Email.
$result = SendEmail(
$to,
$subject,
$msg,
$reply_to,
$cc,
array( array( $pdf_file, $pdf_name ) )
);
// Delete PDF file.
unlink( $pdf_file );
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
);
}
if ( empty( $referral_logs ) )
{
$error[] = _( 'No Students were found.' );
}
else
$note[] = dgettext( 'Email', 'The discpline logs 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="' . PreparePHP_SELF(
$_REQUEST,
array( 'search_modfunc' ),
array( 'modfunc' => 'save' )
) . '" method="POST">';
$extra['header_right'] = SubmitButton( dgettext( 'Email', 'Send Log to Selected Parents' ) );
$extra['extra_header_left'] = '<table>';
$template = GetTemplate();
// Email Template Textarea.
$extra['extra_header_left'] .= '<tr class="st"><td>
<label><textarea name="inputdisciplinelogemailtext" cols="97" rows="5">' . $template . '</textarea>
<span class="legend-gray">' . _( 'Discpline Log' ) . ' - ' . _( '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'] .= '</tr></table>
<span class="legend-gray">' . _( 'Substitutions' ) . '</span></td></tr>';
$extra['extra_header_left'] .= '</table>';
// Add Include in Discipline Log form.
$extra['extra_header_left'] .= '<table>' . ReferralLogIncludeForm() . '</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 Number of Referrals.
$extra['SELECT'] .= ',(SELECT COUNT(dr.ID)
FROM DISCIPLINE_REFERRALS dr
WHERE dr.STUDENT_ID=ssm.STUDENT_ID
AND dr.SYEAR=ssm.SYEAR) AS REFERRALS';
// ORDER BY Name.
$extra['ORDER_BY'] = 'REFERRALS, FULL_NAME';
// Call functions to format Columns.
$extra['functions'] = array( 'CHECKBOX' => '_makeChooseCheckbox' );
// 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(
'REFERRALS' => _( 'Number of Referrals' ),
'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 Log 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 or no Referrals
*/
function _makeChooseCheckbox( $value, $column )
{
global $THIS_RET;
// If valid email & has Referrals.
if ( filter_var( $THIS_RET['PARENT_EMAIL'], FILTER_VALIDATE_EMAIL )
&& intval( $THIS_RET['REFERRALS'] ) > 0 )
{
return '<input type="checkbox" name="student[' . $value . ']" value="' . $value . '" />';
}
else
return '';
}