Gitlab CSE Unil

ajax.php 10.1 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
// 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/>.

/**
 * Process ajax requests
 *
 * @package assignfeedback_editpdfplus
M. Chardon's avatar
GPL3    
M. Chardon committed
22
 * @copyright  2016 Université de Lausanne
M. Chardon's avatar
init  
M. Chardon committed
23
24
25
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
use \assignfeedback_editpdfplus\document_services;
26
use \assignfeedback_editpdfplus\combined_document;
M. Chardon's avatar
init  
M. Chardon committed
27
28
29
30
31
32
33
34
35
use \assignfeedback_editpdfplus\page_editor;

define('AJAX_SCRIPT', true);

require('../../../../config.php');
require_once($CFG->dirroot . '/mod/assign/locallib.php');

require_sesskey();

36
37
38
39
const PLUGIN_NAME = "assignfeedback_editpdfplus";
const PERMISSION_ASSIGN_GRADE = "mod/assign:grade";
const PERMISSION_ASSIGN_SUBMIT = "mod/assign:submit";

M. Chardon's avatar
init  
M. Chardon committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
$action = optional_param('action', '', PARAM_ALPHANUM);
$assignmentid = required_param('assignmentid', PARAM_INT);
$userid = required_param('userid', PARAM_INT);
$attemptnumber = required_param('attemptnumber', PARAM_INT);
$readonly = optional_param('readonly', false, PARAM_BOOL);

$cm = \get_coursemodule_from_instance('assign', $assignmentid, 0, false, MUST_EXIST);
$context = \context_module::instance($cm->id);

$assignment = new \assign($context, null, null);

require_login($assignment->get_course(), false, $cm);

if (!$assignment->can_view_submission($userid)) {
    print_error('nopermission');
}

57
if ($action === 'pollconversions') {
M. Chardon's avatar
init  
M. Chardon committed
58
    $draft = true;
59
    if (!has_capability(PERMISSION_ASSIGN_GRADE, $context)) {
60
61
        // A student always sees the readonly version.
        $readonly = true;
M. Chardon's avatar
init  
M. Chardon committed
62
        $draft = false;
63
        require_capability(PERMISSION_ASSIGN_SUBMIT, $context);
M. Chardon's avatar
init  
M. Chardon committed
64
65
66
    }

    if ($readonly) {
67
        // Whoever is viewing the readonly version should not use the drafts, but the actual annotations.
M. Chardon's avatar
init  
M. Chardon committed
68
69
70
        $draft = false;
    }

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
    $response = (object) [
                'status' => -1,
                'filecount' => 0,
                'pagecount' => 0,
                'pageready' => 0,
                'pages' => [],
    ];

    $combineddocument = document_services::get_combined_document_for_attempt($assignment, $userid, $attemptnumber);
    $response->status = $combineddocument->get_status();
    $response->filecount = $combineddocument->get_document_count();

    if ($response->status === combined_document::STATUS_READY) {
        $combineddocument = document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
        $response->pagecount = $combineddocument->get_page_count();
    } else if ($response->status === combined_document::STATUS_COMPLETE || $response->status === combined_document::STATUS_FAILED) {
        $pages = document_services::get_page_images_for_attempt($assignment, $userid, $attemptnumber, $readonly);

        $response->pagecount = $combineddocument->get_page_count();

        $grade = $assignment->get_user_grade($userid, true, $attemptnumber);

        // The readonly files are stored in a different file area.
        $filearea = document_services::PAGE_IMAGE_FILEAREA;
        if ($readonly) {
            $filearea = document_services::PAGE_IMAGE_READONLY_FILEAREA;
        }
M. Chardon's avatar
init  
M. Chardon committed
98

99
100
101
102
        foreach ($pages as $id => $pagefile) {
            $index = count($response->pages);
            $page = new stdClass();
            //$comments = page_editor::get_comments($grade->id, $index, $draft);
103
            $page->url = moodle_url::make_pluginfile_url($context->id, PLUGIN_NAME, $filearea, $grade->id, '/', $pagefile->get_filename())->out();
104
105
106
107
108
109
110
111
112
113
114
115
            //$page->comments = $comments;
            if ($imageinfo = $pagefile->get_imageinfo()) {
                $page->width = $imageinfo['width'];
                $page->height = $imageinfo['height'];
            } else {
                $page->width = 0;
                $page->height = 0;
            }
            $annotations = page_editor::get_annotations($grade->id, $index, $draft);
            $page->annotations = $annotations;
            $response->pages[] = $page;

116
            $component = PLUGIN_NAME;
117
118
119
120
121
122
123
124
125
126
127
128
            $filearea = document_services::PAGE_IMAGE_FILEAREA;
            $filepath = '/';
            $fs = get_file_storage();
            $files = $fs->get_directory_files($context->id, $component, $filearea, $grade->id, $filepath);
            $response->pageready = count($files);

            $tools = page_editor::get_tools(null);
            $typetools = page_editor::get_typetools(null);
            $axis = page_editor::get_axis(null);
            $response->tools = $tools;
            $response->typetools = $typetools;
            $response->axis = $axis;
M. Chardon's avatar
init  
M. Chardon committed
129
130
131
132
133
134
        }
    }

    echo json_encode($response);
    die();
} else if ($action == 'savepage') {
135
    require_capability(PERMISSION_ASSIGN_GRADE, $context);
M. Chardon's avatar
init  
M. Chardon committed
136
137
138
139

    $response = new stdClass();
    $response->errors = array();

140
    $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
M. Chardon's avatar
init  
M. Chardon committed
141
142
143
144
145
146
147

    $pagejson = required_param('page', PARAM_RAW);
    $page = json_decode($pagejson);
    $index = required_param('index', PARAM_INT);

    $added = page_editor::set_annotations($grade->id, $index, $page->annotations);
    if ($added != count($page->annotations)) {
148
        array_push($response->errors, get_string('couldnotsavepage', PLUGIN_NAME, $index + 1));
M. Chardon's avatar
init  
M. Chardon committed
149
150
151
152
    }
    echo json_encode($response);
    die();
} else if ($action == 'generatepdf') {
153

154
    $refresh = optional_param('refresh', false, PARAM_BOOL);
M. Chardon's avatar
init  
M. Chardon committed
155

156
    if (!$refresh) {
157
        require_capability(PERMISSION_ASSIGN_GRADE, $context);
158
    } else {
159
        require_capability(PERMISSION_ASSIGN_SUBMIT, $context);
160
    }
M. Chardon's avatar
init  
M. Chardon committed
161
    $response = new stdClass();
162
    $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
M. Chardon's avatar
M. Chardon committed
163
    $file = document_services::generate_feedback_document($assignment, $userid, $attemptnumber, $refresh);
M. Chardon's avatar
init  
M. Chardon committed
164
165
166

    $response->url = '';
    if ($file) {
167
        $url = moodle_url::make_pluginfile_url($assignment->get_context()->id, PLUGIN_NAME, document_services::FINAL_PDF_FILEAREA, $grade->id, '/', $file->get_filename(), false);
M. Chardon's avatar
init  
M. Chardon committed
168
169
170
171
        $response->url = $url->out(true);
        $response->filename = $file->get_filename();
    }

172
    if ($refresh) {
173
        $teachers = get_users_by_capability($context, 'assignfeedback/editpdfplus:notify');
M. Chardon's avatar
M. Chardon committed
174
175
176
177
178
179
        $contextb = $assignment->get_context();
        $course = $assignment->get_course();
        $coursemodule = $assignment->get_course_module();
        $modulename = get_string('modulename', 'assign');
        $assignmentname = $assignment->get_instance()->name;
        $formatparams = array('context' => $contextb->get_course_context());
180
181
        $flecheHtml = ' → ';
        $retourChariot = "\n";
M. Chardon's avatar
M. Chardon committed
182
        $body = format_string($course->shortname, true, $formatparams)
183
                . $flecheHtml
184
                . $modulename
185
186
187
                . $flecheHtml
                . format_string($assignmentname, true, $formatparams) . $retourChariot
                . $retourChariot . "---------------------------------------------------------------------" . $retourChariot
M. Chardon's avatar
M. Chardon committed
188
                . "La correction du devoir a été mise à jour. Vous pouvez accéder au document en suivant ce lien : "
189
                . $response->url
190
                . $retourChariot . $retourChariot . "Ceci est un mail automatique.";
M. Chardon's avatar
M. Chardon committed
191
        $bodyhtml = '<p><font face="sans-serif">'
192
193
194
195
196
197
198
                . getHtmlLink($CFG->wwwroot . '/course/view.php?id=' . $course->id, format_string($course->shortname, true, $formatparams))
                . $flecheHtml
                . getHtmlLink($CFG->wwwroot . '/mod/assign/index.php?id=' . $course->id, $modulename)
                . $flecheHtml
                . getHtmlLink($CFG->wwwroot . '/mod/assign/view.php?id=' . $coursemodule->id, format_string($assignmentname, true, $formatparams))
                . '</font></p>'
                . '<hr/><font face="sans-serif">'
199
                . "<b>Information Moodle</b><br/>"
200
201
202
                . "<p>La correction du devoir a été mise à jour. Vous pouvez accéder au document en suivant ce "
                . getHtmlLink($response->url, "lien")
                . "</p>"
203
                . "<i>Ceci est un mail automatique, merci de ne pas y répondre.</i>";
204
205
206
207
208
        foreach ($teachers as $teacher) {
            $res = email_to_user($teacher, $USER, "[Moodle] Mise à jour devoir", $body, $bodyhtml);
        }
    }

M. Chardon's avatar
init  
M. Chardon committed
209
210
    echo json_encode($response);
    die();
M. Chardon's avatar
ménage    
M. Chardon committed
211
} else if ($action == 'revertchanges') {
212
    require_capability(PERMISSION_ASSIGN_GRADE, $context);
M. Chardon's avatar
init  
M. Chardon committed
213

214
    $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
M. Chardon's avatar
init  
M. Chardon committed
215
216
217
218
219

    $result = page_editor::revert_drafts($gradeid);

    echo json_encode($result);
    die();
M. Chardon's avatar
ménage    
M. Chardon committed
220
} else if ($action == 'deletefeedbackdocument') {
221
    require_capability(PERMISSION_ASSIGN_GRADE, $context);
M. Chardon's avatar
init  
M. Chardon committed
222

223
    $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
M. Chardon's avatar
init  
M. Chardon committed
224
225
226
227
228
    $result = document_services::delete_feedback_document($assignment, $userid, $attemptnumber);

    $result = $result && page_editor::unrelease_drafts($grade->id);
    echo json_encode($result);
    die();
229
} else if ($action == 'updatestudentview') {
230
    require_capability(PERMISSION_ASSIGN_SUBMIT, $context);
231
232
233
234
235
236
237
238
239
240

    $response = new stdClass();
    $response->errors = array();

    $grade = $assignment->get_user_grade($userid, true);

    $pagejson = required_param('page', PARAM_RAW);
    $page = json_decode($pagejson);
    $index = required_param('index', PARAM_INT);

241
    $added = page_editor::update_annotations_status($page->annotations);
242
    if ($added != count($page->annotations)) {
243
        array_push($response->errors, get_string('couldnotsavepage', PLUGIN_NAME, $index + 1));
244
245
246
    }
    echo json_encode($response);
    die();
M. Chardon's avatar
init  
M. Chardon committed
247
248
}

249
250
251
function getHtmlLink($url, $text) {
    return '<a href="' . $url . '">' . $text . '</a>';
}