Gitlab CSE Unil

locallib.php 12.9 KB
Newer Older
M. Chardon's avatar
init  
M. Chardon committed
1
<?php
2

M. Chardon's avatar
init  
M. Chardon committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 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/>.

/**
 * This file contains the definition for the library class for PDF feedback plugin
 *
 *
 * @package   assignfeedback_editpdfplus
M. Chardon's avatar
GPL3    
M. Chardon committed
23
 * @copyright  2016 Université de Lausanne
M. Chardon's avatar
init  
M. Chardon committed
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
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
defined('MOODLE_INTERNAL') || die();

use \assignfeedback_editpdfplus\document_services;
use \assignfeedback_editpdfplus\page_editor;

/**
 * library class for editpdf feedback plugin extending feedback plugin base class
 *
 * @package   assignfeedback_editpdfplus
 * @copyright 2012 Davo Smith
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class assign_feedback_editpdfplus extends assign_feedback_plugin {

    /** @var boolean|null $enabledcache Cached lookup of the is_enabled function */
    private $enabledcache = null;

    /**
     * Get the name of the file feedback plugin
     * @return string
     */
    public function get_name() {
        return get_string('pluginname', 'assignfeedback_editpdfplus');
    }

    /**
     * Create a widget for rendering the editor.
     *
     * @param int $userid
     * @param stdClass $grade
     * @param bool $readonly
     * @return assignfeedback_editpdfplus_widget
     */
    public function get_widget($userid, $grade, $readonly) {
        $attempt = -1;
        if ($grade && $grade->attemptnumber) {
            $attempt = $grade->attemptnumber;
        } else {
            $grade = $this->assignment->get_user_grade($userid, true);
        }

67
        $feedbackfile = document_services::get_feedback_document($this->assignment->get_instance()->id, $userid, $attempt);
M. Chardon's avatar
init  
M. Chardon committed
68
69
70
71
72

        $stampfiles = array();
        $fs = get_file_storage();
        $syscontext = context_system::instance();

73
74
75
76
77
        // get the costum toolbars
        $toolbars = array();
        $coursecontext = context::instance_by_id($this->assignment->get_context()->id);
        $coursecontexts = array_filter(explode('/', $coursecontext->path), 'strlen');
        $tools = page_editor::get_tools($coursecontexts);
M. Chardon's avatar
M. Chardon committed
78
79
80
81
82
83
84
85
86
        $axis = page_editor::get_axis($coursecontexts);
        foreach ($axis as $ax) {
            $toolbars[$ax->id]['label'] = $ax->label;
            foreach ($tools as $tool) {
                if ($tool->axis == $ax->id) {
                    $toolbars[$ax->id]['tool'][$tool->id] = $tool;
                }
            }
        }
87

M. Chardon's avatar
init  
M. Chardon committed
88
        // Copy any new stamps to this instance.
89
        if ($files = $fs->get_area_files($syscontext->id, 'assignfeedback_editpdfplus', 'stamps', 0, "filename", false)) {
M. Chardon's avatar
init  
M. Chardon committed
90
91
92
93
            foreach ($files as $file) {
                $filename = $file->get_filename();
                if ($filename !== '.') {

94
                    $existingfile = $fs->get_file($this->assignment->get_context()->id, 'assignfeedback_editpdfplus', 'stamps', $grade->id, '/', $file->get_filename());
M. Chardon's avatar
init  
M. Chardon committed
95
96
97
98
99
100
101
102
103
104
105
                    if (!$existingfile) {
                        $newrecord = new stdClass();
                        $newrecord->contextid = $this->assignment->get_context()->id;
                        $newrecord->itemid = $grade->id;
                        $fs->create_file_from_storedfile($newrecord, $file);
                    }
                }
            }
        }

        // Now get the full list of stamp files for this instance.
106
        if ($files = $fs->get_area_files($this->assignment->get_context()->id, 'assignfeedback_editpdfplus', 'stamps', $grade->id, "filename", false)) {
M. Chardon's avatar
init  
M. Chardon committed
107
108
109
            foreach ($files as $file) {
                $filename = $file->get_filename();
                if ($filename !== '.') {
110
                    $url = moodle_url::make_pluginfile_url($this->assignment->get_context()->id, 'assignfeedback_editpdfplus', 'stamps', $grade->id, '/', $file->get_filename(), false);
M. Chardon's avatar
init  
M. Chardon committed
111
112
113
114
115
116
117
118
                    array_push($stampfiles, $url->out());
                }
            }
        }

        $url = false;
        $filename = '';
        if ($feedbackfile) {
119
120
            $url = moodle_url::make_pluginfile_url($this->assignment->get_context()->id, 'assignfeedback_editpdfplus', document_services::FINAL_PDF_FILEAREA, $grade->id, '/', $feedbackfile->get_filename(), false);
            $filename = $feedbackfile->get_filename();
M. Chardon's avatar
init  
M. Chardon committed
121
122
        }

123
        $widget = new assignfeedback_editpdfplus_widget($this->assignment->get_instance()->id, $userid, $attempt, $url, $filename, $stampfiles, $readonly, $toolbars, $axis);
M. Chardon's avatar
init  
M. Chardon committed
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
        return $widget;
    }

    /**
     * Get form elements for grading form
     *
     * @param stdClass $grade
     * @param MoodleQuickForm $mform
     * @param stdClass $data
     * @param int $userid
     * @return bool true if elements were added to the form
     */
    public function get_form_elements_for_user($grade, MoodleQuickForm $mform, stdClass $data, $userid) {
        global $PAGE;

        $attempt = -1;
        if ($grade) {
            $attempt = $grade->attemptnumber;
        }

        $renderer = $PAGE->get_renderer('assignfeedback_editpdfplus');

        $widget = $this->get_widget($userid, $grade, false);

        $html = $renderer->render($widget);
        $mform->addElement('static', 'editpdf', get_string('editpdf', 'assignfeedback_editpdfplus'), $html);
        $mform->addHelpButton('editpdf', 'editpdf', 'assignfeedback_editpdfplus');
        $mform->addElement('hidden', 'editpdf_source_userid', $userid);
        $mform->setType('editpdf_source_userid', PARAM_INT);
        $mform->setConstant('editpdf_source_userid', $userid);
    }

    /**
     * Check to see if the grade feedback for the pdf has been modified.
     *
     * @param stdClass $grade Grade object.
     * @param stdClass $data Data from the form submission (not used).
     * @return boolean True if the pdf has been modified, else false.
     */
    public function is_feedback_modified(stdClass $grade, stdClass $data) {
        // We only need to know if the source user's PDF has changed. If so then all
        // following users will have the same status. If it's only an individual annotation
        // then only one user will come through this method.
        // Source user id is only added to the form if there was a pdf.
        if (!empty($data->editpdf_source_userid)) {
            $sourceuserid = $data->editpdf_source_userid;
            // Retrieve the grade information for the source user.
            $sourcegrade = $this->assignment->get_user_grade($sourceuserid, true, $grade->attemptnumber);
            $pagenumbercount = document_services::page_number_for_attempt($this->assignment, $sourceuserid, $sourcegrade->attemptnumber);
            for ($i = 0; $i < $pagenumbercount; $i++) {
                // Select all annotations.
                $draftannotations = page_editor::get_annotations($sourcegrade->id, $i, true);
                $nondraftannotations = page_editor::get_annotations($grade->id, $i, false);
                // Check to see if the count is the same.
                if (count($draftannotations) != count($nondraftannotations)) {
                    // The count is different so we have a modification.
                    return true;
                } else {
                    $matches = 0;
                    // Have a closer look and see if the draft files match all the non draft files.
                    foreach ($nondraftannotations as $ndannotation) {
                        foreach ($draftannotations as $dannotation) {
                            foreach ($ndannotation as $key => $value) {
                                if ($key != 'id' && $value != $dannotation->{$key}) {
                                    continue 2;
                                }
                            }
                            $matches++;
                        }
                    }
                    if ($matches !== count($nondraftannotations)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * Generate the pdf.
     *
     * @param stdClass $grade
     * @param stdClass $data
     * @return bool
     */
    public function save(stdClass $grade, stdClass $data) {
        // Source user id is only added to the form if there was a pdf.
        if (!empty($data->editpdf_source_userid)) {
            $sourceuserid = $data->editpdf_source_userid;
            // Copy drafts annotations and comments if current user is different to sourceuserid.
            if ($sourceuserid != $grade->userid) {
                page_editor::copy_drafts_from_to($this->assignment, $grade, $sourceuserid);
            }
        }
        if (page_editor::has_annotations_or_comments($grade->id, true)) {
            document_services::generate_feedback_document($this->assignment, $grade->userid, $grade->attemptnumber);
        }

        return true;
    }

    /**
     * Display the list of files in the feedback status table.
     *
     * @param stdClass $grade
     * @param bool $showviewlink (Always set to false).
     * @return string
     */
    public function view_summary(stdClass $grade, & $showviewlink) {
        $showviewlink = false;
        return $this->view($grade);
    }

    /**
     * Display the list of files in the feedback status table.
     *
     * @param stdClass $grade
     * @return string
     */
    public function view(stdClass $grade) {
        global $PAGE;
246
        
M. Chardon's avatar
init  
M. Chardon committed
247
248
249
        $html = '';
        // Show a link to download the pdf.
        if (page_editor::has_annotations_or_comments($grade->id, false)) {
250
251
            $html = $this->assignment->render_area_files('assignfeedback_editpdfplus', document_services::FINAL_PDF_FILEAREA, $grade->id);
            //debugging('tutu');
M. Chardon's avatar
init  
M. Chardon committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
            // Also show the link to the read-only interface.
            $renderer = $PAGE->get_renderer('assignfeedback_editpdfplus');
            $widget = $this->get_widget($grade->userid, $grade, true);

            $html .= $renderer->render($widget);
        }
        return $html;
    }

    /**
     * Return true if there are no released comments/annotations.
     *
     * @param stdClass $grade
     */
    public function is_empty(stdClass $grade) {
        global $DB;

269
        $annotations = $DB->count_records('assignfeedback_editpp_annot', array('gradeid' => $grade->id, 'draft' => 0));
270
        return $annotations == 0;
M. Chardon's avatar
init  
M. Chardon committed
271
272
273
274
275
276
277
278
279
    }

    /**
     * The assignment has been deleted - remove the plugin specific data
     *
     * @return bool
     */
    public function delete_instance() {
        global $DB;
280
        $grades = $DB->get_records('assign_grades', array('assignment' => $this->assignment->get_instance()->id), '', 'id');
M. Chardon's avatar
init  
M. Chardon committed
281
282
        if ($grades) {
            list($gradeids, $params) = $DB->get_in_or_equal(array_keys($grades), SQL_PARAMS_NAMED);
283
            $DB->delete_records_select('assignfeedback_editpp_annot', 'gradeid ' . $gradeids, $params);
M. Chardon's avatar
init  
M. Chardon committed
284
285
286
287
288
        }
        return true;
    }

    /**
M. Chardon's avatar
M. Chardon committed
289
     * Automatically enable or disable editpdfplus feedback plugin based on
M. Chardon's avatar
init  
M. Chardon committed
290
291
292
293
294
     * whether the ghostscript path is set correctly.
     *
     * @return bool
     */
    public function is_enabled() {
295
296
297
        if ($this->enabledcache === null) {
            $testpath = assignfeedback_editpdfplus\pdf::test_gs_path(false);
            if ($this->assignment->get_context()) {
298
                $this->enabledcache = ($testpath->status == assignfeedback_editpdfplus\pdf::GSPATH_OK) && has_capability('mod/assignfeedback_editpdfplus:use', $this->assignment->get_context(), null, false);
299
300
301
            } else {
                $this->enabledcache = false;
            }
M. Chardon's avatar
init  
M. Chardon committed
302
        }
303
        return $this->enabledcache;
M. Chardon's avatar
init  
M. Chardon committed
304
    }
305

M. Chardon's avatar
init  
M. Chardon committed
306
307
308
309
310
311
    /**
     * Automatically hide the setting for the editpdf feedback plugin.
     *
     * @return bool false
     */
    public function is_configurable() {
312
        return false;
M. Chardon's avatar
init  
M. Chardon committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
    }

    /**
     * Get file areas returns a list of areas this plugin stores files.
     *
     * @return array - An array of fileareas (keys) and descriptions (values)
     */
    public function get_file_areas() {
        return array(document_services::FINAL_PDF_FILEAREA => $this->get_name());
    }

    /**
     * This plugin will inject content into the review panel with javascript.
     * @return bool true
     */
    public function supports_review_panel() {
        return true;
    }
331

332
333
334
335
336
337
338
339
340
341
    /**
     * Return the plugin configs for external functions.
     *
     * @return array the list of settings
     * @since Moodle 3.2
     */
    public function get_config_for_external() {
        return (array) $this->get_config();
    }

M. Chardon's avatar
init  
M. Chardon committed
342
}