forked from emeneo/moodle-enrol_apply
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
652 lines (562 loc) · 25.3 KB
/
lib.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
<?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/>.
/**
* @package enrol_apply
* @copyright emeneo.com (http://emeneo.com/)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author emeneo.com (http://emeneo.com/)
* @author Johannes Burk <[email protected]>
*/
/** The user is put onto a waiting list and therefore the enrolment not active (used in user_enrolments->status) */
define('ENROL_APPLY_USER_WAIT', 2);
class enrol_apply_plugin extends enrol_plugin {
/**
* Add new instance of enrol plugin with default settings.
* @param object $course
* @return int id of new instance
*/
public function add_default_instance($course) {
$fields = $this->get_instance_defaults();
return $this->add_instance($course, $fields);
}
public function allow_unenrol(stdClass $instance) {
// Users with unenrol cap may unenrol other users manually.
return true;
}
public function roles_protected() {
// Users may tweak the roles later.
return false;
}
public function allow_apply(stdClass $instance) {
if ($instance->status != ENROL_INSTANCE_ENABLED) {
return get_string('cantenrol', 'enrol_apply');
}
if (!$instance->customint6) {
// New enrols not allowed.
return get_string('cantenrol', 'enrol_apply');
}
return true;
}
/**
* Prevent to unenrol an user with a pending application
*
* @param stdClass $instance course enrol instance
* @param stdClass $ue record from user_enrolments table, specifies user
* @return bool
*/
public function allow_unenrol_user(stdClass $instance, stdClass $ue) {
global $DB;
if ($DB->record_exists('enrol_apply_applicationinfo', ['userenrolmentid' => $ue->id])) {
return false;
}
return parent::allow_unenrol_user($instance, $ue);
}
public function allow_manage(stdClass $instance) {
// Users with manage cap may tweak period and status.
return true;
}
/**
* Returns link to page which may be used to add new instance of enrolment plugin in course.
* Multiple instances supported.
* @param int $courseid
* @return moodle_url page url
*/
public function get_newinstance_link($courseid) {
$context = context_course::instance($courseid, MUST_EXIST);
if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/apply:config', $context)) {
return null;
}
return new moodle_url('/enrol/apply/edit.php', array('courseid' => $courseid));
}
// TODO mover para outro local, usado também em certificado
function obtemCampoCustomizadoCurso($idCurso, $nomeCampo) {
global $DB;
$sql = "
SELECT d.value, f.configdata::json->>'options' as options
FROM mdl_course c
JOIN mdl_context ctx
ON c.id = ?
AND ctx.contextlevel = 50
AND ctx.instanceid = c.id
JOIN mdl_customfield_field f
ON f.shortname = ?
JOIN mdl_customfield_data d
ON d.fieldid = f.id
AND d.contextid = ctx.id
";
$valueArray = $DB->get_record_sql($sql, [$idCurso, $nomeCampo]);
if($valueArray) {
$value = $valueArray->value;
$options = $valueArray->options;
if($options == null) {
return $value;
} else {
$optionsArray = preg_split("/\s*\n\s*/", trim($options));
return $optionsArray[$value-1];
}
} else {
return '';
}
}
public function insere_assinatura_sigad(/*$codProtocolo, $codUsuario*/) {
// retorna true se inseriu ou se já tinha, ou falso em caso de erro ou timeout
return true;
}
// Chamado quando usuário solicita inscrição em um curso
// e também depois da confirmação da inscrição
public function enrol_page_hook(stdClass $instance) {
global $CFG, $OUTPUT, $SESSION, $USER, $DB;
if (isguestuser()) {
// Can not enrol guest!
return null;
}
// Assegura que método de inscrição está ativo
$allowapply = $this->allow_apply($instance);
if ($allowapply !== true) {
return '<div class="alert alert-error">' . $allowapply . '</div>';
}
// Testa se foi chamado após a inscrição
if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
// Já está inserido na tabela
// Devo pegar quantitativo do curso e posição real, dentre os já aprovados
// Se estiver entre as vagas, já marca como matriculado e envia e-mail
// Senão, informa que está na fila e envia e-mail
/*
PROC insere assinatura sigad
Você foi pre-matriculado no curso XXXX.
A partir de agora, você tem 48 horas para assinar o termo de responsabilidade
no SIGAD, de modo a assegurar sua vaga.
<link para assinatura>
Caso não faça a assinatura dentro desse período, perderá direito a vaga, que
poderá ser ocupada por outro interessado.
*/
// TODO: incluir aqui chamada para WS que atribui assinatura a documento no SIGAD
// analisar resultado: se timeout ou erro, assinatura deve ser inserida manualmente
// DO contrário já mostra link para usuário assinar.
$textoAssinatura = '';
if(true) {
$textoAssinatura = 'Inseriu';
} else {
$textoAssinatura = 'Erro';
}
$textoassinatura = '<a target=_moodleSigad href=\'https://intra.senado.leg.br/sigad/novo/protocolo/assinador.asp?area=documento&cod_protocolo=2648004&cod_cabecalho=1\'>Assinar</a>';
return $textoassinatura . ' '. $OUTPUT->notification(get_string('notification', 'enrol_apply'), 'notifysuccess');
}
// Se deseja se inscrever, verifica se já não atingiu limite de inscritos
// FIXME evidenciar que é pré-inscrição, ver como tratar esse limite!
if ($instance->customint3 > 0) {
// Max enrol limit specified.
$count = $DB->count_records('user_enrolments', array('enrolid' => $instance->id));
if ($count >= $instance->customint3) {
// Bad luck, no more self enrolments here.
return '<div class="alert alert-error">'.get_string('maxenrolledreached_left', 'enrol_apply')." (".$count.") ".get_string('maxenrolledreached_right', 'enrol_apply').'</div>';
}
}
// Exibe formulário para matrícula
require_once("$CFG->dirroot/enrol/apply/apply_form.php");
$form = new enrol_apply_apply_form(null, $instance);
// Se formulário foi submetido, efetiva a matrícula e
// redireciona para página do curso
// FIXME não redirecionar ainda
// FIXME definir datas de enrol
if ($data = $form->get_data()) {
// Only process when form submission is for this instance (multi instance support).
if ($data->instance == $instance->id) {
$timestart = 0;
$timeend = 0;
$roleid = $instance->roleid;
$this->enrol_user($instance, $USER->id, $roleid, $timestart, $timeend, ENROL_USER_SUSPENDED);
$userenrolment = $DB->get_record(
'user_enrolments',
array(
'userid' => $USER->id,
'enrolid' => $instance->id),
'id', MUST_EXIST);
$applicationinfo = new stdClass();
$applicationinfo->userenrolmentid = $userenrolment->id;
$applicationinfo->comment = $data->applydescription;
$DB->insert_record('enrol_apply_applicationinfo', $applicationinfo, false);
// FIXME verificar se essa notificação é apropriada
$this->send_application_notification($instance, $USER->id, $data);
// FIXME verificar redirecionamento
redirect("$CFG->wwwroot/course/view.php?id=$instance->courseid");
}
}
// Exibe informações sobre público alvo do curso
// e botões para matrícula
// Em caso de mais de uma instância, todas aparecem aqui
// FIXME definir formato
$output = $form->render();
return $OUTPUT->box($OUTPUT->box($output));
}
// Monta os botões de ação que serão exibidos para este método de inscrição
// na página de métodos de inscrição do curso (/enrol/instances.php?id=IDCURSO)
public function get_action_icons(stdClass $instance) {
global $OUTPUT;
if ($instance->enrol !== 'apply') {
throw new coding_exception('invalid enrol instance!');
}
$context = context_course::instance($instance->courseid);
$icons = array();
if (has_capability('enrol/apply:config', $context)) {
$editlink = new moodle_url("/enrol/apply/edit.php", array('courseid' => $instance->courseid, 'id' => $instance->id));
$icons[] = $OUTPUT->action_icon($editlink, new pix_icon(
't/edit',
get_string('edit'),
'core',
array('class' => 'iconsmall')));
}
if (has_capability('enrol/apply:manageapplications', $context)) {
$managelink = new moodle_url("/enrol/apply/manage.php", array('id' => $instance->id));
$icons[] = $OUTPUT->action_icon($managelink, new pix_icon(
'i/users',
get_string('confirmenrol', 'enrol_apply'),
'core',
array('class' => 'iconsmall')));
$infolink = new moodle_url("/enrol/apply/info.php", array('id' => $instance->id));
$icons[] = $OUTPUT->action_icon($infolink, new pix_icon(
'i/files',
get_string('submitted_info', 'enrol_apply'),
'core',
array('class' => 'iconsmall')));
}
return $icons;
}
/**
* Is it possible to hide/show enrol instance via standard UI?
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/apply:config', $context);
}
/**
* Is it possible to delete enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/apply:config', $context);
}
/**
* Sets up navigation entries.
*
* @param stdClass $instancesnode
* @param stdClass $instance
* @return void
*/
public function add_course_navigation($instancesnode, stdClass $instance) {
if ($instance->enrol !== 'apply') {
throw new coding_exception('Invalid enrol instance type!');
}
$context = context_course::instance($instance->courseid);
if (has_capability('enrol/apply:config', $context)) {
$managelink = new moodle_url('/enrol/apply/edit.php', array('courseid' => $instance->courseid, 'id' => $instance->id));
$instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
}
}
public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
$actions = array();
//return $actions;
$context = $manager->get_context();
$instance = $ue->enrolmentinstance;
$params = $manager->get_moodlepage()->url->params();
$params['ue'] = $ue->id;
if ( ($this->allow_unenrol_user($instance, $ue)) && has_capability("enrol/apply:unenrol", $context)) {
$url = new moodle_url('/enrol/unenroluser.php', $params);
$actions[] = new user_enrolment_action(
new pix_icon('t/delete', ''),
get_string('unenrol', 'enrol'),
$url,
array('class' => 'unenrollink', 'rel' => $ue->id));
}
if ($this->allow_manage($instance) && has_capability("enrol/apply:manage", $context)) {
$url = new moodle_url('/enrol/editenrolment.php', $params);
$actions[] = new user_enrolment_action(new pix_icon('t/edit', ''), get_string('edit'), $url, array('class'=>'editenrollink', 'rel'=>$ue->id));
}
return $actions;
}
/**
* Returns defaults for new instances.
* @return array
*/
public function get_instance_defaults() {
$fields = array();
$fields['status'] = $this->get_config('status');
$fields['roleid'] = $this->get_config('roleid', 0);
$fields['customint1'] = $this->get_config('show_standard_user_profile');
$fields['customint2'] = $this->get_config('show_extra_user_profile');
$fields['customtext2'] = $this->get_config('notifycoursebased') ? '$@ALL@$' : '';
$fields['enrolperiod'] = $this->get_config('enrolperiod', 0);
return $fields;
}
public function confirm_enrolment($enrols) {
global $DB;
foreach ($enrols as $enrol) {
$userenrolment = $DB->get_record_select(
'user_enrolments',
'id = :id AND (status = :enrolusersuspended OR status = :enrolapplyuserwait)',
array(
'id' => $enrol,
'enrolusersuspended' => ENROL_USER_SUSPENDED,
'enrolapplyuserwait' => ENROL_APPLY_USER_WAIT),
'*',
MUST_EXIST);
$instance = $DB->get_record('enrol', array('id' => $userenrolment->enrolid, 'enrol' => 'apply'), '*', MUST_EXIST);
// Check privileges.
$context = context_course::instance($instance->courseid, MUST_EXIST);
if (!has_capability('enrol/apply:manageapplications', $context)) {
continue;
}
// Set timestart and timeend if an enrolment duration is set.
$userenrolment->timestart = time();
$userenrolment->timeend = 0;
if ($instance->enrolperiod) {
$userenrolment->timeend = $userenrolment->timestart + $instance->enrolperiod;
}
$this->update_user_enrol($instance, $userenrolment->userid, ENROL_USER_ACTIVE, $userenrolment->timestart, $userenrolment->timeend);
$DB->delete_records('enrol_apply_applicationinfo', array('userenrolmentid' => $enrol));
$this->notify_applicant(
$instance,
$userenrolment,
'confirmation',
get_config('enrol_apply', 'confirmmailsubject'),
get_config('enrol_apply', 'confirmmailcontent'));
}
}
public function wait_enrolment($enrols) {
global $DB;
foreach ($enrols as $enrol) {
$userenrolment = $DB->get_record(
'user_enrolments',
array('id' => $enrol, 'status' => ENROL_USER_SUSPENDED),
'*', IGNORE_MISSING);
if ($userenrolment != null) {
$instance = $DB->get_record('enrol', array('id' => $userenrolment->enrolid, 'enrol' => 'apply'), '*', MUST_EXIST);
// Check privileges.
$context = context_course::instance($instance->courseid, MUST_EXIST);
if (!has_capability('enrol/apply:manageapplications', $context)) {
continue;
}
$this->update_user_enrol($instance, $userenrolment->userid, ENROL_APPLY_USER_WAIT);
$this->notify_applicant(
$instance,
$userenrolment,
'waitinglist',
get_config('enrol_apply', 'waitmailsubject'),
get_config('enrol_apply', 'waitmailcontent'));
}
}
}
public function cancel_enrolment($enrols) {
global $DB;
foreach ($enrols as $enrol) {
$userenrolment = $DB->get_record_select(
'user_enrolments',
'id = :id AND (status = :enrolusersuspended OR status = :enrolapplyuserwait)',
array(
'id' => $enrol,
'enrolusersuspended' => ENROL_USER_SUSPENDED,
'enrolapplyuserwait' => ENROL_APPLY_USER_WAIT),
'*',
MUST_EXIST);
$instance = $DB->get_record('enrol', array('id' => $userenrolment->enrolid, 'enrol' => 'apply'), '*', MUST_EXIST);
// Check privileges.
$context = context_course::instance($instance->courseid, MUST_EXIST);
if (!has_capability('enrol/apply:manageapplications', $context)) {
continue;
}
$this->unenrol_user($instance, $userenrolment->userid);
$DB->delete_records('enrol_apply_applicationinfo', array('userenrolmentid' => $enrol));
$this->notify_applicant(
$instance,
$userenrolment,
'cancelation',
get_config('enrol_apply', 'cancelmailsubject'),
get_config('enrol_apply', 'cancelmailcontent'));
}
}
private function notify_applicant($instance, $userenrolment, $type, $subject, $content) {
global $CFG;
require_once($CFG->dirroot.'/enrol/apply/notification.php');
// Required for course_get_url() function.
require_once($CFG->dirroot.'/course/lib.php');
$course = get_course($instance->courseid);
$user = core_user::get_user($userenrolment->userid);
$content = $this->update_mail_content($content, $course, $user, $userenrolment);
$message = new enrol_apply_notification(
$user,
core_user::get_support_user(),
$type,
$subject,
$content,
course_get_url($course),
$instance->courseid);
message_send($message);
}
private function send_application_notification($instance, $userid, $data) {
global $CFG, $PAGE;
require_once($CFG->dirroot.'/enrol/apply/notification.php');
// Required for course_get_url() function.
require_once($CFG->dirroot.'/course/lib.php');
$renderer = $PAGE->get_renderer('enrol_apply');
$course = get_course($instance->courseid);
$applicant = core_user::get_user($userid);
// Include standard user profile fields?
$standarduserfields = null;
if ($instance->customint1) {
$standarduserfields = clone $data;
unset($standarduserfields->applydescription);
}
// Include extra user profile fields?
$extrauserfields = null;
if ($instance->customint2) {
require_once($CFG->dirroot.'/user/profile/lib.php');
profile_load_custom_fields($applicant);
$extrauserfields = $applicant->profile;
}
// Send notification to users with manageapplications in course context (instance depending)?
$courseuserstonotify = $this->get_notifycoursebased_users($instance);
if (!empty($courseuserstonotify)) {
$manageurl = new moodle_url("/enrol/apply/manage.php", array('id' => $instance->id));
$content = $renderer->application_notification_mail_body(
$course,
$applicant,
$manageurl,
$data->applydescription,
$standarduserfields,
$extrauserfields);
foreach ($courseuserstonotify as $user) {
$message = new enrol_apply_notification(
$user,
$applicant,
'application',
get_string('mailtoteacher_suject', 'enrol_apply'),
$content,
$manageurl,
$instance->courseid);
message_send($message);
}
}
// Send notification to users with manageapplications in system context?
$globaluserstonotify = $this->get_notifyglobal_users();
$globaluserstonotify = array_udiff($globaluserstonotify, $courseuserstonotify, function($usera, $userb) {
return $usera->id == $userb->id ? 0 : -1;
});
if (!empty($globaluserstonotify)) {
$manageurl = new moodle_url('/enrol/apply/manage.php');
$content = $renderer->application_notification_mail_body(
$course,
$applicant,
$manageurl,
$data->applydescription,
$standarduserfields,
$extrauserfields);
foreach ($globaluserstonotify as $user) {
$message = new enrol_apply_notification(
$user,
$applicant,
'application',
get_string('mailtoteacher_suject', 'enrol_apply'),
$content,
$manageurl,
$instance->courseid);
message_send($message);
}
}
}
/**
* Returns enrolled users of a course who should be notified about new course enrolment applications.
*
* Note: mostly copied from get_users_from_config() function in moodlelib.php.
* @param array $instance Enrol apply instance record.
* @return array Array of user IDs.
*/
public function get_notifycoursebased_users($instance) {
$value = $instance->customtext3;
if (empty($value) or $value === '$@NONE@$') {
return array();
}
$context = context_course::instance($instance->courseid);
// We have to make sure that users still have the necessary capability,
// it should be faster to fetch them all first and then test if they are present
// instead of validating them one-by-one.
$users = get_enrolled_users($context, 'enrol/apply:manageapplications');
if ($value === '$@ALL@$') {
return $users;
}
$result = array(); // Result in correct order.
$allowed = explode(',', $value);
foreach ($allowed as $uid) {
if (isset($users[$uid])) {
$user = $users[$uid];
$result[$user->id] = $user;
}
}
return $result;
}
/**
* Returns users who should be notified about new course enrolment applications.
* @return array Array of user IDs.
*/
public function get_notifyglobal_users() {
return get_users_from_config($this->get_config('notifyglobal'), 'enrol/apply:manageapplications', false);
}
private function update_mail_content($content, $course, $user, $userenrolment) {
$replace = array(
'firstname' => $user->firstname,
'content' => format_string($course->fullname),
'lastname' => $user->lastname,
'username' => $user->username,
'timeend' => !empty($userenrolment->timeend) ? userdate($userenrolment->timeend) : ''
);
foreach ($replace as $key => $val) {
$content = str_replace('{' . $key . '}', $val, $content);
}
return $content;
}
/**
* Backup execution step hook.
*
* @param backup_enrolments_execution_step $step
* @param stdClass $enrol
*/
public function backup_annotate_custom_fields(backup_enrolments_execution_step $step, stdClass $enrol) {
// annotate customint1 as a role
$step->annotate_id('role', $enrol->customint1);
}
public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
global $DB, $CFG;
$data->customint1 = $step->get_mappingid('role', $data->customint1, null);
$instanceid = $this->add_instance($course, (array)$data);
$step->set_mapping('enrol', $oldid, $instanceid);
//$this->sync_enrols($DB->get_record('enrol', array('id'=>$instanceid)));
}
public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) {
$this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, $oldinstancestatus);
}
/**
* Enrol cron support.
* @return void
*/
public function cron() {
$trace = new text_progress_trace();
$this->process_expirations($trace);
}
}