-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtopicitem.php
157 lines (130 loc) · 5.61 KB
/
topicitem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/*
* Copyright (C) 2015 onwards Catalyst IT
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Eugene Venter <[email protected]>
* @package mod_ojt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_ojt\form\topic_item_select_form;
use mod_ojt\form\topic_item_text_form;
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/lib.php');
require_once(dirname(__FILE__).'/locallib.php');
$ojtid = required_param('bid', PARAM_INT); // OJT instance id.
$topicid = required_param('tid', PARAM_INT); // Topic id.
$itemid = optional_param('id', 0, PARAM_INT); // Topic item id.
$delete = optional_param('delete', 0, PARAM_BOOL);
$itemtype = optional_param('type', OJT_ITEM_TYPE_TEXT, PARAM_INT); // Topic item type (text / select).
$action = optional_param('action', '', PARAM_ALPHANUMEXT); // Action
$ojt = $DB->get_record('ojt', array('id' => $ojtid), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $ojt->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('ojt', $ojt->id, $course->id, false, MUST_EXIST);
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/ojt:manage', $context);
$PAGE->set_url('/mod/ojt/topicitem.php', array('bid' => $ojtid, 'tid' => $topicid, 'id' => $itemid));
// Handle actions
if ($delete) {
$confirm = optional_param('confirm', 0, PARAM_BOOL);
if (!$confirm) {
echo $OUTPUT->header();
$confirmurl = $PAGE->url;
$confirmurl->params(array('delete' => 1, 'confirm' => 1, 'sesskey' => sesskey()));
echo $OUTPUT->confirm(get_string('confirmitemdelete', 'ojt'), $confirmurl, $PAGE->url);
echo $OUTPUT->footer();
die();
}
require_sesskey();
ojt_delete_topic_item($itemid, $context);
$redirecturl = new moodle_url('/mod/ojt/manage.php', array('cmid' => $cm->id));
redirect($redirecturl, get_string('itemdeleted', 'ojt'));
}
switch ($itemtype) {
case OJT_ITEM_TYPE_SELECT:
$form = new topic_item_select_form(null, array('ojtid' => $ojtid, 'topicid' => $topicid, 'type' => $itemtype));
break;
case OJT_ITEM_TYPE_TEXT:
default:
$form = new topic_item_text_form(null, array('ojtid' => $ojtid, 'topicid' => $topicid));
}
if ($data = $form->get_data()) {
// Save topic
$item = new stdClass();
$item->topicid = $data->tid;
$item->name = $data->name;
$item->completionreq = $data->completionreq;
$item->allowfileuploads = $data->allowfileuploads;
$item->allowselffileuploads = $data->allowselffileuploads;
$item->type = $data->type;
if(!empty($data->selectionoptions)) {
$item->other = $data->selectionoptions;
}
if (empty($data->id)) {
// Add
$DB->insert_record('ojt_topic_item', $item);
} else {
// Update
$item->id = $data->id;
$DB->update_record('ojt_topic_item', $item);
}
redirect(new moodle_url('/mod/ojt/manage.php', array('cmid' => $cm->id)));
}
switch ($action) {
case 'topicitemdown':
$topicitem = $DB->get_record('ojt_topic_item', array('id' => $itemid), '*', MUST_EXIST);
$itemrs = $DB->get_records('ojt_topic_item', array('topicid' => $topicid), 'position');
$itemrs = array_values($itemrs);
if (ojt_array_move($itemrs, $topicitem->position, $topicitem->position + 1) !== false) {
foreach ($itemrs as $index => $itemr) {
$itemr->position = $index;
$DB->update_record('ojt_topic_item', $itemr);
}
}
redirect(new moodle_url('/mod/ojt/manage.php', array('cmid' => $cm->id)), get_string('topicitemreordered', 'ojt'));
break;
case 'topicitemup':
$topicitem = $DB->get_record('ojt_topic_item', array('id' => $itemid), '*', MUST_EXIST);
$itemrs = $DB->get_records('ojt_topic_item', array('topicid' => $topicid), 'position');
$itemrs = array_values($itemrs);
if (ojt_array_move($itemrs, $topicitem->position, $topicitem->position - 1) !== false) {
foreach ($itemrs as $index => $itemr) {
$itemr->position = $index;
$DB->update_record('ojt_topic_item', $itemr);
}
}
redirect(new moodle_url('/mod/ojt/manage.php', array('cmid' => $cm->id)), get_string('topicitemreordered', 'ojt'));
break;
default:
break;
}
// Print the page header.
$actionstr = empty($itemid) ? get_string('additem', 'ojt') : get_string('edititem', 'ojt');
$PAGE->set_title(format_string($ojt->name));
$PAGE->set_heading(format_string($ojt->name).' - '.$actionstr);
// Output starts here.
echo $OUTPUT->header();
// Replace the following lines with you own code.
echo $OUTPUT->heading($PAGE->heading);
if (!empty($itemid)) {
$item = $DB->get_record('ojt_topic_item', array('id' => $itemid), '*', MUST_EXIST);
$item->selectionoptions = !empty($item->other) ? $item->other : '';
$form->set_data($item);
}
// Display
$form->display();
// Finish the page.
echo $OUTPUT->footer();