MDL-53443 mod_feedback: templates code clean up and behat tests
authorMarina Glancy <marina@moodle.com>
Fri, 11 Mar 2016 14:42:41 +0000 (22:42 +0800)
committerMarina Glancy <marina@moodle.com>
Wed, 13 Apr 2016 01:21:33 +0000 (09:21 +0800)
15 files changed:
lib/tests/behat/behat_general.php
mod/feedback/classes/templates_table.php [new file with mode: 0644]
mod/feedback/delete_template.php
mod/feedback/delete_template_form.php [deleted file]
mod/feedback/edit.php
mod/feedback/edit_form.php
mod/feedback/import.php
mod/feedback/lang/en/feedback.php
mod/feedback/styles.css
mod/feedback/tests/behat/behat_mod_feedback.php
mod/feedback/tests/behat/export_import.feature [new file with mode: 0644]
mod/feedback/tests/behat/templates.feature [new file with mode: 0644]
mod/feedback/tests/fixtures/testexport.xml [new file with mode: 0644]
mod/feedback/use_templ.php
mod/feedback/use_templ_form.php

index 1e979a8..46ce3ef 100644 (file)
@@ -1266,7 +1266,7 @@ class behat_general extends behat_base {
      * @param string $link the text of the link.
      * @return string the content of the downloaded file.
      */
-    protected function download_file_from_link($link) {
+    public function download_file_from_link($link) {
         // Find the link.
         $linknode = $this->find_link($link);
         $this->ensure_node_is_visible($linknode);
diff --git a/mod/feedback/classes/templates_table.php b/mod/feedback/classes/templates_table.php
new file mode 100644 (file)
index 0000000..aa57224
--- /dev/null
@@ -0,0 +1,87 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Contains class mod_feedback_templates_table
+ *
+ * @package   mod_feedback
+ * @copyright 2016 Marina Glancy
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+global $CFG;
+require_once($CFG->libdir . '/tablelib.php');
+
+/**
+ * Class mod_feedback_templates_table
+ *
+ * @package   mod_feedback
+ * @copyright 2016 Marina Glancy
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class mod_feedback_templates_table extends flexible_table {
+
+    /**
+     * Constructor
+     * @param int $uniqueid all tables have to have a unique id, this is used
+     *      as a key when storing table properties like sort order in the session.
+     * @param moodle_url $baseurl
+     */
+    public function __construct($uniqueid, $baseurl) {
+        parent::__construct($uniqueid);
+
+        $tablecolumns = array('template', 'action');
+        $tableheaders = array(get_string('template', 'feedback'), '');
+
+        $this->set_attribute('class', 'templateslist');
+
+        $this->define_columns($tablecolumns);
+        $this->define_headers($tableheaders);
+        $this->define_baseurl($baseurl);
+        $this->column_class('template', 'template');
+        $this->column_class('action', 'action');
+
+        $this->sortable(false);
+    }
+
+    /**
+     * Displays the table with the given set of templates
+     * @param array $templates
+     */
+    public function display($templates) {
+        global $OUTPUT;
+        if (empty($templates)) {
+            echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'),
+                             'generalbox boxaligncenter');
+            return;
+        }
+
+        $this->setup();
+        $strdeletefeedback = get_string('delete_template', 'feedback');
+
+        foreach ($templates as $template) {
+            $data = array();
+            $data[] = format_string($template->name);
+            $url = new moodle_url($this->baseurl, array('deletetempl' => $template->id));
+
+            $data[] = $OUTPUT->single_button($url, $strdeletefeedback, 'post');
+            $this->add_data($data);
+        }
+        $this->finish_output();
+    }
+}
index 2d4dbed..7d73350 100644 (file)
 
 require_once("../../config.php");
 require_once("lib.php");
-require_once('delete_template_form.php');
-require_once($CFG->libdir.'/tablelib.php');
 
 $current_tab = 'templates';
 
 $id = required_param('id', PARAM_INT);
-$canceldelete = optional_param('canceldelete', false, PARAM_INT);
-$shoulddelete = optional_param('shoulddelete', false, PARAM_INT);
 $deletetempl = optional_param('deletetempl', false, PARAM_INT);
 
-$url = new moodle_url('/mod/feedback/delete_template.php', array('id'=>$id));
-if ($canceldelete !== false) {
-    $url->param('canceldelete', $canceldelete);
-}
-if ($shoulddelete !== false) {
-    $url->param('shoulddelete', $shoulddelete);
-}
-if ($deletetempl !== false) {
-    $url->param('deletetempl', $deletetempl);
-}
-$PAGE->set_url($url);
-
-if (($formdata = data_submitted()) AND !confirm_sesskey()) {
-    print_error('invalidsesskey');
-}
-
-if ($canceldelete == 1) {
-    $editurl = new moodle_url('/mod/feedback/edit.php', array('id'=>$id, 'do_show'=>'templates'));
-    redirect($editurl->out(false));
-}
-
-if (! $cm = get_coursemodule_from_id('feedback', $id)) {
-    print_error('invalidcoursemodule');
-}
-
-if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
-    print_error('coursemisconf');
-}
-
-if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
-    print_error('invalidcoursemodule');
-}
+$baseurl = new moodle_url('/mod/feedback/delete_template.php', array('id' => $id));
+$PAGE->set_url($baseurl);
 
+list($course, $cm) = get_course_and_cm_from_cmid($id, 'feedback');
 $context = context_module::instance($cm->id);
 
 require_login($course, true, $cm);
-
 require_capability('mod/feedback:deletetemplate', $context);
 
-$mform = new mod_feedback_delete_template_form();
-$newformdata = array('id'=>$id,
-                    'deletetempl'=>$deletetempl,
-                    'confirmdelete'=>'1');
+$feedback = $PAGE->activityrecord;
+$systemcontext = context_system::instance();
 
