|
27 | 27 | require_once($CFG->libdir . '/tablelib.php'); |
28 | 28 |
|
29 | 29 | $id = required_param('id', PARAM_INT); |
30 | | -admin_externalpage_setup('enrolsettingsarlocontactmergefailure', |
31 | | - null, ['id' => $id], '/enrol/arlo/admin/contactmergefailure.php'); |
| 30 | +$action = optional_param('action', '', PARAM_ALPHA); |
| 31 | +admin_externalpage_setup( |
| 32 | + 'enrolsettingsarlocontactmergefailure', |
| 33 | + null, |
| 34 | + ['id' => $id], |
| 35 | + '/enrol/arlo/admin/contactmergefailure.php' |
| 36 | +); |
32 | 37 | $registration = new \enrol_arlo\local\persistent\registration_persistent($id); |
33 | 38 | $contact = $registration->get_contact(); |
34 | 39 | $event = $registration->get_event(); |
35 | 40 | $onlineactivity = $registration->get_online_activity(); |
36 | 41 | $code = ($event) ? $event->get('code') : $onlineactivity->get('code'); |
37 | 42 | $returnurl = new moodle_url('/enrol/arlo/admin/unsuccessfulenrolments.php'); |
38 | 43 | $output = $PAGE->get_renderer('enrol_arlo'); |
| 44 | +// Check for failed contact merge requests first. |
| 45 | +$contactmergerequests = \enrol_arlo\local\persistent\contact_merge_request_persistent::get_records( |
| 46 | + ['destinationcontactid' => $contact->get('sourceid'), 'mergefailed' => 1] |
| 47 | +); |
| 48 | +if (!$contactmergerequests) { |
| 49 | + redirect($returnurl); |
| 50 | +} |
| 51 | +// Just deal with first. |
| 52 | +$contactmergerequest = reset($contactmergerequests); |
| 53 | +$sourcecontact = $contactmergerequest->get_source_contact(); |
| 54 | +$destinationcontact = $contactmergerequest->get_destination_contact(); |
| 55 | +// When both contacts resolve to the same Moodle user there is nothing to merge, |
| 56 | +// so the merge request can be safely marked as complete. |
| 57 | +$sameuser = $sourcecontact && $destinationcontact |
| 58 | + && $sourcecontact->get('userid') > 0 |
| 59 | + && $sourcecontact->get('userid') == $destinationcontact->get('userid'); |
| 60 | +if ($action === 'markcomplete' && confirm_sesskey()) { |
| 61 | + if (!$sameuser || !enrol_is_enabled('arlo')) { |
| 62 | + throw new moodle_exception('invalidrecord'); |
| 63 | + } |
| 64 | + // Mark the merge request as applied. |
| 65 | + $contactmergerequest->set('active', 0); |
| 66 | + $contactmergerequest->set('mergefailed', 0); |
| 67 | + $contactmergerequest->update(); |
| 68 | + // Re-attempt the enrolment now that the merge request is resolved. |
| 69 | + $plugin = \enrol_arlo\api::get_enrolment_plugin(); |
| 70 | + $enrolmentinstance = $plugin::get_instance_record($registration->get('enrolid'), MUST_EXIST); |
| 71 | + $result = \enrol_arlo\local\job\memberships_job::process_enrolment_registration( |
| 72 | + $enrolmentinstance, |
| 73 | + $registration |
| 74 | + ); |
| 75 | + $message = ($result) ? get_string('success') : get_string('failed', 'enrol_arlo'); |
| 76 | + redirect($returnurl, $message, 1); |
| 77 | +} |
39 | 78 | echo $OUTPUT->header(); |
40 | 79 | $params = [ |
41 | 80 | 'fullname' => $contact->get('firstname') . ' ' . $contact->get('lastname'), |
42 | | - 'code' => $code |
| 81 | + 'code' => $code, |
43 | 82 | ]; |
44 | 83 | $heading = get_string('unsuccessfulenrolmentof', 'enrol_arlo', $params); |
45 | 84 | echo $OUTPUT->heading(format_string($heading), 3); |
46 | | -// Check for failed contact merge requests first. |
47 | | -$contactmergerequests = \enrol_arlo\local\persistent\contact_merge_request_persistent::get_records( |
48 | | - ['destinationcontactid' => $contact->get('sourceid'), 'mergefailed' => 1] |
49 | | -); |
50 | | -if ($contactmergerequests) { |
51 | | - // Just deal with first. |
52 | | - $contactmergerequest = reset($contactmergerequests); |
53 | | - $sourcecontact = new \enrol_arlo\output\contact($contactmergerequest->get_source_contact(), 'source'); |
54 | | - $destinationcontact = new \enrol_arlo\output\contact($contactmergerequest->get_destination_contact(), 'destination'); |
55 | | - echo $OUTPUT->heading(get_string('contactmergerequestfailure', 'enrol_arlo'), 3); |
56 | | - echo html_writer::start_div('container'); |
57 | | - echo html_writer::start_div('row'); |
58 | | - echo html_writer::start_div('col-sm-6'); |
59 | | - echo $output->render($sourcecontact); |
60 | | - echo html_writer::end_div(); |
61 | | - echo html_writer::start_div('col-sm-6'); |
62 | | - echo $output->render($destinationcontact); |
63 | | - echo html_writer::end_div(); |
64 | | - echo html_writer::end_div(); |
65 | | - echo html_writer::end_div(); |
66 | | -} else { |
67 | | - redirect($returnurl); |
| 85 | +$sourcecontactoutput = new \enrol_arlo\output\contact($sourcecontact, 'source'); |
| 86 | +$destinationcontactoutput = new \enrol_arlo\output\contact($destinationcontact, 'destination'); |
| 87 | +echo $OUTPUT->heading(get_string('contactmergerequestfailure', 'enrol_arlo'), 3); |
| 88 | +echo html_writer::start_div('container'); |
| 89 | +echo html_writer::start_div('row'); |
| 90 | +echo html_writer::start_div('col-sm-6'); |
| 91 | +echo $output->render($sourcecontactoutput); |
| 92 | +echo html_writer::end_div(); |
| 93 | +echo html_writer::start_div('col-sm-6'); |
| 94 | +echo $output->render($destinationcontactoutput); |
| 95 | +echo html_writer::end_div(); |
| 96 | +echo html_writer::end_div(); |
| 97 | +echo html_writer::end_div(); |
| 98 | +if ($sameuser) { |
| 99 | + echo $OUTPUT->notification(get_string('contactmergerequestsameuser', 'enrol_arlo'), 'info'); |
| 100 | + $markcompleteurl = new moodle_url($PAGE->url, ['action' => 'markcomplete', 'sesskey' => sesskey()]); |
| 101 | + $markcompletebutton = new single_button($markcompleteurl, get_string('markmergecomplete', 'enrol_arlo'), 'post'); |
| 102 | + $markcompletebutton->add_confirm_action(get_string('markmergecompleteconfirm', 'enrol_arlo')); |
| 103 | + echo $OUTPUT->render($markcompletebutton); |
68 | 104 | } |
69 | 105 | echo html_writer::start_div('row float-right'); |
70 | 106 | echo html_writer::start_tag('h4'); |
71 | | -echo $OUTPUT->action_link($returnurl, |
72 | | - get_string('returntounsucessfulenrolments', 'enrol_arlo')); |
| 107 | +echo $OUTPUT->action_link( |
| 108 | + $returnurl, |
| 109 | + get_string('returntounsucessfulenrolments', 'enrol_arlo') |
| 110 | +); |
73 | 111 | echo html_writer::end_tag('h4'); |
74 | 112 | echo html_writer::end_div(); |
75 | 113 | echo $OUTPUT->footer(); |
0 commit comments