From 9bf08acb8e1f686291086e4d965d8352392c67c1 Mon Sep 17 00:00:00 2001 From: Johannes Burk Date: Tue, 12 Apr 2016 12:10:43 +0200 Subject: [PATCH] Refactor: combine manage.php and apply.php to manage.php, rename locallib.php to apply_form.php and containing class from enrol_apply_enrol_form to enrol_apply_apply_form refactor code in manage.php --- apply.php | 112 -------------------------- locallib.php => apply_form.php | 2 +- lib.php | 34 ++++---- manage.php | 138 +++++++++++++++++++++++++-------- selectlib.php | 2 +- unenroluser.php | 1 - 6 files changed, 126 insertions(+), 163 deletions(-) delete mode 100644 apply.php rename locallib.php => apply_form.php (99%) diff --git a/apply.php b/apply.php deleted file mode 100644 index ba13992..0000000 --- a/apply.php +++ /dev/null @@ -1,112 +0,0 @@ -dirroot.'/enrol/renderer.php'); -require_once($CFG->dirroot.'/enrol/locallib.php'); -require_once($CFG->dirroot.'/lib/outputcomponents.php'); -require_once ('lib.php'); - -$site = get_site (); -$systemcontext = context_system::instance(); - -$id = required_param ( 'id', PARAM_INT ); // course id -$course = $DB->get_record ( 'course', array ('id' => $id ), '*', MUST_EXIST ); -$context = context_course::instance($course->id, MUST_EXIST); - -$enrolid = optional_param('enrolid', 0, PARAM_INT); - -require_login ( $course ); -require_capability ( 'moodle/course:enrolreview', $context ); - -$PAGE->set_url ( '/enrol/apply.php', array ('id' => $course->id ) ); -//$PAGE->set_context($systemcontext); -$PAGE->set_pagelayout ( 'admin' ); -$PAGE->set_heading ( $course->fullname ); - -$PAGE->navbar->add ( get_string ( 'confirmusers', 'enrol_apply' ) ); -$PAGE->set_title ( "$site->shortname: " . get_string ( 'confirmusers', 'enrol_apply' ) ); - -$userenrolments = optional_param_array('userenrolments', null, PARAM_INT); -if ($userenrolments != null) { - if ($_POST ['type'] == 'confirm') { - confirmEnrolment($userenrolments); - } elseif ($_POST ['type'] == 'wait') { - waitEnrolment ($userenrolments); - } elseif ($_POST ['type'] == 'cancel') { - cancelEnrolment($userenrolments); - } - redirect ( "$CFG->wwwroot/enrol/apply/apply.php?id=" . $id . "&enrolid=" . $enrolid ); -} - -$enrols = getAllEnrolment ($enrolid); -$applicationinfo = $DB->get_records_sql(' - SELECT userenrolmentid, comment - FROM {enrol_apply_applicationinfo} - WHERE userenrolmentid IN ( - SELECT id - FROM {user_enrolments} - WHERE enrolid = ?)', array($enrolid)); - -echo $OUTPUT->header (); -echo $OUTPUT->heading ( get_string ( 'confirmusers', 'enrol_apply' ) ); -echo get_string('confirmusers_desc', 'enrol_apply'); -echo '
'; -echo ''; -echo ''; -echo ''; -echo ''; -echo ''; -echo ''; -echo ''; -echo ''; -echo ''; -echo ''; -foreach ( $enrols as $enrol ) { - $picture = get_user_picture($enrol->userid); - if ($enrol->status == 2) { - echo ''; - } else { - echo ''; - } - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; -} -echo '
 ' . get_string ( 'coursename', 'enrol_apply' ) . ' ' . get_string ( 'applyuser', 'enrol_apply' ) . '' . get_string ( 'applyusermail', 'enrol_apply' ) . '' . get_string ( 'applydate', 'enrol_apply' ) . '' . get_string ( 'comment', 'enrol_apply' ) . '
