-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
1148 lines (1005 loc) · 42.4 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/>.
/**
* SPAY Sudani Payment enrolment plugin.
*
* @package enrol_spay
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* SPAY Sudani Payment enrolment plugin implementation.
* @author Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_spay_plugin extends enrol_plugin
{
protected $lasternoller = null;
protected $lasternollerinstanceid = 0;
/**
* Returns optional enrolment information icons.
*
* This is used in course list for quick overview of enrolment options.
*
* We are not using single instance parameter because sometimes
* we might want to prevent icon repetition when multiple instances
* of one type exist. One instance may also produce several icons.
*
* @param array $instances all enrol instances of this type in one course
* @return array of pix_icon
*/
public function get_info_icons(array $instances)
{
foreach ($instances as $instance) {
if ($this->can_spay_enrol($instance, false) !== true) {
// User can not enrol himspay.
// Note that we do not check here if user is already enrolled for performance reasons -
// such check would execute extra queries for each course in the list of courses and
// would hide spay-enrolment icons from guests.
continue;
}
}
$icons = array();
$icons[] = new pix_icon('icon', get_string('pluginname', 'enrol_spay'), 'enrol_spay');
return $icons;
}
/**
* Returns localised name of enrol instance
*
* @param stdClass $instance (null is accepted too)
* @return string
*/
public function get_instance_name($instance)
{
global $DB;
if (empty($instance->name)) {
if (!empty($instance->roleid) and $role = $DB->get_record('role', array('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);
}
}
public function roles_protected()
{
// Users may tweak the roles later.
return false;
}
public function allow_unenrol(stdClass $instance)
{
// Users with unenrol cap may unenrol other users manually manually.
return true;
}
public function allow_manage(stdClass $instance)
{
// Users with manage cap may tweak period and status.
return true;
}
public function show_enrolme_link(stdClass $instance)
{
if (true !== $this->can_spay_enrol($instance, false)) {
return false;
}
return true;
}
/**
* 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) or !has_capability('enrol/spay:config', $context)) {
return false;
}
return true;
}
/**
* SPAY Sudani Payment enrol user to course
*
* @param stdClass $instance enrolment instance
* @param stdClass $data data needed for enrolment.
* @return bool|array true if enroled else eddor code and messege
*/
public function enrol_spay(stdClass $instance, $data = null)
{
global $DB, $USER, $CFG;
<<<<<<< HEAD
=======
<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> 9b19520677b7515acef0b0a5abc540e713bf6cda
>>>>>>> d8668c00992f3283667ddcdaf0998e32deb4dbb2
require_once('Spay/lib/Spay.php');
Spay::$url = $this->get_config('url');
Spay::$providerkey = $this->get_config('providerkey');
Spay::$username = $this->get_config('username');
Spay::$password = $this->get_config('passwd');
<<<<<<< HEAD
$record = $DB->get_record('enrol_spay', array('instanceid' => $instance->id),'*', IGNORE_MISSING);
if ($record) {
$result = Spay::enrol_spay_pay($data->pin, $record->requestid);
if($result->status){
=======
<<<<<<< HEAD
=======
=======
// require_once('./Spay/lib/Spay.php');
$spay = new Spay(
$this->get_config('url'),
$this->get_config('providerkey'),
$this->get_config('username'),
$this->get_config('passwd')
);
>>>>>>> 28fe3a301719f03891d6628fa35f6f85fdab6979
>>>>>>> 9b19520677b7515acef0b0a5abc540e713bf6cda
$record = $DB->get_record('enrol_spay', array('userid' => $USER->id, 'courseid' => $instance->courseid, 'instanceid' => $instance->id), IGNORE_MISSING);
if ($record) {
<<<<<<< HEAD
$result = Spay::enrol_spay_pay($data->pin, $record->requestid);
if($result->responseCode == 105){
$errors['pin'] = $result->responseMessage;
return;
}
>>>>>>> d8668c00992f3283667ddcdaf0998e32deb4dbb2
$new = new stdClass();
$new->id = $record->id;
$new->pin = $data->pin;
$new->subscriptiondate = time();
<<<<<<< HEAD
$DB->update_record('enrol_spay', $new, false);
$timestart = time();
$timeend = $instance->enrolperiod ? ($timestart + $instance->enrolperiod) : 0;
$this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
$this->email_welcome_message($instance, $USER);
}else{
return;
}
=======
$DB->insrt_record('enrol_spay', $new);
$timestart = time();
if ($instance->enrolperiod) {
$timeend = $timestart + $instance->enrolperiod;
} else {
$timeend = 0;
}
$this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
if ($instance->customint4 != ENROL_DO_NOT_SEND_EMAIL) {
$this->email_welcome_message($instance, $USER);
}
=======
$timestart = time();
// $timeend = $timestart + ;
>>>>>>> 9b19520677b7515acef0b0a5abc540e713bf6cda
>>>>>>> d8668c00992f3283667ddcdaf0998e32deb4dbb2
} else {
$enrol = Spay::enrol_spay_init_pay($data->msisdn, $data->servicecode);
$new = new stdClass();
$new->userid = $USER->id;
$new->courseid = $instance->courseid;
$new->instanceid = $instance->id;
$new->requestid = $enrol->requestId;
$new->msisdn = $data->msisdn;
$new->servicename = $instance->name;
$new->servicecost = $data->servicecost;
$new->servicecode = $data->servicecode;
$new->requestsubmitdate = time();
$DB->insert_record('enrol_spay', $new);
return;
}
$timestart = time();
if ($instance->enrolperiod) {
$timeend = $timestart + $instance->enrolperiod;
} else {
$timeend = 0;
}
$this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
\core\notification::success(get_string('youenrolledincourse', 'enrol'));
/* if ($instance->password and $instance->customint1 and $data->enrolpassword !== $instance->password) {
// It must be a group enrolment, let's assign group too.
$groups = $DB->get_records('groups', array('courseid' => $instance->courseid), 'id', 'id, enrolmentkey');
foreach ($groups as $group) {
if (empty($group->enrolmentkey)) {
continue;
}
if ($group->enrolmentkey === $data->enrolpassword) {
// Add user to group.
require_once($CFG->dirroot . '/group/lib.php');
groups_add_member($group->id, $USER->id);
break;
}
}
} */
// Send welcome message.
if ($instance->customint4 != ENROL_DO_NOT_SEND_EMAIL) {
$this->email_welcome_message($instance, $USER);
}
}
/**
* 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 $CFG, $OUTPUT, $USER;
require_once("$CFG->dirroot/enrol/spay/locallib.php");
$enrolstatus = $this->can_spay_enrol($instance);
if (true === $enrolstatus) {
// This user can spay enrol using this instance.
$form = new enrol_spay_enrol_form(null, $instance);
$instanceid = optional_param('instance', 0, PARAM_INT);
if ($instance->id == $instanceid) {
if ($data = $form->get_data()) {
$this->enrol_spay($instance, $data);
}
}
} else {
// This user can not spay enrol using this instance. Using an empty form to keep
// the UI consistent with other enrolment plugins that returns a form.
$data = new stdClass();
$data->header = $this->get_instance_name($instance);
$data->info = $enrolstatus;
// The can_spay_enrol call returns a button to the login page if the user is a
// guest, setting the login url to the form if that is the case.
$url = isguestuser() ? get_login_url() : null;
$form = new enrol_spay_empty_form($url, $data);
}
ob_start();
$form->display();
$output = ob_get_clean();
return $OUTPUT->box($output);
}
/**
* Checks if user can spay enrol.
*
* @param stdClass $instance enrolment instance
* @param bool $checkuserenrolment if true will check if user enrolment is inactive.
* used by navigation to improve performance.
* @return bool|string true if successful, else error message or false.
*/
public function can_spay_enrol(stdClass $instance, $checkuserenrolment = true)
{
global $CFG, $DB, $OUTPUT, $USER;
if ($checkuserenrolment) {
if (isguestuser()) {
// Can not enrol guest.
return get_string('noguestaccess', 'enrol') . $OUTPUT->continue_button(get_login_url());
}
// Check if user is already enroled.
if ($DB->get_record('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
return get_string('canntenrol', 'enrol_spay');
}
}
if ($instance->status != ENROL_INSTANCE_ENABLED) {
return get_string('canntenrol', 'enrol_spay');
}
/* if ($instance->enrolstartdate != 0 and $instance->enrolstartdate > time()) {
return get_string('canntenrolearly', 'enrol_spay', userdate($instance->enrolstartdate));
} */
/* if ($instance->enrolenddate != 0 and $instance->enrolenddate < time()) {
return get_string('canntenrollate', 'enrol_spay', userdate($instance->enrolenddate));
} */
/* if (!$instance->customint6) {
// New enrols not allowed.
return get_string('canntenrol', 'enrol_spay');
} */
if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
return get_string('canntenrol', 'enrol_spay');
}
// i move out thise link of code and hardcode max enrol to 1
$count = $DB->count_records('user_enrolments', array('enrolid' => $instance->id));
if ($count >= 1) {
// Bad luck, no more spay enrolments here.
return get_string('maxenrolledreached', 'enrol_spay');
}
/* if ($instance->customint3 > 0) {
// Max enrol limit specified.
} */
// if ($instance->customint5) {
// require_once("$CFG->dirroot/cohort/lib.php");
// if (!cohort_is_member($instance->customint5, $USER->id)) {
// $cohort = $DB->get_record('cohort', array('id' => $instance->customint5));
// if (!$cohort) {
// return null;
// }
// $a = format_string($cohort->name, true, array('context' => context::instance_by_id($cohort->contextid)));
// return markdown_to_html(get_string('cohortnonmemberinfo', 'enrol_spay', $a));
// }
// }
return true;
}
/**
* 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)
{
$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->can_spay_enrol($instance);
if ($instance->password) {
$instanceinfo->requiredparam = new stdClass();
$instanceinfo->requiredparam->enrolpassword = get_string('password', 'enrol_spay');
}
// If enrolment is possible and password is required then return ws function name to get more information.
if ((true === $instanceinfo->status) && $instance->password) {
$instanceinfo->wsfunction = 'enrol_spay_get_instance_info';
}
return $instanceinfo;
}
/**
* Add new instance of enrol plugin with default settings.
* @param stdClass $course
* @return int id of new instance
*/
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 = 1;
$notifyall = 1;
$fields = array();
$fields['status'] = $this->get_config('status');
$fields['roleid'] = $this->get_config('roleid');
$fields['enrolperiod'] = $this->get_config('enrolperiod');
$fields['expirynotify'] = $expirynotify;
$fields['notifyall'] = $notifyall;
$fields['expirythreshold'] = $this->get_config('expirythreshold');
// $fields['customint1'] = $this->get_config('groupkey');
// $fields['customint2'] = $this->get_config('longtimenosee');
// $fields['customint3'] = $this->get_config('maxenrolled');
$fields['customint4'] = $this->get_config('sendcoursewelcomemessage');
$fields['customint5'] = 0;
// $fields['customint6'] = $this->get_config('newenrols');
return $fields;
}
/**
* Send welcome email to specified user.
*
* @param stdClass $instance
* @param stdClass $user user record
* @return void
*/
protected function email_welcome_message($instance, $user)
{
global $CFG, $DB;
$course = $DB->get_record('course', array('id' => $instance->courseid), '*', MUST_EXIST);
$context = context_course::instance($course->id);
$a = new stdClass();
$a->coursename = format_string($course->fullname, true, array('context' => $context));
$a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id";
if (trim($instance->customtext1) !== '') {
$message = $instance->customtext1;
$key = array('{$a->coursename}', '{$a->profileurl}', '{$a->fullname}', '{$a->email}');
$value = array($a->coursename, $a->profileurl, 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, array('context' => $context, 'para' => false, 'newlines' => true, 'filter' => true));
$messagetext = html_to_text($messagehtml);
}
} else {
$messagetext = get_string('welcometocoursetext', 'enrol_spay', $a);
$messagehtml = text_to_html($messagetext, null, false, true);
}
$subject = get_string('welcometocourse', 'enrol_spay', format_string($course->fullname, true, array('context' => $context)));
$sendoption = $instance->customint4;
$contact = $this->get_welcome_email_contact($sendoption, $context);
// Directly emailing welcome message rather than using messaging.
email_to_user($user, $contact, $subject, $messagetext, $messagehtml);
}
/**
* Sync all meta course links.
*
* @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(progress_trace $trace, $courseid = null)
{
global $DB;
if (!enrol_is_enabled('spay')) {
$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 spay-enrolments...');
$params = array('now' => time(), 'useractive' => ENROL_USER_ACTIVE, 'courselevel' => CONTEXT_COURSE);
$coursesql = "";
if ($courseid) {
$coursesql = "AND e.courseid = :courseid";
$params['courseid'] = $courseid;
}
// Note: the logic of spay enrolment guarantees that user logged in at least once (=== u.lastaccess set)
// and that user accessed course at least once too (=== user_lastaccess record exists).
// 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 = 'spay' AND e.customint2 > 0)
JOIN {user} u ON u.id = ue.userid
WHERE :now - u.lastaccess > e.customint2
$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->customint2 / 60 * 60 * 24;
$trace->output("unenrolling user $userid from course $instance->courseid as they have 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 = 'spay' AND e.customint2 > 0)
JOIN {user_lastaccess} ul ON (ul.userid = ue.userid AND ul.courseid = e.courseid)
WHERE :now - ul.timeaccess > e.customint2
$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->customint2 / 60 * 60 * 24;
$trace->output("unenrolling user $userid from course $instance->courseid as they have did not access course for at least $days days", 1);
}
$rs->close();
$trace->output('...user spay-enrolment updates finished.');
$trace->finished();
$this->process_expirations($trace, $courseid);
return 0;
}
/**
* Returns the user who is responsible for spay enrolments in given instance.
*
* Usually it is the first editing teacher - the person with "highest authority"
* as defined by sort_by_roleassignment_authority() having 'enrol/spay:manage'
* capability.
*
* @param int $instanceid enrolment instance id
* @return stdClass user record
*/
protected function get_enroller($instanceid)
{
global $DB;
if ($this->lasternollerinstanceid == $instanceid and $this->lasternoller) {
return $this->lasternoller;
}
$instance = $DB->get_record('enrol', array('id' => $instanceid, 'enrol' => $this->get_name()), '*', MUST_EXIST);
$context = context_course::instance($instance->courseid);
if ($users = get_enrolled_users($context, 'enrol/spay: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;
}
/**
* 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 = array(
'courseid' => $data->courseid,
'enrol' => $this->get_name(),
'status' => $data->status,
'roleid' => $data->roleid,
);
}
if ($merge and $instances = $DB->get_records('enrol', $merge, 'id')) {
$instance = reset($instances);
$instanceid = $instance->id;
} else {
if (!empty($data->customint5)) {
if ($step->get_task()->is_samesite()) {
// Keep cohort restriction unchanged - we are on the same site.
} else {
// Use some id that can not exist in order to prevent spay enrolment,
// because we do not know what cohort it is in this site.
$data->customint5 = -1;
}
}
$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 $oldinstancestatus
* @param int $userid
*/
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)
{
// This is necessary only because we may migrate other types to this instance,
// we do not use component in manual or spay enrol.
role_assign($roleid, $userid, $contextid, '', 0);
}
/**
* 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/spay: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);
if (!has_capability('enrol/spay:config', $context)) {
return false;
}
// If the instance is currently disabled, before it can be enabled,
// we must check whether the password meets the password policies.
if ($instance->status == ENROL_INSTANCE_DISABLED) {
if ($this->get_config('requirepassword')) {
if (empty($instance->password)) {
return false;
}
}
// Only check the password if it is set.
if (!empty($instance->password) && $this->get_config('usepasswordpolicy')) {
if (!check_password_policy($instance->password, $errmsg)) {
return false;
}
}
}
return true;
}
/**
* Return an array of valid options for the status.
*
* @return array
*/
protected function get_status_options()
{
$options = array(
ENROL_INSTANCE_ENABLED => get_string('yes'),
ENROL_INSTANCE_DISABLED => get_string('no')
);
return $options;
}
/**
* Return an array of valid options for the newenrols property.
*
* @return array
*/
protected function get_newenrols_options()
{
$options = array(1 => get_string('yes'), 0 => get_string('no'));
return $options;
}
/**
* Return an array of valid options for the groupkey property.
*
* @return array
*/
protected function get_groupkey_options()
{
$options = array(1 => get_string('yes'), 0 => get_string('no'));
return $options;
}
/**
* Return an array of valid options for the expirynotify property.
*
* @return array
*/
protected function get_expirynotify_options()
{
$options = array(
0 => get_string('no'),
1 => get_string('expirynotifyenroller', 'enrol_spay'),
2 => get_string('expirynotifyall', 'enrol_spay')
);
return $options;
}
/**
* Return an array of valid options for the longtimenosee property.
*
* @return array
*/
protected function get_longtimenosee_options()
{
$options = array(
0 => get_string('never'),
1800 * 3600 * 24 => get_string('numdays', '', 1800),
1000 * 3600 * 24 => get_string('numdays', '', 1000),
365 * 3600 * 24 => get_string('numdays', '', 365),
180 * 3600 * 24 => get_string('numdays', '', 180),
150 * 3600 * 24 => get_string('numdays', '', 150),
120 * 3600 * 24 => get_string('numdays', '', 120),
90 * 3600 * 24 => get_string('numdays', '', 90),
60 * 3600 * 24 => get_string('numdays', '', 60),
30 * 3600 * 24 => get_string('numdays', '', 30),
21 * 3600 * 24 => get_string('numdays', '', 21),
14 * 3600 * 24 => get_string('numdays', '', 14),
7 * 3600 * 24 => get_string('numdays', '', 7)
);
return $options;
}
/**
* The spay enrollment 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)
{
global $CFG;
require_once($CFG->dirroot . '/enrol/spay/locallib.php');
$context = $manager->get_context();
$bulkoperations = array();
if (has_capability("enrol/spay:manage", $context)) {
$bulkoperations['editselectedusers'] = new enrol_spay_editselectedusers_operation($manager, $this);
}
if (has_capability("enrol/spay:unenrol", $context)) {
$bulkoperations['deleteselectedusers'] = new enrol_spay_deleteselectedusers_operation($manager, $this);
}
return $bulkoperations;
}
/**
* Add elements to the edit instance form.
*
* @param stdClass $instance
* @param MoodleQuickForm $mform
* @param context $context
* @return bool
*/
public function edit_instance_form($instance, MoodleQuickForm $mform, $context)
{
global $CFG;
// Merge these two settings to one value for the single selection element.
if ($instance->notifyall and $instance->expirynotify) {
$instance->expirynotify = 2;
}
unset($instance->notifyall);
$services = array();
$servicenames = array();
foreach (explode("\n", $this->get_config('servicecodes')) as $service) {
$tmp = explode("|", $service);
$services[$tmp[0]]['code'] = $tmp[1];
$services[$tmp[0]]['cost'] = intval($tmp[2]);
$servicenames["{$tmp[0]}"] = $tmp[0];
}
$mform->addElement('select', 'name', get_string('custominstancename', 'enrol'), $servicenames);
$mform->setType('name', PARAM_TEXT);
$options = $this->get_status_options();
$mform->addElement('select', 'status', get_string('status', 'enrol_spay'), $options);
// $mform->addHelpButton('status', 'status', 'enrol_spay');
/*
$options = $this->get_newenrols_options();
$mform->addElement('select', 'customint6', get_string('newenrols', 'enrol_spay'), $options);
$mform->addHelpButton('customint6', 'newenrols', 'enrol_spay');
$mform->disabledIf('customint6', 'status', 'eq', ENROL_INSTANCE_DISABLED);
*/
$options = array('optional' => true, 'defaultunit' => 86400);
$mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_spay'), $options);
$mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_spay');
/*
$options = $this->get_expirynotify_options();
$mform->addElement('select', 'expirynotify', get_string('expirynotify', 'core_enrol'), $options);
$mform->addHelpButton('expirynotify', 'expirynotify', 'core_enrol');
*/
/*
$options = array('optional' => false, 'defaultunit' => 3600);
$mform->addElement('duration', 'expirythreshold', get_string('expirythreshold', 'core_enrol'), $options);
$mform->addHelpButton('expirythreshold', 'expirythreshold', 'core_enrol');
$mform->disabledIf('expirythreshold', 'expirynotify', 'eq', 0);
*/
/*
$options = array('optional' => true);
$mform->addElement('date_time_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_spay'), $options);
$mform->setDefault('enrolstartdate', 0);
$mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_spay');
*/
/*
$options = array('optional' => true);
$mform->addElement('date_time_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_spay'), $options);
$mform->setDefault('enrolenddate', 0);
$mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_spay');
*/
/*
$options = $this->get_longtimenosee_options();
$mform->addElement('select', 'customint2', get_string('longtimenosee', 'enrol_spay'), $options);
$mform->addHelpButton('customint2', 'longtimenosee', 'enrol_spay');
*/
/*
$mform->addElement('text', 'customint3', get_string('maxenrolled', 'enrol_spay'));
$mform->addHelpButton('customint3', 'maxenrolled', 'enrol_spay');
$mform->setType('customint3', PARAM_INT);
*/
// require_once($CFG->dirroot . '/cohort/lib.php');
// $cohorts = array(0 => get_string('no'));
// $allcohorts = cohort_get_available_cohorts($context, 0, 0, 0);
// if ($instance->customint5 && !isset($allcohorts[$instance->customint5])) {
// $c = $DB->get_record(
// 'cohort',
// array('id' => $instance->customint5),
// 'id, name, idnumber, contextid, visible',
// IGNORE_MISSING
// );
// if ($c) {
// // Current cohort was not found because current user can not see it. Still keep it.
// $allcohorts[$instance->customint5] = $c;
// }
// }
// foreach ($allcohorts as $c) {
// $cohorts[$c->id] = format_string($c->name, true, array('context' => context::instance_by_id($c->contextid)));
// if ($c->idnumber) {
// $cohorts[$c->id] .= ' [' . s($c->idnumber) . ']';
// }
// }
// if ($instance->customint5 && !isset($allcohorts[$instance->customint5])) {
// // Somebody deleted a cohort, better keep the wrong value so that random ppl can not enrol.
// $cohorts[$instance->customint5] = get_string('unknowncohort', 'cohort', $instance->customint5);
// }
// if (count($cohorts) > 1) {
// $mform->addElement('select', 'customint5', get_string('cohortonly', 'enrol_spay'), $cohorts);
// $mform->addHelpButton('customint5', 'cohortonly', 'enrol_spay');
// } else {
// $mform->addElement('hidden', 'customint5');
// $mform->setType('customint5', PARAM_INT);
// $mform->setConstant('customint5', 0);
// }
$mform->addElement(
'select',
'customint4',
get_string('sendcoursewelcomemessage', 'enrol_spay'),
enrol_send_welcome_email_options()
);
$mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_spay');
$options = array('cols' => '60', 'rows' => '8');
$mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'enrol_spay'), $options);
$mform->addHelpButton('customtext1', 'customwelcomemessage', 'enrol_spay');
if (enrol_accessing_via_instance($instance)) {
$warntext = get_string('instanceeditspaywarningtext', 'core_enrol');
$mform->addElement('static', 'spaywarn', get_string('instanceeditspaywarning', 'core_enrol'), $warntext);
}
}
/**
* 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;
}
/**
* Perform custom validation of the data used to edit the instance.
*
* @param array $data array of ("fieldname"=>value) of submitted data
* @param array $files array of uploaded files "element_name"=>tmp_file_path
* @param object $instance The instance loaded from the DB
* @param context $context The context of the instance we are editing
* @return array of "element_name"=>"error_description" if there are errors,
* or an empty array if everything is OK.
* @return void
*/
public function edit_instance_validation($data, $files, $instance, $context)
{
$errors = array();
// if ($data['status'] == ENROL_INSTANCE_ENABLED) {
// if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) {
// $errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_spay');
// }
// }
/* if ($data['expirynotify'] > 0 and $data['expirythreshold'] < 86400) {
$errors['expirythreshold'] = get_string('errorthresholdlow', 'core_enrol');
} */
$validstatus = array_keys($this->get_status_options());
// $validnewenrols = array_keys($this->get_newenrols_options());
// if (core_text::strlen($data['password']) > 50) {