/** * @param ilTermsOfServiceAcceptanceEntity $entity * @return ilTermsOfServiceAcceptanceEntity */ public function loadCurrentAcceptanceOfUser(ilTermsOfServiceAcceptanceEntity $entity) { $this->db->setLimit(1, 0); $res = $this->db->queryF(' SELECT tos_versions.*, tos_acceptance_track.ts accepted_ts FROM tos_acceptance_track INNER JOIN tos_versions ON id = tosv_id WHERE usr_id = %s ORDER BY tos_acceptance_track.ts DESC ', array('integer'), array($entity->getUserId())); $row = $this->db->fetchAssoc($res); $entity->setId($row['id']); $entity->setUserId($row['usr_id']); $entity->setIso2LanguageCode($row['lng']); $entity->setSource($row['src']); $entity->setSourceType($row['src_type']); $entity->setText($row['text']); $entity->setTimestamp($row['accepted_ts']); $entity->setHash($row['hash']); return $entity; }
/** * @param array $params * @param array $filter * @return array * @throws InvalidArgumentException */ public function getList(array $params, array $filter) { $data = array('items' => array(), 'cnt' => 0); $select = $this->getSelectPart($filter); $where = $this->getWherePart($filter); $from = $this->getFromPart($filter); $order = $this->getOrderByPart($params); $group = $this->getGroupByPart(); $having = $this->getHavingPart($filter); if (isset($params['group'])) { if (!is_string($params['group'])) { throw new InvalidArgumentException('Please provide a valid group field parameter.'); } $group = $params['group']; } if (isset($params['limit'])) { if (!is_numeric($params['limit'])) { throw new InvalidArgumentException('Please provide a valid numerical limit.'); } if (!isset($params['offset'])) { $params['offset'] = 0; } else { if (!is_numeric($params['offset'])) { throw new InvalidArgumentException('Please provide a valid numerical offset.'); } } $this->db->setLimit($params['limit'], $params['offset']); } $where = strlen($where) ? 'WHERE ' . $where : ''; $query = "SELECT {$select} FROM {$from} {$where}"; if (strlen($group)) { $query .= " GROUP BY {$group}"; } if (strlen($having)) { $query .= " HAVING {$having}"; } if (strlen($order)) { $query .= " ORDER BY {$order}"; } $res = $this->db->query($query); while ($row = $this->db->fetchAssoc($res)) { $data['items'][] = $row; } $data = $this->getAdditionalItems($data); if (isset($params['limit'])) { $cnt_sql = "SELECT COUNT(*) cnt FROM ({$query}) subquery"; $row_cnt = $this->db->fetchAssoc($this->db->query($cnt_sql)); $data['cnt'] = $row_cnt['cnt']; } return $data; }
/** * Fetch a list of entries from the database. * * The getList() method can be used to retrieve a collection * of entries saved in the database in a given table. * * @params array $params Possible parameters indexes include: * - limit [~numeric] * - offset [~numeric] * - order_field [~string] * - order_direction [=ASC|DESC] * - group [~string] * * @param array $params * @param array $filters * @throws InvalidArgumentException * @return array with indexes 'items' and 'cnt'. */ public function getList(array $params = array(), array $filters = array()) { $data = array('items' => array(), 'cnt' => 0); $select = $this->getSelectPart(); $where = $this->getWherePart($filters); $from = $this->getFromPart(); $order = ""; $group = ""; $limit = ""; /* Build ORDER BY */ if (isset($params['order_field'])) { if (!is_string($params['order_field'])) { throw new InvalidArgumentException("Please provide a valid order field."); } if (!isset($params['order_direction'])) { /* Defaulting to ASC(ending) order. */ $params['order_direction'] = "ASC"; } elseif (!in_array(strtolower($params['order_direction']), array("asc", "desc"))) { throw new InvalidArgumentException("Please provide a valid order direction."); } $order = $params['order_field'] . ' ' . $params['order_direction']; } /* Build GROUP BY */ if (isset($params['group'])) { if (!is_string($params['group'])) { throw new InvalidArgumentException("Please provide a valid group field parameter."); } $group = $params['group']; } /* Build LIMIT */ if (isset($params['limit'])) { if (!is_numeric($params['limit'])) { throw new InvalidArgumentException("Please provide a valid numerical limit."); } if (!isset($params['offset'])) { $params['offset'] = 0; } elseif (!is_numeric($params['offset'])) { throw new InvalidArgumentException("Please provide a valid numerical offset."); } $this->db->setLimit($params['limit'], $params['offset']); } /* Build SQL query */ $query = "\n\t\t\tSELECT\n\t\t\t\t{$select}\n\t\t\tFROM\n\t\t\t\t{$from}\n\t\t\tWHERE\n\t\t\t\t{$where}\n\t\t"; if (!empty($group)) { $query .= " GROUP BY {$group}"; } if (!empty($order)) { $query .= " ORDER BY {$order}"; } /* Execute query and fetch items. */ $result = $this->db->query($query); while ($row = $this->db->fetchObject($result)) { $data['items'][] = $row; } if (isset($params['limit'])) { /* Fill 'cnt' with total count of items */ $cntSQL = "SELECT COUNT(*) cnt FROM ({$query}) subquery"; $rowCnt = $this->db->fetchAssoc($this->db->query($cntSQL)); $data['cnt'] = $rowCnt['cnt']; } return $data; }