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
// 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/>.
16
 
17
/**
18
 * Library of functions specific to course/modedit.php and course API functions.
19
 * The course API function calling them are course/lib.php:create_module() and update_module().
20
 * This file has been created has an alternative solution to a full refactor of course/modedit.php
21
 * in order to create the course API functions.
22
 *
23
 * @copyright 2013 Jerome Mouneyrac
24
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @package core_course
26
 */
27
 
28
defined('MOODLE_INTERNAL') || die;
29
 
30
use core\di;
31
use core\hook;
32
use core_courseformat\formatactions;
33
use core_grades\component_gradeitems;
34
 
35
require_once($CFG->dirroot.'/course/lib.php');
36
 
37
/**
38
 * Add course module.
39
 *
40
 * The function does not check user capabilities.
41
 * The function creates course module, module instance, add the module to the correct section.
42
 * It also trigger common action that need to be done after adding/updating a module.
43
 *
44
 * @param object $moduleinfo the moudle data
45
 * @param object $course the course of the module
46
 * @param object $mform this is required by an existing hack to deal with files during MODULENAME_add_instance()
47
 * @return object the updated module info
48
 */
49
function add_moduleinfo($moduleinfo, $course, $mform = null) {
50
    global $DB, $CFG;
51
 
52
    // Attempt to include module library before we make any changes to DB.
53
    include_modulelib($moduleinfo->modulename);
54
 
55
    $moduleinfo->course = $course->id;
56
    $moduleinfo = set_moduleinfo_defaults($moduleinfo);
57
 
58
    if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
59
        $moduleinfo->groupmode = 0; // Do not set groupmode.
60
    }
61
 
62
    // First add course_module record because we need the context.
63
    $newcm = new stdClass();
64
    $newcm->course           = $course->id;
65
    $newcm->module           = $moduleinfo->module;
66
    $newcm->instance         = 0; // Not known yet, will be updated later (this is similar to restore code).
67
    $newcm->visible          = $moduleinfo->visible;
68
    $newcm->visibleold       = $moduleinfo->visible;
69
    $newcm->visibleoncoursepage = $moduleinfo->visibleoncoursepage;
70
    if (isset($moduleinfo->cmidnumber)) {
71
        $newcm->idnumber         = $moduleinfo->cmidnumber;
72
    }
73
    if (isset($moduleinfo->downloadcontent)) {
74
        $newcm->downloadcontent = $moduleinfo->downloadcontent;
75
    }
76
    if (has_capability('moodle/course:setforcedlanguage', context_course::instance($course->id))) {
77
        $newcm->lang = $moduleinfo->lang ?? null;
78
    } else {
79
        $newcm->lang = null;
80
    }
81
    $newcm->groupmode        = $moduleinfo->groupmode;
82
    $newcm->groupingid       = $moduleinfo->groupingid;
83
    $completion = new completion_info($course);
84
    if ($completion->is_enabled()) {
85
        $newcm->completion                = $moduleinfo->completion;
86
        $newcm->completionpassgrade       = $moduleinfo->completionpassgrade ?? 0;
87
        if ($moduleinfo->completiongradeitemnumber === '') {
88
            $newcm->completiongradeitemnumber = null;
89
        } else {
90
            $newcm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
91
        }
92
        $newcm->completionview            = $moduleinfo->completionview;
93
        $newcm->completionexpected        = $moduleinfo->completionexpected;
94
    }
95
    if(!empty($CFG->enableavailability)) {
96
        // This code is used both when submitting the form, which uses a long
97
        // name to avoid clashes, and by unit test code which uses the real
98
        // name in the table.
99
        $newcm->availability = null;
100
        if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
101
            if ($moduleinfo->availabilityconditionsjson !== '') {
102
                $newcm->availability = $moduleinfo->availabilityconditionsjson;
103
            }
104
        } else if (property_exists($moduleinfo, 'availability')) {
105
            $newcm->availability = $moduleinfo->availability;
106
        }
107
        // If there is any availability data, verify it.
108
        if ($newcm->availability) {
109
            $tree = new \core_availability\tree(json_decode($newcm->availability));
110
            // Save time and database space by setting null if the only data
111
            // is an empty tree.
112
            if ($tree->is_empty()) {
113
                $newcm->availability = null;
114
            }
115
        }
116
    }
117
    if (isset($moduleinfo->showdescription)) {
118
        $newcm->showdescription = $moduleinfo->showdescription;
119
    } else {
120
        $newcm->showdescription = 0;
121
    }
122
    if (empty($moduleinfo->beforemod)) {
123
        $moduleinfo->beforemod = null;
124
    }
125
 
126
    // From this point we make database changes, so start transaction.
127
    $transaction = $DB->start_delegated_transaction();
128
 
129
    if (!$moduleinfo->coursemodule = add_course_module($newcm)) {
130
        throw new \moodle_exception('cannotaddcoursemodule');
131
    }
132
 
133
    if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true) &&
134
            isset($moduleinfo->introeditor)) {
135
        $introeditor = $moduleinfo->introeditor;
136
        unset($moduleinfo->introeditor);
137
        $moduleinfo->intro       = $introeditor['text'];
138
        $moduleinfo->introformat = $introeditor['format'];
139
    }
