-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
278 lines (205 loc) · 7.19 KB
/
locallib.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
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
<?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/>.
/**
* Booktool_validator module local lib functions
*
* @package booktool_validator
* @copyright 2014 Ivana Skelic, Hrvoje Golcic
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once(dirname(__FILE__).'/lib.php');
require_once($CFG->libdir.'/filelib.php');
require_once($CFG->dirroot.'/mod/book/edit_form.php');
/**
* Returns integer value 1 (true) if given chapter of book has
* no validation faults or 0 (false) if there are validations faults
*
* @param stdClass $book->id
* @param $chapterid
* @return int
*/
function chapter_checkvalidation($bookid, $chapterid) {
global $DB;
$query = $DB->get_field('book_chapters', 'content', array('id'=>$chapterid, 'bookid'=>$bookid));
$content = serialize($query);
//setp alt regular expression and run
$alt_pat = '/<img(\s*(?!alt)([\w\-])+=([\"])[^\"]+\3)*\s*\/?>/i'; //counts ones that don't have alt tag
preg_match_all($alt_pat, $content, $img_pregmatch);
$image_pregmatch_cnt = count($img_pregmatch[0]);
//set regular expressions for table and run
$summ_pat = '/<table(\s*(?!summary)([\w\-])+=([\"])[^\"]+\3)*\s*\/?>/i'; //counts ones that don't have summary tag
preg_match_all($summ_pat, $content, $table_pregmatch);
$table_pregmatch_cnt = count($table_pregmatch[0]);
if ($image_pregmatch_cnt == 0 && $table_pregmatch_cnt == 0) {
return 1; //true, there are no validation faults
} else {
return 0; //false, there are validation faults
}
}
/**
* Get chapter name.
*
* Finds and returns chapter name for given parameters.
*
* @param $book->id
* @param $chapterid
* @return array
*/
function chapter_getname($book, $chapterid) {
global $DB;
$query = 'SELECT title FROM {book_chapters} WHERE id = ? AND bookid = ?';
$params = array($chapterid, $book->id);
$query_result = $DB->get_record_sql($query, $params);
if (!$query_result) {
return false;
}
return $query_result->title;
}
function number_od_faults($bookid, $chapterid) {
global $DB;
$query = 'SELECT faults FROM {booktoool_validator} WHERE chapterid = ? AND bookid = ?';
$query_result = $DB->get_records($query, $chapterid, $book->id);
if (!$query_result) {
return;
}
return $query_result;
}
/**
* Counts number of images that lack alt attribute and number of tables that lack
* summarry attribute. Returns number of faults.
*
* @param stdClass $book->id
* @param $chapterid
* @return int
*/
function cnt_faults($bookid, $chapterid) {
global $DB;
$query = $DB->get_field('book_chapters', 'content', array('id'=>$chapterid, 'bookid'=>$bookid));
$content = serialize($query);
//set regular expressions for image and run
$alt_pat = '/<img(\s*(?!alt)([\w\-])+=([\"])[^\"]+\3)*\s*\/?>/i';;
preg_match_all($alt_pat, $content, $img_alt_pregmatch);
//set regular expressions for table and run
$summ_pat = '/<table(\s*(?!summary)([\w\-])+=([\"])[^\"]+\3)*\s*\/?>/i';
preg_match_all($summ_pat, $content, $table_summ_pregmatch);
$nof = (count($img_alt_pregmatch[0]) + count($table_summ_pregmatch[0]));
return $nof;
}
/**
* Finds and prints images that lack alt attribute for given arguments
*
* @param stdClass $book->id
* @param $chapterid
* @return
*/
function print_images($bookid, $chapterid) {
global $DB;
$query = $DB->get_field('book_chapters', 'content', array('id'=>$chapterid, 'bookid'=>$bookid));
$content = serialize($query);
$alt_pat = '/<img(\s*(?!alt)([\w\-])+=([\"])[^\"]+\3)*\s*\/?>/i';;
preg_match_all($alt_pat, $content, $img_alt_pregmatch);
if (count($img_alt_pregmatch[0]) != 0) {
echo get_string('image','booktool_validator');
echo "<br> <br>";
foreach($img_alt_pregmatch[0] as $print) {
echo $print . "<br>";
}
}
}
/**
* Echo tables that lack summary attribute
*
* Finds and prints tables that lack summary attribute for given arguments
*
* @param stdClass $book->id
* @param $chapterid
* @return
*/
function print_tables($bookid, $chapterid) {
global $DB;
$query = $DB->get_field('book_chapters', 'content', array('id'=>$chapterid, 'bookid'=>$bookid));
//$content = serialize($query);
$table_pattern = '/<table(.*?)>.*?<\/table>/s'; //regular expression for table tag search
preg_match_all($table_pattern, $query, $table_pregmatch);
foreach($table_pregmatch[0] as $child) {
if(strpos($child, "<table ") !== FALSE && strpos($child, "summary=") == FALSE) {
echo get_string('table', 'booktool_validator');
echo "<br> <br>";
echo $child . "<br>";
}
}
}
/**
* Finds and prints all images and their alt attribute
*
* @param stdClass $book->id
* @param $chapterid
* @return
*/
function find_images($bookid, $chapterid) {
global $DB;
$query = $DB->get_field('book_chapters', 'content', array('id'=>$chapterid, 'bookid'=>$bookid));
$content = serialize($query);
$img_pat = '/<img[^>]+>/i'; //regular expression for image tag search
preg_match_all($img_pat, $query, $img_pregmatch);
if (!empty($img_pregmatch[0])) {
foreach($img_pregmatch[0] as $image) {
echo $image . "<br>"; //echoes image
$alt_pat = '/(alt)=("[^"]*")/i';
preg_match_all($alt_pat, $image, $alt_pregmatch);
if (empty($alt_pregmatch[0])) {
echo '<b>' . get_string('image', 'booktool_validator') . '</b>';
}
foreach ($alt_pregmatch[2] as $alt) {
echo $alt; //echoes alt
echo '<br>' . '<b>' . get_string('words','booktool_validator') . ': </b>' . str_word_count($alt) .'<br>';
}
}
} else {
echo get_string('no_images','booktool_validator');
}
}
/**
* Finds and prints all tables and their summary attribute
*
* @param stdClass $book->id
* @param $chapterid
* @return
*/
function find_tables($bookid, $chapterid) {
global $DB;
$query = $DB->get_field('book_chapters', 'content', array('id'=>$chapterid, 'bookid'=>$bookid));
//$content = serialize($query);
$table_pat = '/<table(.*?)>.*?<\/table>/s'; //regular expression for table tag search
preg_match_all($table_pat, $query, $table_pregmatch);
if (!empty($table_pregmatch[0])) {
foreach($table_pregmatch[0] as $table) {
echo $table . "<br>"; //echoes table
$summ_pat = '/(summary)=("[^"]*")/i';
preg_match_all($summ_pat, $table, $summ_pregmatch);
if (empty($summ_pregmatch[0])) {
echo '<b>' . get_string('table', 'booktool_validator') . '</b>';
}
foreach ($summ_pregmatch[2] as $summary) {
echo $summary; //echoes summary
echo '<br>' . '<b>' . get_string('words','booktool_validator') . ': </b>' . str_word_count($summary) .'<br>';
}
}
} else {
echo get_string('no_tables','booktool_validator');
}
}