LabService.php 2.71 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
32
        error_log(print_r('creating', TRUE));
        unset($data['id']);
        $lab = R::dispense('lab');
        $this->persist($lab, $data);
33

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

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

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

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    private function persist($lab, $data)
    {
        R::debug(TRUE);
        error_log(print_r($data, TRUE));
        $lab->school_id       = $data['school_id'];
        $lab->name            = $data['name'];
        $lab->type            = $data['type'];
        $lab->area            = $data['area'];
        $lab->sharedCourse    = $this->getCoursesById($data['lessons']);
        $lab->out_school_use  = $data['use_ext_program'];
        $lab->in_school_use   = $data['use_in_program'];
        $lab->attachment      = 'attachment';
        $lab->has_network     = isset($data['has_network']);
        $lab->has_server      = isset($data['has_server']);
        $lab->responsible     = $data['responsible'];
        
        $id = R::store($lab);
66
67
    }

68
69
70
    public function getLabById($id)
    {
        $lab = R::load('lab', $id);
kanellov's avatar
kanellov committed
71

72
73
        return $lab;
    }
Georgios Tsakalos's avatar
Georgios Tsakalos committed
74

75
76
    public function getLabsBySchoolId($id)
    {
Georgios Tsakalos's avatar
Georgios Tsakalos committed
77
        $labs = R::findAll('lab', 'school_id = ?', [$id]);
kanellov's avatar
kanellov committed
78

Georgios Tsakalos's avatar
Georgios Tsakalos committed
79
        return array_map([$this, 'export'], $labs);
80
    }
81

82
83
    public function getCourses()
    {
84
        $courses = R::findAll('course');
85

86
87
88
        return  $courses;
    }

89
90
91
    public function getCoursesByLabId($id)
    {
        $lab     = R::load('lab', $id);
92
        $courses = $lab->sharedCourse;
93

94
95
96
        return $courses;
    }

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

105
106
        return $courses;
    }
107
}