Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
// This file is part of Moodle - http://moodle.org/
4
//
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
/**
19
 * Moves, adds, updates, duplicates or deletes modules in a course
20
 *
21
 * @copyright 1999 Martin Dougiamas  http://dougiamas.com
22
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @package course
24
 */
25
 
26
require("../config.php");
27
require_once("lib.php");
28
 
29
$sectionreturn = optional_param('sr', null, PARAM_INT);
30
$add           = optional_param('add', '', PARAM_ALPHANUM);
31
$type          = optional_param('type', '', PARAM_ALPHA);
1441 ariadna 32
$indent        = optional_param('indent', 0, PARAM_INT); // TODO remove this param as part of MDL-83530.
1 efrain 33
$update        = optional_param('update', 0, PARAM_INT);
1441 ariadna 34
$duplicate     = optional_param('duplicate', 0, PARAM_INT); // TODO remove this param as part of MDL-83530.
35
$hide          = optional_param('hide', 0, PARAM_INT); // TODO remove this param as part of MDL-83530.
36
$stealth       = optional_param('stealth', 0, PARAM_INT); // TODO remove this param as part of MDL-83530.
37
$show          = optional_param('show', 0, PARAM_INT); // TODO remove this param as part of MDL-83530.
38
$copy          = optional_param('copy', 0, PARAM_INT); // TODO remove this param as part of MDL-83530.
39
$moveto        = optional_param('moveto', 0, PARAM_INT); // TODO remove this param as part of MDL-83530.
40
$movetosection = optional_param('movetosection', 0, PARAM_INT); // TODO remove this param as part of MDL-83530.
41
$delete        = optional_param('delete', 0, PARAM_INT); // TODO remove this param as part of MDL-83530.
1 efrain 42
$course        = optional_param('course', 0, PARAM_INT);
1441 ariadna 43
$groupmode     = optional_param('groupmode', -1, PARAM_INT); // TODO remove this param as part of MDL-83530.
44
$cancelcopy    = optional_param('cancelcopy', 0, PARAM_BOOL); // TODO remove this param as part of MDL-83530.
45
$confirm       = optional_param('confirm', 0, PARAM_BOOL); // TODO remove this param as part of MDL-83530.
1 efrain 46
 
47
// This page should always redirect
48
$url = new moodle_url('/course/mod.php');
49
foreach (compact('indent','update','hide','show','copy','moveto','movetosection','delete','course','cancelcopy','confirm') as $key=>$value) {
50
    if ($value !== 0) {
51
        $url->param($key, $value);
52
    }
53
}
54
// Force it to be null if it's not a valid section number.
55
if ($sectionreturn < 0) {
56
    $sectionreturn = null;
57
}
58
$urloptions = [];
59
if (!is_null($sectionreturn)) {
60
    $url->param('sr', $sectionreturn);
61
    $urloptions['sr'] = $sectionreturn;
62
}
63
if ($add !== '') {
64
    $url->param('add', $add);
65
}
66
if ($type !== '') {
67
    $url->param('type', $type);
68
}
69
if ($groupmode !== '') {
70
    $url->param('groupmode', $groupmode);
71
}
72
$PAGE->set_url($url);
73
 
74
require_login();
75
 
