CMS  Version 3.9
survey.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9 Copyright (c) 2010 Sonjara, Inc
10 
11 Permission is hereby granted, free of charge, to any person
12 obtaining a copy of this software and associated documentation
13 files (the "Software"), to deal in the Software without
14 restriction, including without limitation the rights to use,
15 copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the
17 Software is furnished to do so, subject to the following
18 conditions:
19 
20 The above copyright notice and this permission notice shall be
21 included in all copies or substantial portions of the Software.
22 
23 Except as contained in this notice, the name(s) of the above
24 copyright holders shall not be used in advertising or otherwise
25 to promote the sale, use or other dealings in this Software
26 without prior written authorization.
27 
28 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35 OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 /*
40  * Title: survey.inc
41  *
42  * Description: Datamodel for Email Templates.
43  *
44  * author: Janice Gallant for Sonjara, Inc.
45  *
46  * date: 2/18/10
47  *
48  */
49 Fakoli::using("questionnaire");
50 
51 
53 {
54  var $fields = array(
55  "survey_id" => Number,
56  "title" => String,
57  "introduction" => Text,
58  "start_date" => Date,
59  "end_date" => Date,
60  "instructions" => HTML,
61  "user_id" => Number,
62  "sender_email" => String,
63  "recipients" => Text,
64  "cc_recipients" => Text,
65  "additional_recipients" => Text,
66  "allow_anonymous_responses" => Boolean,
67  "show_preview_before_submitting" => Boolean,
68  "message" => Text,
69  "confirmation_message" => HTML,
70  "status" => String,
71  "deleted" => Boolean
72  );
73 
74  var $relations = array(
75  "Responses" => SurveyResponse,
76  "Questions" => SurveyQuestion,
77  "SurveyQuestionXrefs" => SurveyQuestionXref,
78  "User" => "",
79  "Author" => "",
80  );
81 
82  var $fieldAliases = array(
83  "cc_recipients" => "CC Recipients",
84  );
85 
86  static $statusTypes = array(
87  "not sent" => "Not Sent",
88  "open" => "Open",
89  "closed" => "Closed"
90  );
91 
92 
93  // Need to account for user tables that don't have "user_id"
94  // as primary key.
95  function User()
96  {
97  $mgr = new UserManager();
98  $class = $mgr->getUserClass();
99  $l_user = new $class();
100  $pk = $l_user->getPrimaryKey();
101 
102  $authors = Query::create($class, "WHERE $pk =:user_id")
103  ->bind(":user_id", $this->user_id)
104  ->execute();
105  return (count($authors) > 0) ? $authors[0] : null;
106  }
107 
108  function Author()
109  {
110  return $this->User();
111  }
112 
113  function SurveyQuestionXrefs($constraint = "ORDER BY sort_order")
114  {
115  return $this->getRelatedList(SurveyQuestionXref, "survey_id", $constraint);
116  }
117 
118  function Questions($constraint = "ORDER BY sort_order")
119  {
120  if($this->survey_id)
121  return $this->crossReference(SurveyQuestion, SurveyQuestionXref, $constraint);
122  }
123 
124  function Responses($constraint = "")
125  {
126  return $this->getRelatedList(SurveyResponse);
127  }
128 
129  /* for results aggregation */
130  function Answers($constraint = "")
131  {
132  $query = "WHERE response_id IN (SELECT response_id FROM survey_response WHERE status = 'submitted' AND survey_id=:survey_id AND include_in_results=true)";
133  if($constraint)
134  {
135  $query .= preg_replace("/WHERE/i", " AND", $constraint);
136  }
137 
138  if($constraint)
139  {
140  $constraint = preg_replace("/WHERE/i", " AND", $constraint);
141  $constraint = preg_replace("/ question_id/", " survey_question_id", $constraint);
142  $query .= $constraint;
143  }
144 
145  trace("Email survey answers constraint ". $query, 3);
146  return Query::create(SurveyAnswer, $query)
147  ->bind(":survey_id", $this->survey_id)
148  ->execute();
149 
150  $answers = query(SurveyAnswer, $query);
151  return $answers;
152  }
153 
154  function getTitle()
155  {
157  $title .= ($this->start_date > 0) ? " &ndash; " . $this->format("{start_date:F d, Y}") : "";
158 
159  return $title;
160  }
161 
162  function isEditable()
163  {
164  if(!$this->survey_id)
165  return true;
166 
167  return ($this->isAuthor() AND $this->status == "not sent") ? true : false;
168  }
169 
170  function isSent()
171  {
172  return ($this->status == "not sent") ? false : true;
173  }
174 
175  function isOpen()
176  {
177  return ($this->status == "open") ? true : false;
178  }
179 
180  function setDefaults()
181  {
182  global $user;
183 
184  $this->status = "not sent";
185  $this->user_id = $user->user_id;
186  }
187 
188  /* set to closed if end_date is greater than today */
189  function isClosed()
190  {
191  if($this->status == "open" AND $this->end_date > 0)
192  {
193  $today = date("Y-m-d");
194  if($this->end_date < $today)
195  {
196  $this->setStatus("closed");
197  }
198  }
199 
200  return ($this->status == "closed") ? true : false;
201  }
202 
203  function isAuthor()
204  {
205  global $user;
206  return (checkRole("admin,data") || $this->site_user_id == $user->site_user_id) ? true : false;
207 
208  }
209 
210  function setStatus($status)
211  {
212  $today = date("Y-m-d"); // today's date
213 
214  if($status == "open")
215  {
216  $this->filter = new InclusionFilter("start_date", "end_date", "status");
217 
218  if($this->start_date > 0)
219  {
220  if($this->end_date > 0) // reopening
221  $this->end_date = 0;
222  }
223  else // opening for the first time
224  $this->start_date = $today;
225 
226  }
227  elseif($status == "closed")
228  {
229  $this->filter = new InclusionFilter("end_date", "status");
230  $this->end_date = $today;
231  }
232 
233  $this->status = $status;
234  $this->save();
235  }
236 
242  function getRecipientCount()
243  {
244  $count = Query::create(SurveyResponse, "WHERE survey_id=:survey_id AND include_in_results=true")
245  ->bind(":survey_id", $this->survey_id)
246  ->executeValue("COUNT(1)");
247 
248  return $count;
249  }
250 
258  function getResponseCount()
259  {
260  $count = Query::create(SurveyResponse, "WHERE status = 'submitted' AND survey_id=:survey_id AND include_in_results=true")
261  ->bind(":survey_id", $this->survey_id)
262  ->executeValue("COUNT(1)");
263 
264  return $count;
265  }
266 
272  function getNonResponders()
273  {
274  $nonResponders = Query::create(SurveyResponse, "WHERE survey_id=:survey_id AND status != 'submitted' AND include_in_results=true")
275  ->bind(":survey_id", $this->survey_id)
276  ->execute();
277 
278  return $nonResponders;
279  }
280 
281  function allowDelete()
282  {
283  if(!$this->survey_id)
284  return true;
285 
286  return ($this->status != "open") ? true : false;
287  }
288 
289  function Survey()
290  {
291  $this->table = "survey";
292  $this->primary_key = "survey_id";
293  $this->answer_class = SurveyAnswer;
294 
295  $this->pretty_class_name = "Survey";
296 
297  $this->DataItem(func_get_args());
298  }
299 }
300 
301 
302 ?>
$constraint
$table filter
return false
$progress status
Defines the relations and functions required of a Questionnaire class that uses Questionnaire form an...
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
allowDelete()
Definition: survey.inc:281
$fields
Definition: survey.inc:54
getTitle()
Definition: survey.inc:154
isAuthor()
Definition: survey.inc:203
setStatus($status)
Definition: survey.inc:210
static $statusTypes
Definition: survey.inc:86
isClosed()
Definition: survey.inc:189
$fieldAliases
Definition: survey.inc:82
Responses($constraint="")
Definition: survey.inc:124
Survey()
Definition: survey.inc:289
Questions($constraint="ORDER BY sort_order")
Definition: survey.inc:118
isSent()
Definition: survey.inc:170
getResponseCount()
Called by SurveyResultsManager.
Definition: survey.inc:258
isEditable()
Definition: survey.inc:162
isOpen()
Definition: survey.inc:175
Answers($constraint="")
Definition: survey.inc:130
Author()
Definition: survey.inc:108
getRecipientCount()
Called by SurveyResultsManager.
Definition: survey.inc:242
User()
Definition: survey.inc:95
setDefaults()
Set the initial default values in the item e.g., user_id = user->user_id status = xx.
Definition: survey.inc:180
$relations
Definition: survey.inc:74
SurveyQuestionXrefs($constraint="ORDER BY sort_order")
Definition: survey.inc:113
getNonResponders()
Called by SurveyResultsManager.
Definition: survey.inc:272
Provides the interface to the user model for the application.
global $user
$answers
$table column("Redirect From", "<a href='redirect_form?redirect_id={redirect_id}'>{redirect_from}</a>", true, "width: 30%") -> column("Redirect To", "<a href='{redirect_to}' target='_blank'>{redirect_to}</a>", true, "width: 30%") ->column("Last Modified", "{last_modified}", true, "width: 20%; text-align: center") ->column("Override", "{ override true
Definition: redirects.inc:9
$feedback user_id
Definition: save.inc:14