source: Dev/trunk/classes/AnswerSet.php @ 191

Last change on this file since 191 was 186, checked in by jkraaijeveld, 13 years ago

Implemented lazy evaluation.

  • Initially, references to other database objects are now given as an ID rather than an object.
  • Every model class now has an evaluate() function, which gets all the objects that object has references to. Returns true if it got all the values correctly, false if there are invalid references.
  • Every connector now first evaluates an object before storing, to make sure only objects with valid references get stored.
File size: 1.5 KB
Line 
1<?php
2
3/**
4 * AnswerSet is a collection of answers corresponding to a Session
5 *
6 */
7class AnswerSet extends ResearchToolObject{
8        public $survey;
9        public $respondent;
10        public $answers;
11
12        /**
13         * Constructor for an AnswerSet object
14         * @param type $uid
15         * @param type $survey
16         * @param type $respondent
17         * @param type $answers
18         */
19        public function __construct($uid=null, $survey=null, $respondent=null, $answers=null)
20        {
21                if(!isset($uid))
22                {
23                        $uid = md5(uniqid(rand(), true));
24                }
25                $this->uid = $uid;
26                $this->survey = $survey;
27                $this->respondent = $respondent;
28                $this->answers = $answers;
29        }
30
31        /**
32         * function evaluate()
33         * evaluates the references of survey, respondent and answers.
34         */
35        public function evaluate()
36        {
37                $dbi = new DatabaseInterface();
38                if(is_string($this->survey))
39                {
40                        $result = $dbi->get("survey", array("uid" => $this->survey));
41                        if(!isset($result[0]))
42                                return false;
43                        $this->survey = $result[0];
44                }
45                if(is_string($this->respondent))
46                {
47                        $result = $dbi->get("respondent", array("uid" => $this->respondent));
48                        if(!isset($result[0]))
49                                return false;
50                        $this->respondent = $result[0];
51                }
52                if(!empty($this->answers) && is_string($this->answers[0]))
53                {
54                        $newanswers = array();
55                        foreach($this->answers as $answer)
56                        {
57                                $result = $dbi->get("answer", array("uid" => $answer));
58                                if(!isset($result[0]))
59                                        return false;
60                                $newanswers[] = $result[0];
61                        }
62                        $this->answers = $newanswers;
63                }
64                return true;
65        }
66}
67
Note: See TracBrowser for help on using the repository browser.