Permalink
Browse files

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

  • Loading branch information...
1 parent e70dab0 commit 7b1126fd2004ab304437cf479bc966cedbf688db @grabs grabs committed Oct 11, 2011
Showing with 161 additions and 73 deletions.
  1. +95 −26 mod/feedback/delete_template.php
  2. +1 −1 mod/feedback/edit.php
  3. +21 −15 mod/feedback/edit_form.php
  4. +5 −1 mod/feedback/item/label/lib.php
  5. +39 −30 mod/feedback/lib.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(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,45 +108,102 @@
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
-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');
echo $OUTPUT->heading(get_string('confirmdeletetemplate', 'feedback'));
$mform->display();
echo $OUTPUT->box_end();
}else {
+ //first we get the own templates
$templates = feedback_get_template_list($course, 'own');
- echo '<div class="mdl-align">';
if(!is_array($templates)) {
echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'), 'generalbox boxaligncenter');
}else {
- echo '<table width="30%">';
- echo '<tr><th>'.get_string('templates', 'feedback').'</th><th>&nbsp;</th></tr>';
+ 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 '<tr><td align="left">'.$template->name.$suffix.'</td>';
- echo '<td align="center">';
- echo '<form action="delete_template.php" method="post">';
- echo '<input title="'.get_string('delete_template','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/delete') . '" hspace="1" height="11" width="11" border="0" />';
- echo '<input type="hidden" name="deletetempl" value="'.$template->id.'" />';
- echo '<input type="hidden" name="shoulddelete" value="1" />';
- echo '<input type="hidden" name="id" value="'.$id.'" />';
- echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
- echo '</form>';
- echo '</td></tr>';
+ $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 '</table>';
+ $table_course->finish_output();
+ echo $OUTPUT->box_end();
}
-?>
- <form name="frm" action="delete_template.php" method="post">
- <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
- <input type="hidden" name="id" value="<?php echo $id;?>" />
- <input type="hidden" name="canceldelete" value="0" />
- <button type="button" onclick="this.form.canceldelete.value=1;this.form.submit();"><?php print_string('cancel');?></button>
- </form>
- </div>
-<?php
+ //now we get the public templates if it is permitted
+ $systemcontext = get_system_context();
+ if(has_capability('mod/feedback:createpublictemplate', $systemcontext) AND
+ has_capability('mod/feedback:deletetemplate', $systemcontext)) {
+ $templates = feedback_get_template_list($course, 'public');
+ if(!is_array($templates)) {
+ echo $OUTPUT->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();
View
@@ -99,7 +99,7 @@
$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;
View
@@ -79,20 +79,29 @@ function set_form_elements(){
$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 @@ function set_form_elements(){
$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
@@ -123,7 +123,11 @@ function print_item($item){
//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);
View
@@ -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,23 +1398,36 @@ 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;
$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($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));

0 comments on commit 7b1126f

Please sign in to comment.