forked from integral-learning/moodle-auth_mumie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
171 lines (149 loc) · 4.8 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A library of functions and constants for the MUMIE auth plugin
*
* @package auth_mumie
* @copyright 2017-2020 integral-learning GmbH (https://www.integral-learning.de/)
* @author Tobias Goltz ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Get complete url for single sign in to MUMIE server
*
* @param stdClass $mumietask
* @return string login url
*/
function auth_mumie_get_login_url($mumietask) {
return $mumietask->server . 'public/xapi/auth/sso/login';
}
/**
* Get complete url for single sign out from MUMIE server
*
* @param stdClass $mumietask
* @return string logout url
*/
function auth_mumie_get_logout_url($mumietask) {
return $mumietask->server . 'public/xapi/auth/sso/logout';
}
/**
* Get complete url to the problem on MUMIE server
*
* @param stdClass $mumietask
* @return string login url
*/
function auth_mumie_get_problem_url($mumietask) {
return $mumietask->server . $mumietask->taskurl;
}
/**
* Get path to the problem on LEMON servers
*
* @param stdClass $mumietask
* @return string path
*/
function auth_mumie_get_problem_path($mumietask) {
return substr($mumietask->taskurl, 0, strpos($mumietask->taskurl, '?lang='));
}
/**
* Generate a randomized token for single sign in to MUMIE servers
*
* @param int $length word length of the token
* @return string token
*/
function auth_mumie_get_token($length) {
$token = "";
$codealphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$codealphabet .= "abcdefghijklmnopqrstuvwxyz";
$codealphabet .= "0123456789";
$max = strlen($codealphabet) - 1;
for ($i = 0; $i < $length; $i++) {
$token .= $codealphabet[rand(0, $max)];
}
return $token;
}
/**
* Get mumieserver_form as a fragment
*
* @param stdClass $args context and formdata
* @return string html code necessary to display mumieserver form as fragment
*/
function auth_mumie_output_fragment_new_mumieserver_form($args) {
global $CFG;
require_once($CFG->dirroot . '/auth/mumie/mumieserver_form.php');
$args = (object) $args;
$context = $args->context;
$o = '';
$formdata = [];
if (!empty($args->jsonformdata)) {
$serialiseddata = json_decode($args->jsonformdata);
if (is_string($serialiseddata)) {
parse_str($serialiseddata, $formdata);
} else {
$formdata = $serialiseddata;
}
}
$mumieserver = new stdClass();
$editoroptions = [
'maxfiles' => EDITOR_UNLIMITED_FILES,
'maxbytes' => $CFG->maxbytes,
'trust' => false,
'context' => $context,
'noclean' => true,
'subdirs' => false,
];
$mumieserver = file_prepare_standard_editor(
$mumieserver,
'description',
$editoroptions,
$context,
'mumieserver',
'description',
null
);
$mform = new mumieserver_form(null, array('editoroptions' => $editoroptions), 'post', '', null, true, $formdata);
$mform->set_data($mumieserver);
if (!empty($args->jsonformdata) && strcmp($args->jsonformdata, "[]") !== 0) {
// If we were passed non-empty form data we want the mform to call validation functions and show errors.
$mform->is_validated();
}
ob_start();
$mform->display();
$o .= ob_get_contents();
ob_end_clean();
return $o;
}
/**
* Get a hashed string from the moodle user id.
*
* Some institutions use personal data (like matriculation numbers) as user id in moodle.
* We need to pseudonymize the id to improve data protection.
* We use the first 10 characters of the xapi-key as salt to further increase security.
*
* @param string $id userId that should be hashed
* @return string Hashed string with 128 characters
*/
function auth_mumie_get_hashed_id($id) {
return hash("sha512", $id . substr(get_config('auth_mumie', 'mumie_api_key'), 0, 10));
}
/**
* Transforms the deadline(Unix Timestamp) from seconds to milliseconds.
* @param int $deadline timestamp in s
* @return int timestamp in ms
*/
function auth_mumie_get_deadline_in_ms($deadline) {
return $deadline * 1000;
}