MDL-42023 assign: Edit PDF plugin - Damyon's contributions
[moodle.git] / mod / assign / feedback / editpdf / locallib.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 * This file contains the definition for the library class for PDF feedback plugin
19 *
20 *
21 * @package assignfeedback_editpdf
22 * @copyright 2012 Davo Smith
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28use \assignfeedback_editpdf\document_services;
29use \assignfeedback_editpdf\page_editor;
30
31/**
32 * library class for editpdf feedback plugin extending feedback plugin base class
33 *
34 * @package assignfeedback_editpdf
35 * @copyright 2012 Davo Smith
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class assign_feedback_editpdf extends assign_feedback_plugin {
39
40 /**
41 * Get the name of the file feedback plugin
42 * @return string
43 */
44 public function get_name() {
45 return get_string('pluginname', 'assignfeedback_editpdf');
46 }
47
48 /**
49 * Create a widget for rendering the editor.
50 *
51 * @param int $userid
52 * @param stdClass $grade
53 * @param bool $readonly
54 * @return assignfeedback_editpdf_widget
55 */
56 public function get_widget($userid, $grade, $readonly) {
57 $attempt = -1;
58 if ($grade) {
59 $attempt = $grade->attemptnumber;
60 } else {
61 $grade = $this->assignment->get_user_grade($userid, true);
62 }
63
64 $feedbackfile = document_services::get_feedback_document($this->assignment->get_instance()->id,
65 $userid,
66 $attempt);
67
68 $stampfiles = array();
69 $fs = get_file_storage();
70 $syscontext = context_system::instance();
71
72 // Copy any new stamps to this instance.
73 if ($files = $fs->get_area_files($syscontext->id,
74 'assignfeedback_editpdf',
75 'stamps',
76 0,
77 "filename",
78 false)) {
79 foreach ($files as $file) {
80 $filename = $file->get_filename();
81 if ($filename !== '.') {
82
83 $existingfile = $fs->get_file($this->assignment->get_context()->id,
84 'assignfeedback_editpdf',
85 'stamps',
86 $grade->id,
87 '/',
88 $file->get_filename());
89 if (!$existingfile) {
90 $newrecord = new stdClass();
91 $newrecord->contextid = $this->assignment->get_context()->id;
92 $newrecord->itemid = $grade->id;
93 $fs->create_file_from_storedfile($newrecord, $file);
94 }
95 }
96 }
97 }
98
99 // Now get the full list of stamp files for this instance.
100 if ($files = $fs->get_area_files($this->assignment->get_context()->id,
101 'assignfeedback_editpdf',
102 'stamps',
103 $grade->id,
104 "filename",
105 false)) {
106 foreach ($files as $file) {
107 $filename = $file->get_filename();
108 if ($filename !== '.') {
109 $url = moodle_url::make_pluginfile_url($this->assignment->get_context()->id,
110 'assignfeedback_editpdf',
111 'stamps',
112 $grade->id,
113 '/',
114 $file->get_filename(),
115 false);
116 array_push($stampfiles, $url->out());
117 }
118 }
119 }
120
121 $url = false;
122 $filename = '';
123 if ($feedbackfile) {
124 $url = moodle_url::make_pluginfile_url($this->assignment->get_context()->id,
125 'assignfeedback_editpdf',
126 document_services::FINAL_PDF_FILEAREA,
127 $grade->id,
128 '/',
129 $feedbackfile->get_filename(),
130 false);
131 $filename = $feedbackfile->get_filename();
132 }
133
134
135 $widget = new assignfeedback_editpdf_widget($this->assignment->get_instance()->id,
136 $userid,
137 $attempt,
138 $url,
139 $filename,
140 $stampfiles,
141 $readonly);
142 return $widget;
143 }
144
145 /**
146 * Get form elements for grading form
147 *
148 * @param stdClass $grade
149 * @param MoodleQuickForm $mform
150 * @param stdClass $data
151 * @param int $userid
152 * @return bool true if elements were added to the form
153 */
154 public function get_form_elements_for_user($grade, MoodleQuickForm $mform, stdClass $data, $userid) {
155 global $PAGE;
156
157 $attempt = -1;
158 if ($grade) {
159 $attempt = $grade->attemptnumber;
160 }
161
162 $files = document_services::list_compatible_submission_files_for_attempt($this->assignment, $userid, $attempt);
163 // Only show the editor if there was a compatible file submitted.
164 if (count($files)) {
165
166 $renderer = $PAGE->get_renderer('assignfeedback_editpdf');
167
168 $widget = $this->get_widget($userid, $grade, false);
169
170 $html = $renderer->render($widget);
171 $mform->addElement('static', 'editpdf', get_string('editpdf', 'assignfeedback_editpdf'), $html);
172 $mform->addHelpButton('editpdf', 'editpdf', 'assignfeedback_editpdf');
173 }
174 }
175
176 /**
177 * Generate the pdf.
178 *
179 * @param stdClass $grade
180 * @param stdClass $data
181 * @return bool
182 */
183 public function save(stdClass $grade, stdClass $data) {
184 if (page_editor::has_annotations_or_comments($grade->id, true)) {
185 document_services::generate_feedback_document($this->assignment, $grade->userid, $grade->attemptnumber);
186 }
187
188 return true;
189 }
190
191 /**
192 * Display the list of files in the feedback status table.
193 *
194 * @param stdClass $grade
195 * @param bool $showviewlink (Always set to false).
196 * @return string
197 */
198 public function view_summary(stdClass $grade, & $showviewlink) {
199 $showviewlink = false;
200 return $this->view($grade);
201 }
202
203 /**
204 * Display the list of files in the feedback status table.
205 *
206 * @param stdClass $grade
207 * @return string
208 */
209 public function view(stdClass $grade) {
210 global $PAGE;
211 $html = '';
212 // Show a link to download the pdf.
213 if (page_editor::has_annotations_or_comments($grade->id, false)) {
214 $html = $this->assignment->render_area_files('assignfeedback_editpdf',
215 document_services::FINAL_PDF_FILEAREA,
216 $grade->id);
217
218 // Also show the link to the read-only interface.
219 $renderer = $PAGE->get_renderer('assignfeedback_editpdf');
220 $widget = $this->get_widget($grade->userid, $grade, true);
221
222 $html .= $renderer->render($widget);
223 }
224 return $html;
225 }
226
227 /**
228 * Return true if there are no released comments/annotations.
229 *
230 * @param stdClass $grade
231 */
232 public function is_empty(stdClass $grade) {
233 global $DB;
234
235 $comments = $DB->count_records('assignfeedback_editpdf_cmnt', array('gradeid'=>$grade->id, 'draft'=>0));
236 $annotations = $DB->count_records('assignfeedback_editpdf_annot', array('gradeid'=>$grade->id, 'draft'=>0));
237 return $comments == 0 && $annotations == 0;
238 }
239
240 /**
241 * The assignment has been deleted - remove the plugin specific data
242 *
243 * @return bool
244 */
245 public function delete_instance() {
246 global $DB;
247 $grades = $DB->get_records('assign_grades', array('assignment'=>$this->assignment->id), '', 'id');
248 list($gradeids, $params) = $DB->get_in_or_equal(array_keys($grades), SQL_PARAMS_NAMED);
249 $DB->delete_records_select('assignfeedback_editpdf_annot', $gradeids, $params);
250 $DB->delete_records_select('assignfeedback_editpdf_cmnt', $gradeids, $params);
251 }
252
253}