edit.php 17.1 KB
Newer Older
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 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/>.
16

17
/**
18
19
20
21
 * prints the form to edit the feedback items such moving, deleting and so on
 *
 * @author Andreas Grabs
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22
 * @package mod_feedback
23
 */
24

25
26
require_once('../../config.php');
require_once('lib.php');
27
require_once('edit_form.php');
28

29
feedback_init_feedback_session();
30

31
$id = required_param('id', PARAM_INT);
32

33
if (($formdata = data_submitted()) AND !confirm_sesskey()) {
34
35
    print_error('invalidsesskey');
}
36

37
38
39
40
41
42
$do_show = optional_param('do_show', 'edit', PARAM_ALPHA);
$moveupitem = optional_param('moveupitem', false, PARAM_INT);
$movedownitem = optional_param('movedownitem', false, PARAM_INT);
$moveitem = optional_param('moveitem', false, PARAM_INT);
$movehere = optional_param('movehere', false, PARAM_INT);
$switchitemrequired = optional_param('switchitemrequired', false, PARAM_INT);
43

44
$current_tab = $do_show;
45

Andreas Grabs's avatar
Andreas Grabs committed
46
47
$url = new moodle_url('/mod/feedback/edit.php', array('id'=>$id, 'do_show'=>$do_show));

48
49
50
51
if (! $cm = get_coursemodule_from_id('feedback', $id)) {
    print_error('invalidcoursemodule');
}

52
if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
53
54
55
    print_error('coursemisconf');
}

56
if (! $feedback = $DB->get_record('feedback', array('id'=>$cm->instance))) {
57
    print_error('invalidcoursemodule');
58
}
59

60
$context = context_module::instance($cm->id);
61

62
require_login($course, true, $cm);
63

64
require_capability('mod/feedback:edititems', $context);
65

66
//Move up/down items
67
if ($moveupitem) {
68
69
70
    $item = $DB->get_record('feedback_item', array('id'=>$moveupitem));
    feedback_moveup_item($item);
}
71
if ($movedownitem) {
72
73
74
75
    $item = $DB->get_record('feedback_item', array('id'=>$movedownitem));
    feedback_movedown_item($item);
}

76
//Moving of items
77
if ($movehere && isset($SESSION->feedback->moving->movingitem)) {
78
79
80
81
    $item = $DB->get_record('feedback_item', array('id'=>$SESSION->feedback->moving->movingitem));
    feedback_move_item($item, intval($movehere));
    $moveitem = false;
}
82
if ($moveitem) {
83
84
85
86
87
88
89
    $item = $DB->get_record('feedback_item', array('id'=>$moveitem));
    $SESSION->feedback->moving->shouldmoving = 1;
    $SESSION->feedback->moving->movingitem = $moveitem;
} else {
    unset($SESSION->feedback->moving);
}

90
if ($switchitemrequired) {
91
92
    $item = $DB->get_record('feedback_item', array('id'=>$switchitemrequired));
    @feedback_switch_item_required($item);
Andreas Grabs's avatar
Andreas Grabs committed
93
    redirect($url->out(false));
94
95
96
    exit;
}

97
98
99
100
101
102
103
104
// 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);
105
    }
106
107
108
    $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);
109
    } else {
110
        redirect($url, get_string('template_saved', 'feedback'), null, \core\output\notification::NOTIFY_SUCCESS);
111
    }
112
113
}

114
//Get the feedbackitems
115
116
$lastposition = 0;
$feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id), 'position');
117
if (is_array($feedbackitems)) {
118
    $feedbackitems = array_values($feedbackitems);
119
    if (count($feedbackitems) > 0) {
120
121
        $lastitem = $feedbackitems[count($feedbackitems)-1];
        $lastposition = $lastitem->position;
122
    } else {
123
        $lastposition = 0;
124
    }
125
126
127
128
}
$lastposition++;


129
//The use_template-form
130
$use_template_form = new feedback_edit_use_template_form('use_templ.php', array('course' => $course, 'id' => $id));
131

132
//Print the page header.
133
134
$strfeedbacks = get_string('modulenameplural', 'feedback');
$strfeedback  = get_string('modulename', 'feedback');
135

