View source
<?php
function course_signup_admin($form, &$form_state) {
$form = array();
$form['signup_intro']['#markup'] = t("Course uses Signup's built-in functionality to allow users to register for courses and for administrators to manage attendance.");
$form['course_signup_bypass_checkout'] = array(
'#title' => t('Bypass registration and checkout for free courses.'),
'#type' => 'checkbox',
'#default_value' => variable_get('course_signup_bypass_checkout', 1),
'#description' => t("Turning this on will allow registered users to immediately begin free courses without going through any kind of course registration or checkout when Ubercart Signup is used."),
);
return system_settings_form($form);
}
function course_signup_menu() {
$items = array();
$items['admin/course/signup'] = array(
'access arguments' => array(
'administer courses',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'course_signup_admin',
),
'type' => MENU_LOCAL_TASK,
'title' => 'Signup',
);
return $items;
}
function course_signup_quick_register($node, $account, $notify = TRUE) {
if (!($signup = db_query('SELECT * FROM {signup_log} WHERE nid = :nid AND uid = :uid', array(
':nid' => $node->nid,
':uid' => $account->uid,
))
->fetch())) {
if ($node->signup_status && $notify) {
signup_sign_up_user(array(
'nid' => $node->nid,
'uid' => $account->uid,
), $notify);
}
else {
$signup = new stdClass();
$signup->nid = $node->nid;
$signup->uid = $account->uid;
$signup->signup_time = REQUEST_TIME;
$signup->form_data = array();
$signup->count_towards_limit = 1;
signup_save_signup($signup);
$signup = signup_load_signup($signup->sid);
if ($node->signup_send_confirmation) {
signup_send_confirmation_mail($signup, $node);
}
}
}
else {
$signup->count_towards_limit = 1;
signup_save_signup($signup);
}
}
function course_signup_signup_insert($signup) {
course_signup_signup_enroll($signup);
}
function course_signup_signup_update($signup) {
course_signup_signup_enroll($signup);
}
function course_signup_signup_delete($signup) {
$node = node_load($signup->nid);
$user = user_load($signup->uid);
course_unenroll($node, $user);
}
function course_signup_signup_enroll($signup) {
if ($enrollment = course_enrollment_load($signup->nid, $signup->uid)) {
if (!$enrollment->status) {
return;
}
}
if ($signup->uid && $signup->nid && $signup->count_towards_limit && empty($signup->anon_mail)) {
$node = node_load($signup->nid);
if (course_node_is_course($node)) {
$user = user_load($signup->uid);
course_enroll($node, $user, 'course_signup');
}
}
}
function course_signup_signup_cancel($signup, $node) {
$user = user_load($signup->uid);
course_unenroll($node, $user);
}
function course_signup_init() {
$node = node_load(arg(1));
global $conf;
global $user;
if (course_node_is_course($node)) {
global $conf;
$conf['signup_ignore_default_fields'] = 1;
$conf['signup_fieldset_collapsed'] = 0;
$conf['uc_signup_add_cart_text'] = t('Register');
$conf['uc_signup_signups_closed_text'] = t('Registration is closed for this activity.');
}
}
function course_signup_form_uc_signup_attendees_form_alter(&$form, &$form_state) {
global $user;
if (!empty($user->mail)) {
foreach ($form as $key => $element) {
if ($element['#type'] == 'fieldset') {
$form[$key][0]['#default_value'] = $user->mail;
$form[$key][0]['#value'] = $user->mail;
$form[$key][0]['#disabled'] = TRUE;
$form[$key][0]['#description'] = t('Your email has been pre-filled from your account.');
}
}
}
}
function course_signup_add_to_cart($nid, $qty, $data) {
global $user;
$node = node_load($nid);
if (course_node_is_course($node) && $node->signup) {
$_SESSION['uc_signup']['nids'][$nid][0] = $user->mail;
}
}
function course_signup_course_enrollment_insert($enrollment) {
$uc_signup = module_exists('uc_signup') && $from == 'ubercart' && isset($_SESSION['uc_signup']);
$uc_sid = FALSE;
if ($uc_signup) {
$uc_sid = db_query("SELECT ucsl.sid FROM {uc_signup_log} ucsl\n LEFT JOIN {signup_log} sl USING (sid)\n WHERE sl.nid = :sl.nid AND sl.uid = :sl.uid", array(
':sl.nid' => $enrollment->nid,
':sl.uid' => $enrollment->uid,
))
->fetchField();
}
if ($enrollment->enrollmenttype != 'course_signup' && !$uc_sid) {
$node = node_load($enrollment->nid);
$account = user_load($enrollment->uid);
course_signup_quick_register($node, $account);
}
}
function course_signup_course_enrollment_delete($enrollment) {
db_query("DELETE FROM {signup_log} WHERE nid = :nid and uid = :uid", array(
':nid' => $enrollment->nid,
':uid' => $enrollment->uid,
));
}
function course_signup_course_access($op, $node, $user) {
if ($op == 'enroll') {
$hooks = array();
if (module_exists('signup_restrict_by_role')) {
$check = signup_restrict_by_role_access_signup($node, $user);
$check['message'] = str_replace('signup', 'course enrollment', $check['message']);
$hooks[] = $check;
}
if (!empty($node->signup)) {
if (!empty($node->signup_status)) {
module_load_include('inc', 'signup', 'includes/date');
if (($field = signup_date_field($node->type)) && $field != 'none') {
$items = field_get_items('node', $node, $field['field_name']);
if (!empty($items[0]['value'])) {
$close_stamp = strtotime($items[0]['value'] . ' UTC');
$signup_close_early = variable_get('signup_close_early', 0);
$close_stamp -= $signup_close_early * 3600;
if (REQUEST_TIME > $close_stamp) {
$hooks['course_signup'] = array(
'header' => t(''),
'success' => FALSE,
'message' => t('Enrollments closed on @date.', array(
'@date' => format_date($close_stamp, 'long'),
)),
'weight' => 100,
);
}
}
}
}
else {
$hooks['course_signup'] = array(
'header' => t(''),
'success' => FALSE,
'message' => t('Enrollments are closed.'),
'weight' => 100,
);
}
}
return $hooks;
}
}
function course_signup_views_api() {
return array(
'api' => '3.0',
);
}
function course_signup_course_handlers() {
return array(
'object' => array(
'signup_attendance' => array(
'name' => t('Attendance'),
'class' => 'CourseObjectSignup',
'description' => t('An attendance course object.'),
),
),
);
}
function course_signup_watchdog($watchdog) {
if (preg_match('/Marked signup @signup_id attended./', $watchdog['message'])) {
$sid = reset($watchdog['variables']);
$signup = signup_load_signup($sid);
$account = user_load($signup->uid);
$complete = strpos($watchdog['message'], 'did not') === FALSE;
if ($courseObject = course_get_course_object('course_signup', 'signup_attendance', $signup->nid, $account)) {
$courseObject
->getFulfillment()
->setComplete($complete)
->save();
}
}
}
function course_signup_migrate_enrollments() {
if (db_table_exists('course_enrollment') && db_table_exists('signup_log')) {
$sql = "SELECT ce.* FROM {course_enrollment} ce\n LEFT JOIN {signup_log} sl ON (ce.nid = sl.nid AND ce.uid = sl.uid)\n where sid is null and ce.status";
$result = db_query($sql);
while ($row = $result
->fetch()) {
$row->signup_time = $row->timestamp;
drupal_write_record('signup_log', $row);
}
}
}
function course_signup_get_signup_from_enrollment($enrollment) {
$signup = db_query("SELECT * FROM {signup_log} WHERE nid = :nid AND uid = :uid", array(
':nid' => $enrollment->nid,
':uid' => $enrollment->uid,
));
return $signup
->fetch();
}
function course_signup_action_info() {
$actions = array();
$actions['course_signup_mark_attended_action'] = array(
'type' => 'course_enrollment',
'label' => t('Mark user attended'),
'configurable' => FALSE,
'vbo_configurable' => FALSE,
);
$actions['course_signup_mark_not_attended_action'] = array(
'type' => 'course_enrollment',
'label' => t('Mark user not attended'),
'configurable' => FALSE,
'vbo_configurable' => FALSE,
);
return $actions;
}
function course_signup_mark_attended_action(&$enrollment, $context) {
if ($signup = course_signup_get_signup_from_enrollment($enrollment)) {
signup_mark_attended_action($signup);
}
}
function course_signup_mark_not_attended_action(&$enrollment, $context) {
if ($signup = course_signup_get_signup_from_enrollment($enrollment)) {
signup_mark_not_attended_action($signup);
}
}
function course_signup_form_alter(&$form, &$form_state, $form_id) {
if (in_array($form_id, array(
'signup_form',
))) {
$node = node_load(arg(1));
if (course_node_is_course($node)) {
$form['#access'] = FALSE;
}
}
}
function course_signup_signup_suppress($node) {
if (course_node_is_course($node)) {
return TRUE;
}
}