~adamzammit/quexs/quexslinkcall

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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?php 
/**
 * Popup screen to manage calling and hanging up and assigning outcomes to calls
 *
 *
 *	This file is part of queXS
 *	
 *	queXS is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *	
 *	queXS is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with queXS; if not, write to the Free Software
 *	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 * @author Adam Zammit <adam.zammit@deakin.edu.au>
 * @copyright Deakin University 2007,2008
 * @package queXS
 * @subpackage user
 * @link http://www.deakin.edu.au/dcarf/ queXS was writen for DCARF - Deakin Computer Assisted Research Facility
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License (GPL) Version 2
 * 
 */

/**
 * Configuration file
 */
include ("config.inc.php");

/**
 * Database file
 */
include ("db.inc.php");

/** 
 * Authentication
 */
require ("auth-interviewer.php");


/**
 * XHTML functions
 */
include ("functions/functions.xhtml.php");

/**
 * Calendar functions
 */
include("functions/functions.calendar.php");

/**
 * Operator functions
 */
include("functions/functions.operator.php");

/**
 * Input functions
 */
include("functions/functions.input.php");

/**
 * LimeSurvey functions
 */
include("functions/functions.limesurvey.php");

/**
 * Display appropriate outcomes based on current call attempt status
 *
 * @param int $contacted 0 for not contacted, 1 for contacted (a person on the phone)
 * @param int $ca Call attempt id
 * @param int $case_id The Case id
 *
 */
function display_outcomes($contacted,$ca,$case_id)
{
	global $db;

	$completed = limesurvey_is_completed($case_id);

	//see if the case is completed
	if ($completed)
	{
		$sql = "SELECT outcome_id,description
			FROM outcome
			WHERE outcome_id = 10";
	}
	else if (limesurvey_is_quota_full($case_id))
	{
		$sql = "SELECT outcome_id,description
			FROM outcome
			WHERE outcome_id = 32";
	}
	else
	{
		//see if we have made an appointment on this call attempt
	
		$sql = "SELECT appointment_id
			FROM appointment
			WHERE completed_call_id IS NULL
			AND call_attempt_id = '$ca'";
	
		$rs = $db->GetAll($sql);
		
		$outcomes = $db->GetOne("SELECT q.outcomes FROM `questionnaire` as q JOIN `case` as c ON (c.questionnaire_id =q.questionnaire_id) WHERE c.case_id = $case_id");
	
		if (!empty($rs))
		{
			//we have an appointment made ... only select appointment ID's
			$sql = "SELECT outcome_id,description
				FROM outcome
				WHERE outcome_type_id = '5'
				AND outcome_id IN ($outcomes)";		
		}
		else
		{
			if ($contacted === false) $ctd = "";
			else
			{
				$contacted = bigintval($contacted);
				$ctd = "AND contacted = '$contacted'";
			}
		
			$sql = "SELECT outcome_id,description
					FROM outcome
					WHERE outcome_type_id != '5'
					$ctd
					AND outcome_id IN ($outcomes)
					AND outcome_id NOT IN(10,42,43,44,45)"; //don't show completed if not, hide max calls as the supposed to be automatic or admin
		}
	}
	$rs = $db->GetAll($sql);

	print "<div>";
	if (!empty($rs))
	{
		$lime_sid = get_limesurvey_id(get_operator_id());

		//Check to see if we have sent an email on this call and set the default outcome
		$sql = "SELECT 41
			FROM `case` as c, " . LIME_PREFIX . "tokens_$lime_sid as t
			WHERE t.sent = '$ca'
			AND c.case_id = $case_id
			AND t.token = c.token";

		$do = $db->GetOne($sql);

		if (isset($_GET['defaultoutcome'])) $do = bigintval($_GET['defaultoutcome']);
		foreach($rs as $r)
		{
			if ($do == $r['outcome_id']) $selected = "checked='checked'"; else $selected = "";
			print "<div><label class='label'><input type='radio' class='radio' name='outcome' id='outcome-{$r['outcome_id']}' value='{$r['outcome_id']}' $selected/>" . T_($r['description']) . "</label></div>";
		}
	}
	print "</div>";


}


