user-binding.php 2.86 KB
Newer Older
Francois Gannaz's avatar
Francois Gannaz committed
1
2
3
4
5
6
7
8
9
10
11
<?php

/**
 * Inner HTL page.
 * Bind a Moodle user to a LabNbook account
 *
 * @package     mod_labnbook
 * @copyright   2019 Université Grenoble Alpes
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

12
13
14
15
16
17
18
19
20
21
22
23
24
use \mod_labnbook\fetch;

require_once dirname(dirname(__DIR__)) . '/config.php';

$returnUrl = base64_decode(required_param('return', PARAM_ALPHANUM));
$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);
25
$isTeacher = has_capability('mod/labnbook:addinstance', $context);
26
27
28
29
30
31
if ($create && !fetch\user::isAuthenticated()) {
    global $USER;
    if (createUser($USER, $isTeacher)) {
        redirect($returnUrl);
    }
}
32
$role = ($isTeacher ? 'teacher' : 'learner');
Francois Gannaz's avatar
Francois Gannaz committed
33
34
35
36
37
38

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

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

46
47
48
49
50
51
52
<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">
53
54
            <form action="<?= $fetchInstitution->getRedirectUrl() ?>" method="GET">
                <input type="hidden" name="token" value="<?= $fetchInstitution->getBindingTokenForRedirect($CFG->wwwroot . $returnUrl, $role) ?>" />
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
                <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">
                <input type="hidden" name="return" value="<?= base64_encode($returnUrl) ?>" />
                <input type="hidden" name="courseid" value="<?= $COURSE->id ?>" />
                <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
70
71
72

<?php
echo $OUTPUT->footer();
73
74
75
76
77
78
79
80
81
82
83

function createUser($user, $isTeacher) {
    $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);
}