MDL-21227 - convert Feedback to new files api

Currently there is a temporary workarround. Files for templates will be saved in the filearea "course_summary"
This commit is contained in:
Andreas Grabs 2010-04-27 16:34:16 +00:00
parent 214f6731bb
commit 1483231357
2 changed files with 104 additions and 19 deletions

View File

@ -100,8 +100,17 @@ class feedback_item_label extends feedback_item_base {
function print_item($item){ function print_item($item){
global $DB; global $DB;
$cm = get_coursemodule_from_instance('feedback', $item->feedback);
$context = get_context_instance(CONTEXT_MODULE, $cm->id); //is the item a template?
if(!$item->feedback AND $item->template) {
$template = $DB->get_record('feedback_template', array('id'=>$item->template));
$context = get_context_instance(CONTEXT_COURSE, $template->course);
$filearea = 'course_summary';
}else {
$cm = get_coursemodule_from_instance('feedback', $item->feedback);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$filearea = 'feedback_item';
}
$item->presentationformat = FORMAT_HTML; $item->presentationformat = FORMAT_HTML;
$item->presentationtrust = 1; $item->presentationtrust = 1;
@ -109,7 +118,7 @@ class feedback_item_label extends feedback_item_base {
?> ?>
<td colspan="2"> <td colspan="2">
<?php <?php
$output = file_rewrite_pluginfile_urls($item->presentation, 'pluginfile.php', $context->id,'feedback_item',$item->id); $output = file_rewrite_pluginfile_urls($item->presentation, 'pluginfile.php', $context->id, $filearea, $item->id);
echo format_text($output, FORMAT_HTML); echo format_text($output, FORMAT_HTML);
?> ?>
</td> </td>

View File

@ -150,7 +150,13 @@ function feedback_pluginfile($course, $cminfo, $context, $filearea, $args, $forc
return false; return false;
} }
if ($filearea === 'feedback_item') { if($filearea === 'feedback_template') {
$usedcontext = get_context_instance(CONTEXT_COURSE, $course->id);
}else {
$usedcontext = $context;
}
if ($filearea === 'feedback_item' OR $filearea === 'feedback_template') {
$itemid = (int)array_shift($args); $itemid = (int)array_shift($args);
if (!$cm = get_coursemodule_from_instance('feedback', $cminfo->instance, $course->id)) { if (!$cm = get_coursemodule_from_instance('feedback', $cminfo->instance, $course->id)) {
@ -172,7 +178,7 @@ function feedback_pluginfile($course, $cminfo, $context, $filearea, $args, $forc
} }
if ($item->feedback == $cminfo->instance) { if ($item->feedback == $cminfo->instance) {
$filecontext = $context; $filecontext = $usedcontext;
} else { } else {
return false; return false;
} }
@ -213,7 +219,11 @@ function feedback_delete_instance($id) {
$DB->delete_records("feedback_value", array("item"=>$feedbackitem->id)); $DB->delete_records("feedback_value", array("item"=>$feedbackitem->id));
$DB->delete_records("feedback_valuetmp", array("item"=>$feedbackitem->id)); $DB->delete_records("feedback_valuetmp", array("item"=>$feedbackitem->id));
} }
$DB->delete_records("feedback_item", array("feedback"=>$id)); if($delitems = $DB->get_records("feedback_item", array("feedback"=>$id))) {
foreach($delitems as $delitem) {
feedback_delete_item($delitem->id, false);
}
}
} }
//deleting the referenced tracking data //deleting the referenced tracking data
@ -780,7 +790,8 @@ function feedback_create_template($courseid, $name, $ispublic = 0) {
$templ->name = $name; $templ->name = $name;
$templ->ispublic = $ispublic; $templ->ispublic = $ispublic;
return $DB->insert_record('feedback_template', $templ); $templid = $DB->insert_record('feedback_template', $templ);
return $DB->get_record('feedback_template', array('id'=>$templid));
} }
/** /**
@ -796,6 +807,7 @@ function feedback_create_template($courseid, $name, $ispublic = 0) {
*/ */
function feedback_save_as_template($feedback, $name, $ispublic = 0) { function feedback_save_as_template($feedback, $name, $ispublic = 0) {
global $DB; global $DB;
$fs = get_file_storage();
if (!$feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id))) { if (!$feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id))) {
return false; return false;
@ -804,12 +816,31 @@ function feedback_save_as_template($feedback, $name, $ispublic = 0) {
if (!$newtempl = feedback_create_template($feedback->course, $name, $ispublic)) { if (!$newtempl = feedback_create_template($feedback->course, $name, $ispublic)) {
return false; return false;
} }
//files in the template_item are in the context of the current course
//files in the feedback_item are in the feedback_context of the feedback
$c_context = get_context_instance(CONTEXT_COURSE, $newtempl->course);
$cm = get_coursemodule_from_instance('feedback', $feedback->id);
$f_context = get_context_instance(CONTEXT_MODULE, $cm->id);
//create items of this new template //create items of this new template
foreach($feedbackitems as $item) { foreach($feedbackitems as $item) {
unset($item->id); $t_item = clone($item);
$item->feedback = 0;
$item->template = $newtempl; unset($t_item->id);
$DB->insert_record('feedback_item', $item); $t_item->feedback = 0;
$t_item->template = $newtempl->id;
$t_item->id = $DB->insert_record('feedback_item', $t_item);
//copy all included files to the feedback_template filearea
if ($itemfiles = $fs->get_area_files($f_context->id, 'feedback_item', $item->id, "id", false)) {
foreach($itemfiles as $ifile) {
$file_record = new object();
$file_record->contextid = $c_context->id;
$file_record->filearea = 'course_summary';
$file_record->itemid = $t_item->id;
$fs->create_file_from_storedfile($file_record, $ifile);
}
}
} }
return true; return true;
} }
@ -824,7 +855,20 @@ function feedback_save_as_template($feedback, $name, $ispublic = 0) {
function feedback_delete_template($id) { function feedback_delete_template($id) {
global $DB; global $DB;
$DB->delete_records("feedback_item", array("template"=>$id)); $template = $DB->get_record("feedback_template", array("id"=>$id));
//deleting the files from the item
$fs = get_file_storage();
$context = get_context_instance(CONTEXT_COURSE, $template->course);
if($t_items = $DB->get_records("feedback_item", array("template"=>$id))) {
foreach($t_items as $t_item) {
if ($templatefiles = $fs->get_area_files($context->id, 'course_summary', $t_item->id, "id", false)) {
$fs->delete_area_files($context->id, 'course_summary', $t_item->id);
}
}
}
$DB->delete_records("feedback_template", array("id"=>$id)); $DB->delete_records("feedback_template", array("id"=>$id));
} }
@ -840,12 +884,19 @@ function feedback_delete_template($id) {
*/ */
function feedback_items_from_template($feedback, $templateid, $deleteold = false) { function feedback_items_from_template($feedback, $templateid, $deleteold = false) {
global $DB; global $DB;
$fs = get_file_storage();
//get all templateitems //get all templateitems
if(!$templitems = $DB->get_records('feedback_item', array('template'=>$templateid))) { if(!$templitems = $DB->get_records('feedback_item', array('template'=>$templateid))) {
return false; return false;
} }
//files in the template_item are in the context of the current course
//files in the feedback_item are in the feedback_context of the feedback
$c_context = get_context_instance(CONTEXT_COURSE, $feedback->course);
$cm = get_coursemodule_from_instance('feedback', $feedback->id);
$f_context = get_context_instance(CONTEXT_MODULE, $cm->id);
//if deleteold then delete all old items before //if deleteold then delete all old items before
//get all items //get all items
if($deleteold) { if($deleteold) {
@ -866,13 +917,25 @@ function feedback_items_from_template($feedback, $templateid, $deleteold = false
$positionoffset = $DB->count_records('feedback_item', array('feedback'=>$feedback->id)); $positionoffset = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
} }
foreach($templitems as $newitem) { foreach($templitems as $t_item) {
unset($newitem->id); $item = clone($t_item);
$newitem->feedback = $feedback->id; unset($item->id);
$newitem->template = 0; $item->feedback = $feedback->id;
$newitem->position = $newitem->position + $positionoffset; $item->template = 0;
$item->position = $item->position + $positionoffset;
$DB->insert_record('feedback_item', $newitem); $item->id = $DB->insert_record('feedback_item', $item);
//TODO: moving the files to the new items
if ($templatefiles = $fs->get_area_files($c_context->id, 'course_summary', $t_item->id, "id", false)) {
foreach($templatefiles as $tfile) {
$file_record = new object();
$file_record->contextid = $f_context->id;
$file_record->filearea = 'feedback_item';
$file_record->itemid = $item->id;
$fs->create_file_from_storedfile($file_record, $tfile);
}
}
} }
} }
@ -1026,7 +1089,20 @@ function feedback_update_item($item){
function feedback_delete_item($itemid, $renumber = true){ function feedback_delete_item($itemid, $renumber = true){
global $DB; global $DB;
$item = $DB->get_record('feedback_item', array('id'=>$itemid)); $item = $DB->get_record('feedback_item', array('id'=>$itemid));
//deleting the files from the item
$fs = get_file_storage();
if (!$cm = get_coursemodule_from_instance('feedback', $item->feedback)) {
return false;
}
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
if ($itemfiles = $fs->get_area_files($context->id, 'feedback_item', $item->id, "id", false)) {
$fs->delete_area_files($context->id, 'feedback_item', $item->id);
}
$DB->delete_records("feedback_value", array("item"=>$itemid)); $DB->delete_records("feedback_value", array("item"=>$itemid));
$DB->delete_records("feedback_valuetmp", array("item"=>$itemid)); $DB->delete_records("feedback_valuetmp", array("item"=>$itemid));
$DB->delete_records("feedback_item", array("id"=>$itemid)); $DB->delete_records("feedback_item", array("id"=>$itemid));