-
Notifications
You must be signed in to change notification settings - Fork 28
/
upload_image.php
166 lines (142 loc) · 6.56 KB
/
upload_image.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
66
67
68
69
70
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
/**
* Upload image from users list in quizaccess_proctoring plugin.
*
* @package quizaccess_proctoring
* @copyright 2022 Brain Station 23 Ltd.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
require_once(__DIR__ . '/../../../../config.php');
require_once($CFG->dirroot . '/mod/quiz/accessrule/proctoring/classes/form/upload_image_form.php');
require_once($CFG->dirroot . '/mod/quiz/accessrule/proctoring/lib.php');
$PAGE->set_url(new moodle_url('/mod/quiz/accessrule/proctoring/upload_image.php'));
$PAGE->set_context(\context_system::instance());
$PAGE->set_title(get_string('upload_image_title', 'quizaccess_proctoring'));
require_login();
if (!is_siteadmin()) {
redirect($CFG->wwwroot, get_string('no_permission', 'quizaccess_proctoring'), null, \core\output\notification::NOTIFY_ERROR);
}
$userid = optional_param('id', -1, PARAM_INT);
// Instantiate imageupload_form.
$mform = new imageupload_form();
// Checking form.
if ($mform->is_cancelled()) {
redirect($CFG->wwwroot . '/mod/quiz/accessrule/proctoring/userslist.php',
get_string('cancel_image_upload', 'quizaccess_proctoring'),
null,
\core\output\notification::NOTIFY_INFO);
} else if ($data = $mform->get_data()) {
// Store or update $student.
file_save_draft_area_files(
$data->user_photo,
$data->context_id,
'quizaccess_proctoring',
'user_photo',
$data->id,
array('subdirs' => 0, 'maxfiles' => 50)
);
// Save the face image.
$faceimagefile = new stdClass();
$faceimagefile->filearea = 'face_image';
$faceimagefile->component = 'quizaccess_proctoring';
$faceimagefile->filepath = '';
$faceimagefile->itemid = $userid;
$faceimagefile->license = '';
$faceimagefile->author = '';
$context = context_system::instance();
$fs = get_file_storage();
$faceimagefile->filepath = file_correct_filepath($faceimagefile->filepath);
// For base64 to file.
$faceimagedata = $data->face_image;
list(, $faceimagedata) = explode(';', $faceimagedata);
// Get the face image url of admin uploaded image.
$url = quizaccess_proctoring_geturl_of_faceimage($faceimagedata, $userid, $faceimagefile, $context, $fs);
$facetablerecord = new stdClass();
$facetablerecord->parent_type = 'admin_image';
$facetablerecord->faceimage = "{$url}";
$facetablerecord->facefound = 1;
$facetablerecord->timemodified = time();
if ($DB->record_exists_select('proctoring_user_images', 'user_id = :id', array('id' => $data->id))) {
$record = $DB->get_record_select('proctoring_user_images', 'user_id = :id', array('id' => $data->id));
$record->photo_draft_id = $data->user_photo;
$DB->update_record('proctoring_user_images', $record);
// Save face image in face table.
$facetablerecord->parentid = $record->id;
if ($DB->record_exists('proctoring_face_images',
array('parentid' => $facetablerecord->parentid,
'parent_type' => $facetablerecord->parent_type))) {
$facetablerow = $DB->get_record('proctoring_face_images',
array('parentid' => $facetablerecord->parentid,
'parent_type' => $facetablerecord->parent_type));
$facetablerecord->id = $facetablerow->id;
$DB->update_record('proctoring_face_images', $facetablerecord);
} else {
$DB->insert_record('proctoring_face_images', $facetablerecord);
}
redirect($CFG->wwwroot . '/mod/quiz/accessrule/proctoring/userslist.php',
get_string('image_updated', 'quizaccess_proctoring'),
null,
\core\output\notification::NOTIFY_SUCCESS);
} else {
$record = new stdClass;
$record->user_id = $data->id;
$record->photo_draft_id = $data->user_photo;
$parentid = $DB->insert_record('proctoring_user_images', $record);
$facetablerecord->parentid = $parentid;
if ($DB->record_exists('proctoring_face_images',
array('parentid' => $facetablerecord->parentid,
'parent_type' => $facetablerecord->parent_type))) {
$facetablerow = $DB->get_record('proctoring_face_images',
array('parentid' => $facetablerecord->parentid,
'parent_type' => $facetablerecord->parent_type));
$facetablerecord->id = $facetablerow->id;
$DB->update_record('proctoring_face_images', $facetablerecord);
} else {
$DB->insert_record('proctoring_face_images', $facetablerecord);
}
redirect($CFG->wwwroot . '/mod/quiz/accessrule/proctoring/userslist.php',
get_string('image_updated', 'quizaccess_proctoring'),
null, \core\output\notification::NOTIFY_SUCCESS);
}
}
$context = context_system::instance();
$username = $DB->get_record_select('user', 'id=:id', array('id' => $userid), 'firstname ,lastname');
// Prepare image file.
if (empty($user->id)) {
$user = new stdClass;
$user->id = $userid;
$user->username = $username->firstname . ' ' . $username->lastname;
$user->context_id = $context->id;
$user->face_image = "";
}
$draftitemid = file_get_submitted_draft_itemid('user_photo');
file_prepare_draft_area(
$draftitemid,
$context->id,
'quizaccess_proctoring',
'user_photo',
$user->id,
array('subdirs' => 0, 'maxfiles' => 1)
);
$user->user_photo = $draftitemid;
$mform->set_data($user);
$modelurl = $CFG->wwwroot . '/mod/quiz/accessrule/proctoring/thirdpartylibs/models';
$PAGE->requires->js("/mod/quiz/accessrule/proctoring/amd/build/face-api.min.js", true);
$PAGE->requires->js_call_amd('quizaccess_proctoring/validateAdminUploadedImage', 'setup', array($modelurl));
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();