Proyectos de Subversion Moodle

Rev

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

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
// This file is part of Moodle - http://moodle.org/
4
//
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
/**
19
 * This file is part of the Database module for Moodle
20
 *
21
 * @copyright 2005 Martin Dougiamas  http://dougiamas.com
22
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @package mod_data
24
 */
25
 
26
use mod_data\manager;
27
 
28
require_once('../../config.php');
29
require_once('locallib.php');
30
require_once("$CFG->libdir/rsslib.php");
31
require_once("$CFG->libdir/form/filemanager.php");
32
 
33
$id = optional_param('id', 0, PARAM_INT); // Course module id.
34
$d = optional_param('d', 0, PARAM_INT); // Database id.
35
$rid = optional_param('rid', 0, PARAM_INT); // Record id.
36
$mode = 'addtemplate'; // Define the mode for this page, only 1 mode available.
37
$tags = optional_param_array('tags', [], PARAM_TAGLIST);
38
$redirectbackto = optional_param('backto', '', PARAM_LOCALURL); // The location to redirect back.
39
 
40
$url = new moodle_url('/mod/data/edit.php');
41
 
42
$record = null;
43
 
44
if ($id) {
45
    list($course, $cm) = get_course_and_cm_from_cmid($id, manager::MODULE);
46
    $manager = manager::create_from_coursemodule($cm);
47
} else {   // We must have $d.
48
    $data = $DB->get_record('data', ['id' => $d], '*', MUST_EXIST);
49
    $manager = manager::create_from_instance($data);
50
    $cm = $manager->get_coursemodule();
51
    $course = get_course($cm->course);
52
}
53
$data = $manager->get_instance();
54
$context = $manager->get_context();
55
$url->param('id', $cm->id);
56
 
57
if ($rid !== 0) {
58
    $record = $DB->get_record(
59
        'data_records',
60
        ['id' => $rid, 'dataid' => $data->id],
61
        '*',
62
        MUST_EXIST
63
    );
64
    $url->param('rid', $rid);
65
}
66
 
67
$PAGE->set_url($url);
68
require_login($course, false, $cm);
69
 
70
$url->param('backto', $redirectbackto);
71
 
72
if (isguestuser()) {
73
    redirect('view.php?d='.$data->id);
74
}
75
 
76
/// Can't use this if there are no fields
77
if ($manager->can_manage_templates()) {
78
    if (!$manager->has_fields()) {
79
        redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);  // Redirect to field entry.
80
    }
81
}
82
 
83
// Get Group information for permission testing and record creation.
84
$currentgroup = groups_get_activity_group($cm);
85
$groupmode = groups_get_activity_groupmode($cm);
86
 
87
if (!has_capability('mod/data:manageentries', $context)) {
88
    if ($rid) {
89
        // User is editing an existing record.
90
        if (!data_user_can_manage_entry($record, $data, $context)) {
91
            throw new \moodle_exception('noaccess', 'data');
92
        }
93
    } else if (!data_user_can_add_entry($data, $currentgroup, $groupmode, $context)) {
94
        // User is trying to create a new record.
95
        throw new \moodle_exception('noaccess', 'data');
96
    }
97
}
98
 
99
// RSS and CSS and JS meta.
100
if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
101
    $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
102
    $rsstitle = $courseshortname . ': ' . format_string($data->name);
103
    rss_add_http_header($context, 'mod_data', $data, $rsstitle);
104
}
105
if ($data->csstemplate) {
106
    $PAGE->requires->css('/mod/data/css.php?d='.$data->id);
107
}
108
if ($data->jstemplate) {
109
    $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
110
}
111
 
112
// Define page variables.
113
$strdata = get_string('modulenameplural','data');
114
 
115
if ($rid) {
116
    $PAGE->navbar->add(get_string('editentry', 'data'));
117
}
118
 
1441 ariadna 119
$PAGE->add_body_class('limitedwidth');
1 efrain 120
if ($rid) {
121
    $pagename = get_string('editentry', 'data');
122
} else {
123
    $pagename = get_string('newentry', 'data');
124
}
125
$PAGE->navbar->add($pagename);
126
$titleparts = [
127
    $pagename,
128
    format_string($data->name),
129
    format_string($course->fullname),
130
];
131
$PAGE->set_title(implode(moodle_page::TITLE_SEPARATOR, $titleparts));
132
$PAGE->force_settings_menu(true);
133
$PAGE->set_secondary_active_tab('modulepage');
134
$PAGE->activityheader->disable();
135
 
