Gitlab CSE Unil

ajax.php 9.48 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
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
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
use \assignfeedback_editpdfplus\document_services;
use \assignfeedback_editpdfplus\page_editor;
use \assignfeedback_editpdfplus\comments_quick_list;

define('AJAX_SCRIPT', true);

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

require_sesskey();

$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');
}

if ($action == 'loadallpages') {
    $draft = true;
    if (!has_capability('mod/assign:grade', $context)) {
        $draft = false;
        $readonly = true; // A student always sees the readonly version.
        require_capability('mod/assign:submit', $context);
    }

    // Whoever is viewing the readonly version should not use the drafts, but the actual annotations.
    if ($readonly) {
        $draft = false;
    }

66
    $pages = document_services::get_page_images_for_attempt($assignment, $userid, $attemptnumber, $readonly);
M. Chardon's avatar
init  
M. Chardon committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

    $response = new stdClass();
    $response->pagecount = count($pages);
    $response->pages = array();

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

    // 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;
    }

    foreach ($pages as $id => $pagefile) {
        $index = count($response->pages);
        $page = new stdClass();
        $comments = page_editor::get_comments($grade->id, $index, $draft);
84
        $page->url = moodle_url::make_pluginfile_url($context->id, 'assignfeedback_editpdfplus', $filearea, $grade->id, '/', $pagefile->get_filename())->out();
M. Chardon's avatar
init  
M. Chardon committed
85
86
87
88
89
90
91
92
93
94
95
96
97
        $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;
        array_push($response->pages, $page);
    }

98
99
100
    $tools = page_editor::get_tools(null);
    $typetools = page_editor::get_typetools(null);
    $axis = page_editor::get_axis(null);
101
    $response->tools = $tools;
M. Chardon's avatar
M. Chardon committed
102
    $response->typetools = $typetools;
M. Chardon's avatar
M. Chardon committed
103
    $response->axis = $axis;
104

M. Chardon's avatar
init  
M. Chardon committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    echo json_encode($response);
    die();
} else if ($action == 'savepage') {
    require_capability('mod/assign:grade', $context);

    $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);

    $added = page_editor::set_comments($grade->id, $index, $page->comments);
    if ($added != count($page->comments)) {
121
        array_push($response->errors, get_string('couldnotsavepage', 'assignfeedback_editpdfplus', $index + 1));
M. Chardon's avatar
init  
M. Chardon committed
122
123
124
    }
    $added = page_editor::set_annotations($grade->id, $index, $page->annotations);
    if ($added != count($page->annotations)) {
125
        array_push($response->errors, get_string('couldnotsavepage', 'assignfeedback_editpdfplus', $index + 1));
M. Chardon's avatar
init  
M. Chardon committed
126
127
128
129
    }
    echo json_encode($response);
    die();
} else if ($action == 'generatepdf') {
130
    $refresh = optional_param('refresh', false, PARAM_BOOL);
M. Chardon's avatar
init  
M. Chardon committed
131

132
133
134
135
136
    if (!$refresh) {
        require_capability('mod/assign:grade', $context);
    } else {
        require_capability('mod/assign:submit', $context);
    }
M. Chardon's avatar
init  
M. Chardon committed
137
138
    $response = new stdClass();
    $grade = $assignment->get_user_grade($userid, true);
M. Chardon's avatar
M. Chardon committed
139
    $file = document_services::generate_feedback_document($assignment, $userid, $attemptnumber, $refresh);
M. Chardon's avatar
init  
M. Chardon committed
140
141
142

    $response->url = '';
    if ($file) {
143
        $url = moodle_url::make_pluginfile_url($assignment->get_context()->id, 'assignfeedback_editpdfplus', document_services::FINAL_PDF_FILEAREA, $grade->id, '/', $file->get_filename(), false);
M. Chardon's avatar
init  
M. Chardon committed
144
145
146
147
        $response->url = $url->out(true);
        $response->filename = $file->get_filename();
    }

148
    if ($refresh) {
M. Chardon's avatar
M. Chardon committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
        $teachers = get_users_by_capability($context, 'mod/assignfeedback_editpdf:notify');
        $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());
        $body = format_string($course->shortname, true, $formatparams)
                . ' -> '
                . $modulename 
                . ' -> '
                . format_string($assignmentname, true, $formatparams) . "\n"
                . "\n---------------------------------------------------------------------\n"
                . "La correction du devoir a été mise à jour. Vous pouvez accéder au document en suivant ce lien : "
163
164
                . $response->url
                . "\n\nCeci est un mail automatique.";
M. Chardon's avatar
M. Chardon committed
165
166
167
168
169
170
171
172
173
174
175
176
        $bodyhtml = '<p><font face="sans-serif">'
                .'<a href="' . $CFG->wwwroot . '/course/view.php?id=' . $course->id . '">'
                .format_string($course->shortname, true, $formatparams)
                .'</a> ->'
                .'<a href="' . $CFG->wwwroot . '/mod/assign/index.php?id=' . $course->id . '">'
                .$modulename
                .'</a> ->' 
                .'<a href="' . $CFG->wwwroot . '/mod/assign/view.php?id=' . $coursemodule->id . '">'
                .format_string($assignmentname, true, $formatparams)
                .'</a></font></p>'
                .'<hr /><font face="sans-serif">'
                ."<b>Information Moodle</b><br/>"
177
178
179
                . "<p>La correction du devoir a été mise à jour. Vous pouvez accéder au document en suivant ce <a href='"
                . $response->url
                . "'>lien</a></p>"
M. Chardon's avatar
M. Chardon committed
180
                . "<i>Ceci est un mail automatique.</i>";
181
182
183
184
185
        foreach ($teachers as $teacher) {
            $res = email_to_user($teacher, $USER, "[Moodle] Mise à jour devoir", $body, $bodyhtml);
        }
    }

M. Chardon's avatar
init  
M. Chardon committed
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
    echo json_encode($response);
    die();
} else if ($action == 'loadquicklist') {
    require_capability('mod/assign:grade', $context);

    $result = comments_quick_list::get_comments();

    echo json_encode($result);
    die();
} else if ($action == 'addtoquicklist') {
    require_capability('mod/assign:grade', $context);

    $comment = required_param('commenttext', PARAM_RAW);
    $width = required_param('width', PARAM_INT);
    $colour = required_param('colour', PARAM_ALPHA);

    $result = comments_quick_list::add_comment($comment, $width, $colour);

    echo json_encode($result);
    die();
} else if ($action == 'revertchanges') {
    require_capability('mod/assign:grade', $context);

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

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

    echo json_encode($result);
    die();
} else if ($action == 'removefromquicklist') {
    require_capability('mod/assign:grade', $context);

    $commentid = required_param('commentid', PARAM_INT);

    $result = comments_quick_list::remove_comment($commentid);

    echo json_encode($result);
    die();
} else if ($action == 'deletefeedbackdocument') {
    require_capability('mod/assign:grade', $context);

    $grade = $assignment->get_user_grade($userid, true);
    $result = document_services::delete_feedback_document($assignment, $userid, $attemptnumber);

    $result = $result && page_editor::unrelease_drafts($grade->id);
    echo json_encode($result);
    die();
233
} else if ($action == 'updatestudentview') {
234
    require_capability('mod/assign:submit', $context);
235
236
237
238
239
240
241
242
243
244

    $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);

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