-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
798 lines (695 loc) · 29.9 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
<?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/>.
/**
* enrol_codes enrolment plugin.
*
*/
/**
* enrol_codes enrolment plugin implementation.
*/
class enrol_codes_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 $instance) {
$key = true;
$icons = array();
if ($key) {
$icons[] = new pix_icon('withkey', get_string('pluginname', 'enrol_codes'), 'enrol_codes');
}
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_enrol_by_code($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) {
global $DB;
$context = context_course::instance($courseid, MUST_EXIST);
if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/codes:config', $context)) {
return false;
}
if ($DB->record_exists('enrol', array('courseid'=>$courseid, 'enrol'=>'codes'))) {
// Multiple instances not supported.
return false;
}
return true;
}
/**
* Self 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_self(stdClass $instance, $data = null) {
global $DB, $USER, $CFG;
// Don't enrol user if password is not passed when required.
if (!isset($data->enrolpassword)) {
return;
}
$timestart = time();
if ($instance->enrolperiod) {
$timeend = $timestart + $instance->enrolperiod;
} else {
$timeend = 0;
}
$this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
//now update the record
//$params=array('enrolid'=>$instance->id,'enrol_code'=>$data->enrolpassword);
$sql="UPDATE {enrol_codes} SET userid=".$USER->id. " WHERE enrol_code='".$data->enrolpassword."' AND enrolid=".$instance->id ;
$DB->execute($sql);
\core\notification::success(get_string('youenrolledincourse', 'enrol'));
// 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/codes/locallib.php");
$enrolstatus = $this->can_enrol_by_code($instance);
if (true === $enrolstatus) {
// This user can self enrol using this instance.
$form = new enrol_codes_enrol_form(null, $instance);
$instanceid = optional_param('instance', 0, PARAM_INT);
if ($instance->id == $instanceid) {
if ($data = $form->get_data()) {
$this->enrol_self($instance, $data);
}
}
} else {
// This user can not self 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_enrol_by_code 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_codes_empty_form($url, $data);
}
ob_start();
$form->display();
$output = ob_get_clean();
return $OUTPUT->box($output);
}
/**
* Checks if user can enrol using enrollment codes.
*
* @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_enrol_by_code(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_codes');
}
}
if ($instance->status != ENROL_INSTANCE_ENABLED) {
return get_string('canntenrol', 'enrol_codes');
}
if ($instance->enrolstartdate != 0 and $instance->enrolstartdate > time()) {
return get_string('canntenrolearly', 'enrol_codes', userdate($instance->enrolstartdate));
}
if ($instance->enrolenddate != 0 and $instance->enrolenddate < time()) {
return get_string('canntenrollate', 'enrol_codes', userdate($instance->enrolenddate));
}
if (!$instance->customint6) {
// New enrols not allowed.
return get_string('canntenrol', 'enrol_codes');
}
if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
return get_string('canntenrol', 'enrol_codes');
}
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_enrol_by_code($instance);
$instanceinfo->requiredparam = new stdClass();
$instanceinfo->requiredparam->enrolpassword = get_string('password', 'enrol_codes');
// If enrolment is possible and password is required then return ws function name to get more information.
if ((true === $instanceinfo->status)) {
$instanceinfo->wsfunction = 'enrol_codes_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 $instanceid= $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 = 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['customint4'] = $this->get_config('sendcoursewelcomemessage');
$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_codes', $a);
$messagehtml = text_to_html($messagetext, null, false, true);
}
$subject = get_string('welcometocourse', 'enrol_codes', 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);
}
/**
* Returns the user who is responsible for codes 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/codes: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/codes: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 {
$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 self 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/codes: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/codes:config', $context)) {
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 expirynotify property.
*
* @return array
*/
protected function get_expirynotify_options() {
$options = array(0 => get_string('no'),
1 => get_string('expirynotifyenroller', 'enrol_codes'),
2 => get_string('expirynotifyall', 'enrol_codes'));
return $options;
}
/**
* The self 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/codes/locallib.php');
$context = $manager->get_context();
$bulkoperations = array();
if (has_capability("enrol/codes:manage", $context)) {
$bulkoperations['editselectedusers'] = new enrol_codes_editselectedusers_operation($manager, $this);
}
if (has_capability("enrol/codes:unenrol", $context)) {
$bulkoperations['deleteselectedusers'] = new enrol_codes_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, $DB;
// Merge these two settings to one value for the single selection element.
if ($instance->notifyall and $instance->expirynotify) {
$instance->expirynotify = 2;
}
unset($instance->notifyall);
$nameattribs = array('size' => '20', 'maxlength' => '255');
$mform->addElement('text', 'name', get_string('custominstancename', 'enrol'), $nameattribs);
$mform->setType('name', PARAM_TEXT);
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'server');
$options = $this->get_status_options();
$mform->addElement('select', 'status', get_string('status', 'enrol_codes'), $options);
$mform->addHelpButton('status', 'status', 'enrol_codes');
$options = $this->get_newenrols_options();
$mform->addElement('select', 'customint6', get_string('newenrols', 'enrol_codes'), $options);
$mform->addHelpButton('customint6', 'newenrols', 'enrol_codes');
$mform->disabledIf('customint6', 'status', 'eq', ENROL_INSTANCE_DISABLED);
$roles = $this->extend_assignable_roles($context, $instance->roleid);
$mform->addElement('select', 'roleid', get_string('role', 'enrol_codes'), $roles);
$options = array('optional' => true, 'defaultunit' => 86400);
$mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_codes'), $options);
$mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_codes');
$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' => 86400);
$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_codes'), $options);
$mform->setDefault('enrolstartdate', 0);
$mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_codes');
$options = array('optional' => true);
$mform->addElement('date_time_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_codes'), $options);
$mform->setDefault('enrolenddate', 0);
$mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_codes');
$mform->addElement('select', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_codes'),
enrol_send_welcome_email_options());
$mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_codes');
$options = array('cols' => '60', 'rows' => '8');
$mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'enrol_codes'), $options);
$mform->addHelpButton('customtext1', 'customwelcomemessage', 'enrol_codes');
if (enrol_accessing_via_instance($instance)) {
$warntext = get_string('instanceeditselfwarningtext', 'core_enrol');
$mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), $warntext);
}
}
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();
$checkpassword = true;
if ($data['status'] == ENROL_INSTANCE_ENABLED) {
if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) {
$errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_self');
}
}
if ($data['expirynotify'] > 0 and $data['expirythreshold'] < 86400) {
$errors['expirythreshold'] = get_string('errorthresholdlow', 'core_enrol');
}
// Now these ones are checked by quickforms, but we may be called by the upload enrolments tool, or a webservive.
if (core_text::strlen($data['name']) > 255) {
$errors['name'] = get_string('err_maxlength', 'form', 255);
}
$validstatus = array_keys($this->get_status_options());
$validnewenrols = array_keys($this->get_newenrols_options());
$context = context_course::instance($instance->courseid);
$validroles = array_keys($this->extend_assignable_roles($context, $instance->roleid));
$validexpirynotify = array_keys($this->get_expirynotify_options());
$tovalidate = array(
'enrolstartdate' => PARAM_INT,
'enrolenddate' => PARAM_INT,
'name' => PARAM_TEXT,
'customint4' => PARAM_INT,
'customint6' => $validnewenrols,
'status' => $validstatus,
'enrolperiod' => PARAM_INT,
'expirynotify' => $validexpirynotify,
'roleid' => $validroles
);
if ($data['expirynotify'] != 0) {
$tovalidate['expirythreshold'] = PARAM_INT;
}
$typeerrors = $this->validate_param_types($data, $tovalidate);
$errors = array_merge($errors, $typeerrors);
return $errors;
}
/**
* Add new instance of enrol plugin.
* @param object $course
* @param array $fields instance fields
* @return int id of new instance, null if can not be created
*/
public function add_instance($course, array $fields = null) {
global $DB;
if ($DB->record_exists('enrol', array('courseid'=>$course->id, 'enrol'=>'codes'))) {
// only one instance allowed, sorry
return NULL;
}
// In the form we are representing 2 db columns with one field.
if (!empty($fields) && !empty($fields['expirynotify'])) {
if ($fields['expirynotify'] == 2) {
$fields['expirynotify'] = 1;
$fields['notifyall'] = 1;
} else {
$fields['notifyall'] = 0;
}
}
return parent::add_instance($course, $fields);
}
/**
* Update instance of enrol plugin.
* @param stdClass $instance
* @param stdClass $data modified instance fields
* @return boolean
*/
public function update_instance($instance, $data) {
// In the form we are representing 2 db columns with one field.
if ($data->expirynotify == 2) {
$data->expirynotify = 1;
$data->notifyall = 1;
} else {
$data->notifyall = 0;
}
// Keep previous/default value of disabled expirythreshold option.
if (!$data->expirynotify) {
$data->expirythreshold = $instance->expirythreshold;
}
// Add previous value of newenrols if disabled.
if (!isset($data->customint6)) {
$data->customint6 = $instance->customint6;
}
return parent::update_instance($instance, $data);
}
/**
* Gets a list of roles that this user can assign for the course as the default for codes-enrolment.
*
* @param context $context the context.
* @param integer $defaultrole the id of the role that is set as the default for codes-enrolment
* @return array index is the role id, value is the role name
*/
public function extend_assignable_roles($context, $defaultrole) {
global $DB;
$roles = get_assignable_roles($context, ROLENAME_BOTH);
if (!isset($roles[$defaultrole])) {
if ($role = $DB->get_record('role', array('id' => $defaultrole))) {
$roles[$defaultrole] = role_get_name($role, $context, ROLENAME_BOTH);
}
}
return $roles;
}
/**
* Get the "from" contact which the email will be sent from.
*
* @param int $sendoption send email from constant ENROL_SEND_EMAIL_FROM_*
* @param $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;
$contact = null;
// Send as the first user assigned as the course contact.
if ($sendoption == ENROL_SEND_EMAIL_FROM_COURSE_CONTACT) {
$rusers = array();
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 {
$allnames = get_all_user_name_fields(true, 'u');
$rusers = get_role_users($croles[$i], $context, true, 'u.id, u.confirmed, u.username, '. $allnames . ',
u.email, r.sortorder, ra.id', 'r.sortorder, ra.id ASC, ' . $sort, null, '', '', '', '', $sortparams);
$i++;
} while (empty($rusers) && !empty($croles[$i]));
}
if ($rusers) {
$contact = array_values($rusers)[0];
}
}
// If send welcome email option is set to no reply or if previous options have
// returned a contact send welcome message as noreplyuser.
if ($sendoption == ENROL_SEND_EMAIL_FROM_NOREPLY || empty($contact)) {
$contact = core_user::get_noreply_user();
}
return $contact;
}
public function is_password_valid($password,$instance)
{
global $CFG,$DB;
$params=array('enrolid'=>$instance->id,'enrol_code'=>$password);
$is_valid_obj=$DB->get_record_select('enrol_codes',
"userid IS NULL AND enrolid= :enrolid AND enrol_code= :enrol_code",$params);
if (!empty($is_valid_obj)) {
return true;
}
return false;
}
}
/**
* Get icon mapping for font-awesome.
*/
function enrol_codes_get_fontawesome_icon_map() {
return [
'enrol_codes:withkey' => 'fa-key',
'enrol_codes:withoutkey' => 'fa-sign-in',
];
}
function enrol_codes_extend_navigation_course($navigation, $course, $context) {
global $DB;
$context = context_course::instance($course->id, MUST_EXIST);
// Check that the codes plugin is enabled.
// check if course has any instance of it or not
$conditions=array('courseid'=>$course->id,'enrol'=>'codes');
$instance=$DB->get_record('enrol', $conditions, $fields='id,status');
if (!empty($instance) && $instance->status==ENROL_INSTANCE_ENABLED) {
// Check that they can add an instance.
if (has_capability('enrol/codes:config', $context)) {
$url = new moodle_url('/enrol/codes/codes.php', array('courseid' => $course->id));
$settingsnode = navigation_node::create(get_string('enrolcodes', 'enrol_codes'), $url,
navigation_node::TYPE_SETTING, null, null, new pix_icon('e/table', ''));
$navigation->add_node($settingsnode);
}
}
}