Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class BookSelectionController{
- private $books;
- private $difficulty;
- private $rules = array();
- public function __construct(array $books){
- $this->books = $books;
- }
- public function setDifficulty($diff){
- $this->difficulty = $diff;
- }
- private function getRules(){
- $manager = new FileDBManager();
- $db = $manager->table("pickrules");
- $pickrules = $db->select(array("difficulty"=>$this->difficulty));
- while($pickrule = $pickrules->getRow()){
- $rule = $pickrule->getArray();
- $rule['count'] = 0;
- $rule['ok'] = false;
- $rules[$pickrule->parent_id][$rule['id']] = $this->getRuleText($rule);
- }
- $this->rules = $rules;
- }
- private function getRuleText($rule){
- foreach($this->books as $section){
- foreach($section as $book){
- if($book[$rule['attribute']] == $rule['value'] and array_key_exists($rule['text'],$book)){
- $rule['text'] = $book[$rule['text']];
- return $rule;
- }
- }
- }
- return $rule;
- }
- public function countMissing(){
- $this->getRules();
- $result = $this->execRules($this->rules,$this->books,0);
- return $this->rules = $result['rules'];
- }
- private function execRules($rules,$books,$parent){
- $result['result'] = true;
- foreach($rules[$parent] as $rule_id => $rule){
- foreach($books as $section){
- foreach($section as $book){
- if(array_key_exists($rule['attribute'],$book)){
- if($book['marked'] == true and $book[$rule['attribute']] == $rule['value']){
- $rules[$parent][$rule_id]['count']++;
- $books_passed[$book['section_name']][] = $book;
- }elseif($book['marked'] == true and $rule['value'] == 'count'){
- $count[$book[$rule['attribute']]] = true;
- }
- }
- }
- }
- if(isset($count) and $rule['value'] == 'count'){
- $rules[$parent][$rule_id]['count'] = count($count);
- }
- if($rules[$parent][$rule_id]['count'] >= $rules[$parent][$rule_id]['number']){
- $rules[$parent][$rule_id]['ok'] = true;
- }else{
- $result['result'] = false;
- }
- if(isset($books_passed) and array_key_exists($rule['id'],$rules)){
- $child_result = $this->execRules($rules,$books_passed,$rule['id']);
- $rules = $child_result['rules'];
- $rules[$parent][$rule_id]['ok'] = $child_result['result'];
- }
- }
- $result['rules'] = $rules;
- return $result;
- }
- public function checkPassed(){
- foreach($this->rules[0] as $rule){
- if($rule['ok'] != true) return false;
- }
- return true;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement