/**
 * Notify modules course credit was awarded.
 *
 * @param array $record
 *   The course credit awarded record, including 'ccaid' Course credit awarded
 *   ID from drupal_write_record().
 *
 * @see course_credit_award_save()
 */
function hook_course_credit_awarded_insert($record)
{
    // Example: send users an email when they are awarded credit.
    // TODO Convert "user_load" to "user_load_multiple" if "$record['uid']" is other than a uid.
    // To return a single user object, wrap "user_load_multiple" with "array_shift" or equivalent.
    // Example: array_shift(user_load_multiple(array(), $record['uid']))
    $params['account'] = $account = user_load($record['uid']);
    $params['record'] = $record;
    $params['subject'] = t("You've got new credit");
    drupal_mail('my_module', 'credit_awarded', $account->mail, user_preferred_language($account), $params);
}
function os_poker_report_abuse_form_submit($form, &$form_state)
{
    $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
    if ($op == t('Send')) {
        $to = variable_get('os_poker_abuse_mail_to', 1);
        if (is_numeric($to)) {
            $to = user_load($to)->mail;
        }
        $account = $form['#reporter'];
        $from = ($account->profile_nickname ? $account->profile_nickname : $account->name) . '<' . $account->mail . '>';
        $params = array('reason' => $form['reason']['#options'][reset(array_filter($form_state['values']['reason']))], 'details' => $form_state['values']['details'], 'reporter' => $form['#reporter'], 'reported' => $form['#reported']);
        drupal_mail('os_poker', 'abuse', $to, user_preferred_language($account), $params, $from);
        drupal_set_message(t('Your message has been sent.'));
    }
}
/**
 * It delivers all the notification messages and empties the queue
 *
 * @param $send
 *   If FALSE, the messages won't be sent nor removed from the queue
 *
 * @return
 *   Message sent or to be sent, HTML formatted
 */
