Subversion Repositories php-qbpwcf

Rev

Rev 226 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php

/*

        QBPWCF, Quick Build PHP website Component base on Fedora Linux.
    Copyright (C) 2015~2025 Min-Jhin,Chen

    This file is part of QBPWCF.

    QBPWCF is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QBPWCF is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.

*/
namespace qbpwcf;

/*
類別說明:
跟考試應用相關的類別.
備註:
無.
*/
class exam{
        
        /*
        #函式說明:
        #當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
        #回傳結果:
        #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
        #$reuslt["error"],執行不正常結束的錯訊息陣列.
        #$result["function"],當前執行的函式名稱.
        #必填參數:
        #$method,物件,為物件實體或類別名稱,會自動置入該參數.
        #$arguments,陣列,為呼叫方法時所用的參數.
        #參考資料:
        #__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
        */
        public function __call($method,$arguments){
                
                #取得當前執行的函式
                $result["function"]=__FUNCTION__;
                
                #設置執行不正常
                $result["status"]="false";
                
                #設置執行錯誤
                $result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
                
                #設置所丟入的參數
                $result["error"][]=$arguments;
                
                #回傳結果
                return $result;
                
                }#function __call end
                
        /*
        #函式說明:
        #當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
        #回傳結果:
        #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
        #$reuslt["error"],執行不正常結束的錯訊息陣列.
        #$result["function"],當前執行的函式名稱.
        #必填參數:
        #$method,物件,為物件實體或類別名稱,會自動置入該參數.
        #$arguments,陣列,為呼叫方法時所用的參數.
        #參考資料:
        #__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
        */
        public static function __callStatic($method,$arguments){
                
                #取得當前執行的函式
                $result["function"]=__FUNCTION__;
                
                #設置執行不正常
                $result["status"]="false";
                
                #設置執行錯誤
                $result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
                
                #設置所丟入的參數
                $result["error"][]=$arguments;
                
                #回傳結果
                return $result;
                
                }#function __callStatic end
        
