From 7b1126fd2004ab304437cf479bc966cedbf688db Mon Sep 17 00:00:00 2001 From: Andreas Grabs Date: Tue, 11 Oct 2011 22:37:47 +0200 Subject: [PATCH] MDL-19488 - allow the creation of a "Public" feedback --- mod/feedback/delete_template.php | 121 ++++++++++++++++++++++++------- mod/feedback/edit.php | 2 +- mod/feedback/edit_form.php | 36 +++++---- mod/feedback/item/label/lib.php | 6 +- mod/feedback/lib.php | 69 ++++++++++-------- 5 files changed, 161 insertions(+), 73 deletions(-) diff --git a/mod/feedback/delete_template.php b/mod/feedback/delete_template.php index 0aa4543c0a1..e9685083edc 100644 --- a/mod/feedback/delete_template.php +++ b/mod/feedback/delete_template.php @@ -11,6 +11,7 @@ require_once("../../config.php"); require_once("lib.php"); require_once('delete_template_form.php'); +require_once($CFG->libdir.'/tablelib.php'); // $SESSION->feedback->current_tab = 'templates'; $current_tab = 'templates'; @@ -77,13 +78,24 @@ if ($mform->is_cancelled()) { } 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)); } /// Print the page header $strfeedbacks = get_string("modulenameplural", "feedback"); $strfeedback = get_string("modulename", "feedback"); +$str_delete_feedback = get_string('delete_template','feedback'); $PAGE->set_heading(format_string($course->fullname)); $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) { echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter boxwidthnormal'); @@ -104,37 +116,94 @@ if($shoulddelete == 1) { $mform->display(); echo $OUTPUT->box_end(); }else { + //first we get the own templates $templates = feedback_get_template_list($course, 'own'); - echo '
'; if(!is_array($templates)) { echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'), 'generalbox boxaligncenter'); }else { - echo ''; - echo ''; + echo $OUTPUT->heading(get_string('course'), 3); + 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) { - $suffix = $template->ispublic ? (' ('.get_string('public', 'feedback').')') : ''; - echo ''; - echo ''; + $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_course->add_data($data); } - echo '
'.get_string('templates', 'feedback').' 
'.$template->name.$suffix.''; - echo '
'; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo '
'; - echo '
'; + $table_course->finish_output(); + echo $OUTPUT->box_end(); } -?> -
- - - - -
-
-box(get_string('no_templates_available_yet', 'feedback'), 'generalbox boxaligncenter'); + }else { + 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(); diff --git a/mod/feedback/edit.php b/mod/feedback/edit.php index 4b7299e394e..c0bc9951df7 100644 --- a/mod/feedback/edit.php +++ b/mod/feedback/edit.php @@ -99,7 +99,7 @@ if(isset($create_template_formdata->savetemplate) && $create_template_formdata-> $savereturn = 'notsaved_name'; }else { //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; }else { $create_template_formdata->ispublic = 0; diff --git a/mod/feedback/edit_form.php b/mod/feedback/edit_form.php index ccea10f5e10..0d69d04bb6d 100644 --- a/mod/feedback/edit_form.php +++ b/mod/feedback/edit_form.php @@ -79,20 +79,29 @@ class feedback_edit_use_template_form extends moodleform { $templates_options = array(); $owntemplates = feedback_get_template_list($this->feedbackdata->course, 'own'); $publictemplates = feedback_get_template_list($this->feedbackdata->course, 'public'); - if($owntemplates OR $publictemplates){//get the templates - $templates_options[' '] = get_string('select'); - foreach($owntemplates as $template) { - if($template->ispublic) { - continue; + + $options = array(); + if($owntemplates or $publictemplates) { + $options[''] = array('' => get_string('choose')); + + 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()"'; - $elementgroup[] =& $mform->createElement('select', 'templateid', '', $templates_options, $attributes); - // buttons + $elementgroup[] =& $mform->createElement('selectgroups', 'templateid', '', $options, $attributes); $elementgroup[] =& $mform->createElement('submit', 'use_template', get_string('use_this_template', 'feedback')); }else { $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('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(SITEID === $this->feedbackdata->course->id) { - if(has_capability('mod/feedback:createpublictemplate', $this->feedbackdata->context)) { - $elementgroup[] =& $mform->createElement('checkbox', 'ispublic', get_string('public', 'feedback'), get_string('public', 'feedback')); - } + if(has_capability('mod/feedback:createpublictemplate', get_system_context())) { + $elementgroup[] =& $mform->createElement('checkbox', 'ispublic', get_string('public', 'feedback'), get_string('public', 'feedback')); } // buttons diff --git a/mod/feedback/item/label/lib.php b/mod/feedback/item/label/lib.php index 5e5df4c6478..6a1081a31dc 100644 --- a/mod/feedback/item/label/lib.php +++ b/mod/feedback/item/label/lib.php @@ -123,7 +123,11 @@ class feedback_item_label extends feedback_item_base { //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); + if($template->ispublic) { + $context = get_system_context(); + }else { + $context = get_context_instance(CONTEXT_COURSE, $template->course); + } $filearea = 'template'; }else { $cm = get_coursemodule_from_instance('feedback', $item->feedback); diff --git a/mod/feedback/lib.php b/mod/feedback/lib.php index 030cf810b2b..0342cf6f6e6 100644 --- a/mod/feedback/lib.php +++ b/mod/feedback/lib.php @@ -240,7 +240,6 @@ function feedback_pluginfile($course, $cm, $context, $filearea, $args, $forcedow return false; } - /** * this will delete a given instance. * all referenced data also will be deleted @@ -990,7 +989,7 @@ function feedback_create_template($courseid, $name, $ispublic = 0) { global $DB; $templ = new stdClass(); - $templ->course = $courseid; + $templ->course = ($ispublic ? 0 : $courseid); $templ->name = $name; $templ->ispublic = $ispublic; @@ -1023,9 +1022,14 @@ function feedback_save_as_template($feedback, $name, $ispublic = 0) { 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 - $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); $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)) { foreach($itemfiles as $ifile) { $file_record = new stdClass(); - $file_record->contextid = $c_context->id; + $file_record->contextid = $s_context->id; $file_record->component = 'mod_feedback'; $file_record->filearea = 'template'; $file_record->itemid = $t_item->id; @@ -1076,27 +1080,19 @@ function feedback_save_as_template($feedback, $name, $ispublic = 0) { * * @global object * @uses CONTEXT_COURSE - * @param int $id the templateid + * @param object $template the template * @return void */ -function feedback_delete_template($id) { +function feedback_delete_template($template) { global $DB; - $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))) { + //deleting the files from the item is done by feedback_delete_item + if($t_items = $DB->get_records("feedback_item", array("template"=>$template->id))) { foreach($t_items as $t_item) { - if ($templatefiles = $fs->get_area_files($context->id, 'mod_feedback', 'template', $t_item->id, "id", false)) { - $fs->delete_area_files($context->id, 'mod_feedback', 'template', $t_item->id); - } + feedback_delete_item($t_item->id, false, $template); } } - $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 feedback_item are in the feedback_context of the feedback if($template->ispublic) { - $c_context = get_context_instance(CONTEXT_COURSE, $template->course); + $s_context = get_system_context(); }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)); $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); //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) { $file_record = new stdClass(); $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'); break; 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; } return $templates; @@ -1402,9 +1398,10 @@ function feedback_update_item($item){ * @uses CONTEXT_MODULE * @param int $itemid * @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 */ -function feedback_delete_item($itemid, $renumber = true){ +function feedback_delete_item($itemid, $renumber = true, $template = false){ global $DB; @@ -1412,13 +1409,25 @@ function feedback_delete_item($itemid, $renumber = true){ //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($template) { + if($template->ispublic) { + $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)) { - $fs->delete_area_files($context->id, 'mod_feedback', 'item', $item->id); + 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); + } } $DB->delete_records("feedback_value", array("item"=>$itemid)); -- 2.43.0