Changeset 1081 for trunk


Ignore:
Timestamp:
12/17/09 00:03:05 (2 years ago)
Author:
savin.tiberiu@…
Message:

Made the interface for adding tags to a task.
Reviewed: http://reviewboard.infoarena.ro/r/120/

Location:
trunk
Files:
1 added
8 edited

Legend:

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

    r1080 r1081  
    5454} 
    5555 
     56// Receives an array of tag_id's 
     57// Return an array with id's of their parents 
     58// Each parent id appears only once 
     59function tag_get_parents($tags) { 
     60    $query = sprintf("SELECT DISTINCT(`parent`) 
     61            FROM ia_tags 
     62            WHERE `id` IN (%s)", 
     63            implode(",", array_map('db_quote', $tags)) 
     64        ); 
     65    $result = db_fetch_all($query); 
     66    $ret = Array(); 
     67    foreach ($result as $row) { 
     68        $ret[] = $row['parent']; 
     69    } 
     70    return $ret; 
     71} 
     72 
    5673// Get tag id for a certain tag 
    5774function tag_get_id($tag) { 
     
    8198} 
    8299 
     100// Get a list of tags from a list of tag ids 
     101function tag_get_by_ids($tag_ids) { 
     102    $query = sprintf( 
     103            "SELECT `id` AS tag_id, `name` AS tag_name, 
     104                    `parent` AS tag_parent, `type` AS tag_type 
     105            FROM ia_tags 
     106            WHERE `id` IN (%s)", implode(', ', array_map('db_quote', $tag_ids)) 
     107        ); 
     108    return db_fetch_all($query); 
     109} 
     110 
    83111// Assign numeric id to a given tag name 
    84112function tag_assign_id($tag) { 
     
    170198        db_escape($obj), $where, "%s" 
    171199    ); 
    172     if (is_null($type)) { 
    173         $join = ""; 
    174     } else { 
     200 
     201    $join = ""; 
     202    if (!is_null($type)) { 
    175203        $join = "LEFT JOIN ia_tags AS tags ON obj_tags.tag_id = tags.id"; 
    176204        $where .= sprintf(" AND tags.type = %s", db_quote($type)); 
    177205    } 
     206 
    178207    $subquery = sprintf("SELECT tag_id FROM ia_%s_tags AS obj_tags %s WHERE %s", 
    179208        db_escape($obj), $join, $where); 
  • trunk/common/tags.php

    r1080 r1081  
    5252} 
    5353 
     54// Receives a list of parent tags and children tags 
     55// For each parent_tag adds an array 'sub_tags' containing 
     56// an array with all his children tags 
     57function build_tags_tree($parent_tags, $sub_tags) { 
     58    log_assert(is_array($parent_tags), "Parent tags is not an array"); 
     59    log_assert(is_array($sub_tags), "Children tags is not an array"); 
     60 
     61    $parent_tags_key = Array(); 
     62    foreach ($parent_tags as $key => $tag) { 
     63        $parent_tags[$key]['sub_tags'] = Array(); 
     64        $parent_tags_key[ $tag['tag_id'] ] = $key; 
     65    } 
     66 
     67    foreach ($sub_tags as $tag) { 
     68        log_assert(isset($parent_tags_key[ $tag['tag_parent'] ]), "Child tag doesn't have a parent"); 
     69        $parent_tag_key = $parent_tags_key[ $tag['tag_parent'] ]; 
     70        $parent_tags[ $parent_tag_key ]['sub_tags'][] = $tag; 
     71    } 
     72 
     73    return $parent_tags; 
     74} 
     75 
    5476// Receives a list of tags of a certain type and, optionally, with a 
    5577// certain parent and updates the tag list for the specified object. 
  • trunk/common/task.php

    r996 r1081  
    277277} 
    278278 
     279// Receives a list of method and algorithm tag ids and adds links them to task_id 
     280function task_update_tags($task_id, $method_tags_id, $algorithm_tags_id) { 
     281    log_assert(is_array($method_tags_id), 'method_tags must be an array'); 
     282    log_assert(is_array($algorithm_tags_id), 'algorithm_tags must be an array'); 
     283    log_assert(is_task_id($task_id), "Invalid task_id"); 
     284 
     285    tag_clear('task', $task_id, 'method'); 
     286    tag_clear('task', $task_id, 'algorithm'); 
     287 
     288    foreach ($method_tags_id as $tag_id) { 
     289        tag_add('task', $task_id, $tag_id); 
     290    } 
     291 
     292    foreach ($algorithm_tags_id as $tag_id) { 
     293        tag_add('task', $task_id, $tag_id); 
     294    } 
     295} 
     296 
    279297?> 
  • trunk/www/controllers/task.php

    r1080 r1081  
    6565 
    6666    // Tag data 
    67     $tag_types = Array('author', 'contest', 'year', 'round', 'age_group', 'method', 'algorithm'); 
     67    $tag_types = Array('author', 'contest', 'year', 'round', 'age_group'); 
    6868    $tag_parents = Array('year' => 'contest', 'round' => 'year', 'age_group' => 'contest'); 
    6969 
     
    265265} 
    266266 
     267// Tag a task 
     268function controller_task_tag($task_id) { 
     269    if (!is_task_id($task_id)) { 
     270        flash_error("Problema inexistenta"); 
     271        redirect(url_home()); 
     272    } 
     273 
     274    $task = task_get($task_id); 
     275    identity_require('task-tag', $task); 
     276 
     277    if (!$task) { 
     278        flash_error("Problema inexistenta"); 
     279        redirect(url_home()); 
     280    } 
     281 
     282    if (request_is_post()) { 
     283        $algorithm_tags_id = request("algorithm_tags"); 
     284        $method_tags_id = tag_get_parents($algorithm_tags_id); 
     285 
     286        if (!is_array($algorithm_tags_id)) { 
     287            flash_error("Datele trimise sunt invalide. Raporteaza aceasta problema unui admin."); 
     288            redirect(url_task_edit_tag($task_id)); 
     289        } 
     290 
     291        foreach ($algorithm_tags_id as $tag_id) { 
     292            if (!is_tag_id($tag_id)) { 
     293                flash_error("Datele trimise sunt invalide. Raporteaza aceasta problema unui admin."); 
     294                redirect(url_task_edit_tag($task_id)); 
     295            } 
     296        } 
     297 
     298        $algorithm_tags = tag_get_by_ids($algorithm_tags_id); 
     299        $count = 0; 
     300        foreach ($algorithm_tags as $tag) { 
     301            if ($tag['tag_type'] == 'algorithm') { 
     302                $count++; 
     303            } 
     304        } 
     305        if ($count != count($algorithm_tags_id)) { 
     306            flash_error("Datele trimise sunt invalide. Raporteaza aceasta problema unui admin."); 
     307            redirect(url_task_edit_tag($task_id)); 
     308        } 
     309 
     310        task_update_tags($task_id, $method_tags_id, $algorithm_tags_id); 
     311        flash("Tagurile au fost salvate cu succes"); 
     312    } 
     313 
     314    $tags = tag_get_all( Array('method') ); 
     315    $sub_tags = tag_get_all( Array('algorithm') ); 
     316 
     317    // Build_tags_tree looks for tag_id, tag_name etc. 
     318    // tag_get_all return id, name, etc. 
     319    foreach ($tags as &$tag) { 
     320        $tag['tag_id'] = $tag['id']; 
     321        $tag['tag_name'] = $tag['name']; 
     322        $tag['tag_type'] = $tag['type']; 
     323        $tag['tag_parent'] = $tag['parent']; 
     324    } 
     325 
     326    // Same for subtags 
     327    foreach ($sub_tags as &$tag) { 
     328        $tag['tag_id'] = $tag['id']; 
     329        $tag['tag_name'] = $tag['name']; 
     330        $tag['tag_type'] = $tag['type']; 
     331        $tag['tag_parent'] = $tag['parent']; 
     332    } 
     333 
     334    $tags_tree = build_tags_tree($tags, $sub_tags); 
     335 
     336    // Get tags for task_id 
     337    $task_tags = tag_get('task', $task_id, 'algorithm'); 
     338 
     339    $view['title'] = "Editeaza tagurile pentru problema ".$task['title']; 
     340    $view['task'] = $task; 
     341    $view['tags_tree'] = $tags_tree; 
     342    $view['task_tags'] = $task_tags; 
     343    execute_view_die('views/task_tag_edit.php', $view); 
     344} 
    267345 
    268346?> 
  • trunk/www/index.php

    r1080 r1081  
    8585// Task detail editor 
    8686else if ($urlstart == 'admin' && getattr($pagepath, 1) == 'problema') { 
     87    require_once(IA_ROOT_DIR.'www/controllers/task.php'); 
    8788    $obj_id = implode("/", array_slice($pagepath, 2)); 
    88     require_once(IA_ROOT_DIR.'www/controllers/task.php'); 
    89     controller_task_details($obj_id); 
     89    $action = request('action'); 
     90    if ($action == 'tag-edit') { 
     91        controller_task_tag($obj_id); 
     92    } else { 
     93        controller_task_details($obj_id); 
     94    } 
    9095} 
    9196 
     
    117122} 
    118123 
    119 // Round registration  
     124// Round registration 
    120125else if ($urlstart == 'inregistrare-runda') { 
    121126    $obj_id = implode("/", array_slice($pagepath, 1)); 
  • trunk/www/static/css/sitewide.css

    r1042 r1081  
    276276} 
    277277 
     278a.show_tag_list { 
     279    display: none; 
     280    padding: 5px; 
     281    outline-style: none; 
     282} 
     283 
     284ul.tag_list { 
     285    list-style-type: none; 
     286} 
     287 
     288li.tag_list_item { 
     289    padding-left: 15px; 
     290} 
  • trunk/www/url.php

    r1080 r1081  
    253253} 
    254254 
     255function url_task_edit_tags($task_id) { 
     256    log_assert(is_task_id($task_id)); 
     257    return url_complex("admin/problema/{$task_id}", array( 'action' => 'tag-edit')); 
     258} 
     259 
    255260function url_task_create() { 
    256261    return url_complex("admin/problema-noua"); 
  • trunk/www/views/task_edit.php

    r1080 r1081  
    139139                            'age_group' => Array("label" => "Grupa de varsta", 
    140140                                        "name" => "tag_age_group"), 
    141                             'method' => Array("label" => "Metoda de programare", 
    142                                         "name" => "tag_method"), 
    143                             'algorithm' => Array("label" => "Algoritm", 
    144                                         "name" => "tag_algorithm") 
    145141                        ); 
    146142    ?> 
Note: See TracChangeset for help on using the changeset viewer.