LabService.php 2.67 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)
    {
29
30
31
        unset($data['id']);
        $lab = R::dispense('lab');
        $this->persist($lab, $data);
32

33
34
        //return $this->export($lab);
        return $lab;
35
    }
36

kanellov's avatar
kanellov committed
37
38
    public function updateLab(array $data, $id)
    {
39
40
41
        $lab = R::load('lab', $id);
        if (!$lab->id) {
            throw new \InvalidArgumentException('No lab found');
42
        }
43
44
45
46
        $this->persist($lab, $data);

        return $this->export($lab);
    }
kanellov's avatar
kanellov committed
47

48
49
50
51
52
53
    private function persist($lab, $data)
    {
        $lab->school_id       = $data['school_id'];
        $lab->name            = $data['name'];
        $lab->type            = $data['type'];
        $lab->area            = $data['area'];
54
55
56
57
58
        $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'];
59
60
        $lab->has_server      = isset($data['has_server']);
        $lab->responsible     = $data['responsible'];
61
         
62
        $id = R::store($lab);
63
64
    }

65
66
67
    public function getLabById($id)
    {
        $lab = R::load('lab', $id);
kanellov's avatar
kanellov committed
68

69
70
        return $lab;
    }
Georgios Tsakalos's avatar
Georgios Tsakalos committed
71

72
73
    public function getLabsBySchoolId($id)
    {
Georgios Tsakalos's avatar
Georgios Tsakalos committed
74
        $labs = R::findAll('lab', 'school_id = ?', [$id]);
75
76
77
78
79
        $elabs=[];
        foreach($labs as $lab) {
            $elabs[] = $lab->export();
        }
        return $elabs;
80
    }
81

82
    public function getLessons()
83
    {
84
        $lessons = R::findAll('lesson');
85

86
        return  $lessons;
87
88
    }

89
    public function getLessonsByLabId($id)
90
91
    {
        $lab     = R::load('lab', $id);
92
        $lessons = $lab->sharedLesson;
93

94
        return $lessons;
95
96
    }

97
    private function getLessonsById(array $ids)
98
    {
99
        $lessons= [];
100
        foreach ($ids as $id) {
101
102
            $lesson= R::load('lesson', $id);
            $lessons[] = $lesson;
103
        }
104

105
        return $lesson;
106
    }
107
}