Changeset 1132


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

Merge branch taskreordering into trunk.

Location:
trunk
Files:
9 edited
5 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?> 
  • trunk/www/controllers/round.php

    r1125 r1132  
    33require_once(IA_ROOT_DIR."common/db/db.php"); 
    44require_once(IA_ROOT_DIR."common/db/round.php"); 
     5require_once(IA_ROOT_DIR."common/db/round_task.php"); 
    56require_once(IA_ROOT_DIR."common/db/task.php"); 
    67require_once(IA_ROOT_DIR."common/round.php"); 
     
    203204} 
    204205 
     206function controller_round_task_order($round_id) { 
     207    // Validate round_id 
     208    if (!is_round_id($round_id)) { 
     209        flash_error('Identificatorul rundei este invalid'); 
     210        redirect(url_home()); 
     211    } 
     212 
     213    // Get round 
     214    $round = round_get($round_id); 
     215    if (!$round) { 
     216        flash_error("Runda nu exista"); 
     217        redirect(url_home()); 
     218    } 
     219 
     220    // Security check 
     221    identity_require('round-edit', $round); 
     222 
     223    if (request_is_post()) { 
     224        // Request a list of ids with the new task order 
     225        $task_order_strings = explode(';', request('task_order', '')); 
     226 
     227        // Evil users, abort post. 
     228        foreach ($task_order_strings as $order_string) { 
     229            if (!is_numeric($order_string)) { 
     230                redirect(url_round_edit_task_order($round_id)); 
     231            } 
     232        } 
     233 
     234        // Parse the values 
     235        $task_order = array_map("intval", $task_order_strings); 
     236 
     237        // Increment by 1 
     238        foreach ($task_order as &$order_id) { 
     239            $order_id += 1; 
     240        } 
     241        unset($order_id); 
     242 
     243        // Get the tasks 
     244        $first = min($task_order) - 1; 
     245        $count = count($task_order); 
     246        $tasks = round_get_tasks($round_id, $first, $count); 
     247 
     248        $task_ids = array(); 
     249        foreach ($tasks as $task) { 
     250            $task_ids[$task['order']] = $task['id']; 
     251        } 
     252 
     253        // Check each order_id has an associated task_id 
     254        // Another evil users check 
     255        foreach ($task_order as $order_id) { 
     256            if (!getattr($task_ids, $order_id)) { 
     257                redirect(url_round_edit_task_order($round_id)); 
     258            } 
     259        } 
     260 
     261        $current_id = $first + 1; 
     262        foreach ($task_order as $order_id) { 
     263            $task_id = $task_ids[$order_id]; 
     264 
     265            if ($current_id != $order_id) { 
     266                round_task_update_order_id($round_id, $task_id, $current_id); 
     267                task_update_forum_topic($task_id); 
     268            } 
     269 
     270            $current_id += 1; 
     271        } 
     272    } 
     273 
     274    // Create view. 
     275    $view = array(); 
     276    $view['title'] = "Editare ordine probleme $round_id"; 
     277    $view['page_name'] = url_round_edit_task_order($round_id); 
     278    $view['round_id'] = $round_id; 
     279    $view['round'] = $round; 
     280 
     281    execute_view_die("views/round_task_order.php", $view); 
     282} 
     283 
    205284// Creates a round. Minimalist 
    206285function controller_round_create() 
  • trunk/www/index.php

    r1111 r1132  
    130130} 
    131131 
    132 // Round detail editor and deleter 
    133 else if ($urlstart == 'admin' && getattr($pagepath, 1) == 'runda') { 
    134     $obj_id = implode("/", array_slice($pagepath, 2)); 
     132// Round edit parameters 
     133else if ($urlstart == 'admin' && getattr($pagepath, 1) == 'runda' && 
     134         $action == 'edit-params') { 
    135135    require_once(IA_ROOT_DIR.'www/controllers/round.php'); 
    136     require_once(IA_ROOT_DIR.'www/controllers/textblock.php'); 
    137     if ($action == 'sterge-runda') { 
    138         if (request('delete-pages')) { 
    139             $v = request('textblocks'); 
    140             controller_textblock_delete_many($v, url_round_delete($obj_id)); 
    141         } elseif (request('delete-round')) { 
    142             controller_round_delete($obj_id); 
    143         } else { 
    144             controller_round_delete_view($obj_id); 
    145         } 
     136    $round_id = implode("/", array_slice($pagepath, 2)); 
     137    controller_round_details($round_id); 
     138} 
     139 
     140// Round edit task order 
     141else if ($urlstart == 'admin' && getattr($pagepath, 1) == 'runda' && 
     142         $action == 'edit-task-order') { 
     143    require_once(IA_ROOT_DIR.'www/controllers/round.php'); 
     144    $round_id = implode("/", array_slice($pagepath, 2)); 
     145    controller_round_task_order($round_id); 
     146} 
     147 
     148// Round delete 
     149else if ($urlstart == 'admin' && getattr($pagepath, 1) == 'runda' && 
     150         $action == 'sterge-runda') { 
     151    require_once(IA_ROOT_DIR.'www/controllers/round.php'); 
     152    require_once(IA_ROOT_DIR.'www/controllers/textblock.php'); 
     153    $round_id = implode("/", array_slice($pagepath, 2)); 
     154    if (request('delete-pages')) { 
     155        $v = request('textblocks'); 
     156        controller_textblock_delete_many($v, url_round_delete($round_id)); 
     157    } elseif (request('delete-round')) { 
     158        controller_round_delete($round_id); 
    146159    } else { 
    147         controller_round_details($obj_id); 
     160        controller_round_delete_view($round_id); 
    148161    } 
    149162} 
  • trunk/www/macros/macro_tasks.php

    r1128 r1132  
    153153 
    154154    // get round tasks 
    155     $tasks = round_get_tasks($round_id, 
     155    $tasks = round_get_tasks( 
     156             $round_id, 
    156157             $options['first_entry'], 
    157158             $options['display_entries'], 
    158              $user_id, $scores, 
    159              $filter, $show_progress); 
     159             $user_id, 
     160             $scores, 
     161             $filter, 
     162             $show_progress); 
     163 
    160164    $options['total_entries'] = round_get_task_count( 
    161165             $round_id, $user_id, $filter); 
    162166    $options['row_style'] = 'task_row_style'; 
     167    $options['css_row_parity'] = true; 
     168 
    163169    $options['css_class'] = 'tasks'; 
    164     $options['css_row_parity'] = true; 
     170    if (getattr($args, 'drag_and_drop', false)) 
     171      $options['css_class'] .= ' dragndrop'; 
    165172 
    166173    $column_infos = array(); 
  • trunk/www/static/css/screen.css

    r1128 r1132  
    10441044table { 
    10451045    border-collapse: collapse; 
     1046    line-height: 2em; 
     1047    margin: 0.5em; 
     1048    width: 99%; 
    10461049} 
    10471050 
  • trunk/www/url.php

    r1125 r1132  
    308308function url_round_edit_params($round_id) { 
    309309    log_assert(is_round_id($round_id)); 
    310     return url_complex("admin/runda/" . $round_id); 
     310    return url_complex("admin/runda/" . $round_id, 
     311                       array('action' => 'edit-params')); 
     312} 
     313 
     314function url_round_edit_task_order($round_id) { 
     315    log_assert(is_round_id($round_id)); 
     316    return url_complex("admin/runda/" . $round_id, 
     317                       array('action' => 'edit-task-order')); 
    311318} 
    312319 
  • trunk/www/views/header.php

    r1121 r1132  
    6969    <script type="text/javascript" src="<?= html_escape(url_static('js/tags.js')) ?>"></script> 
    7070    <script type="text/javascript" src="<?= html_escape(url_static('js/roundtimer.js')) ?>"></script> 
     71    <script type="text/javascript" src="<?= html_escape(url_static('js/foreach.js')) ?>"></script> 
     72    <script type="text/javascript" src="<?= html_escape(url_static('js/tablednd.js')) ?>"></script> 
    7173 
    7274    <script type="text/javascript" src="http://summify.com/client/v1/249f00c6/client.js"></script> 
  • trunk/www/views/round_edit_header.php

    r1125 r1132  
    1010    $tab_names = array( 
    1111        'edit' => 'Pagină', 
    12         'round-edit-params' => 'Parametri'); 
     12        'round-edit-params' => 'Parametri', 
     13        'round-edit-task-order' => 'Ordine probleme'); 
    1314 
    1415    $tab_urls = array( 
    1516        'edit' => url_round_edit($round_id), 
    16         'round-edit-params' => url_round_edit_params($round_id)); 
     17        'round-edit-params' => url_round_edit_params($round_id), 
     18        'round-edit-task-order' => url_round_edit_task_order($round_id)); 
    1719 
    1820    $permissions = array( 
    1921        'edit' => 'round-edit', 
    20         'round-edit-params' => 'round-edit'); 
     22        'round-edit-params' => 'round-edit', 
     23        'round-edit-task-order' => 'round-edit'); 
    2124 
    2225    $round = round_get($round_id); 
Note: See TracChangeset for help on using the changeset viewer.