user-binding.php 2.88 KB
Newer Older
Francois Gannaz's avatar
Francois Gannaz committed
1
2
3
4
<?php

/**
 * Inner HTL page.
Francois Gannaz's avatar
Francois Gannaz committed
5
 * Bind a Moodle user to a LabNbook account.
Francois Gannaz's avatar
Francois Gannaz committed
6
7
8
9
 *
 * @copyright   2019 Université Grenoble Alpes
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
Francois Gannaz's avatar
Francois Gannaz committed
10
use mod_labnbook\fetch;
Francois Gannaz's avatar
Francois Gannaz committed
11

Francois Gannaz's avatar
Francois Gannaz committed
12
require_once \dirname(__DIR__, 2) . '/config.php';
13

Francois Gannaz's avatar
Francois Gannaz committed
14
$returnUrl = \base64_decode(required_param('return', PARAM_ALPHANUM));
15
16
17
18
19
20
21
22
$courseid = required_param('courseid', PARAM_INT);
$create = optional_param('create', false, PARAM_BOOL);

$course = $DB->get_record('course', ['id' => $courseid], '*', MUST_EXIST);
require_login($course);

$fetchInstitution = new fetch\institution();
$context = context_course::instance($courseid);
23
$isTeacher = has_capability('mod/labnbook:addinstance', $context);
24
25
26
27
28
29
if ($create && !fetch\user::isAuthenticated()) {
    global $USER;
    if (createUser($USER, $isTeacher)) {
        redirect($returnUrl);
    }
}
30
$role = ($isTeacher ? 'teacher' : 'learner');
Francois Gannaz's avatar
Francois Gannaz committed
31
32
33
34
35
36

/* @var $PAGE moodle_page */
global $PAGE;
$PAGE->set_url('/mod/labnbook/user-binding.php');
$PAGE->set_title("LabNbook");
$PAGE->set_heading("LabNbook");
37
$PAGE->set_context($context);
Francois Gannaz's avatar
Francois Gannaz committed
38
39
40
41
42
43

/* @var $OUTPUT moodle_core_output */
global $OUTPUT;
echo $OUTPUT->header();
?>

44
45
46
47
48
49
50
<p>
    Vous n'avez pas de compte dans LabNbook, ou ce compte n'est pas encore connu de Moodle.
</p>

<div class="container-fluid">
    <div class="row">
        <div class="col" style="text-align: center">
Francois Gannaz's avatar
Francois Gannaz committed
51
52
            <form action="<?= $fetchInstitution->getRedirectUrl(); ?>" method="GET">
                <input type="hidden" name="token" value="<?= $fetchInstitution->getBindingTokenForRedirect($CFG->wwwroot . $returnUrl, $role); ?>" />
53
54
55
56
57
58
                <button type="submit" class="btn btn-primary">Se connecter à LabNbook</button>
            </form>
            <div>Si vous possédez déjà un compte dans LabNbook, ce lien permettra de vous authentifier dans LabNbook et de mettre à jour votre compte Moodle.</div>
        </div>
        <div class="col" style="text-align: center">
            <form action="" method="POST">
Francois Gannaz's avatar
Francois Gannaz committed
59
60
                <input type="hidden" name="return" value="<?= \base64_encode($returnUrl); ?>" />
                <input type="hidden" name="courseid" value="<?= $COURSE->id; ?>" />
61
62
63
64
65
66
67
                <input type="hidden" name="create" value="1" />
                <button type="submit" class="btn btn-primary">Créer un nouveau compte dans LabNbook</button>
            </form>
            <div>Un compte sera créé dans LabNbook et associé à ce compte Moodle.</div>
        </div>
    </div>
</div>
Francois Gannaz's avatar
Francois Gannaz committed
68
69
70

<?php
echo $OUTPUT->footer();
71

Francois Gannaz's avatar
Francois Gannaz committed
72
73
function createUser($user, $isTeacher)
{
David Beniamine's avatar
David Beniamine committed
74
    $fetchInstitution = new fetch\institution();
75
76
77
78
79
80
81
82
83
    $success = $fetchInstitution->createUser([
        'first_name' => $user->firstname,
        'user_name' => $user->lastname,
        'login' => $user->username,
        'email' => $user->email,
        'role' => ($isTeacher ? 'teacher' : 'learner'),
    ]);
    return ($success === fetch\institution::OK);
}