76
//check if we are adding / editing a module that has new forms using formslib
77
if (!empty($add)) {
78
    $id          = required_param('id', PARAM_INT);
79
    $section     = required_param('section', PARAM_INT);
80
    $type        = optional_param('type', '', PARAM_ALPHA);
81
    $returntomod = optional_param('return', 0, PARAM_BOOL);
82
    $beforemod   = optional_param('beforemod', 0, PARAM_INT);
83
 
84
    $params = [
85
        'add' => $add,
86
        'type' => $type,
87
        'course' => $id,
88
        'section' => $section,
89
        'return' => $returntomod,
90
        'beforemod' => $beforemod,
91
    ];
92
    if (!is_null($sectionreturn)) {
93
        $params['sr'] = $sectionreturn;
94
    }
95
 
96
    redirect(
97
        new moodle_url(
98
            '/course/modedit.php',
99
            $params,
100
        )
101
    );
102
 
103
} else if (!empty($update)) {
104
    $cm = get_coursemodule_from_id('', $update, 0, true, MUST_EXIST);
105
    $returntomod = optional_param('return', 0, PARAM_BOOL);
106
 
107
    $params = [
108
        'update' => $update,
109
        'return' => $returntomod,
110
    ];
111
    if (!is_null($sectionreturn)) {
112
        $params['sr'] = $sectionreturn;
113
    }
114
    redirect(
115
        new moodle_url(
116
            '/course/modedit.php',
117
            $params,
118
        )
119
    );
120
} else if (!empty($duplicate) and confirm_sesskey()) {
1441 ariadna 121
    // TODO remove this else if as part of MDL-83530.
122
    debugging(
123
        'The duplicate param is deprecated. Please use action cm_duplicate in course/format/update.php instead.',
124
        DEBUG_DEVELOPER
125
    );
1 efrain 126
     $cm     = get_coursemodule_from_id('', $duplicate, 0, true, MUST_EXIST);
127
     $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
128
 
129
    require_login($course, false, $cm);
1441 ariadna 130
    $coursecontext = context_course::instance($course->id);
131
    require_all_capabilities(['moodle/backup:backuptargetimport', 'moodle/restore:restoretargetimport'], $coursecontext);
1 efrain 132
 
133
    // Duplicate the module.
134
    $newcm = duplicate_module($course, $cm);
135
    redirect(course_get_url($course, $cm->sectionnum, $urloptions));
136
 
137
} else if (!empty($delete)) {
1441 ariadna 138
    // TODO remove this else if as part of MDL-83530.
139
    debugging(
140
        'The delete param is deprecated. Please use action cm_delete in course/format/update.php instead.',
141
        DEBUG_DEVELOPER
142
    );
1 efrain 143
    $cm     = get_coursemodule_from_id('', $delete, 0, true, MUST_EXIST);
144
    $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
145
 
146
    require_login($course, false, $cm);
147
    $modcontext = context_module::instance($cm->id);
148
    require_capability('moodle/course:manageactivities', $modcontext);
149
 
1441 ariadna 150
    if (plugin_supports('mod', $cm->modname, FEATURE_PUBLISHES_QUESTIONS)) {
151
        $return = \core_question\local\bank\question_bank_helper::get_url_for_qbank_list($course->id);
152
    } else {
153
        $return = course_get_url($course, $cm->sectionnum, $urloptions);
154
    }
1 efrain 155
 
156
    if (!$confirm or !confirm_sesskey()) {
157
        $fullmodulename = get_string('modulename', $cm->modname);
158
 
159
        $optionsyes = [
160
            'confirm' => 1,
161
            'delete' => $cm->id,
162
            'sesskey' => sesskey(),
163
        ];
164
        if (!is_null($sectionreturn)) {
165
            $optionsyes['sr'] = $sectionreturn;
166
        }
167
        $strdeletecheck = get_string('deletecheck', '', $fullmodulename);
168
        $strparams = (object)array('type' => $fullmodulename, 'name' => $cm->name);
169
        $strdeletechecktypename = get_string('deletechecktypename', '', $strparams);
170
 
171
        $PAGE->set_pagetype('mod-' . $cm->modname . '-delete');
172
        $PAGE->set_title($strdeletecheck);
173
        $PAGE->set_heading($course->fullname);
174
        $PAGE->navbar->add($strdeletecheck);
175
 
176
        echo $OUTPUT->header();
177
        echo $OUTPUT->box_start('noticebox');
178
        $formcontinue = new single_button(new moodle_url("$CFG->wwwroot/course/mod.php", $optionsyes), get_string('yes'));
179
        $formcancel = new single_button($return, get_string('no'), 'get');
180
        echo $OUTPUT->confirm($strdeletechecktypename, $formcontinue, $formcancel);
181
        echo $OUTPUT->box_end();
182
        echo $OUTPUT->footer();
183
 
184
        exit;
185
    }
186
 
187
    // Delete the module.
188
    course_delete_module($cm->id);
189
 
190
    redirect($return);
191
}
192
 
193
 
