From 54bea820c2ed2986220a19a9b7555cb921a5efd4 Mon Sep 17 00:00:00 2001 From: azammitdcarf Date: Fri, 29 May 2009 00:04:16 +0000 Subject: [PATCH] Removed reference to secondary database link $ldb This means that both the limesurvey and the quexs tables must be in the same database, with the same connection But has the advantage of being able to join records across tables easily --- admin/new.php | 3 +- admin/questionnaireprefill.php | 2 +- admin/quota.php | 4 +- admin/quotarow.php | 4 +- db.inc.php | 6 --- functions/functions.limesurvey.php | 86 +++++++++++------------------- functions/functions.operator.php | 7 +-- nocaseavailable.php | 2 +- 8 files changed, 38 insertions(+), 76 deletions(-) diff --git a/admin/new.php b/admin/new.php index fd72d2fc..d18a8842 100644 --- a/admin/new.php +++ b/admin/new.php @@ -49,7 +49,6 @@ include ("../functions/functions.xhtml.php"); */ include("../functions/functions.input.php"); -global $ldb; global $db; xhtml_head(T_("New: Create new questionnaire"),true,false,array("../js/new.js")); @@ -119,7 +118,7 @@ $sql = "SELECT s.sid as sid, sl.surveyls_title AS title AND sl.surveyls_language = 'en' ) WHERE s.active = 'Y'"; -$surveys = $ldb->GetAll($sql); +$surveys = $db->GetAll($sql); foreach($surveys as $s) { print ""; diff --git a/admin/questionnaireprefill.php b/admin/questionnaireprefill.php index 1eaa0f9b..8c6f98ef 100644 --- a/admin/questionnaireprefill.php +++ b/admin/questionnaireprefill.php @@ -143,7 +143,7 @@ if ($questionnaire_id != false) WHERE q.sid = '$lime_sid'"; - display_chooser($ldb->GetAll($sql),"sgqa","sgqa",true,"questionnaire_id=$questionnaire_id"); + display_chooser($db->GetAll($sql),"sgqa","sgqa",true,"questionnaire_id=$questionnaire_id"); if ($sgqa != false) { diff --git a/admin/quota.php b/admin/quota.php index 60fbd773..7487fc1a 100755 --- a/admin/quota.php +++ b/admin/quota.php @@ -182,7 +182,7 @@ if ($questionnaire_id != false) WHERE q.sid = '$lime_sid'"; - display_chooser($ldb->GetAll($sql),"sgqa","sgqa",true,"questionnaire_id=$questionnaire_id&sample_import_id=$sample_import_id"); + display_chooser($db->GetAll($sql),"sgqa","sgqa",true,"questionnaire_id=$questionnaire_id&sample_import_id=$sample_import_id"); if ($sgqa != false) { @@ -197,7 +197,7 @@ if ($questionnaire_id != false) WHERE q.qid = '$qid' AND l.lid = q.lid"; - $rs = $ldb->GetAll($sql); + $rs = $db->GetAll($sql); if (!isset($rs) || empty($rs)) print "

" . T_("No labels defined for this question") ."

"; diff --git a/admin/quotarow.php b/admin/quotarow.php index 4105cb5e..6332557e 100755 --- a/admin/quotarow.php +++ b/admin/quotarow.php @@ -194,7 +194,7 @@ if ($questionnaire_id != false) WHERE q.sid = '$lime_sid'"; - display_chooser($ldb->GetAll($sql),"sgqa","sgqa",true,"questionnaire_id=$questionnaire_id&sample_import_id=$sample_import_id"); + display_chooser($db->GetAll($sql),"sgqa","sgqa",true,"questionnaire_id=$questionnaire_id&sample_import_id=$sample_import_id"); if ($sgqa != false) { @@ -225,7 +225,7 @@ if ($questionnaire_id != false) WHERE q.qid = '$qid' AND l.lid = q.lid"; - $rs = $ldb->GetAll($sql); + $rs = $db->GetAll($sql); if (!isset($rs) || empty($rs)) print "

" . T_("No labels defined for this question") ."

"; diff --git a/db.inc.php b/db.inc.php index 42361e9f..00adc8bb 100644 --- a/db.inc.php +++ b/db.inc.php @@ -72,12 +72,6 @@ $db->Connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); $db->SetFetchMode(ADODB_FETCH_ASSOC); if (DEBUG == true) $db->debug = true; -//global database variable for limesurvey -$ldb = newADOConnection(LDB_TYPE); -$ldb->Connect(LDB_HOST, LDB_USER, LDB_PASS, LDB_NAME); -$ldb->SetFetchMode(ADODB_FETCH_ASSOC); -if (DEBUG == true) $ldb->debug = true; - //store session in database (see sessions2 table) ADOdb_Session::config(DB_TYPE, DB_HOST, DB_USER, DB_PASS, DB_NAME,$options=false); diff --git a/functions/functions.limesurvey.php b/functions/functions.limesurvey.php index ec1ebbe1..b152ac50 100644 --- a/functions/functions.limesurvey.php +++ b/functions/functions.limesurvey.php @@ -59,42 +59,17 @@ include_once(dirname(__FILE__).'/../db.inc.php'); */ function limesurvey_quota_completions($lime_sgqa,$lime_sid,$questionnaire_id,$sample_import_id,$value,$comparison) { - global $ldb; global $db; $sql = "SELECT count(*) as c - FROM " . LIME_PREFIX . "survey_$lime_sid - WHERE submitdate IS NOT NULL - AND `$lime_sgqa` $comparison '$value'"; + FROM " . LIME_PREFIX . "survey_$lime_sid as s + JOIN `case` as c ON (c.questionnaire_id = '$questionnaire_id') + JOIN `sample` as sam ON (c.sample_id = sam.sample_id AND sam.import_id = '$sample_import_id') + WHERE s.submitdate IS NOT NULL + AND s.token = c.case_id + AND s.`$lime_sgqa` $comparison '$value'"; - $sqm = "SELECT c.case_id as case_id - FROM `case` as c, `sample` as s - WHERE c.questionnaire_id = '$questionnaire_id' - AND c.sample_id = s.sample_id - AND s.import_id = '$sample_import_id'"; - - $r = $db->GetAll($sqm); - - if (!empty($r)) - { - $sql .= " AND ("; - $ccount = count($r); - $ccounter = 0; - foreach($r as $row) - { - $token = $row['case_id']; - $ccounter++; - $sql .= " token = '$token'"; - if ($ccounter < $ccount) - $sql .= " or "; - } - $sql .= ")"; - } - else - return false; - - - $rs = $ldb->GetRow($sql); + $rs = $db->GetRow($sql); if (isset($rs) && !empty($rs)) return $rs['c']; @@ -146,14 +121,14 @@ function getRandomID() */ function create_limesurvey_questionnaire($title) { - global $ldb; + global $db; // Get random ids until one is found that is not used do { $surveyid = getRandomID(); $isquery = "SELECT sid FROM ".db_table_name('surveys')." WHERE sid=$surveyid"; - $isresult = $ldb->Execute($isquery); + $isresult = $db->Execute($isquery); } while (!empty($isresult) && $isresult->RecordCount() > 0); @@ -170,7 +145,7 @@ function create_limesurvey_questionnaire($title) . "'N', '0', 'Y',\n" . "'att1', 'att2', \n" . "'Y', 'Y', 'Y','".date("Y-m-d")."','Y')"; - $isresult = $ldb->Execute($isquery); + $isresult = $db->Execute($isquery); // insert base language into surveys_language_settings $isquery = "INSERT INTO ".db_table_name('surveys_languagesettings') @@ -184,11 +159,11 @@ function create_limesurvey_questionnaire($title) . "'', '',\n" . "'', '',\n" . "'')"; - $isresult = $ldb->Execute($isquery); + $isresult = $db->Execute($isquery); // Insert into survey_rights $isrquery = "INSERT INTO ". LIME_PREFIX . "surveys_rights VALUES($surveyid,1,1,1,1,1,1,1)"; - $isrresult = $ldb->Execute($isrquery) or die ($isrquery."
".$ldb->ErrorMsg()); + $isrresult = $db->Execute($isrquery) or die ($isrquery."
".$db->ErrorMsg()); return $surveyid; } @@ -203,7 +178,7 @@ function create_limesurvey_questionnaire($title) */ function get_lime_id($case_id) { - global $ldb; + global $db; $lime_sid = get_lime_sid($case_id); if ($lime_sid == false) return false; @@ -212,7 +187,7 @@ function get_lime_id($case_id) FROM " . LIME_PREFIX . "survey_$lime_sid WHERE token = '$case_id'"; - $r = $ldb->GetRow($sql); + $r = $db->GetRow($sql); if (!empty($r) && isset($r['id'])) return $r['id']; @@ -232,7 +207,7 @@ function get_lime_id($case_id) */ function get_lime_tid($case_id) { - global $ldb; + global $db; $lime_sid = get_lime_sid($case_id); if ($lime_sid == false) return false; @@ -241,7 +216,7 @@ function get_lime_tid($case_id) FROM " . LIME_PREFIX . "tokens_$lime_sid WHERE token = '$case_id'"; - $r = $ldb->GetRow($sql); + $r = $db->GetRow($sql); if (!empty($r) && isset($r['tid'])) return $r['tid']; @@ -284,7 +259,7 @@ function get_lime_sid($case_id) */ function limesurvey_is_completed($case_id) { - global $ldb; + global $db; $lime_sid = get_lime_sid($case_id); if ($lime_sid == false) return false; @@ -293,7 +268,7 @@ function limesurvey_is_completed($case_id) FROM " . LIME_PREFIX . "tokens_$lime_sid WHERE token = '$case_id'"; - $r = $ldb->GetRow($sql); + $r = $db->GetRow($sql); if (!empty($r)) if ($r['completed'] != 'N') return true; @@ -314,13 +289,13 @@ function limesurvey_is_completed($case_id) */ function limesurvey_get_numberofquestions($lime_sid) { - global $ldb; + global $db; $sql = "SELECT count(qid) as c FROM " . LIME_PREFIX . "questions WHERE sid = '$lime_sid'"; - $r = $ldb->GetRow($sql); + $r = $db->GetRow($sql); if (!empty($r)) return $r['c']; @@ -337,7 +312,7 @@ function limesurvey_get_numberofquestions($lime_sid) */ function limesurvey_percent_complete($case_id) { - global $ldb; + global $db; $lime_sid = get_lime_sid($case_id); if ($lime_sid == false) return false; @@ -347,7 +322,7 @@ function limesurvey_percent_complete($case_id) WHERE sid = '$lime_sid' AND identifier = '$case_id'"; - $r = $ldb->GetRow($sql); + $r = $db->GetRow($sql); if (!empty($r)) { @@ -363,10 +338,10 @@ function limesurvey_percent_complete($case_id) function limesurvey_get_width($qid,$default) { - global $ldb; + global $db; $sql = "SELECT value FROM ".LIME_PREFIX."question_attributes WHERE qid = '$qid' and attribute = 'maximum_chars'"; - $r = $ldb->GetRow($sql); + $r = $db->GetRow($sql); if (!empty($r)) $default = $r['value']; @@ -378,10 +353,10 @@ function limesurvey_get_width($qid,$default) function limesurvey_fixed_width($lid) { - global $ldb; + global $db; $sql = "SELECT MAX(LENGTH(code)) as c FROM ".LIME_PREFIX."labels WHERE lid = $lid"; - $r = $ldb->GetRow($sql); + $r = $db->GetRow($sql); $val = 1; @@ -393,14 +368,14 @@ function limesurvey_fixed_width($lid) function limesurvey_create_multi(&$varwidth,&$vartype,$qid,$varname,$length,$type) { - global $ldb; + global $db; $sql = "SELECT * FROM ".LIME_PREFIX."answers WHERE qid = $qid ORDER BY sortorder ASC"; - $r = $ldb->GetAll($sql); + $r = $db->GetAll($sql); foreach($r as $Row) { @@ -471,7 +446,6 @@ function all_ascii( $stringIn ){ */ function limesurvey_export_fixed_width($questionnaire_id,$sample_import_id = false) { - global $ldb; global $db; //array of varname and width @@ -496,7 +470,7 @@ function limesurvey_export_fixed_width($questionnaire_id,$sample_import_id = fal AND type NOT LIKE 'X' ORDER BY gid,question_order ASC"; - $r = $ldb->GetAll($sql); + $r = $db->GetAll($sql); foreach ($r as $RowQ) { $type = $RowQ['type']; @@ -673,7 +647,7 @@ function limesurvey_export_fixed_width($questionnaire_id,$sample_import_id = fal } - $r = $ldb->GetAll($sql); + $r = $db->GetAll($sql); foreach($r as $Row) { diff --git a/functions/functions.operator.php b/functions/functions.operator.php index 719e6efe..6a5d6005 100644 --- a/functions/functions.operator.php +++ b/functions/functions.operator.php @@ -243,7 +243,6 @@ function get_case_id($operator_id, $create = false) { global $db; - global $ldb; $db->StartTrans(); @@ -441,11 +440,7 @@ function get_case_id($operator_id, $create = false) $sql = "INSERT INTO ".LIME_PREFIX."tokens_$lime_sid (tid,firstname,lastname,email,token,language,sent,completed,attribute_1,attribute_2,mpid) VALUES (NULL,'','','',$case_id,'en','N','N','','',NULL)"; - if (!$ldb->Execute($sql)) //if we cannot insert - { - $db->FailTrans(); - $case_id = false; - } + $db->Execute($sql); } } } diff --git a/nocaseavailable.php b/nocaseavailable.php index e4f06285..a386e2cd 100644 --- a/nocaseavailable.php +++ b/nocaseavailable.php @@ -162,7 +162,7 @@ if (!empty($rs)) { $sql = "SELECT count(*) FROM " . LIME_PREFIX ."tokens_{$r['lime_sid']}"; - $rs2 = $ldb->GetRow($sql); + $rs2 = $db->GetRow($sql); if (empty($rs2)) print "

" . T_("ERROR: No tokens table defined for LimeSurvey questionnaire") . " {$r['lime_sid']} " . T_("from questionnaire:") . " {$r['description']}

";