function guifi_notify_send($send = TRUE)
{
    global $user;
    $destinations = array();
    $messages = array();
    // Get all the queue to be processesed, grouping to every single destination
    $qt = db_query("\n    SELECT *\n    FROM {guifi_notify}");
    while ($message = db_fetch_array($qt)) {
        $messages[$message['id']] = $message;
        foreach (unserialize($message['to_array']) as $dest) {
            $destinations[$dest][] = $message['id'];
        }
    }
    // For every destination, construct a single mail with all messages
    $errors = FALSE;
    $output = '';
    foreach ($destinations as $to => $msgs) {
        $body = str_repeat('-', 72) . "\n\n" . t('Complete trace messages (for trace purposes, to be used by developers)') . "\n" . str_repeat('-', 72) . "\n";
        $subjects = t('Summary of changes:') . "\n" . str_repeat('-', 72) . "\n";
        foreach ($msgs as $msg_id) {
            $subjects .= format_date($messages[$msg_id]['timestamp'], 'small') . ' ** ' . $messages[$msg_id]['who_name'] . ' ' . $messages[$msg_id]['subject'] . "\n";
            $body .= format_date($messages[$msg_id]['timestamp'], 'small') . ' ** ' . $messages[$msg_id]['who_name'] . ' ' . $messages[$msg_id]['subject'] . "\n" . $messages[$msg_id]['body'] . "\n" . str_repeat('-', 72) . "\n";
        }
        $subject = t('[guifi.net notify] Report of changes at !date', array('!date' => format_date(time(), 'small')));
        $output .= '<h2>' . t('Sending a mail to: %to', array('%to' => $to)) . '</h2>';
        $output .= '<h3>' . $subject . '</h3>';
        $output .= '<pre><small>' . $subjects . $body . '</small></pre>';
        $params['mail']['subject'] = $subject;
        $params['mail']['body'] = $subjects . $body;
        $return = FALSE;
        if ($send) {
            $return = drupal_mail('guifi_notify', 'notify', $to, user_preferred_language($user), $params, variable_get('guifi_contact', $user->mail));
            guifi_log(GUIFILOG_TRACE, 'return code for email sent:', $return);
        }
        if ($return['result']) {
            watchdog('guifi', 'Report of changes sent to %name', array('%name' => $to));
        } else {
            watchdog('guifi', 'Unable to notify %name', array('%name' => $to), WATCHDOG_ERROR);
            $errors = TRUE;
        }
    }
    // delete messages
    if (!$errors and $send) {
        db_query("DELETE FROM {guifi_notify}\n       WHERE id in (" . implode(',', array_keys($messages)) . ")");
    }
    return $output;
}
function guifi_user_reset_password($edit)
{
    global $user;
    if (is_numeric($edit)) {
        $edit = guifi_user_load($edit);
    } else {
        $edit = guifi_user_load($edit['id']);
    }
    if (empty($edit['notification'])) {
        form_set_error('notification', t('Don\'t know where to email a new password. ' . 'You need to have an email properly filled to get a new password. ' . 'You should contact network administrators ' . 'for getting a new password.'));
        return;
    }
    $edit['pass'] = user_password();
    $params['account'] = $user;
    $params['username'] = $edit['username'];
    $params['pass'] = $edit['pass'];
    $mail_success = drupal_mail('guifi_user_password', 'reset', $edit['notification'], user_preferred_language($user), $params);
    if ($mail_success) {
        watchdog('user', 'Password mailed to %name for %email.', array('%name' => $edit['notification'], '%email' => $edit['username']));
        drupal_set_message(t('Your password and further instructions ' . 'have been sent to your e-mail address.'));
        $edit['password'] = crypt($edit['pass']);
        guifi_user_save($edit);
    } else {
        watchdog('user', 'Error mailing password to %name at %email.', array('%name' => $edit['username'], '%email' => $edit['notification']), WATCHDOG_ERROR);
        drupal_set_message(t('Unable to send mail to %email. ' . 'Please contact the site admin.', array('%email' => $edit['notification'])));
    }
    drupal_goto('node/' . $edit['nid'] . '/view/users');
}
function os_poker_first_profile_form_submit($form, &$form_state)
{
    require_once drupal_get_path('module', 'os_poker') . "/scheduler.class.php";
    $cuser = CUserManager::instance()->CurrentUser(TRUE);
    $edit =& $form_state['values'];
    $profileComplete = TRUE;
    if (empty($edit["profile_nickname"])) {
        $cuser->profile_nickname = _os_poker_rand_player();
        $profileComplete &= FALSE;
    } else {
        $cuser->profile_nickname = $edit["profile_nickname"];
    }
    $cuser->name = $cuser->profile_nickname;
    if (variable_get('user_email_verification', TRUE)) {
        $raw_password = $cuser->pass2;
        $cuser->pass = $raw_password;
        $cuser->pass2 = NULL;
    }
    if (!empty($edit["profile_dob"])) {
        $cuser->profile_dob = $edit["profile_dob"];
    } else {
        $profileComplete &= FALSE;
    }
    if (!empty($edit["profile_gender"])) {
        $cuser->profile_gender = $edit["profile_gender"];
    } else {
        $profileComplete &= FALSE;
    }
    if (!empty($edit["profile_city"])) {
        $cuser->profile_city = $edit["profile_city"];
    } else {
        $profileComplete &= FALSE;
    }
    if (!empty($edit["profile_country"])) {
        $cuser->profile_country = $edit["profile_country"];
    } else {
        $profileComplete &= FALSE;
    }
    if (!empty($edit["picture"])) {
        $cuser->picture = $edit["picture"];
    } else {
        $profileComplete &= FALSE;
    }
    //Check Profile complete
    if ($profileComplete && $cuser->CompleteProfile() == FALSE) {
        $cuser->AddChips(2000);
        $cuser->SetProfileComplete();
        if (isset($edit["first_profile"])) {
            CScheduler::instance()->RegisterTask(new CDelayMessage(), $cuser->uid, 'login', "-1 Day", array("type" => "os_poker_jump", "body" => array("lightbox" => TRUE, "url" => url("poker/buddies/invite", array("query" => array("height" => 442, "width" => 603), "absolute" => TRUE)))));
        }
    } else {
        if (isset($edit["first_profile"])) {
            CScheduler::instance()->RegisterTask(new CDelayMessage(), $cuser->uid, 'login', "-1 Day", array("type" => "os_poker_jump", "body" => array("lightbox" => TRUE, "url" => url("poker/profile/update", array("query" => array("height" => 442, "width" => 603), "absolute" => TRUE)))));
        }
    }
    $cuser->Save();
    //Trigger the invitation bonus
    CScheduler::instance()->Trigger('first_login');
    CScheduler::instance()->RegisterTask(new CDailyChips(), $cuser->uid, array('login', "live"), "+1 Day 00:00");
    //Send mail
    $account = $cuser->DrupalUser();
    if (variable_get('user_email_verification', TRUE) && !isset($account->fbu)) {
        drupal_mail('os_poker', 'profile', $account->mail, user_preferred_language($account), array('account' => $account, 'raw_password' => $raw_password));
    }
}