forked from tmuras/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodedit.php
283 lines (226 loc) · 11.1 KB
/
modedit.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php // $Id$
// adds or updates modules in a course using new formslib
require("../config.php");
require("lib.php");
require_login();
$add = optional_param('add', '', PARAM_ALPHA);
$update = optional_param('update', 0, PARAM_INT);
//return to course/view.php if false or mod/modname/view.php if true
$return = optional_param('return', 0, PARAM_BOOL);
if (!empty($add)){
$section = required_param('section', PARAM_INT);
$course = required_param('course', PARAM_INT);
if (! $course = get_record("course", "id", $course)) {
error("This course doesn't exist");
}
if (! $module = get_record("modules", "name", $add)) {
error("This module type doesn't exist");
}
$context = get_context_instance(CONTEXT_COURSE, $course->id);
require_capability('moodle/course:manageactivities', $context);
if (!course_allowed_module($course, $module->id)) {
error("This module has been disabled for this particular course");
}
require_login($course->id); // needed to setup proper $COURSE
$form->section = $section; // The section number itself
$form->course = $course->id;
$form->module = $module->id;
$form->modulename = $module->name;
$form->instance = "";
$form->coursemodule = "";
$form->add=$add;
$form->return=0;//must be false if this is an add, go back to course view on cancel
if (!empty($type)) {
$form->type = $type;
}
$sectionname = get_section_name($course->format);
$fullmodulename = get_string("modulename", $module->name);
if ($form->section && $course->format != 'site') {
$heading->what = $fullmodulename;
$heading->to = "$sectionname $form->section";
$pageheading = get_string("addinganewto", "moodle", $heading);
} else {
$pageheading = get_string("addinganew", "moodle", $fullmodulename);
}
$strnav = '';
}elseif (!empty($update)){
if (! $cm = get_record("course_modules", "id", $update)) {
error("This course module doesn't exist");
}
if (! $course = get_record("course", "id", $cm->course)) {
error("This course doesn't exist");
}
require_login($course->id); // needed to setup proper $COURSE
$context = get_context_instance(CONTEXT_COURSE, $course->id);
require_capability('moodle/course:manageactivities', $context);
if (! $module = get_record("modules", "id", $cm->module)) {
error("This module doesn't exist");
}
if (! $form = get_record($module->name, "id", $cm->instance)) {
error("The required instance of this module doesn't exist");
}
if (! $cw = get_record("course_sections", "id", $cm->section)) {
error("This course section doesn't exist");
}
$form->coursemodule = $cm->id;
$form->section = $cm->section; // The section ID
$form->course = $course->id;
$form->module = $module->id;
$form->modulename = $module->name;
$form->instance = $cm->instance;
$form->return = $return;
$form->update = $update;
$sectionname = get_section_name($course->format);
$fullmodulename = get_string("modulename", $module->name);
if ($form->section && $course->format != 'site') {
$heading->what = $fullmodulename;
$heading->in = "$sectionname $cw->section";
$pageheading = get_string("updatingain", "moodle", $heading);
} else {
$pageheading = get_string("updatinga", "moodle", $fullmodulename);
}
$strnav = "<a href=\"$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id\">".format_string($form->name,true)."</a> ->";
}else{
error('Invalid operation.');
}
$modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
if (file_exists($modmoodleform)) {
require_once($modmoodleform);
}else{
error('No formslib form description file found for this activity.');
}
$modlib = "$CFG->dirroot/mod/$module->name/lib.php";
if (file_exists($modlib)) {
include_once($modlib);
} else {
error("This module is missing important code! ($modlib)");
}
$mformclassname = 'mod_'.$module->name.'_mod_form';
$cousesection=isset($cw->section)?$cw->section:$section;
$mform=& new $mformclassname($form->instance, $cousesection, ((isset($cm))?$cm:null));
$mform->set_data($form);
if ($mform->is_cancelled()) {
if ($return && isset($cm)){
redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
} else {
redirect("view.php?id=$course->id#section-".$cousesection);
}
} elseif ($fromform=$mform->get_data()){
if (empty($fromform->coursemodule)) { //add
if (! $course = get_record("course", "id", $fromform->course)) {
error("This course doesn't exist");
}
$fromform->instance = '';
$fromform->coursemodule = '';
} else { //update
if (! $cm = get_record("course_modules", "id", $fromform->coursemodule)) {
error("This course module doesn't exist");
}
if (! $course = get_record("course", "id", $cm->course)) {
error("This course doesn't exist");
}
$fromform->instance = $cm->instance;
$fromform->coursemodule = $cm->id;
}
require_login($course->id); // needed to setup proper $COURSE
$context = get_context_instance(CONTEXT_COURSE, $course->id);
require_capability('moodle/course:manageactivities', $context);
$fromform->course = $course->id;
$fromform->modulename = clean_param($fromform->modulename, PARAM_SAFEDIR); // For safety
$addinstancefunction = $fromform->modulename."_add_instance";
$updateinstancefunction = $fromform->modulename."_update_instance";
if (!empty($fromform->update)) {
if (isset($fromform->name)) {
if (trim($fromform->name) == '') {
unset($fromform->name);
}
}
$returnfromfunc = $updateinstancefunction($fromform);
if (!$returnfromfunc) {
error("Could not update the $fromform->modulename", "view.php?id=$course->id");
}
if (is_string($returnfromfunc)) {
error($returnfromfunc, "view.php?id=$course->id");
}
if (isset($fromform->visible)) {
set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
}
if (isset($fromform->groupmode)) {
set_coursemodule_groupmode($fromform->coursemodule, $fromform->groupmode);
}
add_to_log($course->id, "course", "update mod",
"../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
"$fromform->modulename $fromform->instance");
add_to_log($course->id, $fromform->modulename, "update",
"view.php?id=$fromform->coursemodule",
"$fromform->instance", $fromform->coursemodule);
}elseif (!empty($fromform->add)){
if (!course_allowed_module($course,$fromform->modulename)) {
error("This module ($fromform->modulename) has been disabled for this particular course");
}
if (!isset($fromform->name) || trim($fromform->name) == '') {
$fromform->name = get_string("modulename", $fromform->modulename);
}
$returnfromfunc = $addinstancefunction($fromform);
if (!$returnfromfunc) {
/*if (file_exists($moderr)) {
$form = $fromform;
include_once($moderr);
die;
}*/
error("Could not add a new instance of $fromform->modulename", "view.php?id=$course->id");
}
if (is_string($returnfromfunc)) {
error($returnfromfunc, "view.php?id=$course->id");
}
if (!isset($fromform->groupmode)) { // to deal with pre-1.5 modules
$fromform->groupmode = $course->groupmode; /// Default groupmode the same as course
}
$fromform->instance = $returnfromfunc;
// course_modules and course_sections each contain a reference
// to each other, so we have to update one of them twice.
if (! $fromform->coursemodule = add_course_module($fromform) ) {
error("Could not add a new course module");
}
if (! $sectionid = add_mod_to_section($fromform) ) {
error("Could not add the new course module to that section");
}
if (! set_field("course_modules", "section", $sectionid, "id", $fromform->coursemodule)) {
error("Could not update the course module with the correct section");
}
if (!isset($fromform->visible)) { // We get the section's visible field status
$fromform->visible = get_field("course_sections","visible","id",$sectionid);
}
// make sure visibility is set correctly (in particular in calendar)
set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
add_to_log($course->id, "course", "add mod",
"../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
"$fromform->modulename $fromform->instance");
add_to_log($course->id, $fromform->modulename, "add",
"view.php?id=$fromform->coursemodule",
"$fromform->instance", $fromform->coursemodule);
}else{
error("Data submitted is invalid.");
}
rebuild_course_cache($course->id);
redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
exit;
}else{
$context = get_context_instance(CONTEXT_COURSE, $course->id);
require_capability('moodle/course:manageactivities', $context);
$streditinga = get_string("editinga", "moodle", $fullmodulename);
$strmodulenameplural = get_string("modulenameplural", $module->name);
print_header_simple($streditinga, '',
"<a href=\"$CFG->wwwroot/mod/$module->name/index.php?id=$course->id\">$strmodulenameplural</a> ->
$strnav $streditinga", $mform->focus(), "", false);
if (!empty($cm->id)) {
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$currenttab = 'update';
include_once($CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php');
}
$icon = '<img src="'.$CFG->modpixpath.'/'.$module->name.'/icon.gif" alt=""/>';
print_heading_with_help($pageheading, "mods", $module->name, $icon);
$mform->display();
print_footer($course);
}
?>