-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathedit.php
More file actions
271 lines (237 loc) · 8.96 KB
/
edit.php
File metadata and controls
271 lines (237 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Page for editing pcast episodes
*
* @package mod_pcast
* @copyright 2010 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once(dirname(__FILE__) . '/lib.php');
require_once(dirname(__FILE__) . '/locallib.php');
require_once(dirname(__FILE__) . '/edit_form.php');
$cmid = required_param('cmid', PARAM_INT); // Course Module ID.
$id = optional_param('id', 0, PARAM_INT); // EntryID.
// Check for required stuff.
if ($cmid) {
$cm = get_coursemodule_from_id('pcast', $cmid, 0, false, MUST_EXIST);
$course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
$pcast = $DB->get_record('pcast', ['id' => $cm->instance], '*', MUST_EXIST);
} else {
throw new moodle_exception('invalidcmorid', 'pcast');
}
require_login($course, false, $cm);
$context = context_module::instance($cm->id);
$url = new moodle_url('/mod/pcast/edit.php', ['cmid' => $cm->id]);
if (!empty($id)) {
$url->param('id', $id);
}
$PAGE->set_url($url);
$PAGE->set_context($context);
if ($id) { // If the entry is specified.
if (!has_capability('mod/pcast:write', $context)) {
throw new moodle_exception('noeditprivlidges', 'pcast', new moodle_url('/mod/pcast/view.php', ['id' => $cmid]));
}
if (!$episode = $DB->get_record('pcast_episodes', ['id' => $id, 'pcastid' => $pcast->id])) {
throw new moodle_exception('invalidentry', 'pcast');
}
// Calculate the editing period.
$ineditperiod = ((time() - $episode->timecreated < $CFG->maxeditingtime));
if (has_capability('mod/pcast:manage', $context)) {
// Teacher.
if (!has_capability('mod/pcast:write', $context)) {
// No permissions.
throw new moodle_exception(
'errcannotedit',
'pcast',
"view.php?id=$cm->id&mode=" . PCAST_STANDARD_VIEW . "&hook=$id"
);
}
} else {
// Not A Teacher.
if (!has_capability('mod/pcast:write', $context)) {
// No permissions.
throw new moodle_exception(
'errcannotedit',
'pcast',
"view.php?id=$cm->id&mode=" . PCAST_STANDARD_VIEW . "&hook=$id"
);
} else if ($episode->userid != $USER->id) {
// Not the origional author.
throw new moodle_exception(
'errcannoteditothers',
'pcast',
"view.php?id=$cm->id&mode=" . PCAST_STANDARD_VIEW . "&hook=$id"
);
} else if (!$ineditperiod) {
// After the editing period.
throw new moodle_exception(
'erredittimeexpired',
'pcast',
"view.php?id=$cm->id&mode=" . PCAST_STANDARD_VIEW . "&hook=$id"
);
}
}
} else { // A new entry.
require_capability('mod/pcast:write', $context);
$episode = new stdClass();
$episode->id = null;
$episode->summary = ''; // This will be updated later.
$episode->summaryformat = FORMAT_HTML; // This will be updated later.
$episode->summarytrust = 0; // This will be updated later.
}
$draftitemid = file_get_submitted_draft_itemid('mediafile');
file_prepare_draft_area(
$draftitemid,
$context->id,
'mod_pcast',
'episode',
$episode->id,
[
'subdirs' => 0,
'maxbytes' => $pcast->maxbytes,
'maxfiles' => 1,
'accepted_types' => pcast_get_supported_file_types($pcast),
]
);
$episode->mediafile = $draftitemid;
$episode->cmid = $cm->id;
$draftideditor = file_get_submitted_draft_itemid('summary');
$currenttext = file_prepare_draft_area(
$draftideditor,
$context->id,
'mod_pcast',
'summary',
$episode->id,
['subdirs' => true],
$episode->summary
);
$episode->summary = ['text' => $currenttext, 'format' => $episode->summaryformat, 'itemid' => $draftideditor];
// Create the form and set the initial data.
$mform = new mod_pcast_entry_form(null, ['current' => $episode, 'cm' => $cm, 'pcast' => $pcast, 'context' => $context]);
if ($mform->is_cancelled()) {
if ($id) {
redirect("view.php?id=$cm->id&mode=" . PCAST_ADDENTRY_VIEW . "&hook=$id");
} else {
redirect("view.php?id=$cm->id");
}
} else if ($episode = $mform->get_data()) {
$timenow = time();
// Calculated settings.
if (empty($episode->id)) {
$episode->pcastid = $pcast->id;
$episode->timecreated = $timenow;
$episode->userid = $USER->id;
$episode->course = $COURSE->id;
}
$episode->summaryformat = $episode->summary['format'];
$episode->summary = $episode->summary['text'];
$episode->timemodified = $timenow;
$episode->approved = 0;
$episode->name = clean_param($episode->name, PARAM_TEXT);
// Get the episode category information.
$episode = pcast_get_itunes_categories($episode, $pcast);
// Episode approval.
if (!$pcast->requireapproval || has_capability('mod/pcast:approve', $context)) {
$episode->approved = 1;
}
if (empty($episode->id)) {
// A new entry.
$episode->id = $DB->insert_record('pcast_episodes', $episode);
$isnewentry = true;
} else {
// An existing entry.
$DB->update_record('pcast_episodes', $episode);
$isnewentry = false;
}
file_save_draft_area_files(
$episode->mediafile,
$context->id,
'mod_pcast',
'episode',
$episode->id,
[
'subdirs' => 0,
'maxbytes' => $pcast->maxbytes,
'maxfiles' => 1,
'accepted_types' => pcast_get_supported_file_types($pcast),
]
);
// Get the duration if possible.
$fs = get_file_storage();
if ($files = $fs->get_area_files($context->id, 'mod_pcast', 'episode', $episode->id, "timemodified", false)) {
foreach ($files as $file) {
$target = $file->copy_content_to_temp();
$mediainfo = pcast_get_media_information($target);
if (!empty($mediainfo['playtime_string'])) {
$episode->duration = $mediainfo['playtime_string'];
}
}
}
// Save files in summary field and re-write hyperlinks.
$episode->summary = file_save_draft_area_files(
$draftideditor,
$context->id,
'mod_pcast',
'summary',
$episode->id,
['subdirs' => true],
$episode->summary
);
// Store the updated value values.
$DB->update_record('pcast_episodes', $episode);
// Deal with tags.
if (core_tag_tag::is_enabled('mod_pcast', 'pcast_episodes') && isset($episode->tags)) {
core_tag_tag::set_item_tags('mod_pcast', 'pcast_episodes', $episode->id, $context, $episode->tags);
}
// Refetch the complete entry.
$episode = $DB->get_record('pcast_episodes', ['id' => $episode->id]);
// Trigger event and update completion (if entry was created).
$eventparams = [
'context' => $context,
'objectid' => $episode->id,
'other' => ['name' => $episode->name],
];
if ($isnewentry) {
$event = \mod_pcast\event\episode_created::create($eventparams);
} else {
$event = \mod_pcast\event\episode_updated::create($eventparams);
}
$event->add_record_snapshot('pcast_episodes', $episode);
$event->trigger();
// Update completion state.
$completion = new completion_info($course);
if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $pcast->completionepisodes) {
$completion->update_state($cm, COMPLETION_COMPLETE, $episode->userid);
}
// Calculate hook.
$hook = core_text::substr($episode->name, 0, 1);
redirect("view.php?id=$cm->id&mode=" . PCAST_ADDENTRY_VIEW . "&hook=$hook");
}
if (!empty($id)) {
$PAGE->navbar->add(get_string('edit'));
}
$PAGE->set_title($pcast->name);
$PAGE->set_heading($course->fullname);
$PAGE->set_secondary_active_tab('modulepage');
echo $OUTPUT->header();
echo $OUTPUT->heading(format_string($pcast->name));
$episode = new StdClass();
$episode->tags = core_tag_tag::get_item_tags_array('mod_pcast', 'pcast_episodes', $id);
$mform->set_data($episode);
$mform->display();
echo $OUTPUT->footer();