helper.php 2.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @copyright   2019 Université Grenoble Alpes
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace mod_labnbook\local;

10
11
use mod_labnbook\fetch;

12
13
14
15
16
/* @var $DB \moodle_database */

/**
 * Misc functions.
 */
17
18
19
20
21
22
23
24
25
class helper
{
    /**
     * Stop loading the current page if the current user has no LabnBook account.
     * Redirects on an account binding|creation if necessary.
     *
     * @global \Stdclass $COURSE
     * @throws \moodle_exception
     */
Francois Gannaz's avatar
Francois Gannaz committed
26
27
    public static function requireLabnbookAuthentication()
    {
28
        global $COURSE;
29
        global $CFG;
30
31
32
        if (fetch\user::isAuthenticated()) {
            return;
        }
Francois Gannaz's avatar
Francois Gannaz committed
33
        $institutionFetcher = new fetch\institution();
34
35
36
37
38
        switch ($institutionFetcher->loginUser()) {
            case fetch\institution::OK:
               return;
            case fetch\institution::ERROR_LOGIN_UNKNOWN:
                redirect(
39
                    $CFG->wwwroot."/mod/labnbook/user-binding.php?courseid={$COURSE->id}&return="
Francois Gannaz's avatar
Francois Gannaz committed
40
                    . \base64_encode($_SERVER['REQUEST_URI'])
41
42
43
44
45
46
47
48
                );
                break;
            case fetch\institution::ERROR_TOKEN_NOTVALID:
               throw new \moodle_exception("Internal error with LabNbook");
            default:
               throw new \moodle_exception("Unknown error with LabNbook");
        }
    }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

    /**
     * Grant teacher access to the LnB missions and classes linked to the course.
     *
     * @param int $courseId
     */
    public static function grantTeacherAccess($courseId)
    {
        global $DB, $SESSION;
        if (!empty($SESSION->labnbook->grantTeacherAccess[$courseId])) {
            return;
        }
        if (!fetch\user::isAuthenticated()) {
            return;
        }
64
        $ids = $DB->get_fieldset_select("labnbook", "labnbook_teamconfigid", "course = " . (int) $courseId);
65
66
67
68
69
70
71
72
73
74
        if ($ids) {
            $userFetcher = new fetch\user();
            $userFetcher->grantMissionsAccess($ids);
        }
        // use session to avoid repeating this query
        if (empty($SESSION->labnbook->grantTeacherAccess)) {
            $SESSION->labnbook->grantTeacherAccess = [];
        }
        $SESSION->labnbook->grantTeacherAccess[$courseId] = true;
    }
75
}