-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcss.php
149 lines (119 loc) · 5.58 KB
/
css.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
<?php
// This file is part of mod_datalynx 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/>.
/**
*
* @package mod_datalynx
* @copyright 2013 onwards Ivan Šakić, Thomas Niedermaier
* @copyright based on the work by 2011 Itamar Tzadok
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
require_once('../../config.php');
$urlparams = new stdClass();
$urlparams->d = optional_param('d', 0, PARAM_INT); // Datalynx id.
$urlparams->id = optional_param('id', 0, PARAM_INT); // Course module id.
$urlparams->cssedit = optional_param('cssedit', 0, PARAM_BOOL); // Edit mode.
$cm = get_coursemodule_from_instance('datalynx', $urlparams->d, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
require_login($course, false, $cm);
if ($urlparams->cssedit) {
require_once('classes/datalynx.php');
require_once($CFG->libdir . '/formslib.php');
class mod_datalynx_css_form extends moodleform {
public function definition() {
global $COURSE;
$mform = &$this->_form;
// Buttons.
$this->add_action_buttons(true);
// Css.
$mform->addElement('header', 'generalhdr', get_string('headercss', 'datalynx'));
// Includes.
$attributes = array('wrap' => 'soft', 'rows' => 5, 'cols' => 60);
$mform->addElement('textarea', 'cssincludes', get_string('cssincludes', 'datalynx'), $attributes);
// Code.
$attributes = array('wrap' => 'soft', 'rows' => 15, 'cols' => 60);
$mform->addElement('textarea', 'css', get_string('csscode', 'datalynx'), $attributes);
// Uploads.
$options = array('subdirs' => 0, 'maxbytes' => $COURSE->maxbytes, 'maxfiles' => 10,
'accepted_types' => array('*.css')
);
$mform->addElement('filemanager', 'cssupload', get_string('cssupload', 'datalynx'),
null, $options);
// Buttons.
$this->add_action_buttons(true);
}
}
// Set a datalynx object.
$df = new mod_datalynx\datalynx($urlparams->d, $urlparams->id);
require_capability('mod/datalynx:managetemplates', $df->context);
$df->set_page('css', array('urlparams' => $urlparams));
// Activate navigation node.
navigation_node::override_active_url(
new moodle_url('/mod/datalynx/css.php', array('id' => $df->cm->id, 'cssedit' => 1)));
$mform = new mod_datalynx_css_form(
new moodle_url('/mod/datalynx/css.php', array('d' => $df->id(), 'cssedit' => 1)));
if (!$mform->is_cancelled()) {
if ($data = $mform->get_data()) {
// Update the datalynx.
$rec = new stdClass();
$rec->css = $data->css;
$rec->cssincludes = $data->cssincludes;
$df->update($rec, get_string('csssaved', 'datalynx'));
// Add uploaded files.
$usercontext = context_user::instance($USER->id);
$fs = get_file_storage();
if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data->cssupload,
'sortorder', false)
) {
$filerec = new stdClass();
$filerec->contextid = $df->context->id;
$filerec->component = 'mod_datalynx';
$filerec->filearea = 'css';
$filerec->filepath = '/';
foreach ($files as $file) {
$filerec->filename = $file->get_filename();
$fs->create_file_from_storedfile($filerec, $file);
}
$fs->delete_area_files($usercontext->id, 'user', 'draft', $data->cssupload);
}
$event = \mod_datalynx\event\css_saved::create(
array('context' => $df->context, 'objectid' => $df->id()));
$event->trigger();
}
}
$df->print_header(array('tab' => 'css', 'urlparams' => $urlparams));
$options = array('subdirs' => 0, 'maxbytes' => $COURSE->maxbytes, 'maxfiles' => 10);
$draftitemid = file_get_submitted_draft_itemid('cssupload');
file_prepare_draft_area($draftitemid, $df->context->id, 'mod_datalynx', 'css', 0, $options);
$df->data->cssupload = $draftitemid;
$mform->set_data($df->data);
$mform->display();
$df->print_footer();
} else {
defined('NO_MOODLE_COOKIES') || define('NO_MOODLE_COOKIES', true); // Session not used here.
$lifetime = 0; // Seconds to cache this stylesheet.
$PAGE->set_url('/mod/datalynx/css.php', array('d' => $urlparams->d));
if ($cssdata = $DB->get_field('datalynx', 'css', array('id' => $urlparams->d
))
) {
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
header('Expires: ' . gmdate("D, d M Y H:i:s", time() + $lifetime) . ' GMT');
header('Cache-control: max_age = ' . $lifetime);
header('Pragma: ');
header('Content-type: text/css; charset=utf-8'); // Correct MIME type.
echo $cssdata;
}
}