LabService.php 3.63 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php
/**
 * gredu_labs
 * 
 * @link https://github.com/eellak/gredu_labs for the canonical source repository
 * @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
 * @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
 */

10
namespace GrEduLabs\Schools\Service;
11

12
use InvalidArgumentException;
13
14
15
16
use RedBeanPHP\R;

class LabService implements LabServiceInterface
{
17

18
19
    public function createLab(array $data)
    {
20
21
22
        unset($data['id']);
        $lab = R::dispense('lab');
        $this->persist($lab, $data);
23

24
        return $this->exportLab($lab);
25
    }
26

kanellov's avatar
kanellov committed
27
28
    public function updateLab(array $data, $id)
    {
29
30
        $lab = R::load('lab', $id);
        if (!$lab->id) {
31
            throw new InvalidArgumentException('No lab found');
32
        }
33
34
        $this->persist($lab, $data);

35
        return $this->exportLab($lab);
36
    }
kanellov's avatar
kanellov committed
37

38
39
    private function persist($lab, $data)
    {
40
41
42
43
44
45
46
47
48
49
50
51
52
        $lab->school_id           = $data['school_id'];
        $lab->name                = $data['name'];
        $lab->labtype_id          = $data['labtype_id'];
        $lab->area                = $data['area'];
        $lab->sharedLesson        = $this->getLessonsById($data['lessons']);
        $lab->use_ext_program     = $data['use_ext_program'];
        $lab->use_in_program      = $data['use_in_program'];
        $lab->attachment          = $data['attachment'];
        $lab->has_network         = $data['has_network'];
        $lab->has_server          = $data['has_server'];
        $lab->responsible         = R::load('teacher', $data['responsible_id']);

        R::store($lab);
53
54
    }

55
56
57
    public function getLabById($id)
    {
        $lab = R::load('lab', $id);
58
59
60
        if (!$lab->id) {
            throw new InvalidArgumentException('No lab found');
        }
kanellov's avatar
kanellov committed
61

62
        return $this->export($lab);
63
    }
Georgios Tsakalos's avatar
Georgios Tsakalos committed
64

65
66
    public function getLabsBySchoolId($id)
    {
Georgios Tsakalos's avatar
Georgios Tsakalos committed
67
        $labs = R::findAll('lab', 'school_id = ?', [$id]);
68
69

        return array_map([$this, 'exportLab'], $labs);
70
    }
71

72
    public function getLessons()
73
    {
74
        $lessons = R::findAll('lesson');
75

76
77
78
        return array_map(function ($lesson) {
            return $lesson->export();
        }, $lessons);
79
80
    }

81
    public function getLessonsByLabId($id)
82
83
    {
        $lab     = R::load('lab', $id);
84
        $lessons = $lab->sharedLesson;
85

86
87
88
89
90
91
92
93
94
95
        return array_map(function ($lesson) {
            return $lesson->export();
        }, $lessons);
    }

    public function getLabTypes()
    {
        return array_map(function ($lab) {
            return $lab->export();
        }, R::find('labtype'));
96
97
    }

98
    private function getLessonsById($ids)
99
    {
100
101
        if (!is_array($ids)) {
            $ids = [$ids];
102
        }
103

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
        return array_values(R::loadAll('lesson', $ids));
    }

    private function exportLab($bean)
    {
        $responsible = $bean->fetchAs('teacher')->responsible;
        if ($responsible) {
            $responsible = sprintf("%s %s", $responsible->name, $responsible->surname);
        }

        return array_merge($bean->export(), [
            'labtype'     => $bean->labtype->name,
            'responsible' => $responsible,
            'lessons'     => array_reduce($bean->sharedLesson, function ($ids, $lesson) {
                $ids[] = $lesson->id;

                return $ids;
            }, []),

        ]);
    }

    public function getHasNetworkValues()
    {
        return [
           'ΔΟΜΗΜΕΝΗ ΚΑΛΩΔΙΩΣΗ',
           'ΑΣΥΡΜΑΤΗ ΔΙΚΤΥΟ ΜΕΣΩ WIFI',
           'ΔΕΝ ΥΠΑΡΧΕΙ ΔΙΚΤΥΟ',
        ];
    }

    public function getHasServerValues()
    {
        return ['ΝΑΙ', 'ΟΧΙ'];
138
    }
139
}