ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilTermsOfServiceAcceptanceDatabaseGateway.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/TermsOfService/interfaces/interface.ilTermsOfServiceAcceptanceDataGateway.php';
5 
11 {
15  protected $db;
16 
20  public function __construct(ilDB $db)
21  {
22  $this->db = $db;
23  }
24 
29  {
30  $query = 'SELECT id FROM tos_versions WHERE hash = %s AND lng = %s';
31  $res = $this->db->queryF(
32  $query,
33  array('text', 'text'),
34  array($entity->getHash(), $entity->getIso2LanguageCode())
35  );
36 
37  if($this->db->numRows($res))
38  {
39  $row = $this->db->fetchAssoc($res);
40  $tosv_id = $row['id'];
41  }
42  else
43  {
44  $tosv_id = $this->db->nextId('tos_versions');
45  $this->db->insert(
46  'tos_versions',
47  array(
48  'id' => array('integer', $tosv_id),
49  'lng' => array('text', $entity->getIso2LanguageCode()),
50  'src' => array('text', $entity->getSource()),
51  'src_type' => array('integer', $entity->getSourceType()),
52  'text' => array('clob', $entity->getText()),
53  'hash' => array('text', $entity->getHash()),
54  'ts' => array('integer', $entity->getTimestamp())
55  )
56  );
57  }
58 
59  $this->db->insert(
60  'tos_acceptance_track',
61  array(
62  'tosv_id' => array('integer', $tosv_id),
63  'usr_id' => array('integer', $entity->getUserId()),
64  'ts' => array('integer', $entity->getTimestamp())
65  )
66  );
67  }
68 
74  {
75  $this->db->setLimit(1, 0);
76  $res = $this->db->queryF('
77  SELECT tos_versions.*, tos_acceptance_track.ts accepted_ts
78  FROM tos_acceptance_track
79  INNER JOIN tos_versions ON id = tosv_id
80  WHERE usr_id = %s
81  ORDER BY tos_acceptance_track.ts DESC
82  ',
83  array('integer'),
84  array($entity->getUserId())
85  );
86  $row = $this->db->fetchAssoc($res);
87 
88  $entity->setId($row['id']);
89  $entity->setUserId($row['usr_id']);
90  $entity->setIso2LanguageCode($row['lng']);
91  $entity->setSource($row['src']);
92  $entity->setSourceType($row['src_type']);
93  $entity->setText($row['text']);
94  $entity->setTimestamp($row['accepted_ts']);
95  $entity->setHash($row['hash']);
96 
97  return $entity;
98  }
99 
105  {
106  $res = $this->db->queryF('
107  SELECT *
108  FROM tos_versions
109  WHERE id = %s
110  ',
111  array('integer'),
112  array($entity->getId())
113  );
114  $row = $this->db->fetchAssoc($res);
115 
116  $entity->setId($row['id']);
117  $entity->setIso2LanguageCode($row['lng']);
118  $entity->setSource($row['src']);
119  $entity->setSourceType($row['src_type']);
120  $entity->setText($row['text']);
121  $entity->setTimestamp($row['ts']);
122  $entity->setHash($row['hash']);
123 
124  return $entity;
125  }
126 
131  {
132  $this->db->manipulate('DELETE FROM tos_acceptance_track WHERE usr_id = ' . $this->db->quote($entity->getUserId(), 'integer'));
133  }
134 }