194
if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) {
1441 ariadna 195
    // TODO remove this if as part of MDL-83530.
196
    debugging(
197
        'The moveto param is deprecated. Please use the standard move modal instead.',
198
        DEBUG_DEVELOPER
199
    );
1 efrain 200
    $cm     = get_coursemodule_from_id('', $USER->activitycopy, 0, true, MUST_EXIST);
201
    $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
202
 
203
    require_login($course, false, $cm);
204
    $coursecontext = context_course::instance($course->id);
205
    $modcontext = context_module::instance($cm->id);
206
    require_capability('moodle/course:manageactivities', $modcontext);
207
 
208
    if (!empty($movetosection)) {
209
        if (!$section = $DB->get_record('course_sections', array('id'=>$movetosection, 'course'=>$cm->course))) {
210
            throw new \moodle_exception('sectionnotexist');
211
        }
212
        $beforecm = NULL;
213
 
214
    } else {                      // normal moveto
215
        if (!$beforecm = get_coursemodule_from_id('', $moveto, $cm->course, true)) {
216
            throw new \moodle_exception('invalidcoursemodule');
217
        }
218
        if (!$section = $DB->get_record('course_sections', array('id'=>$beforecm->section, 'course'=>$cm->course))) {
219
            throw new \moodle_exception('sectionnotexist');
220
        }
221
    }
222
 
223
    if (!ismoving($section->course)) {
224
        throw new \moodle_exception('needcopy', '', "view.php?id=$section->course");
225
    }
226
 
227
    moveto_module($cm, $section, $beforecm);
228
 
229
    $sectionreturn = $USER->activitycopysectionreturn;
230
    unset($USER->activitycopy);
231
    unset($USER->activitycopycourse);
232
    unset($USER->activitycopyname);
233
    unset($USER->activitycopysectionreturn);
234
 
235
    redirect(course_get_url($course, $section->section, $urloptions));
236
 
237
} else if (!empty($indent) and confirm_sesskey()) {
1441 ariadna 238
    // TODO remove this else if as part of MDL-83530.
239
    debugging(
240
        'The indent param deprecated. Please use action cm_moveleft and cm_moveright in course/format/update.php instead.',
241
        DEBUG_DEVELOPER
242
    );
1 efrain 243
    $id = required_param('id', PARAM_INT);
244
 
245
    $cm     = get_coursemodule_from_id('', $id, 0, true, MUST_EXIST);
246
    $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
247
 
248
    require_login($course, false, $cm);
249
    $coursecontext = context_course::instance($course->id);
250
    $modcontext = context_module::instance($cm->id);
251
    require_capability('moodle/course:manageactivities', $modcontext);
252
 
253
    $cm->indent += $indent;
254
 
255
    if ($cm->indent < 0) {
256
        $cm->indent = 0;
257
    }
258
 
259
    $DB->set_field('course_modules', 'indent', $cm->indent, array('id'=>$cm->id));
260
 
261
    \course_modinfo::purge_course_module_cache($cm->course, $cm->id);
262
    // Rebuild invalidated module cache.
263
    rebuild_course_cache($cm->course, false, true);
264
 
265
    redirect(course_get_url($course, $cm->sectionnum, $urloptions));
266
 
267
} else if (!empty($hide) and confirm_sesskey()) {
1441 ariadna 268
    // TODO remove this else if as part of MDL-83530.
269
    debugging(
270
        'The hide param deprecated. Please use action cm_hide in course/format/update.php instead.',
271
        DEBUG_DEVELOPER
272
    );
1 efrain 273
    $cm     = get_coursemodule_from_id('', $hide, 0, true, MUST_EXIST);
274
    $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
275
 
276
    require_login($course, false, $cm);
277
    $coursecontext = context_course::instance($course->id);
278
    $modcontext = context_module::instance($cm->id);
279
    require_capability('moodle/course:activityvisibility', $modcontext);
280
 
281
    if (set_coursemodule_visible($cm->id, 0)) {
282
        \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
283
    }
284
    redirect(course_get_url($course, $cm->sectionnum, $urloptions));
285
 
286
} else if (!empty($stealth) and confirm_sesskey()) {
1441 ariadna 287
    // TODO remove this else if as part of MDL-83530.
288
    debugging(
289
        'The stealth param deprecated. Please use action cm_stealth in course/format/update.php instead.',
290
        DEBUG_DEVELOPER
291
    );
1 efrain 292
    list($course, $cm) = get_course_and_cm_from_cmid($stealth);
293
    require_login($course, false, $cm);
294
    require_capability('moodle/course:activityvisibility', $cm->context);
295
 
296
    if (set_coursemodule_visible($cm->id, 1, 0)) {
297
        \core\event\course_module_updated::create_from_cm($cm)->trigger();
298
    }
299
    redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
300
 
301
} else if (!empty($show) and confirm_sesskey()) {
1441 ariadna 302
    // TODO remove this else if as part of MDL-83530.
303
    debugging(
304
        'The show param deprecated. Please use action cm_show in course/format/update.php instead.',
305
        DEBUG_DEVELOPER
306
    );
1 efrain 307
    list($course, $cm) = get_course_and_cm_from_cmid($show);
308
    require_login($course, false, $cm);
309
    require_capability('moodle/course:activityvisibility', $cm->context);
310
    $section = $cm->get_section_info();
311
 
312
    if (set_coursemodule_visible($cm->id, 1)) {
313
        \core\event\course_module_updated::create_from_cm($cm)->trigger();
314
    }
315
    redirect(course_get_url($course, $section->section, $urloptions));
316
 
317
} else if ($groupmode > -1 and confirm_sesskey()) {
1441 ariadna 318
    // TODO remove this else if as part of MDL-83530.
319
    debugging(
320
        'The groupmode param deprecated. Please use the group mode actions in course/format/update.php instead.',
321
        DEBUG_DEVELOPER
322
    );
1 efrain 323
    $id = required_param('id', PARAM_INT);
324
 
325
    $cm     = get_coursemodule_from_id('', $id, 0, true, MUST_EXIST);
326
    $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
327
 
328
    require_login($course, false, $cm);
329
    $coursecontext = context_course::instance($course->id);
330
    $modcontext = context_module::instance($cm->id);
331
    require_capability('moodle/course:manageactivities', $modcontext);
332
 
333
    set_coursemodule_groupmode($cm->id, $groupmode);
334
    \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
335
    redirect(course_get_url($course, $cm->sectionnum, $urloptions));
336
 
337
} else if (!empty($copy) and confirm_sesskey()) { // value = course module
1441 ariadna 338
    // TODO remove this else if as part of MDL-83530.
339
    debugging(
340
        'The copy param is deprecated. Please use the standard move modal instead.',
341
        DEBUG_DEVELOPER
342
    );
1 efrain 343
    $cm     = get_coursemodule_from_id('', $copy, 0, true, MUST_EXIST);
344
    $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
345
 
346
    require_login($course, false, $cm);
347
    $coursecontext = context_course::instance($course->id);
348
    $modcontext = context_module::instance($cm->id);
349
    require_capability('moodle/course:manageactivities', $modcontext);
350
 
351
    $section = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
352
 
353
    $USER->activitycopy              = $copy;
354
    $USER->activitycopycourse        = $cm->course;
355
    $USER->activitycopyname          = $cm->name;
356
    $USER->activitycopysectionreturn = $sectionreturn;
357
 
358
    redirect(course_get_url($course, $section->section, $urloptions));
359
 
360
} else if (!empty($cancelcopy) and confirm_sesskey()) { // value = course module
1441 ariadna 361
    // TODO remove this else if as part of MDL-83530.
362
    debugging(
363
        'The copy param is deprecated. Please use the standard move modal instead.',
364
        DEBUG_DEVELOPER
365
    );
1 efrain 366
    $courseid = $USER->activitycopycourse;
367
    $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
368
 
369
    $cm     = get_coursemodule_from_id('', $USER->activitycopy, 0, true, IGNORE_MISSING);
370
    $sectionreturn = $USER->activitycopysectionreturn;
371
    unset($USER->activitycopy);
372
    unset($USER->activitycopycourse);
373
    unset($USER->activitycopyname);
374
    unset($USER->activitycopysectionreturn);
375
    redirect(course_get_url($course, $cm->sectionnum, $urloptions));
376
} else {
377
    throw new \moodle_exception('unknowaction');
378
}