Fixes to the PaintWeb cron task.
[moodle/mihaisucan.git] / admin / cron_paintweb.php
blob70346127f22f9a9b60eab9cd27408c77664b6b0c
1 <?php
3 /**
4 * Perform the cleanup task for removing any obsolete images saved by PaintWeb.
6 * @uses $CFG
7 */
8 function paintweb_cron_cleanup () {
9 global $CFG;
11 if (empty($CFG->paintwebImagesFolder)) {
12 $CFG->paintwebImagesFolder = 'paintweb_images';
15 $pwtable = 'paintweb_images';
16 $pwfolder = $CFG->dataroot . '/' . $CFG->paintwebImagesFolder;
18 // Get the list of tables being tracked, known to have records with images
19 // saved by PaintWeb.
20 $tables = get_fieldset_select($pwtable, 'DISTINCT(tbl)');
21 if (!$tables) {
22 $tables = array();
25 // Delete obsolete images in the PaintWeb table.
26 foreach ($tables as $tbl) {
27 delete_records_select($pwtable, "NOT EXISTS (SELECT b.id FROM
28 {$CFG->prefix}$tbl AS b WHERE {$CFG->prefix}$pwtable.tbl='$tbl' AND
29 {$CFG->prefix}$pwtable.rid=b.id)");
32 // Get the list of image file names being tracked in the PaintWeb table.
33 $tblimages = get_fieldset_select($pwtable, 'DISTINCT(img)');
34 if (!$tblimages) {
35 $tblimages = array();
38 $cwd = getcwd();
39 chdir($pwfolder);
41 // Get the list of image file names inside the physical folder of PaintWeb.
42 $images = glob("*.{png,jpg}", GLOB_BRACE);
43 if (!$images) {
44 $images = array();
47 // Remove any file which is not in the database.
48 foreach ($images as $img) {
49 if (!in_array($img, $tblimages)) {
50 unlink($img);
54 chdir($cwd);
58 // vim:set spell spl=en fo=tanqrowcb tw=80 ts=4 sw=4 sts=4 sta et noai nocin fenc=utf-8 ff=unix: