From 5b7f03761fddadf44b5bfad5a58e4564383937e8 Mon Sep 17 00:00:00 2001 From: Adam Zammit Date: Thu, 26 Nov 2015 21:07:30 +1100 Subject: [PATCH] Fixed bug: Error inserting case due to token random generation collision Dev: Update to mt_rand function Dev: Multiple tries on failure to generate unique token --- functions/functions.operator.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/functions/functions.operator.php b/functions/functions.operator.php index ee056c3c..83758839 100644 --- a/functions/functions.operator.php +++ b/functions/functions.operator.php @@ -57,9 +57,9 @@ function sRandomChars($length = 15,$pattern="23456789abcdefghijkmnpqrstuvwxyz") for($i=0;$i<$length;$i++) { if(isset($key)) - $key .= $pattern{rand(0,$patternlength)}; + $key .= $pattern{mt_rand(0,$patternlength)}; else - $key = $pattern{rand(0,$patternlength)}; + $key = $pattern{mt_rand(0,$patternlength)}; } return $key; } @@ -354,8 +354,19 @@ function add_case($sample_id,$questionnaire_id,$operator_id = "NULL",$testing = { global $db; + $ttries = 0; + + do { $token = sRandomChars(); + $sql = "SELECT count(*) as c + FROM `case` + WHERE token = '$token'"; + + $ttries++; + } while ($db->GetOne($sql) > 0 && $ttries < 10); + + $sql = "INSERT INTO `case` (case_id, sample_id, questionnaire_id, last_call_id, current_operator_id, current_call_id, current_outcome_id,token) VALUES (NULL, $sample_id, $questionnaire_id, NULL, $operator_id, NULL, '$current_outcome_id','$token')"; @@ -716,6 +727,7 @@ function get_case_id($operator_id, $create = false) } $r2 = $db->GetRow($sql); + if (empty($r2)) {