Proyectos de Subversion Moodle

Rev

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