Completed
Push — master ( 506c79...7daaad )
by Julito
12:09
created

EnrollmentController::index()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
nc 3
nop 0
dl 0
loc 24
rs 9.8333
c 1
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\PluginBundle\WhispeakAuth\Controller;
5
6
use Chamilo\PluginBundle\WhispeakAuth\Request\ApiRequest;
7
8
/**
9
 * Class EnrollmentController.
10
 *
11
 * @package Chamilo\PluginBundle\WhispeakAuth\Controller
12
 */
13
class EnrollmentController extends BaseController
14
{
15
    /**
16
     * @throws \Exception
17
     */
18
    public function index()
19
    {
20
        if (!$this->plugin->toolIsEnabled()) {
21
            throw new \Exception(get_lang('NotAllowed'));
22
        }
23
24
        $user = api_get_user_entity(api_get_user_id());
25
26
        $userIsEnrolled = \WhispeakAuthPlugin::checkUserIsEnrolled($user->getId());
27
28
        if ($userIsEnrolled) {
29
            throw new \Exception($this->plugin->get_lang('SpeechAuthAlreadyEnrolled'));
30
        }
31
32
        $request = new ApiRequest();
33
        $response = $request->createEnrollmentSessionToken($user);
34
35
        \ChamiloSession::write(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT, $response['token']);
36
37
        $this->displayPage(
38
            true,
39
            [
40
                'action' => 'enrollment',
41
                'sample_text' => $response['text'],
42
            ]
43
        );
44
    }
45
46
    /**
47
     * @throws \Exception
48
     */
49
    public function ajax()
50
    {
51
        if (!$this->plugin->toolIsEnabled() || empty($_FILES['audio'])) {
52
            throw new \Exception(get_lang('NotAllowed'));
53
        }
54
55
        $user = api_get_user_entity(api_get_user_id());
56
57
        $audioFilePath = $this->uploadAudioFile($user);
58
59
        $token = \ChamiloSession::read(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT);
60
61
        if (empty($token)) {
62
            throw new \Exception($this->plugin->get_lang('EnrollmentFailed'));
63
        }
64
65
        $request = new ApiRequest();
66
        $response = $request->createEnrollment($token, $audioFilePath, $user);
67
68
        \ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT);
69
70
        $this->plugin->saveEnrollment($user, $response['speaker']);
71
72
        echo \Display::return_message($this->plugin->get_lang('EnrollmentSuccess'), 'success');
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function displayPage($isFullPage, array $variables)
79
    {
80
        global $htmlHeadXtra;
81
82
        $htmlHeadXtra[] = api_get_js('rtc/RecordRTC.js');
83
        $htmlHeadXtra[] = api_get_js_simple(api_get_path(WEB_PLUGIN_PATH).'whispeakauth/assets/js/RecordAudio.js');
84
85
        $pageTitle = $this->plugin->get_lang('EnrollmentTitle');
86
87
        $template = new \Template($pageTitle);
88
89
        foreach ($variables as $key => $value) {
90
            $template->assign($key, $value);
91
        }
92
93
        $pageContent = $template->fetch('whispeakauth/view/record_audio.html.twig');
94
95
        $template->assign('header', $pageTitle);
96
        $template->assign('content', $pageContent);
97
        $template->display_one_col_template();
98
    }
99
}
100