Passed
Push — master ( da8325...8e761e )
by Thomas
08:46
created

CourseSkillController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 26
dl 0
loc 53
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A exportCourseSyllabus() 0 45 4
A __construct() 0 4 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Course;
6
use PhpOffice\PhpWord\IOFactory;
7
use PhpOffice\PhpWord\PhpWord;
8
9
class CourseSkillController extends Controller
10
{
11
    public function __construct()
12
    {
13
        parent::__construct();
14
        $this->middleware(['permission:evaluation.edit']);
15
    }
16
17
    public function exportCourseSyllabus(Course $course)
18
    {
19
        $phpWord = new PhpWord();
20
21
        // Course general info
22
        $section = $phpWord->addSection();
23
24
        $section->addText('Cours : '.$course->name);
25
26
        $section->addText('Session : '.$course->period->name);
27
28
        $section->addText('Enseignant(e) : '.$course->teacher->name);
29
30
        $section->addText('Dates du cours : '.$course->start_date->format('d/m').' - '.$course->end_date->format('d/m'));
31
32
        $section->addTextBreak();
33
34
        // Course skills
35
        $level = '';
36
        $type = '';
37
38
        foreach ($course->skills->sortBy('skill_type_id') as $skill) {
0 ignored issues
show
Bug introduced by
The property skills does not seem to exist on App\Models\Course. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
39
            if ($skill->level->name != $level) {
40
                $level = $skill->level->name;
41
42
                $section->addText('Niveau '.$level);
43
            }
44
45
            if ($skill->skill_type->name != $type) {
46
                $type = $skill->skill_type->name;
47
48
                $section->addText($type);
49
            }
50
51
            $section->addListItem($skill->name);
52
        }
53
54
        // Saving the document as OOXML file...
55
        $objWriter = IOFactory::createWriter($phpWord, 'Word2007');
56
        header('Content-type: application/msword');
57
        header('Cache-Control: no-store, no-cache');
58
        header('Content-Disposition: attachment; filename="document.docx"');
59
60
        $objWriter->save('php://output');
61
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
62
    }
63
}
64