Commit 4a418025 authored by TravisCarden's avatar TravisCarden

By TravisCarden: Added "Checklists" report.

parent b5a0db26
<?php
/**
* @file
* Admin page callback file for the Checklist API module.
*/
/**
* Page callback: Form constructor for the report form.
*
* @see checklistapi_menu()
*
* @ingroup forms
*/
function checklistapi_report_form() {
$header = array(
t('Checklist'),
t('Progress'),
t('Last updated'),
t('Last updated by'),
t('Operations'),
);
$rows = array();
foreach (checklistapi_get_checklist_info() as $key => $value) {
$checklist = checklistapi_checklist_load($key);
$row[] = ($checklist->userHasAccess()) ? l($checklist->title, $checklist->path) : $checklist->title;
$row[] = t('@completed of @total (@percent%) complete', array(
'@completed' => $completed = (!empty($checklist->savedProgress['#completed_items'])) ? $checklist->savedProgress['#completed_items'] : 0,
'@total' => $checklist->totalItems,
'@percent' => $completed / $checklist->totalItems * 100,
));
$row[] = (!empty($checklist->savedProgress['#changed'])) ? format_date($checklist->savedProgress['#changed']) : t('n/a');
$row[] = (!empty($checklist->savedProgress['#changed_by'])) ? theme('username', array('account' => user_load($checklist->savedProgress['#changed_by']))) : t('n/a');
$row[] = ($checklist->userHasAccess() && $checklist->hasSavedProgress()) ? l(t('clear saved progress'), $checklist->path . '/clear', array(
'query' => array('destination' => 'admin/reports/checklistapi'),
)) : '';
$rows[] = $row;
}
return theme('table', array('header' => $header, 'rows' => $rows));
}
......@@ -113,7 +113,7 @@ function hook_checklistapi_checklist_info() {
* @see checklistapi_get_checklist_info()
* @see hook_checklistapi_checklist_info()
*/
function hook_checklistapi_checklist_info_alter(&$checklists) {
function hook_checklistapi_checklist_info_alter(array &$checklists) {
// Add an item.
$checklists['example']['example_group']['sample_item'] = array(
'title' => t('Sample item'),
......
......@@ -3,3 +3,4 @@ description = Provides an API for creating fillable, persistent checklists.
core = 7.x
package = Other
files[] = checklistapi.test
configure = admin/reports/checklistapi
......@@ -66,7 +66,6 @@ function checklistapi_get_checklist_info($id = NULL) {
$checklists = checklistapi_sort_array($checklists);
drupal_alter('checklistapi_checklist_info', $checklists);
$checklists = checklistapi_sort_array($checklists);
// Create #id properties from array keys.
foreach ($checklists as $key => $value) {
$checklists[$key] = array('#id' => $key) + $checklists[$key];
}
......@@ -94,6 +93,13 @@ function checklistapi_help($path, $arg) {
*/
function checklistapi_menu() {
$items = array();
$items['admin/reports/checklistapi'] = array(
'title' => 'Checklists',
'page callback' => 'checklistapi_report_form',
'access arguments' => array('access checklistapi checklists report'),
'file' => 'checklistapi.admin.inc',
);
foreach (checklistapi_get_checklist_info() as $checklist_id => $checklist) {
if (!empty($checklist['#path']) && !empty($checklist['#title'])) {
$path = $checklist['#path'];
......@@ -130,9 +136,19 @@ function checklistapi_menu() {
*/
function checklistapi_permission() {
$perms = array();
// Only link to the report if the active user has access to it.
$report_name_replacement = user_access('access checklistapi checklists report') ? l(t('Checklists'), 'admin/reports/checklistapi') : drupal_placeholder(t('Checklists'));
$perms['access checklistapi checklists report'] = array(
'title' => t(
'Access !name report',
array('!name' => $report_name_replacement)
),
);
$perms['edit any checklistapi checklist'] = array(
'title' => t('Edit any checklist'),
);
foreach (checklistapi_get_checklist_info() as $id => $checklist) {
if (!empty($id)) {
$permission = 'edit ' . $id . ' checklistapi checklist';
......
......@@ -53,7 +53,6 @@ function checklistapi_checklist_clear_confirm_submit($form, &$form_state) {
*/
function checklistapi_checklist_form($form, &$form_state, $id) {
$checklist = checklistapi_checklist_load($id);
$saved_values = $checklist->getSavedProgress();
$save_button = array(
'#type' => 'submit',
'#value' => t('Save'),
......@@ -83,8 +82,9 @@ function checklistapi_checklist_form($form, &$form_state, $id) {
),
);
// Loop through groups.
$groups = $checklist->getItems();
foreach ($groups as $group_key => $group) {
$groups = $checklist->items;
foreach (element_children($groups) as $group_key) {
$group = &$groups[$group_key];
$form['checklistapi'][$group_key] = array(
'#type' => 'fieldset',
......@@ -98,7 +98,7 @@ function checklistapi_checklist_form($form, &$form_state, $id) {
foreach ($item_keys as $item_key) {
$item = &$group[$item_key];
// Get saved state.
$saved_item = !empty($saved_values[$group_key][$item_key]) ? $saved_values[$group_key][$item_key] : 0;
$saved_item = !empty($checklist->savedProgress[$group_key][$item_key]) ? $checklist->savedProgress[$group_key][$item_key] : 0;
// Set default value.
$default_value = FALSE;
if ($saved_item) {
......@@ -132,11 +132,11 @@ function checklistapi_checklist_form($form, &$form_state, $id) {
}
if ($saved_item) {
// Prepend completion details.
$user = user_load($saved_item['uid']);
$user = user_load($saved_item['#uid']);
array_unshift($links, t(
'Completed @time by !user',
array(
'@time' => format_date($saved_item['completed'], 'short'),
'@time' => format_date($saved_item['#completed'], 'short'),
'!user' => theme('username', array('account' => $user)),
)
));
......
......@@ -60,74 +60,55 @@ class ChecklistapiChecklist {
public $weight;
/**
* Constructs a ChecklistapiChecklist object.
* The number of list items in the checklist.
*
* @param array $definition
* A checklist definition, as returned by checklistapi_get_checklist_info().
* @var int
*/
public function __construct($definition) {
foreach ($this->getPropertiesFromDefinition($definition) as $key => $value) {
$this->$key = $value;
}
}
public $totalItems = 0;
/**
* Clears the saved progress for the checklist.
* The checklist groups and items.
*
* Deletes the Drupal variable containing the checklist's saved progress.
* @var array
*/
public function clearSavedProgress() {
variable_del($this->getSavedProgressVariableName());
drupal_set_message(t('%title saved progress has been cleared.', array(
'%title' => $this->title,
)));
}
public $items = array();
/**
* Gets the items for the checklist.
* The saved progress data.
*
* @return array
* A multidimensional array of groups and items as defined by
* hook_checklistapi_checklist_info().
* @var array
*/
public function getItems() {
$definition = checklistapi_get_checklist_info($this->id);
$items = array();
foreach (element_children($definition) as $key) {
$items[$key] = $definition[$key];
}
return $items;
}
public $savedProgress;
/**
* Gets the top-level properties from a checklist definition.
* Constructs a ChecklistapiChecklist object.
*
* @param array $definition
* A checklist definition, as from hook_checklistapi_checklist_info().
*
* @return array
* An array of property values, keyed by camelCased versions of their names.
* A checklist definition, as returned by checklistapi_get_checklist_info().
*/
protected function getPropertiesFromDefinition($definition) {
$element_children = element_children($definition);
$properties = array();
public function __construct(array $definition) {
foreach (element_children($definition) as $group_key) {
$this->totalItems += count(element_children($definition[$group_key]));
$this->items[$group_key] = $definition[$group_key];
unset($definition[$group_key]);
}
foreach ($definition as $key => $value) {
if (!in_array($key, $element_children)) {
$property_name = $this->strtocamel(substr($key, 1));
$properties[$property_name] = $value;
}
$property_name = $this->strtolowercamel(substr($key, 1));
$this->$property_name = $value;
}
return $properties;
$this->savedProgress = variable_get($this->getSavedProgressVariableName(), array());
}
/**
* Gets the saved progress for the checklist.
* Clears the saved progress for the checklist.
*
* @return array
* A multidimensional array of saved progress data.
* Deletes the Drupal variable containing the checklist's saved progress.
*/
public function getSavedProgress() {
return variable_get($this->getSavedProgressVariableName(), array());
public function clearSavedProgress() {
variable_del($this->getSavedProgressVariableName());
drupal_set_message(t('%title saved progress has been cleared.', array(
'%title' => $this->title,
)));
}
/**
......@@ -156,20 +137,21 @@ class ChecklistapiChecklist {
* @param array $values
* A multidimensional array representing.
*/
public function saveProgress($values) {
public function saveProgress(array $values) {
global $user;
$saved_values = $this->getSavedProgress();
$time = time();
$completed_items_counter = 0;
$changed_items_counter = 0;
// Loop through groups.
foreach ($values as $group_key => $group) {
// Loop through items.
if (is_array($group)) {
foreach ($group as $item_key => $item) {
$old_item = &$saved_values[$group_key][$item_key];
$old_item = $this->savedProgress[$group_key][$item_key];
$new_item = &$values[$group_key][$item_key];
// Item is checked.
if ($item == 1) {
$completed_items_counter++;
// Item was previously checked. Use saved value.
if ($old_item) {
$new_item = $old_item;
......@@ -177,8 +159,8 @@ class ChecklistapiChecklist {
// Item is newly checked. Set new value.
else {
$new_item = array(
'completed' => $time,
'uid' => $user->uid,
'#completed' => $time,
'#uid' => $user->uid,
);
// Increment changed items counter.
$changed_items_counter++;
......@@ -194,7 +176,12 @@ class ChecklistapiChecklist {
}
}
}
variable_set($this->getSavedProgressVariableName(), $values);
$progress = array(
'#changed' => $time,
'#changed_by' => $user->uid,
'#completed_items' => $completed_items_counter,
) + $values;
variable_set($this->getSavedProgressVariableName(), $progress);
drupal_set_message(format_plural(
$changed_items_counter,
'Checklist %title has been updated. 1 item changed.',
......@@ -204,7 +191,7 @@ class ChecklistapiChecklist {
}
/**
* Converts a string to camelCase, as suitable for a class property name.
* Converts a string to lowerCamel case, suitably for a class property name.
*
* @param string $string
* The input string.
......@@ -212,11 +199,21 @@ class ChecklistapiChecklist {
* @return string
* The input string converted to camelCase.
*/
protected function strtocamel($string) {
protected function strtolowercamel($string) {
$string = str_replace('_', ' ', $string);
$string = ucwords($string);
$string = str_replace(' ', '', $string);
return lcfirst($string);
}
/**
* Determines whether the current user has access to the checklist.
*
* @return bool
* Returns TRUE if the user has access, or FALSE if not.
*/
public function userHasAccess() {
return checklistapi_checklist_access($this->id);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment