forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.php
180 lines (164 loc) · 6.68 KB
/
repository.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
172
173
174
175
176
177
178
179
180
<?php
require_once(dirname(dirname(__FILE__)) . '/config.php');
require_once($CFG->dirroot . '/repository/lib.php');
require_once($CFG->libdir . '/adminlib.php');
$edit = optional_param('edit', 0, PARAM_FORMAT);
$new = optional_param('new', '', PARAM_FORMAT);
$hide = optional_param('hide', '', PARAM_FORMAT);
$delete = optional_param('delete', 0, PARAM_FORMAT);
$sure = optional_param('sure', '', PARAM_ALPHA);
$move = optional_param('move', '', PARAM_ALPHANUM);
$type = optional_param('type', '', PARAM_ALPHANUM);
$display = true; // fall through to normal display
$pagename = 'repositorycontroller';
if ($edit) {
$pagename = 'repositorysettings' . $edit;
} else if ($delete) {
$pagename = 'repositorydelete';
} else if ($new) {
$pagename = 'repositorynew';
}
require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
admin_externalpage_setup($pagename);
$sesskeyurl = $CFG->wwwroot.'/'.$CFG->admin.'/repository.php?sesskey=' . sesskey();
$baseurl = $CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=managerepositories';
$configstr = get_string('managerepositories', 'repository');
$return = true;
if (!empty($edit) || !empty($new)) {
if (!empty($edit)) {
$repositorytype = repository::get_type_by_typename($edit);
$classname = 'repository_' . $repositorytype->get_typename();
$configs = call_user_func(array($classname,'get_type_option_names'));
$plugin = $repositorytype->get_typename();
} else {
$repositorytype = null;
$plugin = $new;
$typeid = $new;
}
$PAGE->set_pagetype('admin-repository-' . $plugin);
// display the edit form for this instance
$mform = new repository_type_form('', array('plugin' => $plugin, 'instance' => $repositorytype));
$fromform = $mform->get_data();
//detect if we create a new type without config (in this case if don't want to display a setting page during creation)
$nosettings = false;
if (!empty($new)) {
$adminconfignames = repository::static_function($new, 'get_type_option_names');
$nosettings = empty($adminconfignames);
}
// end setup, begin output
if ($mform->is_cancelled()){
redirect($baseurl);
} else if (!empty($fromform) || $nosettings) {
if (!confirm_sesskey()) {
print_error('confirmsesskeybad', '', $baseurl);
}
if ($edit) {
$settings = array();
foreach($configs as $config) {
if (!empty($fromform->$config)) {
$settings[$config] = $fromform->$config;
} else {
// if the config name is not appear in $fromform
// empty this config value
$settings[$config] = '';
}
}
$instanceoptionnames = repository::static_function($edit, 'get_instance_option_names');
if (!empty($instanceoptionnames)) {
if (array_key_exists('enablecourseinstances', $fromform)) {
$settings['enablecourseinstances'] = $fromform->enablecourseinstances;
}
else {
$settings['enablecourseinstances'] = 0;
}
if (array_key_exists('enableuserinstances', $fromform)) {
$settings['enableuserinstances'] = $fromform->enableuserinstances;
}
else {
$settings['enableuserinstances'] = 0;
}
}
$success = $repositorytype->update_options($settings);
} else {
$type = new repository_type($plugin, (array)$fromform);
$type->create();
$success = true;
$data = data_submitted();
}
if ($success) {
$savedstr = get_string('configsaved', 'repository');
$has_instance = repository::static_function($plugin, 'get_instance_option_names');
if (!empty($has_instance)) {
// no common setting for this type, so go to setup instances
redirect($sesskeyurl.'&edit='.$plugin, $savedstr, 1);
} else {
// configs saved
redirect($baseurl, $savedstr, 1);
}
} else {
print_error('instancenotsaved', 'repository', $baseurl);
}
exit;
} else {
admin_externalpage_print_header();
echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
$displaysettingform = true;
if ($edit) {
$typeoptionnames = repository::static_function($edit, 'get_type_option_names');
$instanceoptionnames = repository::static_function($edit, 'get_instance_option_names');
if (empty($typeoptionnames) && empty($instanceoptionnames)) {
$displaysettingform = false;
}
}
if ($displaysettingform){
$OUTPUT->box_start();
$mform->display();
$OUTPUT->box_end();
}
$return = false;
//display instances list and creation form
if ($edit){
$instanceoptionnames = repository::static_function($edit, 'get_instance_option_names');
if (!empty($instanceoptionnames)){
repository::display_instances_list(get_context_instance(CONTEXT_SYSTEM), $edit);
}
}
}
} else if (!empty($hide)) {
if (!confirm_sesskey()) {
print_error('confirmsesskeybad', '', $baseurl);
}
$repositorytype = repository::get_type_by_typename($hide);
if (empty($repositorytype)) {
print_error('invalidplugin', 'repository', '', $hide);
}
$repositorytype->switch_and_update_visibility();
$return = true;
} else if (!empty($delete)) {
$repositorytype = repository::get_type_by_typename($delete);
if ($sure) {
$PAGE->set_pagetype('admin-repository-' . $delete);
if (!confirm_sesskey()) {
print_error('confirmsesskeybad', '', $baseurl);
}
if ($repositorytype->delete()) {
$deletedstr = get_string('removed', 'repository');
redirect($baseurl, $deletedstr, 3);
} else {
print_error('instancenotdeleted', 'repository', $baseurl);
}
exit;
} else {
admin_externalpage_print_header();
echo $OUTPUT->confirm(get_string('confirmremove', 'repository', $repositorytype->get_readablename()), $sesskeyurl . '&delete=' . $delete . '&sure=yes', $baseurl);
$return = false;
}
}
else if (!empty($move) && !empty($type)) {
$repositorytype = repository::get_type_by_typename($type);
$repositorytype->move_order($move);
}
if (!empty($return)) {
redirect($baseurl);
}
echo $OUTPUT->footer();