From a01b669a5f024efee8509595a920ce132fb6639a Mon Sep 17 00:00:00 2001 From: Johannes Burk Date: Tue, 12 Apr 2016 16:52:03 +0200 Subject: [PATCH] Use table_sql for manage table generation. --- lib.php | 30 ----------------- manage.php | 87 ++++++++++++++++-------------------------------- manage_table.php | 86 +++++++++++++++++++++++++++++++++++++++++++++++ style.css | 3 ++ 4 files changed, 117 insertions(+), 89 deletions(-) create mode 100644 manage_table.php create mode 100644 style.css diff --git a/lib.php b/lib.php index 7f44c01..3acf6f8 100644 --- a/lib.php +++ b/lib.php @@ -238,36 +238,6 @@ class enrol_apply_plugin extends enrol_plugin { } } -function getAllEnrolment($id = null) { - global $DB; - if ($id) { - $sql = 'SELECT ue.userid,ue.id,u.firstname,u.lastname,u.email,u.picture,c.fullname as course,ue.timecreated,ue.status - FROM {course} c - JOIN {enrol} e - ON e.courseid = c.id - JOIN {user_enrolments} ue - ON ue.enrolid = e.id - JOIN {user} u - ON ue.userid = u.id - WHERE ue.status != 0 - 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,ue.status - FROM {user_enrolments} ue - LEFT JOIN {user} u - ON ue.userid = u.id - LEFT JOIN {enrol} e - ON ue.enrolid = e.id - LEFT JOIN {course} c - ON e.courseid = c.id - WHERE ue.status != 0 - AND e.enrol = ?'; - $userenrolments = $DB->get_records_sql($sql, array('apply')); - } - return $userenrolments; -} - function confirmEnrolment($enrols){ global $DB; global $CFG; diff --git a/manage.php b/manage.php index 00a8d1a..2995525 100644 --- a/manage.php +++ b/manage.php @@ -12,6 +12,7 @@ require_once ('../../config.php'); require_once($CFG->dirroot.'/enrol/apply/lib.php'); +require_once($CFG->dirroot.'/enrol/apply/manage_table.php'); $id = optional_param('id', null, PARAM_INT); $userenrolments = optional_param_array('userenrolments', null, PARAM_INT); @@ -41,6 +42,7 @@ $PAGE->set_pagelayout('admin'); $PAGE->set_heading($pageheading); $PAGE->navbar->add(get_string('confirmusers', 'enrol_apply')); $PAGE->set_title(get_string('confirmusers', 'enrol_apply')); +$PAGE->requires->css('/enrol/apply/style.css'); if ($userenrolments != null) { $action = required_param('type', PARAM_TEXT); @@ -61,59 +63,37 @@ if ($userenrolments != null) { 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)); -} - echo $OUTPUT->header (); echo $OUTPUT->heading ( get_string ( 'confirmusers', 'enrol_apply' ) ); echo get_string('confirmusers_desc', 'enrol_apply'); + +$table = new enrol_apply_manage_table($id); +$table->define_baseurl($manageurl); +$columns = array( + 'checkboxcolumn', + 'course', + 'fullname', // Magic happens here: The column heading will automatically be set. + 'email', + 'applydate', + 'applycomment'); +$headers = array( + '', + get_string('course'), + 'fullname', // Magic happens here: The column heading will automatically be set due to column name 'fullname'. + get_string('email'), + get_string('applydate', 'enrol_apply'), + get_string('comment', 'enrol_apply')); +$table->define_columns($columns); +$table->define_headers($headers); + +$table->sortable(true, 'id'); + + 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) . '
'; + +$table->out(50, true); + echo '

'; echo ''; echo ''; @@ -125,14 +105,3 @@ 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/manage_table.php b/manage_table.php new file mode 100644 index 0000000..f0601a5 --- /dev/null +++ b/manage_table.php @@ -0,0 +1,86 @@ +. + +/** + * + * @package enrol_apply + * @copyright 2016 Johannes Burk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + */ + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->libdir . '/tablelib.php'); + +class enrol_apply_manage_table extends table_sql { + + public $is_collapsible = false; + + public function __construct($enrolid = null) { + parent::__construct('enrol_apply_manage_table'); + + global $DB; + + $sqlwhere = 'ue.status != 0'; + $sqlparams = array(); + if ($enrolid != null) { + $sqlwhere .= " AND e.id = :enrolid"; + $sqlparams['enrolid'] = $enrolid; + } else { + $sqlwhere = "e.enrol = :enrol"; + $sqlparams['enrol'] = 'apply'; + } + + $this->set_sql( + 'ue.id AS userenrolmentid, ue.userid, ue.status AS enrolstatus, ue.timecreated AS applydate, ai.comment AS applycomment, u.*, c.fullname as course', + "{user_enrolments} AS ue + LEFT JOIN {enrol_apply_applicationinfo} AS ai ON ai.userenrolmentid = ue.id + JOIN {user} AS u ON u.id = ue.userid + JOIN {enrol} AS e ON e.id = ue.enrolid + JOIN {course} AS c ON c.id = e.courseid", + $sqlwhere, + $sqlparams); + } + + /** + * Get any extra classes names to add to this row in the HTML. + * @param $row array the data for this row. Note (Johannes): this is actually an object with all sql columns. + * @return string added to the class="" attribute of the tr. + */ + function get_row_class($row) { + if ($row->enrolstatus == 2) { + return 'enrol_apply_waitinglist_highlight'; + } + return ''; + } + + public function col_checkboxcolumn($row) { + return html_writer::checkbox('userenrolments[]', $row->userenrolmentid, false); + } + + public function col_fullname($row) { + // $row contains all user fields, see sql query. + global $OUTPUT; + $col = $OUTPUT->user_picture($row, array('popup'=>true)); + $col .= fullname($row); + return $col; + } + + public function col_applydate($row) { + return date("Y-m-d", $row->applydate); + } +} diff --git a/style.css b/style.css new file mode 100644 index 0000000..05b7433 --- /dev/null +++ b/style.css @@ -0,0 +1,3 @@ +.enrol_apply_waitinglist_highlight { + border-left: 6px solid grey; +}