This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
locallib.php
386 lines (340 loc) · 11.6 KB
/
locallib.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?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/>.
/**
* This class provides functionality for the ompdf module.
*
* @package mod_ompdf
* @copyright 2013 Dian Mukti Wibowo <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Standard base class for mod_ompdf.
*/
class ompdf {
/** @var stdClass The ompdf record that contains the
* global settings for this ompdf instance.
*/
private $instance;
/** @var context The context of the course module for this ompdf
* instance (or just the course if we are creating a new one).
*/
private $context;
/** @var stdClass The course this ompdf instance belongs to */
private $course;
/** @var ompdf_renderer The custom renderer for this module */
private $output;
/** @var stdClass The course module for this ompdf instance */
private $coursemodule;
/** @var stdClass The plugin default configuration */
private $defaultconfig;
/** @var string modulename Prevents excessive calls to get_string */
private static $modulename = null;
/** @var string modulenameplural Prevents excessive calls to get_string */
private static $modulenameplural = null;
/**
* Constructor for the base ompdf class.
*
* @param mixed $coursemodulecontext context|null The course module context
* (or the course context if the coursemodule
* has not been created yet).
* @param mixed $coursemodule The current course module if it was already loaded,
* otherwise this class will load one from the context
* as required.
* @param mixed $course The current course if it was already loaded,
* otherwise this class will load one from the context as
* required.
*/
public function __construct($coursemodulecontext, $coursemodule, $course) {
global $PAGE;
$this->context = $coursemodulecontext;
$this->coursemodule = $coursemodule;
$this->course = $course;
}
/**
* Set the course data.
*
* @param stdClass $course The course data
*/
public function set_course(stdClass $course) {
$this->course = $course;
}
/**
* Add this instance to the database.
*
* @param stdClass $formdata The data submitted from the form
* @return mixed False if an error occurs or the int id of the new instance
*/
public function add_instance(stdClass $formdata) {
global $DB;
// Add the database record.
$add = new stdClass();
$add->name = $formdata->name;
$add->timemodified = time();
$add->timecreated = time();
$add->course = $formdata->course;
$add->courseid = $formdata->course;
$add->intro = $formdata->intro;
$add->introformat = $formdata->introformat;
$add->display = $formdata->display;
$add->showexpanded = $formdata->showexpanded;
$add->openinnewtab = $formdata->openinnewtab;
$returnid = $DB->insert_record('ompdf', $add);
$this->instance = $DB->get_record('ompdf',
array('id' => $returnid),
'*',
MUST_EXIST);
$this->save_files($formdata);
// Cache the course record.
$this->course = $DB->get_record('course',
array('id' => $formdata->course),
'*',
MUST_EXIST);
return $returnid;
}
/**
* Delete this instance from the database.
*
* @return bool False if an error occurs
*/
public function delete_instance() {
global $DB;
$result = true;
// Delete files associated with this ompdf.
$fs = get_file_storage();
if (! $fs->delete_area_files($this->context->id) ) {
$result = false;
}
// Delete the instance.
// Note: all context files are deleted automatically.
$DB->delete_records('ompdf', array('id' => $this->get_instance()->id));
return $result;
}
/**
* Update this instance in the database.
*
* @param stdClass $formdata The data submitted from the form
* @return bool False if an error occurs
*/
public function update_instance($formdata) {
global $DB;
$update = new stdClass();
$update->id = $formdata->instance;
$update->name = $formdata->name;
$update->timemodified = time();
$update->course = $formdata->course;
$update->intro = $formdata->intro;
$update->introformat = $formdata->introformat;
$update->display = $formdata->display;
$update->showexpanded = $formdata->showexpanded;
$update->openinnewtab = $formdata->openinnewtab;
$result = $DB->update_record('ompdf', $update);
$this->instance = $DB->get_record('ompdf',
array('id' => $update->id),
'*',
MUST_EXIST);
$this->save_files($formdata);
return $result;
}
/**
* Get the name of the current module.
*
* @return string The module name (pdfjs folder)
*/
protected function get_module_name() {
if (isset(self::$modulename)) {
return self::$modulename;
}
self::$modulename = get_string('modulename', 'ompdf');
return self::$modulename;
}
/**
* Get the plural name of the current module.
*
* @return string The module name plural (pdfjs folders)
*/
protected function get_module_name_plural() {
if (isset(self::$modulenameplural)) {
return self::$modulenameplural;
}
self::$modulenameplural = get_string('modulenameplural', 'ompdf');
return self::$modulenameplural;
}
/**
* Has this ompdf been constructed from an instance?
*
* @return bool
*/
public function has_instance() {
return $this->instance || $this->get_course_module();
}
/**
* Get the settings for the current instance of this ompdf.
*
* @return stdClass The settings
*/
public function get_instance() {
global $DB;
if ($this->instance) {
return $this->instance;
}
if ($this->get_course_module()) {
$params = array('id' => $this->get_course_module()->instance);
$this->instance = $DB->get_record('ompdf',
$params,
'*',
MUST_EXIST);
}
if (!$this->instance) {
throw new coding_exception('Improper use of the ompdf class. ' .
'Cannot load the ompdf record.');
}
return $this->instance;
}
/**
* Get the context of the current course.
*
* @return mixed context|null The course context
*/
public function get_course_context() {
if (!$this->context && !$this->course) {
throw new coding_exception('Improper use of the ompdf class. ' .
'Cannot load the course context.');
}
if ($this->context) {
return $this->context->get_course_context();
} else {
return context_course::instance($this->course->id);
}
}
/**
* Get the current course module.
*
* @return mixed stdClass|null The course module
*/
public function get_course_module() {
if ($this->coursemodule) {
return $this->coursemodule;
}
if (!$this->context) {
return null;
}
if ($this->context->contextlevel == CONTEXT_MODULE) {
$this->coursemodule = get_coursemodule_from_id(
'ompdf',
$this->context->instanceid,
0,
false,
MUST_EXIST);
return $this->coursemodule;
}
return null;
}
/**
* Get the default configuration for the plugin.
*
* @return mixed stdClass|boolean The default configuration
*/
public function get_default_config() {
if (!$this->defaultconfig) {
$this->defaultconfig = get_config('ompdf');
}
return $this->defaultconfig;
}
/**
* Get context module.
*
* @return context
*/
public function get_context() {
return $this->context;
}
/**
* Get the current course.
*
* @return mixed stdClass|null The course
*/
public function get_course() {
global $DB;
if ($this->course) {
return $this->course;
}
if (!$this->context) {
return null;
}
$params = array('id' => $this->get_course_context()->instanceid);
$this->course = $DB->get_record('course', $params, '*', MUST_EXIST);
return $this->course;
}
/**
* Util function to add a message to the log.
*
* @param string $action The current action
* @param string $info A detailed description of the change.
* But no more than 255 characters.
* @param string $url The url to the ompdf module instance.
* @return void
*/
public function add_to_log($action = '', $info = '', $url='') {
global $USER;
$fullurl = 'view.php?id=' . $this->get_course_module()->id;
if ($url != '') {
$fullurl .= '&' . $url;
}
$event = \mod_ompdf\event\view_all::create(array(
'objectid' => $this->get_course_module()->id,
'context' => context_module::instance($USER->id)
));
$event->trigger();
}
/**
* Lazy load the page renderer and expose the renderer to plugins.
*
* @return ompdf_renderer
*/
public function get_renderer() {
global $PAGE;
if ($this->output) {
return $this->output;
}
$this->output = $PAGE->get_renderer('mod_ompdf');
return $this->output;
}
/**
* Save draft files.
*
* @param stdClass $formdata
* @return void
*/
protected function save_files($formdata) {
global $DB;
// Storage of files from the filemanager (pdfs).
$options = array('subdirs' => true,
'maxbytes' => 0,
'maxfiles' => -1);
$draftitemid = $formdata->pdfs;
if ($draftitemid) {
file_save_draft_area_files(
$draftitemid,
$this->context->id,
'mod_ompdf',
'pdfs',
0,
$options
);
}
}
}