LabService.php 6.01 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
        $lab->school_id           = $data['school_id'];
        $lab->name                = $data['name'];
        $lab->labtype_id          = $data['labtype_id'];
50
        $lab->is_new              = isset($data['is_new']);
51 52 53 54 55 56 57
        $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
58 59
        R::store($lab);
        $this->storeAttachement($lab, $data['attachment']);
60
        R::store($lab);
61 62
    }

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

kanellov's avatar
kanellov committed
70
        return $this->exportLab($lab);
71
    }
Georgios Tsakalos's avatar
Georgios Tsakalos committed
72

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

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

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

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

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

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

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

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

112 113 114 115 116 117 118 119 120 121
        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);
        }

122
        $lab                    = $bean->export();
123
        $lab['attachment_file'] = basename($lab['attachment']);
124 125

        return array_merge($lab, [
126 127 128 129 130 131 132 133 134 135
            'labtype'     => $bean->labtype->name,
            'responsible' => $responsible,
            'lessons'     => array_reduce($bean->sharedLesson, function ($ids, $lesson) {
                $ids[] = $lesson->id;

                return $ids;
            }, []),
        ]);
    }

kanellov's avatar
kanellov committed
136 137 138 139 140 141 142 143 144 145 146 147
    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
148 149 150
        if ($lab->attachment) {
            unlink($this->filesPath . '/' . $lab->attachment);
        }
kanellov's avatar
kanellov committed
151 152 153 154 155 156 157 158
        // move new file 
        rename($attachment['tmp_name'], $fullPath . '/' . $attachment['name']);
        $lab->attachment      = $schoolPath . '/' . $attachment['name'];
        $lab->attachment_mime = $attachment['type'];

        return $lab;
    }

159 160 161 162 163 164 165 166 167 168 169 170
    public function getHasNetworkValues()
    {
        return [
           'ΔΟΜΗΜΕΝΗ ΚΑΛΩΔΙΩΣΗ',
           'ΑΣΥΡΜΑΤΗ ΔΙΚΤΥΟ ΜΕΣΩ WIFI',
           'ΔΕΝ ΥΠΑΡΧΕΙ ΔΙΚΤΥΟ',
        ];
    }

    public function getHasServerValues()
    {
        return ['ΝΑΙ', 'ΟΧΙ'];
171
    }
kanellov's avatar
kanellov committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

    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
192

kanellov's avatar
kanellov committed
193 194 195
        if (!$lab->attachment) {
            return ;
        }
kanellov's avatar
kanellov committed
196

kanellov's avatar
kanellov committed
197 198 199 200 201 202 203 204
        if (is_writable($this->filesPath . '/' . $lab->attachment)) {
            unlink($this->filesPath . '/' . $lab->attachment);
        }

        $lab->attachment      = null;
        $lab->attachment_mime = null;
        R::store($lab);
    }
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

    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);
        }
    }
222
}