From cec7f27c2938701335fce60ea499cea927bb0b5e Mon Sep 17 00:00:00 2001 From: Johannes Burk Date: Wed, 1 Jun 2016 17:02:57 +0200 Subject: [PATCH] Optimize sending application notification to teachers and managers. --- lib.php | 122 ++++++++++++++++++--------------------------------- renderer.php | 56 +++++++++++------------ 2 files changed, 70 insertions(+), 108 deletions(-) diff --git a/lib.php b/lib.php index ec816b6..b8f377d 100644 --- a/lib.php +++ b/lib.php @@ -62,11 +62,6 @@ class enrol_apply_plugin extends enrol_plugin { if ($data = $form->get_data()) { // Only process when form submission is for this instance (multi instance support). if ($data->instance == $instance->id) { - $userInfo = $data; - $applydescription = $userInfo->applydescription; - unset($userInfo->applydescription); - $userInfo->id = $USER->id; - $timestart = 0; $timeend = 0; $roleid = $instance->roleid; @@ -75,10 +70,10 @@ class enrol_apply_plugin extends enrol_plugin { $userenrolment = $DB->get_record('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id), 'id', MUST_EXIST); $applicationinfo = new stdClass(); $applicationinfo->userenrolmentid = $userenrolment->id; - $applicationinfo->comment = $applydescription; + $applicationinfo->comment = $data->applydescription; $DB->insert_record('enrol_apply_applicationinfo', $applicationinfo, false); - $this->sendConfirmMailToTeachers($instance, $data, $applydescription); - $this->sendConfirmMailToManagers($instance, $data, $applydescription); + + $this->send_application_notification($instance, $USER->id, $data); redirect("$CFG->wwwroot/course/view.php?id=$instance->courseid"); } @@ -284,89 +279,56 @@ class enrol_apply_plugin extends enrol_plugin { email_to_user($user, $contact, $subject, html_to_text($body), $body); } - function sendConfirmMailToTeachers($instance,$info,$applydescription){ - global $DB; - global $CFG; - global $USER; - - $courseid = $instance->courseid; - $instanceid = $instance->id; - - if($instance->customint3 == 1){ - $course = get_course($courseid); - $context = context_course::instance($courseid, MUST_EXIST); - $teacherType = $DB->get_record('role',array("shortname"=>"editingteacher")); - $teachers = $DB->get_records('role_assignments', array('contextid'=>$context->id,'roleid'=>$teacherType->id)); + function send_application_notification($instance, $userid, $data) { + global $PAGE; - if (!$instance->customint1) { - $info = null; - } + $renderer = $PAGE->get_renderer('enrol_apply'); - $extra = null; - if($instance->customint2){ - require_once($CFG->dirroot.'/user/profile/lib.php'); - $user = $DB->get_record('user',array('id'=>$USER->id)); - profile_load_custom_fields($user); - $extra = $user->profile; - } - - $manageurl = new moodle_url("/enrol/apply/manage.php", array('id'=>$instanceid)); - - global $PAGE; - $renderer = $PAGE->get_renderer('enrol_apply'); - $body = $renderer->application_notification_mail_body($course, $USER, $manageurl, $applydescription, $info, $extra); + $course = get_course($instance->courseid); + $user = core_user::get_user($userid); + $contact = core_user::get_support_user(); - $contact = core_user::get_support_user(); + // Include standard user profile fields? + $standarduserfields = null; + if ($instance->customint1) { + $standarduserfields = $data; + unset($standarduserfields->applydescription); + } - foreach($teachers as $teacher){ - $editTeacher = $DB->get_record('user',array('id'=>$teacher->userid)); + // Include extra user profile fields? + $extrauserfields = null; + if ($instance->customint2) { + require_once($CFG->dirroot.'/user/profile/lib.php'); + $user = $DB->get_record('user',array('id' => $user->id)); + profile_load_custom_fields($user); + $extrauserfields = $user->profile; + } - $info = $editTeacher; - $info->coursename = $course->fullname; - email_to_user($info, $contact, get_string('mailtoteacher_suject', 'enrol_apply'), html_to_text($body), $body); + // Send notification to Teachers? Instance depending. + if ($instance->customint3 == 1) { + $context = context_course::instance($instance->courseid); + $editingteacherroles = get_archetype_roles('editingteacher'); + $editingteacherrole = reset($editingteacherroles); + $teachers = get_role_users($editingteacherrole->id, $context); + + $manageurl = new moodle_url("/enrol/apply/manage.php", array('id' => $instance->id)); + $body = $renderer->application_notification_mail_body($course, $user, $manageurl, $data->applydescription, $standarduserfields, $extrauserfields); + foreach ($teachers as $teacher) { + email_to_user($teacher, $contact, get_string('mailtoteacher_suject', 'enrol_apply'), html_to_text($body), $body); } } - } - function sendConfirmMailToManagers($instance,$info,$applydescription){ - global $DB; - global $CFG; - global $USER; - - $courseid = $instance->courseid; - - if(get_config('enrol_apply', 'sendmailtomanager') == 1){ - $course = get_course($courseid); + // Send notification to managers in system context? + if (get_config('enrol_apply', 'sendmailtomanager') == 1) { $context = context_system::instance(); - $managerType = $DB->get_record('role',array("shortname"=>"manager")); - $managers = $DB->get_records('role_assignments', array('contextid'=>$context->id,'roleid'=>$managerType->id)); - - if (!$instance->customint1) { - $info = null; - } - - $extra = null; - if($instance->customint2){ - require_once($CFG->dirroot.'/user/profile/lib.php'); - $user = $DB->get_record('user',array('id'=>$USER->id)); - profile_load_custom_fields($user); - $extra = $user->profile; - } + $managerroles = get_archetype_roles('manager'); + $managerrole = reset($editingteacherroles); + $managers = get_role_users($managerrole->id, $context); $manageurl = new moodle_url('/enrol/apply/manage.php'); - - global $PAGE; - $renderer = $PAGE->get_renderer('enrol_apply'); - $body = $renderer->application_notification_mail_body($course, $USER, $manageurl, $applydescription, $info, $extra); - - $contact = core_user::get_support_user(); - - foreach($managers as $manager){ - $userWithManagerRole = $DB->get_record('user',array('id'=>$manager->userid)); - - $info = $userWithManagerRole; - $info->coursename = $course->fullname; - email_to_user($info, $contact, get_string('mailtoteacher_suject', 'enrol_apply'), html_to_text($body), $body); + $body = $renderer->application_notification_mail_body($course, $user, $manageurl, $data->applydescription, $standarduserfields, $extrauserfields); + foreach ($managers as $manager) { + email_to_user($manager, $contact, get_string('mailtoteacher_suject', 'enrol_apply'), html_to_text($body), $body); } } } diff --git a/renderer.php b/renderer.php index 902902c..74c2e0a 100644 --- a/renderer.php +++ b/renderer.php @@ -72,40 +72,40 @@ class enrol_apply_renderer extends plugin_renderer_base { $table->out(50, true); } - public function application_notification_mail_body($course, $user, $manageurl, $applydescription, $info = null, $extra = null) { + public function application_notification_mail_body($course, $user, $manageurl, $applydescription, $standarduserfields = null, $extrauserfields = null) { $body = '

'. get_string('coursename', 'enrol_apply') .': '.format_string($course->fullname).'

'; $body .= '

'. get_string('applyuser', 'enrol_apply') .': '.$user->firstname.' '.$user->lastname.'

'; $body .= '

'. get_string('comment', 'enrol_apply') .': '.$applydescription.'

'; - if($info){ + if($standarduserfields){ $body .= '

'. get_string('user_profile', 'enrol_apply').'

'; - $body .= '

'. get_string('firstname') .': '.$info->firstname.'

'; - $body .= '

'. get_string('lastname') .': '.$info->lastname.'

'; - $body .= '

'. get_string('email') .': '.$info->email.'

'; - $body .= '

'. get_string('city') .': '.$info->city.'

'; - $body .= '

'. get_string('country') .': '.$info->country.'

'; - $body .= '

'. get_string('preferredlanguage') .': '.$info->lang.'

'; - $body .= '

'. get_string('description') .': '.$info->description_editor['text'].'

'; - - $body .= '

'. get_string('firstnamephonetic') .': '.$info->firstnamephonetic.'

'; - $body .= '

'. get_string('lastnamephonetic') .': '.$info->lastnamephonetic.'

'; - $body .= '

'. get_string('middlename') .': '.$info->middlename.'

'; - $body .= '

'. get_string('alternatename') .': '.$info->alternatename.'

'; - $body .= '

'. get_string('url') .': '.$info->url.'

'; - $body .= '

'. get_string('icqnumber') .': '.$info->icq.'

'; - $body .= '

'. get_string('skypeid') .': '.$info->skype.'

'; - $body .= '

'. get_string('aimid') .': '.$info->aim.'

'; - $body .= '

'. get_string('yahooid') .': '.$info->yahoo.'

'; - $body .= '

'. get_string('msnid') .': '.$info->msn.'

'; - $body .= '

'. get_string('idnumber') .': '.$info->idnumber.'

'; - $body .= '

'. get_string('institution') .': '.$info->institution.'

'; - $body .= '

'. get_string('department') .': '.$info->department.'

'; - $body .= '

'. get_string('phone') .': '.$info->phone1.'

'; - $body .= '

'. get_string('phone2') .': '.$info->phone2.'

'; - $body .= '

'. get_string('address') .': '.$info->address.'

'; + $body .= '

'. get_string('firstname') .': '.$standarduserfields->firstname.'

'; + $body .= '

'. get_string('lastname') .': '.$standarduserfields->lastname.'

'; + $body .= '

'. get_string('email') .': '.$standarduserfields->email.'

'; + $body .= '

'. get_string('city') .': '.$standarduserfields->city.'

'; + $body .= '

'. get_string('country') .': '.$standarduserfields->country.'

'; + $body .= '

'. get_string('preferredlanguage') .': '.$standarduserfields->lang.'

'; + $body .= '

'. get_string('description') .': '.$standarduserfields->description_editor['text'].'

'; + + $body .= '

'. get_string('firstnamephonetic') .': '.$standarduserfields->firstnamephonetic.'

'; + $body .= '

'. get_string('lastnamephonetic') .': '.$standarduserfields->lastnamephonetic.'

'; + $body .= '

'. get_string('middlename') .': '.$standarduserfields->middlename.'

'; + $body .= '

'. get_string('alternatename') .': '.$standarduserfields->alternatename.'

'; + $body .= '

'. get_string('url') .': '.$standarduserfields->url.'

'; + $body .= '

'. get_string('icqnumber') .': '.$standarduserfields->icq.'

'; + $body .= '

'. get_string('skypeid') .': '.$standarduserfields->skype.'

'; + $body .= '

'. get_string('aimid') .': '.$standarduserfields->aim.'

'; + $body .= '

'. get_string('yahooid') .': '.$standarduserfields->yahoo.'

'; + $body .= '

'. get_string('msnid') .': '.$standarduserfields->msn.'

'; + $body .= '

'. get_string('idnumber') .': '.$standarduserfields->idnumber.'

'; + $body .= '

'. get_string('institution') .': '.$standarduserfields->institution.'

'; + $body .= '

'. get_string('department') .': '.$standarduserfields->department.'

'; + $body .= '

'. get_string('phone') .': '.$standarduserfields->phone1.'

'; + $body .= '

'. get_string('phone2') .': '.$standarduserfields->phone2.'

'; + $body .= '

'. get_string('address') .': '.$standarduserfields->address.'

'; } - if($extra){ - foreach ($extra as $key => $value) { + if($extrauserfields){ + foreach ($extrauserfields as $key => $value) { $body .= '

'. $key .': '.$value.'

'; } }