ChecklistapiChecklist.php

Class for Checklist API checklists.

Classes

Namesort descending Description
ChecklistapiChecklist Defines the checklist class.

File

modules/contrib/checklistapi/lib/Drupal/checklistapi/ChecklistapiChecklist.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Class for Checklist API checklists.
  5. */
  6. /**
  7. * Defines the checklist class.
  8. */
  9. class ChecklistapiChecklist {
  10. /**
  11. * The checklist ID.
  12. *
  13. * @var string
  14. */
  15. public $id;
  16. /**
  17. * The checklist title.
  18. *
  19. * @var string
  20. */
  21. public $title;
  22. /**
  23. * The menu item description.
  24. *
  25. * @var string
  26. */
  27. public $description;
  28. /**
  29. * The checklist path.
  30. *
  31. * @var string
  32. */
  33. public $path;
  34. /**
  35. * The checklist help.
  36. *
  37. * @var string
  38. */
  39. public $help;
  40. /**
  41. * The name of the menu to put the menu item in.
  42. *
  43. * @var string
  44. */
  45. public $menuName;
  46. /**
  47. * The checklist weight.
  48. *
  49. * @var float
  50. */
  51. public $weight;
  52. /**
  53. * The number of list items in the checklist.
  54. *
  55. * @var int
  56. */
  57. public $numberOfItems = 0;
  58. /**
  59. * The checklist groups and items.
  60. *
  61. * @var array
  62. */
  63. public $items = array();
  64. /**
  65. * The saved progress data.
  66. *
  67. * @var array
  68. */
  69. public $savedProgress;
  70. /**
  71. * Constructs a ChecklistapiChecklist object.
  72. *
  73. * @param array $definition
  74. * A checklist definition, as returned by checklistapi_get_checklist_info().
  75. */
  76. public function __construct(array $definition) {
  77. foreach (element_children($definition) as $group_key) {
  78. $this->numberOfItems += count(element_children($definition[$group_key]));
  79. $this->items[$group_key] = $definition[$group_key];
  80. unset($definition[$group_key]);
  81. }
  82. foreach ($definition as $property_key => $value) {
  83. $property_name = checklistapi_strtolowercamel(drupal_substr($property_key, 1));
  84. $this->$property_name = $value;
  85. }
  86. $this->savedProgress = variable_get($this->getSavedProgressVariableName(), array());
  87. }
  88. /**
  89. * Gets the total number of completed items.
  90. *
  91. * @return int
  92. * The number of completed items.
  93. */
  94. public function getNumberCompleted() {
  95. return (!empty($this->savedProgress['#completed_items'])) ? $this->savedProgress['#completed_items'] : 0;
  96. }
  97. /**
  98. * Gets the total number of items.
  99. *
  100. * @return int
  101. * The number of items.
  102. */
  103. public function getNumberOfItems() {
  104. return $this->numberOfItems;
  105. }
  106. /**
  107. * Gets the name of the last user to update the checklist.
  108. *
  109. * @return string
  110. * The themed name of the last user to update the checklist, or 'n/a' if
  111. * there is no record of such a user.
  112. */
  113. public function getLastUpdatedUser() {
  114. if (isset($this->savedProgress['#changed_by'])) {
  115. $last_updated_user = user_load($this->savedProgress['#changed_by']);
  116. return theme('username', array('account' => $last_updated_user));
  117. }
  118. else {
  119. return t('n/a');
  120. }
  121. }
  122. /**
  123. * Gets the last updated date.
  124. *
  125. * @return string
  126. * The last updated date formatted with format_date(), or 'n/a' if there is
  127. * no saved progress.
  128. */
  129. public function getLastUpdatedDate() {
  130. return (!empty($this->savedProgress['#changed'])) ? format_date($this->savedProgress['#changed']) : t('n/a');
  131. }
  132. /**
  133. * Gets the percentage of items complete.
  134. *
  135. * @return float
  136. * The percent complete.
  137. */
  138. public function getPercentComplete() {
  139. if ($this->getNumberOfItems() == 0) {
  140. return 100;
  141. }
  142. return ($this->getNumberCompleted() / $this->getNumberOfItems()) * 100;
  143. }
  144. /**
  145. * Clears the saved progress for the checklist.
  146. *
  147. * Deletes the Drupal variable containing the checklist's saved progress.
  148. */
  149. public function clearSavedProgress() {
  150. variable_del($this->getSavedProgressVariableName());
  151. drupal_set_message(t('%title saved progress has been cleared.', array(
  152. '%title' => $this->title,
  153. )));
  154. }
  155. /**
  156. * Gets the name of the Drupal variable for the checklist's saved progress.
  157. *
  158. * @return string
  159. * The Drupal variable name.
  160. */
  161. public function getSavedProgressVariableName() {
  162. return 'checklistapi_checklist_' . $this->id;
  163. }
  164. /**
  165. * Determines whether the checklist has saved progress.
  166. *
  167. * @return bool
  168. * TRUE if the checklist has saved progress, or FALSE if it doesn't.
  169. */
  170. public function hasSavedProgress() {
  171. return (bool) variable_get($this->getSavedProgressVariableName(), FALSE);
  172. }
  173. /**
  174. * Saves checklist progress to a Drupal variable.
  175. *
  176. * @param array $values
  177. * A multidimensional array of form state checklist values.
  178. *
  179. * @see checklistapi_checklist_form_submit()
  180. */
  181. public function saveProgress(array $values) {
  182. global $user;
  183. $time = time();
  184. $num_changed_items = 0;
  185. $progress = array(
  186. '#changed' => $time,
  187. '#changed_by' => $user->uid,
  188. '#completed_items' => 0,
  189. );
  190. // Loop through groups.
  191. foreach ($values as $group_key => $group) {
  192. if (!is_array($group)) {
  193. continue;
  194. }
  195. // Loop through items.
  196. foreach ($group as $item_key => $item) {
  197. $definition = checklistapi_get_checklist_info($this->id);
  198. if (!in_array($item_key, array_keys($definition[$group_key]))) {
  199. // This item wasn't in the checklist definition. Don't include it with
  200. // saved progress.
  201. continue;
  202. }
  203. $old_item = (!empty($this->savedProgress[$item_key])) ? $this->savedProgress[$item_key] : 0;
  204. if ($item == 1) {
  205. // Item is checked.
  206. $progress['#completed_items']++;
  207. if ($old_item) {
  208. // Item was previously checked. Use saved value.
  209. $new_item = $old_item;
  210. }
  211. else {
  212. // Item is newly checked. Set new value.
  213. $new_item = array(
  214. '#completed' => $time,
  215. '#uid' => $user->uid,
  216. );
  217. $num_changed_items++;
  218. }
  219. }
  220. else {
  221. // Item is unchecked.
  222. $new_item = 0;
  223. if ($old_item) {
  224. // Item was previously checked.
  225. $num_changed_items++;
  226. }
  227. }
  228. $progress[$item_key] = $new_item;
  229. }
  230. }
  231. // Sort array elements alphabetically so changes to the order of items in
  232. // checklist definitions over time don't affect the order of elements in the
  233. // saved progress variable. This simplifies use with Strongarm.
  234. ksort($progress);
  235. variable_set($this->getSavedProgressVariableName(), $progress);
  236. drupal_set_message(format_plural(
  237. $num_changed_items,
  238. '%title progress has been saved. 1 item changed.',
  239. '%title progress has been saved. @count items changed.',
  240. array('%title' => $this->title)
  241. ));
  242. }
  243. /**
  244. * Determines whether the current user has access to the checklist.
  245. *
  246. * @param string $operation
  247. * The operation to test access for. Possible values are "view", "edit", and
  248. * "any". Defaults to "any".
  249. *
  250. * @return bool
  251. * Returns TRUE if the user has access, or FALSE if not.
  252. */
  253. public function userHasAccess($operation = 'any') {
  254. return checklistapi_checklist_access($this->id, $operation);
  255. }
  256. }