-
Notifications
You must be signed in to change notification settings - Fork 13
/
sitenotice.php
123 lines (93 loc) · 3.62 KB
/
sitenotice.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
<?php
require_once '../../config.php';
require_once 'sitenotice_form.php';
global $DB;
$id = required_param('id', PARAM_INT); // ID in facetoface_notice
$d = optional_param('d', false, PARAM_BOOL); // set to true to delete the given notice
$confirm = optional_param('confirm', false, PARAM_BOOL); // delete confirmation
$notice = null;
if ($id > 0) {
$notice = $DB->get_record('facetoface_notice', array('id' => $id));
}
$PAGE->set_url('/mod/facetoface/sitenotice.php', array('id' => $id, 'd' => $d, 'confirm' => $confirm));
admin_externalpage_setup('managemodules'); // this is hacky, tehre should be a special hidden page for it
$contextsystem = context_system::instance();
require_capability('moodle/site:config', $contextsystem);
$returnurl = "$CFG->wwwroot/admin/settings.php?section=modsettingfacetoface";
$title = get_string('addnewnotice', 'facetoface');
if ($notice != null) {
$title = $notice->name;
}
$PAGE->set_title($title);
// Handle deletions
if (!empty($d)) {
if (!confirm_sesskey()) {
print_error('confirmsesskeybad', 'error');
}
if (!$confirm) {
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
$info = new stdClass();
$info->name = format_string($notice->name);
$info->text = format_text($notice->text, FORMAT_HTML);
$optionsyes = array('id' => $id, 'sesskey' => $USER->sesskey, 'd' => 1, 'confirm' => 1);
echo $OUTPUT->confirm(get_string('noticedeleteconfirm', 'facetoface', $info),
new moodle_url("sitenotice.php", $optionsyes),
new moodle_url($returnurl));
echo $OUTPUT->footer();
exit;
}
else {
$transaction = $DB->start_delegated_transaction();
$DB->delete_records('facetoface_notice', array('id' => $id));
$DB->delete_records('facetoface_notice_data', array('noticeid' => $id));
$transaction->allow_commit();
redirect($returnurl);
}
}
$customfields = facetoface_get_session_customfields();
$mform = new mod_facetoface_sitenotice_form(null, compact('id', 'customfields'));
if ($mform->is_cancelled()) {
redirect($returnurl);
}
if ($fromform = $mform->get_data()) { // Form submitted
if (empty($fromform->submitbutton)) {
print_error('error:unknownbuttonclicked', 'facetoface', $returnurl);
}
$todb = new stdClass();
$todb->name = trim($fromform->name);
$todb->text = trim($fromform->text['text']);
$transaction = $DB->start_delegated_transaction();
if ($notice != null) {
$todb->id = $notice->id;
$DB->update_record('facetoface_notice', $todb);
} else {
$notice = new stdClass();
$notice->id = $DB->insert_record('facetoface_notice', $todb);
}
foreach ($customfields as $field) {
$fieldname = "custom_$field->shortname";
if (empty($fromform->$fieldname)) {
$fromform->$fieldname = ''; // need to be able to clear fields
}
facetoface_save_customfield_value($field->id, $fromform->$fieldname, $notice->id, 'notice');
}
$transaction->allow_commit();
redirect($returnurl);
} else if ($notice != null) { // Edit mode
// Set values for the form
$toform = new stdClass();
$toform->name = $notice->name;
$toform->text['text'] = $notice->text;
foreach ($customfields as $field) {
$fieldname = "custom_$field->shortname";
$toform->$fieldname = facetoface_get_customfield_value($field, $notice->id, 'notice');
}
$mform->set_data($toform);
}
echo $OUTPUT->header();
echo $OUTPUT->box_start();
echo $OUTPUT->heading($title);
$mform->display();
echo $OUTPUT->box_end();
echo $OUTPUT->footer();