Ejemplo n.º 1
0
} else {
    $context = get_context_instance(CONTEXT_COURSE, $course->id);
    // Course context
}
// Only return, if user have login
require_login($course);
// Need capability viewaparticipants.
require_capability('moodle/course:viewparticipants', $context);
$tablecolumns = array('user', 'to', 'cc', 'bcc', 'dropall');
$urlto = '<a href="#" onclick="action_all_users(\'to\');" > ' . get_string('toall', 'block_email_list') . ' <img src="' . $CFG->wwwroot . '/blocks/email_list/email/images/user_green.png" height="16" width="16" alt="' . get_string("course") . '" /> </a>';
$urlcc = '<a href="#" onclick="action_all_users(\'cc\');" >' . get_string('ccall', 'block_email_list') . ' <img src="' . $CFG->wwwroot . '/blocks/email_list/email/images/user_gray.png" height="16" width="16" alt="' . get_string("course") . '" /> </a>';
$urlbcc = '<a href="#" onclick="action_all_users(\'bcc\');" >' . get_string('bccall', 'block_email_list') . ' <img src="' . $CFG->wwwroot . '/blocks/email_list/email/images/user_suit.png" height="16" width="16" alt="' . get_string("course") . '" /> </a>';
$urlremove = '<a href="#" onclick="action_all_users(\'remove\');" >' . get_string('removeall', 'block_email_list') . ' <img src="' . $CFG->wwwroot . '/blocks/email_list/email/images/user_red.png" height="16" width="16" alt="' . get_string("course") . '" /> </a>';
$tableheaders = array(get_string('user'), $urlto, $urlcc, $urlbcc, $urlremove);
$baseurl = 'participants.php?id=' . $courseid . '&amp;roleid=' . $roleid . '&amp;group=' . $currentgroup . '&amp;perpage=' . $perpage . '&amp;search=' . $search . '&amp;fname=' . $firstinitial . '&amp;lname=' . $lastinitial;
$table = new email_flexible_table('participants');
$table->define_columns($tablecolumns);
$table->define_headers($tableheaders);
$table->define_baseurl($baseurl);
$table->set_attribute('align', 'center');
$table->setup();
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
$frontpagectx = get_context_instance(CONTEXT_COURSE, SITEID);
/// front page course is different
$rolenames = array();
$avoidroles = array();
if ($roles = get_roles_used_in_context($context, true)) {
    $canviewroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $context);
    $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext);
    if ($context->id == $frontpagectx->id) {
        //we want admins listed on frontpage too
/**
 * This function prints all mails
 *
 * @uses $CFG, $COURSE, $SESSION
 * @param int $userid User ID
 * @param string $order Order by ...
 * @param object $options Options for url
 * @param boolean $search When show mails on search
 * @param array $mailssearch Mails who has search
 * @return boolean Success/Fail
 * @todo Finish documenting this function
 **/
function email_showmails($userid, $order = '', $page = 0, $perpage = 10, $options = NULL, $search = false, $mailssearch = NULL)
{
    global $CFG, $COURSE, $SESSION;
    // CONTRIB-690
    if (!empty($_POST['perpage']) and is_numeric($_POST['perpage'])) {
        $SESSION->email_mailsperpage = $_POST['perpage'];
    } else {
        if (!isset($SESSION->email_mailsperpage) or empty($SESSION->email_mailsperpage)) {
            $SESSION->email_mailsperpage = 10;
            // Default value
        }
    }
    require_once 'tablelib.php';
    require_once 'email.class.php';
    // Get actual course
    if (!($course = get_record("course", "id", $COURSE->id))) {
        print_error('invalidcourseid', 'block_email_list');
    }
    if ($course->id == SITEID) {
        $coursecontext = get_context_instance(CONTEXT_SYSTEM);
        // SYSTEM context
    } else {
        $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
        // Course context
    }
    $url = '';
    // Build url part options
    if ($options) {
        $url = email_build_url($options);
    }
    /// Print all mails in this HTML file
    // Should use this variable so that we don't break stuff every time a variable is added or changed.
    $baseurl = $CFG->wwwroot . '/blocks/email_list/email/index.php?' . $url . '&amp;page=' . $page . '&amp;perpage=' . $perpage;
    // Print init form from send data
    echo '<form id="sendmail" action="' . $CFG->wwwroot . '/blocks/email_list/email/index.php?id=' . $course->id . '&amp;folderid=' . $options->folderid . '" method="post" target="' . $CFG->framename . '" name="sendmail">';
    if ($course->id == SITEID) {
        $tablecolumns = array('', 'icon', 'course', 'subject', 'writer', 'timecreated');
    } else {
        $tablecolumns = array('', 'icon', 'subject', 'writer', 'timecreated');
    }
    $folder = NULL;
    if (isset($options->folderid)) {
        if ($options->folderid != 0) {
            // Get folder
            $folder = email_get_folder($options->folderid);
        } else {
            // solve problem with select an x mails per page for maintein in this folder
            if (isset($options->folderoldid) && $options->folderoldid != 0) {
                $options->folderid = $options->folderoldid;
                $folder = email_get_folder($options->folderid);
            }
        }
    }
    // If actual folder is inbox type, ... change tag showing.
    if ($folder) {
        if (email_isfolder_type($folder, EMAIL_INBOX)) {
            $strto = get_string('from', 'block_email_list');
        } else {
            $strto = get_string('to', 'block_email_list');
        }
    } else {
        $strto = get_string('from', 'block_email_list');
    }
    if ($course->id == SITEID) {
        $tableheaders = array('', '', get_string('course'), get_string('subject', 'block_email_list'), $strto, get_string('date', 'block_email_list'));
    } else {
        $tableheaders = array('', '', get_string('subject', 'block_email_list'), $strto, get_string('date', 'block_email_list'));
    }
    $table = new email_flexible_table('list-mails-' . $userid);
    $table->define_columns($tablecolumns);
    $table->define_headers($tableheaders);
    $table->define_baseurl($baseurl);
    $table->set_attribute('align', 'center');
    $table->set_attribute('width', '100%');
    $table->set_attribute('class', 'emailtable');
    $table->set_control_variables(array(TABLE_VAR_SORT => 'ssort', TABLE_VAR_HIDE => 'shide', TABLE_VAR_SHOW => 'sshow', TABLE_VAR_IFIRST => 'sifirst', TABLE_VAR_ILAST => 'silast', TABLE_VAR_PAGE => 'spage'));
    $table->sortable(true, 'timecreated', SORT_DESC);
    $table->setup();
    // When no search
    if (!$search) {
        // Get mails
        $mails = email_get_mails($userid, $course->id, $table->get_sql_sort(), '', '', $options);
    } else {
        $mails = $mailssearch;
    }
    // Define long page.
    $totalcount = count($mails);
    $table->pagesize($SESSION->email_mailsperpage, $totalcount);
    $table->inputs(true);
    // Now, re-getting emails, apply pagesize (limit)
    if (!$search) {
        // Get mails
        $mails = email_get_mails($userid, $course->id, $table->get_sql_sort(), $table->get_page_start(), $table->get_page_size(), $options);
    }
    if (!$mails) {
        $mails = array();
    }
    $mailsids = email_get_ids($mails);
    // Print all rows
    foreach ($mails as $mail) {
        $attribute = array();
        $email = new eMail();
        $email->set_email($mail);
        if ($folder) {
            if (email_isfolder_type($folder, EMAIL_SENDBOX)) {
                $struser = $email->get_users_send(has_capability('moodle/site:viewfullnames', $coursecontext));
            } else {
                if (email_isfolder_type($folder, EMAIL_INBOX)) {
                    $struser = $email->get_fullname_writer(has_capability('moodle/site:viewfullnames', $coursecontext));
                    if (!$email->is_readed($userid, $mail->course)) {
                        $attribute = array('bgcolor' => $CFG->email_table_field_color);
                    }
                } else {
                    if (email_isfolder_type($folder, EMAIL_TRASH)) {
                        $struser = $email->get_fullname_writer(has_capability('moodle/site:viewfullnames', $coursecontext));
                        if (!$email->is_readed($userid, $mail->course)) {
                            $attribute = array('bgcolor' => $CFG->email_table_field_color);
                        }
                    } else {
                        if (email_isfolder_type($folder, EMAIL_DRAFT)) {
                            $struser = $email->get_users_send(has_capability('moodle/site:viewfullnames', $coursecontext));
                            if (!$email->is_readed($userid, $mail->course)) {
                                $attribute = array('bgcolor' => $CFG->email_table_field_color);
                            }
                        } else {
                            $struser = $email->get_fullname_writer(has_capability('moodle/site:viewfullnames', $coursecontext));
                            if (!$email->is_readed($userid, $mail->course)) {
                                $attribute = array('bgcolor' => $CFG->email_table_field_color);
                            }
                        }
                    }
                }
            }
        } else {
            // Format user's
            $struser = $email->get_fullname_writer(has_capability('moodle/site:viewfullnames', $coursecontext));
            if (!$email->is_readed($userid, $mail->course)) {
                $attribute = array('bgcolor' => $CFG->email_table_field_color);
            }
        }
        if (!isset($options->folderid)) {
            $options->folderid = 0;
        }
        if (email_isfolder_type($folder, EMAIL_DRAFT)) {
            $urltosent = '<a href="' . $CFG->wwwroot . '/blocks/email_list/email/sendmail.php?id=' . $mail->id . '&amp;action=' . EMAIL_EDITDRAFT . '&amp;course=' . $course->id . '">' . $mail->subject . '</a>';
        } else {
            if ($course->id == SITEID) {
                $urltosent = '<a href="' . $CFG->wwwroot . '/blocks/email_list/email/view.php?id=' . $mail->id . '&amp;action=' . EMAIL_VIEWMAIL . '&amp;course=' . $mail->course . '&amp;folderid=' . $options->folderid . '&amp;mails=' . $mailsids . '">' . $mail->subject . '</a>';
            } else {
                $urltosent = '<a href="' . $CFG->wwwroot . '/blocks/email_list/email/view.php?id=' . $mail->id . '&amp;action=' . EMAIL_VIEWMAIL . '&amp;course=' . $course->id . '&amp;folderid=' . $options->folderid . '&amp;mails=' . $mailsids . '">' . $mail->subject . '</a>';
            }
        }
        $attachment = '';
        if ($email->has_attachments()) {
            $attachment = '<img src="' . $CFG->wwwroot . '/blocks/email_list/email/images/clip.gif" alt="attachment" /> ';
        }
        // Display diferent color if mail is reply or reply all
        $extraimginfo = '';
        if ($email->is_answered($userid, $course->id)) {
            // Color td
            unset($attribute);
            $attribute = array('bgcolor' => $CFG->email_answered_color);
            // Adding info img
            $extraimginfo = '<img src="' . $CFG->wwwroot . '/blocks/email_list/email/images/answered.gif" alt="" /> ';
        }
        if (!($course_mail = get_record("course", "id", $mail->course))) {
            print_error('invalidcourseid', 'block_email_list');
        }
        if ($course->id == SITEID) {
            $table->add_data(array('<input id="mail" type="checkbox" name="mailid[]" value="' . $mail->id . '" />', $course_mail->fullname, $attachment . $extraimginfo, $urltosent, $struser, userdate($mail->timecreated)), $attribute);
        } else {
            $table->add_data(array('<input id="mail" type="checkbox" name="mailid[]" value="' . $mail->id . '" />', $attachment . $extraimginfo, $urltosent, $struser, userdate($mail->timecreated)), $attribute);
        }
        // Save previous mail
        $previousmail = $mail->id;
    }
    $table->print_html();
    // Print select action, if have mails
    if ($mails) {
        email_print_select_options($options, $SESSION->email_mailsperpage);
    }
    // End form
    echo '</form>';
    return true;
}