aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Courseware/BlockTypes/BlockTypeState.php
blob: b72a9f474cc96030cd5bfb960596f70e3fdac8d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

namespace Courseware\BlockTypes;

use DBManager;
use SimpleORMap;

/**
 * This class represents the activation state of a block type.
 *
 * @SuppressWarnings(PHPMD.StaticAccess)
 */
class BlockTypeState extends SimpleORMap
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'cw_block_type_states';

        parent::configure($config);
    }

    /**
     * Returns all `BlockType`s that are activated in this Stud.IP installation.
     *
     * @return iterable<string> an iterable of all `BlockType` classes that are activated.
     */
    public static function getActivatedBlockTypes(): iterable
    {
        $args = [
            BlockType::getBlockTypes(),
            DBManager::get()->fetchFirst('SELECT `block_type` FROM `cw_block_type_states` WHERE `activated` = 0'),
        ];

        return [...array_diff(...$args)];
    }

    /**
     * Activate the `BlockType` this `BlockTypeState` is relating to.
     *
     * @return `true` on success, `false` otherwise
     */
    public function activate(): bool
    {
        $this->activated = 1;
        return (bool) $this->store();
    }

    /**
     * Deactivate the `BlockType` this `BlockTypeState` is relating to.
     *
     * @return `true` on success, `false` otherwise
     */
    public function deactivate(): bool
    {
        $this->activated = 0;
        return (bool) $this->store();
    }
}