-$mform->set_data($newformdata);
-$formdata = $mform->get_data();
-
-$deleteurl = new moodle_url('/mod/feedback/delete_template.php', array('id'=>$id));
-
-if ($mform->is_cancelled()) {
-    redirect($deleteurl->out(false));
-}
-
-if (isset($formdata->confirmdelete) AND $formdata->confirmdelete == 1) {
-    if (!$template = $DB->get_record("feedback_template", array("id"=>$deletetempl))) {
-        print_error('error');
-    }
+// Process template deletion.
+if ($deletetempl && optional_param('confirm', 0, PARAM_BOOL) && confirm_sesskey()) {
+    $template = $DB->get_record('feedback_template', array('id' => $deletetempl), '*', MUST_EXIST);
 
     if ($template->ispublic) {
-        $systemcontext = context_system::instance();
         require_capability('mod/feedback:createpublictemplate', $systemcontext);
         require_capability('mod/feedback:deletetemplate', $systemcontext);
     }
 
     feedback_delete_template($template);
-    redirect($deleteurl->out(false));
+    redirect($baseurl, get_string('template_deleted', 'feedback'));
 }
 
 /// Print the page header
@@ -107,6 +60,8 @@ $strfeedbacks = get_string("modulenameplural", "feedback");
 $strfeedback  = get_string("modulename", "feedback");
 $strdeletefeedback = get_string('delete_template', 'feedback');
 
+navigation_node::override_active_url(new moodle_url('/mod/feedback/edit.php',
+        array('id' => $id, 'do_show' => 'templates')));
 $PAGE->set_heading($course->fullname);
 $PAGE->set_title($feedback->name);
 echo $OUTPUT->header();
@@ -119,100 +74,30 @@ require('tabs.php');
 ///////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////
 echo $OUTPUT->heading($strdeletefeedback, 3);
-if ($shoulddelete == 1) {
-
-    echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter boxwidthnormal');
-    echo html_writer::tag('p', get_string('confirmdeletetemplate', 'feedback'), array('class' => 'bold'));
-    $mform->display();
-    echo $OUTPUT->box_end();
+if ($deletetempl) {
+    $continueurl = new moodle_url($baseurl, array('deletetempl' => $deletetempl, 'confirm' => 1, 'sesskey' => sesskey()));
+    echo $OUTPUT->confirm(get_string('confirmdeletetemplate', 'feedback'), $continueurl, $baseurl);
 } else {
-    //first we get the own templates
+    // First we get the course templates.
     $templates = feedback_get_template_list($course, 'own');
-    if (!is_array($templates)) {
-        echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'),
-                         'generalbox boxaligncenter');
-    } else {
-        echo $OUTPUT->heading(get_string('course'), 4);
-        echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
-        $tablecolumns = array('template', 'action');
-        $tableheaders = array(get_string('template', 'feedback'), '');
-        $tablecourse = new flexible_table('feedback_template_course_table');
-
-        $tablecourse->define_columns($tablecolumns);
-        $tablecourse->define_headers($tableheaders);
-        $tablecourse->define_baseurl($deleteurl);
-        $tablecourse->column_style('action', 'width', '10%');
-
-        $tablecourse->sortable(false);
-        $tablecourse->set_attribute('width', '100%');
-        $tablecourse->set_attribute('class', 'generaltable');
-        $tablecourse->setup();
-
-        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, $strdeletefeedback, 'post');
-            $tablecourse->add_data($data);
-        }
-        $tablecourse->finish_output();
-        echo $OUTPUT->box_end();
-    }
-    //now we get the public templates if it is permitted
-    $systemcontext = context_system::instance();
+    echo $OUTPUT->box_start('coursetemplates');
+    echo $OUTPUT->heading(get_string('course'), 4);
+    $tablecourse = new mod_feedback_templates_table('feedback_template_course_table', $baseurl);
+    $tablecourse->display($templates);
+    echo $OUTPUT->box_end();
+    // Now we get the public templates if it is permitted.
     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'), 4);
-            echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
-            $tablecolumns = array('template', 'action');
-            $tableheaders = array(get_string('template', 'feedback'), '');
-            $tablepublic = new flexible_table('feedback_template_public_table');
-
-            $tablepublic->define_columns($tablecolumns);
-            $tablepublic->define_headers($tableheaders);
-            $tablepublic->define_baseurl($deleteurl);
-            $tablepublic->column_style('action', 'width', '10%');
-
-            $tablepublic->sortable(false);
-            $tablepublic->set_attribute('width', '100%');
-            $tablepublic->set_attribute('class', 'generaltable');
-            $tablepublic->setup();
-
-            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, $strdeletefeedback, 'post');
-                $tablepublic->add_data($data);
-            }
-            $tablepublic->finish_output();
-            echo $OUTPUT->box_end();
-        }
+        echo $OUTPUT->box_start('publictemplates');
+        echo $OUTPUT->heading(get_string('public', 'feedback'), 4);
+        $tablepublic = new mod_feedback_templates_table('feedback_template_public_table', $baseurl);
+        $tablepublic->display($templates);
+        echo $OUTPUT->box_end();
     }
 
-    echo $OUTPUT->box_start('boxaligncenter boxwidthnormal');
-    $url = new moodle_url($deleteurl, array(
-                                    'id'=>$id,
-                                    'canceldelete'=>1,
-                                    ));
-
+    $url = new moodle_url('/mod/feedback/edit.php', array('id' => $id, 'do_show' => 'templates'));
     echo $OUTPUT->single_button($url, get_string('back'), 'post');
-    echo $OUTPUT->box_end();
 }
 
 echo $OUTPUT->footer();
diff --git a/mod/feedback/delete_template_form.php b/mod/feedback/delete_template_form.php
deleted file mode 100644 (file)
index dd5e398..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-<?php
-// This file is part of Moodle - http://moodle.org/
-//
-// Moodle is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// Moodle is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
-
-/**
- * prints the form to confirm delete a completed
- *
- * @author Andreas Grabs
- * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
- * @package mod_feedback
- */
-
-//It must be included from a Moodle page
-if (!defined('MOODLE_INTERNAL')) {
-    die('Direct access to this script is forbidden.');
-}
-
-require_once($CFG->libdir.'/formslib.php');
-
-class mod_feedback_delete_template_form extends moodleform {
-    public function definition() {
-        $mform =& $this->_form;
-
-        // hidden elements
-        $mform->addElement('hidden', 'id');
-        $mform->setType('id', PARAM_INT);
-        $mform->addElement('hidden', 'deletetempl');
-        $mform->setType('deletetempl', PARAM_INT);
-        $mform->addElement('hidden', 'confirmdelete');
-        $mform->setType('confirmdelete', PARAM_INT);
-
-        //-------------------------------------------------------------------------------
-        // buttons
-        $this->add_action_buttons(true, get_string('yes'));
-
-    }
-}
index 00258be..218d566 100644 (file)
@@ -94,34 +94,20 @@ if ($switchitemrequired) {
     exit;
 }
 
