PageRenderTime 62ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/api/database/limesurvey/tokens.class.php

https://github.com/patrickdemond/beartooth
PHP | 322 lines | 250 code | 25 blank | 47 comment | 97 complexity | d7975155fddc79d35d743b7d9018d523 MD5 | raw file
  1. <?php
  2. /**
  3. * tokens.class.php
  4. *
  5. * @author Patrick Emond <emondpd@mcmaster.ca>
  6. * @filesource
  7. */
  8. namespace beartooth\database\limesurvey;
  9. use cenozo\lib, cenozo\log, beartooth\util;
  10. /**
  11. * Access to limesurvey's tokens_SID tables.
  12. */
  13. class tokens extends sid_record
  14. {
  15. /**
  16. * Updates the token attributes with current values from Mastodon
  17. *
  18. * @author Patrick Emond <emondpd@mcmaster.ca>
  19. * @param database\participant $db_participant The record of the participant linked to this token.
  20. * @param boolean $extended Whether or not to included extended parameters.
  21. * @access public
  22. */
  23. public function update_attributes( $db_participant )
  24. {
  25. if( NULL == $this->token )
  26. {
  27. log::warning( 'Tried to update attributes of token without token string.' );
  28. return;
  29. }
  30. $session = lib::create( 'business\session' );
  31. $db_user = $session->get_user();
  32. $db_site = $session->get_site();
  33. $db_cohort = $db_participant->get_cohort();
  34. // determine the first part of the token
  35. $token_part = substr( $this->token, 0, strpos( $this->token, '_' ) + 1 );
  36. // fill in the email
  37. $this->email = $db_participant->email;
  38. // determine the attributes from the survey with the same ID
  39. $db_surveys = lib::create( 'database\limesurvey\surveys', static::get_sid() );
  40. foreach( $db_surveys->get_token_attribute_names() as $key => $value )
  41. {
  42. $matches = array(); // for pregs below
  43. // now get the info based on the attribute name
  44. if( 'cohort' == $value )
  45. {
  46. $this->$key = $db_cohort->name;
  47. }
  48. else if( 'uid' == $value )
  49. {
  50. $this->$key = $db_participant->uid;
  51. }
  52. else if( 'override quota' == $value )
  53. {
  54. // override_quota is true if the participant's quota is disabled AND
  55. // their override_quota is true
  56. $override_quota = '0';
  57. $db_quota = $db_participant->get_quota();
  58. $this->$key = !is_null( $db_quota ) &&
  59. $db_quota->state_disabled &&
  60. $db_participant->override_quota
  61. ? '1'
  62. : '0';
  63. }
  64. else if( false !== strpos( $value, 'address' ) )
  65. {
  66. $db_address = $db_participant->get_primary_address();
  67. if( 'address street' == $value )
  68. {
  69. if( $db_address )
  70. {
  71. $this->$key = $db_address->address1;
  72. if( !is_null( $db_address->address2 ) ) $this->$key .= ' '.$db_address->address2;
  73. }
  74. else
  75. {
  76. $this->$key = '';
  77. }
  78. }
  79. else if( 'address city' == $value )
  80. {
  81. $this->$key = $db_address ? $db_address->city : '';
  82. }
  83. else if( 'address province' == $value )
  84. {
  85. $this->$key = $db_address ? $db_address->get_region()->name : '';
  86. }
  87. else if( 'address postal code' == $value )
  88. {
  89. $this->$key = $db_address ? $db_address->postcode : '';
  90. }
  91. }
  92. else if( 'age' == $value )
  93. {
  94. $this->$key = strlen( $db_participant->date_of_birth )
  95. ? util::get_interval(
  96. util::get_datetime_object( $db_participant->date_of_birth ) )->y
  97. : "";
  98. }
  99. else if( 'written consent received' == $value )
  100. {
  101. $consent_mod = lib::create( 'database\modifier' );
  102. $consent_mod->where( 'written', '=', true );
  103. $this->$key = 0 < $db_participant->get_consent_count( $consent_mod ) ? '1' : '0';
  104. }
  105. else if( 'consented to provide HIN' == $value )
  106. {
  107. $db_hin = $db_participant->get_hin();
  108. if( is_null( $db_hin ) ) $this->$key = -1;
  109. else $this->$key = 1 == $db_hin->access ? 1 : 0;
  110. }
  111. else if( 'HIN recorded' == $value )
  112. {
  113. $db_hin = $db_participant->get_hin();
  114. $this->$key = !is_null( $db_participant->get_hin()->code );
  115. }
  116. else if( 'provided data' == $value )
  117. {
  118. $event_type_class_name = lib::get_class_name( 'database\event_type' );
  119. // participants have provided data once their first interview is done
  120. $event_mod = lib::create( 'database\modifier' );
  121. $event_mod->where( 'event_type_id', '=',
  122. $event_type_class_name::get_unique_record( 'name', 'completed (Baseline Home)' )->id );
  123. $event_list = $db_participant->get_event_list( $event_mod );
  124. $provided_data = 0 < count( $event_list ) ? 'yes' : 'no';
  125. $this->$key = $provided_data;
  126. }
  127. else if( 'DCS samples' == $value )
  128. {
  129. // get data from Opal
  130. $setting_manager = lib::create( 'business\setting_manager' );
  131. $opal_url = $setting_manager->get_setting( 'opal', 'server' );
  132. $opal_manager = lib::create( 'business\opal_manager', $opal_url );
  133. $this->$key = 0;
  134. if( $opal_manager->get_enabled() )
  135. {
  136. try
  137. {
  138. $blood = $opal_manager->get_value(
  139. 'clsa-dcs', 'Phlebotomy', $db_participant, 'AGREE_BS' );
  140. $urine = $opal_manager->get_value(
  141. 'clsa-dcs', 'Phlebotomy', $db_participant, 'AGREE_URINE' );
  142. $this->$key = 0 == strcasecmp( 'yes', $blood ) ||
  143. 0 == strcasecmp( 'yes', $urine )
  144. ? 1 : 0;
  145. }
  146. catch( \cenozo\exception\base_exception $e )
  147. {
  148. // ignore argument exceptions (data not found in Opal) and report the rest
  149. if( 'argument' != $e->get_type() ) log::warning( $e->get_message() );
  150. }
  151. }
  152. }
  153. else if( 'INCL_2e' == $value )
  154. {
  155. // TODO: This is a custom token attribute which refers to a specific question in the
  156. // introduction survey. This code is not generic and needs to eventually be made
  157. // generic.
  158. $survey_class_name = lib::get_class_name( 'database\limesurvey\survey' );
  159. $db_interview = lib::create( 'business\session')->get_current_assignment()->get_interview();
  160. $phase_mod = lib::create( 'database\modifier' );
  161. $phase_mod->where( 'rank', '=', 1 );
  162. $phase_list = $db_interview->get_qnaire()->get_phase_list( $phase_mod );
  163. // determine the SID of the first phase of the questionnaire (where the question is asked)
  164. if( 1 == count( $phase_list ) )
  165. {
  166. $db_phase = current( $phase_list );
  167. $survey_class_name::set_sid( $db_phase->sid );
  168. $survey_mod = lib::create( 'database\modifier' );
  169. $survey_mod->where( 'token', 'LIKE', $token_part.'%' );
  170. $survey_mod->order_desc( 'datestamp' );
  171. $survey_list = $survey_class_name::select( $survey_mod );
  172. $found = false;
  173. foreach( $survey_list as $db_survey )
  174. { // loop through all surveys until an answer is found
  175. try
  176. {
  177. $this->$key = $db_survey->get_response( $value );
  178. // match any NON NULL response
  179. if( !is_null( $this->$key ) ) $found = true;
  180. }
  181. catch( \cenozo\exception\runtime $e )
  182. {
  183. // ignore the error and continue without setting the attribute
  184. }
  185. if( $found ) break;
  186. }
  187. }
  188. }
  189. else if( 'interviewer first_name' == $value )
  190. {
  191. $this->$key = $db_user->first_name;
  192. }
  193. else if( 'interviewer last_name' == $value )
  194. {
  195. $this->$key = $db_user->last_name;
  196. }
  197. else if( 'participant_source' == $value )
  198. {
  199. $db_source = $db_participant->get_source();
  200. $this->$key = is_null( $db_source ) ? '(none)' : $db_source->name;
  201. }
  202. else if( 'last interview date' == $value )
  203. {
  204. $event_type_class_name = lib::get_class_name( 'database\event_type' );
  205. $event_mod = lib::create( 'database\modifier' );
  206. $event_mod->order_desc( 'datetime' );
  207. $event_mod->where( 'event_type_id', '=',
  208. $event_type_class_name::get_unique_record( 'name', 'completed (Baseline Site)' )->id );
  209. $event_list = $db_participant->get_event_list( $event_mod );
  210. $db_event = 0 < count( $event_list ) ? current( $event_list ) : NULL;
  211. $this->$key = is_null( $db_event )
  212. ? 'DATE UNKNOWN'
  213. : util::get_formatted_date( $db_event->datetime );
  214. }
  215. else if( 'dcs phone_number' == $value )
  216. {
  217. $this->$key = $db_site->phone_number;
  218. }
  219. else if( 'dcs address street' == $value )
  220. {
  221. $this->$key =
  222. $db_site->address1.( is_null( $db_site->address2 ) ? '' : ', '.$db_site->address2 );
  223. }
  224. else if( 'dcs address city' == $value )
  225. {
  226. $this->$key = $db_site->city;
  227. }
  228. else if( 'dcs address province' == $value )
  229. {
  230. $this->$key = $db_site->get_region()->name;
  231. }
  232. else if( 'dcs address postal code' == $value )
  233. {
  234. $this->$key = $db_site->postcode;
  235. }
  236. else if( false !== strpos( $value, 'alternate' ) )
  237. {
  238. $alternate_list = $db_participant->get_alternate_list();
  239. if( 'number of alternate contacts' == $value )
  240. {
  241. $this->$key = count( $alterante_list );
  242. }
  243. else if(
  244. preg_match( '/alternate([0-9]+) (first_name|last_name|phone)/', $value, $matches ) )
  245. {
  246. $alt_number = intval( $matches[1] );
  247. $aspect = $matches[2];
  248. if( count( $alterante_list ) < $alt_number )
  249. {
  250. $this->$key = '';
  251. }
  252. else
  253. {
  254. if( 'phone' == $aspect )
  255. {
  256. $phone_list = $alterante_list[$alt_number - 1]->get_phone_list();
  257. $this->$key = is_array( $phone_list ) ? $phone_list[0]->number : '';
  258. }
  259. else
  260. {
  261. $this->$key = $alterante_list[$alt_number - 1]->$aspect;
  262. }
  263. }
  264. }
  265. }
  266. else if( 'previously completed' == $value )
  267. {
  268. // no need to set the token sid since it should already be set before calling this method
  269. $tokens_mod = lib::create( 'database\modifier' );
  270. $tokens_mod->where( 'token', 'like', $token_part.'%' );
  271. $tokens_mod->where( 'completed', '!=', 'N' );
  272. $this->$key = static::count( $tokens_mod );
  273. }
  274. }
  275. }
  276. /**
  277. * Returns the token name for a particular interview.
  278. * If the survey's phase is repeated then the assignment must also be provided.
  279. * @author Patrick Emond <emondpd@mcmaster.ca>
  280. * @param database\interview $db_interview
  281. * @param database\interview $db_assignment (only used if the phase is repeated)
  282. * @static
  283. * @access public
  284. */
  285. public static function determine_token_string( $db_interview, $db_assignment = NULL )
  286. {
  287. return sprintf( '%s_%s',
  288. $db_interview->id,
  289. is_null( $db_assignment ) ? 0 : $db_assignment->id );
  290. }
  291. /**
  292. * The name of the table's primary key column.
  293. * @var string
  294. * @access protected
  295. */
  296. protected static $primary_key_name = 'tid';
  297. }