Browse Source

Merge pull request #818 from visuddhinanda/get-channel-progress-img

句子完成度支持按照百分比计算
visuddhinanda 4 years ago
parent
commit
46ad7e9817
1 changed files with 114 additions and 39 deletions
  1. 114 39
      app/Http/Controllers/SentenceInfoController.php

+ 114 - 39
app/Http/Controllers/SentenceInfoController.php

@@ -3,6 +3,8 @@
 namespace App\Http\Controllers;
 namespace App\Http\Controllers;
 
 
 use App\Models\Sentence;
 use App\Models\Sentence;
+use App\Models\PaliSentence;
+
 use Illuminate\Http\Request;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\DB;
@@ -40,37 +42,111 @@ class SentenceInfoController extends Controller
         //
         //
     }
     }
 
 
+    private function getSentProgress(Request $request,$date=''){
+        $channel = $request->get('channel');
+        $from = $request->get('from');
+        $to = $request->get('to');
+
+        #默认完成度显示字符数
+        # strlen 
+        # page
+        # percent 
+        $view = 'strlen';
+        if(!empty($request->get('view'))){
+            $view =$request->get('view');
+        }
+        if(!empty($request->get('type'))){
+            $view =$request->get('type');
+        }
+        #一页书中的字符数
+        $pageStrLen = 2000;
+        if(!empty($request->get('strlen'))){
+            $pageStrLen =$request->get('strlen');
+        }
+        if(!empty($request->get('pagelen'))){
+            $pageStrLen =$request->get('pagelen');
+        }
+
+        # 页数
+        $pageNumber = 300;
+        if(!empty($request->get('pages'))){
+            $pageNumber =$request->get('pages');
+        }
+
+        $db = Sentence::where('channel_uid',$request->get('channel'))
+                ->where('book_id','>=',$request->get('book'))
+                ->where('paragraph','>=',$request->get('from'))
+                ->where('paragraph','<=',$request->get('to'));
+        if(!empty($date)){
+            $db = $db->whereDate('created_at','=',$date);
+        }
+        $strlen =$db->sum('strlen');
+        #计算已完成百分比
+        $percent = 0;
+        if(($view==='page' && !empty($request->get('pages'))) || $view==='percent' ){
+            #计算完成的句子在巴利语句子表中的字符串长度百分比
+            $db = Sentence::select(['paragraph','word_start'])
+                ->where('channel_uid',$request->get('channel'))
+                ->where('book_id','>=',$request->get('book'))
+                ->where('paragraph','>=',$request->get('from'))
+                ->where('paragraph','<=',$request->get('to'));
+            if(!empty($date)){
+                $db = $db->whereDate('created_at','=',$date);
+            }
+            $sentFinished = $db->get();
+            #查询这些句子的总共等效巴利语字符数
+            $allStrLen = PaliSentence::where('book',$request->get('book'))
+                            ->where('paragraph','>=',$request->get('from'))
+                            ->where('paragraph','<=',$request->get('to'))
+                            ->sum('length');            
+            $para_strlen = 0;
+            foreach ($sentFinished as $sent) {
+                # code...
+                $para_strlen += PaliSentence::where('book',$request->get('book'))
+                            ->where('paragraph',$sent->paragraph)
+                            ->where('word_begin',$sent->word_start)
+                            ->value('length');
+            }
+
+            $percent = $para_strlen / $allStrLen;
+        }
+        switch ($view) {
+            case 'page':
+                # 输出已经完成的页数
+                if(!empty($request->get('pages'))){
+                    #给了页码,用百分比计算
+                    $resulte = $percent * $request->get('pages');
+                }else{
+                    #没给页码,用每页字符数计算
+                    $resulte = $strlen / $pageStrLen;
+                }
+                break;
+            case 'percent':
+                $resulte = $percent;
+                break;
+            case 'strlen':
+            default:
+                # code...
+                $resulte = $strlen;
+                break;
+        }
+        #保留小数点后两位
+        $resulte = sprintf('%.2f',$resulte);
+        return $resulte;
+    }
     /**
     /**
+    * 输出一张图片显示进度
      * Display the specified resource.
      * Display the specified resource.
      *
      *
      * @param  \App\Models\Sentence  $sentence
      * @param  \App\Models\Sentence  $sentence
      * @return \Illuminate\Http\Response
      * @return \Illuminate\Http\Response
-     * http://127.0.0.1:8000/api/sentence/progress/image?channel=00ae2c48-c204-4082-ae79-79ba2740d506&&book=168&from=916&to=926&type=page
+     * http://127.0.0.1:8000/api/sentence/progress/image?channel=00ae2c48-c204-4082-ae79-79ba2740d506&&book=168&from=916&to=926&view=page
      */
      */
     public function showprogress(Request $request)
     public function showprogress(Request $request)
     {
     {
         ob_clean();
         ob_clean();
         ob_start();
         ob_start();
-        $channel = $request->get('channel');
-        $from = $request->get('from');
-        $to = $request->get('to');
-
-        $strlen = Sentence::where('channel_uid',$request->get('channel'))
-                ->where('book_id','>=',$request->get('book'))
-                ->where('paragraph','>=',$request->get('from'))
-                ->where('paragraph','<=',$request->get('to'))
-                ->sum('strlen');
-        if($request->get('type')==='page'){
-            if(empty($request->get('strlen'))){
-                $pageStrLen = 500;
-            }else{
-                $pageStrLen = $request->get('strlen');
-            }
-            $resulte = $strlen / $pageStrLen;
-        }else{
-            $resulte = $strlen;
-        }
-
+        $resulte = $this->getSentProgress($request);
         $img = imagecreate(strlen($resulte)*10,22) or die('create image fail ');
         $img = imagecreate(strlen($resulte)*10,22) or die('create image fail ');
         imagecolorallocate($img,255,255,255);
         imagecolorallocate($img,255,255,255);
         $color = imagecolorallocate($img,0,0,0);
         $color = imagecolorallocate($img,0,0,0);
@@ -84,7 +160,7 @@ class SentenceInfoController extends Controller
         ]);
         ]);
     }
     }
 
 
