PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/include/classes/DAO/GuidelineGroupsDAO.class.php

https://github.com/atutor/AChecker
PHP | 327 lines | 177 code | 47 blank | 103 comment | 16 complexity | 69bdae4ec22089bb3e97e5ceab3b4397 MD5 | raw file
  1. <?php
  2. /************************************************************************/
  3. /* AChecker */
  4. /************************************************************************/
  5. /* Copyright (c) 2008 - 2011 */
  6. /* Inclusive Design Institute */
  7. /* */
  8. /* This program is free software. You can redistribute it and/or */
  9. /* modify it under the terms of the GNU General Public License */
  10. /* as published by the Free Software Foundation. */
  11. /************************************************************************/
  12. // $Id$
  13. /**
  14. * DAO for "guideline_groups" table
  15. * @access public
  16. * @author Cindy Qi Li
  17. * @package DAO
  18. */
  19. if (!defined('AC_INCLUDE_PATH')) exit;
  20. require_once(AC_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
  21. require_once(AC_INCLUDE_PATH. 'classes/Utility.class.php');
  22. class GuidelineGroupsDAO extends DAO {
  23. /**
  24. * Create a new guideline group
  25. * @access public
  26. * @param $guidelineID : guideline id
  27. * $name
  28. * $abbr
  29. * $principle
  30. * @return group_id : if successful
  31. * false : if not successful
  32. * @author Cindy Qi Li
  33. */
  34. public function Create($guidelineID, $name, $abbr, $principle)
  35. {
  36. global $addslashes;
  37. $guidelineID = intval($guidelineID);
  38. $name = trim($name); // $addslashes is not necessary as it's called in LanguageTextDAO->Create()
  39. $abbr = $addslashes(trim($abbr));
  40. $principle = $addslashes(trim($principle));
  41. $sql = "INSERT INTO ".TABLE_PREFIX."guideline_groups
  42. (`guideline_id`, `abbr`, `principle`)
  43. VALUES
  44. (".$guidelineID.", '".$abbr."', '".$principle."')";
  45. if (!$this->execute($sql))
  46. {
  47. $msg->addError('DB_NOT_UPDATED');
  48. return false;
  49. }
  50. else
  51. {
  52. $group_id = mysql_insert_id();
  53. if ($name <> '')
  54. {
  55. $term = LANG_PREFIX_GUIDELINE_GROUPS_NAME.$group_id;
  56. require_once(AC_INCLUDE_PATH.'classes/DAO/LanguageTextDAO.class.php');
  57. $langTextDAO = new LanguageTextDAO();
  58. if ($langTextDAO->Create($_SESSION['lang'], '_guideline',$term,$name,''))
  59. {
  60. $sql = "UPDATE ".TABLE_PREFIX."guideline_groups
  61. SET name='".$term."' WHERE group_id=".$group_id;
  62. $this->execute($sql);
  63. }
  64. }
  65. return $group_id;
  66. }
  67. }
  68. /**
  69. * Update an existing guideline group
  70. * @access public
  71. * @param $groupID : group id
  72. * $name
  73. * $abbr
  74. * $principle
  75. * @return true : if successful
  76. * false : if not successful
  77. * @author Cindy Qi Li
  78. */
  79. public function Update($groupID, $name, $abbr, $principle)
  80. {
  81. global $addslashes;
  82. $groupID = intval($groupID);
  83. $name = trim($name); // $addslashes is not necessary as it's called in LanguageTextDAO->updateLang()
  84. $abbr = $addslashes(trim($abbr));
  85. $principle = $addslashes(trim($principle));
  86. $sql = "UPDATE ".TABLE_PREFIX."guideline_groups
  87. SET abbr='".$abbr."',
  88. principle = '".$principle."'
  89. WHERE group_id = ".$groupID;
  90. if (!$this->execute($sql))
  91. {
  92. $msg->addError('DB_NOT_UPDATED');
  93. return false;
  94. }
  95. else
  96. {
  97. if ($name <> '')
  98. {
  99. $term = LANG_PREFIX_GUIDELINE_GROUPS_NAME.$groupID;
  100. $this->updateLang($groupID, $term, $name, 'name');
  101. }
  102. }
  103. }
  104. /**
  105. * Delete all entries of given group ID
  106. * @access public
  107. * @param $groupID : group id
  108. * @return true : if successful
  109. * false : if not successful
  110. * @author Cindy Qi Li
  111. */
  112. public function Delete($groupID)
  113. {
  114. require_once(AC_INCLUDE_PATH.'classes/DAO/GuidelineSubgroupsDAO.class.php');
  115. $groupID = intval($groupID);
  116. // Delete all subgroups
  117. $guidelineSubgroupsDAO = new GuidelineSubgroupsDAO();
  118. $sql = "SELECT subgroup_id FROM ".TABLE_PREFIX."guideline_subgroups
  119. WHERE group_id = ".$groupID;
  120. $rows = $this->execute($sql);
  121. if (is_array($rows))
  122. {
  123. foreach ($rows as $row)
  124. $guidelineSubgroupsDAO->Delete($row['subgroup_id']);
  125. }
  126. // delete language for group name
  127. $sql = "DELETE FROM ".TABLE_PREFIX."language_text
  128. WHERE variable='_guideline'
  129. AND term=(SELECT name
  130. FROM ".TABLE_PREFIX."guideline_groups
  131. WHERE group_id=".$groupID.")";
  132. $this->execute($sql);
  133. // delete guideline_groups
  134. $sql = "DELETE FROM ".TABLE_PREFIX."guideline_groups WHERE group_id=".$groupID;
  135. return $this->execute($sql);
  136. }
  137. /**
  138. * Add checks into guideline group
  139. * @access public
  140. * @param $groupID : guideline group id
  141. * $cids : array of check ids to be added into guideline group
  142. * @return true : if successful
  143. * false : if unsuccessful
  144. * @author Cindy Qi Li
  145. */
  146. public function addChecks($groupID, $cids)
  147. {
  148. require_once(AC_INCLUDE_PATH.'classes/DAO/GuidelineSubgroupsDAO.class.php');
  149. require_once(AC_INCLUDE_PATH.'classes/DAO/SubgroupChecksDAO.class.php');
  150. $groupID = intval($groupID);
  151. if ($groupID == 0)
  152. {
  153. $msg->addError('MISSING_GID');
  154. return false;
  155. }
  156. $guidelineSubgroupsDAO = new GuidelineSubgroupsDAO();
  157. $subgroups = $guidelineSubgroupsDAO->getUnnamedSubgroupByGroupID($groupID);
  158. if (is_array($subgroups))
  159. $subgroup_id = $subgroups[0]['subgroup_id'];
  160. else
  161. $subgroup_id = $guidelineSubgroupsDAO->Create($groupID, '','');
  162. if ($subgroup_id)
  163. {
  164. $subgroupChecksDAO = new SubgroupChecksDAO();
  165. if (is_array($cids))
  166. {
  167. foreach ($cids as $cid) {
  168. $cid = intval($cid);
  169. if ($cid > 0) {
  170. $subgroupChecksDAO->Create($subgroup_id, $cid);
  171. }
  172. }
  173. }
  174. }
  175. else return false;
  176. return true;
  177. }
  178. /**
  179. * Return group info of the given group id
  180. * @access public
  181. * @param $groupID : group id
  182. * @return table row: if success
  183. * false : if fail
  184. * @author Cindy Qi Li
  185. */
  186. public function getGroupByID($groupID)
  187. {
  188. $sql = "SELECT * FROM ".TABLE_PREFIX."guideline_groups
  189. WHERE group_id = ".intval($groupID);
  190. $rows = $this->execute($sql);
  191. return $rows[0];
  192. }
  193. /**
  194. * Return group info of the given check id and guideline id
  195. * @access public
  196. * @param $checkID : check id
  197. * $guidelineID: guideline id
  198. * @return table row: if success
  199. * false : if fail
  200. * @author Cindy Qi Li
  201. */
  202. public function getGroupByCheckIDAndGuidelineID($checkID, $guidelineID)
  203. {
  204. $checkID = intval($checkID);
  205. $guidelineID = intval($guidelineID);
  206. $sql = "SELECT * FROM ".TABLE_PREFIX."guideline_groups
  207. WHERE group_id in (SELECT gs.group_id
  208. FROM ".TABLE_PREFIX."guideline_subgroups gs, "
  209. .TABLE_PREFIX."subgroup_checks sc
  210. WHERE gs.subgroup_id = sc.subgroup_id
  211. AND sc.check_id=".$checkID.")
  212. AND guideline_id = ".$guidelineID;
  213. return $this->execute($sql);
  214. }
  215. /**
  216. * Return array of groups info whose name is NOT null, and belong to the given guideline id
  217. * @access public
  218. * @param $guidelineID : guideline id
  219. * @return group id rows : array of group ids, if successful
  220. * false : if not successful
  221. * @author Cindy Qi Li
  222. */
  223. public function getNamedGroupsByGuidelineID($guidelineID)
  224. {
  225. $guidelineID = intval($guidelineID);
  226. $sql = "SELECT * FROM ".TABLE_PREFIX."guideline_groups gg, ".TABLE_PREFIX."language_text l
  227. WHERE gg.guideline_id = ".$guidelineID."
  228. AND gg.name is not NULL
  229. AND gg.name = l.term
  230. AND l.language_code = '".$_SESSION['lang']."'
  231. ORDER BY l.text";
  232. return Utility::sortArrayByNumInField($this->execute($sql), 'text');
  233. }
  234. /**
  235. * Return array of groups info whose name is null, and belong to the given guideline id
  236. * @access public
  237. * @param $guidelineID : guideline id
  238. * @return group id rows : array of group ids, if successful
  239. * false : if not successful
  240. * @author Cindy Qi Li
  241. */
  242. public function getUnnamedGroupsByGuidelineID($guidelineID)
  243. {
  244. $guidelineID = intval($guidelineID);
  245. $sql = "SELECT * FROM ".TABLE_PREFIX."guideline_groups
  246. WHERE guideline_id = ".$guidelineID."
  247. AND name is NULL";
  248. return $this->execute($sql);
  249. }
  250. /**
  251. * insert/update guideline group term into language_text and update according record in table "guideline_groups"
  252. * @access private
  253. * @param $groupID
  254. * $term : term to create/update into 'language_text' table
  255. * $text : text to create/update into 'language_text' table
  256. * $fieldName : field name in table 'guideline_groups' to update
  257. * @return true if update successfully
  258. * false if update unsuccessful
  259. * @author Cindy Qi Li
  260. */
  261. private function updateLang($groupID, $term, $text, $fieldName)
  262. {
  263. global $addslashes;
  264. require_once(AC_INCLUDE_PATH.'classes/DAO/LanguageTextDAO.class.php');
  265. $langTextDAO = new LanguageTextDAO();
  266. $langs = $langTextDAO->getByTermAndLang($term, $_SESSION['lang']);
  267. if (is_array($langs))
  268. {// term already exists. Only need to update modified text
  269. if ($langs[0]['text'] <> $addslashes($text)) $langTextDAO->setText($_SESSION['lang'], '_guideline',$term,$text);
  270. }
  271. else
  272. {
  273. $langTextDAO->Create($_SESSION['lang'], '_guideline',$term,$text,'');
  274. $sql = "UPDATE ".TABLE_PREFIX."guideline_groups SET ".$fieldName."='".$term."' WHERE group_id=".$groupID;
  275. $this->execute($sql);
  276. }
  277. return true;
  278. }
  279. }
  280. ?>