MDL-19488 - allow the creation of a "Public" feedback

This commit is contained in:
Andreas Grabs 2011-10-11 22:37:47 +02:00
parent e70dab0646
commit 7b1126fd20
5 changed files with 161 additions and 73 deletions

View file

@ -11,6 +11,7 @@
require_once("../../config.php"); require_once("../../config.php");
require_once("lib.php"); require_once("lib.php");
require_once('delete_template_form.php'); require_once('delete_template_form.php');
require_once($CFG->libdir.'/tablelib.php');
// $SESSION->feedback->current_tab = 'templates'; // $SESSION->feedback->current_tab = 'templates';
$current_tab = 'templates'; $current_tab = 'templates';
@ -77,13 +78,24 @@ if ($mform->is_cancelled()) {
} }
if(isset($formdata->confirmdelete) AND $formdata->confirmdelete == 1){ if(isset($formdata->confirmdelete) AND $formdata->confirmdelete == 1){
feedback_delete_template($formdata->deletetempl); if(!$template = $DB->get_record("feedback_template", array("id"=>$deletetempl))) {
print_error('error');
}
if($template->ispublic) {
$systemcontext = get_system_context();
require_capability('mod/feedback:createpublictemplate', $systemcontext);
require_capability('mod/feedback:deletetemplate', $systemcontext);
}
feedback_delete_template($template);
redirect($deleteurl->out(false)); redirect($deleteurl->out(false));
} }
/// Print the page header /// Print the page header
$strfeedbacks = get_string("modulenameplural", "feedback"); $strfeedbacks = get_string("modulenameplural", "feedback");
$strfeedback = get_string("modulename", "feedback"); $strfeedback = get_string("modulename", "feedback");
$str_delete_feedback = get_string('delete_template','feedback');
$PAGE->set_heading(format_string($course->fullname)); $PAGE->set_heading(format_string($course->fullname));
$PAGE->set_title(format_string($feedback->name)); $PAGE->set_title(format_string($feedback->name));
@ -96,7 +108,7 @@ include('tabs.php');
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
echo $OUTPUT->heading(get_string('delete_template','feedback')); echo $OUTPUT->heading($str_delete_feedback);
if($shoulddelete == 1) { if($shoulddelete == 1) {
echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter boxwidthnormal'); echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter boxwidthnormal');
@ -104,37 +116,94 @@ if($shoulddelete == 1) {
$mform->display(); $mform->display();
echo $OUTPUT->box_end(); echo $OUTPUT->box_end();
}else { }else {
//first we get the own templates
$templates = feedback_get_template_list($course, 'own'); $templates = feedback_get_template_list($course, 'own');
echo '<div class="mdl-align">';
if(!is_array($templates)) { if(!is_array($templates)) {
echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'), 'generalbox boxaligncenter'); echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'), 'generalbox boxaligncenter');
}else { }else {
echo '<table width="30%">'; echo $OUTPUT->heading(get_string('course'), 3);
echo '<tr><th>'.get_string('templates', 'feedback').'</th><th>&nbsp;</th></tr>'; echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
$tablecolumns = array('template', 'action');
$tableheaders = array(get_string('template', 'feedback'), '');
$table_course = new flexible_table('feedback_template_course_table');
$table_course->define_columns($tablecolumns);
$table_course->define_headers($tableheaders);
$table_course->define_baseurl($deleteurl);
$table_course->column_style('action', 'width', '10%');
$table_course->sortable(false);
$table_course->set_attribute('width', '100%');
$table_course->set_attribute('class', 'generaltable');
$table_course->setup();
foreach($templates as $template) { foreach($templates as $template) {
$suffix = $template->ispublic ? (' ('.get_string('public', 'feedback').')') : ''; $data = array();
echo '<tr><td align="left">'.$template->name.$suffix.'</td>'; $data[] = $template->name;
echo '<td align="center">'; $url = new moodle_url($deleteurl, array(
echo '<form action="delete_template.php" method="post">'; 'id'=>$id,
echo '<input title="'.get_string('delete_template','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/delete') . '" hspace="1" height="11" width="11" border="0" />'; 'deletetempl'=>$template->id,
echo '<input type="hidden" name="deletetempl" value="'.$template->id.'" />'; 'shoulddelete'=>1,
echo '<input type="hidden" name="shoulddelete" value="1" />'; ));
echo '<input type="hidden" name="id" value="'.$id.'" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />'; $data[] = $OUTPUT->single_button($url, $str_delete_feedback, 'post');
echo '</form>'; $table_course->add_data($data);
echo '</td></tr>';
} }
echo '</table>'; $table_course->finish_output();
echo $OUTPUT->box_end();
} }
?> //now we get the public templates if it is permitted
<form name="frm" action="delete_template.php" method="post"> $systemcontext = get_system_context();
<input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" /> if(has_capability('mod/feedback:createpublictemplate', $systemcontext) AND
<input type="hidden" name="id" value="<?php echo $id;?>" /> has_capability('mod/feedback:deletetemplate', $systemcontext)) {
<input type="hidden" name="canceldelete" value="0" /> $templates = feedback_get_template_list($course, 'public');
<button type="button" onclick="this.form.canceldelete.value=1;this.form.submit();"><?php print_string('cancel');?></button> if(!is_array($templates)) {
</form> echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'), 'generalbox boxaligncenter');
</div> }else {
<?php echo $OUTPUT->heading(get_string('public', 'feedback'), 3);
echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
$tablecolumns = $tablecolumns = array('template', 'action');
$tableheaders = array(get_string('template', 'feedback'), '');
$table_public = new flexible_table('feedback_template_public_table');
$table_public->define_columns($tablecolumns);
$table_public->define_headers($tableheaders);
$table_public->define_baseurl($deleteurl);
$table_public->column_style('action', 'width', '10%');
$table_public->sortable(false);
$table_public->set_attribute('width', '100%');
$table_public->set_attribute('class', 'generaltable');
$table_public->setup();
// echo $OUTPUT->heading(get_string('public', 'feedback'), 3);
// echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
foreach($templates as $template) {
$data = array();
$data[] = $template->name;
$url = new moodle_url($deleteurl, array(
'id'=>$id,
'deletetempl'=>$template->id,
'shoulddelete'=>1,
));
$data[] = $OUTPUT->single_button($url, $str_delete_feedback, 'post');
$table_public->add_data($data);
}
$table_public->finish_output();
echo $OUTPUT->box_end();
}
}
echo $OUTPUT->box_start('boxaligncenter boxwidthnormal');
$url = new moodle_url($deleteurl, array(
'id'=>$id,
'canceldelete'=>1,
));
echo $OUTPUT->single_button($url, get_string('back'), 'post');
echo $OUTPUT->box_end();
} }
echo $OUTPUT->footer(); echo $OUTPUT->footer();

