/**
  * Copy the permissions from the given folder to all child folders and items. Do not pass a results
  * parameter, that is for the recursive counting.
  *
  * @param FolderDao $folder folder DAO
  * @param UserDao $user user DAO
  * @param null|ProgressDao $progress progress DAO
  * @param array $results
  * @return array array('success' => number of resources whose policies were successfully changed,
  *                     'failure' => number of resources failed to change due to invalid permissions
  */
 public function applyPoliciesRecursive($folder, $user, $progress = null, $results = array('success' => 0, 'failure' => 0))
 {
     foreach ($folder->getFolders() as $subfolder) {
         if ($progress) {
             $current = $progress->getCurrent() + 1;
             $message = 'Set policies on ' . $current . ' of ' . $progress->getMaximum() . ' resources';
             $this->Progress->updateProgress($progress, $current, $message);
         }
         if (!$this->Folder->policyCheck($subfolder, $user, MIDAS_POLICY_ADMIN)) {
             ++$results['failure'];
             continue;
         }
         // delete all existing policies on the subfolder
         foreach ($subfolder->getFolderpolicygroup() as $folderPolicyGroup) {
             $this->Folderpolicygroup->delete($folderPolicyGroup);
         }
         foreach ($subfolder->getFolderpolicyuser() as $folderPolicyUser) {
             $this->Folderpolicyuser->delete($folderPolicyUser);
         }
         // copy down policies from parent folder
         foreach ($folder->getFolderpolicygroup() as $folderPolicyGroup) {
             $this->Folderpolicygroup->createPolicy($folderPolicyGroup->getGroup(), $subfolder, $folderPolicyGroup->getPolicy());
         }
         foreach ($folder->getFolderpolicyuser() as $folderPolicyUser) {
             $this->Folderpolicyuser->createPolicy($folderPolicyUser->getUser(), $subfolder, $folderPolicyUser->getPolicy());
         }
         ++$results['success'];
         $results = $this->applyPoliciesRecursive($subfolder, $user, $progress, $results);
     }
     foreach ($folder->getItems() as $item) {
         if ($progress) {
             $current = $progress->getCurrent() + 1;
             $message = 'Set policies on ' . $current . ' of ' . $progress->getMaximum() . ' resources';
             $this->Progress->updateProgress($progress, $current, $message);
         }
         if (!$this->Item->policyCheck($item, $user, MIDAS_POLICY_ADMIN)) {
             ++$results['failure'];
             continue;
         }
         // delete all existing policies on the item
         foreach ($item->getItempolicygroup() as $itemPolicyGroup) {
             $this->Itempolicygroup->delete($itemPolicyGroup);
         }
         foreach ($item->getItempolicyuser() as $itemPolicyUser) {
             $this->Itempolicyuser->delete($itemPolicyUser);
         }
         // copy down policies from parent folder
         foreach ($folder->getFolderpolicygroup() as $folderPolicyGroup) {
             $this->Itempolicygroup->createPolicy($folderPolicyGroup->getGroup(), $item, $folderPolicyGroup->getPolicy());
         }
         foreach ($folder->getFolderpolicyuser() as $folderPolicyUser) {
             $this->Itempolicyuser->createPolicy($folderPolicyUser->getUser(), $item, $folderPolicyUser->getPolicy());
         }
         ++$results['success'];
     }
     return $results;
 }
 /**
  * Set a single result value.
  *
  * @param Validation_DashboardDao $dashboard target dashboard
  * @param FolderDao $folder result folder with which the value is associated
  * @param ItemDao $item item associated with the result
  * @param mixed $value scalar value representing a result
  * @return Validation_ScalarResultDao
  * @throws Zend_Exception
  */
 public function setScore($dashboard, $folder, $item, $value)
 {
     if (!$dashboard instanceof Validation_DashboardDao) {
         throw new Zend_Exception('Should be a dashboard.');
     }
     if (!$folder instanceof FolderDao) {
         throw new Zend_Exception('Should be a folder.');
     }
     if (!$item instanceof ItemDao) {
         throw new Zend_Exception('Should be an item.');
     }
     /** @var Validation_ScalarResultModel $scalarResultModel */
     $scalarResultModel = MidasLoader::loadModel('ScalarResult', 'validation');
     $items = $folder->getItems();
     $tgtItem = null;
     foreach ($items as $curItem) {
         if ($curItem->getKey() == $item->getKey()) {
             $tgtItem = $curItem;
             break;
         }
     }
     if (!$tgtItem) {
         throw new Zend_Exception('Target item not part of result set.');
     }
     // remove a previous scalar value if there is one.
     $oldResults = $scalarResultModel->findBy('item_id', $tgtItem->getKey());
     if (count($oldResults) == 1) {
         $oldResult = $oldResults[0];
         $this->database->removeLink('scores', $dashboard, $oldResult);
     }
     /** @var Validation_ScalarResultDao $scalarResult */
     $scalarResult = MidasLoader::newDao('ScalarResultDao', 'validation');
     $scalarResult->setFolderId($folder->getKey());
     $scalarResult->setItemId($tgtItem->getKey());
     $scalarResult->setValue($value);
     $scalarResultModel->save($scalarResult);
     $this->database->link('scores', $dashboard, $scalarResult);
     return $scalarResult;
 }