示例#1
0
function tao_message_target_get($target, $course)
{
    $all = tao_message_targets();
    $targetobj = '';
    if ($course->id == SITEID) {
        $targetobj = $all['site'][$target];
    } else {
        $targetobj = $all['lp'][$target];
    }
    $targetobj = (object) $targetobj;
    $targetobj->key = $target;
    return $targetobj;
}
示例#2
0
/**
 * hook for the messagebyroletab.
 * not correctly namespaced because of limitations in messaging.
 *
 * unfortunately we don't seem to be able to pass
 * parameters from the request here...
*/
function message_print_byrole()
{
    global $CFG, $USER;
    require_once $CFG->dirroot . '/local/lib/messagelib.php';
    $target = optional_param('target', 0, PARAM_INT);
    $course = optional_param('lp', 0, PARAM_INT);
    $page = optional_param('page', 0, PARAM_INT);
    $perpage = optional_param('perpage', 10, PARAM_INT);
    $sitecontext = get_context_instance(CONTEXT_COURSE, SITEID);
    $cansearch = has_capability('moodle/local:cansearchforlptomessage', $sitecontext);
    $searchform = null;
    $messageform = null;
    $courses = array();
    $totalcount = 0;
    if (!empty($course) || !empty($target)) {
        $c = get_record('course', 'id', $course);
        $targetobject = (object) tao_message_target_get($target, $c);
        if ($count = tao_message_count_recipients_by_target($targetobject, $c)) {
            $targetobject->key = $target;
            require_capability('moodle/local:' . $targetobject->capability, get_context_instance(CONTEXT_COURSE, $c->id));
            // give the message send form
            require_once $CFG->dirroot . '/local/forms.php';
            $messageform = new tao_message_send_form('', array('course' => $c, 'target' => $targetobject));
            if ($data = $messageform->get_data()) {
                // send message
                $eventdata = array('body' => $data->body, 'from' => $USER->id, 'format' => $data->format, 'course' => $c, 'target' => $targetobject);
                events_trigger('tao_message_role', $eventdata);
                echo get_string('messagequeued', 'local');
                print_continue($CFG->wwwroot . '/message/index.php?tab=byrole');
                return;
            } else {
                if (!$messageform->is_cancelled()) {
                    $messageform->display();
                    return;
                }
            }
        } else {
            notify(get_string('messagenorecipients', 'local'));
        }
    }
    if ($cansearch) {
        // set up the search form object and process any search requests
        require_capability('moodle/local:cansearchforlptomessage', $sitecontext);
        require_once $CFG->dirroot . '/local/forms.php';
        $searchform = new tao_message_lpsearch_form('', array(), 'get');
        if ($data = $searchform->get_data()) {
            $search = trim(strip_tags($data->search));
            // trim & clean raw searched string
            if ($search) {
                $searchterms = explode(" ", $search);
                // Search for words independently
                foreach ($searchterms as $key => $searchterm) {
                    if (strlen($searchterm) < 1) {
                        unset($searchterms[$key]);
                    }
                }
                $search = trim(implode(" ", $searchterms));
            }
            if (count($searchterms) > 0) {
                $courses = get_courses_search($searchterms, "fullname ASC", $page, $perpage, $totalcount);
            }
            if (empty($courses)) {
                $nosearchresults = true;
            }
        }
    }
    // print the main part of the page
    $targets = (object) tao_message_targets();
    // SITE wide message groups first
    $sitecontent = '';
    foreach ($targets->site as $key => $target) {
        $target = (object) $target;
        $target->key = $key;
        $sitecontent .= tao_print_target($target);
    }
    $lpcontent = '';
    if (empty($courses)) {
        // if we haven't come from a search, get all courses they have a direct relationship with
        if (has_capability('moodle/local:hasdirectlprelationship', $sitecontext)) {
            // Non-cached - get accessinfo
            if (isset($USER->access)) {
                $accessinfo = $USER->access;
            } else {
                $accessinfo = get_user_access_sitewide($USER->id);
            }
            $courses = get_user_courses_bycap($USER->id, 'moodle/local:hasdirectlprelationship', $accessinfo, false, 'c.fullname', array('fullname'));
        }
    }
    if ($courses) {
        // either from a search, or from the 'direct' relationships
        foreach ($courses as $course) {
            // print the targets for each course
            $coursecontent = '';
            foreach ($targets->lp as $key => $target) {
                $target = (object) $target;
                $target->key = $key;
                $coursecontent .= tao_print_target($target, $course, !empty($search));
            }
            if (!empty($coursecontent)) {
                $lpcontent .= '<b>' . $course->fullname . '</b><br />' . $coursecontent . '<br />';
            }
        }
        if (!empty($searchform) && !empty($search)) {
            $url = $searchform->get_fake_url($CFG->wwwroot . '/message/index.php', array('search' => urlencode(stripslashes($search)), 'perpage' => $perpage));
            $lpcontent .= print_paging_bar($totalcount, $page, $perpage, $url, 'page', $perpage == 99999, true);
        }
    }
    if (empty($sitecontent) && empty($lpcontent) && empty($cansearch)) {
        print_error('nomessagetargets', 'local');
    }
    if (!empty($sitecontent)) {
        print_heading(get_string('sitelists', 'local'));
        echo $sitecontent . '<br /><br />';
    }
    if (!empty($lpcontent)) {
        print_heading(get_string('lplists', 'local'));
        echo $lpcontent;
        $lpprinted = true;
    }
    if (!empty($searchform)) {
        if (empty($lpprinted)) {
            print_heading(get_string('lplists', 'local'));
        } else {
            echo '<br /><br />';
        }
        if (!empty($nosearchresults)) {
            notify(get_string('noresults'));
        }
        $searchform->display();
    }
}