source: trunk/common/db/parameter.php @ 1184

Revision 852, 2.0 KB checked in by cdleonard@…, 4 years ago (diff)

svn:eol-style native in trunk. Also svn:executable on all scripts

  • Property svn:eol-style set to native
Line 
1<?php
2
3require_once(IA_ROOT_DIR."common/db/db.php");
4require_once(IA_ROOT_DIR."common/cache.php");
5
6// Round / task parameters
7// This is sort of shared between rounds and tasks.
8
9// Replaces all parameter values according to the given dictionary
10// :WARNING: This function does not check for parameter validity!
11// It only stores them to database.
12//
13// $object_type is "task" or "round"
14// NOTE: magic caching enabled
15function parameter_update_values($object_type, $object_id, $dict) {
16    log_assert($object_type == 'task' || $object_type == 'round');
17
18    // delete all parameters connected to this task
19    $query = sprintf("DELETE FROM ia_parameter_value
20                      WHERE object_type = %s AND object_id = %s",
21                     db_quote($object_type), db_quote($object_id));
22    db_query($query);
23
24    // insert given parameters
25    foreach ($dict as $k => $v) {
26        $query = sprintf("INSERT INTO ia_parameter_value
27                            (`object_type`, `object_id`, `parameter_id`, `value`)
28                          VALUES (%s, %s, %s, %s)",
29                         db_quote($object_type), db_quote($object_id),
30                         db_quote($k), db_quote($v));
31        db_query($query);
32    }
33
34    // Store to cache.
35    mem_cache_set("$object_type-params-by-id:$object_id", $dict);
36}
37
38// Returns hash with task parameter values
39function parameter_get_values($object_type, $object_id) {
40    log_assert($object_type == 'task' || $object_type == 'round');
41
42    // Search in cache.
43    if (($res = mem_cache_get("$object_type-params-by-id:$object_id")) !== false) {
44        return $res;
45    }
46
47    $query = sprintf("SELECT *
48                      FROM ia_parameter_value
49                      WHERE object_type = %s AND object_id = %s",
50                     db_quote($object_type), db_quote($object_id));
51    $dict = array();
52    foreach (db_fetch_all($query) as $row) {
53        $dict[$row['parameter_id']] = $row['value'];
54    }
55   
56    // Store in cache
57    mem_cache_set("$object_type-params-by-id:$object_id", $dict);
58    return $dict;
59}
60
61?>
Note: See TracBrowser for help on using the repository browser.