/**
 * Lookup list of instances associated with this user
 * @param username value of username in moodle, currently this is ccid
 *
 * @return array list of instance records associated with user. array( stdClass, ...) stdClass->id, stdClass->url, stdClass->token, stdClass->sourceid
 */
function eclass_getUserInstances($username)
{
    global $DB, $CFG;
    //using default cache name
    $eCache = new EclassCache();
    $data = $eCache->getData(INSTANCES_DATAKEY);
    if ($data != ECLASS_CACHE_EXPIRED) {
        return $data;
    }
    //otherwise do a query
    $dbman = $DB->get_manager();
    //check if our tables exist
    if (!$dbman->table_exists("eclass_instances") || !$dbman->table_exists("eclass_user_instances")) {
        return array();
    }
    $records = $DB->get_records_sql("select i.* from {eclass_instances} AS i JOIN {eclass_user_instances} AS ui ON (i.id = ui.instance_id) where ui.userid = :userid", array('userid' => $username));
    $eCache->setData(INSTANCES_DATAKEY, 5, $records);
    $instances = Instance::wrapRecords($records);
    debugging("User Instances Found: " . print_r($records, true), DEBUG_DEVELOPER);
    return $instances;
}
示例#2
0
/**
 * Get remote calendar events
 * @param int $tstart Start time of time range for events
 * @param int $tend   End time of time range for events
 * @param array/int/boolean $users array of users, user id or boolean for all/no user events
 * @param array/int/boolean $groups array of groups, group id or boolean for all/no group events
 * @param array/int/boolean $courses array of courses, course id or boolean for all/no course events
 *
 * @return array of selected remote events + local events or return the events passed to this function (local)
 */
function get_remote_events($tstart, $tend, $users, $event_index)
{
    global $DB, $USER, $CFG, $SESSION;
    $events = array();
    if (is_string($users) || is_int($users)) {
        // get instances
        require_once $CFG->dirroot . '/local/eclass/lib/session_storage.php';
        $instances = eclass_getUserInstances($USER->username);
        foreach ($instances as $instance) {
            $serverurl = $instance->url . "/webservice/xmlrpc/server.php" . '?wstoken=' . $instance->token;
            require_once 'Zend/XmlRpc/Client.php';
            $xmlrpcclient = new Zend_XmlRpc_Client($serverurl);
            //make the web service call
            $function = 'eclass_get_events';
            $params = array('userid' => $USER->username, 'start' => $tstart, 'end' => $tend, 'cal_global' => (int) $SESSION->cal_show_global, 'cal_groups' => (int) $SESSION->cal_show_groups, 'cal_course' => (int) $SESSION->cal_show_course, 'cal_user' => (int) $SESSION->cal_show_user);
            try {
                $remote_events = $xmlrpcclient->call($function, $params);
                foreach ($remote_events as $anevent) {
                    $anevent = (object) $anevent;
                    $events[$event_index] = $anevent;
                    $events[$event_index]->id = $event_index;
                    $event_index++;
                }
            } catch (Exception $e) {
                var_dump($e);
            }
        }
    }
    if (!empty($events)) {
        $event_cache = new StdClass();
        $event_cache->events = $events;
        $event_cache->s_time = $tstart;
        $event_cache->e_time = $tend;
        $eCache = new EclassCache();
        $eCache->setData("events" . $SESSION->cal_show_global . $SESSION->cal_show_groups . $SESSION->cal_show_course . $SESSION->cal_show_user, 5, $event_cache);
    }
    return $events;
}