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
use core_completion\manager;
18
 
19
/**
20
 * Default activity completion form
21
 *
22
 * @package     core_completion
23
 * @copyright   2017 Marina Glancy
24
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
class core_completion_defaultedit_form extends core_completion_edit_base_form {
27
    /** @var array */
28
    protected $modules;
29
    /** @var array */
30
    protected $_modnames;
31
 
32
    public function __construct(
33
        $action = null,
34
        $customdata = null,
35
        $method = 'post',
36
        $target = '',
37
        $attributes = null,
38
        $editable = true,
39
        $ajaxformdata = null
40
    ) {
41
        $this->modules = $customdata['modules'];
42
        if ($modname = $this->get_module_name()) {
43
            // Set the form suffix to the module name so that the form identifier is unique for each module type.
44
            $this->set_suffix('_' . $modname);
45
        }
46
 
47
        parent::__construct($action, $customdata, $method, $target, $attributes, $editable, $ajaxformdata);
48
    }
49
 
50
 
51
    /**
52
     * Returns list of types of selected modules
53
     *
54
     * @return array modname=>modfullname
55
     */
56
    protected function get_module_names() {
57
        if ($this->_modnames !== null) {
58
            return $this->_modnames;
59
        }
60
        $this->_modnames = [];
61
        foreach ($this->modules as $module) {
62
            $this->_modnames[$module->name] = $module->formattedname;
63
        }
64
        return $this->_modnames;
65
    }
66
 
67
    /**
68
     * Returns an instance of component-specific module form for the first selected module
69
     *
70
     * @return moodleform_mod|null
71
     */
72
    protected function get_module_form() {
73
        if ($this->_moduleform) {
74
            return $this->_moduleform;
75
        }
76
 
77
        $modnames = array_keys($this->get_module_names());
78
        $this->_moduleform = manager::get_module_form(
79
                modname: $modnames[0],
80
                course: $this->course,
81
                suffix: $this->get_suffix(),
82
        );
83
 
84
        return $this->_moduleform;
85
    }
86
 
87
    /**
88
     * Form definition,
89
     */
90
    public function definition() {
91
        $course = $this->_customdata['course'];
92
        $this->course = is_numeric($course) ? get_course($course) : $course;
93
        $this->modules = $this->_customdata['modules'];
94
 
95
        $mform = $this->_form;
96
 
97
        foreach ($this->modules as $modid => $module) {
98
            $mform->addElement('hidden', 'modids['.$modid.']', $modid);
99
            $mform->setType('modids['.$modid.']', PARAM_INT);
100
        }
101
 
102
        parent::definition();
103
 
104
        $modform = $this->get_module_form();
105
        if ($modform) {
106
            $modnames = array_keys($this->get_module_names());
107
            $modname = $modnames[0];
108
            // Pre-fill the form with the current completion rules of the first selected module type.
109
            list($module, $context, $cw, $cmrec, $data) = prepare_new_moduleinfo_data(
110
                $this->course,
111
                $modname,
112
                0,
113
                $this->get_suffix()
114
            );
115
            $data = (array)$data;
116
            try {
117
                $modform->data_preprocessing($data);
118
            } catch (moodle_exception $e) {
119
                debugging(
120
                    'data_preprocessing function of module ' . $modnames[0] .
121
                    ' should be fixed so it can be shown together with other Default activity completion forms',
122
                    DEBUG_DEVELOPER
123
                );
124
            }
125
            // Unset fields that will conflict with this form and set data to this form.
126
            unset($data['cmid']);
127
            unset($data['modids']);
128
            unset($data['id']);
129
            $this->set_data($data);
130
        }
131
    }
132
 
133
    /**
134
     * This method has been overridden because the form identifier must be unique for each module type.
135
     * Otherwise, the form will display the same data for each module type once it's submitted.
136
     */
137
    protected function get_form_identifier() {
138
        return parent::get_form_identifier() . $this->get_suffix();
139
    }
140
}