fa1e21db5f1de9f69258550f9a5466b3933b1e39
[moodle.git] / mod / assign / feedback / editpdf / ajax.php
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/>.
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  */
25 use \assignfeedback_editpdf\document_services;
26 use \assignfeedback_editpdf\combined_document;
27 use \assignfeedback_editpdf\page_editor;
28 use \assignfeedback_editpdf\comments_quick_list;
30 define('AJAX_SCRIPT', true);
32 require('../../../../config.php');
33 require_once($CFG->dirroot . '/mod/assign/locallib.php');
35 require_sesskey();
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);
41 $readonly = optional_param('readonly', false, PARAM_BOOL);
43 $cm = \get_coursemodule_from_instance('assign', $assignmentid, 0, false, MUST_EXIST);
44 $context = \context_module::instance($cm->id);
46 $assignment = new \assign($context, null, null);
48 require_login($assignment->get_course(), false, $cm);
50 if (!$assignment->can_view_submission($userid)) {
51     print_error('nopermission');
52 }
54 if ($action === 'pollconversions') {
55     $draft = true;
56     if (!has_capability('mod/assign:grade', $context)) {
57         // A student always sees the readonly version.
58         $readonly = true;
59         $draft = false;
60         require_capability('mod/assign:submit', $context);
61     }
63     if ($readonly) {
64         // Whoever is viewing the readonly version should not use the drafts, but the actual annotations.
65         $draft = false;
66     }
68     $response = (object) [
69             'status' => -1,
70             'filecount' => 0,
71             'pagecount' => 0,
72             'pageready' => 0,
73             'partial' => false,
74             'pages' => [],
75         ];
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();
81     $readystatuslist = [combined_document::STATUS_READY, combined_document::STATUS_READY_PARTIAL];
82     $completestatuslist = [combined_document::STATUS_COMPLETE, combined_document::STATUS_FAILED];
84     if (in_array($response->status, $readystatuslist)) {
85         $combineddocument = document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
86         $response->pagecount = $combineddocument->get_page_count();
87     } else if (in_array($response->status, $completestatuslist)) {
88         $pages = document_services::get_page_images_for_attempt($assignment,
89                                                                 $userid,
90                                                                 $attemptnumber,
91                                                                 $readonly);
93         $response->pagecount = $combineddocument->get_page_count();
95         $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
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         }
102         $response->partial = $combineddocument->is_partial_conversion();
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;
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);
132         }
133     }
135     echo json_encode($response);
136     die();
137 } else if ($action == 'savepage') {
138     require_capability('mod/assign:grade', $context);
140     $response = new stdClass();
141     $response->errors = array();
143     $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
145     $pagejson = required_param('page', PARAM_RAW);
146     $page = json_decode($pagejson);
147     $index = required_param('index', PARAM_INT);
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();
160 } else if ($action == 'generatepdf') {
162     require_capability('mod/assign:grade', $context);
163     $response = new stdClass();
164     $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
165     $file = document_services::generate_feedback_document($assignment, $userid, $attemptnumber);
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     }
180     echo json_encode($response);
181     die();
182 } else if ($action == 'loadquicklist') {
183     require_capability('mod/assign:grade', $context);
185     $result = comments_quick_list::get_comments();
187     echo json_encode($result);
188     die();
190 } else if ($action == 'addtoquicklist') {
191     require_capability('mod/assign:grade', $context);
193     $comment = required_param('commenttext', PARAM_RAW);
194     $width = required_param('width', PARAM_INT);
195     $colour = required_param('colour', PARAM_ALPHA);
197     $result = comments_quick_list::add_comment($comment, $width, $colour);
199     echo json_encode($result);
200     die();
201 } else if ($action == 'revertchanges') {
202     require_capability('mod/assign:grade', $context);
204     $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
206     $result = page_editor::revert_drafts($gradeid);
208     echo json_encode($result);
209     die();
210 } else if ($action == 'removefromquicklist') {
211     require_capability('mod/assign:grade', $context);
213     $commentid = required_param('commentid', PARAM_INT);
215     $result = comments_quick_list::remove_comment($commentid);
217     echo json_encode($result);
218     die();
219 } else if ($action == 'deletefeedbackdocument') {
220     require_capability('mod/assign:grade', $context);
222     $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
223     $result = document_services::delete_feedback_document($assignment, $userid, $attemptnumber);
225     $result = $result && page_editor::unrelease_drafts($grade->id);
226     echo json_encode($result);
227     die();