forked from integral-learning/moodle-auth_mumie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_selector.php
100 lines (89 loc) · 4.31 KB
/
problem_selector.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
<?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/>.
/**
* Opens the Problem Selector with SSO
*
* @package auth_mumie
* @copyright 2017-2023 integral-learning GmbH (https://www.integral-learning.de/)
* @author Yannic Lapawczyk ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_mumie;
use auth_mumie\token\token_service;
use auth_mumie\user\mumie_user_service;
require_once("../../config.php");
require_once($CFG->dirroot . '/auth/mumie/classes/sso/sso_service.php');
/**
* Generates a hidden input field to send the selection when submitting the form
*
* @param string|null $selection The potential selection
*
* @return string The HTML representation of the hidden input field for the selection value
*/
function selection_input(?string $selection) : string {
if ($selection === null) {
return '';
}
return "<input type='hidden' name='selection' id='selection' type ='text' value='{$selection}'/>";
}
/**
* Generates a form to open the problem selector and submit the form automatically
*
* @param \stdClass $user The user object
* @param string $serverurl The server URL
* @param string $gradingtype The grading type
* @param string $problemlang The problem language
* @param string $origin The origin of the request
* @param string|null $selection The potential selection
*
* @return string The HTML representation of the problem selector form
* @throws \dml_exception
*/
function open_problem_selector(\stdClass $user, string $serverurl, string $gradingtype, string $problemlang,
string $origin, string $selection = null) : string {
$problemselectorurl = get_config('auth_mumie', 'mumie_problem_selector_url');
$mumieuser = mumie_user_service::get_problem_selector_user($user->id);
$ssotoken = token_service::generate_sso_token($mumieuser);
$org = get_config("auth_mumie", "mumie_org");
$selectioninput = selection_input($selection);
return"
<form id='mumie_problem_selector_form' name='mumie_problem_selector_form' method='post' action='{$problemselectorurl}/api/sso/problem-selector'>
<input type='hidden' name='userId' id='userId' type ='text' value='{$ssotoken->get_user()}'/>
<input type='hidden' name='token' id='token' type ='text' value='{$ssotoken->get_token()}'/>
<input type='hidden' name='org' id='org' type ='text' value='{$org}'/>
<input type='hidden' name='uiLang' id='uiLang' type ='text' value='{$user->lang}'/>
<input type='hidden' name='serverUrl' id='serverUrl' type ='text' value='{$serverurl}'/>
<input type='hidden' name='gradingType' id='gradingType' type ='text' value='{$gradingtype}'/>
<input type='hidden' name='problemLang' id='problemLang' type ='text' value='{$problemlang}'/>
<input type='hidden' name='origin' id='origin' type ='text' value='{$origin}'/>
{$selectioninput}
</form>
<script>
document.forms['mumie_problem_selector_form'].submit();
</script>
";
}
require_login();
global $USER;
$serverurl = required_param('serverurl', PARAM_URL);
$gradingtype = required_param('gradingtype', PARAM_ALPHA);
$problemlang = required_param('problemlang', PARAM_LANG);
$origin = required_param('origin', PARAM_URL);
$contextid = required_param('contextid', PARAM_INT);
$selection = optional_param('selection', null, PARAM_STRINGID);
$context = \context::instance_by_id($contextid);
require_capability('auth/mumie:ssotoproblemselector', $context);
echo open_problem_selector($USER, $serverurl, $gradingtype, $problemlang, $origin, $selection);