-
Notifications
You must be signed in to change notification settings - Fork 22
/
lib.php
1441 lines (1270 loc) · 54.6 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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Autoenrol enrolment plugin.
*
* This plugin automatically enrols a user onto a course the first time they try to access it.
*
* @package enrol_autoenrol
* @copyright 2013 Mark Ward & Matthew Cannings - based on code by Martin Dougiamas, Petr Skoda, Eugene Venter and others
* @copyright 2017 onwards Roberto Pinna
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use enrol_autoenrol\enrol_form;
/**
* Class enrol_autoenrol_plugin
*
* @package enrol_autoenrol
* @copyright 2013 Mark Ward & Matthew Cannings - based on code by Martin Dougiamas, Petr Skoda, Eugene Venter and others
* @copyright 2017 onwards Roberto Pinna
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_autoenrol_plugin extends enrol_plugin {
/**
* Database fields mapping
*
* name => Instance name
* status => Instance status (enabled/disabled)
* roleid => User role id
* enrolperiod => Enrolment duration
* expirenotify => Who need to be notified on expire
* expirethreshold => How many minutes before expire need to send notify
* enrolstartdate => When start to enrol
* enrolenddate => When stop to enrol
* customint1 => Enrol on course access, on login or with user confirmation
* customint2 => -- NOT USED -- Old group field filter
* customint3 => Longtime no see unenrol
* customint4 => New enrolment enabled
* customint5 => Enrolment Limit number
* customint6 => Enable self unenrol
* customint7 => Welcome message enabled
* customint8 => Always enrol
* customchar1 => Group Name
* customchar2 => -- NOT USED --
* customchar3 => Group by field name
* customtext1 => Welcome message
* customtext2 => Conditional rule
*/
/**
* Get enrol method icon
*
* @param array $instances
*
* @return array
*/
public function get_info_icons(array $instances) {
return [new pix_icon('icon', get_string('pluginname', 'enrol_autoenrol'), 'enrol_autoenrol')];
}
/**
* Users with role assign cap may tweak the roles later.
*
* @return bool
*/
public function roles_protected() {
return false;
}
/**
* Users with unenrol cap may unenrol other users manually - requires enrol/autoenrol:unenrol.
*
* @param stdClass $instance
*
* @return bool
*/
public function allow_unenrol(stdClass $instance) {
return true;
}
/**
* Users with manage cap may tweak period and status - requires enrol/autoenrol:manage.
*
* @param stdClass $instance
*
* @return bool
*/
public function allow_manage(stdClass $instance) {
return true;
}
/**
* We are a good plugin and don't invent our own UI/validation code path.
*
* @return boolean
*/
public function use_standard_editing_ui() {
return true;
}
/**
* Must show enrolme link.
*
* @param stdClass $instance
*
* @return bool
*/
public function show_enrolme_link(stdClass $instance) {
global $USER;
if ($this->get_config('loginenrol') && $instance->customint1 == 1) {
// Don't offer enrol link if we are going to enrol them on login.
return false;
} else if ($this->enrol_allowed($instance, $USER)) {
return true;
}
return false;
}
/**
* Returns list of unenrol links for all enrol instances in course.
*
* @param int $instance
*
* @return moodle_url or NULL if self unenrolment is not supported
*/
public function get_unenrolself_link($instance) {
if (($this->get_config('loginenrol') && ($instance->customint1 == 1)) || ($instance->customint6 == 0)) {
// Don't offer unenrolself if we are going to re-enrol them on login or if not permitted.
return null;
}
return parent::get_unenrolself_link($instance);
}
/**
* Return information for enrolment instance containing list of parameters required
* for enrolment, name of enrolment plugin etc.
*
* @param stdClass $instance enrolment instance
* @return stdClass instance info.
*/
public function get_enrol_info(stdClass $instance) {
global $USER;
$instanceinfo = new stdClass();
$instanceinfo->id = $instance->id;
$instanceinfo->courseid = $instance->courseid;
$instanceinfo->type = $this->get_name();
$instanceinfo->name = $this->get_instance_name($instance);
$instanceinfo->status = $this->enrol_allowed($instance, $USER);
return $instanceinfo;
}
/**
* Custom function, checks to see if user fulfills
* our requirements before enrolling them.
*
* @param stdClass $instance
* @param object $user
*
* @return bool
*/
public function enrol_allowed(stdClass $instance, $user) {
global $DB;
if (isguestuser($user)) {
// Can not enrol guest!!
return false;
}
// Do not reenrol if already enrolled with this method.
if ($DB->record_exists('user_enrolments', ['userid' => $user->id, 'enrolid' => $instance->id])) {
return false;
}
if (!$instance->customint8) {
// Do not reenrol if already enrolled with another method.
$context = context_course::instance($instance->courseid);
if (is_enrolled($context, $user)) {
// No need to enrol someone who is already enrolled.
return false;
}
}
if ($instance->status != ENROL_INSTANCE_ENABLED) {
return false;
}
if (!$instance->customint4) {
// New enrols not allowed.
return false;
}
if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) {
return false;
}
if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) {
return false;
}
if ($instance->customint5 > 0) {
// We need to check that we haven't reached the limit count.
$totalenrolments = $DB->count_records('user_enrolments', ['enrolid' => $instance->id]);
if ($totalenrolments >= $instance->customint5) {
return false;
}
}
if (!$this->check_rule($instance, $user)) {
return false;
}
return true;
}
/**
* Checks if user field match the rule.
*
* @param stdClass $instance
* @param object $user
*
* @return bool
*/
private function check_rule($instance, $user) {
// Very quick check to see if the user is being filtered.
if (!empty($instance->customtext2)) {
if (!is_object($user)) {
return false;
}
$info = new \enrol_autoenrol\filter_info($instance);
$information = '';
return $info->is_available($information, false, $user->id);
}
return true;
}
/**
* Attempt to enrol a user in course and update groups enrolments,
* calling code has to make sure the plugin and instance are active.
*
* @param stdClass $instance course enrol instance
* @param stdClass $user user data
*
* @return bool true means enrolled, false means not enrolled
* @throws coding_exception
*/
public function user_autoenrol(stdClass $instance, stdClass $user) {
if ($instance->customint1 == 2) {
return false;
}
if (!defined('ENROL_DO_NOT_SEND_EMAIL')) {
define('ENROL_DO_NOT_SEND_EMAIL', 0);
}
if ($instance->enrol !== 'autoenrol') {
throw new coding_exception('Invalid enrol instance type!');
}
if ($this->enrol_allowed($instance, $user)) {
$timestart = time();
if ($instance->enrolperiod) {
$timeend = $timestart + $instance->enrolperiod;
} else {
$timeend = 0;
}
$this->enrol_user($instance, $user->id, $instance->roleid, $timestart, $timeend);
$this->process_group($instance, $user);
// Send welcome message.
if ($instance->customint7 != ENROL_DO_NOT_SEND_EMAIL) {
$this->email_welcome_message($instance, $user);
}
return true;
}
return false;
}
/**
* Gets an array of the user enrolment actions.
*
* @param course_enrolment_manager $manager
* @param stdClass $ue A user enrolment object
*
* @return array An array of user_enrolment_actions
*/
public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
$actions = [];
$context = $manager->get_context();
$instance = $ue->enrolmentinstance;
$params = $manager->get_moodlepage()->url->params();
$params['ue'] = $ue->id;
if ($this->allow_manage($instance) && has_capability("enrol/{$instance->enrol}:manage", $context)) {
$url = new moodle_url('/enrol/editenrolment.php', $params);
$actions[] = new user_enrolment_action(
new pix_icon('t/edit', ''), get_string('editenrolment', 'enrol'), $url,
array(
'class' => 'editenrollink',
'rel' => $ue->id,
'data-action' => ENROL_ACTION_EDIT
));
}
if ($this->allow_unenrol_user($instance, $ue) && has_capability('enrol/autoenrol: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,
['class' => 'unenrollink', 'rel' => $ue->id]);
}
return $actions;
}
/**
* Returns localised name of enrol instance
*
* @param object $instance (null is accepted too)
*
* @return string
*/
public function get_instance_name($instance) {
global $DB;
if (empty($instance->name)) {
if (!empty($instance->roleid) && $role = $DB->get_record('role', ['id' => $instance->roleid])) {
$role = ' (' . role_get_name($role, context_course::instance($instance->courseid, IGNORE_MISSING)) . ')';
} else {
$role = '';
}
$enrol = $this->get_name();
return get_string('pluginname', 'enrol_'.$enrol) . $role;
} else {
return format_string($instance->name);
}
}
/**
* This is important especially for external enrol plugins,
* this function is called for all enabled enrol plugins
* right after every user login.
*
* @param object $user user record
* @param boolean $onlogin standard on login or scheduled call
* @param int $course course id
*
* @return void
*/
public function sync_user_enrolments($user, $onlogin=true, $course = null) {
global $DB, $PAGE;
$instances = [];
if (!empty($course)) {
// Get records of all enabled the AutoEnrol instances in a specified course.
$instances = $DB->get_records('enrol', ['enrol' => 'autoenrol', 'status' => 0, 'courseid' => $course], null, '*');
} else {
// Get records of all enabled the AutoEnrol instances.
$instances = $DB->get_records('enrol', ['enrol' => 'autoenrol', 'status' => 0], null, '*');
}
// Now get a record of all of the users enrolments.
$userenrolments = $DB->get_records('user_enrolments', ['userid' => $user->id], null, '*');
// Run through all of the autoenrolment instances and check that the user has been enrolled.
foreach ($instances as $instance) {
$found = false;
foreach ($userenrolments as $userenrolment) {
if ($userenrolment->enrolid == $instance->id) {
$found = true;
}
}
if (!$found && (($this->get_config('loginenrol') && ($instance->customint1 == 1)) || !$onlogin)) {
// If user is not enrolled and this instance enrol on login or called with sync task, try to enrol.
$PAGE->set_context(context_course::instance($instance->courseid));
if ($this->user_autoenrol($instance, $user)) {
if ($onlogin) {
// Purge the associated caches for the current user only.
$presignupcache = \cache::make('core', 'presignup');
$presignupcache->purge_current_user();
}
}
} else if ($found) {
// If user is enrolled check if the rule still verified.
if (!$this->check_rule($instance, $user)) {
if (!$context = context_course::instance($instance->courseid, IGNORE_MISSING)) {
// Very weird.
continue;
}
// Deal with enrolments of users that no more match the rule.
$unenrolaction = $this->get_config('autounenrolaction');
if ($unenrolaction === false) {
$unenrolaction = ENROL_EXT_REMOVED_UNENROL;
}
if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) {
$this->unenrol_user($instance, $user->id);
} else if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND || $unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
// Suspend users.
foreach ($userenrolments as $userenrolment) {
if ($userenrolment->enrolid == $instance->id) {
if ($userenrolment->status != ENROL_USER_SUSPENDED) {
$this->update_user_enrol($instance, $user->id, ENROL_USER_SUSPENDED);
}
}
}
if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
if (!empty($roleassigns[$instance->courseid])) {
// We want this "other user" to keep their roles.
continue;
}
role_unassign_all([
'contextid' => $context->id,
'userid' => $user->id,
'component' => 'enrol_autoenrol',
'itemid' => $instance->id,
]);
}
}
} else {
// If rule is verified update user group enrolments.
$this->process_group($instance, $user);
}
}
}
}
/**
* Forces synchronisation of all autoenrol instances for all users.
*
* @param progress_trace $trace
* @param int $course course id
*
* @return void
*/
public function sync_enrolments(progress_trace $trace, $course) {
global $DB;
// We may need a lot of memory here.
core_php_time_limit::raise();
raise_memory_limit(MEMORY_HUGE);
// Get records of all active users.
$users = $DB->get_records('user', ['deleted' => '0'], null, '*');
$trace->output(get_string('checksync', 'enrol_autoenrol', count($users)));
foreach ($users as $user) {
if (!is_siteadmin($user) && (!isguestuser($user))) {
$this->sync_user_enrolments($user, false, $course);
}
}
}
/**
* Process long time not seen user and expiration
*
* @param progress_trace $trace
* @param int $courseid one course, empty mean all
* @return int 0 means ok, 1 means error, 2 means plugin disabled
*/
public function sync_expirations(progress_trace $trace, $courseid = null) {
global $DB;
if (!enrol_is_enabled('autoenrol')) {
$trace->finished();
return 2;
}
// Unfortunately this may take a long time, execution can be interrupted safely here.
core_php_time_limit::raise();
raise_memory_limit(MEMORY_HUGE);
$trace->output('Verifying autoenrolments expiration...');
$params = ['now' => time(), 'useractive' => ENROL_USER_ACTIVE, 'courselevel' => CONTEXT_COURSE];
$coursesql = "";
if ($courseid) {
$coursesql = "AND e.courseid = :courseid";
$params['courseid'] = $courseid;
}
// First deal with users that did not log in for a really long time - they do not have user_lastaccess records.
$sql = "SELECT e.*, ue.userid
FROM {user_enrolments} ue
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'autoenrol' AND e.customint3 > 0)
JOIN {user} u ON u.id = ue.userid
WHERE :now - u.lastaccess > e.customint3
$coursesql";
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $instance) {
$userid = $instance->userid;
unset($instance->userid);
$this->unenrol_user($instance, $userid);
$days = $instance->customint3 / DAYSECS;
$trace->output("unenrolling user $userid from course $instance->courseid " .
"as they did not log in for at least $days days", 1);
}
$rs->close();
// Now unenrol from course user did not visit for a long time.
$sql = "SELECT e.*, ue.userid
FROM {user_enrolments} ue
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'autoenrol' AND e.customint3 > 0)
JOIN {user_lastaccess} ul ON (ul.userid = ue.userid AND ul.courseid = e.courseid)
WHERE :now - ul.timeaccess > e.customint3
$coursesql";
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $instance) {
$userid = $instance->userid;
unset($instance->userid);
$this->unenrol_user($instance, $userid);
$days = $instance->customint3 / DAYSECS;
$trace->output("unenrolling user $userid from course $instance->courseid " .
"as they did not access the course for at least $days days", 1);
}
$rs->close();
$trace->output('...user autoenrolment updates finished.');
$trace->finished();
$this->process_expirations($trace, $courseid);
return 0;
}
/**
* Returns the user who is responsible for autoenrolments in given instance.
*
* Usually it is the first editing teacher - the person with "highest authority"
* as defined by sort_by_roleassignment_authority() having 'enrol/autoenrol:manage'
* capability.
*
* @param int $instanceid enrolment instance id
* @return stdClass user record
*/
protected function get_enroller($instanceid) {
global $DB;
if ($this->lasternollerinstanceid == $instanceid && $this->lasternoller) {
return $this->lasternoller;
}
$instance = $DB->get_record('enrol', ['id' => $instanceid, 'enrol' => $this->get_name()], '*', MUST_EXIST);
$context = context_course::instance($instance->courseid);
if ($users = get_enrolled_users($context, 'enrol/autoenrol:manage')) {
$users = sort_by_roleassignment_authority($users, $context);
$this->lasternoller = reset($users);
unset($users);
} else {
$this->lasternoller = parent::get_enroller($instanceid);
}
$this->lasternollerinstanceid = $instanceid;
return $this->lasternoller;
}
/**
* Return true if we can add a new instance to this course.
*
* @param int $courseid
* @return boolean
*/
public function can_add_instance($courseid) {
$context = context_course::instance($courseid, MUST_EXIST);
if (!has_capability('moodle/course:enrolconfig', $context) || !has_capability('enrol/autoenrol:config', $context)) {
return false;
}
return true;
}
/**
* The autoenrol plugin has several bulk operations that can be performed.
* @param course_enrolment_manager $manager
* @return array
*/
public function get_bulk_operations(course_enrolment_manager $manager) {
$context = $manager->get_context();
$bulkoperations = [];
if (has_capability('enrol/autoenrol:manage', $context)) {
$bulkoperations['editselectedusers'] = new enrol_autoenrol_editselectedusers_operation($manager, $this);
}
if (has_capability('enrol/autoenrol:unenrol', $context)) {
$bulkoperations['deleteselectedusers'] = new enrol_autoenrol_deleteselectedusers_operation($manager, $this);
}
return $bulkoperations;
}
/**
* Restore instance and map settings.
*
* @param restore_enrolments_structure_step $step
* @param stdClass $data
* @param stdClass $course
* @param int $oldid
*/
public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
global $DB;
if ($step->get_task()->get_target() == backup::TARGET_NEW_COURSE) {
$merge = false;
} else {
$merge = [
'courseid' => $data->courseid,
'enrol' => $this->get_name(),
'status' => $data->status,
'roleid' => $data->roleid,
];
}
if ($merge && $instances = $DB->get_records('enrol', $merge, 'id')) {
$instance = reset($instances);
$instanceid = $instance->id;
} else {
$instanceid = $this->add_instance($course, (array)$data);
}
$step->set_mapping('enrol', $oldid, $instanceid);
}
/**
* Restore user enrolment.
*
* @param restore_enrolments_structure_step $step
* @param stdClass $data
* @param stdClass $instance
* @param int $userid
* @param int $oldinstancestatus
*/
public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) {
$this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, $data->status);
}
/**
* Restore role assignment.
*
* @param stdClass $instance
* @param int $roleid
* @param int $userid
* @param int $contextid
*/
public function restore_role_assignment($instance, $roleid, $userid, $contextid) {
global $DB;
// Just restore every role.
if ($DB->record_exists('user_enrolments', ['enrolid' => $instance->id, 'userid' => $userid])) {
role_assign($roleid, $userid, $contextid, 'enrol_'.$instance->enrol, $instance->id);
}
}
/**
* Restore user group membership.
* @param stdClass $instance
* @param int $groupid
* @param int $userid
*/
public function restore_group_member($instance, $groupid, $userid) {
global $CFG;
require_once($CFG->dirroot . '/group/lib.php');
// This might be called when forcing restore as manual enrolments.
groups_add_member($groupid, $userid);
}
/**
* 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/autoenrol:config', $context);
}
/**
* 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/autoenrol:config', $context);
}
/**
* Intercepts the instance deletion call and gives some
* custom instructions before resuming the parent function
*
* @param stdClass $instance
*/
public function delete_instance($instance) {
global $DB, $CFG;
if ($this->get_config('removegroups')) {
require_once($CFG->dirroot . '/group/lib.php');
$groups = $DB->get_records_sql('SELECT * FROM {groups} WHERE "courseid" = :courseid AND ' .
$DB->sql_like('idnumber', ':idnumber'),
['idnumber' => 'autoenrol|' . $instance->id . '|%', 'courseid' => $instance->courseid]);
foreach ($groups as $group) {
groups_delete_group($group);
}
}
parent::delete_instance($instance);
}
/**
* Creates course enrol form, checks if form submitted
* and enrols user if necessary. It can also redirect.
*
* @param stdClass $instance
*
* @return string html text, usually a form in a text box
*/
public function enrol_page_hook(stdClass $instance) {
global $USER, $OUTPUT;
if (!$this->enrol_allowed($instance, $USER)) {
return false;
}
if ($this->user_autoenrol($instance, $USER)) {
return true;
}
$form = new enrol_form(null, $instance);
$instanceid = optional_param('instance', 0, PARAM_INT);
if ($instance->id == $instanceid) {
if ($data = $form->get_data()) {
$this->enrol_user($instance, $USER->id, $instance->roleid, time(), 0);
$this->process_group($instance, $USER);
// Send welcome message.
if ($instance->customint7 != ENROL_DO_NOT_SEND_EMAIL) {
$this->email_welcome_message($instance, $USER);
}
}
}
return $OUTPUT->box($form->render());
}
/**
* Add new instance of enrol plugin with default settings.
*
* @param object $course
*
* @return int id of new instance, null if can not be created
*/
public function add_default_instance($course) {
$fields = $this->get_instance_defaults();
return $this->add_instance($course, $fields);
}
/**
* Returns defaults for new instances.
*
* @return array
*/
public function get_instance_defaults() {
$expirynotify = $this->get_config('expirynotify');
if ($expirynotify == 2) {
$expirynotify = 1;
$notifyall = 1;
} else {
$notifyall = 0;
}
$fields = [];
$fields['roleid'] = $this->get_config('defaultrole');
$fields['enrolperiod'] = $this->get_config('enrolperiod');
$fields['expirynotify'] = $expirynotify;
$fields['notifyall'] = $notifyall;
$fields['expirythreshold'] = $this->get_config('expirythreshold');
$fields['customint1'] = 0;
$fields['customint3'] = $this->get_config('longtimenosee');
$fields['customint4'] = $this->get_config('newenrols');
$fields['customint5'] = $this->get_config('maxenrolled');
$fields['customint6'] = $this->get_config('selfunenrol');
$fields['customint7'] = $this->get_config('sendcoursewelcomemessage');
$fields['customint8'] = 0;
return $fields;
}
/**
* Check and add or remove user from/to related groups
*
* @param stdClass $instance
* @param object $user
*/
private function process_group(stdClass $instance, $user) {
global $CFG;
$profileattribute = '';
if (isset($instance->customchar3) && ($instance->customchar3 != '-')) {
$profileattribute = $instance->customchar3;
} else if (isset($instance->customint2)) {
$oldfields = [0 => '', 1 => 'auth', 2 => 'department', 3 => 'institution', 4 => 'lang', 5 => 'email'];
$profileattribute = $oldfields[$instance->customint2];
}
if (!empty($profileattribute)) {
require_once($CFG->dirroot . '/group/lib.php');
$name = '';
if (!empty($instance->customchar1)) {
$name = $instance->customchar1;
} else {
$standardfields = ['auth', 'lang', 'department', 'institution', 'address', 'city', 'email'];
if (in_array($profileattribute, $standardfields)) {
$name = $user->$profileattribute;
} else {
require_once($CFG->dirroot.'/user/profile/lib.php');
$userdata = profile_user_record($user->id);
if (!empty($userdata) && isset($userdata->$profileattribute)) {
$name = $userdata->$profileattribute;
} else {
$name = get_string('emptyfield', 'enrol_autoenrol', $profileattribute);
}
}
}
$groupid = 0;
if (!empty($name)) {
$groupid = $this->get_group($instance, $name);
}
// Check if this instance already added this user to a group and remove membership if needed.
$usergroups = groups_get_all_groups($instance->courseid, $user->id);
if (!empty($usergroups)) {
foreach ($usergroups as $usergroupid => $usergroup) {
// Check if each group with this user as member was created by Autoenrol.
if (strpos($usergroup->idnumber, 'autoenrol|'.$instance->id.'|') === false) {
unset($usergroups[$usergroupid]);
}
// ATTENTION!! - We can't remove user membership from groups not created by Autoenrol.
}
if (!empty($usergroups) && (count($usergroups) == 1)) {
$usergroup = reset($usergroups);
if ($usergroup->id != $groupid) {
groups_remove_member($usergroup->id, $user->id);
}
}
}
if (!empty($name)) {
return groups_add_member($groupid, $user->id);
} else {
return null;
}
}
}
/**
* Get group id named groupname if not exists create it
*
* @param stdClass $instance
* @param string $groupname
*
* @return int|mixed id of the group
*
* @throws coding_exception
* @throws moodle_exception
*/
private function get_group(stdClass $instance, $groupname) {
global $DB;
// Try to get group from idnumber.
$hash = md5($groupname);
$idnumber = 'autoenrol|' . $instance->id . '|' .$hash;
$group = $DB->get_record('groups', ['idnumber' => $idnumber, 'courseid' => $instance->courseid]);
if ($group == null) {
// If not exists idnumber Try to get group from name.
$group = $DB->get_record('groups', ['name' => $groupname, 'courseid' => $instance->courseid]);
}
if ($group == null) {
$newgroupdata = new stdclass();
$newgroupdata->courseid = $instance->courseid;
$newgroupdata->name = $groupname;
$newgroupdata->idnumber = $idnumber;
$newgroupdata->description = get_string('auto_desc', 'enrol_autoenrol');
$groupid = groups_create_group($newgroupdata);
} else {
$groupid = $group->id;
}
return $groupid;
}
/**
* Send welcome email to specified user.
*
* @param stdClass $instance
* @param stdClass $user user record
* @return void
*/
protected function email_welcome_message($instance, $user) {
global $DB;
$course = $DB->get_record('course', ['id' => $instance->courseid], '*', MUST_EXIST);
$context = context_course::instance($course->id);
$a = new stdClass();
$a->coursename = format_string($course->fullname, true, ['context' => $context]);
$a->profileurl = new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $course->id]);
$a->link = course_get_url($course)->out();
if (trim($instance->customtext1) !== '') {
$message = $instance->customtext1;
$key = ['{$a->coursename}', '{$a->profileurl}', '{$a->link}', '{$a->fullname}', '{$a->email}'];
$value = [$a->coursename, $a->profileurl, $a->link, fullname($user), $user->email];
$message = str_replace($key, $value, $message);
if (strpos($message, '<') === false) {
// Plain text only.
$messagetext = $message;
$messagehtml = text_to_html($messagetext, null, false, true);
} else {
// This is most probably the tag/newline soup known as FORMAT_MOODLE.
$messagehtml = format_text($message, FORMAT_MOODLE,
['context' => $context, 'para' => false, 'newlines' => true, 'filter' => false]);
$messagetext = html_to_text($messagehtml);
}
} else {
$messagetext = get_string('welcometocoursetext', 'enrol_autoenrol', $a);
$messagehtml = text_to_html($messagetext, null, false, true);
}
$subject = get_string('welcometocourse', 'enrol_autoenrol',
format_string($course->fullname, true, ['context' => $context]));
$sendoption = $instance->customint7;
$contact = $this->get_welcome_email_contact($sendoption, $context);
// Directly emailing welcome message rather than using messaging.
email_to_user($user, $contact, $subject, $messagetext, $messagehtml);
}
/**
* Get the "from" contact which the email will be sent from.
*
* @param int $sendoption send email from constant ENROL_SEND_EMAIL_FROM_*
* @param object $context context where the user will be fetched
* @return mixed|stdClass the contact user object.
*/
public function get_welcome_email_contact($sendoption, $context) {
global $CFG;
if (!defined('ENROL_SEND_EMAIL_FROM_COURSE_CONTACT')) {
define('ENROL_SEND_EMAIL_FROM_COURSE_CONTACT', 1);
}
if (!defined('ENROL_SEND_EMAIL_FROM_NOREPLY')) {
define('ENROL_SEND_EMAIL_FROM_NOREPLY', 3);
}
$contact = null;
// Send as the first user assigned as the course contact.
if ($sendoption == ENROL_SEND_EMAIL_FROM_COURSE_CONTACT) {
$rusers = [];
if (!empty($CFG->coursecontact)) {
$croles = explode(',', $CFG->coursecontact);
list($sort, $sortparams) = users_order_by_sql('u');
// We only use the first user.
$i = 0;
do {
$rusers = get_role_users($croles[$i], $context, true, '',
'r.sortorder ASC, ' . $sort, null, '', '', '', '', $sortparams);
$i++;
} while (empty($rusers) && !empty($croles[$i]));
}
if ($rusers) {
$contact = array_values($rusers)[0];
}