//display the respondents phone numbers as a drop down list for this call

global $db;

$db->StartTrans();

$operator_id = get_operator_id();

if (isset($_POST['submit']))
{
	if (isset($_POST['contact_phone']))
	{
		$contact_phone_id = bigintval($_POST['contact_phone']);
		$call_attempt_id = get_call_attempt($operator_id,false);
		$respondent_id = get_respondent_id($call_attempt_id);
		$call_id = get_call($operator_id,$respondent_id,$contact_phone_id,true);
		if ($call_id)
		{
			if (is_voip_enabled($operator_id))
			{
				include("functions/functions.voip.php");
				$v = new voip();
				$v->connect(VOIP_SERVER);
				$v->dial(get_extension($operator_id),get_call_number($call_id));
			}
			
      $btext = "onload='openParentObject(\"main-content\",\"" . get_respondentselection_url($operator_id) . "\"); parent.closePopup();'";

			$js = "js/window.js";
			if (browser_ie()) $js = "js/window_ie6.js";
			xhtml_head(T_("Call"),true,array("css/call.css"),array($js),$btext,false,false,false,false);
		}
	}
	else if (isset($_POST['outcome']))
	{
		$outcome_id = bigintval($_POST['outcome']);
		end_call($operator_id,$outcome_id);
		if (is_voip_enabled($operator_id))
		{
			include("functions/functions.voip.php");
			$v = new voip();
			$v->connect(VOIP_SERVER);
			$v->hangup(get_extension($operator_id));
		}
		//disable recording
		$newtext = T_("Start REC");

		$js = "js/window.js";
		if (browser_ie()) $js = "js/window_ie6.js";

		xhtml_head(T_("Call"),true,array("css/call.css"),array($js),"onload='toggleRec(\"$newtext\",\"record.php?start=start\",\"offline\"); openParentObject(\"main-content\",\"" . get_respondentselection_url($operator_id) . "\"); parent.closePopup();'",false,false,false,false);
	}
	else
	{
		//if no outcome selected, just hang up the call
		if (is_voip_enabled($operator_id))
		{
			include("functions/functions.voip.php");
			$v = new voip();
			$v->connect(VOIP_SERVER);
			$v->hangup(get_extension($operator_id));
		}
		//disable recording
		$newtext = T_("Start REC");
		$js = "js/window.js";
		if (browser_ie()) $js = "js/window_ie6.js";
		xhtml_head(T_("Call"),true,array("css/call.css"),array($js),"onload='toggleRec(\"$newtext\",\"record.php?start=start\",\"offline\"); openParentObject(\"main-content\",\"" . get_respondentselection_url($operator_id) . "\"); parent.closePopup();'",false,false,false,false);

	}

	print "<p></p>"; //for XHTML
	xhtml_foot();
	$db->CompleteTrans();
	exit();
}

$call_attempt_id = get_call_attempt($operator_id,false);
$case_id = get_case_id($operator_id);

/**
 * Set the state manually if necessary (i.e if VOIP state is playing up)
 */
if (isset($_GET['newstate']))
{
	$ns = bigintval($_GET['newstate']);
	$sql = "UPDATE  `call`
		SET state = '$ns'
		WHERE case_id = '$case_id'
		AND operator_id = '$operator_id'
		AND call_attempt_id = '$call_attempt_id'
		AND outcome_id = '0'";
	$db->Execute($sql);
}

$js = "js/window.js";
if (browser_ie()) $js = "js/window_ie6.js";

xhtml_head(T_("Call"),true,array("css/call.css"),array($js,"include/jquery/jquery-1.4.2.min.js"),false,false,false,false,false);

