/
lib.php
281 lines (227 loc) · 8.55 KB
/
lib.php
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
272
273
274
275
276
277
278
279
280
281
<?php
// This file is part of mod_publication for Moodle - http://moodle.org/
//
// It 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.
//
// It 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/>.
/**
* Contains interface and callback methods for mod_publication
*
* @package mod_publication
* @author Philipp Hager
* @author Andreas Windbichler
* @copyright 2014 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Adds a new publication instance
*
* @param stdClass $publication data (from mod_publication_mod_form)
* @return int publication ID
*/
function publication_add_instance($publication) {
global $DB, $OUTPUT;
$cmid = $publication->coursemodule;
$courseid = $publication->course;
$id = 0;
try {
$id = $DB->insert_record('publication', $publication);
} catch (Exception $e) {
echo $OUTPUT->notification($e->getMessage(), 'error');
}
$DB->set_field('course_modules', 'instance', $id, ['id' => $cmid]);
$record = $DB->get_record('publication', ['id' => $id]);
$record->course = $courseid;
$record->cmid = $cmid;
$course = $DB->get_record('course', ['id' => $record->course], '*', MUST_EXIST);
$cm = get_coursemodule_from_id('publication', $cmid, 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
$instance = new publication($cm, $course, $context);
if ($instance->get_instance()->mode == PUBLICATION_MODE_IMPORT
&& !empty($instance->get_instance()->autoimport)) {
// Fetch all files right now!
$instance->importfiles();
}
return $record->id;
}
/**
* Return the list if Moodle features this module supports
*
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, null if doesn't know
*/
function publication_supports($feature) {
switch ($feature) {
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_GRADE_HAS_GRADE:
return false;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
case FEATURE_IDNUMBER:
return false;
default:
return null;
}
}
/**
* updates an existing publication instance
*
* @param stdClass $publication from mod_publication_mod_form
* @return bool true
*/
function publication_update_instance($publication) {
global $DB;
$publication->id = $publication->instance;
$publication->timemodified = time();
$DB->update_record('publication', $publication);
$course = $DB->get_record('course', ['id' => $publication->course], '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('publication', $publication->id, 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
$instance = new publication($cm, $course, $context);
if ($instance->get_instance()->mode == PUBLICATION_MODE_IMPORT
&& !empty($instance->get_instance()->autoimport)) {
// Fetch all files right now!
$instance->importfiles();
}
return true;
}
/**
* complete deletes an publication instance
*
* @param int $id
* @return bool
*/
function publication_delete_instance($id) {
global $DB;
if (!$publication = $DB->get_record('publication', ['id' => $id])) {
return false;
}
$DB->delete_records('publication_extduedates', ['publication' => $publication->id]);
$fs = get_file_storage();
$fs->delete_area_files($publication->id, 'mod_publication', 'attachment');
$DB->delete_records('publication_file', ['publication' => $publication->id]);
$result = true;
if (!$DB->delete_records('publication', ['id' => $publication->id])) {
$result = false;
}
return $result;
}
/**
* Returns info object about the course module
*
* @param stdClass $coursemodule The coursemodule object (record).
* @return bool|cached_cm_info An object on information that the courses will know about (most noticeably, an icon) or false.
*/
function publication_get_coursemodule_info($coursemodule) {
global $DB;
$dbparams = ['id' => $coursemodule->instance];
$fields = 'id, name, alwaysshowdescription, allowsubmissionsfromdate, intro, introformat';
if (!$publication = $DB->get_record('publication', $dbparams, $fields)) {
return false;
}
$result = new cached_cm_info();
$result->name = $publication->name;
if ($coursemodule->showdescription) {
if ($publication->alwaysshowdescription || time() > $publication->allowsubmissionsfromdate) {
// Convert intro to html. Do not filter cached version, filters run at display time.
$result->content = format_module_intro('publication', $publication, $coursemodule->id, false);
}
}
return $result;
}
/**
* Defines which elements mod_publication needs to add to reset form
*
* @param MoodleQuickForm $mform The reset course form to extend
*/
function publication_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'publicationheader', get_string('modulenameplural', 'publication'));
$mform->addElement('checkbox', 'reset_publication_userdata', get_string('reset_userdata', 'publication'));
}
/**
* Reset the userdata in publication module
*
* @param object $data settings object which userdata to reset
* @return array[] array of associative arrays giving feedback what has been successfully reset
*/
function publication_reset_userdata($data) {
global $DB;
if (!$DB->count_records('publication', ['course' => $data->courseid])) {
return [];
}
$componentstr = get_string('modulenameplural', 'publication');
$status = [];
if (isset($data->reset_publication_userdata)) {
$publications = $DB->get_records('publication', ['course' => $data->courseid]);
foreach ($publications as $publication) {
$DB->delete_records('publication_extduedates', ['publication' => $publication->id]);
$filerecords = $DB->get_records('publication_file', ['publication' => $publication->id]);
$fs = get_file_storage();
foreach ($filerecords as $filerecord) {
if ($file = $fs->get_file_by_id($filerecord->fileid)) {
$file->delete();
}
}
$DB->delete_records('publication_file', ['publication' => $publication->id]);
$status[] = [
'component' => $componentstr,
'item' => $publication->name,
'error' => false
];
}
}
return $status;
}
/**
* Serves resource files.
*
* @param mixed $course course or id of the course
* @param mixed $cm course module or id of the course module
* @param context $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @param array $options additional options affecting the file serving
* @return bool false if file not found, does not return if found - just send the file
*/
function mod_publication_pluginfile($course, $cm, context $context, $filearea, $args, $forcedownload, array $options = []) {
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}
require_login($course, false, $cm);
if (!has_capability('mod/publication:view', $context)) {
return false;
}
if ($filearea !== 'attachment') {
return false;
}
$itemid = (int)array_shift($args);
$relativepath = implode('/', $args);
$fullpath = "/{$context->id}/mod_publication/$filearea/$itemid/$relativepath";
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
send_stored_file($file, 0, 0, $forcedownload, $options);
// Wont be reached!
return false;
}