LabService.php 5.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
use InvalidArgumentException;
kanellov's avatar
kanellov committed
13
use RedBeanPHP\OODBBean;
14 15 16 17
use RedBeanPHP\R;

class LabService implements LabServiceInterface
{
kanellov's avatar
kanellov committed
18 19 20 21 22 23
    private $filesPath;

    public function __construct($filesPath)
    {
        $this->filesPath = $filesPath;
    }
24

25 26
    public function createLab(array $data)
    {
27 28 29
        unset($data['id']);
        $lab = R::dispense('lab');
        $this->persist($lab, $data);
30

31
        return $this->exportLab($lab);
32
    }
33

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

42
        return $this->exportLab($lab);
43
    }
kanellov's avatar
kanellov committed
44

45 46
    private function persist($lab, $data)
    {
47 48 49 50 51 52 53 54 55 56
        $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->has_network         = $data['has_network'];
        $lab->has_server          = $data['has_server'];
        $lab->responsible         = R::load('teacher', $data['responsible_id']);
kanellov's avatar
kanellov committed
57 58
        R::store($lab);
        $this->storeAttachement($lab, $data['attachment']);
59
        R::store($lab);
60 61
    }

62 63 64
    public function getLabById($id)
    {
        $lab = R::load('lab', $id);
65 66 67
        if (!$lab->id) {
            throw new InvalidArgumentException('No lab found');
        }
kanellov's avatar
kanellov committed
68

kanellov's avatar
kanellov committed
69
        return $this->exportLab($lab);
70
    }
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

        return array_map([$this, 'exportLab'], $labs);
77
    }
78

79
    public function getLessons()
80
    {
81
        $lessons = R::findAll('lesson');
82

83 84 85
        return array_map(function ($lesson) {
            return $lesson->export();
        }, $lessons);
86 87
    }

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

93 94 95 96 97 98 99 100 101 102
        return array_map(function ($lesson) {
            return $lesson->export();
        }, $lessons);
    }

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

105
    private function getLessonsById($ids)
106
    {
107 108
        if (!is_array($ids)) {
            $ids = [$ids];
109
        }
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        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;
            }, []),

        ]);
    }

kanellov's avatar
kanellov committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    private function storeAttachement(OODBBean $lab, array $attachment = null)
    {
        if (null === $attachment) {
            return;
        }

        $schoolPath = $lab->school->registry_no . '/lab_' . $lab->id;
        $fullPath   = $this->filesPath . '/' . $schoolPath;
        if (!is_dir($fullPath)) {
            mkdir($fullPath, 0700, true);
        }
        // remove previous
        unlink($this->filesPath . '/' . $lab->attachment);
        // move new file 
        rename($attachment['tmp_name'], $fullPath . '/' . $attachment['name']);
        $lab->attachment      = $schoolPath . '/' . $attachment['name'];
        $lab->attachment_mime = $attachment['type'];

        return $lab;
    }

154 155 156 157 158 159 160 161 162 163 164 165
    public function getHasNetworkValues()
    {
        return [
           'ΔΟΜΗΜΕΝΗ ΚΑΛΩΔΙΩΣΗ',
           'ΑΣΥΡΜΑΤΗ ΔΙΚΤΥΟ ΜΕΣΩ WIFI',
           'ΔΕΝ ΥΠΑΡΧΕΙ ΔΙΚΤΥΟ',
        ];
    }

    public function getHasServerValues()
    {
        return ['ΝΑΙ', 'ΟΧΙ'];
166
    }
kanellov's avatar
kanellov committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

    public function getLabForSchool($school_id, $id)
    {
        $lab = R::findOne('lab', ' school_id = ? AND id = ? ', [
            $school_id,
            $id,
        ]);
        if (!$lab) {
            return;
        }

        return $this->exportLab($lab);
    }

    public function removeLabAttachment($lab_id)
    {
        $lab = R::load('lab', $lab_id);
        if (!$lab->id) {
            throw new InvalidArgumentException('No lab');
        }
kanellov's avatar
kanellov committed
187

kanellov's avatar
kanellov committed
188 189 190
        if (!$lab->attachment) {
            return ;
        }
kanellov's avatar
kanellov committed
191

kanellov's avatar
kanellov committed
192 193 194 195 196 197 198 199
        if (is_writable($this->filesPath . '/' . $lab->attachment)) {
            unlink($this->filesPath . '/' . $lab->attachment);
        }

        $lab->attachment      = null;
        $lab->attachment_mime = null;
        R::store($lab);
    }
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

    public function removeLab($id, $school_id = null)
    {
        $sql      = ' id = ? ';
        $bindings = [(int) $id];
        if (null !== $school_id) {
            $sql .= ' AND school_id = ? ';
            $bindings[] = (int) $school_id;
        }
        $lab = R::findOne('lab', $sql, $bindings);
        if (null !== $lab) {
            if ($lab->attachment && is_writable($this->filesPath . '/' . $lab->attachment)) {
                unlink($this->filesPath . '/' . $lab->attachment);
            }
            R::trash($lab);
        }
    }
217
}