140
 
141
    $addinstancefunction    = $moduleinfo->modulename."_add_instance";
142
    try {
143
        $returnfromfunc = $addinstancefunction($moduleinfo, $mform);
144
    } catch (moodle_exception $e) {
145
        $returnfromfunc = $e;
146
    }
147
    if (!$returnfromfunc or !is_number($returnfromfunc)) {
148
        // Undo everything we can. This is not necessary for databases which
149
        // support transactions, but improves consistency for other databases.
150
        context_helper::delete_instance(CONTEXT_MODULE, $moduleinfo->coursemodule);
151
        $DB->delete_records('course_modules', array('id'=>$moduleinfo->coursemodule));
152
 
153
        if ($returnfromfunc instanceof moodle_exception) {
154
            throw $returnfromfunc;
155
        } else if (!is_number($returnfromfunc)) {
156
            throw new \moodle_exception('invalidfunction', '', course_get_url($course, $moduleinfo->section));
157
        } else {
158
            throw new \moodle_exception('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->section),
159
                $moduleinfo->modulename);
160
        }
161
    }
162
 
163
    $moduleinfo->instance = $returnfromfunc;
164
 
165
    $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$moduleinfo->coursemodule));
166
 
167
    // Update embedded links and save files.
168
    $modcontext = context_module::instance($moduleinfo->coursemodule);
169
    if (!empty($introeditor)) {
170
        // This will respect a module that has set a value for intro in it's modname_add_instance() function.
171
        $introeditor['text'] = $moduleinfo->intro;
172
 
173
        $moduleinfo->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id,
174
                                                      'mod_'.$moduleinfo->modulename, 'intro', 0,
175
                                                      array('subdirs'=>true), $introeditor['text']);
176
        $DB->set_field($moduleinfo->modulename, 'intro', $moduleinfo->intro, array('id'=>$moduleinfo->instance));
177
    }
178
 
179
    // Add module tags.
180
    if (core_tag_tag::is_enabled('core', 'course_modules') && isset($moduleinfo->tags)) {
181
        core_tag_tag::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule, $modcontext, $moduleinfo->tags);
182
    }
183
 
184
    // Course_modules and course_sections each contain a reference to each other.
185
    // So we have to update one of them twice.
1441 ariadna 186
    $sectionid = course_add_cm_to_section(
187
        $course,
188
        $moduleinfo->coursemodule,
189
        $moduleinfo->section,
190
        $moduleinfo->beforemod,
191
        $moduleinfo->modulename
192
    );
1 efrain 193
 
194
    // Trigger event based on the action we did.
195
    // Api create_from_cm expects modname and id property, and we don't want to modify $moduleinfo since we are returning it.
196
    $eventdata = clone $moduleinfo;
197
    $eventdata->modname = $eventdata->modulename;
198
    $eventdata->id = $eventdata->coursemodule;
199
    $event = \core\event\course_module_created::create_from_cm($eventdata, $modcontext);
200
    $event->trigger();
201
 
202
    $moduleinfo = edit_module_post_actions($moduleinfo, $course);
203
    $transaction->allow_commit();
204
 
205
    return $moduleinfo;
206
}
207
 
208
/**
209
 * Hook for plugins to take action when a module is created or updated.
210
 *
211
 * @param stdClass $moduleinfo the module info
212
 * @param stdClass $course the course of the module
213
 *
214
 * @return stdClass moduleinfo updated by plugins.
215
 */
216
function plugin_extend_coursemodule_edit_post_actions($moduleinfo, $course) {
217
    $callbacks = get_plugins_with_function('coursemodule_edit_post_actions', 'lib.php');
218
    foreach ($callbacks as $type => $plugins) {
219
        foreach ($plugins as $plugin => $pluginfunction) {
220
            $moduleinfo = $pluginfunction($moduleinfo, $course);
221
        }
222
    }
223
    return $moduleinfo;
224
}
225
 
226
/**
227
 * Common create/update module module actions that need to be processed as soon as a module is created/updaded.
228
 * For example:create grade parent category, add outcomes, rebuild caches, regrade, save plagiarism settings...
229
 * Please note this api does not trigger events as of MOODLE 2.6. Please trigger events before calling this api.
230
 *
231
 * @param object $moduleinfo the module info
232
 * @param object $course the course of the module
233
 *
234
 * @return object moduleinfo update with grading management info
235
 */
