Skip to content

Commit

Permalink
MDL-56307 course: Refactor file_storage
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-russ authored and jleyva committed Oct 25, 2016
1 parent 26659f6 commit cebce76
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
11 changes: 6 additions & 5 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3701,11 +3701,12 @@ function course_check_module_updates_since($cm, $from, $fileareas = array(), $fi
}
if (!empty($fileareas) and (empty($filter) or in_array('fileareas', $filter))) {
$fs = get_file_storage();
$extrasql = 'AND (f.timecreated > :since1 OR f.timemodified > :since2)';
$extraconditions = array('since1' => $from, 'since2' => $from);
foreach ($fileareas as $fileareaname) {
$files = $fs->get_area_files($context->id, $component, $fileareaname, false, "itemid", true, $extrasql, $extraconditions);
$updates->{$fileareaname . 'files'} = !empty($files);
$files = $fs->get_area_files($context->id, $component, $fileareas, false, "filearea, timemodified DESC", true, $from);
foreach ($fileareas as $filearea) {
$updates->{$filearea . 'files'} = false;
}
foreach ($files as $file) {
$updates->{$file->get_filearea() . 'files'} = true;
}
}

Expand Down
29 changes: 18 additions & 11 deletions lib/filestorage/file_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,38 +794,45 @@ public function get_external_files($repositoryid, $sort = '') {
*
* @param int $contextid context ID
* @param string $component component
* @param string $filearea file area
* @param mixed $filearea file area/s, you cannot specify multiple fileareas as well as an itemid
* @param int $itemid item ID or all files if not specified
* @param string $sort A fragment of SQL to use for sorting
* @param bool $includedirs whether or not include directories
* @param string $extrasql a fragment of SQL to append to the WHERE part of the query
* @param array $extraconditions an array of additional condition values for the $extrasql
* @param int $updatedsince return files updated since this time
* @return stored_file[] array of stored_files indexed by pathanmehash
*/
public function get_area_files($contextid, $component, $filearea, $itemid = false, $sort = "itemid, filepath, filename",
$includedirs = true, $extrasql = '', $extraconditions = array()) {
$includedirs = true, $updatedsince = 0) {
global $DB;

$conditions = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea);
if ($itemid !== false) {
list($areasql, $conditions) = $DB->get_in_or_equal($filearea, SQL_PARAMS_NAMED);
$conditions['contextid'] = $contextid;
$conditions['component'] = $component;

if ($itemid !== false && is_array($filearea)) {
throw new coding_exception('You cannot specify multiple fileareas as well as an itemid.');
} else if ($itemid !== false) {
$itemidsql = ' AND f.itemid = :itemid ';
$conditions['itemid'] = $itemid;
} else {
$itemidsql = '';
}

$updatedsincesql = '';
if (!empty($updatedsince)) {
$conditions['time'] = $updatedsince;
$updatedsincesql = 'AND f.timemodified > :time';
}

$sql = "SELECT ".self::instance_sql_fields('f', 'r')."
FROM {files} f
LEFT JOIN {files_reference} r
ON f.referencefileid = r.id
WHERE f.contextid = :contextid
AND f.component = :component
AND f.filearea = :filearea
AND f.filearea $areasql
$updatedsincesql
$itemidsql";
if (!empty($extrasql)) {
$sql .= " $extrasql";
$conditions = array_merge($conditions, $extraconditions);
}
if (!empty($sort)) {
$sql .= " ORDER BY {$sort}";
}
Expand Down

0 comments on commit cebce76

Please sign in to comment.