Changeset 2435 for trunk


Ignore:
Timestamp:
Jul 15, 2008, 3:29:23 AM (16 years ago)
Author:
rvelices
Message:
  • image rating on picture page done through ajax (tested safari/ie 6&7/ff)
Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions_rate.inc.php

    r2299 r2435  
    3737      or !in_array($rate, $conf['rate_items']))
    3838  {
    39     return;
     39    return false;
    4040  }
    4141
     
    4444  if ($user_anonymous and !$conf['rate_anonymous'])
    4545  {
    46     return;
     46    return false;
    4747  }
    4848
     
    119119  // update of images.average_rate field
    120120  $query = '
    121 SELECT ROUND(AVG(rate),2) AS average_rate
     121SELECT COUNT(rate) AS count
     122     , ROUND(AVG(rate),2) AS average
     123     , ROUND(STD(rate),2) AS stdev
    122124  FROM '.RATE_TABLE.'
    123125  WHERE element_id = '.$image_id.'
    124126;';
    125   $row = mysql_fetch_array(pwg_query($query));
     127  $row = mysql_fetch_assoc(pwg_query($query));
    126128  $query = '
    127129UPDATE '.IMAGES_TABLE.'
    128   SET average_rate = '.$row['average_rate'].'
     130  SET average_rate = '.$row['average'].'
    129131  WHERE id = '.$image_id.'
    130132;';
    131133  pwg_query($query);
     134  return $row;
    132135}
    133136
  • trunk/include/ws_functions.inc.php

    r2429 r2435  
    775775  return new PwgNamedStruct('image',$ret, null, array('name','comment') );
    776776}
     777
     778
     779/**
     780 * rates the image_id in the parameter
     781 */
     782function ws_images_Rate($params, &$service)
     783{
     784  $image_id = (int)$params['image_id'];
     785  $query = '
     786SELECT DISTINCT id FROM '.IMAGES_TABLE.'
     787  INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id
     788  WHERE id='.$image_id
     789  .get_sql_condition_FandF(
     790    array(
     791        'forbidden_categories' => 'category_id',
     792        'forbidden_images' => 'id',
     793      ),
     794    '    AND'
     795    ).'
     796    LIMIT 1';
     797  if ( mysql_num_rows( pwg_query($query) )==0 )
     798  {
     799    return new PwgError(404, "Invalid image_id or access denied" );
     800  }
     801  $rate = (int)$params['rate'];
     802  include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php');
     803  $res = rate_picture( $image_id, $rate );
     804  if ($res==false)
     805  {
     806    global $conf;
     807    return new PwgError( 403, "Forbidden or rate not in ". implode(',',$conf['rate_items']));
     808  }
     809  return $res;
     810}
     811
    777812
    778813/**
  • trunk/template/yoga/picture.tpl

    r2429 r2435  
    163163selectElement.disabled = true;
    164164var y = new PwgWS(rootUrl);
    165 
    166165y.callService(
    167166  "pwg.images.setPrivacyLevel", {image_id: id, level:level} ,
     
    183182  <tr>
    184183    <td class="label">{'Average rate'|@translate}</td>
    185     <td class="value">
     184    <td class="value" id="ratingSummary">
    186185    {if $rate_summary.count}
    187186      {assign var='rate_text' value='%.2f (rated %d times, standard deviation = %.2f)'|@translate }
     
    214213<form action="{$rating.F_ACTION}" method="post" id="rateForm">
    215214<div>
    216 {if isset($rating.USER_RATE)}{'update_rate'|@translate}{else}{'new_rate'|@translate}{/if}
     215<span id="updateRate">{if isset($rating.USER_RATE)}{'update_rate'|@translate}{else}{'new_rate'|@translate}{/if}</span>
    217216:
    218217{foreach from=$rating.marks item=mark name=rate_loop}
     
    225224{/foreach}
    226225<script type="text/javascript" src="{$ROOT_URL}{$themeconf.template_dir}/rating.js"></script>
     226<script type="text/javascript">
     227makeNiceRatingForm( {ldelim}rootUrl: '{$ROOT_URL|@escape:"javascript"}', image_id: {$current.id},
     228updateRateText: "{'update_rate'|@translate|@escape:'javascript'}", updateRateElement: document.getElementById("updateRate"),
     229ratingSummaryText: "{'%.2f (rated %d times, standard deviation = %.2f)'|@translate|@escape:'javascript'}", ratingSummaryElement: document.getElementById("ratingSummary") {rdelim} );
     230</script>
    227231</div>
    228232</form>
  • trunk/template/yoga/rating.js

    r1912 r2435  
    1 makeNiceRatingForm();
     1var gRatingOptions, gRatingButtons, gUserRating;
    22
    3 function makeNiceRatingForm()
     3function makeNiceRatingForm(options)
    44{
    5   var form = document.getElementById('rateForm');
    6   if (!form) return; //? template changed
    7   gRatingButtons = form.getElementsByTagName('input');
     5        gRatingOptions = options || {};
     6        var form = document.getElementById('rateForm');
     7        if (!form) return; //? template changed
    88
    9   gUserRating = "";
    10   for (var i=0; i<gRatingButtons.length; i++)
    11   {
    12     if ( gRatingButtons[i].type=="button" )
    13     {
    14       gUserRating = gRatingButtons[i].value;
    15       break;
    16     }
    17   }
     9        gRatingButtons = form.getElementsByTagName('input');
     10        gUserRating = "";
     11        for (var i=0; i<gRatingButtons.length; i++)
     12        {
     13                if ( gRatingButtons[i].type=="button" )
     14                {
     15                        gUserRating = gRatingButtons[i].value;
     16                        break;
     17                }
     18        }
    1819
    19   for (var i=0; i<gRatingButtons.length; i++)
    20   {
    21     var rateButton = gRatingButtons[i];
    22     rateButton.initialRateValue = rateButton.value; // save it as a property
     20        for (var i=0; i<gRatingButtons.length; i++)
     21        {
     22                var rateButton = gRatingButtons[i];
     23                rateButton.initialRateValue = rateButton.value; // save it as a property
     24                try { rateButton.type = "button"; } catch (e){}// avoid normal submit (use ajax); not working in IE6
    2325
    24     rateButton.value = ""; //hide the text IE/Opera
    25     with (rateButton.style)
    26     {
    27       textIndent = "-50px"; //hide the text FF
    28       marginLeft = marginRight = 0;
    29     }
     26                if (navigator.userAgent.indexOf('AppleWebKit/') == -1 ) rateButton.value = ""; //hide the text IE/Opera - breaks safari
     27                with (rateButton.style)
     28                {
     29                        textIndent = "-50px"; //hide the text FF
     30                        marginLeft = marginRight = 0;
     31                }
    3032
    31     if (i!=gRatingButtons.length-1 && rateButton.nextSibling.nodeType == 3 /*TEXT_NODE*/)
    32       rateButton.parentNode.removeChild(rateButton.nextSibling);
    33     if (i>0 && rateButton.previousSibling.nodeType == 3 /*TEXT_NODE*/)
    34       rateButton.parentNode.removeChild(rateButton.previousSibling);
     33                if (i!=gRatingButtons.length-1 && rateButton.nextSibling.nodeType == 3 /*TEXT_NODE*/)
     34                        rateButton.parentNode.removeChild(rateButton.nextSibling);
     35                if (i>0 && rateButton.previousSibling.nodeType == 3 /*TEXT_NODE*/)
     36                        rateButton.parentNode.removeChild(rateButton.previousSibling);
    3537
    36     if(window.addEventListener){ // Mozilla, Netscape, Firefox
    37       rateButton.addEventListener("click", updateRating, false );
    38       rateButton.addEventListener("mouseout", resetRatingStarDisplay, false );
    39       rateButton.addEventListener("mouseover", updateRatingStarDisplayEvt, false );
    40     }
    41     else if(window.attachEvent) { // IE
    42       rateButton.attachEvent("onclick", updateRating);
    43       rateButton.attachEvent("onmouseout", resetRatingStarDisplay);
    44       rateButton.attachEvent("onmouseover", updateRatingStarDisplayEvt);
    45     }
    46   }
    47   resetRatingStarDisplay();
     38                if(window.addEventListener){ // Mozilla, Netscape, Firefox
     39                        rateButton.addEventListener("click", updateRating, false );
     40                        rateButton.addEventListener("mouseout", resetRatingStarDisplay, false );
     41                        rateButton.addEventListener("mouseover", updateRatingStarDisplayEvt, false );
     42                }
     43                else if(window.attachEvent) { // IE
     44                        rateButton.attachEvent("onclick", updateRating);
     45                        rateButton.attachEvent("onmouseout", resetRatingStarDisplay);
     46                        rateButton.attachEvent("onmouseover", updateRatingStarDisplayEvt);
     47                }
     48        }
     49        resetRatingStarDisplay();
    4850}
    4951
    5052function resetRatingStarDisplay()
    5153{
    52   updateRatingStarDisplay( gUserRating );
     54        updateRatingStarDisplay( gUserRating );
    5355}
    5456
    5557function updateRatingStarDisplay(userRating)
    5658{
    57   for (i=0; i<gRatingButtons.length; i++)
    58   {
    59     var rateButton = gRatingButtons[i];
    60     if (userRating!=="" && userRating>=rateButton.initialRateValue )
    61     {
    62       rateButton.className = "rateButtonStarFull";
    63     }
    64     else
    65     {
    66       rateButton.className = "rateButtonStarEmpty";
    67     }
    68   }
     59        for (var i=0; i<gRatingButtons.length; i++)
     60                gRatingButtons[i].className = (userRating!=="" && userRating>=gRatingButtons[i].initialRateValue ) ? "rateButtonStarFull" : "rateButtonStarEmpty";
    6961}
    7062
    7163function updateRatingStarDisplayEvt(e)
    7264{
    73   if (e.target)
    74     updateRatingStarDisplay(e.target.initialRateValue);
    75   else //IE
    76     updateRatingStarDisplay(e.srcElement.initialRateValue);
     65        updateRatingStarDisplay(
     66                e.target ? e.target.initialRateValue : e.srcElement.initialRateValue);
    7767}
    7868
    7969function updateRating(e)
    8070{
    81   if (e.target)
    82     var rateButton = e.target;
    83   else //IE
    84     var rateButton = e.srcElement;
    85   if (rateButton.initialRateValue == gUserRating)
    86     return false; //nothing to do
    87   // some ajax here one day would be nice
    88   rateButton.value = rateButton.initialRateValue; // put back real value
    89   return true;
     71        var rateButton = e.target || e.srcElement;
     72        if (rateButton.initialRateValue == gUserRating)
     73                return false; //nothing to do
     74
     75        for (var i=0; i<gRatingButtons.length; i++) gRatingButtons[i].disabled=true;
     76        var y = new PwgWS(gRatingOptions.rootUrl);
     77        y.callService(
     78                "pwg.images.rate", {image_id: gRatingOptions.image_id, rate: rateButton.initialRateValue } ,
     79                {
     80                        onFailure: function(num, text) {
     81                                alert(num + " " + text);
     82                                document.location = rateButton.form.action + "&rate="+rateButton.initialRateValue;
     83                        },
     84                        onSuccess: function(result) {
     85                                gUserRating = rateButton.initialRateValue;
     86                                for (var i=0; i<gRatingButtons.length; i++) gRatingButtons[i].disabled=false;
     87                                if (gRatingOptions.updateRateElement) gRatingOptions.updateRateElement.innerHTML = gRatingOptions.updateRateText;
     88                                if (gRatingOptions.ratingSummaryElement)
     89                                {
     90                                        var t = gRatingOptions.ratingSummaryText;
     91                                        var args =[result.average, result.count, result.stdev], idx = 0, rexp = new RegExp( /%\.?\d*[sdf]/ );
     92                                        _xxx = t.match( rexp );
     93                                        while (idx<args.length) t=t.replace(rexp, args[idx++]);
     94                                        gRatingOptions.ratingSummaryElement.innerHTML = t;
     95                                }
     96                        }
     97                }
     98        );
     99        return false;
    90100}
  • trunk/ws.php

    r2429 r2435  
    4444      'retrieves the PWG version');
    4545
    46   $service->addMethod('pwg.caddie.add', 'ws_caddie_add', 
     46  $service->addMethod('pwg.caddie.add', 'ws_caddie_add',
    4747      array(
    4848        'image_id'=> array( 'flags'=>WS_PARAM_FORCE_ARRAY ),
     
    9595        'image_id' => array(),
    9696        'comments_page' => array('default'=>0 ),
    97         'comments_per_page' => array( 
    98               'default' => $conf['nb_comment_page'], 
     97        'comments_per_page' => array(
     98              'default' => $conf['nb_comment_page'],
    9999              'maxValue' => 2*$conf['nb_comment_page'],
    100100            ),
    101101      ),
    102102      'retrieves information about the given photo' );
     103
     104  $service->addMethod('pwg.images.rate', 'ws_images_rate',
     105      array(
     106        'image_id' => array(),
     107        'rate' =>     array(),
     108      ),
     109      'rate the image' );
    103110
    104111  $service->addMethod('pwg.images.search', 'ws_images_search',
Note: See TracChangeset for help on using the changeset viewer.