-    //http://127.0.0.1:8000/api/sentence/progress/daily/image?channel=00ae2c48-c204-4082-ae79-79ba2740d506&&book=168&from=916&to=926&type=page
+    //http://127.0.0.1:8000/api/sentence/progress/daily/image?channel=00ae2c48-c204-4082-ae79-79ba2740d506&&book=168&from=916&to=926&view=page
     public function showprogressdaily(Request $request)
     public function showprogressdaily(Request $request)
     {
     {
         $imgWidth = 300;
         $imgWidth = 300;
@@ -95,7 +171,19 @@ class SentenceInfoController extends Controller
         $maxPage = 20;
         $maxPage = 20;
         $yLineSpace = 5;
         $yLineSpace = 5;
 
 
-        $pageStrLen = 500;
+        #默认完成度显示字符数
+        # strlen 
+        # page
+        # percent 
+        $view = 'strlen';
+        if(!empty($request->get('view'))){
+            $view =$request->get('view');
+        }
+        if(!empty($request->get('type'))){
+            $view =$request->get('type');
+        }
+
+
 
 
         $pagePix = ($imgHeight-$xAxisOffset)/$maxPage;
         $pagePix = ($imgHeight-$xAxisOffset)/$maxPage;
         $dayPix = ($imgWidth-$yAxisOffset)/$maxDay;
         $dayPix = ($imgWidth-$yAxisOffset)/$maxDay;
@@ -137,24 +225,11 @@ class SentenceInfoController extends Controller
         }
         }
 
 
         $last=0;
         $last=0;
-        for($i = 0; $i < $maxDay; $i++){
+        for($i = 1; $i <= $maxDay; $i++){
             $day = strtotime("today -{$i} day");
             $day = strtotime("today -{$i} day");
             $date = date("Y-m-d",$day);
             $date = date("Y-m-d",$day);
-            $strlen = Sentence::where('channel_uid',$request->get('channel'))
-                    ->where('book_id','>=',$request->get('book'))
-                    ->where('paragraph','>=',$request->get('from'))
-                    ->where('paragraph','<=',$request->get('to'))
-                    ->whereDate('created_at','=',$date)
-                    ->sum('strlen');
-            if($request->get('type')==='page'){
-                if(!empty($request->get('strlen'))){
-                    $pageStrLen = $request->get('strlen');
-                }
-                $resulte = $strlen / $pageStrLen * $pagePix;
-            }else{
-                $resulte = $strlen / 50;
-            }
-            
+
+            $resulte = $this->getSentProgress($request,$date)*$pagePix;
             if($i>0){
             if($i>0){
                 imageline($img,($imgWidth-$i*$dayPix),$imgHeight-$xAxisOffset-$resulte,($imgWidth-($i-1)*$dayPix),$imgHeight-$xAxisOffset-$last,$dataLineColor);
                 imageline($img,($imgWidth-$i*$dayPix),$imgHeight-$xAxisOffset-$resulte,($imgWidth-($i-1)*$dayPix),$imgHeight-$xAxisOffset-$last,$dataLineColor);
             }
             }