LabService.php 1.8 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
13
14
15
16
17
18
19

use RedBeanPHP\R;

class LabService implements LabServiceInterface
{
    protected $schoolService;
    protected $staffService;

    public function __construct(
kanellov's avatar
kanellov committed
20
        SchoolServiceInterface $schoolService,
21
22
23
        StaffServiceInterface $staffService
    ) {
        $this->schoolService = $schoolService;
kanellov's avatar
kanellov committed
24
        $this->staffService  = $staffService;
25
    }
26

27
28
    public function createLab(array $data)
    {
kanellov's avatar
kanellov committed
29
30
31
32
33
        $lab      = R::dispense('lab');
        $required = ['school_id', 'name', 'type', 'area', 'in_school_use', 'out_school_use',
                     'courses', 'attachment', 'has_network', 'has_server', ];
        foreach ($required as $value) {
            if (array_key_exists($value, $data)) {
34
                $lab[$value] = $data[$value];
kanellov's avatar
kanellov committed
35
            } else {
36
37
38
                return -1;
            }
        }
39
40
41

        if (array_key_exists('teacher_id', $data)) {
            $lab['teacher_id'] = $data['teacher_id'];
42
        }
43

44
        $id = R::store($lab);
kanellov's avatar
kanellov committed
45

46
47
        return $id;
    }
48

kanellov's avatar
kanellov committed
49
50
    public function updateLab(array $data, $id)
    {
51
        $lab= R::load('lab', $id);
kanellov's avatar
kanellov committed
52
        foreach ($data as $key => $value) {
53
54
55
            $lab[$key] = $value;
        }
        $id = R::store($lab);
kanellov's avatar
kanellov committed
56

57
58
59
        return $id;
    }

60
61
62
    public function getLabById($id)
    {
        $lab = R::load('lab', $id);
kanellov's avatar
kanellov committed
63

64
65
        return $lab;
    }
Georgios Tsakalos's avatar
Georgios Tsakalos committed
66

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

Georgios Tsakalos's avatar
Georgios Tsakalos committed
71
        return array_map([$this, 'export'], $labs);
72
73
    }
}