136
// Process incoming data for adding/updating records.
137
 
138
// Keep track of any notifications ad submitted data.
139
$processeddata = null;
140
$datarecord = data_submitted() ?: null;
141
 
142
// Process the submitted form.
143
if ($datarecord && confirm_sesskey()) {
144
    // Validate the form to ensure that enough data was submitted.
145
    $fields = $manager->get_field_records();
146
    $processeddata = data_process_submission($data, $fields, $datarecord);
147
 
148
    if ($processeddata->validated) {
149
        if ($rid) {
150
            $recordid = $rid;
151
            // Updating an existing record.
152
            data_update_record_fields_contents($data, $record, $context, $datarecord, $processeddata);
153
        } else {
154
            // Add instance to data_record.
155
            $recordid = data_add_record($data, $currentgroup);
156
            if ($recordid) {
157
                // Now populate the fields contents of the new record.
158
                data_add_fields_contents_to_new_record($data, $context, $recordid, $fields, $datarecord, $processeddata);
159
            }
160
        }
161
 
162
        if ($recordid) {
163
            core_tag_tag::set_item_tags('mod_data', 'data_records', $recordid, $context, $tags);
164
 
165
            if (!empty($datarecord->saveandadd)) {
166
                // User has clicked "Save and add another". Reset all of the fields.
167
                $datarecord = null;
168
            } else {
169
                $viewurl = new moodle_url('/mod/data/view.php', [
170
                    'd' => $data->id,
171
                    'rid' => $recordid,
172
                ]);
173
                redirect($viewurl);
174
            }
175
        }
176
    }
177
}
178
// End of form processing.
179
 
180
echo $OUTPUT->header();
181
 
182
groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id);
183
 
184
// Form goes here first in case add template is empty.
185
echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
186
echo '<div>';
187
echo '<input name="d" value="'.$data->id.'" type="hidden" />';
188
echo '<input name="rid" value="'.$rid.'" type="hidden" />';
189
echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
190
echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
191
 
192
echo $OUTPUT->heading($pagename);
193
 
194
$template = $manager->get_template($mode);
195
echo $template->parse_add_entry($processeddata, $rid, $datarecord);
196
 
197
if (empty($redirectbackto)) {
198
    $redirectbackto = new \moodle_url('/mod/data/view.php', ['id' => $cm->id]);
199
}
200
 
201
$actionbuttons = html_writer::link(
202
    $redirectbackto,
203
    get_string('cancel'),
204
    ['class' => 'btn btn-secondary mx-1', 'role' => 'button']
205
);
206
$actionbuttons .= html_writer::empty_tag('input', [
207
    'type' => 'submit',
208
    'name' => 'saveandview',
209
    'value' => get_string('save'),
210
    'class' => 'btn btn-primary mx-1'
211
]);
212
 
213
if (!$rid && ((!$data->maxentries) ||
214
    has_capability('mod/data:manageentries', $context) ||
215
    (data_numentries($data) < ($data->maxentries - 1)))) {
216
    $actionbuttons .= html_writer::empty_tag('input', [
217
        'type' => 'submit', 'name' => 'saveandadd',
218
        'value' => get_string('saveandadd', 'data'), 'class' => 'btn btn-primary mx-1'
219
    ]);
220
}
221
 
222
$stickyfooter = new core\output\sticky_footer($actionbuttons);
223
echo $OUTPUT->render($stickyfooter);
224
 
225
echo $OUTPUT->box_end();
226
echo '</div></form>';
227
 
228
$possiblefields = $manager->get_fields();
229
foreach ($possiblefields as $field) {
230
    $field->print_after_form();
231
}
232
 
233
// Finish the page.
234
if (empty($possiblefields)) {
235
    throw new \moodle_exception('nofieldindatabase', 'data');
236
}
237
echo $OUTPUT->footer();