' . format_string($enrol->course) . '' . $OUTPUT->render($picture) . ''.$enrol->firstname . ' ' . $enrol->lastname.'' . $enrol->email . '' . date ( "Y-m-d", $enrol->timecreated ) . '' . htmlspecialchars($applicationinfo[$enrol->id]->comment) . '
'; -echo '

'; -echo ''; -echo ''; -echo ''; -echo '

'; -echo '
'; -echo ''; -echo $OUTPUT->footer (); - - -function get_user_picture($userid){ - global $DB; - - $extrafields[] = 'lastaccess'; - $ufields = user_picture::fields('u', $extrafields); - $sql = "SELECT DISTINCT $ufields FROM {user} u where u.id=$userid"; - - $user = $DB->get_record_sql($sql); - return new user_picture($user); -} \ No newline at end of file diff --git a/locallib.php b/apply_form.php similarity index 99% rename from locallib.php rename to apply_form.php index 4ee596a..228b7a9 100644 --- a/locallib.php +++ b/apply_form.php @@ -23,7 +23,7 @@ defined('MOODLE_INTERNAL') || die(); require_once("$CFG->libdir/formslib.php"); -class enrol_apply_enrol_form extends moodleform { +class enrol_apply_apply_form extends moodleform { protected $instance; /** diff --git a/lib.php b/lib.php index aa27236..7f44c01 100644 --- a/lib.php +++ b/lib.php @@ -22,17 +22,22 @@ class enrol_apply_plugin extends enrol_plugin { } public function allow_unenrol(stdClass $instance) { - // users with unenrol cap may unenrol other users manually manually + // Users with unenrol cap may unenrol other users manually. return true; } + /** + * Returns link to page which may be used to add new instance of enrolment plugin in course. + * Multiple instances supported. + * @param int $courseid + * @return moodle_url page url + */ public function get_newinstance_link($courseid) { $context = context_course::instance($courseid, MUST_EXIST); if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/apply:config', $context)) { return NULL; } - // multiple instances supported - different roles with different password return new moodle_url('/enrol/apply/edit.php', array('courseid'=>$courseid)); } @@ -44,18 +49,16 @@ class enrol_apply_plugin extends enrol_plugin { return null; } if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id))) { - //TODO: maybe we should tell them they are already enrolled, but can not access the course - //return null; return $OUTPUT->notification(get_string('notification', 'enrol_apply'), 'notifysuccess'); } - require_once("$CFG->dirroot/enrol/apply/locallib.php"); + require_once("$CFG->dirroot/enrol/apply/apply_form.php"); - $form = new enrol_apply_enrol_form(NULL, $instance); + $form = new enrol_apply_apply_form(NULL, $instance); - $instanceid = optional_param('instance', 0, PARAM_INT); - if ($instance->id == $instanceid) { - if ($data = $form->get_data()) { + 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); @@ -124,13 +127,10 @@ class enrol_apply_plugin extends enrol_plugin { redirect("$CFG->wwwroot/course/view.php?id=$instance->courseid"); } } - //exit; - ob_start(); - $form->display(); - $output = ob_get_clean(); - return $OUTPUT->box($output); + $output = $form->render(); + return $OUTPUT->box($output); } public function get_action_icons(stdClass $instance) { @@ -149,7 +149,7 @@ class enrol_apply_plugin extends enrol_plugin { } if (has_capability('enrol/apply:manage', $context)) { - $managelink = new moodle_url("/enrol/apply/apply.php", array('id'=>$_GET['id'],'enrolid'=>$instance->id)); + $managelink = new moodle_url("/enrol/apply/manage.php", array('id'=>$instance->id)); $icons[] = $OUTPUT->action_icon($managelink, new pix_icon('i/users', get_string('confirmenrol', 'enrol_apply'), 'core', array('class'=>'iconsmall'))); } @@ -253,7 +253,7 @@ function getAllEnrolment($id = null) { AND e.id = ?'; $userenrolments = $DB->get_records_sql($sql, array($id)); } else { - $sql = 'SELECT ue.id,ue.userid,u.firstname,u.lastname,u.email,u.picture,c.fullname as course,ue.timecreated + $sql = 'SELECT ue.id,ue.userid,u.firstname,u.lastname,u.email,u.picture,c.fullname as course,ue.timecreated,ue.status FROM {user_enrolments} ue LEFT JOIN {user} u ON ue.userid = u.id @@ -425,7 +425,7 @@ function sendConfirmMailToTeachers($instance,$info,$applydescription){ } } - $body .= '

