MDL-61537 assignfeedback_editpdf: Rotate PDF page
[moodle.git] / mod / assign / feedback / editpdf / ajax.php
CommitLineData
5c386472
DW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Process ajax requests
19 *
20 * @package assignfeedback_editpdf
21 * @copyright 2012 Davo Smith
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25use \assignfeedback_editpdf\document_services;
f7a9f1dd 26use \assignfeedback_editpdf\combined_document;
5c386472
DW
27use \assignfeedback_editpdf\page_editor;
28use \assignfeedback_editpdf\comments_quick_list;
29
1d45b0e4 30define('AJAX_SCRIPT', true);
5c386472 31
1d45b0e4 32require('../../../../config.php');
5c386472
DW
33require_once($CFG->dirroot . '/mod/assign/locallib.php');
34
35require_sesskey();
36
37$action = optional_param('action', '', PARAM_ALPHANUM);
38$assignmentid = required_param('assignmentid', PARAM_INT);
39$userid = required_param('userid', PARAM_INT);
40$attemptnumber = required_param('attemptnumber', PARAM_INT);
098f7dd4 41$readonly = optional_param('readonly', false, PARAM_BOOL);
5c386472
DW
42
43$cm = \get_coursemodule_from_instance('assign', $assignmentid, 0, false, MUST_EXIST);
44$context = \context_module::instance($cm->id);
45
46$assignment = new \assign($context, null, null);
47
1d45b0e4
DW
48require_login($assignment->get_course(), false, $cm);
49
5c386472
DW
50if (!$assignment->can_view_submission($userid)) {
51 print_error('nopermission');
52}
53
f7a9f1dd 54if ($action === 'pollconversions') {
5c386472
DW
55 $draft = true;
56 if (!has_capability('mod/assign:grade', $context)) {
f7a9f1dd
AN
57 // A student always sees the readonly version.
58 $readonly = true;
5c386472
DW
59 $draft = false;
60 require_capability('mod/assign:submit', $context);
61 }
62
098f7dd4 63 if ($readonly) {
f7a9f1dd 64 // Whoever is viewing the readonly version should not use the drafts, but the actual annotations.
098f7dd4
FM
65 $draft = false;
66 }
67
f7a9f1dd
AN
68 $response = (object) [
69 'status' => -1,
70 'filecount' => 0,
71 'pagecount' => 0,
72 'pageready' => 0,
86c57709 73 'partial' => false,
f7a9f1dd
AN
74 'pages' => [],
75 ];
76
77 $combineddocument = document_services::get_combined_document_for_attempt($assignment, $userid, $attemptnumber);
78 $response->status = $combineddocument->get_status();
79 $response->filecount = $combineddocument->get_document_count();
80
86c57709
DW
81 $readystatuslist = [combined_document::STATUS_READY, combined_document::STATUS_READY_PARTIAL];
82 $completestatuslist = [combined_document::STATUS_COMPLETE, combined_document::STATUS_FAILED];
83
84 if (in_array($response->status, $readystatuslist)) {
f7a9f1dd
AN
85 $combineddocument = document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
86 $response->pagecount = $combineddocument->get_page_count();
86c57709 87 } else if (in_array($response->status, $completestatuslist)) {
f7a9f1dd
AN
88 $pages = document_services::get_page_images_for_attempt($assignment,
89 $userid,
90 $attemptnumber,
91 $readonly);
92
93 $response->pagecount = $combineddocument->get_page_count();
94
95 $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
96
97 // The readonly files are stored in a different file area.
98 $filearea = document_services::PAGE_IMAGE_FILEAREA;
99 if ($readonly) {
100 $filearea = document_services::PAGE_IMAGE_READONLY_FILEAREA;
101 }
86c57709 102 $response->partial = $combineddocument->is_partial_conversion();
098f7dd4 103
f7a9f1dd
AN
104 foreach ($pages as $id => $pagefile) {
105 $index = count($response->pages);
106 $page = new stdClass();
107 $comments = page_editor::get_comments($grade->id, $index, $draft);
108 $page->url = moodle_url::make_pluginfile_url($context->id,
109 'assignfeedback_editpdf',
110 $filearea,
111 $grade->id,
112 '/',
113 $pagefile->get_filename())->out();
114 $page->comments = $comments;
115 if ($imageinfo = $pagefile->get_imageinfo()) {
116 $page->width = $imageinfo['width'];
117 $page->height = $imageinfo['height'];
118 } else {
119 $page->width = 0;
120 $page->height = 0;
121 }
122 $annotations = page_editor::get_annotations($grade->id, $index, $draft);
123 $page->annotations = $annotations;
124 $response->pages[] = $page;
125
126 $component = 'assignfeedback_editpdf';
127 $filearea = document_services::PAGE_IMAGE_FILEAREA;
128 $filepath = '/';
129 $fs = get_file_storage();
130 $files = $fs->get_directory_files($context->id, $component, $filearea, $grade->id, $filepath);
131 $response->pageready = count($files);
50c12f01 132 }
5c386472
DW
133 }
134
135 echo json_encode($response);
136 die();
137} else if ($action == 'savepage') {
138 require_capability('mod/assign:grade', $context);
139
140 $response = new stdClass();
141 $response->errors = array();
142
f5ba9ac9 143 $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
5c386472
DW
144
145 $pagejson = required_param('page', PARAM_RAW);
146 $page = json_decode($pagejson);
147 $index = required_param('index', PARAM_INT);
148
149 $added = page_editor::set_comments($grade->id, $index, $page->comments);
150 if ($added != count($page->comments)) {
151 array_push($response->errors, get_string('couldnotsavepage', 'assignfeedback_editpdf', $index+1));
152 }
153 $added = page_editor::set_annotations($grade->id, $index, $page->annotations);
154 if ($added != count($page->annotations)) {
155 array_push($response->errors, get_string('couldnotsavepage', 'assignfeedback_editpdf', $index+1));
156 }
157 echo json_encode($response);
158 die();
159
160} else if ($action == 'generatepdf') {
161
162 require_capability('mod/assign:grade', $context);
163 $response = new stdClass();
f5ba9ac9 164 $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
5c386472
DW
165 $file = document_services::generate_feedback_document($assignment, $userid, $attemptnumber);
166
167 $response->url = '';
168 if ($file) {
169 $url = moodle_url::make_pluginfile_url($assignment->get_context()->id,
170 'assignfeedback_editpdf',
171 document_services::FINAL_PDF_FILEAREA,
172 $grade->id,
173 '/',
174 $file->get_filename(),
175 false);
176 $response->url = $url->out(true);
177 $response->filename = $file->get_filename();
178 }
179
180 echo json_encode($response);
181 die();
182} else if ($action == 'loadquicklist') {
183 require_capability('mod/assign:grade', $context);
184
185 $result = comments_quick_list::get_comments();
186
187 echo json_encode($result);
188 die();
189
190} else if ($action == 'addtoquicklist') {
191 require_capability('mod/assign:grade', $context);
192
193 $comment = required_param('commenttext', PARAM_RAW);
194 $width = required_param('width', PARAM_INT);
195 $colour = required_param('colour', PARAM_ALPHA);
196
197 $result = comments_quick_list::add_comment($comment, $width, $colour);
198
199 echo json_encode($result);
200 die();
201} else if ($action == 'revertchanges') {
202 require_capability('mod/assign:grade', $context);
203
f5ba9ac9 204 $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
5c386472
DW
205
206 $result = page_editor::revert_drafts($gradeid);
207
208 echo json_encode($result);
209 die();
210} else if ($action == 'removefromquicklist') {
211 require_capability('mod/assign:grade', $context);
212
213 $commentid = required_param('commentid', PARAM_INT);
214
215 $result = comments_quick_list::remove_comment($commentid);
216
217 echo json_encode($result);
218 die();
219} else if ($action == 'deletefeedbackdocument') {
220 require_capability('mod/assign:grade', $context);
221
f5ba9ac9 222 $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
5c386472
DW
223 $result = document_services::delete_feedback_document($assignment, $userid, $attemptnumber);
224
225 $result = $result && page_editor::unrelease_drafts($grade->id);
226 echo json_encode($result);
227 die();
9432553b
NN
228} else if ($action == 'rotatepage') {
229 require_capability('mod/assign:grade', $context);
230 $response = new stdClass();
231 $index = required_param('index', PARAM_INT);
232 $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
233 $rotateleft = required_param('rotateleft', PARAM_BOOL);
234 $filearea = document_services::PAGE_IMAGE_FILEAREA;
235 $pagefile = document_services::rotate_page($assignment, $userid, $attemptnumber, $index, $rotateleft);
236 $page = new stdClass();
237 $page->url = moodle_url::make_pluginfile_url($context->id, document_services::COMPONENT, $filearea,
238 $grade->id, '/', $pagefile->get_filename())->out();
239 if ($imageinfo = $pagefile->get_imageinfo()) {
240 $page->width = $imageinfo['width'];
241 $page->height = $imageinfo['height'];
242 } else {
243 $page->width = 0;
244 $page->height = 0;
245 }
246 $response = (object)['page' => $page];
247 echo json_encode($response);
248 die();
5c386472
DW
249}
250