136
$PAGE->set_url('/mod/feedback/edit.php', array('id'=>$cm->id, 'do_show'=>$do_show));
137
138
$PAGE->set_heading($course->fullname);
$PAGE->set_title($feedback->name);
139

140
//Adding the javascript module for the items dragdrop.
141
if (count($feedbackitems) > 1) {
142
    if ($do_show == 'edit') {
143
144
145
146
147
148
149
150
        $PAGE->requires->strings_for_js(array(
               'pluginname',
               'move_item',
               'position',
            ), 'feedback');
        $PAGE->requires->yui_module('moodle-mod_feedback-dragdrop', 'M.mod_feedback.init_dragdrop',
                array(array('cmid' => $cm->id)));
    }
151
152
}

153
echo $OUTPUT->header();
154
echo $OUTPUT->heading(format_string($feedback->name));
155
156

/// print the tabs
157
require('tabs.php');
158

159
/// Print the main part of the page.
160
161
162
163
164
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
165
///Print the template-section.
166
///////////////////////////////////////////////////////////////////////////
167
if ($do_show == 'templates') {
168
169
    $use_template_form->display();

170
    if ($cancreatetemplates) {
171
        $deleteurl = new moodle_url('/mod/feedback/delete_template.php', array('id' => $id));
172
        $create_template_form->display();
173
174
175
176
        echo '<p><a href="'.$deleteurl->out().'">'.
             get_string('delete_templates', 'feedback').
             '</a></p>';
    } else {
177
        echo '&nbsp;';
178
    }
179

180
181
182
    if (has_capability('mod/feedback:edititems', $context)) {
        $urlparams = array('action'=>'exportfile', 'id'=>$id);
        $exporturl = new moodle_url('/mod/feedback/export.php', $urlparams);
Andreas Grabs's avatar
Andreas Grabs committed
183
        $importurl = new moodle_url('/mod/feedback/import.php', array('id'=>$id));
184
        echo '<p>
Andreas Grabs's avatar
Andreas Grabs committed
185
186
            <a href="'.$exporturl->out().'">'.get_string('export_questions', 'feedback').'</a>/
            <a href="'.$importurl->out().'">'.get_string('import_questions', 'feedback').'</a>
187
        </p>';
188
    }
189
190
}
///////////////////////////////////////////////////////////////////////////
191
///Print the Item-Edit-section.
192
///////////////////////////////////////////////////////////////////////////
193
if ($do_show == 'edit') {
194

195
196
197
198
    $select = new single_select(new moodle_url('/mod/feedback/edit_item.php', array('cmid' => $id, 'position' => $lastposition)),
        'typ', feedback_load_feedback_items_options());
    $select->label = get_string('add_item', 'mod_feedback');
    echo $OUTPUT->render($select);
199

200
    if (is_array($feedbackitems)) {
201
        $itemnr = 0;
202

203
        $align = right_to_left() ? 'right' : 'left';
204

205
        $helpbutton = $OUTPUT->help_icon('preview', 'feedback');
206

207
        echo $OUTPUT->heading(get_string('preview', 'feedback').$helpbutton, 3);
208
209
210
211
212
        if (isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->shouldmoving == 1) {
            $anker = '<a href="edit.php?id='.$id.'">';
            $anker .= get_string('cancel_moving', 'feedback');
            $anker .= '</a>';
            echo $OUTPUT->heading($anker);
213
214
        }

215
        //Check, if there exists required-elements.
216
217
218
        $params = array('feedback' => $feedback->id, 'required' => 1);
        $countreq = $DB->count_records('feedback_item', $params);
        if ($countreq > 0) {
219
220
221
222
            echo '<div class="fdescription required">';
            echo get_string('somefieldsrequired', 'form', '<img alt="'.get_string('requiredelement', 'form').
                '" src="'.$OUTPUT->pix_url('req') .'" class="req" />');
            echo '</div>';
223
224
        }

225
        //Use list instead a table
226
        echo $OUTPUT->box_start('feedback_items');
227
228
229
        if (isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->shouldmoving == 1) {
            $moveposition = 1;
            $movehereurl = new moodle_url($url, array('movehere'=>$moveposition));
230
            //Only shown if shouldmoving = 1
231
232
233
234
235
236
237
238
239
            echo $OUTPUT->box_start('feedback_item_box_'.$align.' clipboard');
            $buttonlink = $movehereurl->out();
            $strbutton = get_string('move_here', 'feedback');
            $src = $OUTPUT->pix_url('movehere');
            echo '<a title="'.$strbutton.'" href="'.$buttonlink.'">
                    <img class="movetarget" alt="'.$strbutton.'" src="'.$src.'" />
                  </a>';
            echo $OUTPUT->box_end();
        }
240
        //Print the inserted items
241
        $itempos = 0;
242
        echo '<div id="feedback_dragarea">'; //The container for the dragging area
243
        echo '<ul id="feedback_draglist">'; //The list what includes the draggable items
244
245
        foreach ($feedbackitems as $feedbackitem) {
            $itempos++;
246
            //Hiding the item to move
247
248
            if (isset($SESSION->feedback->moving)) {
                if ($SESSION->feedback->moving->movingitem == $feedbackitem->id) {
249
                    continue;
250
                }
251
            }
252
            //Here come the draggable items, each one in a single li-element.
253
254
            echo '<li class="feedback_itemlist generalbox" id="feedback_item_'.$feedbackitem->id.'">';
            echo '<span class="spinnertest"> </span>';
255
256
257
258
259
            if ($feedbackitem->dependitem > 0) {
                $dependstyle = ' feedback_depend';
            } else {
                $dependstyle = '';
            }
260
261
            echo $OUTPUT->box_start('feedback_item_box_'.$align.$dependstyle,
                                    'feedback_item_box_'.$feedbackitem->id);
262
            //Items without value only are labels
263
264
265
266
            if ($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
                $itemnr++;
                echo $OUTPUT->box_start('feedback_item_number_'.$align);
                echo $itemnr;
267
                echo $OUTPUT->box_end();
268
            }
269
            echo $OUTPUT->box_start('box boxalign_'.$align);
270
            echo $OUTPUT->box_start('feedback_item_commands_'.$align);
271
            echo '<span class="feedback_item_commands position">';
272
273
            echo '('.get_string('position', 'feedback').':'.$itempos .')';
            echo '</span>';
274
            //Print the moveup-button
275
276
277
278
279
280
281
282
283
284
            if ($feedbackitem->position > 1) {
                echo '<span class="feedback_item_command_moveup">';
                $moveupurl = new moodle_url($url, array('moveupitem'=>$feedbackitem->id));
                $buttonlink = $moveupurl->out();
                $strbutton = get_string('moveup_item', 'feedback');
                echo '<a class="icon up" title="'.$strbutton.'" href="'.$buttonlink.'">
                        <img alt="'.$strbutton.'" src="'.$OUTPUT->pix_url('t/up') . '" />
                      </a>';
                echo '</span>';
            }
285
            //Print the movedown-button
286
287
288
289
290
291
292
293
294
295
296
            if ($feedbackitem->position < $lastposition - 1) {
                echo '<span class="feedback_item_command_movedown">';
                $urlparams = array('movedownitem'=>$feedbackitem->id);
                $movedownurl = new moodle_url($url, $urlparams);
                $buttonlink = $movedownurl->out();
                $strbutton = get_string('movedown_item', 'feedback');
                echo '<a class="icon down" title="'.$strbutton.'" href="'.$buttonlink.'">
                        <img alt="'.$strbutton.'" src="'.$OUTPUT->pix_url('t/down') . '" />
                      </a>';
                echo '</span>';
            }
297
            //Print the move-button
298
299
300
301
302
303
304
305
306
307
            if (count($feedbackitems) > 1) {
                echo '<span class="feedback_item_command_move">';
                $moveurl = new moodle_url($url, array('moveitem'=>$feedbackitem->id));
                $buttonlink = $moveurl->out();
                $strbutton = get_string('move_item', 'feedback');
                echo '<a class="editing_move" title="'.$strbutton.'" href="'.$buttonlink.'">
                        <img alt="'.$strbutton.'" src="'.$OUTPUT->pix_url('t/move') . '" />
                      </a>';
                echo '</span>';
            }
308
            //Print the button to edit the item
309
310
311
312
313
314
315
316
            if ($feedbackitem->typ != 'pagebreak') {
                echo '<span class="feedback_item_command_edit">';
                $editurl = new moodle_url('/mod/feedback/edit_item.php');
                $editurl->params(array('do_show'=>$do_show,
                                         'cmid'=>$id,
                                         'id'=>$feedbackitem->id,
                                         'typ'=>$feedbackitem->typ));

317
318
                // In edit_item.php the param id is used for the itemid
                // and the cmid is the id to get the module.
319
320
321
322
323
324
325
326
                $buttonlink = $editurl->out();
                $strbutton = get_string('edit_item', 'feedback');
                echo '<a class="editing_update" title="'.$strbutton.'" href="'.$buttonlink.'">
                        <img alt="'.$strbutton.'" src="'.$OUTPUT->pix_url('t/edit') . '" />
                      </a>';
                echo '</span>';
            }

327
            //Print the toggle-button to switch required yes/no
328
329
330
331
            if ($feedbackitem->hasvalue == 1) {
                echo '<span class="feedback_item_command_toggle">';
                if ($feedbackitem->required == 1) {
                    $buttontitle = get_string('switch_item_to_not_required', 'feedback');
332
                    $buttonimg = $OUTPUT->pix_url('required', 'feedback');
333
334
                } else {
                    $buttontitle = get_string('switch_item_to_required', 'feedback');
335
                    $buttonimg = $OUTPUT->pix_url('notrequired', 'feedback');
336
                }
337
338
339
340
341
342
343
344
345
346
347
348
                $urlparams = array('switchitemrequired'=>$feedbackitem->id);
                $requiredurl = new moodle_url($url, $urlparams);
                $buttonlink = $requiredurl->out();
                echo '<a class="icon '.
                        'feedback_switchrequired" '.
                        'title="'.$buttontitle.'" '.
                        'href="'.$buttonlink.'">'.
                        '<img alt="'.$buttontitle.'" src="'.$buttonimg.'" />'.
                        '</a>';
                echo '</span>';
            }

349
            //Print the delete-button
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
            echo '<span class="feedback_item_command_toggle">';
            $deleteitemurl = new moodle_url('/mod/feedback/delete_item.php');
            $deleteitemurl->params(array('id'=>$id,
                                         'do_show'=>$do_show,
                                         'deleteitem'=>$feedbackitem->id));

            $buttonlink = $deleteitemurl->out();
            $strbutton = get_string('delete_item', 'feedback');
            $src = $OUTPUT->pix_url('t/delete');
            echo '<a class="icon delete" title="'.$strbutton.'" href="'.$buttonlink.'">
                    <img alt="'.$strbutton.'" src="'.$src.'" />
                  </a>';
            echo '</span>';
            echo $OUTPUT->box_end();
            if ($feedbackitem->typ != 'pagebreak') {
                feedback_print_item_preview($feedbackitem);
            } else {
                echo $OUTPUT->box_start('feedback_pagebreak');
                echo get_string('pagebreak', 'feedback').'<hr class="feedback_pagebreak" />';
                echo $OUTPUT->box_end();
370
            }
371
372
            echo $OUTPUT->box_end();
            echo $OUTPUT->box_end();
373
374
            echo '<div class="clearer">&nbsp;</div>';
            echo '</li>';
375
            //Print out the target box if we ar moving an item
376
            if (isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->shouldmoving == 1) {
377
                echo '<li>';
378
379
                $moveposition++;
                $movehereurl->param('movehere', $moveposition);
380
                echo $OUTPUT->box_start('clipboard'); //Only shown if shouldmoving = 1
381
382
383
384
385
386
387
                $buttonlink = $movehereurl->out();
                $strbutton = get_string('move_here', 'feedback');
                $src = $OUTPUT->pix_url('movehere');
                echo '<a title="'.$strbutton.'" href="'.$buttonlink.'">
                        <img class="movetarget" alt="'.$strbutton.'" src="'.$src.'" />
                      </a>';
                echo $OUTPUT->box_end();
388
                echo '</li>';
389
390
            }
        }
391
        echo $OUTPUT->box_end();
392
393
        echo '</ul>';
        echo '</div>';
394
395
396
    } else {
        echo $OUTPUT->box(get_string('no_items_available_yet', 'feedback'),
                         'generalbox boxaligncenter');
397
    }
398
399
400
401
402
}
/// Finish the page
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
403

404
echo $OUTPUT->footer();