Answer.php 2.13 KB
Newer Older
Raph El's avatar
Hotfix  
Raph El committed
1 2 3 4 5 6 7 8 9 10 11 12
<?php

class Answer{
	const COLLECTION = "answers";
	const CONTROLLER = "answer";
	const ICON = "fa-calendar-check-o";

	public static function getById($id, $fields=array()){
		return PHDB::findOne( self::COLLECTION, array( "_id" => new MongoId((string)$id) ), $fields );
	}


13
	public static function generateAnswer($formId){
Raph El's avatar
Hotfix  
Raph El committed
14
		$costum = CacheHelper::getCostum();
15
		//Rest::json($costum); exit;
Raph El's avatar
Hotfix  
Raph El committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
		$ans = [
			"user" => Yii::app()->session["userId"],
			"links"=>[
				"answered" => [ Yii::app()->session["userId"] ] ,
				//proposed,particpated,decision,financed,financed,referenced,commented,worked,payed,tested,validated
				//each actor is connectable to his activitystream
			],
			"created" => time()
		];

		if(isset($costum["contextType"]) && isset($costum["contextId"])){
			$ans["parentSlug"] = Slug::getSlugByIdAndCol($costum["contextId"], $costum["contextType"]);
		}
		$formList = null ;
		if( !empty($costum["form"]) && !empty($costum["form"]["ids"]) ){
			$formList = $costum["form"]["ids"];
32
			$ans["formId"] = implode("|", $formList);
Raph El's avatar
Hotfix  
Raph El committed
33
			$ans["formList"] = count($formList);
34
			//$ans["step"] = $formList[0];
Raph El's avatar
Hotfix  
Raph El committed
35 36
		}

37 38 39 40 41 42 43 44 45
		if(Costum::isSameFunction("generateAnswerBeforeSave")){
			$paramBeforeSave=Costum::sameFunction("generateAnswerBeforeSave", 
										array("answer" => $ans, "formList"=>$formList) );
			$ans = $paramBeforeSave["answer"]; 
		}

		PHDB::insert(Answer::COLLECTION, $ans);
		$answer = PHDB::findOne( Answer::COLLECTION, [ "_id" => new MongoId($ans["_id"]) ] ) ;
		return $answer;
Raph El's avatar
Hotfix  
Raph El committed
46 47 48
		
	}

Raph El's avatar
Raph El committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

	public static function csv($elements, $idElt, $valElt, $forms ){
		$elements[$idElt]["id"] = $idElt;
		$elements[$idElt]["user"] = $valElt["user"];
		$elements[$idElt]["created"] = $valElt["created"];
		if(!empty($valElt["answers"])){
			foreach ($forms as $keyF => $valF) {
				if(!empty($valF["id"]) && !empty($valElt["answers"][$valF["id"]])){
					foreach ($valElt["answers"][$valF["id"]] as $keyAns => $valAns) {
						if(!empty($valF["inputs"]) && !empty($valF["inputs"][$keyAns]) && !empty($valF["inputs"][$keyAns]["label"])){
							$elements[$idElt][$valF["inputs"][$keyAns]["label"]] = $valAns;
						}
					}
				}
			}
		}
		return $elements;
	}

Raph El's avatar
Hotfix  
Raph El committed
68 69
}
?>