forked from silecs/moodle-local-resourcenotif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresourcenotif_form.php
130 lines (113 loc) · 5.55 KB
/
resourcenotif_form.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
<?php
/**
* @package local_resourcenotif
* @copyright 2012-2018 Silecs {@link http://www.silecs.info/societe}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.');
}
require_once($CFG->libdir.'/formslib.php');
class local_resourcenotif_resourcenotif_form extends moodleform {
public function definition() {
global $CFG;
$mform = $this->_form;
$customdata = $this->_customdata;
// hidden elements
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'mod');
$mform->setType('mod', PARAM_ALPHA);
$mform->addElement('hidden', 'courseid');
$mform->setType('courseid', PARAM_INT);
//recipients
$sendok = true;
$mform->addElement('header', 'recipient', get_string('recipients', 'local_resourcenotif'));
$optionsSendAll = [];
if ($customdata['nbNotifiedStudents'] == 0) {
$optionsSendAll = ['disabled' => 'disabled'];
$sendok = false;
}
$mform->addElement('radio', 'send', '', '<span class="fake-fitemtitle">' . $customdata['recipicents'] . '</span>', 'all', $optionsSendAll);
if($sendok) {
$mform->setDefault('send', 'all');
}
$allgroups = resourcenotif_get_all_groups($customdata['courseid']);
$allgroupings = resourcenotif_get_all_groupings($customdata['courseid']);
$selected = [];
if (count($allgroups)) {
$selectgroup = $mform->CreateElement('select','groups', 'Groupes', $allgroups);
$selectgroup->setMultiple(true);
$selected[] = $selectgroup;
}
if (count($allgroupings)) {
$selectgrouping = $mform->CreateElement('select','groupings', 'Groupements', $allgroupings);
$selectgrouping->setMultiple(true);
$selected[] = $selectgrouping;
}
if (count($selected)) {
$mform->addElement(
'radio',
'send',
'',
'<span class="fake-fitemtitle">' . get_string('selectedmembers', 'local_resourcenotif') . '</span>',
'selection'
);
$mform->addGroup($selected, 'myselected', "", array(' '), false);
$mform->disabledIf('groups[]', 'send', 'neq', 'selection');
$mform->disabledIf('groupings[]', 'send', 'neq', 'selection');
if ($sendok == false) {
$mform->setDefault('send', 'selection');
$sendok = true;
}
} else {
$mform->addElement('radio', 'send', '', '<span class="fake-fitemtitle">'
. get_string('groupsgroupingsnone', 'local_resourcenotif') . '</span>', 'selection', ['disabled' => 'disabled']);
}
$liststudents = resourcenotif_get_list_students($customdata['courseid']);
if (count($liststudents)) {
$mform->addElement('radio', 'send', '', '<span class="fake-fitemtitle">' .
get_string('selectstudents', 'local_resourcenotif') . '</span>', 'selectionstudents', $optionsSendAll);
$mform->addElement('select', 'students', '', $liststudents)->setMultiple(true);
$mform->disabledIf('students', 'send', 'neq', 'selectionstudents');
}
//message
$mform->addElement('header', 'message', get_string('content', 'local_resourcenotif'));
$subjectlabel = html_writer::tag('span', get_string('subject', 'local_resourcenotif'), array('class' => 'notificationgras'));
$msgbody = resourcenotif_get_email_body($customdata['msgbodyinfo'], 'html');
$msghtml = html_writer::tag('p', $subjectlabel . $customdata['mailsubject'], array('class' => 'notificationlabel'))
. html_writer::tag('p', get_string('body', 'local_resourcenotif'), array('class' => 'notificationlabel notificationgras'))
. html_writer::tag('p', $msgbody, array('class' => 'notificationlabel'));
$mform->addElement('html', $msghtml);
$mform->setExpanded('message');
$mform->addElement('header', 'complementheader', get_string('complement', 'local_resourcenotif') );
$mform->setExpanded('complementheader');
$mform->addElement('textarea', 'complement', null, ['rows' => 5, 'class' => 'complement', 'style' => 'resize:both;']);
$mform->setType('complement',PARAM_RAW);
//-------------------------------------------------------------------------------
// buttons
if ($sendok) {
$this->add_action_buttons(true, get_string('submit', 'local_resourcenotif'));
}
}
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if (empty($errors)) {
$this->validation_recipicent($data, $errors);
}
return $errors;
}
private function validation_recipicent($data, &$errors) {
if (isset($data['send'])) {
if ($data['send'] == 'selection' && !isset($data['groups']) && !isset($data['groupings'])) {
$errors['myselected'] = get_string('errorselectgroup', 'local_resourcenotif');
}
if ($data['send'] == 'selectionstudents' && !isset($data['students'])) {
$errors['students'] = get_string('errorselectstudent', 'local_resourcenotif');
}
} else {
$errors['send'] = get_string('errorselectrecipicent', 'local_resourcenotif');
}
return $errors;
}
}