forked from gjbarnard/moodle-format_grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditimage.php
executable file
·134 lines (116 loc) · 5.37 KB
/
editimage.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
<?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/>.
/**
* Grid Format - A topics based format that uses a grid of user selectable images to popup a light box of the section.
*
* @package format_grid
* @version See the value of '$plugin->version' in version.php.
* @copyright © 2012 G J Barnard in respect to modifications of standard topics format.
* @author G J Barnard - {@link http://about.me/gjbarnard} and
* {@link http://moodle.org/user/profile.php?id=442195}
* @author Based on code originally written by Paul Krix and Julian Ridden.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/* Imports */
require_once('../../../config.php');
require_once($CFG->dirroot . '/repository/lib.php');
require_once($CFG->dirroot . '/course/format/grid/editimage_form.php');
require_once($CFG->dirroot . '/course/format/grid/lib.php');
/* Page parameters */
$contextid = required_param('contextid', PARAM_INT);
$sectionid = required_param('sectionid', PARAM_INT);
$id = optional_param('id', null, PARAM_INT);
/* No idea, copied this from an example. Sets form data options but I don't know what they all do exactly */
$formdata = new stdClass();
$formdata->userid = required_param('userid', PARAM_INT);
$formdata->offset = optional_param('offset', null, PARAM_INT);
$formdata->forcerefresh = optional_param('forcerefresh', null, PARAM_INT);
$formdata->mode = optional_param('mode', null, PARAM_ALPHA);
$formdata->alttext = optional_param('alttext', null, PARAM_TEXT);
$url = new moodle_url('/course/format/grid/editimage.php', array(
'contextid' => $contextid,
'id' => $id,
'offset' => $formdata->offset,
'forcerefresh' => $formdata->forcerefresh,
'userid' => $formdata->userid,
'mode' => $formdata->mode));
/* Not exactly sure what this stuff does, but it seems fairly straightforward */
list($context, $course, $cm) = get_context_info_array($contextid);
require_login($course, true, $cm);
if (isguestuser()) {
die();
}
$PAGE->set_url($url);
$PAGE->set_context($context);
/* Functional part. Create the form and display it, handle results, etc */
$options = array(
'subdirs' => 0,
'maxfiles' => 1,
'accepted_types' => array('gif', 'jpe', 'jpeg', 'jpg', 'png'),
'return_types' => FILE_INTERNAL);
// Fetch the existing grid image record if it exists.
$alttext = $DB->get_field('format_grid_icon', 'alttext',
array('courseid' => $course->id, 'sectionid' => $sectionid), IGNORE_MISSING);
$mform = new grid_image_form(null, array(
'contextid' => $contextid,
'userid' => $formdata->userid,
'sectionid' => $sectionid,
'options' => $options,
'alttext' => $alttext));
if ($mform->is_cancelled()) {
// Someone has hit the 'cancel' button.
redirect(new moodle_url($CFG->wwwroot . '/course/view.php?id=' . $course->id));
} else if ($formdata = $mform->get_data()) { // Form has been submitted.
if ($formdata->deleteimage == 1) {
// Delete the old images....
$courseformat = course_get_format($course);
$courseformat->delete_image($sectionid, $context->id);
} else {
if ($newfilename = $mform->get_new_filename('imagefile')) {
$fs = get_file_storage();
// We have a new file so can delete the old....
$courseformat = course_get_format($course);
$sectionimage = $courseformat->get_image($course->id, $sectionid);
if (isset($sectionimage->image)) {
if ($file = $fs->get_file($context->id, 'course', 'section', $sectionid, '/', $sectionimage->image)) {
$file->delete();
}
}
// Resize the new image and save it...
$storedfilerecord = $courseformat->create_original_image_record($contextid, $sectionid, $newfilename);
$tempfile = $mform->save_stored_file(
'imagefile',
$storedfilerecord['contextid'],
$storedfilerecord['component'],
$storedfilerecord['filearea'],
$storedfilerecord['itemid'],
$storedfilerecord['filepath'],
'temp.' . $storedfilerecord['filename'],
true);
$courseformat->create_section_image($tempfile, $storedfilerecord, $sectionimage);
}
// Set alt text value regardless of whether image has changed.
$conditionsarray = array('courseid' => $course->id, 'sectionid' => $sectionid);
$DB->set_field('format_grid_icon', 'alttext', $formdata->alttext, $conditionsarray);
}
redirect($CFG->wwwroot . "/course/view.php?id=" . $course->id);
}
/* Draw the form */
echo $OUTPUT->header();
echo $OUTPUT->box_start('generalbox');
$mform->display();
echo $OUTPUT->box_end();
echo $OUTPUT->footer();