View file

@ -99,7 +99,7 @@ if(isset($create_template_formdata->savetemplate) && $create_template_formdata->
$savereturn = 'notsaved_name'; $savereturn = 'notsaved_name';
}else { }else {
//if the feedback is located on the frontpage then templates can be public //if the feedback is located on the frontpage then templates can be public
if(SITEID === $course->id && has_capability('mod/feedback:createpublictemplate', $context)) { if(has_capability('mod/feedback:createpublictemplate', get_system_context())) {
$create_template_formdata->ispublic = isset($create_template_formdata->ispublic) ? 1 : 0; $create_template_formdata->ispublic = isset($create_template_formdata->ispublic) ? 1 : 0;
}else { }else {
$create_template_formdata->ispublic = 0; $create_template_formdata->ispublic = 0;

View file

@ -79,20 +79,29 @@ class feedback_edit_use_template_form extends moodleform {
$templates_options = array(); $templates_options = array();
$owntemplates = feedback_get_template_list($this->feedbackdata->course, 'own'); $owntemplates = feedback_get_template_list($this->feedbackdata->course, 'own');
$publictemplates = feedback_get_template_list($this->feedbackdata->course, 'public'); $publictemplates = feedback_get_template_list($this->feedbackdata->course, 'public');
if($owntemplates OR $publictemplates){//get the templates
$templates_options[' '] = get_string('select'); $options = array();
foreach($owntemplates as $template) { if($owntemplates or $publictemplates) {
if($template->ispublic) { $options[''] = array('' => get_string('choose'));
continue;
if($owntemplates) {
$courseoptions = array();
foreach($owntemplates as $template) {
$courseoptions[$template->id] = $template->name;
} }
$templates_options[$template->id] = $template->name; $options[get_string('course')] = $courseoptions;
} }
foreach($publictemplates as $template) {
$templates_options[$template->id] = '*'.$template->name; if($publictemplates) {
$publicoptions = array();
foreach($publictemplates as $template) {
$publicoptions[$template->id] = $template->name;
}
$options[get_string('public', 'feedback')] = $publicoptions;
} }
$attributes = 'onChange="this.form.submit()"'; $attributes = 'onChange="this.form.submit()"';
$elementgroup[] =& $mform->createElement('select', 'templateid', '', $templates_options, $attributes); $elementgroup[] =& $mform->createElement('selectgroups', 'templateid', '', $options, $attributes);
// buttons
$elementgroup[] =& $mform->createElement('submit', 'use_template', get_string('use_this_template', 'feedback')); $elementgroup[] =& $mform->createElement('submit', 'use_template', get_string('use_this_template', 'feedback'));
}else { }else {
$mform->addElement('static', 'info', get_string('no_templates_available_yet', 'feedback')); $mform->addElement('static', 'info', get_string('no_templates_available_yet', 'feedback'));
@ -144,11 +153,8 @@ class feedback_edit_create_template_form extends moodleform {
$elementgroup[] =& $mform->createElement('static', 'templatenamelabel', get_string('name', 'feedback')); $elementgroup[] =& $mform->createElement('static', 'templatenamelabel', get_string('name', 'feedback'));
$elementgroup[] =& $mform->createElement('text', 'templatename', get_string('name', 'feedback'), array('size'=>'40', 'maxlength'=>'200')); $elementgroup[] =& $mform->createElement('text', 'templatename', get_string('name', 'feedback'), array('size'=>'40', 'maxlength'=>'200'));
//If the feedback is located on the frontpage the we can create public templates if(has_capability('mod/feedback:createpublictemplate', get_system_context())) {
if(SITEID === $this->feedbackdata->course->id) { $elementgroup[] =& $mform->createElement('checkbox', 'ispublic', get_string('public', 'feedback'), get_string('public', 'feedback'));
if(has_capability('mod/feedback:createpublictemplate', $this->feedbackdata->context)) {
$elementgroup[] =& $mform->createElement('checkbox', 'ispublic', get_string('public', 'feedback'), get_string('public', 'feedback'));
}
} }
// buttons // buttons

View file

@ -123,7 +123,11 @@ class feedback_item_label extends feedback_item_base {
//is the item a template? //is the item a template?
if(!$item->feedback AND $item->template) { if(!$item->feedback AND $item->template) {
$template = $DB->get_record('feedback_template', array('id'=>$item->template)); $template = $DB->get_record('feedback_template', array('id'=>$item->template));
$context = get_context_instance(CONTEXT_COURSE, $template->course); if($template->ispublic) {
$context = get_system_context();
}else {
$context = get_context_instance(CONTEXT_COURSE, $template->course);
}
$filearea = 'template'; $filearea = 'template';
}else { }else {
$cm = get_coursemodule_from_instance('feedback', $item->feedback); $cm = get_coursemodule_from_instance('feedback', $item->feedback);

View file

@ -240,7 +240,6 @@ function feedback_pluginfile($course, $cm, $context, $filearea, $args, $forcedow
return false; return false;
} }
/** /**
* this will delete a given instance. * this will delete a given instance.
* all referenced data also will be deleted * all referenced data also will be deleted
@ -990,7 +989,7 @@ function feedback_create_template($courseid, $name, $ispublic = 0) {
global $DB; global $DB;
$templ = new stdClass(); $templ = new stdClass();
$templ->course = $courseid; $templ->course = ($ispublic ? 0 : $courseid);
$templ->name = $name; $templ->name = $name;
$templ->ispublic = $ispublic; $templ->ispublic = $ispublic;
@ -1023,9 +1022,14 @@ function feedback_save_as_template($feedback, $name, $ispublic = 0) {
return false; return false;
} }
//files in the template_item are in the context of the current course //files in the template_item are in the context of the current course or
//if the template is public the files are in the system context
//files in the feedback_item are in the feedback_context of the feedback //files in the feedback_item are in the feedback_context of the feedback
$c_context = get_context_instance(CONTEXT_COURSE, $newtempl->course); if($ispublic) {
$s_context = get_system_context();
}else {
$s_context = get_context_instance(CONTEXT_COURSE, $newtempl->course);
}
$cm = get_coursemodule_from_instance('feedback', $feedback->id); $cm = get_coursemodule_from_instance('feedback', $feedback->id);
$f_context = get_context_instance(CONTEXT_MODULE, $cm->id); $f_context = get_context_instance(CONTEXT_MODULE, $cm->id);
@ -1046,7 +1050,7 @@ function feedback_save_as_template($feedback, $name, $ispublic = 0) {
if ($itemfiles = $fs->get_area_files($f_context->id, 'mod_feedback', 'item', $item->id, "id", false)) { if ($itemfiles = $fs->get_area_files($f_context->id, 'mod_feedback', 'item', $item->id, "id", false)) {
foreach($itemfiles as $ifile) { foreach($itemfiles as $ifile) {
$file_record = new stdClass(); $file_record = new stdClass();
$file_record->contextid = $c_context->id; $file_record->contextid = $s_context->id;
$file_record->component = 'mod_feedback'; $file_record->component = 'mod_feedback';
$file_record->filearea = 'template'; $file_record->filearea = 'template';
$file_record->itemid = $t_item->id; $file_record->itemid = $t_item->id;
@ -1076,27 +1080,19 @@ function feedback_save_as_template($feedback, $name, $ispublic = 0) {
* *
* @global object * @global object
* @uses CONTEXT_COURSE * @uses CONTEXT_COURSE
* @param int $id the templateid * @param object $template the template
* @return void * @return void
*/ */
function feedback_delete_template($id) { function feedback_delete_template($template) {
global $DB; global $DB;
$template = $DB->get_record("feedback_template", array("id"=>$id)); //deleting the files from the item is done by feedback_delete_item
if($t_items = $DB->get_records("feedback_item", array("template"=>$template->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) { foreach($t_items as $t_item) {
if ($templatefiles = $fs->get_area_files($context->id, 'mod_feedback', 'template', $t_item->id, "id", false)) { feedback_delete_item($t_item->id, false, $template);
$fs->delete_area_files($context->id, 'mod_feedback', 'template', $t_item->id);
}
} }
} }
$DB->delete_records("feedback_template", array("id"=>$id)); $DB->delete_records("feedback_template", array("id"=>$template->id));
} }
/** /**
@ -1129,9 +1125,9 @@ function feedback_items_from_template($feedback, $templateid, $deleteold = false
//files in the template_item are in the context of the current course //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 //files in the feedback_item are in the feedback_context of the feedback
if($template->ispublic) { if($template->ispublic) {
$c_context = get_context_instance(CONTEXT_COURSE, $template->course); $s_context = get_system_context();
}else { }else {
$c_context = get_context_instance(CONTEXT_COURSE, $feedback->course); $s_context = get_context_instance(CONTEXT_COURSE, $feedback->course);
} }
$course = $DB->get_record('course', array('id'=>$feedback->course)); $course = $DB->get_record('course', array('id'=>$feedback->course));
$cm = get_coursemodule_from_instance('feedback', $feedback->id); $cm = get_coursemodule_from_instance('feedback', $feedback->id);
@ -1182,7 +1178,7 @@ function feedback_items_from_template($feedback, $templateid, $deleteold = false
$item->id = $DB->insert_record('feedback_item', $item); $item->id = $DB->insert_record('feedback_item', $item);
//TODO: moving the files to the new items //TODO: moving the files to the new items
if ($templatefiles = $fs->get_area_files($c_context->id, 'mod_feedback', 'template', $t_item->id, "id", false)) { if ($templatefiles = $fs->get_area_files($s_context->id, 'mod_feedback', 'template', $t_item->id, "id", false)) {
foreach($templatefiles as $tfile) { foreach($templatefiles as $tfile) {
$file_record = new stdClass(); $file_record = new stdClass();
$file_record->contextid = $f_context->id; $file_record->contextid = $f_context->id;
@ -1228,7 +1224,7 @@ function feedback_get_template_list($course, $onlyownorpublic = '') {
$templates = $DB->get_records('feedback_template', array('course'=>$course->id), 'name'); $templates = $DB->get_records('feedback_template', array('course'=>$course->id), 'name');
break; break;
case 'public': case 'public':
$templates = $DB->get_records('feedback_template', array('course'=>SITEID, 'ispublic'=>1), 'name'); $templates = $DB->get_records('feedback_template', array('ispublic'=>1), 'name');
break; break;
} }
return $templates; return $templates;
@ -1402,9 +1398,10 @@ function feedback_update_item($item){
* @uses CONTEXT_MODULE * @uses CONTEXT_MODULE
* @param int $itemid * @param int $itemid
* @param boolean $renumber should the kept items renumbered Yes/No * @param boolean $renumber should the kept items renumbered Yes/No
* @param object $template if the template is given so the items are bound to it
* @return void * @return void
*/ */
function feedback_delete_item($itemid, $renumber = true){ function feedback_delete_item($itemid, $renumber = true, $template = false){
global $DB; global $DB;
@ -1412,13 +1409,25 @@ function feedback_delete_item($itemid, $renumber = true){
//deleting the files from the item //deleting the files from the item
$fs = get_file_storage(); $fs = get_file_storage();
if (!$cm = get_coursemodule_from_instance('feedback', $item->feedback)) {
return false; if($template) {
} if($template->ispublic) {
$context = get_context_instance(CONTEXT_MODULE, $cm->id); $context = get_system_context();
}else {
$context = get_context_instance(CONTEXT_COURSE, $template->course);
}
if ($templatefiles = $fs->get_area_files($context->id, 'mod_feedback', 'template', $item->id, "id", false)) {
$fs->delete_area_files($context->id, 'mod_feedback', 'template', $item->id);
}
}else {
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, 'mod_feedback', 'item', $item->id, "id", false)) { if ($itemfiles = $fs->get_area_files($context->id, 'mod_feedback', 'item', $item->id, "id", false)) {
$fs->delete_area_files($context->id, 'mod_feedback', 'item', $item->id); $fs->delete_area_files($context->id, 'mod_feedback', 'item', $item->id);
}
} }
$DB->delete_records("feedback_value", array("item"=>$itemid)); $DB->delete_records("feedback_value", array("item"=>$itemid));