$state = is_on_call($operator_id);
switch($state)
{
	case false: //no call made
	case 0: //not called -- shouldn't come here as we should create requesting call immediately
		print "<div class='status'>" . T_("Not on a call") . "</div>";


		//if we are on an appointment, we will just call the specified number for the appointment
		$appointment_id = is_on_appointment($call_attempt_id);

		if ($appointment_id)
		{
			if (isset($_GET['end']))
			{
				//end the case
				if (!isset($_GET['end'])) print "<div>" . T_("End work") . "</div>";
				print "<p><a href='javascript:openParent(\"endcase=endcase\")'>" . T_("End case") . "</a></p>";
				print "<p><a href='javascript:openParent(\"endwork=endwork\")'>" . T_("End work") . "</a></p>";
			}
			else
			{
				//determine whether to begin calling based on extension status
				$es = 1;
				if (is_voip_enabled($operator_id))
				{
					if (get_extension_status($operator_id))
						$es = 1;	
					else
						$es = 0;
				}

				if ($es)
				{

					$sql = "SELECT c.*
						FROM contact_phone as c, appointment as a
						WHERE a.appointment_id = '$appointment_id'
						AND a.contact_phone_id = c.contact_phone_id";
		
					$r = $db->GetRow($sql);
					
					print "<div>" . T_("Press the call button to dial the number for this appointment:") . "</div>";
		
					print "<form action='?' method='post'><div>";
					print "<p>" . T_("Number to call:") . " {$r['phone']} - {$r['description']}</p>";
					print "</div><div><input type='hidden' id='contact_phone' name='contact_phone' value='{$r['contact_phone_id']}'/><input type='submit' value=\"" . T_("Call") . "\" name='submit' id='submit'/></div></form>";
				}
				else
					print "<div>" . T_("Your VoIP extension is not enabled. Please close this window and enable VoIP by clicking once on the red button that says 'VoIP Off'") . "</div>";
			}
		}
		else
		{
			//determine whether we should make any more calls based on the last call outcome
	
			$sql = "SELECT o.tryanother, o.require_note
				FROM `call` as c, `outcome` as o
				WHERE c.call_attempt_id = '$call_attempt_id'
				AND c.outcome_id = o.outcome_id
				ORDER BY call_id DESC
				LIMIT 1";
	
			$rs = $db->GetRow($sql);
	
			if (!isset($_GET['end']) && (empty($rs) || $rs['tryanother'] == 1)) //dial another number only when available and not ending
			{
				$rn = 0;
				if (!empty($rs) && $rs['require_note'] == 1) $rn = 1;
	
				//an exclusion left join
				$sql = "SELECT c. *
					FROM contact_phone AS c
					LEFT JOIN (
						SELECT contact_phone.contact_phone_id
						FROM contact_phone
						LEFT JOIN `call` ON ( call.contact_phone_id = contact_phone.contact_phone_id )
						LEFT JOIN outcome ON ( call.outcome_id = outcome.outcome_id )
						WHERE contact_phone.case_id = '$case_id'
						AND outcome.tryagain =0
						) AS l ON l.contact_phone_id = c.contact_phone_id
					LEFT JOIN
						(
						SELECT contact_phone_id
						FROM `call`
						WHERE call_attempt_id = '$call_attempt_id'
						AND outcome_id != 18
						) as ca on ca.contact_phone_id = c.contact_phone_id
					WHERE c.case_id = '$case_id'
					AND l.contact_phone_id IS NULL
					AND ca.contact_phone_id IS NULL"; //only select numbers that should be tried again and have not been tried in this attempt which are not the accidental hang up outcome
	
					//could be updated to take in to account the time delay and outcome
	
				$rs = $db->GetAll($sql);
	
				if (!empty($rs))
				{
					//determine whether to begin calling based on extension status
					$es = 1;
					if (is_voip_enabled($operator_id))
					{
						if (get_extension_status($operator_id))
							$es = 1;	
						else
							$es = 0;
					}

					if ($es)
					{
						print "<div>" . T_("Select phone number to dial:") . "</div>";
			
						print "<form action='?' method='post'><div><select id='contact_phone' name='contact_phone'>";
						foreach($rs as $r)
						{
							print "<option value='{$r['contact_phone_id']}'>{$r['phone']} - {$r['description']}</option>";
						}
            print "</select></div><div><input type='submit' value=\"" . T_("Call") . "\" name='submit' id='submit'/></div></form>";
					}
					else
						print "<div>" . T_("Your VoIP extension is not enabled. Please close this window and enable VoIP by clicking once on the red button that says 'VoIP Off'") . "</div>";
				}
				else //no phone numbers left
				{
					//end the case
					print "<div>" . T_("The last call completed this call attempt") . "</div>";

					if ($rn) // a note is required to be entered
					{
						print "<div><label for='note'>" . T_("Enter a reason for this outcome before completing this case:") . "</label><input type='text' id='note' name='note' size='48'/><br/><br/><br/><br/></div>";
						//give focus on load
						print '<script type="text/javascript">$(document).ready(function(){$("#note").focus();});</script>';
						//put these lower on the screen so they don't get "automatically" clicked
						print "<p><a href='javascript:openParentNote(\"endcase=endcase\")'>" . T_("End case") . "</a></p>";
						print "<p><a href='javascript:openParentNote(\"endwork=endwork\")'>" . T_("End work") . "</a></p>";
					}
					else
					{
						print "<p><a href='javascript:openParent(\"endcase=endcase\")'>" . T_("End case") . "</a></p>";
						print "<p><a href='javascript:openParent(\"endwork=endwork\")'>" . T_("End work") . "</a></p";
					}
				}
			}
			else //don't try any more
			{
				$rn = 0;
				if (!empty($rs) && $rs['require_note'] == 1) $rn = 1;

				//end the case

				if ($rn) // a note is required to be entered
				{
					print "<div><label for='note'>" . T_("Enter a reason for this outcome before completing this case:") . "</label><input type='text' id='note' name='note' size='48'/><br/><br/><br/><br/></div>";
					print '<script type="text/javascript">$(document).ready(function(){$("#note").focus();});</script>';
					print "<p><a href='javascript:openParentNote(\"endcase=endcase\")'>" . T_("End case") . "</a></p>";
					print "<p><a href='javascript:openParentNote(\"endwork=endwork\")'>" . T_("End work") . "</a></p>";
				}
				else
				{
					if (!isset($_GET['end'])) print "<div>" . T_("The last call completed this call attempt") . "</div>";
					print "<p><a href='javascript:openParent(\"endcase=endcase\")'>" . T_("End case") . "</a></p>";
					print "<p><a href='javascript:openParent(\"endwork=endwork\")'>" . T_("End work") . "</a></p>";
				}
			}
		}
		break;
	case 1: //requesting call
		print "<div class='status'>" . T_("Requesting call") . "</div>";
		print "<div><a href='?newstate=3'>" . T_("Call Answered") . "</a></div>";
		print "<form action='?' method='post'>";
		display_outcomes(0,$call_attempt_id,$case_id);
		print "<div><input type='submit' value=\"" . T_("Hangup") . "\" name='submit' id='submit'/></div></form>";
		break;
	case 2: //ringing
		print "<div class='status'>" . T_("Ringing") . "</div>";
		print "<div><a href='?newstate=3'>" . T_("Call Answered") . "</a></div>";
		print "<form action='?' method='post'>";
		display_outcomes(0,$call_attempt_id,$case_id);
		print "<div><input type='submit' value=\"" . T_("Hangup") . "\" name='submit' id='submit'/></div></form>";
		break;
	case 3: //answered
		print "<div class='status'>" . T_("Answered") . "</div>";
		print "<div><a href='?newstate=2'>" . T_("Not Answered") . "</a></div>";
		print "<form action='?' method='post'>";
		display_outcomes(1,$call_attempt_id,$case_id);
		print "<div><input type='submit' value=\"" . T_("Hangup") . "\" name='submit' id='submit'/></div></form>";
		break;
	case 4: //requires coding
		print "<div class='status'>" . T_("Requires coding") . "</div>";
		print "<form action='?' method='post'>";
		display_outcomes(false,$call_attempt_id,$case_id);
		print "<div><input type='submit' value=\"" . T_("Assign outcome") . "\" name='submit' id='submit'/></div></form>";
		break;
	case 5: //done -- shouldn't come here as should be coded + done
	default:
		print "<div class='status'>" . T_("Error: Close window") . "</div>";
		break;

}


xhtml_foot();
$db->CompleteTrans();

?>