236
function edit_module_post_actions($moduleinfo, $course) {
237
    global $CFG, $USER;
238
    require_once($CFG->libdir.'/gradelib.php');
239
 
240
    $modcontext = context_module::instance($moduleinfo->coursemodule);
241
    $hasgrades = plugin_supports('mod', $moduleinfo->modulename, FEATURE_GRADE_HAS_GRADE, false);
242
    $hasoutcomes = plugin_supports('mod', $moduleinfo->modulename, FEATURE_GRADE_OUTCOMES, true);
243
 
244
    $items = grade_item::fetch_all([
245
        'itemtype' => 'mod',
246
        'itemmodule' => $moduleinfo->modulename,
247
        'iteminstance' => $moduleinfo->instance,
248
        'courseid' => $course->id,
249
    ]);
250
 
251
    // Create parent category if requested and move to correct parent category.
252
    $component = "mod_{$moduleinfo->modulename}";
253
    if ($items) {
254
        foreach ($items as $item) {
255
            $update = false;
256
 
257
            // Sync idnumber with grade_item.
258
            // Note: This only happens for itemnumber 0 at this time.
259
            if ($item->itemnumber == 0 && ($item->idnumber != $moduleinfo->cmidnumber)) {
260
                $item->idnumber = $moduleinfo->cmidnumber;
261
                $update = true;
262
            }
263
 
264
            // Determine the grade category.
265
            $gradecatfieldname = component_gradeitems::get_field_name_for_itemnumber($component, $item->itemnumber, 'gradecat');
266
            if (property_exists($moduleinfo, $gradecatfieldname)) {
267
                $gradecat = $moduleinfo->$gradecatfieldname;
268
                if ($gradecat == -1) {
269
                    $gradecategory = new grade_category();
270
                    $gradecategory->courseid = $course->id;
271
                    $gradecategory->fullname = $moduleinfo->name;
272
                    $gradecategory->insert();
273
 
274
                    $parent = $item->get_parent_category();
275
                    $gradecategory->set_parent($parent->id);
276
                    $gradecat = $gradecategory->id;
277
                }
278
 
279
                $oldgradecat = null;
280
                if ($parent = $item->get_parent_category()) {
281
                    $oldgradecat = $parent->id;
282
                }
283
                if ($oldgradecat != $gradecat) {
284
                    $item->set_parent($gradecat);
285
                    $update = true;
286
                }
287
            }
288
 
289
            // Determine the gradepass.
290
            $gradepassfieldname = component_gradeitems::get_field_name_for_itemnumber($component, $item->itemnumber, 'gradepass');
291
            if (isset($moduleinfo->{$gradepassfieldname})) {
292
                $gradepass = $moduleinfo->{$gradepassfieldname};
293
                if (null !== $gradepass && $gradepass != $item->gradepass) {
294
                    $item->gradepass = $gradepass;
295
                    $update = true;
296
                }
297
            }
298
 
299
            if ($update) {
300
                $item->update();
301
            }
302
 
303
            if (!empty($moduleinfo->add)) {
304
                $gradecategory = $item->get_parent_category();
305
                if ($item->set_aggregation_fields_for_aggregation(0, $gradecategory->aggregation)) {
306
                    $item->update();
307
                }
308
            }
309
        }
310
    }
311
 
312
    require_once($CFG->libdir.'/grade/grade_outcome.php');
313
    // Add outcomes if requested.
314
    if ($hasoutcomes && $outcomes = grade_outcome::fetch_all_available($course->id)) {
315
        // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes.
316
        $max_itemnumber = 999;
317
        if ($items) {
318
            foreach($items as $item) {
319
                if ($item->itemnumber > $max_itemnumber) {
320
                    $max_itemnumber = $item->itemnumber;
321
                }
322
            }
323
        }
324
 
325
        foreach($outcomes as $outcome) {
326
            $elname = 'outcome_'.$outcome->id;
327
 
328
            if (property_exists($moduleinfo, $elname) and $moduleinfo->$elname) {
329
                // Check if this is a new outcome grade item.
330
                $outcomeexists = false;
331
                if ($items) {
332
                    foreach($items as $item) {
333
                        if ($item->outcomeid == $outcome->id) {
334
                            $outcomeexists = true;
335
                            break;
336
                        }
337
                    }
338
                    if ($outcomeexists) {
339
                        continue;
340
                    }
341
                }
342
 
343
                $max_itemnumber++;
344
 
345
                $outcomeitem = new grade_item();
346
                $outcomeitem->courseid     = $course->id;
347
                $outcomeitem->itemtype     = 'mod';
348
                $outcomeitem->itemmodule   = $moduleinfo->modulename;
349
                $outcomeitem->iteminstance = $moduleinfo->instance;
350
                $outcomeitem->itemnumber   = $max_itemnumber;
351
                $outcomeitem->itemname     = $outcome->fullname;
352
                $outcomeitem->outcomeid    = $outcome->id;
353
                $outcomeitem->gradetype    = GRADE_TYPE_SCALE;
354
                $outcomeitem->scaleid      = $outcome->scaleid;
355
                $outcomeitem->insert();
356
 
357
                if ($items) {
358
                    // Move the new outcome into the same category and immediately after the first grade item.
359
                    $item = reset($items);
360
                    $outcomeitem->set_parent($item->categoryid);
361
                    $outcomeitem->move_after_sortorder($item->sortorder);
362
                } else if (isset($moduleinfo->gradecat)) {
363
                    $outcomeitem->set_parent($moduleinfo->gradecat);
364
                }
365
 
366
                if (!$outcomeexists) {
367
                    $gradecategory = $outcomeitem->get_parent_category();
368
                    if ($outcomeitem->set_aggregation_fields_for_aggregation(0, $gradecategory->aggregation)) {
369
                        $outcomeitem->update();
370
                    }
371
                }
372
            }
373
        }
374
    }
375
 
376
    if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_ADVANCED_GRADING, false)