-//The create_template-form
-$create_template_form = new feedback_edit_create_template_form();
-$create_template_form->set_feedbackdata(array('context'=>$context, 'course'=>$course));
-$create_template_form->set_form_elements();
-$create_template_form->set_data(array('id'=>$id, 'do_show'=>'templates'));
-$create_template_formdata = $create_template_form->get_data();
-if (isset($create_template_formdata->savetemplate) && $create_template_formdata->savetemplate == 1) {
-    //Check the capabilities to create templates.
-    if (!has_capability('mod/feedback:createprivatetemplate', $context) AND
-            !has_capability('mod/feedback:createpublictemplate', $context)) {
-        print_error('cannotsavetempl', 'feedback');
+// Process the create template form.
+$cancreatetemplates = has_capability('mod/feedback:createprivatetemplate', $context) ||
+            has_capability('mod/feedback:createpublictemplate', $context);
+$create_template_form = new feedback_edit_create_template_form(null, array('id' => $id));
+if ($data = $create_template_form->get_data()) {
+    // Check the capabilities to create templates.
+    if (!$cancreatetemplates) {
+        print_error('cannotsavetempl', 'feedback', $url);
     }
-    if (trim($create_template_formdata->templatename) == '') {
-        $savereturn = 'notsaved_name';
+    $ispublic = !empty($data->ispublic) ? 1 : 0;
+    if (!feedback_save_as_template($feedback, $data->templatename, $ispublic)) {
+        redirect($url, get_string('saving_failed', 'feedback'), null, \core\output\notification::NOTIFY_ERROR);
     } else {
-        //If the feedback is located on the frontpage then templates can be public.
-        if (has_capability('mod/feedback:createpublictemplate', context_system::instance())) {
-            $create_template_formdata->ispublic = isset($create_template_formdata->ispublic) ? 1 : 0;
-        } else {
-            $create_template_formdata->ispublic = 0;
-        }
-        if (!feedback_save_as_template($feedback,
-                                      $create_template_formdata->templatename,
-                                      $create_template_formdata->ispublic)) {
-            $savereturn = 'failed';
-        } else {
-            $savereturn = 'saved';
-        }
+        redirect($url, get_string('template_saved', 'feedback'), null, \core\output\notification::NOTIFY_SUCCESS);
     }
 }
 
@@ -141,10 +127,7 @@ $lastposition++;
 
 
 //The use_template-form
-$use_template_form = new feedback_edit_use_template_form('use_templ.php');
-$use_template_form->set_feedbackdata(array('course' => $course));
-$use_template_form->set_form_elements();
-$use_template_form->set_data(array('id'=>$id));
+$use_template_form = new feedback_edit_use_template_form('use_templ.php', array('course' => $course, 'id' => $id));
 
 //Print the page header.
 $strfeedbacks = get_string('modulenameplural', 'feedback');
@@ -178,36 +161,13 @@ require('tabs.php');
 ///////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////
 
-$savereturn=isset($savereturn)?$savereturn:'';
-
-//Print the messages.
-if ($savereturn == 'notsaved_name') {
-    echo '<p align="center"><b><font color="red">'.
-          get_string('name_required', 'feedback').
-          '</font></b></p>';
-}
-
-if ($savereturn == 'saved') {
-    echo '<p align="center"><b><font color="green">'.
-          get_string('template_saved', 'feedback').
-          '</font></b></p>';
-}
-
-if ($savereturn == 'failed') {
-    echo '<p align="center"><b><font color="red">'.
-          get_string('saving_failed', 'feedback').
-          '</font></b></p>';
-}
-
 ///////////////////////////////////////////////////////////////////////////
 ///Print the template-section.
 ///////////////////////////////////////////////////////////////////////////
 if ($do_show == 'templates') {
-    echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
     $use_template_form->display();
 
-    if (has_capability('mod/feedback:createprivatetemplate', $context) OR
-                has_capability('mod/feedback:createpublictemplate', $context)) {
+    if ($cancreatetemplates) {
         $deleteurl = new moodle_url('/mod/feedback/delete_template.php', array('id' => $id));
         $create_template_form->display();
         echo '<p><a href="'.$deleteurl->out().'">'.
@@ -226,7 +186,6 @@ if ($do_show == 'templates') {
             <a href="'.$importurl->out().'">'.get_string('import_questions', 'feedback').'</a>
         </p>';
     }
-    echo $OUTPUT->box_end();
 }
 ///////////////////////////////////////////////////////////////////////////
 ///Print the Item-Edit-section.
index 7de1cba..4d81cb8 100644 (file)
@@ -30,34 +30,15 @@ if (!defined('MOODLE_INTERNAL')) {
 require_once($CFG->libdir.'/formslib.php');
 
 class feedback_edit_use_template_form extends moodleform {
-    private $feedbackdata;
 
+    /**
+     * Form definition
+     */
     public function definition() {
-        $this->feedbackdata = new stdClass();
-        //this function can not be called, because not all data are available at this time
-        //I use set_form_elements instead
-    }
-
-    //this function set the data used in set_form_elements()
-    //in this form the only value have to set is course
-    //eg: array('course' => $course)
-    public function set_feedbackdata($data) {
-        if (is_array($data)) {
-            if (!isset($this->feedbackdata)) {
-                $this->feedbackdata = new stdClass();
-            }
-            foreach ($data as $key => $val) {
-                $this->feedbackdata->{$key} = $val;
-            }
-        }
-    }
-
-    //here the elements will be set
-    //this function have to be called manually
-    //the advantage is that the data are already set
-    public function set_form_elements() {
         $mform =& $this->_form;
 
+        $course = $this->_customdata['course'];
+
         $elementgroup = array();
         //headline
         $mform->addElement('header', 'using_templates', get_string('using_templates', 'feedback'));
@@ -67,17 +48,17 @@ class feedback_edit_use_template_form extends moodleform {
 
         // visible elements
         $templates_options = array();
-        $owntemplates = feedback_get_template_list($this->feedbackdata->course, 'own');
-        $publictemplates = feedback_get_template_list($this->feedbackdata->course, 'public');
+        $owntemplates = feedback_get_template_list($course, 'own');
+        $publictemplates = feedback_get_template_list($course, 'public');
 
         $options = array();
         if ($owntemplates or $publictemplates) {
-            $options[''] = array('' => get_string('choose'));
+            $options[''] = array('' => get_string('choosedots'));
 
             if ($owntemplates) {
                 $courseoptions = array();
                 foreach ($owntemplates as $template) {
-                    $courseoptions[$template->id] = $template->name;
+                    $courseoptions[$template->id] = format_string($template->name);
                 }
                 $options[get_string('course')] = $courseoptions;
             }
@@ -85,7 +66,7 @@ class feedback_edit_use_template_form extends moodleform {
             if ($publictemplates) {
                 $publicoptions = array();
                 foreach ($publictemplates as $template) {
-                    $publicoptions[$template->id] = $template->name;
+                    $publicoptions[$template->id] = format_string($template->name);
                 }
                 $options[get_string('public', 'feedback')] = $publicoptions;
             }
@@ -93,52 +74,38 @@ class feedback_edit_use_template_form extends moodleform {
             $attributes = 'onChange="M.core_formchangechecker.set_form_submitted(); this.form.submit()"';
             $elementgroup[] = $mform->createElement('selectgroups',
                                                      'templateid',
-                                                     '',
+                                                     get_string('using_templates', 'feedback'),
                                                      $options,
                                                      $attributes);
 
             $elementgroup[] = $mform->createElement('submit',
                                                      'use_template',
-                                                     get_string('use_this_template', 'feedback'));
+                                                     get_string('use_this_template', 'feedback'),
+                                                     array('class' => 'hiddenifjs'));
 
             $mform->addGroup($elementgroup, 'elementgroup', '', array(' '), false);
         } else {
             $mform->addElement('static', 'info', get_string('no_templates_available_yet', 'feedback'));
         }
+
+        $this->set_data(array('id' => $this->_customdata['id']));
     }
 }
 
 class feedback_edit_create_template_form extends moodleform {
-    private $feedbackdata;
 
+    /**
+     * Form definition
+     */
     public function definition() {
-    }
-
-    public function data_preprocessing(&$default_values) {
-        $default_values['templatename'] = '';
-    }
-
-    public function set_feedbackdata($data) {
-        if (is_array($data)) {
-            if (!isset($this->feedbackdata)) {
-                $this->feedbackdata = new stdClass();
-            }
-            foreach ($data as $key => $val) {
-                $this->feedbackdata->{$key} = $val;
-            }
-        }
-    }
-
-    public function set_form_elements() {
         $mform =& $this->_form;
 
         // hidden elements
         $mform->addElement('hidden', 'id');
         $mform->setType('id', PARAM_INT);
         $mform->addElement('hidden', 'do_show');
-        $mform->setType('do_show', PARAM_INT);
-        $mform->addElement('hidden', 'savetemplate', 1);
-        $mform->setType('savetemplate', PARAM_INT);
+        $mform->setType('do_show', PARAM_ALPHANUMEXT);
+        $mform->setConstant('do_show', 'templates');
 
         //headline
         $mform->addElement('header', 'creating_templates', get_string('creating_templates', 'feedback'));
@@ -146,10 +113,6 @@ class feedback_edit_create_template_form extends moodleform {
         // visible elements
         $elementgroup = array();
 
-        $elementgroup[] = $mform->createElement('static',
-                                                 'templatenamelabel',
-                                                 get_string('name', 'feedback'));
-
         $elementgroup[] = $mform->createElement('text',
                                                  'templatename',
                                                  get_string('name', 'feedback'),
@@ -175,6 +138,23 @@ class feedback_edit_create_template_form extends moodleform {
 
         $mform->setType('templatename', PARAM_TEXT);
 
+        $this->set_data(array('id' => $this->_customdata['id']));
+    }
+
+    /**
+     * Form validation
+     *
+     * @param array $data array of ("fieldname"=>value) of submitted data
+     * @param array $files array of uploaded files "element_name"=>tmp_file_path
+     * @return array of "element_name"=>"error_description" if there are errors,
+     *         or an empty array if everything is OK (true allowed for backwards compatibility too).
+     */
+    public function validation($data, $files) {
+        $errors = parent::validation($data, $files);
+        if (!isset($data['templatename']) || trim(strval($data['templatename'])) === '') {
+            $errors['elementgroup'] = get_string('name_required', 'feedback');
+        }
+        return $errors;
     }
 }
 
index 1339afd..f125a11 100644 (file)
@@ -97,6 +97,7 @@ $PAGE->set_title($feedback->name);
 echo $OUTPUT->header();
 echo $OUTPUT->heading(format_string($feedback->name));
 /// print the tabs
+$current_tab = 'templates';
 require('tabs.php');
 
 /// Print the main part of the page
index e2492c5..35415ba 100644 (file)
@@ -268,6 +268,7 @@ $string['switch_item_to_not_required'] = 'switch to: answer not required';
 $string['switch_item_to_required'] = 'switch to: answer required';
 $string['template'] = 'Template';
 $string['templates'] = 'Templates';
+$string['template_deleted'] = 'Template deleted';
 $string['template_saved'] = 'Template saved';
 $string['textarea'] = 'Longer text answer';
 $string['textarea_height'] = 'Number of lines';
index b7ec33e..c2175f5 100644 (file)
@@ -123,6 +123,11 @@ div img.feedback_bar_image {
 
 .path-mod-feedback #analysis-form label { display: inline; }
 
+.path-mod-feedback .templateslist td.cell.action,
+.path-mod-feedback .templateslist th.header.action {
+    width: 10%;
+}
+
 /* Responses navigation */
 .path-mod-feedback .response_navigation {
     margin: .5em 0;
index eaf2087..24df576 100644 (file)
@@ -98,4 +98,86 @@ class behat_mod_feedback extends behat_base {
         // Log out.
         $this->execute('behat_auth::i_log_out');
     }
+
+    /**
+     * Exports feedback and makes sure the export file is the same as in the fixture
+     *
+     * @Then /^following "(?P<link_string>(?:[^"]|\\")*)" should export feedback identical to "(?P<filename_string>(?:[^"]|\\")*)"$/
+     * @param string $link
+     * @param string $filename
+     */
+    public function following_should_export_feedback_identical_to($link, $filename) {
+        global $CFG;
+        $exception = new ExpectationException('Error while downloading data from ' . $link, $this->getSession());
+
+        // It will stop spinning once file is downloaded or time out.
+        $behatgeneralcontext = behat_context_helper::get('behat_general');
+        $result = $this->spin(
+            function($context, $args) use ($behatgeneralcontext) {
+                $link = $args['link'];
+                return $behatgeneralcontext->download_file_from_link($link);
+            },
+            array('link' => $link),
+            self::EXTENDED_TIMEOUT,
+            $exception
+        );
+
+        $this->compare_exports(file_get_contents($CFG->dirroot . '/' . $filename), $result);
+    }
+
+    /**
+     * Ensures two feedback export files are identical
+     *
+     * Maps the itemids and converts DEPENDITEM if necessary
+     *
+     * Throws ExpectationException if exports are different
+     *
+     * @param string $expected
+     * @param string $actual
+     * @throws ExpectationException
+     */
+    protected function compare_exports($expected, $actual) {
+        $dataexpected = xmlize($expected, 1, 'UTF-8');
+        $dataexpected = $dataexpected['FEEDBACK']['#']['ITEMS'][0]['#']['ITEM'];
+        $dataactual = xmlize($actual, 1, 'UTF-8');
+        $dataactual = $dataactual['FEEDBACK']['#']['ITEMS'][0]['#']['ITEM'];
+
+        if (count($dataexpected) != count($dataactual)) {
+            throw new ExpectationException('Expected ' . count($dataexpected) .
+                    ' items in the export file, found ' . count($dataactual), $this->getSession());
+        }
+
+        $itemmapping = array();
+        $itemactual = reset($dataactual);
+        foreach ($dataexpected as $idx => $itemexpected) {
+            // Map ITEMID and DEPENDITEM.
+            $itemmapping[intval($itemactual['#']['ITEMID'][0]['#'])] = intval($itemexpected['#']['ITEMID'][0]['#']);
+            $itemactual['#']['ITEMID'][0]['#'] = $itemexpected['#']['ITEMID'][0]['#'];
+            $expecteddependitem = $actualdependitem = 0;
+            if (isset($itemexpected['#']['DEPENDITEM'][0]['#'])) {
+                $expecteddependitem = intval($itemexpected['#']['DEPENDITEM'][0]['#']);
+            }
+            if (isset($itemactual['#']['DEPENDITEM'][0]['#'])) {
+                $actualdependitem = intval($itemactual['#']['DEPENDITEM'][0]['#']);
+            }
+            if ($expecteddependitem && !$actualdependitem) {
+                throw new ExpectationException('Expected DEPENDITEM in ' . ($idx + 1) . 'th item', $this->getSession());
+            }
+            if (!$expecteddependitem && $actualdependitem) {
+                throw new ExpectationException('Unexpected DEPENDITEM in ' . ($idx + 1) . 'th item', $this->getSession());
+            }
+            if ($expecteddependitem && $actualdependitem) {
+                if (!isset($itemmapping[$actualdependitem]) || $itemmapping[$actualdependitem] != $expecteddependitem) {
+                    throw new ExpectationException('Unknown DEPENDITEM in ' . ($idx + 1) . 'th item', $this->getSession());
+                }
+                $itemactual['#']['DEPENDITEM'][0]['#'] = $itemexpected['#']['DEPENDITEM'][0]['#'];
+            }
+            // Now, after mapping, $itemexpected should be exactly the same as $itemactual.
+            if (json_encode($itemexpected) !== json_encode($itemactual)) {
+                throw new ExpectationException('Actual ' . ($idx + 1) . 'th item does not match expected', $this->getSession());
+            }
+            // Get the next itemactual.
+            $itemactual = next($dataactual);
+        }
+    }
 }
diff --git a/mod/feedback/tests/behat/export_import.feature b/mod/feedback/tests/behat/export_import.feature
new file mode 100644 (file)
index 0000000..7faf73f
--- /dev/null
@@ -0,0 +1,132 @@
+@mod @mod_feedback
+Feature: Exporting and importing feedbacks
+  In order to quickly copy feedbacks across courses and sites
+  As a teacher
+  I need to be able to export and import feedbacks
+
+  Background:
+    Given the following "users" exist:
+      | username | firstname | lastname |
+      | teacher | Teacher   | 1        |
+    And the following "courses" exist:
+      | fullname | shortname |
+      | Course 1 | C1        |
+      | Course 2 | C2        |
+    And the following "course enrolments" exist:
+      | user     | course | role           |
+      | teacher | C1     | editingteacher |
+      | teacher | C1     | editingteacher |
+    And I log in as "admin"
+    And I navigate to "Manage activities" node in "Site administration > Plugins > Activity modules"
+    And I click on "Show" "link" in the "Feedback" "table_row"
+    And I log out
+    And the following "activities" exist:
+      | activity   | name                | course | idnumber    |
+      | feedback   | Learning experience | C1     | feedback0   |
+
+  Scenario: Export sample feedback and compare with the fixture
+    When I log in as "teacher"
+    And I follow "Course 1"
+    And I follow "Learning experience"
+    And I follow "Edit questions"
+    And I add a "Information" question to the feedback with:
+      | Question         | this is an information question |
+      | Label            | info                            |
+      | Information-Type | Course                          |
+    And I add a "Label" question to the feedback with:
+      | Contents | label text |
+    And I add a "Longer text answer" question to the feedback with:
+      | Question         | this is a longer text answer |
+      | Label            | longertext                   |
+      | Required         | 1                            |
+    And I add a "Multiple choice" question to the feedback with:
+      | Question         | this is a multiple choice 1 |
+      | Label            | multichoice1                |
+      | Multiple choice type | Multiple choice - single answer |
+      | Multiple choice values | option a\noption b\noption c  |
+    And I select "Add a page break" from the "Add question" singleselect
+    And I add a "Multiple choice" question to the feedback with:
+      | Question                       | this is a multiple choice 2        |
+      | Label                          | multichoice2                       |
+      | Multiple choice type           | Multiple choice - multiple answers |
+      | Hide the "Not selected" option | Yes                                |
+      | Multiple choice values         | option d\noption e\noption f       |
+      | Dependence item                | multichoice1                       |
+      | Dependence value               | option a                           |
+    And I add a "Multiple choice" question to the feedback with:
+      | Question                       | this is a multiple choice 3        |
+      | Label                          | multichoice3                       |
+      | Multiple choice type           | Multiple choice - single answer allowed (dropdownlist) |
+      | Multiple choice values         | option g\noption h\noption i                           |
+    And I add a "Multiple choice (rated)" question to the feedback with:
+      | Question               | this is a multiple choice rated |
+      | Label                  | multichoice4                    |
+      | Multiple choice type   | Multiple choice - single answer |
+      | Multiple choice values | 0/option k\n1/option l\n5/option m |
+    And I add a "Numeric answer" question to the feedback with:
+      | Question               | this is a numeric answer |
+      | Label                  | numeric                  |
+      | Range to               | 100                      |
+    And I add a "Short text answer" question to the feedback with:
+      | Question               | this is a short text answer |
+      | Label                  | shorttext                   |
+      | Maximum characters accepted | 200                    |
+    And I follow "Templates"
+    Then following "Export questions" should export feedback identical to "mod/feedback/tests/fixtures/testexport.xml"
+    And I log out
+
+  @javascript @_file_upload
+  Scenario: Import feedback deleting old items
+    When I log in as "teacher"
+    And I follow "Course 1"
+    And I follow "Learning experience"
+    And I follow "Edit questions"
+    And I add a "Numeric answer" question to the feedback with:
+      | Question               | Existing question |
+      | Label                  | numeric           |
+      | Range to               | 100               |
+    And I follow "Templates"
+    And I follow "Import questions"
+    And I upload "mod/feedback/tests/fixtures/testexport.xml" file to "File" filemanager
+    And I press "Yes"
+    And I follow "Edit questions"
+    Then I should not see "Existing question"
+    And I should see "this is an information question"
+    And I should see "label text"
+    And I should see "this is a longer text answer"
+    And I should see "this is a multiple choice 1"
+    And I should see "this is a multiple choice 2"
+    And I should see "this is a multiple choice 3"
+    And I should see "this is a multiple choice rated"
+    And I should see "this is a numeric answer"
+    And I should see "this is a short text answer"
+    And I log out
+
+  @javascript @_file_upload
+  Scenario: Import feedback appending new items
+    When I log in as "teacher"
+    And I follow "Course 1"
+    And I follow "Learning experience"
+    And I follow "Edit questions"
+    And I add a "Numeric answer" question to the feedback with:
+      | Question               | Existing question |
+      | Label                  | numeric           |
+      | Range to               | 100               |
+    And I follow "Templates"
+    And I follow "Import questions"
+    And I set the field "Append new items" to "1"
+    And I upload "mod/feedback/tests/fixtures/testexport.xml" file to "File" filemanager
+    And I press "Yes"
+    And I follow "Edit questions"
+    Then I should see "Existing question"
+    And "Existing question" "text" should appear before "this is an information question" "text"
+    And I should see "this is an information question"
+    And I should see "label text"
+    And I should see "this is a longer text answer"
+    And I should see "this is a multiple choice 1"
+    And I should see "this is a multiple choice 2"
+    And I should see "this is a multiple choice 3"
+    And I should see "this is a multiple choice rated"
+    And I should see "this is a numeric answer"
+    And I should see "this is a short text answer"
+    And I log out
diff --git a/mod/feedback/tests/behat/templates.feature b/mod/feedback/tests/behat/templates.feature
new file mode 100644 (file)
index 0000000..0e721e4
--- /dev/null
@@ -0,0 +1,204 @@
+@mod @mod_feedback
+Feature: Saving, using and deleting feedback templates
+  In order to quickly create feedbacks
+  As a manager
+  I need to be able to create feedback templates
+
+  Background:
+    Given the following "users" exist:
+      | username | firstname | lastname |
+      | teacher  | Teacher   | 1        |
+      | manager  | Manager   | 1        |
+    And the following "courses" exist:
+      | fullname | shortname |
+      | Course 1 | C1        |
+      | Course 2 | C2        |
+    And the following "course enrolments" exist:
+      | user     | course | role           |
+      | teacher  | C1     | editingteacher |
+      | teacher  | C2     | editingteacher |
+    And the following "system role assigns" exist:
+      | user    | course               | role    |
+      | manager | Acceptance test site | manager |
+    And I log in as "admin"
+    And I navigate to "Manage activities" node in "Site administration > Plugins > Activity modules"
+    And I click on "Show" "link" in the "Feedback" "table_row"
+    And I log out
+    And the following "activities" exist:
+      | activity   | name                         | course | idnumber    |
+      | feedback   | Learning experience course 1 | C1     | feedback1   |
+      | feedback   | Another feedback in course 1 | C1     | feedback2   |
+      | feedback   | Learning experience course 2 | C2     | feedback3   |
+    And I log in as "teacher"
+    And I follow "Course 1"
+    And I follow "Learning experience course 1"
+    And I follow "Edit questions"
+    And I add a "Multiple choice" question to the feedback with:
+      | Question         | this is a multiple choice 1 |
+      | Label            | multichoice1                |
+      | Multiple choice type | Multiple choice - single answer |
+      | Multiple choice values | option a\noption b\noption c  |
+    And I log out
+
+  Scenario: Teacher can save template and re-use it in the same course only
+    # Go to feedback templates and make sure none exist yet
+    When I log in as "teacher"
+    And I follow "Course 1"
+    And I follow "Learning experience course 1"
+    And I follow "Templates"
+    Then I should see "No templates available yet"
+    And "Use a template" "field" should not exist
+    And "Public" "field" should not exist
+    And I follow "Delete template..."
+    And "No templates available yet" "text" should exist in the ".coursetemplates" "css_element"
+    And ".publictemplates" "css_element" should not exist
+    And I press "Back"
+    # Save as a course template
+    And I set the field "Name" to "My first template"
+    And I press "Save as new template"
+    And I should see "Template saved"
+    And the "Use a template" select box should contain "My first template"
+    # Create a feedback from this template in the same course
+    And I follow "Course 1"
+    And I follow "Another feedback in course 1"
+    And I follow "Templates"
+    And I set the field "Use a template" to "My first template"
+    And I press "Use this template"
+    And I should see "this is a multiple choice 1"
+    And I press "Save changes"
+    And I follow "Edit questions"
+    And I should see "this is a multiple choice 1"
+    # Make sure this template is not available in another course
+    And I am on site homepage
+    And I follow "Course 2"
+    And I follow "Learning experience course 2"
+    And I follow "Templates"
+    And I should see "No templates available yet"
+    And "Use a template" "field" should not exist
+    And I log out
+
+  Scenario: Teacher can append template to existing questions or remove them
+    # Save feedback as a course template
+    When I log in as "teacher"
+    And I follow "Course 1"
+    And I follow "Learning experience course 1"
+    And I follow "Templates"
+    And I set the field "Name" to "My first template"
+    And I press "Save as new template"
+    # Add questions to another feedback
+    And I follow "Course 1"
+    And I follow "Another feedback in course 1"
+    And I follow "Edit questions"
+    And I add a "Multiple choice" question to the feedback with:
+      | Question         | What is your favourite subject |
+      | Label            | subjectchoice                  |
+      | Multiple choice type | Multiple choice - single answer   |
+      | Multiple choice values | Maths\bScience\nEnglish\nOther  |
+    # Import template appending items
+    And I follow "Templates"
+    And I set the field "Use a template" to "My first template"
+    And I press "Use this template"
+    And I set the field "Append new items" to "1"
+    And I press "Save changes"
+    And I follow "Edit questions"
+    Then "What is your favourite subject" "text" should appear before "this is a multiple choice 1" "text"
+    # Import template replacing items
+    And I follow "Templates"
+    And I set the field "Use a template" to "My first template"
+    And I press "Use this template"
+    And I set the field "Delete old items" to "1"
+    And I press "Save changes"
+    And I follow "Edit questions"
+    And I should see "this is a multiple choice 1"
+    And I should not see "What is your favourite subject"
+    And I log out
+
+  Scenario: Manager can save template as public and it will be available in any course
+    When I log in as "manager"
+    And I am on site homepage
+    And I follow "Course 1"
+    And I follow "Learning experience course 1"
+    And I follow "Templates"
+    And I set the field "Name" to "My first template"
+    And I set the field "Public" to "1"
+    And I press "Save as new template"
+    And I log out
+    And I log in as "teacher"
+    And I follow "Course 2"
+    And I follow "Learning experience course 2"
+    And I follow "Templates"
+    And I set the field "Use a template" to "My first template"
+    And I press "Use this template"
+    Then I should see "this is a multiple choice 1"
+    And I press "Save changes"
+    And I follow "Edit questions"
+    And I should see "this is a multiple choice 1"
+    And I log out
+
+  Scenario: Teacher can delete course templates but can not delete public templates
+    # Save feedback as both public and course template
+    When I log in as "manager"
+    And I am on site homepage
+    And I follow "Course 1"
+    And I follow "Learning experience course 1"
+    And I follow "Templates"
+    And I set the field "Name" to "My public template"
+    And I set the field "Public" to "1"
+    And I press "Save as new template"
+    And I set the field "Name" to "My course template"
+    And I press "Save as new template"
+    And I log out
+    # Login as teacher and try to delete templates
+    And I log in as "teacher"
+    And I follow "Course 1"
+    And I follow "Another feedback in course 1"
+    And I follow "Templates"
+    And I follow "Delete template..."
+    Then I should not see "My public template"
+    And ".publictemplates" "css_element" should not exist
+    And "My course template" "text" should exist in the ".coursetemplates" "css_element"
+    And I click on "Delete" "button" in the "My course template" "table_row"
+    And I should see "Are you sure you want to delete this template?"
+    And I press "Continue"
+    And I should see "Template deleted"
+    And "My course template" "text" should not exist in the ".coursetemplates" "css_element"
+    And "No templates available yet" "text" should exist in the ".coursetemplates" "css_element"
+    And I press "Back"
+    And the "Use a template" select box should not contain "My course template"
+    And the "Use a template" select box should contain "My public template"
+    And I log out
+
+  Scenario: Manager can delete both course and public templates
+    # Save feedback as both public and course template
+    When I log in as "manager"
+    And I am on site homepage
+    And I follow "Course 1"
+    And I follow "Learning experience course 1"
+    And I follow "Templates"
+    And I set the field "Name" to "My public template"
+    And I set the field "Public" to "1"
+    And I press "Save as new template"
+    And I set the field "Name" to "My course template"
+    And I press "Save as new template"
+    # Delete course template
+    And I follow "Delete template..."
+    Then "My public template" "text" should exist in the ".publictemplates" "css_element"
+    And "My course template" "text" should exist in the ".coursetemplates" "css_element"
+    And I click on "Delete" "button" in the "My course template" "table_row"
+    And I should see "Are you sure you want to delete this template?"
+    And I press "Continue"
+    And I should see "Template deleted"
+    And "My course template" "text" should not exist in the ".coursetemplates" "css_element"
+    And "No templates available yet" "text" should exist in the ".coursetemplates" "css_element"
+    # Delete public template
+    And "My public template" "text" should exist in the ".publictemplates" "css_element"
+    And I click on "Delete" "button" in the "My public template" "table_row"
+    And I should see "Are you sure you want to delete this template?"
+    And I press "Continue"
+    And I should see "Template deleted"
+    And "My public template" "text" should not exist in the ".publictemplates" "css_element"
+    And "No templates available yet" "text" should exist in the ".publictemplates" "css_element"
+    And I press "Back"
+    And I should see "No templates available yet"
+    And "Use a template" "field" should not exist
+    And I log out
diff --git a/mod/feedback/tests/fixtures/testexport.xml b/mod/feedback/tests/fixtures/testexport.xml
new file mode 100644 (file)
index 0000000..6ad7759
--- /dev/null
@@ -0,0 +1,235 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<FEEDBACK VERSION="200701" COMMENT="XML-Importfile for mod/feedback">
+     <ITEMS>
+          <ITEM TYPE="info" REQUIRED="0">
+               <ITEMID>
+                    <![CDATA[1000]]>
+               </ITEMID>
+               <ITEMTEXT>
+                    <![CDATA[this is an information question]]>
+               </ITEMTEXT>
+               <ITEMLABEL>
+                    <![CDATA[info]]>
+               </ITEMLABEL>
+               <PRESENTATION>
+                    <![CDATA[2]]>
+               </PRESENTATION>
+               <OPTIONS>
+                    <![CDATA[]]>
+               </OPTIONS>
+               <DEPENDITEM>
+                    <![CDATA[0]]>
+               </DEPENDITEM>
+               <DEPENDVALUE>
+                    <![CDATA[]]>
+               </DEPENDVALUE>
+          </ITEM>
+          <ITEM TYPE="label" REQUIRED="0">
+               <ITEMID>
+                    <![CDATA[1001]]>
+               </ITEMID>
+               <ITEMTEXT>
+                    <![CDATA[label]]>
+               </ITEMTEXT>
+               <ITEMLABEL>
+                    <![CDATA[]]>
+               </ITEMLABEL>
+               <PRESENTATION>
+                    <![CDATA[label text]]>
+               </PRESENTATION>
+               <OPTIONS>
+                    <![CDATA[]]>
+               </OPTIONS>
+               <DEPENDITEM>
+                    <![CDATA[0]]>
+               </DEPENDITEM>
+               <DEPENDVALUE>
+                    <![CDATA[]]>
+               </DEPENDVALUE>
+          </ITEM>
+          <ITEM TYPE="textarea" REQUIRED="1">
+               <ITEMID>
+                    <![CDATA[1002]]>
+               </ITEMID>
+               <ITEMTEXT>
+                    <![CDATA[this is a longer text answer]]>
+               </ITEMTEXT>
+               <ITEMLABEL>
+                    <![CDATA[longertext]]>
+               </ITEMLABEL>
+               <PRESENTATION>
+                    <![CDATA[30|5]]>
+               </PRESENTATION>
+               <OPTIONS>
+                    <![CDATA[]]>
+               </OPTIONS>
+               <DEPENDITEM>
+                    <![CDATA[0]]>
+               </DEPENDITEM>
+               <DEPENDVALUE>
+                    <![CDATA[]]>
+               </DEPENDVALUE>
+          </ITEM>
+          <ITEM TYPE="multichoice" REQUIRED="0">
+               <ITEMID>
+                    <![CDATA[1003]]>
+               </ITEMID>
+               <ITEMTEXT>
+                    <![CDATA[this is a multiple choice 1]]>
+               </ITEMTEXT>
+               <ITEMLABEL>
+                    <![CDATA[multichoice1]]>
+               </ITEMLABEL>
+               <PRESENTATION>
+                    <![CDATA[r>>>>>option a|option b|option c]]>
+               </PRESENTATION>
+               <OPTIONS>
+                    <![CDATA[]]>
+               </OPTIONS>
+               <DEPENDITEM>
+                    <![CDATA[0]]>
+               </DEPENDITEM>
+               <DEPENDVALUE>
+                    <![CDATA[]]>
+               </DEPENDVALUE>
+          </ITEM>
+          <ITEM TYPE="pagebreak" REQUIRED="0">
+               <ITEMID>
+                    <![CDATA[1004]]>
+               </ITEMID>
+               <ITEMTEXT>
+                    <![CDATA[]]>
+               </ITEMTEXT>
+               <ITEMLABEL>
+                    <![CDATA[]]>
+               </ITEMLABEL>
+               <PRESENTATION>
+                    <![CDATA[]]>
+               </PRESENTATION>
+               <OPTIONS>
+                    <![CDATA[]]>
+               </OPTIONS>
+               <DEPENDITEM>
+                    <![CDATA[0]]>
+               </DEPENDITEM>
+               <DEPENDVALUE>
+                    <![CDATA[]]>
+               </DEPENDVALUE>
+          </ITEM>
+          <ITEM TYPE="multichoice" REQUIRED="0">
+               <ITEMID>
+                    <![CDATA[1005]]>
+               </ITEMID>
+               <ITEMTEXT>
+                    <![CDATA[this is a multiple choice 2]]>
+               </ITEMTEXT>
+               <ITEMLABEL>
+                    <![CDATA[multichoice2]]>
+               </ITEMLABEL>
+               <PRESENTATION>
+                    <![CDATA[c>>>>>option d|option e|option f]]>
+               </PRESENTATION>
+               <OPTIONS>
+                    <![CDATA[h]]>
+               </OPTIONS>
+               <DEPENDITEM>
+                    <![CDATA[1003]]>
+               </DEPENDITEM>
+               <DEPENDVALUE>
+                    <![CDATA[option a]]>
+               </DEPENDVALUE>
+          </ITEM>
+          <ITEM TYPE="multichoice" REQUIRED="0">
+               <ITEMID>
+                    <![CDATA[1006]]>
+               </ITEMID>
+               <ITEMTEXT>
+                    <![CDATA[this is a multiple choice 3]]>
+               </ITEMTEXT>
+               <ITEMLABEL>
+                    <![CDATA[multichoice3]]>
+               </ITEMLABEL>
+               <PRESENTATION>
+                    <![CDATA[d>>>>>option g|option h|option i]]>
+               </PRESENTATION>
+               <OPTIONS>
+                    <![CDATA[]]>
+               </OPTIONS>
+               <DEPENDITEM>
+                    <![CDATA[0]]>
+               </DEPENDITEM>
+               <DEPENDVALUE>
+                    <![CDATA[]]>
+               </DEPENDVALUE>
+          </ITEM>
+          <ITEM TYPE="multichoicerated" REQUIRED="0">
+               <ITEMID>
+                    <![CDATA[1007]]>
+               </ITEMID>
+               <ITEMTEXT>
+                    <![CDATA[this is a multiple choice rated]]>
+               </ITEMTEXT>
+               <ITEMLABEL>
+                    <![CDATA[multichoice4]]>
+               </ITEMLABEL>
+               <PRESENTATION>
+                    <![CDATA[r>>>>>0####option k|1####option l|5####option m]]>
+               </PRESENTATION>
+               <OPTIONS>
+                    <![CDATA[]]>
+               </OPTIONS>
+               <DEPENDITEM>
+                    <![CDATA[0]]>
+               </DEPENDITEM>
+               <DEPENDVALUE>
+                    <![CDATA[]]>
+               </DEPENDVALUE>
+          </ITEM>
+          <ITEM TYPE="numeric" REQUIRED="0">
+               <ITEMID>
+                    <![CDATA[1008]]>
+               </ITEMID>
+               <ITEMTEXT>
+                    <![CDATA[this is a numeric answer]]>
+               </ITEMTEXT>
+               <ITEMLABEL>
+                    <![CDATA[numeric]]>
+               </ITEMLABEL>
+               <PRESENTATION>
+                    <![CDATA[-|100]]>
+               </PRESENTATION>
+               <OPTIONS>
+                    <![CDATA[]]>
+               </OPTIONS>
+               <DEPENDITEM>
+                    <![CDATA[0]]>
+               </DEPENDITEM>
+               <DEPENDVALUE>
+                    <![CDATA[]]>
+               </DEPENDVALUE>
+          </ITEM>
+          <ITEM TYPE="textfield" REQUIRED="0">
+               <ITEMID>
+                    <![CDATA[1009]]>
+               </ITEMID>
+               <ITEMTEXT>
+                    <![CDATA[this is a short text answer]]>
+               </ITEMTEXT>
+               <ITEMLABEL>
+                    <![CDATA[shorttext]]>
+               </ITEMLABEL>
+               <PRESENTATION>
+                    <![CDATA[30|200]]>
+               </PRESENTATION>
+               <OPTIONS>
+                    <![CDATA[]]>
+               </OPTIONS>
+               <DEPENDITEM>
+                    <![CDATA[0]]>
+               </DEPENDITEM>
+               <DEPENDVALUE>
+                    <![CDATA[]]>
+               </DEPENDVALUE>
+          </ITEM>
+     </ITEMS>
+</FEEDBACK>
index 073b783..08dab5f 100644 (file)
@@ -28,51 +28,30 @@ require_once('use_templ_form.php');
 
 $id = required_param('id', PARAM_INT);
 $templateid = optional_param('templateid', false, PARAM_INT);
-$deleteolditems = optional_param('deleteolditems', 0, PARAM_INT);
 
 if (!$templateid) {
     redirect('edit.php?id='.$id);
 }
 
 $url = new moodle_url('/mod/feedback/use_templ.php', array('id'=>$id, 'templateid'=>$templateid));
-if ($deleteolditems !== 0) {
-    $url->param('deleteolditems', $deleteolditems);
-}
 $PAGE->set_url($url);
 
-if (! $cm = get_coursemodule_from_id('feedback', $id)) {
-    print_error('invalidcoursemodule');
-}
-
-if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
-    print_error('coursemisconf');
-}
-
-if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
-    print_error('invalidcoursemodule');
-}
-
+list($course, $cm) = get_course_and_cm_from_cmid($id, 'feedback');
 $context = context_module::instance($cm->id);
 
 require_login($course, true, $cm);
 
+$feedback = $PAGE->activityrecord;
+
 require_capability('mod/feedback:edititems', $context);
 
 $mform = new mod_feedback_use_templ_form();
-$newformdata = array('id'=>$id,
-                    'templateid'=>$templateid,
-                    'confirmadd'=>'1',
-                    'deleteolditems'=>'1',
-                    'do_show'=>'edit');
-$mform->set_data($newformdata);
-$formdata = $mform->get_data();
+$mform->set_data(array('id' => $id, 'templateid' => $templateid));
 
 if ($mform->is_cancelled()) {
     redirect('edit.php?id='.$id.'&do_show=templates');
-}
-
-if (isset($formdata->confirmadd) AND $formdata->confirmadd == 1) {
-    feedback_items_from_template($feedback, $templateid, $deleteolditems);
+} else if ($formdata = $mform->get_data()) {
+    feedback_items_from_template($feedback, $templateid, $formdata->deleteolditems);
     redirect('edit.php?id=' . $id);
 }
 
@@ -80,8 +59,8 @@ if (isset($formdata->confirmadd) AND $formdata->confirmadd == 1) {
 $strfeedbacks = get_string("modulenameplural", "feedback");
 $strfeedback  = get_string("modulename", "feedback");
 
-$PAGE->navbar->add($strfeedbacks, new moodle_url('/mod/feedback/index.php', array('id'=>$course->id)));
-$PAGE->navbar->add(format_string($feedback->name));
+navigation_node::override_active_url(new moodle_url('/mod/feedback/edit.php',
+        array('id' => $id, 'do_show' => 'templates')));
 $PAGE->set_heading($course->fullname);
 $PAGE->set_title($feedback->name);
 echo $OUTPUT->header();
@@ -92,13 +71,10 @@ echo $OUTPUT->header();
 ///////////////////////////////////////////////////////////////////////////
 echo $OUTPUT->heading(format_string($feedback->name));
 
-echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
-echo $OUTPUT->heading(get_string('confirmusetemplate', 'feedback'), 3);
+echo $OUTPUT->heading(get_string('confirmusetemplate', 'feedback'), 4);
 
 $mform->display();
 
-echo $OUTPUT->box_end();
-
 $templateitems = $DB->get_records('feedback_item', array('template'=>$templateid), 'position');
 if (is_array($templateitems)) {
     $templateitems = array_values($templateitems);
index 08c4ff6..4f535a8 100644 (file)
@@ -32,13 +32,11 @@ class mod_feedback_use_templ_form extends moodleform {
     public function definition() {
         $mform =& $this->_form;
 
-        //headline
-        $mform->addElement('header', 'general', get_string('general', 'form'));
-
         // visible elements
-        $mform->addElement('radio', 'deleteolditems', '1)', get_string('delete_old_items', 'feedback'), 1);
-        $mform->addElement('radio', 'deleteolditems', '2)', get_string('append_new_items', 'feedback'), 0);
+        $mform->addElement('radio', 'deleteolditems', '', get_string('delete_old_items', 'feedback'), 1);
+        $mform->addElement('radio', 'deleteolditems', '', get_string('append_new_items', 'feedback'), 0);
         $mform->setType('deleteolditems', PARAM_INT);
+        $mform->setDefault('deleteolditems', 1);
 
         // hidden elements
         $mform->addElement('hidden', 'id');
@@ -47,8 +45,7 @@ class mod_feedback_use_templ_form extends moodleform {
         $mform->setType('templateid', PARAM_INT);
         $mform->addElement('hidden', 'do_show');
         $mform->setType('do_show', PARAM_INT);
-        $mform->addElement('hidden', 'confirmadd');
-        $mform->setType('confirmadd', PARAM_INT);
+        $mform->setConstant('do_show', 'edit');
 
         //-------------------------------------------------------------------------------
         // buttons