示例#1
0
文件: query.php 项目: danorama/orsee
function query__apply_permanent_queries()
{
    global $settings;
    $continue = true;
    $target = '';
    $num_queries = 0;
    $num_p = 0;
    $num_assigned = 0;
    if ($continue) {
        if ($settings['allow_permanent_queries'] != 'y') {
            $continue = false;
        }
    }
    if ($continue) {
        $ppart = array();
        $ppart = participant__get_permanent_query_participants();
        if (count($ppart) == 0) {
            $continue = false;
        }
    }
    if ($continue) {
        $pqu = array();
        $pqu = query__get_permanent();
        if (count($pqu) == 0) {
            $continue = false;
        } else {
            $num_queries = count($pqu);
        }
    }
    if ($continue) {
        $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
        $query_assigned = array();
        foreach ($ppart as $p) {
            $num_p++;
            foreach ($pqu as $q) {
                $experiment = orsee_db_load_array("experiments", $q['experiment_id'], "experiment_id");
                if (!isset($experiment['experiment_id'])) {
                    $continue = false;
                }
                if ($continue) {
                    $posted_query = $json->decode($q['json_query']);
                    $query_array = query__get_query_array($posted_query['query']);
                    $active_clause = array('query' => participant_status__get_pquery_snippet("eligible_for_experiments"), 'pars' => array());
                    $exptype_clause = array('query' => "subscriptions LIKE (:experiment_ext_type)", 'pars' => array(':experiment_ext_type' => "%|" . $experiment['experiment_ext_type'] . "|%"));
                    $notyetassigned_clause = array('query' => "participant_id NOT IN (SELECT participant_id FROM " . table('participate_at') . " WHERE experiment_id= :experiment_id)", 'pars' => array(':experiment_id' => $experiment['experiment_id']));
                    $additional_clauses = array($active_clause, $exptype_clause, $notyetassigned_clause);
                    $query = query__get_query($query_array, time(), $additional_clauses, '');
                    $result = or_query($query['query'], $query['pars']);
                    $p_is_eligibe = false;
                    while ($pc = pdo_fetch_assoc($result)) {
                        if ($pc['participant_id'] = $p['participant_id']) {
                            $p_is_eligibe = true;
                        }
                    }
                    if (!$p_is_eligibe) {
                        $continue = false;
                    }
                }
                if ($continue) {
                    // assign participant
                    $pars = array(':participant_id' => $p['participant_id'], ':experiment_id' => $experiment['experiment_id']);
                    $query = "INSERT INTO " . table('participate_at') . " (participant_id,experiment_id)\n                            VALUES (:participant_id , :experiment_id)";
                    $done = or_query($query, $pars);
                    $num_assigned++;
                    if ($settings['permanent_queries_invite'] == 'y') {
                        // send invitation into mail queue
                        $pars = array(':experiment_id' => $experiment['experiment_id'], ':now' => time(), ':recipient' => $p['participant_id']);
                        $query = "INSERT INTO " . table('mail_queue') . "\n                                SET timestamp = :now,\n                                mail_type = 'invitation',\n                                mail_recipient = :recipient,\n                                experiment_id = :experiment_id ";
                        $done = or_query($query, $pars);
                    }
                    if (!isset($query_assigned[$q['query_id']])) {
                        $query_assigned[$q['query_id']] = 0;
                    }
                    $query_assigned[$q['query_id']]++;
                }
            }
            // done with participant, reset permanent status
            $pars = array(':participant_id' => $p['participant_id']);
            $query = "UPDATE " . table('participants') . "\n                    SET apply_permanent_queries = 0\n                    WHERE participant_id = :participant_id";
            $done = or_query($query, $pars);
        }
        // and now update permanent queries with assignment numbers
        foreach ($pqu as $q) {
            if (!isset($query_assigned[$q['query_id']])) {
                $query_assigned[$q['query_id']] = 0;
            }
            $done = query__update_permanent_query($q['query_id'], $query_assigned[$q['query_id']]);
        }
    }
    $target = 'Participants checked: ' . $num_p;
    if ($num_p > 0) {
        $target .= ', PermQueries found: ' . $num_queries;
    }
    if ($num_queries > 0) {
        $target .= ', Assignments made: ' . $num_assigned;
    }
    return $target;
}
 echo $pseudo_query_display;
 echo '</TD><TD align="center">';
 // permanent query button
 if ($settings['allow_permanent_queries'] == 'y' && check_allow('experiment_assign_query_permanent_activate')) {
     $cgivars = array();
     $cgivars[] = "make_permanent=true";
     if (isset($_REQUEST['search_sort'])) {
         $cgivars[] = 'search_sort=' . urlencode($_REQUEST['search_sort']);
     }
     $cgivars[] = 'experiment_id=' . $experiment_id;
     $link = thisdoc();
     if (count($cgivars) > 0) {
         $link .= '?' . implode("&", $cgivars);
     }
     echo button_link($link, lang('activate_query_permanently'), 'toggle-on');
     $perm_queries = query__get_permanent($experiment_id);
     if (count($perm_queries) > 0) {
         echo '<BR>' . lang('found_active_permanent_query');
     }
 }
 echo '</TD></TR></TABLE>';
 echo '<BR><BR>';
 $query_array = query__get_query_array($posted_query['query']);
 $active_clause = array('query' => participant_status__get_pquery_snippet("eligible_for_experiments"), 'pars' => array());
 $exptype_clause = array('query' => "subscriptions LIKE (:experiment_ext_type)", 'pars' => array(':experiment_ext_type' => "%|" . $experiment['experiment_ext_type'] . "|%"));
 $notyetassigned_clause = array('query' => "participant_id NOT IN (SELECT participant_id FROM " . table('participate_at') . " WHERE experiment_id= :experiment_id)", 'pars' => array(':experiment_id' => $experiment_id));
 $additional_clauses = array($active_clause, $exptype_clause, $notyetassigned_clause);
 $query = query__get_query($query_array, $query_id, $additional_clauses, $sort);
 //echo '<TABLE width="70%" border=0><TR><TD><B>Query:</B></TD></TR><TR><TD>';
 //echo $query['query'];
 //echo '</TD></TR></TABLE>';