377
            and has_capability('moodle/grade:managegradingforms', $modcontext)) {
378
        require_once($CFG->dirroot.'/grade/grading/lib.php');
379
        $gradingman = get_grading_manager($modcontext, 'mod_'.$moduleinfo->modulename);
380
        $showgradingmanagement = false;
381
        foreach ($gradingman->get_available_areas() as $areaname => $aretitle) {
382
            $formfield = 'advancedgradingmethod_'.$areaname;
383
            if (isset($moduleinfo->{$formfield})) {
384
                $gradingman->set_area($areaname);
385
                $methodchanged = $gradingman->set_active_method($moduleinfo->{$formfield});
386
                if (empty($moduleinfo->{$formfield})) {
387
                    // Going back to the simple direct grading is not a reason to open the management screen.
388
                    $methodchanged = false;
389
                }
390
                $showgradingmanagement = $showgradingmanagement || $methodchanged;
391
            }
392
        }
393
        // Update grading management information.
394
        $moduleinfo->gradingman = $gradingman;
395
        $moduleinfo->showgradingmanagement = $showgradingmanagement;
396
    }
397
 
398
    \course_modinfo::purge_course_module_cache($course->id, $moduleinfo->coursemodule);
399
    rebuild_course_cache($course->id, true, true);
400
 
401
    if ($hasgrades) {
402
        // If regrading will be slow, and this is happening in response to front-end UI...
403
        if (!empty($moduleinfo->frontend) && grade_needs_regrade_progress_bar($course->id)) {
404
            // And if it actually needs regrading...
405
            $courseitem = grade_item::fetch_course_item($course->id);
406
            if ($courseitem->needsupdate) {
1441 ariadna 407
                // Queue an asynchronous regrade.
408
                grade_regrade_final_grades($course->id, async: true);
1 efrain 409
            }
410
        } else {
411
            // Regrade now.
1441 ariadna 412
            $result = grade_regrade_final_grades($course->id);
413
            if (is_array($result)) {
414
                foreach ($result as $error) {
415
                    \core\notification::add($error, \core\output\notification::NOTIFY_ERROR);
416
                }
417
            }
1 efrain 418
        }
419
    }
420
 
421
    // Allow plugins to extend the course module form.
422
    $moduleinfo = plugin_extend_coursemodule_edit_post_actions($moduleinfo, $course);
423
 
424
    if (!empty($moduleinfo->coursecontentnotification)) {
425
        // Schedule adhoc-task for delivering the course content updated notification.
426
        if ($course->visible && $moduleinfo->visible) {
427
            $adhocktask = new \core_course\task\content_notification_task();
428
            $adhocktask->set_custom_data(
429
                ['update' => $moduleinfo->update, 'cmid' => $moduleinfo->coursemodule,
430
                'courseid' => $course->id, 'userfrom' => $USER->id]);
431
            $adhocktask->set_component('course');
432
            \core\task\manager::queue_adhoc_task($adhocktask, true);
433
        }
434
    }
435
 
1441 ariadna 436
    if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_USES_QUESTIONS) && empty($moduleinfo->skipdefaultcategory)) {
437
        // Create the default question category.
438
        question_get_default_category($modcontext->id, true);
439
    }
440
 
1 efrain 441
    return $moduleinfo;
442
}
443
 
444
/**
445
 * Set module info default values for the unset module attributs.
446
 *
447
 * @param object $moduleinfo the current known data of the module
448
 * @return object the completed module info
449
 */
450
function set_moduleinfo_defaults($moduleinfo) {
451
 
452
    if (empty($moduleinfo->coursemodule)) {
453
        // Add.
454
        $cm = null;
455
        $moduleinfo->instance     = '';
456
        $moduleinfo->coursemodule = '';
457
    } else {
458
        // Update.
459
        $cm = get_coursemodule_from_id('', $moduleinfo->coursemodule, 0, false, MUST_EXIST);
460
        $moduleinfo->instance     = $cm->instance;
461
        $moduleinfo->coursemodule = $cm->id;
462
    }
463
    // For safety.
464
    $moduleinfo->modulename = clean_param($moduleinfo->modulename, PARAM_PLUGIN);
465
 
466
    if (!isset($moduleinfo->groupingid)) {
467
        $moduleinfo->groupingid = 0;
468
    }
469
 
470
    if (!isset($moduleinfo->name)) { // Label.
471
        $moduleinfo->name = $moduleinfo->modulename;
472
    }
473
 
474
    if (!isset($moduleinfo->completion)) {
475
        $moduleinfo->completion = COMPLETION_DISABLED;
476
    }
477
    if (!isset($moduleinfo->completionview)) {
478
        $moduleinfo->completionview = COMPLETION_VIEW_NOT_REQUIRED;
479
    }
480
    if (!isset($moduleinfo->completionexpected)) {
481
        $moduleinfo->completionexpected = 0;
482
    }
483
 
484
    // Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
485
    if (isset($moduleinfo->completionusegrade) &&
486
        $moduleinfo->completionusegrade &&
487
        !isset($moduleinfo->completiongradeitemnumber
488
        )) {
489
        $moduleinfo->completiongradeitemnumber = 0;
490
    } else if (!isset($moduleinfo->completiongradeitemnumber)) {
491
        // If there is no gradeitemnumber set, make sure to disable completionpassgrade.
492
        $moduleinfo->completionpassgrade = 0;
493
        $moduleinfo->completiongradeitemnumber = null;
494
    }
495
 
496
    if (!isset($moduleinfo->conditiongradegroup)) {
497
        $moduleinfo->conditiongradegroup = array();
498
    }
499
    if (!isset($moduleinfo->conditionfieldgroup)) {
500
        $moduleinfo->conditionfieldgroup = array();
501
    }
502
    if (!isset($moduleinfo->visibleoncoursepage)) {
503
        $moduleinfo->visibleoncoursepage = 1;
504
    }
505
 
506
    if (!isset($moduleinfo->downloadcontent)) {
507
        $moduleinfo->downloadcontent = DOWNLOAD_COURSE_CONTENT_ENABLED;
508
    }
509
 
1441 ariadna 510
    // Module types with this flag set to false must always be in section number 0.
511
    if (!course_modinfo::is_mod_type_visible_on_course($moduleinfo->modulename)) {
512
        $moduleinfo->section = 0;
513
    }
514
 
1 efrain 515
    return $moduleinfo;
516
}
517
 