'. html_writer::link(new moodle_url("/enrol/apply/apply.php", array('id'=>$courseid,'enrolid'=>$instanceid)), get_string('applymanage', 'enrol_apply')).'

'; + $body .= '

'. html_writer::link(new moodle_url("/enrol/apply/manage.php", array('id'=>$instanceid)), get_string('applymanage', 'enrol_apply')).'

'; $contact = core_user::get_support_user(); $info = $editTeacher; $info->coursename = $course->fullname; diff --git a/manage.php b/manage.php index 5da4902..00a8d1a 100644 --- a/manage.php +++ b/manage.php @@ -9,54 +9,130 @@ * ************************************************************************* * ************************************************************************ */ -require ('../../config.php'); -require_once ('lib.php'); + +require_once ('../../config.php'); +require_once($CFG->dirroot.'/enrol/apply/lib.php'); + +$id = optional_param('id', null, PARAM_INT); +$userenrolments = optional_param_array('userenrolments', null, PARAM_INT); + require_login(); -require_capability('enrol/apply:manage', context_system::instance()); - -$site = get_site (); -$systemcontext = context_system::instance(); - -$PAGE->set_url ( '/enrol/manage.php'); -$PAGE->set_context($systemcontext); -$PAGE->set_pagelayout ( 'admin' ); -//$PAGE->set_heading ( $course->fullname ); - -$PAGE->navbar->add ( get_string ( 'confirmusers', 'enrol_apply' ) ); -$PAGE->set_title ( "$site->shortname: " . get_string ( 'confirmusers', 'enrol_apply' ) ); - -if (isset ( $_POST ['enrolid'] )) { - if ($_POST ['enrolid']) { - if ($_POST ['type'] == 'confirm') { - confirmEnrolment ( $_POST ['enrolid'] ); - } elseif ($_POST ['type'] == 'cancel') { - cancelEnrolment ( $_POST ['enrolid'] ); - } - redirect ( "$CFG->wwwroot/enrol/apply/manage.php" ); + +$manageurlparams = array(); +if ($id == null) { + $context = context_system::instance(); + require_capability('enrol/apply:manage', $context); + $pageheading = get_string('confirmusers', 'enrol_apply'); +} else { + $instance = $DB->get_record('enrol', array('id'=>$id, 'enrol'=>'apply'), '*', MUST_EXIST); + require_course_login($instance->courseid); + $course = get_course($instance->courseid); + $context = context_course::instance($course->id, MUST_EXIST); + require_capability('moodle/course:enrolreview', $context); + $manageurlparams['id'] = $instance->id; + $pageheading = $course->fullname; +} + +$manageurl = new moodle_url('/enrol/apply/manage.php', $manageurlparams); + +$PAGE->set_context($context); +$PAGE->set_url($manageurl); +$PAGE->set_pagelayout('admin'); +$PAGE->set_heading($pageheading); +$PAGE->navbar->add(get_string('confirmusers', 'enrol_apply')); +$PAGE->set_title(get_string('confirmusers', 'enrol_apply')); + +if ($userenrolments != null) { + $action = required_param('type', PARAM_TEXT); + switch ($action) { + case 'confirm': + confirmEnrolment($userenrolments); + break; + case 'wait': + waitEnrolment ($userenrolments); + break; + case 'cancel': + cancelEnrolment($userenrolments); + break; + default: + # code... + break; } + redirect($manageurl); +} + +$enrols = getAllEnrolment($id); +if ($id == null) { + $applicationinfo = $DB->get_records_sql(' + SELECT userenrolmentid, comment + FROM {enrol_apply_applicationinfo} + WHERE userenrolmentid IN ( + SELECT id + FROM {user_enrolments} + WHERE enrolid IN ( + SELECT id + FROM {enrol} + WHERE enrol = ?))', array('apply')); +} else { + $applicationinfo = $DB->get_records_sql(' + SELECT userenrolmentid, comment + FROM {enrol_apply_applicationinfo} + WHERE userenrolmentid IN ( + SELECT id + FROM {user_enrolments} + WHERE enrolid = ?)', array($instance->id)); } -$enrols = getAllEnrolment(); echo $OUTPUT->header (); echo $OUTPUT->heading ( get_string ( 'confirmusers', 'enrol_apply' ) ); -echo '
'; +echo get_string('confirmusers_desc', 'enrol_apply'); +echo ''; echo ''; echo ''; echo ''; echo ''; +echo ''; echo ''; echo ''; echo ''; +echo ''; echo ''; foreach ( $enrols as $enrol ) { - echo ''; + $picture = get_user_picture($enrol->userid); + if ($enrol->status == 2) { + echo ''; + } else { + echo ''; + } + echo ''; echo ''; - echo ''; + echo ''; + echo ''; echo ''; - echo ''; + echo ''; + echo ''; + echo ''; } echo '
 ' . get_string ( 'coursename', 'enrol_apply' ) . ' ' . get_string ( 'applyuser', 'enrol_apply' ) . '' . get_string ( 'applyusermail', 'enrol_apply' ) . '' . get_string ( 'applydate', 'enrol_apply' ) . '' . get_string ( 'comment', 'enrol_apply' ) . '
