Changeset 1132 for trunk/common


Ignore:
Timestamp:
04/24/10 14:13:20 (2 years ago)
Author:
bogdan2412
Message:

Merge branch taskreordering into trunk.

Location:
trunk/common/db
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/common/db/round.php

    r1120 r1132  
    33require_once(IA_ROOT_DIR."common/db/db.php"); 
    44require_once(IA_ROOT_DIR."common/db/parameter.php"); 
     5require_once(IA_ROOT_DIR."common/db/round_task.php"); 
    56 
    67function _round_cache_add($round) { 
     
    111112    } 
    112113    $fields = "round_task.task_id AS id, ". 
    113               "task.`order` AS `order`, ". 
     114              "round_task.`order_id` AS `order`, ". 
    114115              "task.`title` AS `title`, ". 
    115116              "task.`page_name` AS `page_name`, ". 
     
    125126                          LEFT JOIN ia_task as task ON task.id = round_task.task_id 
    126127                          WHERE `round_task`.`round_id` = '%s' 
    127                           ORDER BY task.`order` LIMIT %d, %d", 
     128                          ORDER BY round_task.`order_id` LIMIT %d, %d", 
    128129                          db_escape($round_id), db_escape($first), db_escape($count)); 
    129130    } else { 
     
    139140                          WHERE `round_task`.`round_id` = '%s' 
    140141                            AND %s 
    141                           ORDER BY task.`order` LIMIT %d, %d", 
     142                          ORDER BY round_task.`order_id` LIMIT %d, %d", 
    142143                         db_escape($user_id), 
    143144                         db_escape($round_id), db_escape($filter_clause), 
     
    218219    log_assert(is_round_id($round_id)); 
    219220 
    220     // delete all round-task relations 
    221     $query = sprintf("DELETE FROM ia_round_task 
    222                       WHERE round_id = '%s'", 
    223                      db_escape($round_id)); 
    224     db_query($query); 
     221    $old_tasks_count = count($old_tasks); 
     222 
     223    // Do nothing with common tasks, be smart. 
     224    $common_tasks = array_intersect($old_tasks, $tasks); 
     225    $old_tasks = array_diff($old_tasks, $common_tasks); 
     226    $tasks = array_diff($tasks, $common_tasks); 
     227 
     228    // delete round-task relations 
     229    if (count($old_tasks) > 0) { 
     230        $query = sprintf("DELETE FROM ia_round_task 
     231                          WHERE round_id = %s AND task_id IN (%s)", 
     232                         db_quote($round_id), 
     233                         implode(',', array_map("db_quote", $old_tasks))); 
     234        db_query($query); 
     235    } 
     236 
    225237    foreach($old_tasks as $task) { 
    226238        // Update parent round cache for old tasks 
     
    231243        // insert new relations 
    232244        $values = array(); 
     245        $order_id = $old_tasks_count; 
    233246        foreach ($tasks as $task_id) { 
    234             $values[] = "('".db_escape($round_id)."', '".db_escape($task_id)."')"; 
     247            $order_id += 1; 
     248            $values[] = "('".db_escape($round_id)."', '". 
     249                        db_escape($task_id)."', '".db_escape($order_id)."')"; 
    235250        } 
    236         $query = "INSERT INTO ia_round_task (round_id, task_id) 
     251        $query = "INSERT INTO ia_round_task (round_id, task_id, order_id) 
    237252                  VALUES ". implode(', ', $values); 
    238253        db_query($query); 
     
    242257        } 
    243258    } 
     259 
     260    round_task_recompute_order($round_id); 
    244261} 
    245262 
  • trunk/common/db/task.php

    r1120 r1132  
    44require_once(IA_ROOT_DIR . "common/task.php"); 
    55require_once(IA_ROOT_DIR . "common/db/parameter.php"); 
     6require_once(IA_ROOT_DIR . "common/db/round_task.php"); 
    67 
    78// Add $task to cache if not null, return $task. 
     
    5556} 
    5657 
     58// Deletes a task from ia_round_task 
     59function task_delete_from_rounds($task_id) { 
     60    // Get all rounds for the task 
     61    $query = "SELECT DISTINCT round_id FROM ia_round_task 
     62              WHERE task_id = " . db_quote($task_id); 
     63    $res = db_fetch_all($query); 
     64 
     65    // Delete task 
     66    db_query("DELETE FROM ia_round_task WHERE task_id = ".db_quote($task_id)); 
     67 
     68    // Repair rounds order 
     69    foreach ($res as $row) { 
     70        round_task_recompute_order($row['round_id']); 
     71    } 
     72} 
     73 
    5774// Delete a task, including scores, jobs and page 
    5875// WARNING: This is irreversible. 
     
    8097 
    8198    // Remove task from all rounds 
    82     db_query("DELETE FROM `ia_round_task` 
    83               WHERE `task_id` = " . db_quote($task["id"])); 
     99    task_delete_from_rounds($task['id']); 
    84100 
    85101    // Delete task jobs 
     
    247263    return $tasks; 
    248264} 
     265 
     266// Updates the forum topic associated with a task. 
     267function task_update_forum_topic($task_id, $round_id = "arhiva") { 
     268    if (!is_task_id($task_id)) { 
     269        log_error("Invalid task id"); 
     270    } 
     271 
     272    // Get task info 
     273    $query = "SELECT title, page_name FROM ia_task 
     274              WHERE id = " . db_quote($task_id); 
     275    $task = db_fetch($query); 
     276 
     277    // Get the forum topic 
     278    $query = "SELECT forum_topic 
     279              FROM ia_textblock 
     280              WHERE name = " . db_quote($task['page_name']); 
     281    $res = db_fetch($query); 
     282    $topic_id = $res['forum_topic']; 
     283 
     284    // Check the textblock has an associated forum topic. 
     285    if (is_null($topic_id)) { 
     286        return; 
     287    } 
     288 
     289    // Get the first message from the topic 
     290    $query = "SELECT ID_FIRST_MSG AS `msg_id` 
     291              FROM ia_smf_topics 
     292              WHERE ID_TOPIC = " . db_quote($topic_id); 
     293    $res = db_fetch($query); 
     294    // Topic id doesn't exist 
     295    if (is_null($res)) { 
     296        return; 
     297    } 
     298    $message_id = $res['msg_id']; 
     299 
     300    // Get the subject and the body of the message 
     301    $query = "SELECT subject, body FROM ia_smf_messages 
     302              WHERE ID_MSG = " . db_quote($message_id); 
     303    $message = db_fetch($query); 
     304 
     305    // Find the number associated with the (task, round) pair. 
     306    $query = sprintf( 
     307        "SELECT order_id FROM ia_round_task 
     308         WHERE round_id = %s AND task_id = %s", 
     309         db_quote($round_id), db_quote($task_id)); 
     310    $res = db_fetch($query); 
     311    $task_number = sprintf("%03d", $res['order_id'] - 1); 
     312 
     313    // New info 
     314    $new_subject = $task_number . " " . $task['title']; 
     315    $body_start = mb_substr($message['body'], 0, 35); 
     316    if ($body_start != "Aici puteti discuta despre problema" && 
     317        $body_start != "Aici puteÈ›i discuta despre prob" && 
     318        $body_start != "Aici puteÅ£i discuta despre probl") { 
     319        $new_body = 'Aici puteÅ£i discuta despre problema ' . 
     320                    '[url=http://infoarena.ro/problema/' . $task_id . ']' . 
     321                    $task['title'] . '[/url].'; 
     322    } else { 
     323        $new_body = $message['body']; 
     324    } 
     325 
     326    // Finally, update the message 
     327    $query = sprintf( 
     328        "UPDATE ia_smf_messages 
     329         SET subject = %s, body = %s 
     330         WHERE ID_MSG = %s", 
     331         db_quote($new_subject), db_quote($new_body), db_quote($message_id)); 
     332    db_query($query); 
     333 
     334    // Not finished yet, must change replies too. 
     335    // This is extremely time consuming. 
     336    if ($new_subject != $message['subject']) { 
     337        $query = sprintf( 
     338            "UPDATE ia_smf_messages 
     339             SET subject = %s 
     340             WHERE subject LIKE %s", 
     341             db_quote("Răspuns: " . $new_subject), 
     342             db_quote("%" . $message['subject'])); 
     343        db_query($query); 
     344    } 
     345} 
     346 
    249347?> 
Note: See TracChangeset for help on using the changeset viewer.