518
/**
519
 * Check that the user can add a module. Also returns some information like the module, context and course section info.
520
 * The fucntion create the course section if it doesn't exist.
521
 *
522
 * @param stdClass $course the course of the module
523
 * @param string $modulename the module name
524
 * @param int $sectionnum the section of the module
525
 * @return array list containing module, context, course section.
526
 * @throws moodle_exception if user is not allowed to perform the action or module is not allowed in this course
527
 */
528
function can_add_moduleinfo($course, $modulename, $sectionnum) {
529
    global $DB;
530
 
531
    $module = $DB->get_record('modules', ['name' => $modulename], '*', MUST_EXIST);
532
 
533
    $context = context_course::instance($course->id);
534
    require_capability('moodle/course:manageactivities', $context);
535
 
536
    // If the $sectionnum is a delegated section, we cannot execute create_if_missing
537
    // because it only works to create regular sections. To prevent that from happening, we
538
    // check if the section is already there, no matter if it is delegated or not.
539
    $sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum);
540
    if (!$sectioninfo) {
541
        formatactions::section($course)->create_if_missing([$sectionnum]);
542
        $sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum);
543
    }
544
 
545
    if (!course_allowed_module($course, $module->name)) {
546
        throw new \moodle_exception('moduledisable');
547
    }
548
 
549
    return [$module, $context, $sectioninfo];
550
}
551
 
552
/**
553
 * Check if user is allowed to update module info and returns related item/data to the module.
554
 *
555
 * @param object $cm course module
556
 * @return array - list of course module, context, module, moduleinfo, and course section.
557
 * @throws moodle_exception if user is not allowed to perform the action
558
 */
559
function can_update_moduleinfo($cm) {
560
    global $DB;
561
 
562
    // Check the $USER has the right capability.
563
    $context = context_module::instance($cm->id);
564
    require_capability('moodle/course:manageactivities', $context);
565
 
566
    // Check module exists.
567
    $module = $DB->get_record('modules', array('id'=>$cm->module), '*', MUST_EXIST);
568
 
569
    // Check the moduleinfo exists.
570
    $data = $DB->get_record($module->name, array('id'=>$cm->instance), '*', MUST_EXIST);
571
 
572
    // Check the course section exists.
573
    $cw = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
574
 
575
    return array($cm, $context, $module, $data, $cw);
576
}
577
 
578
 
579
/**
580
 * Update the module info.
581
 * This function doesn't check the user capabilities. It updates the course module and the module instance.
582
 * Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...).
583
 *
584
 * @param object $cm course module
585
 * @param object $moduleinfo module info
586
 * @param object $course course of the module
587
 * @param object $mform - the mform is required by some specific module in the function MODULE_update_instance(). This is due to a hack in this function.
588
 * @return array list of course module and module info.
589
 */
590
function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
591
    global $DB, $CFG;
592
 
593
    $data = new stdClass();
594
    if ($mform) {
595
        $data = $mform->get_data();
596
    }
597
 
598
    // Attempt to include module library before we make any changes to DB.
599
    include_modulelib($moduleinfo->modulename);
600
 
601
    $moduleinfo->course = $course->id;
602
    $moduleinfo = set_moduleinfo_defaults($moduleinfo);
603
 
604
    $modcontext = context_module::instance($moduleinfo->coursemodule);
605
    if (has_capability('moodle/course:setforcedlanguage', $modcontext)) {
606
        $cm->lang = $moduleinfo->lang ?? null;
607
    } else {
608
        unset($cm->lang);
609
    }
610
 
611
    if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
612
        $moduleinfo->groupmode = $cm->groupmode; // Keep original.
613
    }
614
 
615
    // Update course module first.
616
    $cm->groupmode = $moduleinfo->groupmode;
617
    if (isset($moduleinfo->groupingid)) {
618
        $cm->groupingid = $moduleinfo->groupingid;
619
    }
620
 
621
    $completion = new completion_info($course);
