Plugin Directory

Changeset 1988370


Ignore:
Timestamp:
12/07/2018 10:44:18 PM (7 years ago)
Author:
Levdbas
Message:
  • Fixed widget circle percentage when multiple times hours were added
  • Fixed total time issue in the widget output.
Location:
support-hours
Files:
40 added
6 edited

Legend:

Unmodified
Added
Removed
  • support-hours/trunk/README.txt

    r1975615 r1988370  
    44Requires at least: 4.6
    55Tested up to: 5.0
    6 Stable tag: 1.5.3
     6Stable tag: 1.5.4
    77Requires PHP: 5.6
    88License: GPLv2 or later
     
    4646
    4747== Changelog ==
     48
     49= 1.5.4 =
     50* Fixed widget circle percentage when multiple times hours were added
     51* Fixed total time issue in the widget output.
    4852
    4953= 1.5.3 =
  • support-hours/trunk/admin/partials/common/bottom-message.php

    r1960674 r1988370  
    1818  endif;
    1919  if ($pagenow == 'index.php'):
    20     $percentage = percentage($used_minutes, $bought_minutes);
     20    $percentage = percentage($workFields, $used_minutes, $bought_minutes);
    2121    ?>
    2222    <p>
  • support-hours/trunk/admin/partials/common/worktable.php

    r1975615 r1988370  
    2121      <tbody>
    2222        <?php
     23        $widget_fields = $workFields;
    2324        if ($pagenow == 'index.php'):
    24           $workFields = array_slice($workFields, -5);
     25          $widget_fields = array_slice($workFields, -5);
    2526        endif;
    26         foreach ( $workFields as $field ) { ?>
     27        foreach ( $widget_fields as $field ) { ?>
    2728          <tr>
    2829            <td><?php if(!empty($field['used'])) echo $field['date'] ?></td>
  • support-hours/trunk/admin/partials/widgets/widget-clock.php

    r1975615 r1988370  
    1 <div class="progress-bar position <?php echo $current_color;?>" data-percent="<?php echo percentage($used_minutes, $bought_minutes); ?>">
     1<div class="progress-bar position <?php echo $current_color;?>" data-percent="<?php echo percentage($workFields, $used_minutes, $bought_minutes); ?>">
    22  <div class="progress-bar__background">
    33    <div class="progress-bar__rotate"></div>
  • support-hours/trunk/admin/support-hours-admin-functions.php

    r1968512 r1988370  
    1515// NOTE: temp function to transform first set of hours to new hour system.
    1616function transform_fixed_time($static_time, $workFields, $name, $options){
    17   if($options['bought_hours'] != '00:00'){
    18     echo "<h3>". __('Update of Support Hours complete, please refresh page', $name)."</h3>";
    19     $newdata =  array (
    20       'date' => date('d-m-Y'),
    21       'description' => __('First bought time - added by Support Hours', $name),
    22       'used' => $static_time,
    23       'type' => 'time-added'
    24     );
    25     array_unshift($options['workFields'], $newdata);
    26     //$options = array_diff_key($options, ['bought_hours' => "xy"]);
    27 
    28     $options['workFields'] = array_map(function($arr){
    29       return $arr + ['type' =>'time-used'];
    30     }, $options['workFields']);
    31 
    32     update_option($name, $options);
    33   }
     17if($options['bought_hours'] != '00:00'){
     18echo "<h3>". __('Update of Support Hours complete, please refresh page', $name)."</h3>";
     19$newdata =  array (
     20'date' => date('d-m-Y'),
     21'description' => __('First bought time - added by Support Hours', $name),
     22'used' => $static_time,
     23'type' => 'time-added'
     24);
     25array_unshift($options['workFields'], $newdata);
     26//$options = array_diff_key($options, ['bought_hours' => "xy"]);
     27
     28$options['workFields'] = array_map(function($arr){
     29return $arr + ['type' =>'time-used'];
     30}, $options['workFields']);
     31
     32update_option($name, $options);
     33}
    3434}
    3535echo transform_fixed_time($static_time, $workFields, $name, $options);
    3636*/
    3737
     38/**
     39 * Transforms $minutes to HH:MM
     40 * @since
     41 * @param  int $minutes minutes, can be bought or used
     42 * @return string          HH:MM
     43 */
    3844function minutestoTime($minutes){
    3945  return sprintf("%02d:%02d", floor($minutes/60), $minutes%60);  // 01:37
    4046}
    41 // function to strip the hours displayed in the clock of minutes if the hour is round.
     47
     48/**
     49 * function to strip the hours displayed in the clock of minutes if the hour is round.
     50 * @since
     51 * @param  [type] $minutes minutes, can be bought or used
     52 * @return string Retunrs full hour without zeros
     53 */
    4254function minutestoTimeRound($minutes) {
    43   if($minutes %60 == 0):
     55  if($minutes % 60 == 0):
    4456    $time = $minutes/60;
    4557  else:
     
    4961}
    5062
    51 /*
    52 Checks the workfields for the time fields. Adds all timefields and returns them.
    53 If no workfields and therefore no time fields are filled, returns 00:00
    54 */
    55 
     63
     64
     65/**
     66 * Checks the workfields array for the time fields. Adds all timefields and returns them.
     67 * If no workfields and therefore no time fields are filled, returns 00:00
     68 * @since   1.4
     69 * @param   array   $workFields   The workfields from this plugin, contains bought and used hours
     70 * @param   string  $type         can be used or bought
     71 * @param   string  $returns      if minutes returns total minutes
     72 * @return  string                Returns full hours format or total minutes of used or bought hours
     73 */
    5674function AddTime($workFields, $type, $returns = null) {
     75  $output = 0;
     76  $minutes = 0;
    5777  if($workFields != null){
    58     $minutes = 0;
    5978    if(isset($workFields[0]['type'])):
    6079      $workFields = array_filter($workFields, function ($var) use ($type) {
    6180        return ($var['type'] == $type);
    6281      });
    63         foreach ($workFields as $time) {
     82      foreach ($workFields as $time) {
    6483        //  Check if the field is not empty. Else stay with 0.
    6584        if($time['type'] !== ""){
     
    7089      }
    7190      if($returns == 'minutes'):
    72         return $minutes;
     91        $output = $minutes;
    7392      else:
    7493        $hours = floor($minutes / 60);
    7594        $minutes -= $hours * 60;
    7695        $time = sprintf('%02d:%02d', $hours, $minutes);
    77         return $time;
     96        $output = $time;
    7897      endif;
    7998    endif;
    80   } else{
    81     return "0";
    82   }
    83 }
    84 
     99  }
     100  return $output;
     101}
     102
     103
     104
     105/**
     106 * Searches our $workFields array for the last bought hours.
     107 * @since
     108 * @param  array   $workFields      The workfields from this plugin, contains bought and used hours
     109 * @return int                      Last bought hours in minute format
     110 */
    85111function last_bought($workFields){
    86112  $minutes = 0;
    87113  if($workFields != null){
    88     $workFields = array_reverse($workFields);
    89     $key = array_search('time-added', array_column($workFields, 'type'));
    90 
    91     if($key !== false):
    92       $time = $workFields[$key]['used'];
    93       list($hour, $minute) = explode(':', $time);
    94       $minutes += $hour * 60;
    95       $minutes += $minute;
    96       return $minutes;
    97     else:
    98       return 0;
     114    $workFields = array_reverse($workFields); // invert array so we can search DESC
     115    $key = array_search('time-added', array_column($workFields, 'type')); // searching for the last time-added value in key 'type'
     116
     117    if($key !== false): // if there is a key!
     118      $time = $workFields[$key]['used']; // take our time in HH:MM format from the field
     119      list($hour, $minute) = explode(':', $time); // explode the time
     120      $minutes += $hour * 60; // hours to minutes, add them to minutes
     121      $minutes += $minute; // add minutes to total as well.
    99122    endif;
    100   }
    101   return 0;
    102 }
     123   
     124  }
     125  return $minutes;
     126}
     127
     128
     129
     130/**
     131 * To not overcluther the interface we only show the time you actually care about in the widget.
     132 * If multiple time hours are bought, and most of it was spent, we only want to see the
     133 * last bought hours and the hours left calculated against that.
     134 * This function is used to return all used minutes minus all the minutes prior to the last bought hours
     135 * so we can display the used hours correctly. Used in percentage() and widget_output()
     136 * @since
     137 * @param  array   $workFields      The workfields from this plugin, contains bought and used hours
     138 * @param  int     $bought_minutes  Total bought minutes
     139 * @return int                      minutes
     140 */
    103141function bought_minus_last($workFields, $bought_minutes){
     142  $output = 0;
     143
    104144  if($workFields != null){
    105     return $bought_minutes - last_bought($workFields);
    106   } else{
    107     return 0;
    108   }
    109 }
    110 
     145    $output = $bought_minutes - last_bought($workFields);
     146  }
     147
     148  return $output;
     149}
     150
     151
     152
     153
     154/**
     155 * This function is being used to display the hours in the widget.
     156 * Output depends on time spent, time bought and time bought the last time.
     157 * @since 1.4
     158 * @param  array  $workFields     The workfields from this plugin, contains bought and used hours
     159 * @param  int    $used_minutes   Total used minutes
     160 * @param  int    $bought_minutes Total bought minutes
     161 * @return string                 Returns hours in HH:MM format or HH format where the first is used time, the second the bought time.
     162 */
    111163function widget_output($workFields, $used_minutes, $bought_minutes){
    112164  $bought_minus_last = bought_minus_last($workFields, $bought_minutes);
    113165  $minutes_to_spent = $bought_minutes - $used_minutes;
    114166  $last_bought_minutes = last_bought($workFields);
    115   // NOTE: calculates spent hours
     167
     168  // NOTE: calculates used hours
    116169  if($used_minutes > $bought_minus_last){
    117170    $used_minutes = $used_minutes - $bought_minus_last;
     
    119172    $used_minutes = $used_minutes;
    120173  }
    121   // NOTE: returns hours left
     174
     175  // NOTE: calculates bought hours
    122176  if($minutes_to_spent < $last_bought_minutes){
    123177    $bought_minutes = $last_bought_minutes;
     
    125179    $bought_minutes = $bought_minutes;
    126180  }
     181
    127182  $widget_hours = minutestoTimeRound($used_minutes).' / '.minutestoTimeRound($bought_minutes);
    128183  return $widget_hours;
    129184}
    130185
    131 function percentage($used_minutes, $bought_minutes){
    132 
    133   if($used_minutes > $bought_minutes){
    134     $used_minutes = $bought_minutes;
    135   }
    136   if(!empty($bought_minutes)){
    137     $percentage = $used_minutes * 100 / $bought_minutes;
    138     if($percentage > 100){
    139       $percentage = 100;
    140     }
    141     $percentage = round($percentage);
    142     return $percentage;
    143   }
    144 }
    145 
    146 
    147 // function to control the font size
     186
     187
     188
     189/**
     190 * used for calculating the hours left in the notices
     191 * and in the widget to animate the circle.
     192 * @since
     193 * @param  array  $workFields     The workfields from this plugin, contains bought and used hours
     194 * @param  int    $used_minutes   Total used minutes
     195 * @param  int    $bought_minutes Total bought minutes
     196 * @return int                    Calculated percentage
     197 */
     198function percentage($workFields, $used_minutes, $bought_minutes){
     199
     200  $bought_minus_last = bought_minus_last($workFields, $bought_minutes);
     201  $minutes_to_spent = $bought_minutes - $used_minutes;
     202  $last_bought_minutes = last_bought($workFields);
     203
     204  // NOTE: calculates used hours
     205  if($used_minutes > $bought_minus_last){
     206    $used_minutes = $used_minutes - $bought_minus_last;
     207  } else{
     208    $used_minutes = $used_minutes;
     209  }
     210
     211  // NOTE: calculates bought hours
     212  if($minutes_to_spent < $last_bought_minutes){
     213    $bought_minutes = $last_bought_minutes;
     214  } else{
     215    $bought_minutes = $bought_minutes;
     216  }
     217
     218  $percentage = $used_minutes * 100 / $bought_minutes;
     219  if($percentage > 100){
     220    $percentage = 100;
     221  }
     222  $percentage = round($percentage);
     223  return $percentage;
     224
     225}
     226
     227
     228
     229/**
     230 * Control the font size of the time in the widget
     231 * big font size if time doesn't include :00 (minutes)
     232 * and bought hours are less then 99 hours. Otherwise UI will be too cluttered.
     233 * @since
     234 * @param  int    $used_minutes   Total used minutes
     235 * @param  int    $bought_minutes Total bought minutes
     236 * @return string                 small or big, this is used in our css to display the correct font size
     237 */
    148238function font_size($used_minutes, $bought_minutes){
    149   // big font size if time doesn't include :00 (minutes) and bought hours are less then 99 hours. Otherwise UI will be too cluttered.
     239  $size = 'small';
     240
    150241  if($used_minutes %60 == 0 && $bought_minutes < 5940):
    151242    $size = 'big';
    152   else:
    153     $size = 'small';
    154243  endif;
     244
    155245  return $size;
    156246}
  • support-hours/trunk/support-hours.php

    r1975615 r1988370  
    1717 * Plugin URI:        http://basedonline.nl
    1818 * Description:       Use Support hours to give yourself and your clients insights on the status of pre-paid work.
    19  * Version:           1.5.3
     19 * Version:           1.5.4
    2020 * Author:            Erik van der Bas
    2121 * Author URI:        https://basedonline.nl
Note: See TracChangeset for help on using the changeset viewer.