From 762ed28d56b0add28810417a85ffded9300dbd5f Mon Sep 17 00:00:00 2001 From: Johannes Burk Date: Wed, 1 Jun 2016 12:34:36 +0200 Subject: [PATCH] Optimize confirm, wait and cancel enrolments functions. --- lib.php | 103 +++++++++++++++++++++++++++++++++++----------------- manage.php | 7 ++-- version.php | 2 +- 3 files changed, 74 insertions(+), 38 deletions(-) diff --git a/lib.php b/lib.php index f70d236..854875c 100644 --- a/lib.php +++ b/lib.php @@ -9,8 +9,11 @@ * ************************************************************************* * ************************************************************************ */ -class enrol_apply_plugin extends enrol_plugin { +/** User participation in course is suspended (used in user_enrolments->status) */ +define('ENROL_APPLY_USER_WAIT', 2); + +class enrol_apply_plugin extends enrol_plugin { /** * Add new instance of enrol plugin with default settings. * @param object $course @@ -196,50 +199,82 @@ class enrol_apply_plugin extends enrol_plugin { return $fields; } -} -function confirmEnrolment($enrols){ - global $DB; - global $CFG; - foreach ($enrols as $enrol){ - @$enroluser->id = $enrol; - @$enroluser->status = 0; - - if($DB->update_record('user_enrolments',$enroluser)){ - $userenrolments = $DB->get_record_sql('select * from '.$CFG->prefix.'user_enrolments where id='.$enrol); - $role = $DB->get_record_sql("select * from ".$CFG->prefix."role where archetype='student' limit 1"); - @$roleAssignments->userid = $userenrolments->userid; - @$roleAssignments->roleid = $role->id; - @$roleAssignments->contextid = 3; - @$roleAssignments->timemodified = time(); - @$roleAssignments->modifierid = 2; - $DB->insert_record('role_assignments',$roleAssignments); + function confirmEnrolment($enrols){ + global $DB; + foreach ($enrols as $enrol){ + // $userenrolment = $DB->get_record('user_enrolments', array('id' => $enrol), '*', MUST_EXIST); + $userenrolment = $DB->get_record_select( + 'user_enrolments', + 'id = :id AND (status = :enrolusersuspended OR status = :enrolapplyuserwait)', + array( + 'id' => $enrol, + 'enrolusersuspended' => ENROL_USER_SUSPENDED, + 'enrolapplyuserwait' => ENROL_APPLY_USER_WAIT), + '*', + MUST_EXIST); + + $instance = $DB->get_record('enrol', array('id' => $userenrolment->enrolid, 'enrol' => 'apply'), '*', MUST_EXIST); + + // Check privileges. + $context = context_course::instance($instance->courseid, MUST_EXIST); + if (!has_capability('enrol/apply:manageapplications', $context)) { + continue; + } + + $this->update_user_enrol($instance, $userenrolment->userid, ENROL_USER_ACTIVE); + $info = getRelatedInfo($enrol); $DB->delete_records('enrol_apply_applicationinfo', array('userenrolmentid' => $enrol)); sendConfirmMail($info); } } -} -function waitEnrolment($enrols){ - global $DB; - global $CFG; - foreach ($enrols as $enrol){ - @$enroluser->id = $enrol; - @$enroluser->status = 2; + function waitEnrolment($enrols){ + global $DB; + foreach ($enrols as $enrol){ + $userenrolment = $DB->get_record('user_enrolments', array('id' => $enrol, 'status' => ENROL_USER_SUSPENDED), '*', IGNORE_MISSING); - if($DB->update_record('user_enrolments',$enroluser)){ - $info = getRelatedInfo($enrol); - sendWaitMail($info); + if ($userenrolment != null) { + $instance = $DB->get_record('enrol', array('id' => $userenrolment->enrolid, 'enrol' => 'apply'), '*', MUST_EXIST); + + // Check privileges. + $context = context_course::instance($instance->courseid, MUST_EXIST); + if (!has_capability('enrol/apply:manageapplications', $context)) { + continue; + } + + $this->update_user_enrol($instance, $userenrolment->userid, ENROL_APPLY_USER_WAIT); + + $info = getRelatedInfo($enrol); + sendWaitMail($info); + } } } -} -function cancelEnrolment($enrols){ - global $DB; - foreach ($enrols as $enrol){ - $info = getRelatedInfo($enrol); - if($DB->delete_records('user_enrolments',array('id'=>$enrol))){ + function cancelEnrolment($enrols){ + global $DB; + foreach ($enrols as $enrol){ + $userenrolment = $DB->get_record_select( + 'user_enrolments', + 'id = :id AND (status = :enrolusersuspended OR status = :enrolapplyuserwait)', + array( + 'id' => $enrol, + 'enrolusersuspended' => ENROL_USER_SUSPENDED, + 'enrolapplyuserwait' => ENROL_APPLY_USER_WAIT), + '*', + MUST_EXIST); + + $instance = $DB->get_record('enrol', array('id' => $userenrolment->enrolid, 'enrol' => 'apply'), '*', MUST_EXIST); + + // Check privileges. + $context = context_course::instance($instance->courseid, MUST_EXIST); + if (!has_capability('enrol/apply:manageapplications', $context)) { + continue; + } + + $info = getRelatedInfo($enrol); + $this->unenrol_user($instance, $userenrolment->userid); $DB->delete_records('enrol_apply_applicationinfo', array('userenrolmentid' => $enrol)); sendCancelMail($info); } diff --git a/manage.php b/manage.php index 9c09660..e7f3413 100644 --- a/manage.php +++ b/manage.php @@ -46,12 +46,13 @@ $PAGE->set_title(get_string('confirmusers', 'enrol_apply')); $PAGE->requires->css('/enrol/apply/style.css'); if ($userenrolments != null) { + $enrolapply = enrol_get_plugin('apply'); if (optional_param('confirm', false, PARAM_BOOL)) { - confirmEnrolment($userenrolments); + $enrolapply->confirmEnrolment($userenrolments); } else if (optional_param('wait', false, PARAM_BOOL)) { - waitEnrolment ($userenrolments); + $enrolapply->waitEnrolment($userenrolments); } else if (optional_param('cancel', false, PARAM_BOOL)) { - cancelEnrolment($userenrolments); + $enrolapply->cancelEnrolment($userenrolments); } redirect($manageurl); } diff --git a/version.php b/version.php index 46fca18..4781493 100644 --- a/version.php +++ b/version.php @@ -19,7 +19,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016042202; +$plugin->version = 2016042203; $plugin->requires = 2011080100; $plugin->maturity = MATURITY_STABLE; $plugin->release = 'Enrolment upon approval plugin Version 3.0-d';