* * @_PATTERNS_@ * // get the ID, name, title, owner, and realm for all surveys the current user can access * $ok = survey_get_for_current_user($surveys); * if ($ok) { * // use $surveys === array (1 => array ('id' => 1, 'name' => 'foo', ...), ...); * } * * // get the where clause to limit a SQL query for the current user * $where = survey_fetch_limiting_where(); * $sql = sprintf('SELECT * FROM %s WHERE %s', $GLOBALS['ESPCONFIG']['survey_table'], $where); * * @_NOTES_@ * Interpretation of the open constants returned by survey_open(): * Constant Meaning * ------------------------ ----------------------------------------------------------------------------------- * STATUS_OPEN The current time is within the open & close dates, or those dates aren't given * STATUS_CLOSED_TOO_EARLY The current time is before the open date * STATUS_CLOSED_TOO_LATE The current time is after the close date * */ // {{{ constants define('STATUS_OPEN', 0); define('STATUS_CLOSED_TOO_EARLY', 1); define('STATUS_CLOSED_TOO_LATE', 2); // }}} /* survey inquisition */ // ... when you know the survey ID only, use these // ... when you already have information from the survey, use these // {{{ survey_status_is_edit() Is the survey being edited? function survey_status_is_edit($status) { return (is_numeric($status) && (0 === (int)$status) ? true : false); } // }}} // {{{ survey_status_is_active() Is the survey in active status? function survey_status_is_active($status) { return survey_status_is($status, STATUS_ACTIVE); } // }}} // {{{ survey_status_is_done() Is the survey in done status? function survey_status_is_done($status) { return survey_status_is($status, STATUS_DONE); } // }}} // {{{ survey_status_is_deleted() Is the survey in deleted status? function survey_status_is_deleted($status) { return survey_status_is($status, STATUS_DELETED); } // }}} // {{{ survey_status_is_test() Is the survey in test status? function survey_status_is_test($status) { return survey_status_is($status, STATUS_TEST); } // }}} // {{{ survey_status_is() Does the survey status match the given bit? function survey_status_is($status, $bit) { return (is_numeric($status) && ($bit === ((int)$status & $bit)) ? true : false); } // }}} // {{{ survey_open() Is "now" within the given open and close date times? function survey_open($open_date, $close_date, $now = null) { // figure out if we have valid open and closing dates // ... exclude nulls and empty dates $has_open_date = (empty($open_date) || '0000-00-00 00:00:00' == $open_date ? false : true); $has_close_date = (empty($close_date) || '0000-00-00 00:00:00' == $close_date ? false : true); // ... convert to time stamps, and if failure, assume the date is empty $badts = (is_callable('version_compare') && version_compare('5.1.0', PHP_VERSION, '<=') ? false : -1); if ($has_open_date) { $open_date_ts = (is_int($open_date) ? $open_date : strtotime($open_date)); if ($badts === $open_date_ts) { $has_open_date = false; } } if ($has_close_date) { $close_date_ts = (is_int($close_date) ? $close_date : strtotime($close_date)); if ($badts === $close_date_ts) { $has_close_date = false; } } $nowts = (is_int($now) ? $now : (is_null($now) ? time() : strtotime($now))); // if there's an open date, but the current time is before then, we're too early if ($has_open_date && $nowts < $open_date_ts) { return STATUS_CLOSED_TOO_EARLY; // if there's a close date, but the current time is after then, we're too late } else if ($has_close_date && $close_date_ts < $nowts) { return STATUS_CLOSED_TOO_LATE; // otherwise, we're open for business } else { return STATUS_OPEN; } } // }}} /* designer/administrator survey recovery methods */ // {{{ survey_get_for_current_user() Get the ID and information about ACTIVE or DONE surveys the CURRENT user can see function survey_get_for_current_user(&$surveys) { // get the where clause appropriate for the current user $where = survey_fetch_limiting_where(); // get the survey data based on those limitations $sql =<<= 1 o active: === 1 if active, === 0 otherwise o open: either STATUS_OPEN, STATUS_CLOSED_TOO_EARLY, or STATUS_CLOSED_TOO_LATE o available: === 1 if available, === 0 otherwise o visible: === 1 */ function survey_get_accessibility(&$accessibility, $sids, $username = null, $realm = null) { $yes = _addslashes('Y'); // construct the accessibilty clauses // ... active $statusActive = db_bin('tblSurvey.status', STATUS_ACTIVE); $statusActive =<<MoveNext(); // make sure we have an ID column if (isset($row[$surveyIDCol])) { $id = $row[$surveyIDCol]; } else { continue; } // add in the survey information to the previous survey information if (isset($surveys[$id])) { if ($needsConversion) { // we've collided, but not yet converted to an array: do so $surveys[$id] = array ($surveys[$id]); $needsConversion = false; } $surveys[$id][] = $row; } else { $surveys[$id] = $row; } } db_close($res); return true; } // }}} // {{{ survey_merge_sets() Combine two or more survey sets into one // NOTE: This is like array_merge(), but squashes duplicate surveys that appear function survey_merge_sets(&$merged, $a /* ... */) { // make sure we have an initial array if (! is_array($merged)) { $merged = array (); } // add in surveys from the second argument on, skipping duplicates for ($i = 1; $i < func_num_args(); $i++) { $set = func_get_arg($i); if (! is_array($set)) { continue; } foreach ($set as $sid => $survey) { if (! array_key_exists($sid, $merged)) { $merged[$sid] = $survey; } } } return true; } // }}} // {{{ survey_fetch_url_by_survey_name() Return the URL to a survey with a given name function survey_fetch_url_by_survey_name($name) { return rtrim($GLOBALS['ESPCONFIG']['base_url'], '/') . '/public/survey.php?name=' . htmlentities($name); } // }}} ?>