622
    if ($completion->is_enabled()) {
623
        // Completion settings that would affect users who have already completed
624
        // the activity may be locked; if so, these should not be updated.
625
        if (!empty($moduleinfo->completionunlocked)) {
626
            $cm->completion = $moduleinfo->completion;
627
            $cm->completionpassgrade = $moduleinfo->completionpassgrade ?? 0;
628
            if ($moduleinfo->completiongradeitemnumber === '') {
629
                $cm->completiongradeitemnumber = null;
630
            } else {
631
                $cm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
632
            }
633
            $cm->completionview = $moduleinfo->completionview;
634
        }
635
        // The expected date does not affect users who have completed the activity,
636
        // so it is safe to update it regardless of the lock status.
637
        $cm->completionexpected = $moduleinfo->completionexpected;
638
    }
639
    if (!empty($CFG->enableavailability)) {
640
        // This code is used both when submitting the form, which uses a long
641
        // name to avoid clashes, and by unit test code which uses the real
642
        // name in the table.
1441 ariadna 643
        $newavailability = $cm->availability;
1 efrain 644
        if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
645
            if ($moduleinfo->availabilityconditionsjson !== '') {
1441 ariadna 646
                $newavailability = $moduleinfo->availabilityconditionsjson;
1 efrain 647
            } else {
1441 ariadna 648
                $newavailability = null;
1 efrain 649
            }
650
        } else if (property_exists($moduleinfo, 'availability')) {
1441 ariadna 651
            $newavailability = $moduleinfo->availability;
1 efrain 652
        }
1441 ariadna 653
        if ($cm->availability != $newavailability) {
654
            $cm->availability = $newavailability;
655
        }
1 efrain 656
        // If there is any availability data, verify it.
657
        if ($cm->availability) {
658
            $tree = new \core_availability\tree(json_decode($cm->availability));
659
            // Save time and database space by setting null if the only data
660
            // is an empty tree.
661
            if ($tree->is_empty()) {
662
                $cm->availability = null;
663
            }
664
        }
665
    }
666
    if (isset($moduleinfo->showdescription)) {
667
        $cm->showdescription = $moduleinfo->showdescription;
668
    } else {
669
        $cm->showdescription = 0;
670
    }
671
 
672
    $DB->update_record('course_modules', $cm);
673
 
674
    // Update embedded links and save files.
675
    if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
676
        $moduleinfo->intro = file_save_draft_area_files($moduleinfo->introeditor['itemid'], $modcontext->id,
677
                                                      'mod_'.$moduleinfo->modulename, 'intro', 0,
678
                                                      array('subdirs'=>true), $moduleinfo->introeditor['text']);
679
        $moduleinfo->introformat = $moduleinfo->introeditor['format'];
680
        unset($moduleinfo->introeditor);
681
    }
682
    // Get the a copy of the grade_item before it is modified incase we need to scale the grades.
683
    $oldgradeitem = null;
684
    $newgradeitem = null;
685
    if (!empty($data->grade_rescalegrades) && $data->grade_rescalegrades == 'yes') {
686
        // Fetch the grade item before it is updated.
687
        $oldgradeitem = grade_item::fetch(array('itemtype' => 'mod',
688
                                                'itemmodule' => $moduleinfo->modulename,
689
                                                'iteminstance' => $moduleinfo->instance,
690
                                                'itemnumber' => 0,
691
                                                'courseid' => $moduleinfo->course));
692
    }
693
 
694
    $updateinstancefunction = $moduleinfo->modulename."_update_instance";
695
    if (!$updateinstancefunction($moduleinfo, $mform)) {
696
        throw new \moodle_exception('cannotupdatemod', '', course_get_url($course, $cm->section), $moduleinfo->modulename);
697
    }
698
 
699
    // This needs to happen AFTER the grademin/grademax have already been updated.
700
    if (!empty($data->grade_rescalegrades) && $data->grade_rescalegrades == 'yes') {
701
        // Get the grade_item after the update call the activity to scale the grades.
702
        $newgradeitem = grade_item::fetch(array('itemtype' => 'mod',
703
                                                'itemmodule' => $moduleinfo->modulename,
704
                                                'iteminstance' => $moduleinfo->instance,
705
                                                'itemnumber' => 0,
706
                                                'courseid' => $moduleinfo->course));
707
        if ($newgradeitem && $oldgradeitem->gradetype == GRADE_TYPE_VALUE && $newgradeitem->gradetype == GRADE_TYPE_VALUE) {
708
            $params = array(
709
                $course,
710
                $cm,
711
                $oldgradeitem->grademin,
712
                $oldgradeitem->grademax,
713
                $newgradeitem->grademin,
714
                $newgradeitem->grademax
715
            );
716
            if (!component_callback('mod_' . $moduleinfo->modulename, 'rescale_activity_grades', $params)) {
717
                throw new \moodle_exception('cannotreprocessgrades', '', course_get_url($course, $cm->section),
718
                    $moduleinfo->modulename);
719
            }
720
        }
721
    }
722
 
723
    // Make sure visibility is set correctly (in particular in calendar).
724
    if (has_capability('moodle/course:activityvisibility', $modcontext)) {
725
        set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible, $moduleinfo->visibleoncoursepage);
726
    }
727
 
