This repository was archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathview.php
More file actions
executable file
·373 lines (310 loc) · 12.3 KB
/
view.php
File metadata and controls
executable file
·373 lines (310 loc) · 12.3 KB
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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
// 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/>.
/**
* @package block_filescan
* @copyright 2018 Swarthmore College ITS
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
defined('MOODLE_INTERNAL') || die;
global $DB;
// Check for all required variables.
$courseid = required_param('courseid', PARAM_INT);
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('invalidcourse', 'block_filescan', $courseid);
}
require_login($course);
$PAGE->set_url('/block/filescan/view.php', array('id' => $courseid));
$PAGE->set_pagelayout('standard');
$PAGE->set_heading(get_string('viewdetailspage', 'block_filescan'));
global $COURSE, $CFG, $DB, $OUTPUT, $PAGE;
require_once($CFG->dirroot . '/course/lib.php');
$fs = get_file_storage();
$context = context_course::instance($COURSE->id);
$canview = has_capability('block/filescan:viewpages', $context);
// If the role can't view the block, return an empty string for the content
// and exit (blocks with no content aren't shown)
if (!$canview) {
echo get_string('cannot_view', 'block_filescan');
return;
}
// Clear any existing cache data for this course
$cache = cache::make('block_filescan', 'filescan');
$filescan_cache = $cache->delete($COURSE->id);
// Determine course metadata
$coursename = $COURSE->fullname;
$courseshortname = $COURSE->shortname;
$courseurl = course_get_url($COURSE);
$filenames = "";
$output_html = "";
$file_listing = [];
$cmid = null;
// Get any existing data from db
$cms = ($cmid === null) ? get_fast_modinfo($COURSE)->get_cms() : array(get_fast_modinfo($COURSE)->get_cm($cmid));
$file_list = array(); // Array containing detailed file info
// Loop through each module in the course looking for files
foreach ($cms as $cm) {
if ($cm->is_user_access_restricted_by_capability()) {
continue;
}
$cmtype = $cm->modname;
$module_name = $cm->name;
$section_no = $cm->get_course_module_record(true)->sectionnum;
$sectionName = get_section_name($COURSE->id, $section_no);
// if resource is a folder
if ($cmtype === 'folder') {
$cmfiles = $fs->get_area_files($cm->context->id, 'mod_folder', 'content', false, 'timemodified', false);
// Loop through all files in a folder
foreach ($cmfiles as $f) {
if (isset($f) && ($f->get_mimetype() === 'application/pdf')) {
$file_item = create_file_item($DB, $cm, $cmtype, $module_name, $sectionName, $section_no, $f);
array_push($file_list, $file_item);
}
}
} else if ($cmtype === 'resource') { // if resource is a file.
// Get files in "file" resource
$files = $fs->get_area_files($cm->context->id, 'mod_resource', 'content', false, 'timemodified', false);
// Loop through each file
foreach ($files as $f) {
if (isset($f) && ($f->get_mimetype() === 'application/pdf')) {
//$status = check_db_for_file($f->get_contenthash(), $f);
$file_item = create_file_item($DB, $cm, $cmtype, $module_name, $sectionName, $section_no, $f);
array_push($file_list, $file_item);
}
}
}
}
// Loop through PDF listing to format the output into a table
$success_icon = '<i class="fa fa-check text-success fa-fw" aria-hidden="true"></i>';
$unknown_icon = '<i class="fa fa-question text-info fa-fw" aria-hidden="true"></i>';
$partial_success_icon = '<i class="fa fa-exclamation text-warning fa-fw" aria-hidden="true"></i>';
$fail_icon = '<i class="fa fa-times text-danger fa-fw" aria-hidden="true"></i>';
$previous_section_number = "";
$output_html .= "<table class='filescan-details table table-striped table-condensed'><tbody>";
function get_help_icon($header, $link, $title)
{
global $OUTPUT;
$o = "<a href='$link' title='$title' aria-label='$header: $title' target='_blank'>";
$o .= "<img class='icon iconhelp' alt='' aria-hidden='true' src='" . $OUTPUT->image_url('help') . "'></a>";
return $o;
}
function get_table_header($id)
{
// Params.
$header = get_string("table:{$id}_header", 'block_filescan');
$link = get_config('block_filescan', $id . '_help');
$title = get_string('helptitle', 'block_filescan');
// Output.
$o = "<th class='fs-table-header fs-table-header-$id'>$header";
if (!empty($link)) {
$o .= get_help_icon($header, $link, $title);
}
$o .= '</th>';
return $o;
}
foreach ($file_list as $f) {
if ($f['sectionNumber'] != $previous_section_number) {
$output_html .= "<tr><td colspan='7' style='background-color: transparent;border:none;'><h4>" . $f['sectionName'] . "</h4></td></tr>";
$output_html .= "<tr><th style=''>" . get_string('table:mod_header', 'block_filescan') . '</th>';
$output_html .= '<th>' . get_string('table:filename_header', 'block_filescan') . '</th>';
$output_html .= '<th>' . get_string('table:status_header', 'block_filescan') . '</th>';
$output_html .= get_table_header('text_check');
$output_html .= get_table_header('title_check');
$output_html .= get_table_header('lang_check');
$output_html .= get_table_header('outline_check');
$output_html .= "</tr>";
}
switch ($f['status']['hastext']) {
case -1:
$hastext = "❗";
break;
case "1":
$hastext = $success_icon; // Yes
break;
case "0":
$hastext = $fail_icon; // No
break;
default:
$hastext = $unknown_icon; // Unknown
}
switch ($f['status']['hastitle']) {
case -1:
$hastitle = "❗";
break;
case "1":
$hastitle = $success_icon; // Yes
break;
case "0":
$hastitle = $fail_icon; // No
break;
default:
$hastitle = $unknown_icon; // Unknown
}
switch ($f['status']['haslanguage']) {
case -1:
$haslanguage = "❗";
break;
case "1":
$haslanguage = $success_icon; // Yes
break;
case "0":
$haslanguage = $fail_icon; // No
break;
default:
$haslanguage = $unknown_icon; // Unknown
}
switch ($f['status']['hasoutline']) {
case -1:
$hasoutline = "❗";
break;
case "1":
$hasoutline = $success_icon; // Yes
break;
case "0":
$hasoutline = $fail_icon; // No
break;
default:
$hasoutline = $unknown_icon; // Unknown
}
# Determine overall status
switch ($f['status']['status']) {
case "fail":
$overallstatus = $fail_icon; // Bad
break;
case "pass":
$overallstatus = $success_icon; // Good
break;
case "check":
$overallstatus = $partial_success_icon; // check
break;
default:
$overallstatus = $unknown_icon; // Unknown
}
// Check to see if this is a folder
if ($f['mod_type'] == 'folder') {
$mod_link = "<a href=\"" . $f['mod_url'] . "\" title=\"" . $f['mod_name'] . "\">📂</a>";
} else {
$mod_link = "<a href=\"" . $f['mod_url'] . "\" title=\"" . $f['mod_name'] . "\">📄</a>";
}
$output_html .= "<tr><td style='text-align:center'>" . $mod_link . "<td><a href=\"" . $f['fileurl'] . "\" target='_blank'>" . $f['filename'] . "</a></td>
<td style='text-align:center;'><span title=\"" . get_string('table:status_header', 'block_filescan') . '">' . $overallstatus . "</span></td>
<td style='text-align:center;border-left-width:2px;'><span title=\"" . get_string('table:text_check_header', 'block_filescan') . '">' . $hastext . "</span></td>
<td style='text-align:center'><span title=\"" . get_string('table:title_check_header', 'block_filescan') . '">' . $hastitle . "</span></td>
<td style='text-align:center'><span title=\"" . get_string('table:lang_check_header', 'block_filescan') . '">' . $haslanguage . "</span></td>
<td style='text-align:center'><span title=\"" . get_string('table:outline_check_header', 'block_filescan') . '">' . $hasoutline . "</span></td>
</tr>";
$previous_section_number = $f['sectionNumber'];
}
$output_html .= "</tbody></table>"; // Close out last table on last section
// Calculate summary information
$processing = false; // Assume no files are still processing.
$ocr_summary = array('Accessible' => 0, 'Partially Accessible' => 0, 'Inaccessible' => 0, 'Pending' => 0, 'Error' => 0, 'Unknown' => 0);
foreach ($file_list as $f) {
switch ($f['status']['status']) {
case "pending":
$ocr_summary['Pending']++;
break;
case "error":
$ocr_summary['Error']++;
break;
case "pass":
$ocr_summary['Accessible']++;
break;
case "fail":
$ocr_summary['Inaccessible']++;
break;
case "check":
$ocr_summary['Partially Accessible']++;
break;
default:
$ocr_summary['Unknown']++;
}
}
// Check to see if there are any files still processing and update status flag as needed
if (array_key_exists ('pending', $ocr_summary) && $ocr_summary['pending'] > 0) {
$processing = true;
}
$ocr_summary_array = array();
foreach (array_keys($ocr_summary) as $s) {
if ($ocr_summary[$s] > 0) {
array_push($ocr_summary_array, $ocr_summary[$s] . " " . $s);
}
}
$summary_text = count($file_list) . " total PDF files";
if ($ocr_summary['Accessible'] > 0) {
$summary_text .= '<BR>' . $success_icon . get_string('summary:files_accessible', 'block_filescan', $ocr_summary['Accessible']);
}
if ($ocr_summary['Partially Accessible'] > 0) {
$summary_text .= '<BR>' . $partial_success_icon . get_string('summary:files_partially_accessible', 'block_filescan', $ocr_summary['Partially Accessible']);
}
if ($ocr_summary['Inaccessible'] > 0) {
$summary_text .= '<BR>' . $fail_icon . get_string('summary:files_inaccessible', 'block_filescan', $ocr_summary['Inaccessible']);
}
if ($ocr_summary['Unknown'] > 0) {
$summary_text .= '<BR>' . $unknown_icon . get_string('summary:files_accessibility_unknown', 'block_filescan', $ocr_summary['Unknown']);
}
// Prepend summary to top of content
$output_html = "<h1>" . get_string('summary:title', 'block_filescan') . "</h1>" . $summary_text . "<BR><BR>" . $output_html;
echo $OUTPUT->header();
echo $output_html;
echo $OUTPUT->footer();
// From https://github.com/omer-ilhan/moodle-block_files/blob/master/block_files.php
/**
* Create a file item.
*
* @param $coursename string name of the course this file item belongs to
* @param $courseshortname string short name of the course this file item belongs to
* @param $courseurl url of the course this file item belongs to
* @param $cm course module this file item belongs to
* @param $cmfile the actual file
* @return array describing a file item, used to create table items
*/
function create_file_item($DB, $cm, $cmtype, $mod_name, $sectionName, $section_no, $file)
{
// See if there is an file scan entry for this file
$results = $DB->get_record("block_filescan_files", array('contenthash' => $file->get_contenthash()), $fields = '*');
// Assume no status -- correct if it exists
if ($results) {
$status = array(
"checked" => ($results->checked),
"status" => ($results->status),
"hastext" => ($results->hastext),
"hastitle" => ($results->hastitle),
"haslanguage" => ($results->haslanguage),
"hasoutline" => ($results->hasoutline),
"pagecount" => ($results->pagecount)
);
} else {
$status = null;
}
return array(
'mod_id' => $cm->id,
'mod_type' => $cmtype,
'mod_name' => $mod_name,
'mod_url' => $cm->url,
'filename' => $cm->get_formatted_name(),
'fileurl' => moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()), //$cm->context->get_url(),
'sectionName' => $sectionName,
'sectionNumber' => $section_no,
'timemodified' => $file->get_timemodified(),
'filename' => $file->get_filename(),
'contenthash' => $file->get_contenthash(),
'filepath' => $file->get_filepath(),
'status' => $status,
'file' => $file
);
}
?>