Skip to content

Commit

Permalink
$pdo->query to $pdo->runQuery edit
Browse files Browse the repository at this point in the history
  • Loading branch information
luukee committed Jul 26, 2022
1 parent d6d2cc4 commit 3f6818e
Show file tree
Hide file tree
Showing 28 changed files with 88 additions and 121 deletions.
95 changes: 31 additions & 64 deletions database/class-enp_quiz_db.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
class enp_quiz_Db extends PDO
{

public function __construct() {
public function __construct()
{
// check if a connection already exists
try {
// config file for connection info and necessary variables
include($_SERVER["DOCUMENT_ROOT"].'/enp-quiz-database-config.php');
include($_SERVER["DOCUMENT_ROOT"] . '/enp-quiz-database-config.php');
// Table names for dynamic reference
$this->quiz_table = $enp_quiz_table_quiz;
$this->quiz_option_table = $enp_quiz_table_quiz_option;
Expand All @@ -40,28 +41,35 @@ public function __construct() {
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// create the new connection
parent::__construct('mysql:host='.$enp_db_host.';dbname='.$enp_db_name,
$enp_db_user,
$enp_db_password,
$options);
parent::__construct(
'mysql:host=' . $enp_db_host . ';dbname=' . $enp_db_name,
// for windows users possible fix for PDO error, change 'mysql:host=' line above to:
// 'sqlsrv:Server=' . $enp_db_host . ';Database=' . $enp_db_name,
$enp_db_user,
$enp_db_password,
$options
);
} catch (Exception $e) {
$this->errors = $e->getMessage();
}
}

public function query($sql, $params = null) {
public function runQuery($sql, $params = null)
{
$stmt = $this->prepare($sql);
$stmt->execute($params);
return $stmt;
}

public function fetchOne($sql, $params = []) {
$stmt = $this->query($sql, $params);
public function fetchOne($sql, $params = [])
{
$stmt = $this->runQuery($sql, $params);
return $stmt->fetch(PDO::FETCH_ASSOC);
}

public function fetchAll($sql, $params = []) {
$stmt = $this->query($sql, $params);
public function fetchAll($sql, $params = [])
{
$stmt = $this->runQuery($sql, $params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

Expand All @@ -73,14 +81,7 @@ public function getQuizzes($where = [])
{

$params = $this->buildParams($where);
$sql = "SELECT * from ".$this->quiz_table." WHERE quiz_is_deleted = 0";

if($where) {
$sql .= $this->buildWhere($params, true);
}

return $this->fetchAll($sql, $params);
}
$sql = "SELECT * from " . $this->quiz_table . " WHERE quiz_is_deleted = 0";

if ($where) {
$sql .= $this->buildWhere($params, true);
Expand All @@ -97,15 +98,15 @@ public function getDomains($where = [])
{

$params = $this->buildParams($where);
$sql = "SELECT DISTINCT(SUBSTRING_INDEX((SUBSTRING_INDEX((SUBSTRING_INDEX(embed_site_url, '://', -1)), '/', 1)), '.', -2)) as domain from ".$this->embed_site_table;
if($where) {
$sql = "SELECT DISTINCT(SUBSTRING_INDEX((SUBSTRING_INDEX((SUBSTRING_INDEX(embed_site_url, '://', -1)), '/', 1)), '.', -2)) as domain from " . $this->embed_site_table;

if ($where) {
$sql .= $this->buildWhere($params, true);
}

return $this->fetchAll($sql, $params);
}

/*
* Get Sites
*
Expand All @@ -114,14 +115,7 @@ public function getSites($where = [])
{

$params = $this->buildParams($where);
$sql = "SELECT * from ".$this->embed_site_table;

if($where) {
$sql .= $this->buildWhere($params, true);
}

return $this->fetchAll($sql, $params);
}
$sql = "SELECT * from " . $this->embed_site_table;

if ($where) {
$sql .= $this->buildWhere($params, true);
Expand All @@ -138,14 +132,7 @@ public function getEmbeds($where = [])
{

$params = $this->buildParams($where);
$sql = "SELECT * from ".$this->embed_quiz_table;

if($where) {
$sql .= $this->buildWhere($params, true);
}

return $this->fetchAll($sql, $params);
}
$sql = "SELECT * from " . $this->embed_quiz_table;

if ($where) {
$sql .= $this->buildWhere($params, true);
Expand Down Expand Up @@ -179,30 +166,10 @@ public function getSliderQuestionsTotal()
return (int) $this->fetchOne($sql)['COUNT(*)'];
}

}
public function buildWhere($params, $where = true) {
$sql = '';
if($where === true) {
$sql = ' WHERE ';
}
if(!empty($params)) {
$i = 1;
foreach($params as $key => $val) {
if(is_array($val)) {
// for things like 'date > :date'
$sql .= $val['key'].' '.$val['operator'].' '.$val['val'];
} else {
$sql .= $key.' = '.$val;
}
if($i !== count($params)) {
// not the last one, so add an AND statement
$where .= " AND ";
$i++;
}
}
}
return $sql;
}
public function getUniqueUsersTotal()
{
$sql = "SELECT COUNT(DISTINCT user_id) as users
FROM " . $this->response_quiz_table;

return (int) $this->fetchOne($sql)['users'];
}
Expand Down
4 changes: 2 additions & 2 deletions database/class-enp_quiz_save_ab_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private function insert_ab_test($ab_test) {
:ab_test_updated_at
)";
// insert the quiz into the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

$this->response['action'] = 'insert';
// success!
Expand Down Expand Up @@ -277,7 +277,7 @@ private function delete_ab_test($ab_test) {
AND quiz_id_a = :quiz_id_a
AND quiz_id_b = :quiz_id_b";
// insert the quiz into the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// start the response
$this->response['action'] = 'update';
Expand Down
4 changes: 2 additions & 2 deletions database/class-enp_quiz_save_embed_quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ protected function insert_embed_quiz($embed_quiz) {
:embed_quiz_updated_at
)";
// insert the mc_option into the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down Expand Up @@ -245,7 +245,7 @@ protected function update_embed_quiz_loads($embed_quiz) {
embed_quiz_updated_at = :embed_quiz_updated_at
WHERE embed_quiz_id = :embed_quiz_id";
// insert the mc_option into the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down
2 changes: 1 addition & 1 deletion database/class-enp_quiz_save_embed_site.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ protected function insert_embed_site($embed_site) {
:embed_site_updated_at
)";
// insert the mc_option into the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down
4 changes: 2 additions & 2 deletions database/class-enp_quiz_save_mc_option.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ protected function insert_mc_option() {
:mc_option_responses
)";
// insert the mc_option into the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down Expand Up @@ -263,7 +263,7 @@ protected function update_mc_option() {
WHERE mc_option_id = :mc_option_id";
// update the mc_option in the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down
4 changes: 2 additions & 2 deletions database/class-enp_quiz_save_question.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ protected function insert_question() {
:question_time_spent_average
)';
// insert the question into the database
$stmt = $pdo->query( $sql, $params );
$stmt = $pdo->runQuery( $sql, $params );

// success!
if($stmt !== false ) {
Expand Down Expand Up @@ -475,7 +475,7 @@ protected function update_question() {
WHERE question_id = :question_id';
// update the question in the database
$stmt = $pdo->query( $sql, $params );
$stmt = $pdo->runQuery( $sql, $params );

// success!
if($stmt !== false ) {
Expand Down
8 changes: 4 additions & 4 deletions database/class-enp_quiz_save_quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ protected function insert_quiz() {
:quiz_is_deleted
)";
// insert the quiz into the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down Expand Up @@ -588,7 +588,7 @@ protected function update_quiz() {
AND quiz_owner = :quiz_owner
";

$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down Expand Up @@ -675,7 +675,7 @@ protected function pdo_publish_quiz() {
WHERE quiz_id = :quiz_id
AND quiz_owner = :quiz_owner";

$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down Expand Up @@ -1101,7 +1101,7 @@ protected function get_all_quiz_ab_tests($quiz_id) {
$sql = "SELECT ab_test_id from ".$pdo->ab_test_table."
WHERE (quiz_id_a = :quiz_id OR quiz_id_b = :quiz_id)
AND ab_test_is_deleted = 0";
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);
$ab_test_row = $stmt->fetchAll(PDO::FETCH_COLUMN);
// return the found quiz row
return $ab_test_row;
Expand Down
6 changes: 3 additions & 3 deletions database/class-enp_quiz_save_quiz_option.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function select_quiz_option($quiz_option) {
$sql = "SELECT quiz_option_id from ".$pdo->quiz_option_table."
WHERE quiz_id = :quiz_id
AND quiz_option_name = :quiz_option_name" ;
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);
$option_row = $stmt->fetch();
return $option_row;
}
Expand All @@ -80,7 +80,7 @@ protected function insert_quiz_option($quiz_option) {
:quiz_option_value
)";
// insert the quiz into the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down Expand Up @@ -116,7 +116,7 @@ protected function update_quiz_option($quiz_option, $quiz_option_id) {
AND quiz_option_name = :quiz_option_name
";

$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private function insert_response_ab_test($ab_test_id, $response_quiz_id) {
:ab_test_id
)";
// update the question view the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down
4 changes: 2 additions & 2 deletions database/class-enp_quiz_save_quiz_take_response_mc.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function insert_response_mc($response) {
:mc_option_id
)";
// insert the mc_option into the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down Expand Up @@ -87,7 +87,7 @@ protected function increase_mc_option_responses($mc_option_id) {
SET mc_option_responses = mc_option_responses + 1
WHERE mc_option_id = :mc_option_id";
// update the question view the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down
8 changes: 4 additions & 4 deletions database/class-enp_quiz_save_quiz_take_response_question.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function insert_response_question($response) {
:response_question_updated_at
)";
// insert the mc_option into the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down Expand Up @@ -235,7 +235,7 @@ public function update_response_question($response) {
response_question_updated_at = :response_question_updated_at
WHERE response_question_id = :response_question_id";
// insert the mc_option into the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down Expand Up @@ -310,7 +310,7 @@ protected function update_question_response_data($response) {
question_responses_incorrect_percentage = question_responses_incorrect/question_responses
WHERE question_id = :question_id";
// update the question view the database
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);

// success!
if($stmt !== false) {
Expand Down Expand Up @@ -340,7 +340,7 @@ protected function get_response_question_id($response) {
$sql = "SELECT response_question_id from ".$pdo->response_question_table." WHERE
response_quiz_id = :response_quiz_id
AND question_id = :question_id";
$stmt = $pdo->query($sql, $params);
$stmt = $pdo->runQuery($sql, $params);
$result = $stmt->fetch();
return $result['response_question_id'];
}
Expand Down
Loading

0 comments on commit 3f6818e

Please sign in to comment.