Esempio n. 1
0
 public static function bulk_delete($artefactids)
 {
     global $USER;
     require_once 'group.php';
     if (empty($artefactids)) {
         return;
     }
     $idstr = join(',', array_map('intval', $artefactids));
     db_begin();
     // Get the size of all the files we're about to delete that belong to
     // the user.
     if ($group = group_current_group()) {
         $totalsize = get_field_sql('
             SELECT SUM(size)
             FROM {artefact_file_files} f JOIN {artefact} a ON f.artefact = a.id
             WHERE a.group = ? AND f.artefact IN (' . $idstr . ')', array($group->id));
     } else {
         $totalsize = get_field_sql('
             SELECT SUM(size)
             FROM {artefact_file_files} f JOIN {artefact} a ON f.artefact = a.id
             WHERE a.owner = ? AND f.artefact IN (' . $idstr . ')', array($USER->get('id')));
     }
     // Get all fileids so that we can delete the files on disk
     $filetodeleteids = get_column_sql('
         SELECT fileid
         FROM {artefact_file_files} aff1
         WHERE artefact IN (' . $idstr . ')
         GROUP BY fileid
         HAVING COUNT(aff1.artefact) IN
            (SELECT COUNT(aff2.artefact)
             FROM {artefact_file_files} aff2
             WHERE aff1.fileid = aff2.fileid)', null);
     // The current rule is that file deletion should be logged in the artefact_log table
     // only for group-owned files.  To save time we will be slightly naughty here and
     // log deletion for all these files if at least one is group-owned.
     $log = (bool) count_records_select('artefact', 'id IN (' . $idstr . ') AND "group" IS NOT NULL');
     delete_records_select('artefact_attachment', 'attachment IN (' . $idstr . ')');
     delete_records_select('artefact_file_files', 'artefact IN (' . $idstr . ')');
     parent::bulk_delete($artefactids, $log);
     foreach ($filetodeleteids as $filetodeleteid) {
         $file = get_config('dataroot') . self::get_file_directory($filetodeleteid) . '/' . $filetodeleteid;
         if (is_file($file)) {
             unlink($file);
         }
     }
     if ($totalsize) {
         if ($group) {
             group_quota_remove($group->id, $totalsize);
         } else {
             $USER->quota_remove($totalsize);
             $USER->commit();
         }
     }
     db_commit();
 }
Esempio n. 2
0
 public static function bulk_delete($artefactids)
 {
     global $USER;
     if (empty($artefactids)) {
         return;
     }
     $idstr = join(',', array_map('intval', $artefactids));
     db_begin();
     // Get the size of all the files we're about to delete that belong to
     // the user.
     $totalsize = get_field_sql('
         SELECT SUM(size)
         FROM {artefact_file_files} f JOIN {artefact} a ON f.artefact = a.id
         WHERE a.owner = ? AND f.artefact IN (' . $idstr . ')', array($USER->get('id')));
     // Get all fileids so that we can delete the files on disk
     $fileids = get_records_select_assoc('artefact_file_files', 'artefact IN (' . $idstr . ')', array());
     $fileidcounts = get_records_sql_assoc('
         SELECT fileid, COUNT(fileid) AS fileidcount
         FROM {artefact_file_files}
         WHERE artefact IN (' . $idstr . ')
         GROUP BY fileid', null);
     // The current rule is that file deletion should be logged in the artefact_log table
     // only for group-owned files.  To save time we will be slightly naughty here and
     // log deletion for all these files if at least one is group-owned.
     $log = (bool) count_records_select('artefact', 'id IN (' . $idstr . ') AND "group" IS NOT NULL');
     delete_records_select('artefact_attachment', 'attachment IN (' . $idstr . ')');
     delete_records_select('artefact_file_files', 'artefact IN (' . $idstr . ')');
     parent::bulk_delete($artefactids, $log);
     foreach ($fileids as $r) {
         // Delete the file on disk if there's only one artefact left pointing to it
         if ($fileidcounts[$r->fileid]->fileidcount == 1) {
             $file = get_config('dataroot') . self::get_file_directory($r->fileid) . '/' . $r->fileid;
             if (is_file($file)) {
                 unlink($file);
             }
         }
         $fileidcounts[$r->fileid]->fileidcount--;
     }
     if ($totalsize) {
         $USER->quota_remove($totalsize);
         $USER->commit();
     }
     db_commit();
 }