ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilTermsOfServiceAcceptanceDatabaseGateway.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
27  protected ilDBInterface $db;
28 
29  public function __construct(ilDBInterface $db)
30  {
31  $this->db = $db;
32  }
33 
34  public function trackAcceptance(ilTermsOfServiceAcceptanceEntity $entity): void
35  {
36  $res = $this->db->queryF(
37  'SELECT id FROM tos_versions WHERE hash = %s AND doc_id = %s',
38  ['text', 'integer'],
39  [$entity->getHash(), $entity->getDocumentId()]
40  );
41 
42  if ($this->db->numRows($res)) {
43  $row = $this->db->fetchAssoc($res);
44  $versionId = $row['id'];
45  } else {
46  $versionId = $this->db->nextId('tos_versions');
47  $this->db->insert(
48  'tos_versions',
49  [
50  'id' => ['integer', $versionId],
51  'text' => ['clob', $entity->getText()],
52  'hash' => ['text', $entity->getHash()],
53  'doc_id' => ['integer', $entity->getDocumentId()],
54  'title' => ['text', $entity->getTitle()],
55  'ts' => ['integer', $entity->getTimestamp()]
56  ]
57  );
58  }
59 
60  $this->db->insert(
61  'tos_acceptance_track',
62  [
63  'tosv_id' => ['integer', $versionId],
64  'usr_id' => ['integer', $entity->getUserId()],
65  'criteria' => ['clob', $entity->getSerializedCriteria()],
66  'ts' => ['integer', $entity->getTimestamp()]
67  ]
68  );
69  }
70 
71  public function loadCurrentAcceptanceOfUser(
74  $this->db->setLimit(1, 0);
75 
76  $res = $this->db->queryF(
77  '
78  SELECT tos_versions.*,
79  tos_acceptance_track.ts accepted_ts,
80  tos_acceptance_track.criteria,
81  tos_acceptance_track.usr_id
82  FROM tos_acceptance_track
83  INNER JOIN tos_versions ON id = tosv_id
84  WHERE usr_id = %s
85  ORDER BY tos_acceptance_track.ts DESC
86  ',
87  ['integer'],
88  [$entity->getUserId()]
89  );
90  $row = $this->db->fetchAssoc($res);
91 
92  if ($row === null) {
93  return $entity;
94  }
95 
96  $entity = $entity
97  ->withId((int) $row['id'])
98  ->withUserId((int) $row['usr_id'])
99  ->withText((string) $row['text'])
100  ->withTimestamp((int) $row['accepted_ts'])
101  ->withHash((string) $row['hash'])
102  ->withDocumentId((int) $row['doc_id'])
103  ->withTitle((string) $row['title'])
104  ->withSerializedCriteria((string) $row['criteria']);
105 
106  return $entity;
107  }
108 
110  {
111  $res = $this->db->queryF(
112  '
113  SELECT *
114  FROM tos_versions
115  WHERE id = %s
116  ',
117  ['integer'],
118  [$entity->getId()]
119  );
120  $row = $this->db->fetchAssoc($res);
121 
122  $entity = $entity
123  ->withId($row['id'])
124  ->withText($row['text'])
125  ->withHash($row['hash'])
126  ->withDocumentId($row['doc_id'])
127  ->withTitle($row['title']);
128 
129  return $entity;
130  }
131 
133  {
134  $this->db->manipulate(
135  'DELETE FROM tos_acceptance_track WHERE usr_id = ' . $this->db->quote($entity->getUserId(), 'integer')
136  );
137  }
138 }
$res
Definition: ltiservices.php:69
Class ilTermsOfServiceAcceptanceEntity.
Interface ilTermsOfServiceAcceptanceDataGateway.