Skip to content

Commit

Permalink
MDL-22548, added sortorder field to files table, drop mainfile field …
Browse files Browse the repository at this point in the history
…from resource table, use sortorder to decide main file
  • Loading branch information
Dongsheng Cai committed May 28, 2010
1 parent f484c0a commit f79321f
Show file tree
Hide file tree
Showing 17 changed files with 250 additions and 88 deletions.
9 changes: 9 additions & 0 deletions files/files_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@
}
break;

case 'setmainfile':
$filepath = file_correct_filepath($filepath);
// reset sort order
file_reset_sortorder($user_context->id, $filearea, $itemid);
// set main file
$return = file_set_sortorder($user_context->id, $filearea, $itemid, $filepath, $filename, 1);
echo json_encode($return);
break;

case 'renamedir':
$fs = get_file_storage();
$fb = get_file_browser();
Expand Down
3 changes: 2 additions & 1 deletion lib/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2249,7 +2249,8 @@
<FIELD NAME="author" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="The original author of the file" PREVIOUS="source" NEXT="license"/>
<FIELD NAME="license" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="license of the file to guide reuse" PREVIOUS="author" NEXT="timecreated"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="license" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="timecreated"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="timecreated" NEXT="sortorder"/>
<FIELD NAME="sortorder" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" COMMENT="order of files" PREVIOUS="timemodified"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="contextid"/>
Expand Down
16 changes: 15 additions & 1 deletion lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4156,14 +4156,28 @@ function xmldb_main_upgrade($oldversion) {
/// Main savepoint reached
upgrade_main_savepoint($result, 2010052700);
}

if ($result && $oldversion < 2010052800) {
/// Changes to modinfo mean we need to rebuild course cache
require_once($CFG->dirroot . '/course/lib.php');
rebuild_course_cache(0, true);
upgrade_main_savepoint($result, 2010052800);
}

if ($result && $oldversion < 2010052801) {

/// Define field sortorder to be added to files
$table = new xmldb_table('files');
$field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');

/// Conditionally launch add field sortorder
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

/// Main savepoint reached
upgrade_main_savepoint($result, 2010052801);
}
return $result;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/file/file_info.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ public function get_source() {
return null;
}

/**
* Returns the sort order of the file
* @return int
*/
public function get_sortorder() {
return 0;
}

/**
* Create new directory, may throw exception - make sure
* params are valid.
Expand Down
8 changes: 8 additions & 0 deletions lib/file/file_info_stored.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ public function get_source() {
return $this->lf->get_source();
}

/**
* Returns the sort order of the file
* @return int
*/
public function get_sortorder() {
return $this->lf->get_sortorder();
}

