1<?php
2
3declare(strict_types=1);
4
5/**
6 * Persistence for online/activation period
7 *
8 * @author Nils Haagen <nils.haagen@concepts-and-training.de>
9 */
10class ilLearningSequenceActivationDB
11{
12    const TABLE_NAME = 'lso_activation';
13
14    /**
15     * @var ilDBInterface
16     */
17    protected $database;
18
19    public function __construct(ilDBInterface $database)
20    {
21        $this->database = $database;
22    }
23
24    public function getActivationForRefId(int $ref_id) : ilLearningSequenceActivation
25    {
26        $data = $this->select($ref_id);
27        if (count($data) == 0) {
28            $settings = $this->buildActivationSettings($ref_id);
29            $this->insert($settings);
30        } else {
31            if ($data['activation_start_ts']) {
32                $start = new \DateTime();
33                $start->setTimestamp((int) $data['activation_start_ts']);
34            } else {
35                $start = null;
36            }
37
38            if ($data['activation_end_ts']) {
39                $end = new \DateTime();
40                $end->setTimestamp((int) $data['activation_end_ts']);
41            } else {
42                $end = null;
43            }
44
45            $settings = $this->buildActivationSettings(
46                (int) $data['ref_id'],
47                (bool) $data['online'],
48                (bool) $data['effective_online'],
49                $start,
50                $end
51            );
52        }
53
54        return $settings;
55    }
56
57    public function deleteForRefId(int $ref_id)
58    {
59        $query = "DELETE FROM " . static::TABLE_NAME . PHP_EOL
60            . "WHERE ref_id = " . $this->database->quote($ref_id, "integer") . PHP_EOL
61        ;
62        $this->database->manipulate($query);
63    }
64
65    public function store(ilLearningSequenceActivation $settings)
66    {
67        $where = array(
68            "ref_id" => array("integer", $settings->getRefId())
69        );
70
71        $start = $settings->getActivationStart();
72        $end = $settings->getActivationEnd();
73
74        if ($start) {
75            $start = $start->getTimestamp();
76        }
77        if ($end) {
78            $end = $end->getTimestamp();
79        }
80        $values = array(
81            "online" => array("integer", $settings->getIsOnline()),
82            "activation_start_ts" => array("integer", $start),
83            "activation_end_ts" => array("integer", $end)
84        );
85        $this->database->update(static::TABLE_NAME, $values, $where);
86    }
87
88    protected function insert(ilLearningSequenceActivation $settings)
89    {
90        $start = $settings->getActivationStart();
91        $end = $settings->getActivationEnd();
92        if ($start) {
93            $start = $start->getTimestamp();
94        }
95        if ($end) {
96            $end = $end->getTimestamp();
97        }
98        $values = array(
99            "ref_id" => array("integer", $settings->getRefId()),
100            "online" => array("integer", $settings->getIsOnline()),
101            "effective_online" => array("integer", $settings->getEffectiveOnlineStatus()),
102            "activation_start_ts" => array("integer", $start),
103            "activation_end_ts" => array("integer", $end)
104        );
105        $this->database->insert(static::TABLE_NAME, $values);
106    }
107
108    protected function select(int $ref_id) : array
109    {
110        $ret = [];
111        $query =
112             "SELECT ref_id, online, effective_online, activation_start_ts, activation_end_ts" . PHP_EOL
113            . "FROM " . static::TABLE_NAME . PHP_EOL
114            . "WHERE ref_id = " . $this->database->quote($ref_id, "integer") . PHP_EOL
115        ;
116
117        $result = $this->database->query($query);
118
119        if ($result->numRows() !== 0) {
120            $ret = $this->database->fetchAssoc($result);
121        }
122
123        return $ret;
124    }
125
126    protected function buildActivationSettings(
127        int $ref_id,
128        bool $online = false,
129        bool $effective_online = false,
130        \DateTime $activation_start = null,
131        \DateTime $activation_end = null
132    ) : ilLearningSequenceActivation {
133        return new ilLearningSequenceActivation(
134            $ref_id,
135            $online,
136            $effective_online,
137            $activation_start,
138            $activation_end
139        );
140    }
141
142    public function setEffectiveOnlineStatus(int $ref_id, bool $status)
143    {
144        $where = array(
145            "ref_id" => array("integer", $ref_id)
146        );
147
148        $values = array(
149            "effective_online" => array("integer", $status),
150        );
151
152        $this->database->update(static::TABLE_NAME, $values, $where);
153    }
154}
155