728
    if (isset($moduleinfo->cmidnumber)) { // Label.
729
        // Set cm idnumber - uniqueness is already verified by form validation.
730
        set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber);
731
    }
732
 
733
    if (isset($moduleinfo->downloadcontent)) {
734
        set_downloadcontent($moduleinfo->coursemodule, $moduleinfo->downloadcontent);
735
    }
736
 
737
    // Update module tags.
738
    if (core_tag_tag::is_enabled('core', 'course_modules') && isset($moduleinfo->tags)) {
739
        core_tag_tag::set_item_tags('core', 'course_modules', $moduleinfo->coursemodule, $modcontext, $moduleinfo->tags);
740
    }
741
    $moduleinfo = edit_module_post_actions($moduleinfo, $course);
742
 
743
    // Now that module is fully updated, also update completion data if required.
744
    // (this will wipe all user completion data and recalculate it)
745
    if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) {
746
        // Rebuild course cache before resetting completion states to ensure that the cm_info attributes are up to date.
747
        course_modinfo::build_course_cache($course);
748
        // Fetch this course module's info.
749
        $cminfo = cm_info::create($cm);
750
        $completion->reset_all_state($cminfo);
751
    }
752
 
753
    if ($cm->name != $moduleinfo->name) {
754
        di::get(hook\manager::class)->dispatch(
755
            new \core_courseformat\hook\after_cm_name_edited(
756
                get_fast_modinfo($course)->get_cm($cm->id),
757
                $moduleinfo->name
758
            ),
759
        );
760
    }
761
 
762
    $cm->name = $moduleinfo->name;
763
    \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
764
 
765
    return array($cm, $moduleinfo);
766
}
767
 
768
/**
769
 * Include once the module lib file.
770
 *
771
 * @param string $modulename module name of the lib to include
772
 * @throws moodle_exception if lib.php file for the module does not exist
773
 */
774
function include_modulelib($modulename) {
775
    global $CFG;
776
    $modlib = "$CFG->dirroot/mod/$modulename/lib.php";
777
    if (file_exists($modlib)) {
778
        include_once($modlib);
779
    } else {
780
        throw new moodle_exception('modulemissingcode', '', '', $modlib);
781
    }
782
}
783
 
784
/**
785
 * Get module information data required for updating the module.
786
 *
787
 * @param  stdClass $cm     course module object
788
 * @param  stdClass $course course object
789
 * @return array required data for updating a module
790
 * @since  Moodle 3.2
791
 */
792
function get_moduleinfo_data($cm, $course) {
793
    global $CFG;
794
    require_once($CFG->libdir . '/gradelib.php');
795
 
796
    list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
797
 
798
    $data->coursemodule       = $cm->id;
799
    $data->section            = $cw->section;  // The section number itself - relative!!! (section column in course_sections)
800
    $data->visible            = $cm->visible; //??  $cw->visible ? $cm->visible : 0; // section hiding overrides
801
    $data->visibleoncoursepage = $cm->visibleoncoursepage;
802
    $data->cmidnumber         = $cm->idnumber;          // The cm IDnumber
803
    $data->groupmode          = groups_get_activity_groupmode($cm); // locked later if forced
804
    $data->groupingid         = $cm->groupingid;
805
    $data->course             = $course->id;
806
    $data->module             = $module->id;
807
    $data->modulename         = $module->name;
808
    $data->instance           = $cm->instance;
809
    $data->completion         = $cm->completion;
810
    $data->completionview     = $cm->completionview;
811
    $data->completionexpected = $cm->completionexpected;
812
    $data->completionusegrade = is_null($cm->completiongradeitemnumber) ? 0 : 1;
813
    $data->completionpassgrade = $cm->completionpassgrade;
814
    $data->completiongradeitemnumber = $cm->completiongradeitemnumber;
815
    $data->showdescription    = $cm->showdescription;
816
    $data->downloadcontent    = $cm->downloadcontent;
817
    $data->lang               = $cm->lang;
818
    $data->tags               = core_tag_tag::get_item_tags_array('core', 'course_modules', $cm->id);
819
    if (!empty($CFG->enableavailability)) {
820
        $data->availabilityconditionsjson = $cm->availability;
821
    }
822
 
823
    if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
824
        $draftid_editor = file_get_submitted_draft_itemid('introeditor');
825
        $currentintro = file_prepare_draft_area($draftid_editor, $context->id, 'mod_'.$data->modulename, 'intro', 0, array('subdirs'=>true), $data->intro);
826
        $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor);
827
    }
828
 
829
    if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
830
            and has_capability('moodle/grade:managegradingforms', $context)) {
831
        require_once($CFG->dirroot.'/grade/grading/lib.php');
832
        $gradingman = get_grading_manager($context, 'mod_'.$data->modulename);
833
        $data->_advancedgradingdata['methods'] = $gradingman->get_available_methods();
834
        $areas = $gradingman->get_available_areas();
835
 
836
        foreach ($areas as $areaname => $areatitle) {
837
            $gradingman->set_area($areaname);
838
            $method = $gradingman->get_active_method();
839
            $data->_advancedgradingdata['areas'][$areaname] = array(
840
                'title'  => $areatitle,
841
                'method' => $method,
842
            );
843
            $formfield = 'advancedgradingmethod_'.$areaname;
844
            $data->{$formfield} = $method;
845
        }
846
    }
