function enumerateResult($training_id) { $db = new DB(); $query = "SELECT questions.data as question,questions.answer=exams.answer as result FROM exams " . "INNER JOIN questions ON questions.id=exams.question_id " . "WHERE exams.training_id=" . $training_id . " " . "ORDER BY exams.id "; $db->select($query); if ($db->valid()) { return $db; } }
function invert($id) { $db = new DB(); $db->select("SELECT * FROM {$this->tablename} WHERE id=\$1", array($id)); if ($db->valid()) { $data = $db->current(); $data['active'] = $data['active'] == 't' ? 'f' : 't'; $this->update($id, array('active' => $data['active'])); } }
function add($data) { if (!isset($data['name']) || !isset($data['email'])) { throw new Exception("No data", self::NO_DATA); } $db = new DB(); $db->select("SELECT * FROM {$this->tablename} WHERE name=\$1", array($data['name'])); if ($db->valid()) { throw new Exception("Trainer exists", self::EXISTS); } return parent::add($data); }
function start() { $db = new DB(); $db->select("select data from sessions where id = \$1 and remote_addr = \$2", array($_COOKIE['session_id'], $_SERVER['REMOTE_ADDR'])); if ($db->valid()) { /* Session is saved before and not deleted after lifetime period exceed */ $res = $db->current(); $_SESSION = unserialize($res['data']); setcookie('session_id', $_COOKIE['session_id'], time() + self::LIFETIME, '/'); } else { /* Session is not present in database, should create new */ $this->create(); } }
function find($cond, $limit = null) { $db = new DB(); $query = "SELECT * FROM {$this->tablename} WHERE"; $i = 1; foreach (array('name', 'email', 'company', 'city') as $key => $val) { $query .= ' ' . ($i > 1 ? 'OR ' : '') . $val . ' like ' . "'%" . pg_escape_string($cond) . "%'"; $i++; } if (isset($limit)) { $query .= " LIMIT {$limit}"; } $db->select($query); if (!$db->valid()) { throw new Exception("Object not exists"); } return $db; }
function enumerateIDs($search = null, $start = null, $limit = null, $order = null) { $db = new DB(); $query = "SELECT id FROM {$this->tablename}"; if (isset($search)) { $query .= $this->addSearch($search); } if (isset($order)) { $query .= " ORDER BY " . $order; } if (isset($start)) { $query .= " OFFSET {$start}"; } if (isset($limit)) { $query .= " LIMIT {$limit}"; } $db->select($query); if ($db->valid()) { return $db; } }
function enumerate($search = null, $start = null, $limit = null, $order = null) { $db = new DB(); $query = "SELECT requests.id as id,\n requests.course_id as course_id,\n courses.name as course_name,\n courses.category_id as category_id,\n requests.user_id as user_id,\n requests.created as created,\n users.name as user_name,\n users.company as user_company,\n users.position as user_position,\n requests.planned_date as planned_date,\n requests.active as active\n FROM {$this->tablename}\nINNER JOIN courses ON courses.id=requests.course_id\nINNER JOIN users ON users.id=requests.user_id"; if (isset($search)) { $query .= $this->addSearch($search); } if (isset($order)) { $query .= " ORDER BY " . $order; } if (isset($start)) { $query .= " OFFSET {$start}"; } if (isset($limit)) { $query .= " LIMIT {$limit}"; } $db->select($query); if ($db->valid()) { return $db; } }
function get($cond) { $db = new DB(); $query = "SELECT * FROM {$this->tablename} WHERE"; $i = 1; unset($param); foreach ($cond as $key => $val) { $query .= ' ' . ($i > 1 ? 'AND ' : '') . $key . '=' . "'" . pg_escape_string($val) . "'"; $i++; $param[] = $val; } $db->select($query); if (!$db->valid()) { throw new Exception("Object not exists"); } return $db->current(); }
function enumerate($search = null, $start = null, $limit = null, $order = '1 DESC') { $db = new DB(); $query = "SELECT trainings.id as id,\n trainings.course_id as course_id,\n courses.name as course_name,\n courses.category_id as category_id,\n courses.category_name as category_name,\n courses.format_id as format_id,\n trainings.user_id as user_id,\n trainings.created as created,\n trainings.start as start,\n trainings.finish as finish,\n trainings.finished as finished,\n trainings.course_hash as course_hash,\n trainings.exam_hash as exam_hash,\n trainings.result as result,\n trainings.questions as questions,\n trainings.answers as answers,\n trainings.tries as tries,\n courses.exam as exam,\n trainings.examed as examed,\n trainings.status_id as status_id\n FROM {$this->tablename}\nINNER JOIN courses ON courses.id=trainings.course_id"; if (isset($search)) { $query .= $this->addSearch($search); } if (isset($order)) { $query .= " ORDER BY {$order}"; } if (isset($start)) { $query .= " OFFSET {$start}"; } if (isset($limit)) { $query .= " LIMIT {$limit}"; } $db->select($query); if ($db->valid()) { return $db; } }