        /*
        #函式說明:
        #根據座位表csv檔與坐者csv檔來隨機安排座位,並自動下載安排好的座位表csv檔.
        #回傳結果:
        #$result["status"],執行正常與否,"true"為正常,"false"為不正常.
        #$result["error"],錯誤訊息陣列.
        #$result["function"],當前執行的函數名稱
        #$result["content"],處理好的座位表csv檔位置.
        #必填參數:
        #$conf["seaterList"],字串,坐者的csv檔位置與名稱,單攔,每一列為坐者的代稱.
        $conf["seaterList"]="";
        #$conf["seatMap"],字串,座位表示意csv檔位置與名稱,格子內容為0代表非座位,1為座位.
        $conf["seatMap"]="";
        #$conf["outputAddress"],字串,csv檔要輸出到哪邊,不用加csv副檔名.
        $conf["outputAddress"]="";              
        #$conf["fileArgu"],字串,__FILE__的內容.
        $conf["fileArgu"]=__FILE__;
        #可省略參數:
        #$conf["emptySeatName"],字串,要空下來的座位,其代稱要用什麼?預設為empty.
        #$conf["emptySeatName"]="empty";
        #$conf["repeat"],字串,能否連續兩個位置的名稱都一樣,"false"代表不連續重複,"true"代表允許連續重複,預設為"false".
        #$conf["repeat"]="false"
        #$conf["noDownload"],字串,是否不要立馬下載處理好的坐位表csv檔,"true"代表不要要立馬下載處理好的坐位表csv檔.
        #$conf["noDownload"]="true";
        #參考資料:
        #無.
        #備註:
        #無.
        */
        public static function randomSeater(&$conf){
                
                #初始化要回傳的內容
                $result=array();

                #取得當前執行的函數名稱
                $result["function"]=__FUNCTION__;

                #如果 $conf 不為陣列
                if(gettype($conf)!="array"){
                        
                        #設置執行失敗
                        $result["status"]="false";
                        
                        #設置執行錯誤訊息
                        $result["error"][]="\$conf變數須為陣列形態";

                        #如果傳入的參數為 null
                        if($conf==null){
                                
                                #設置執行錯誤訊息
                                $result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
                                
                                }#if end

                        #回傳結果
                        return $result;
                        
                        }#if end
                
                #檢查參數
                #函式說明:
                #檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
                #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
                #$reuslt["error"],執行不正常結束的錯訊息陣列.
                #$result["function"],當前執行的函式名稱.
                #$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
                #$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
                #$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
                #$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
                #$result["argu"],字串陣列,目前輸入的參數名稱陣列.
                #$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
                #$result["notNeedVar"],字串陣列,多餘的參數名稱.
                #必填寫的參數:
                #$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
                $conf["variableCheck::checkArguments"]["varInput"]=&$conf;
                #$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
                $conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("seaterList","seatMap","outputAddress","fileArgu");
                #$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double"); 
                $conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string","string","string");
                #$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
                $conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
                #可以省略的參數:
                #$conf["variableCheck::checkArguments"]["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
                $conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
                #$conf["variableCheck::checkArguments"]["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
                $conf["variableCheck::checkArguments"]["skipableVariableName"]=array("emptySeatName","noDownload","repeat");
                #$conf["variableCheck::checkArguments"]["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
                $conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string");
                #$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
                $conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("empty","true","false");
                #$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
                #$conf["arrayCountEqualCheck"][]=array();
                #參考資料來源:
                #array_keys=>http://php.net/manual/en/function.array-keys.php
                $checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
                unset($conf["variableCheck::checkArguments"]);
                
                #如果 $checkResult["status"] 等於 "false"
                if($checkResult["status"]=="false"){
                        
                        #設置執行錯誤
                        $result["status"]="false";
                        
                        #設置錯誤訊息
                        $result["error"]=$checkResult;
                        
                        #回傳結果
                        return $result;
                        
                        }#if end
                        
                #如果 $checkResult["passed"] 等於 "false"
                if($checkResult["passed"]=="false"){
                        
                        #設置執行錯誤
                        $result["status"]="false";
                        
                        #設置錯誤訊息
                        $result["error"]=$checkResult;
                        
                        #回傳結果
                        return $result;
                        
                        }#if end
                                                
                #讀座位表csv檔
                #涵式說明:
                #讀取csv檔案的內容
                #回傳得結果:
                #$result["status"],執行是否正常,"true"代表正常;"false"代表不正常.
                #$result["error"],錯誤訊息陣列.
                #$result["csvFileContent"],csv檔案內容的陣列
                        #$result["csvFileContent"]["$i"]["$j"]代表第$i+1列第$j+1欄位的內容.
                        #如果 $conf["firstLineVarName"]爲"true"的話則會回傳 $result["csvFileContent"]["第幾列"]["第一列各個欄位的名稱"]
                #$result["lineArray"],csv檔案逐列內容的陣列.
                #$result["lineArrayCount"],總共幾列資料
                #必填的參數:
                $conf["csv"]["loadCsvFile"]["csvFilePositionAndName"]=$conf["seatMap"];#csv檔案的位置
                $conf["csv"]["loadCsvFile"]["fileArgu"]=$conf["fileArgu"];
                #可省略的參數:
                $conf["csv"]["loadCsvFile"]["firstLineVarName"]="false";#第一行是否爲變數名稱(各個欄位的名稱)
                #參考資料來源:
                #http://www.php.net/manual/en/function.str-split.php
                #參考資料來源
                #http://chuangmaster.pixnet.net/blog/post/33560606-%5B%E8%BD%89%E8%B2%BC%5D%E9%97%9C%E6%96%BC%E5%9B%9E%E8%BB%8A(%5Cr)%E8%88%87%E6%8F%9B%E8%A1%8C(%5Cn)
                $seatMap=csv::loadCsvFile($conf["csv"]["loadCsvFile"]);
                unset($conf["csv"]["loadCsvFile"]);
                
                #如果 $seatMap["status"] 等於 "false"
                if($seatMap["status"]=="false"){

                        #設置執行錯誤
                        $result["status"]="false";
                        
                        #設置錯誤訊息
                        $result["error"]=$seatMap;
                        
                        #回傳結果
                        return $result;
                        
                        }#if end
                        
                #讀取坐者csv檔
                #涵式說明:
                #讀取csv檔案的內容
                #回傳得結果:
                #$result["status"],執行是否正常,"true"代表正常;"false"代表不正常.
                #$result["error"],錯誤訊息陣列.
                #$result["csvFileContent"],csv檔案內容的陣列
                        #$result["csvFileContent"]["$i"]["$j"]代表第$i+1列第$j+1欄位的內容.
                        #如果 $conf["firstLineVarName"]爲"true"的話則會回傳 $result["csvFileContent"]["第幾列"]["第一列各個欄位的名稱"]
                #$result["lineArray"],csv檔案逐列內容的陣列.
                #$result["lineArrayCount"],總共幾列資料
                #必填的參數:
                $conf["csv"]["loadCsvFile"]["csvFilePositionAndName"]=$conf["seaterList"];#csv檔案的位置
                $conf["csv"]["loadCsvFile"]["fileArgu"]=$conf["fileArgu"];
                #可省略的參數:
                $conf["csv"]["loadCsvFile"]["firstLineVarName"]="false";#第一行是否爲變數名稱(各個欄位的名稱)
                #參考資料來源:
                #http://www.php.net/manual/en/function.str-split.php
                #參考資料來源
                #http://chuangmaster.pixnet.net/blog/post/33560606-%5B%E8%BD%89%E8%B2%BC%5D%E9%97%9C%E6%96%BC%E5%9B%9E%E8%BB%8A(%5Cr)%E8%88%87%E6%8F%9B%E8%A1%8C(%5Cn)
                $seaterList=csv::loadCsvFile($conf["csv"]["loadCsvFile"]);
                unset($conf["csv"]["loadCsvFile"]);
                
                #如果 $seaterList["status"] 等於 "false"
                if($seaterList["status"]=="false"){

                        #設置執行錯誤
                        $result["status"]="false";
                        
                        #設置錯誤訊息
                        $result["error"]=$seaterList;
                        
                        #回傳結果
                        return $result;
                        
                        }#if end
                        
                #取得坐者的數量
                $seaterCount=$seaterList["lineArrayCount"];
                
                #取得座位表的空位數
                #函式說明:
                #計屬csv檔裡面的關鍵字.
                #回傳的結果:
                #$result["status"],執行正常與否,"true"為正常,"false"為不正常.
                #$result["error"],錯誤訊息陣列.
                #$result["content"],找到的關鍵字數量.
                #必填的參數:
                #$conf["csvFileAddress"],字串,csv檔案的位置與名稱.
                $conf["csv"]["keyWordsCount"]["csvFileAddress"]=$conf["seatMap"];
                #$conf["keyWord"],字串,要尋找的關鍵字.
                $conf["csv"]["keyWordsCount"]["keyWord"]="1";
                $conf["csv"]["keyWordsCount"]["fileArgu"]=$conf["fileArgu"];
                #可省略的參數: 
                $conf["csv"]["keyWordsCount"]["firstLineVarName"]="false";#第一行是否爲變數名稱(各個欄位的名稱),"true"表示第一列不在搜尋範圍裡.
                $seatCount=csv::keyWordsCount($conf["csv"]["keyWordsCount"]);
                unset($conf["csv"]["keyWordsCount"]);
                
                #如果 $seatCount["status"] 等於 "false" 
                if($seatCount["status"]=="false"){
                        
                        #設置執行錯誤
                        $result["status"]="false";
                        
                        #設置錯誤訊息
                        $result["error"]=$seatCount;
                        
                        #回傳結果
                        return $result;
                        
                        }#if end
                        
                #如果空座位數小於坐者
                if($seatCount["content"]<$seaterCount){
                        
                        #設置執行錯誤
                        $result["status"]="false";
                        
                        #設置錯誤訊息
                        $result["error"][]="座位數量不足,至少需要".$seaterCount."個座位";
                        
                        #回傳結果
                        return $result;
                        
                        }#if end
                        
                #初始化儲存坐者清單的一維陣列
                $seaterListArray=array();
                
                #根據坐者csv檔清單裡每列內容
                foreach($seaterList["csvFileContent"] as $row){
                        
                        #根據每列的欄元素
                        foreach($row as $col){
                        
                                #取得每個元素內容
                                $seaterListArray[]=$col;
                        
                                }#foreach end
                        
                        }#foreach end
                
                #取得座位表跟坐者的差
                $range=$seatCount["content"]-$seaterCount;      
                        
                #將坐者清單加上代表沒人坐的名單,讓其數量跟座位數一樣
                #缺幾個名單就執行幾次
                for($i=0;$i<$range;$i++){
                        
                        #將沒有要坐的名稱設為$conf["emptySeatName"]
                        $seaterListArray[]=$conf["emptySeatName"];
                        
                        }#for end
                
                #根據座位表csv檔裡每列內容
                foreach($seatMap["csvFileContent"] as $rowKey=>$rowValue){
                        
                        #根據每列的欄元素
                        foreach($rowValue as $colKey=>$colValue){
                                
                                #初始化沒有上一位坐者
                                $lastSeat=null;
                                
                                #如果該地方是座位(等於1)
                                if($colValue=="1"){
                                        
                                        #隨機安排坐者                                             
                                        #函式說明: 
                                        #亂數產生特定陣列裡面的內容
                                        #回傳的結果:
                                        #$result["status"],執行正常與否,"true"為正常,"false"為不正常.
                                        #$result["error"],錯誤訊息陣列.
                                        #$result["content"]["name"],產生的元素在$conf["randomArray"]裡面的key是?
                                        #$result["content"]["value"],產生的元素內容.
                                        #必填參數:
                                        #$conf["randomArray"],字串陣列,要隨機產生的元素.
                                        $conf["math"]["randomFromArray"]["randomArray"]=$seaterListArray;
                                        #可省略參數:
                                        #無
                                        $randomResult=math::randomFromArray($conf["math"]["randomFromArray"]);
                                        unset($conf["math"]["randomFromArray"]);
                                        
                                        #如果 $randomResult["status"] 等於 "false" 
                                        if($randomResult["status"]=="false"){
                                                
                                                #設置執行錯誤
                                                $result["status"]="false";
                                                
                                                #設置錯誤訊息
                                                $result["error"]=$randomResult;
                                                
                                                #回傳結果
                                                return $result;
                                                
                                                }#if end
                                        
                                        #如果座位不能重複
                                        if($conf["repeat"]=="false"){
                                                
                                                #如果上一位坐者不為 null
                                                if($lastSeat!=null){
                                                        
                                                        #如果抽出來的坐者是連續的
                                                        while($randomResult["content"]["value"]==$lastSeat){
                                                                
                                                                #若這是最後一個坐者了
                                                                if(count($seaterListArray)==1){
                                                                        
                                                                        #跳出
                                                                        break;
                                                                        
                                                                        }#if end
                                                                
                                                                #初始化是否要繼續執行的辨別變數
                                                                $continue="false";
                                                                
                                                                #若剩下坐者全部跟 $lastSeat(上一個坐者) 一樣
                                                                foreach($seaterListArray as $seaterName){
                                                                        
                                                                        #如果剩下的坐者有跟 $lastSeat(上一個坐者) 不一樣
                                                                        if($seaterName!=$lastSeat){
                                                                                
                                                                                #設置繼續執行
                                                                                $continue="true";
                                                                                
                                                                                #跳出 foreach
                                                                                break;
                                                                                
                                                                                }#if end
                                                                        
                                                                        }#foreach end
                                                                        
                                                                #如果不繼續執行
                                                                if($continue=="false"){
                                                                        
                                                                        #跳出 while
                                                                        break;
                                                                        
                                                                        }#if end
                                                                
                                                                #隨機安排坐者                                             
                                                                #函式說明: 
                                                                #亂數產生特定陣列裡面的內容
                                                                #回傳的結果:
                                                                #$result["status"],執行正常與否,"true"為正常,"false"為不正常.
                                                                #$result["error"],錯誤訊息陣列.
                                                                #$result["content"]["name"],產生的元素在$conf["randomArray"]裡面的key是?
                                                                #$result["content"]["value"],產生的元素內容.
                                                                #必填參數:
                                                                #$conf["randomArray"],字串陣列,要隨機產生的元素.
                                                                $conf["math"]["randomFromArray"]["randomArray"]=$seaterListArray;
                                                                #可省略參數:
                                                                #無
                                                                $randomResult=math::randomFromArray($conf["math"]["randomFromArray"]);
                                                                unset($conf["math"]["randomFromArray"]);
                                                                
                                                                #如果 $randomResult["status"] 等於 "false" 
                                                                if($randomResult["status"]=="false"){
                                                                        
                                                                        #設置執行錯誤
                                                                        $result["status"]="false";
                                                                        
                                                                        #設置錯誤訊息
                                                                        $result["error"]=$randomResult;
                                                                        
                                                                        #回傳結果
                                                                        return $result;
                                                                        
                                                                        }#if end
                                                                
                                                                }#while end
                                                        
                                                        }#if end
                                                
                                                }#if end
                                                                                        
                                        #將其欄位內容置換為坐者代稱
                                        $seatMap["csvFileContent"][$rowKey][$colKey]=$randomResult["content"]["value"];
                                        
                                        #涵式說明:
                                        #將陣列中特定元素剔除,並重新按照順序排序
                                        #回傳的結果:
                                        #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
                                        #$result["error"],錯誤訊息
                                        #$result["function"],當前執行的函數
                                        #$result["content"]["byName"],剔除掉特定元素的陣列,使用原來陣列的key來儲存.
                                        #$result["content"]["byNumber"],剔除掉特定元素的陣列,使用從0開始的整數key來儲存.
                                        #必填的參數:
                                        $conf["arrays"]["eraseElement"]["rawInputArray"]=$seaterListArray;#要處理的原始數字陣列
                                        $conf["arrays"]["eraseElement"]["eraseElementKey"]=(string)$randomResult["content"]["key"];#要移除的元素key值  
                                        $eraseElement=arrays::eraseElement($conf["arrays"]["eraseElement"]);
                                        unset($conf["arrays"]["eraseElement"]);
                                        
                                        #如果移除元素失敗
                                        if($eraseElement["status"]=="false"){
                                                
                                                #設置執行錯誤
                                                $result["status"]="false";
                                                
                                                #設置錯誤訊息
                                                $result["error"]=$eraseElement;
                                                
                                                #回傳結果
                                                return $result;
                                                
                                                }#if end
                                        
                                        }#if end
                                        
                                #反之為 0(不是位置)
                                else{
                                        
                                        #設為沒有上一位坐者
                                        $lastSeat=null;
                                        
                                        }#else end
                                
                                }#foreach end
                                        
                        }#foreach end
        
                #輸出排好的座位表csv檔
                #涵式說明:
                #快速建立一個csv檔案,並且要求使用者下載。
                #回傳的結果:
                #$result["status"],執行是否成功,"true"代表成功,"false"代表失敗.
                #$result["error"],錯誤訊息陣列.
                #$result["content"],輸出的csv檔案位置.
                #如果有設置$conf["noDownload"]爲"true"的話,則會回傳檔案的位置
                #必填的參數:
                $conf["csv"]["create"]["dataArray"]=$seatMap["csvFileContent"];#要寫入的文字,
                        #$conf["dataArray"][$i],代表第 $i 行的內容,
                        #$conf["dataArray"][$i][$j],代表第 $i 第 $j個欄位的內容.
                $conf["csv"]["create"]["fileArgu"]=$conf["fileArgu"];
                #可省略的參數:
                $conf["csv"]["create"]["csvFilePathAndName"]=$conf["outputAddress"];#輸出的csv檔案路徑與名稱,預設爲系統時間,副檔名"csv"會自動加上.
                
                #如果 $conf["noDownload"] 存在
                if(isset($conf["noDownload"])){
                        
                        #如果 $conf["noDownload"] 等於 "true"
                        if($conf["noDownload"]=="true"){
                                
                                #設為 "true"
                                $conf["csv"]["create"]["noDownload"]="true";
                                
                                }#if end
                        
                        }#if end
                
                #參考資料來源:
                #http://php.net/manual/en/function.str-split.php
                $createCsvResult=csv::create($conf["csv"]["create"]);
                unset($conf["csv"]["create"]);
                        
                #如果 $createCsvResult["status"] 等於 "false"
                if($createCsvResult["status"]=="false"){
                        
                        #設置執行錯誤
                        $result["status"]="false";
                        
                        #設置錯誤訊息
                        $result["error"]=$createCsvResult;
                        
                        #回傳結果
                        return $result;
                        
                        }#if end
                        
                #設置執行正常
                $result["status"]="true";
                
                #設置建立的csv檔案位置
                $result["content"]=$createCsvResult["content"];
                
                #回傳結果
                return $result;
                                                                
                }#funtion randomSeater end
        
        }#class exam end

?>