' . format_string($enrol->course) . '' . $enrol->firstname . ' ' . $enrol->lastname . '' . $OUTPUT->render($picture) . ''.$enrol->firstname . ' ' . $enrol->lastname.'' . $enrol->email . '' . date ( "Y-m-d", $enrol->timecreated ) . '
' . date ( "Y-m-d", $enrol->timecreated ) . '' . htmlspecialchars($applicationinfo[$enrol->id]->comment) . '
'; -echo '

  

'; +echo '

'; +echo ''; +echo ''; +echo ''; +echo '

'; echo '
'; -echo ''; -echo $OUTPUT->footer (); \ No newline at end of file +echo ''; +echo $OUTPUT->footer (); + +function get_user_picture($userid){ + global $DB; + + $extrafields[] = 'lastaccess'; + $ufields = user_picture::fields('u', $extrafields); + $sql = "SELECT DISTINCT $ufields FROM {user} u where u.id=$userid"; + + $user = $DB->get_record_sql($sql); + return new user_picture($user); +} \ No newline at end of file diff --git a/selectlib.php b/selectlib.php index f49c143..f162cab 100644 --- a/selectlib.php +++ b/selectlib.php @@ -149,7 +149,7 @@ class enrol_apply_current_participant extends user_selector_base { protected function get_options() { $options = parent::get_options(); $options['enrolid'] = $this->enrolid; - $options['file'] = 'enrol/apply/locallib.php'; + $options['file'] = 'enrol/apply/apply_form.php'; return $options; } } diff --git a/unenroluser.php b/unenroluser.php index dd2f7f1..f2b1912 100644 --- a/unenroluser.php +++ b/unenroluser.php @@ -19,7 +19,6 @@ */ require('../../config.php'); -require_once("$CFG->dirroot/enrol/locallib.php"); require_once("$CFG->dirroot/enrol/renderer.php"); $ueid = required_param('ue', PARAM_INT); // user enrolment id