847
 
848
    $component = "mod_{$data->modulename}";
849
    $items = grade_item::fetch_all([
850
        'itemtype' => 'mod',
851
        'itemmodule' => $data->modulename,
852
        'iteminstance' => $data->instance,
853
        'courseid' => $course->id,
854
    ]);
855
 
856
    if ($items) {
857
        // Add existing outcomes.
858
        foreach ($items as $item) {
859
            if (!empty($item->outcomeid)) {
860
                $data->{'outcome_' . $item->outcomeid} = 1;
861
            } else if (isset($item->gradepass)) {
862
                $gradepassfieldname = component_gradeitems::get_field_name_for_itemnumber($component, $item->itemnumber, 'gradepass');
863
                $data->{$gradepassfieldname} = format_float($item->gradepass, $item->get_decimals());
864
            }
865
 
866
        }
867
 
868
        // set category if present
869
        $gradecat = [];
870
        foreach ($items as $item) {
871
            if (!isset($gradecat[$item->itemnumber])) {
872
                $gradecat[$item->itemnumber] = $item->categoryid;
873
            }
874
            if ($gradecat[$item->itemnumber] != $item->categoryid) {
875
                // Mixed categories.
876
                $gradecat[$item->itemnumber] = false;
877
            }
878
        }
879
        foreach ($gradecat as $itemnumber => $cat) {
880
            if ($cat !== false) {
881
                $gradecatfieldname = component_gradeitems::get_field_name_for_itemnumber($component, $itemnumber, 'gradecat');
882
                // Do not set if mixed categories present.
883
                $data->{$gradecatfieldname} = $cat;
884
            }
885
        }
886
    }
887
    return array($cm, $context, $module, $data, $cw);
888
}
889
 
890
/**
891
 * Prepare the standard module information for a new module instance.
892
 *
893
 * @param  stdClass $course  course object
894
 * @param  string $modulename  module name
895
 * @param  int $section section number
896
 * @param  string $suffix the suffix to add to the name of the completion rules.
897
 * @return array module information about other required data
898
 * @since  Moodle 3.2
1441 ariadna 899
 * @throws coding_exception if you try to set a section other than 0 on a module type that has feature flag FEATURE_CAN_DISPLAY
900
 * set to false.
1 efrain 901
 */
902
function prepare_new_moduleinfo_data($course, $modulename, $section, string $suffix = '') {
903
    global $CFG;
904
 
1441 ariadna 905
    // Module types with this flag set to false must always be in section number 0.
906
    if ($section != 0 && !course_modinfo::is_mod_type_visible_on_course($modulename)) {
907
        throw new coding_exception("Modules with feature flag FEATURE_CAN_DISPLAY set to false can only be in section 0");
908
    }
909
 
1 efrain 910
    list($module, $context, $cw) = can_add_moduleinfo($course, $modulename, $section);
911
 
912
    $cm = null;
913
 
914
    $data = new stdClass();
915
    $data->section          = $section;  // The section number itself - relative!!! (section column in course_sections)
916
    $data->visible          = $cw->visible;
917
    $data->course           = $course->id;
918
    $data->module           = $module->id;
919
    $data->modulename       = $module->name;
920
    $data->groupmode        = $course->groupmode;
921
    $data->groupingid       = $course->defaultgroupingid;
922
    $data->id               = '';
923
    $data->instance         = '';
924
    $data->coursemodule     = '';
925
    $data->downloadcontent  = DOWNLOAD_COURSE_CONTENT_ENABLED;
926
 
927
    // Apply completion defaults.
928
    $defaults = \core_completion\manager::get_default_completion($course, $module, true, $suffix);
929
    foreach ($defaults as $key => $value) {
930
        $data->$key = $value;
931
    }
932
 
933
    if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
934
        $draftid_editor = file_get_submitted_draft_itemid('introeditor');
935
        file_prepare_draft_area($draftid_editor, null, null, null, null, array('subdirs'=>true));
936
        $data->introeditor = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftid_editor);
937
    }
938
 
939
    if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
940
            and has_capability('moodle/grade:managegradingforms', $context)) {
941
        require_once($CFG->dirroot.'/grade/grading/lib.php');
942
 
943
        $data->_advancedgradingdata['methods'] = grading_manager::available_methods();
944
        $areas = grading_manager::available_areas('mod_'.$module->name);
945
 
946
        foreach ($areas as $areaname => $areatitle) {
947
            $data->_advancedgradingdata['areas'][$areaname] = array(
948
                'title'  => $areatitle,
949
                'method' => '',
950
            );
951
            $formfield = 'advancedgradingmethod_'.$areaname;
952
            $data->{$formfield} = '';
953
        }
954
    }
955
 
1441 ariadna 956
    if (plugin_supports('mod', $data->modulename, FEATURE_QUICKCREATE)) {
957
        if (get_string_manager()->string_exists('quickcreatename', "mod_{$data->modulename}")) {
958
            $data->name = get_string("quickcreatename", "mod_{$data->modulename}");
959
        }
960
    }
961
 
1 efrain 962
    return array($module, $context, $cw, $cm, $data);
963
}