/**
* Returns list of children.
* @return array of file_info instances
Expand Down
22 changes: 20 additions & 2 deletions lib/file/file_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public function get_file($contextid, $filearea, $itemid, $filepath, $filename) {
* @param bool $includedirs
* @return array of stored_files indexed by pathanmehash
*/
public function get_area_files($contextid, $filearea, $itemid=false, $sort="itemid, filepath, filename", $includedirs = true) {
public function get_area_files($contextid, $filearea, $itemid=false, $sort="sortorder, itemid, filepath, filename", $includedirs = true) {
global $DB;

$conditions = array('contextid'=>$contextid, 'filearea'=>$filearea);
Expand Down Expand Up @@ -247,7 +247,7 @@ public function get_area_files($contextid, $filearea, $itemid=false, $sort="item
*/
public function get_area_tree($contextid, $filearea, $itemid) {
$result = array('dirname'=>'', 'dirfile'=>null, 'subdirs'=>array(), 'files'=>array());
$files = $this->get_area_files($contextid, $filearea, $itemid, $sort="itemid, filepath, filename", true);
$files = $this->get_area_files($contextid, $filearea, $itemid, $sort="sortorder, itemid, filepath, filename", true);
// first create directory structure
foreach ($files as $hash=>$dir) {
if (!$dir->is_directory()) {
Expand Down Expand Up @@ -654,6 +654,14 @@ public function create_file_from_pathname($file_record, $pathname) {
throw new file_exception('storedfileproblem', 'Invalid itemid');
}

if (!empty($file_record->sortorder)) {
if (!is_number($file_record->sortorder) or $file_record->sortorder < 0) {
$file_record->sortorder = 0;
}
} else {
$file_record->sortorder = 0;
}

$file_record->filepath = clean_param($file_record->filepath, PARAM_PATH);
if (strpos($file_record->filepath, '/') !== 0 or strrpos($file_record->filepath, '/') !== strlen($file_record->filepath)-1) {
// path must start and end with '/'
Expand Down Expand Up @@ -683,6 +691,7 @@ public function create_file_from_pathname($file_record, $pathname) {
$newrecord->source = empty($file_record->source) ? null : $file_record->source;
$newrecord->author = empty($file_record->author) ? null : $file_record->author;
$newrecord->license = empty($file_record->license) ? null : $file_record->license;
$newrecord->sortorder = $file_record->sortorder;

list($newrecord->contenthash, $newrecord->filesize, $newfile) = $this->add_file_to_pool($pathname);

Expand Down Expand Up @@ -734,6 +743,14 @@ public function create_file_from_string($file_record, $content) {
throw new file_exception('storedfileproblem', 'Invalid itemid');
}

if (!empty($file_record->sortorder)) {
if (!is_number($file_record->sortorder) or $file_record->sortorder < 0) {
$file_record->sortorder = 0;
}
} else {
$file_record->sortorder = 0;
}

$file_record->filepath = clean_param($file_record->filepath, PARAM_PATH);
if (strpos($file_record->filepath, '/') !== 0 or strrpos($file_record->filepath, '/') !== strlen($file_record->filepath)-1) {
// path must start and end with '/'
Expand Down Expand Up @@ -763,6 +780,7 @@ public function create_file_from_string($file_record, $content) {
$newrecord->source = empty($file_record->source) ? null : $file_record->source;
$newrecord->author = empty($file_record->author) ? null : $file_record->author;
$newrecord->license = empty($file_record->license) ? null : $file_record->license;
$newrecord->sortorder = $file_record->sortorder;

list($newrecord->contenthash, $newrecord->filesize, $newfile) = $this->add_string_to_pool($content);

Expand Down
10 changes: 9 additions & 1 deletion lib/file/stored_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,5 +448,13 @@ public function get_author() {
public function get_source() {
return $this->file_record->source;
}
}

/**
* Returns the sort order of file
*
* @return int
*/
public function get_sortorder() {
return $this->file_record->sortorder;
}
}
64 changes: 63 additions & 1 deletion lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ function file_get_user_area_folders($draftitemid, $filepath, &$data, $filearea =
foreach ($files as $file) {
if ($file->is_directory()) {
$item = new stdclass;
$item->sortorder = $file->get_sortorder();
$item->filepath = $file->get_filepath();

$foldername = explode('/', trim($item->filepath, '/'));
Expand Down Expand Up @@ -603,6 +604,7 @@ function file_get_user_area_files($draftitemid, $filepath = '/', $filearea = 'us
}

$list = array();
$maxlength = 12;
if ($files = $fs->get_directory_files($context->id, $filearea, $draftitemid, $filepath, false)) {
foreach ($files as $file) {
$item = new stdclass;
Expand All @@ -611,10 +613,16 @@ function file_get_user_area_files($draftitemid, $filepath = '/', $filearea = 'us
$item->fullname = trim($item->filename, '/');
$filesize = $file->get_filesize();
$item->filesize = $filesize ? display_size($filesize) : '';
if (strlen($item->fullname) >= $maxlength) {
$item->shortname = trim(substr($item->fullname, 0, $maxlength)).'...';
} else {
$item->shortname = $item->fullname;
}

$icon = mimeinfo_from_type('icon', $file->get_mimetype());
$icon = str_replace('.gif', '', $icon);
$item->icon = $OUTPUT->pix_url('f/' . $icon)->out();
$item->sortorder = $file->get_sortorder();

if ($icon == 'zip') {
$item->type = 'zip';
Expand All @@ -628,10 +636,16 @@ function file_get_user_area_files($draftitemid, $filepath = '/', $filearea = 'us
$item->type = 'folder';
$foldername = explode('/', trim($item->filepath, '/'));
$item->fullname = trim(array_pop($foldername), '/');
if (strlen($item->fullname) >= $maxlength) {
$item->shortname = trim(substr($item->fullname, 0, $maxlength)).'...';
} else {
$item->shortname = $item->fullname;
}
} else {
$fb = get_file_browser();
$fileinfo = $fb->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
$item->url = $fileinfo->get_url();
$item->sortorder = $fileinfo->get_sortorder();
}
$list[] = $item;
}
Expand Down Expand Up @@ -740,7 +754,8 @@ function file_save_draft_area_files($draftitemid, $contextid, $filearea, $itemid
$filecount = 0;
foreach ($oldfiles as $file) {
$oldhash = $file->get_pathnamehash();
if (isset($newhashes[$oldhash])) {
// check if sortorder, filename, filepath, filearea, itemid and contextid changed
if (isset($newhashes[$oldhash]) && $file->get_sortorder() == $newhashes[$oldhash]->get_sortorder()) {
if (!$file->is_directory()) {
$filecount++;
}
Expand Down Expand Up @@ -797,6 +812,53 @@ function file_save_draft_area_files($draftitemid, $contextid, $filearea, $itemid
return $text;
}

/**
* Set file sort order
* @global object $DB
* @param integer $contextid the context id
* @param string $filearea file area.
* @param integer $itemid itemid.
* @param string $filepath file path.
* @param string $filename file name.
* @param integer $sortorer the sort order of file.
* @return boolean
*/
function file_set_sortorder($contextid, $filearea, $itemid, $filepath, $filename, $sortorder) {
global $DB;
$conditions = array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename);
if ($file_record = $DB->get_record('files', $conditions)) {
$sortorder = (int)$sortorder;
$file_record->sortorder = $sortorder;
$DB->update_record('files', $file_record);
return true;
}
return false;
}

/**
* reset file sort order number to 0
* @global object $DB
* @param integer $contextid the context id
* @param string $filearea file area.
* @param integer $itemid itemid.
* @return boolean
*/
function file_reset_sortorder($contextid, $filearea, $itemid=false) {
global $DB;

$conditions = array('contextid'=>$contextid, 'filearea'=>$filearea);
if ($itemid !== false) {
$conditions['itemid'] = $itemid;
}

$file_records = $DB->get_records('files', $conditions);
foreach ($file_records as $file_record) {
$file_record->sortorder = 0;
$DB->update_record('files', $file_record);
}
return true;
}

/**
* Returns description of upload error
*
Expand Down
31 changes: 17 additions & 14 deletions lib/form/filemanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ M.form_filemanager.init = function(Y, options) {
this.options = options;
if (options.mainfile) {
this.enablemainfile = options.mainfile;
var mainid = '#id_'+this.enablemainfile;
var filename = Y.one(mainid).get('value');
this.mainfilename = '';
if (filename) {
var parts = filename.split('/');
this.mainfilename = parts[parts.length-1];
}
}
this.client_id = options.client_id;
this.currentpath = '/';
Expand Down Expand Up @@ -396,11 +389,12 @@ M.form_filemanager.init = function(Y, options) {
break;
}
var fullname = list[i].fullname;
var shorten = list[i].shortname;

if (this.mainfilename == fullname) {
html = html.replace('___fullname___', '<strong><a href="'+url+'" id="'+fileid+'"><img src="'+list[i].icon+'" /> ' + fullname + '</a></strong>');
if (list[i].sortorder == 1) {
html = html.replace('___fullname___', '<strong><a title="'+fullname+'" href="'+url+'" id="'+fileid+'"><img src="'+list[i].icon+'" /> ' + shorten + '</a></strong>');
} else {
html = html.replace('___fullname___', '<a href="'+url+'" id="'+fileid+'"><img src="'+list[i].icon+'" /> ' + fullname + '</a>');
html = html.replace('___fullname___', '<a title="'+fullname+'" href="'+url+'" id="'+fileid+'"><img src="'+list[i].icon+'" /> ' + shorten + '</a>');
}
html = html.replace('___action___', '<span class="fm-menuicon" id="'+action+'"><img alt="▶" src="'+M.util.image_url('i/menu')+'" /></span>');
html = '<li id="'+htmlid+'">'+html+'</li>';
Expand Down Expand Up @@ -452,11 +446,20 @@ M.form_filemanager.init = function(Y, options) {
];
function setmainfile(type, ev, obj) {
var file = obj[node.get('id')];
this.mainfilename = file.filename;
Y.one(mainid).set('value', file.filepath+file.filename);
scope.refresh(scope.currentpath);
//Y.one(mainid).set('value', file.filepath+file.filename);
var params = {};
params['filepath'] = file.filepath;
params['filename'] = file.filename;
this.request({
action: 'setmainfile',
scope: scope,
params: params,
callback: function(id, obj, args) {
scope.refresh(scope.currentpath);
}
});
}
if (this.enablemainfile && (file.filename != this.mainfilename)) {
if (this.enablemainfile && (file.sortorder != 1)) {
var mainid = '#id_'+this.enablemainfile;
var menu = {text: M.str.repository.setmainfile, onclick:{fn: setmainfile, obj:data, scope:this}};
menuitems.push(menu);
Expand Down
7 changes: 3 additions & 4 deletions mod/resource/db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/resource/db" VERSION="20090722" COMMENT="XMLDB file for Resource module"
<XMLDB PATH="mod/resource/db" VERSION="20100527" COMMENT="XMLDB file for Resource module"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
Expand All @@ -11,9 +11,8 @@
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="course" NEXT="intro"/>
<FIELD NAME="intro" TYPE="text" LENGTH="small" NOTNULL="false" SEQUENCE="false" PREVIOUS="name" NEXT="introformat"/>
<FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="intro" NEXT="tobemigrated"/>
<FIELD NAME="tobemigrated" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="introformat" NEXT="mainfile"/>
<FIELD NAME="mainfile" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" PREVIOUS="tobemigrated" NEXT="legacyfiles"/>
<FIELD NAME="legacyfiles" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="mainfile" NEXT="legacyfileslast"/>
<FIELD NAME="tobemigrated" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="introformat" NEXT="legacyfiles"/>
<FIELD NAME="legacyfiles" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="tobemigrated" NEXT="legacyfileslast"/>
<FIELD NAME="legacyfileslast" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" PREVIOUS="legacyfiles" NEXT="display"/>
<FIELD NAME="display" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="legacyfileslast" NEXT="displayoptions"/>
<FIELD NAME="displayoptions" TYPE="text" LENGTH="small" NOTNULL="false" SEQUENCE="false" PREVIOUS="display" NEXT="filterfiles"/>
Expand Down
35 changes: 35 additions & 0 deletions mod/resource/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,40 @@ function xmldb_resource_upgrade($oldversion) {
upgrade_mod_savepoint($result, 2009063000, 'resource');
}

if ($result && $oldversion < 2009080501) {
require_once("$CFG->libdir/filelib.php");

$sql = "SELECT r.id,
r.mainfile,
cm.id AS cmid
FROM {resource} r
JOIN {modules} m ON m.name='resource'
JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = r.id)";

if ($instances = $DB->get_recordset_sql($sql)) {
foreach ($instances as $instance) {
$context = get_context_instance(CONTEXT_MODULE, $instance->cmid);
$filearea = 'resource_content';
$itemid = 0;
$filepath = file_correct_filepath(dirname($instance->mainfile));
$filename = basename($instance->mainfile);
file_set_sortorder($context->id, $filearea, $itemid, $filepath, $filename, 1);
}
}

/// Define field mainfile to be dropped from resource
$table = new xmldb_table('resource');
$field = new xmldb_field('mainfile');

/// Conditionally launch drop field mainfile
if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field);
}

/// resource savepoint reached
upgrade_mod_savepoint($result, 2009080501, 'resource');
}


return $result;
}
Loading

0 comments on commit f79321f

Please sign in to comment.