MDL-64506 templates: replace spans where and col-x appears
[moodle.git] / admin / tool / oauth2 / userfieldmappings.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * OAuth 2 Endpoint Configuration page.
19  *
20  * @package    tool_oauth2
21  * @copyright  2017 Damyon Wiese <damyon@moodle.com>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 require_once(__DIR__ . '/../../../config.php');
26 require_once($CFG->libdir.'/adminlib.php');
27 require_once($CFG->libdir.'/tablelib.php');
29 $PAGE->set_url('/admin/tool/oauth2/userfieldmappings.php', ['issuerid' => required_param('issuerid', PARAM_INT)]);
30 $PAGE->set_context(context_system::instance());
31 $PAGE->set_pagelayout('admin');
32 $strheading = get_string('pluginname', 'tool_oauth2');
33 $PAGE->set_title($strheading);
34 $PAGE->set_heading($strheading);
36 require_login();
38 require_capability('moodle/site:config', context_system::instance());
40 $renderer = $PAGE->get_renderer('tool_oauth2');
42 $action = optional_param('action', '', PARAM_ALPHAEXT);
43 $issuerid = required_param('issuerid', PARAM_INT);
44 $userfieldmappingid = optional_param('userfieldmappingid', '', PARAM_INT);
45 $userfieldmapping = null;
46 $mform = null;
48 $issuer = \core\oauth2\api::get_issuer($issuerid);
49 if (!$issuer) {
50     print_error('invaliddata');
51 }
52 $PAGE->navbar->override_active_url(new moodle_url('/admin/tool/oauth2/issuers.php'), true);
54 if (!empty($userfieldmappingid)) {
55     $userfieldmapping = \core\oauth2\api::get_user_field_mapping($userfieldmappingid);
56 }
58 if ($action == 'edit') {
59     if ($userfieldmapping) {
60         $PAGE->navbar->add(get_string('edituserfieldmapping', 'tool_oauth2', s($issuer->get('name'))));
61     } else {
62         $PAGE->navbar->add(get_string('createnewuserfieldmapping', 'tool_oauth2', s($issuer->get('name'))));
63     }
65     $mform = new \tool_oauth2\form\user_field_mapping(null, ['persistent' => $userfieldmapping, 'issuerid' => $issuerid]);
66 }
68 if ($mform && $mform->is_cancelled()) {
69     redirect(new moodle_url('/admin/tool/oauth2/userfieldmappings.php', ['issuerid' => $issuerid]));
70 } else if ($action == 'edit') {
72     if ($data = $mform->get_data()) {
74         try {
75             if (!empty($data->id)) {
76                 core\oauth2\api::update_user_field_mapping($data);
77             } else {
78                 core\oauth2\api::create_user_field_mapping($data);
79             }
80             redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
81         } catch (Exception $e) {
82             redirect($PAGE->url, $e->getMessage(), null, \core\output\notification::NOTIFY_ERROR);
83         }
84     } else {
85         echo $OUTPUT->header();
86         if ($issuer) {
87             echo $OUTPUT->heading(get_string('edituserfieldmapping', 'tool_oauth2', s($issuer->get('name'))));
88         } else {
89             echo $OUTPUT->heading(get_string('createnewuserfieldmapping', 'tool_oauth2', s($issuer->get('name'))));
90         }
91         $mform->display();
92         echo $OUTPUT->footer();
93     }
95 } else if ($action == 'delete') {
97     if (!optional_param('confirm', false, PARAM_BOOL)) {
98         $continueparams = [
99             'action' => 'delete',
100             'issuerid' => $issuerid,
101             'userfieldmappingid' => $userfieldmappingid,
102             'sesskey' => sesskey(),
103             'confirm' => true
104         ];
105         $continueurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', $continueparams);
106         $cancelurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php');
107         echo $OUTPUT->header();
108         $str = get_string('deleteuserfieldmappingconfirm', 'tool_oauth2', s($issuer->get('name')));
109         echo $OUTPUT->confirm($str, $continueurl, $cancelurl);
110         echo $OUTPUT->footer();
111     } else {
112         require_sesskey();
113         core\oauth2\api::delete_user_field_mapping($userfieldmappingid);
114         redirect($PAGE->url, get_string('userfieldmappingdeleted', 'tool_oauth2'), null, \core\output\notification::NOTIFY_SUCCESS);
115     }
117 } else {
118     echo $OUTPUT->header();
119     echo $OUTPUT->heading(get_string('userfieldmappingsforissuer', 'tool_oauth2', s($issuer->get('name'))));
120     $userfieldmappings = core\oauth2\api::get_user_field_mappings($issuer);
121     echo $renderer->user_field_mappings_table($userfieldmappings, $issuerid);
123     $addurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', ['action' => 'edit', 'issuerid' => $issuerid]);
124     echo $renderer->single_button($addurl, get_string('createnewuserfieldmapping', 'tool_oauth2', s($issuer->get('name'))));
125     echo $OUTPUT->footer();