Skip to content

Commit

Permalink
MDL-22950 adding new component column to the files table, unfortunate…
Browse files Browse the repository at this point in the history
…ly this change requires changes in all 2.0dev code, please review all custom code that was already upgraded to 2.0; fixing multiple problems and regressions in mod/assignment
  • Loading branch information
skodak committed Jul 3, 2010
1 parent ffc3f86 commit 64f9379
Show file tree
Hide file tree
Showing 220 changed files with 4,635 additions and 4,297 deletions.
10 changes: 6 additions & 4 deletions admin/bloglevelupgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,26 @@ function bloglevelupgrade_entries($blogentries, $forum, $cm, $groupid=-1) {

// Copy file attachment records
$fs = get_file_storage();
$files = $fs->get_area_files($sitecontext->id, 'blog_attachment', $blogentry->id);
$files = $fs->get_area_files($sitecontext->id, 'blog', 'attachment', $blogentry->id);

if (!empty($files)) {
foreach ($files as $storedfile) {
$newfile = new object();
$newfile->filearea = 'forum_attachment';
$newfile->component = 'mod_forum';
$newfile->filearea = 'attachment';
$newfile->itemid = $discussion->firstpost;
$newfile->contextid = $forumcontext->id;
$fs->create_file_from_storedfile($newfile, $storedfile->get_id());
}
}

$files = $fs->get_area_files($sitecontext->id, 'blog_post', $blogentry->id);
$files = $fs->get_area_files($sitecontext->id, 'blog', 'post', $blogentry->id);

if (!empty($files)) {
foreach ($files as $storedfile) {
$newfile = new object();
$newfile->filearea = 'forum_post';
$newfile->component = 'mod_forum';
$newfile->filearea = 'post';
$newfile->itemid = $discussion->firstpost;
$newfile->contextid = $forumcontext->id;
$fs->create_file_from_storedfile($newfile, $storedfile->get_id());
Expand Down
3 changes: 1 addition & 2 deletions admin/settings/grades.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
// new CFG variable for gradebook (what roles to display)
$temp->add(new admin_setting_special_gradebookroles());

// enable outcomes checkbox
$temp->add(new admin_setting_configcheckbox('enableoutcomes', get_string('enableoutcomes', 'grades'), get_string('enableoutcomes_help', 'grades'), 0));
// enable outcomes checkbox now in subsystems area

$temp->add(new admin_setting_grade_profilereport());

Expand Down
2 changes: 1 addition & 1 deletion backup/backup_execute.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
//Print final message
echo $OUTPUT->box(get_string("backupfinished"));
$context = get_context_instance(CONTEXT_COURSE, $course->id);
echo $OUTPUT->continue_button("$CFG->wwwroot/files/index.php?contextid=".$context->id."&filearea=course_backup&itemid=0");
echo $OUTPUT->continue_button("$CFG->wwwroot/files/index.php?contextid=".$context->id."&component=backup&filearea=course&itemid=0");
} else {
echo $OUTPUT->box(get_string('importdataexported'));
if (!empty($preferences->backup_destination)) {
Expand Down
2 changes: 1 addition & 1 deletion backup/backuplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2637,7 +2637,7 @@ function copy_zip_to_course_dir ($preferences) {
//Define zip destination (course dir)
$context = get_context_instance(CONTEXT_COURSE, $preferences->backup_course);
$fs = get_file_storage();
$file_record = array('contextid'=>$context->id, 'filearea'=>'course_backup',
$file_record = array('contextid'=>$context->id, 'component'=>'backup', 'filearea'=>'course',
'itemid'=>0, 'filepath'=>'/', 'filename'=>$preferences->backup_name,
'timecreated'=>time(), 'timemodified'=>time());
$fs->create_file_from_pathname($file_record, $from_zip_file);
Expand Down
19 changes: 10 additions & 9 deletions backup/moodle2/backup_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ protected function define_structure() {
$section->set_source_alias('section', 'number');

// Set annotations
$section->annotate_files(array('course_section'), 'id');
$section->annotate_files('course', 'section', 'id');

return $section;
}
Expand Down Expand Up @@ -311,7 +311,8 @@ protected function define_structure() {

$course->annotate_ids('grouping', 'defaultgroupingid');

$course->annotate_files(array('course_summary', 'course_content'), null);
$course->annotate_files('course', 'summary', null);
$course->annotate_files('course', 'legacy', null);

// Return root element ($course)

Expand Down Expand Up @@ -656,8 +657,9 @@ protected function define_structure() {

// Define file annotations

// TODO: Change "course_group_image" file area to the one finally used for group images
$group->annotate_files(array('course_group_description', 'course_group_image'), 'id');
//TODO: not implemented yet
$group->annotate_files('group', 'description', 'id');
$group->annotate_files('group', 'image', 'id');

// Return the root element (groups)
return $groups;
Expand Down Expand Up @@ -980,7 +982,7 @@ protected function define_structure() {
$files = new backup_nested_element('files');

$file = new file_nested_element('file', array('id'), array(
'contenthash', 'contextid', 'filearea', 'itemid',
'contenthash', 'contextid', 'component', 'filearea', 'itemid',
'filepath', 'filename', 'userid', 'filesize',
'mimetype', 'status', 'timecreated', 'timemodified',
'source', 'author', 'license', 'sortorder'));
Expand Down Expand Up @@ -1238,9 +1240,8 @@ protected function define_execution() {
global $DB;

// List of fileareas we are going to annotate
// TODO: Change "user_image" file area to the one finally used for user images
$fileareas = array(
'user_private', 'user_profile', 'user_image');
// TODO: user image not implemented yet
$fileareas = array('private', 'profile', 'image');

// Fetch all annotated (final) users
$rs = $DB->get_recordset('backup_ids_temp', array(
Expand All @@ -1252,7 +1253,7 @@ protected function define_execution() {
foreach ($fileareas as $filearea) {
// We don't need to specify itemid ($userid - 4th param) as far as by
// context we can get all the associated files. See MDL-22092
backup_structure_dbops::annotate_files($this->get_backupid(), $userctxid, $filearea, null);
backup_structure_dbops::annotate_files($this->get_backupid(), $userctxid, 'user', $filearea, null);
}
}
$rs->close();
Expand Down
5 changes: 3 additions & 2 deletions backup/util/dbops/backup_structure_dbops.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@ public static function insert_backup_ids_record($backupid, $itemname, $itemid) {
}
}

public static function annotate_files($backupid, $contextid, $filearea, $itemid) {
public static function annotate_files($backupid, $contextid, $component, $filearea, $itemid) {
global $DB;
$sql = 'SELECT id
FROM {files}
WHERE contextid = ?
AND component = ?
AND filearea = ?';
$params = array($contextid, $filearea);
$params = array($contextid, $component, $filearea);

if (!is_null($itemid)) { // Add itemid to query and params if necessary
$sql .= ' AND itemid = ?';
Expand Down
51 changes: 29 additions & 22 deletions backup/util/helper/backup_helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ static public function store_backup_file($backupid, $filepath) {

// Extract useful information to decide
$hasusers = (bool)$sinfo['users']->value; // Backup has users
$isannon = (bool)$sinfo['anonymize']->value; // Backup is annonymzed
$isannon = (bool)$sinfo['anonymize']->value; // Backup is anonymised
$filename = $sinfo['filename']->value; // Backup filename
$backupmode= $dinfo[0]->mode; // Backup mode backup::MODE_GENERAL/IMPORT/HUB
$backuptype= $dinfo[0]->type; // Backup type backup::TYPE_1ACTIVITY/SECTION/COURSE
Expand All @@ -203,50 +203,57 @@ static public function store_backup_file($backupid, $filepath) {
}

// Calculate file storage options of id being backup
$ctxid = 0;
$filearea = '';
$itemid = 0;
$ctxid = 0;
$filearea = '';
$component = '';
$itemid = 0;
switch ($backuptype) {
case backup::TYPE_1ACTIVITY:
$ctxid = get_context_instance(CONTEXT_MODULE, $id)->id;
$filearea = 'activity_backup';
$itemid = 0;
$ctxid = get_context_instance(CONTEXT_MODULE, $id)->id;
$component = 'backup';
$filearea = 'activity';
$itemid = 0;
break;
case backup::TYPE_1SECTION:
$ctxid = get_context_instance(CONTEXT_COURSE, $courseid)->id;
$filearea = 'section_backup';
$itemid = $id;
$ctxid = get_context_instance(CONTEXT_COURSE, $courseid)->id;
$component = 'backup';
$filearea = 'section';
$itemid = $id;
break;
case backup::TYPE_1COURSE:
$ctxid = get_context_instance(CONTEXT_COURSE, $courseid)->id;
$filearea = 'course_backup';
$itemid = 0;
$ctxid = get_context_instance(CONTEXT_COURSE, $courseid)->id;
$component = 'backup';
$filearea = 'course';
$itemid = 0;
break;
}

// Backups of type HUB (by definition never have user info)
// are sent to user's "user_tohub" file area. The upload process
// will be responsible for cleaning that filearea once finished
if ($backupmode == backup::MODE_HUB) {
$ctxid = get_context_instance(CONTEXT_USER, $userid)->id;
$filearea = 'user_tohub';
$itemid = 0;
$ctxid = get_context_instance(CONTEXT_USER, $userid)->id;
$component = 'user';
$filearea = 'tohub';
$itemid = 0;
}

// Backups without user info or withe the anoymise functionality
// Backups without user info or with the anonymise functionality
// enabled are sent to user's "user_backup"
// file area. Maintenance of such area is responsibility of
// the user via corresponding file manager frontend
if ($backupmode == backup::MODE_GENERAL && (!$hasusers || $isannon)) {
$ctxid = get_context_instance(CONTEXT_USER, $userid)->id;
$filearea = 'user_backup';
$itemid = 0;
$ctxid = get_context_instance(CONTEXT_USER, $userid)->id;
$component = 'user';
$filearea = 'backup';
$itemid = 0;
}

// Let's send the file to file storage, everything already defined
$fs = get_file_storage();
$fr = array(
'contextid' => $ctxid,
'component' => $component,
'filearea' => $filearea,
'itemid' => $itemid,
'filepath' => '/',
Expand All @@ -257,8 +264,8 @@ static public function store_backup_file($backupid, $filepath) {
// If file already exists, delete if before
// creating it again. This is BC behaviour - copy()
// overwrites by default
if ($fs->file_exists($fr['contextid'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename'])) {
$pathnamehash = $fs->get_pathname_hash($fr['contextid'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename']);
if ($fs->file_exists($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename'])) {
$pathnamehash = $fs->get_pathname_hash($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename']);
$sf = $fs->get_file_by_hash($pathnamehash);
$sf->delete();
}
Expand Down
35 changes: 15 additions & 20 deletions backup/util/structure/backup_nested_element.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class backup_nested_element extends base_nested_element implements processable {
protected $params; // Unprocessed params as specified in the set_source() call
protected $procparams;// Processed (path resolved) params array
protected $aliases; // Define DB->final element aliases
protected $fileannotelement; // Element to be used as itemid for file annotations
protected $fileannotareas; // array of file areas to be searched by file annotations
protected $fileannotations; // array of file areas to be searched by file annotations
protected $counter; // Number of instances of this element that have been processed

/**
Expand All @@ -54,8 +53,7 @@ public function __construct($name, $attributes = null, $final_elements = null) {
$this->params = null;
$this->procparams= null;
$this->aliases = array();
$this->fileannotelement = null;
$this->fileannotareas = array();
$this->fileannotations = array();
$this->counter = 0;
}

Expand Down Expand Up @@ -148,21 +146,21 @@ public function set_source_alias($dbname, $finalelementname) {
}
}

public function annotate_files($areas, $elementname) {
if (!is_array($areas)) { // Check we are passing array
throw new base_element_struct_exception('annotate_files_requires_array_of_areas', $areas);
}
$annotations = $this->get_file_annotations();
if (!empty($annotations[0])) { // Check we haven't defined file annotations already
throw new base_element_struct_exception('annotate_files_already_defined', $this->get_name());
public function annotate_files($component, $filearea, $elementname) {
// note: it is possible to annotate areas ONLY in current context, ie modules may backup only from module context
if (!array_key_exists($component, $this->fileannotations)) {
$this->fileannotations[$component] = array();
}

if ($elementname !== null) { // Check elementname is valid
$element = $this->find_element($elementname);
// Annotate the element
$this->fileannotelement= $element;
$elementname = $this->find_element($elementname); //TODO: no warning here? (skodak)
}

if (array_key_exists($filearea, $this->fileannotations[$component])) {
throw new base_element_struct_exception('annotate_files_duplicate_annotation', "$component/$filearea/$elementname");
}
// Annotate the areas
$this->fileannotareas = $areas;

$this->fileannotations[$component][$filearea] = $elementname;
}

public function annotate_ids($itemname, $elementname) {
Expand All @@ -175,10 +173,7 @@ public function annotate_ids($itemname, $elementname) {
* @backup_structure and the areas to be searched
*/
public function get_file_annotations() {
if (empty($this->fileannotareas)) {
return array(null, null);
}
return array($this->fileannotareas, $this->fileannotelement);
return $this->fileannotations;
}

public function get_source_array() {
Expand Down
16 changes: 9 additions & 7 deletions backup/util/structure/backup_structure_processor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ public function pre_process_nested_element(base_nested_element $nested) {

public function process_nested_element(base_nested_element $nested) {
// Proceed with all the file annotations for this element
list($fileareas, $element) = $nested->get_file_annotations();
if ($fileareas) { // If there are areas to search
$backupid = $this->get_var(backup::VAR_BACKUPID);
$contextid= $this->get_var(backup::VAR_CONTEXTID);
$itemid = !is_null($element) ? $element->get_value() : null;
foreach ($fileareas as $filearea) {
backup_structure_dbops::annotate_files($backupid, $contextid, $filearea, $itemid);
$fileannotations = $nested->get_file_annotations();
if ($fileannotations) { // If there are areas to search
$backupid = $this->get_var(backup::VAR_BACKUPID);
$contextid = $this->get_var(backup::VAR_CONTEXTID);
foreach ($fileannotations as $component=>$area) {
foreach ($area as $filearea=>$element) {
$itemid = !is_null($element) ? $element->get_value() : null;
backup_structure_dbops::annotate_files($backupid, $contextid, $component, $filearea, $itemid);
}
}
}
}
Expand Down
Loading

0 comments on commit 64f9379

Please sign in to comment.