public function insert(array $data)
 {
     $auth = Zend_Auth::getInstance();
     $user_id = $auth->getIdentity()->id;
     $user_table = new User();
     $user_row = $user_table->fetchRow('id = ' . $user_id);
     $data['created_by'] = $user_id;
     if (!isset($data['approval_status']) or !$data['approval_status']) {
         $data['approval_status'] = 'new';
     }
     //get recipients
     $training_id = $data['training_id'];
     $select = $this->select()->setIntegrityCheck(false)->from($this->_name)->join(array('u' => 'user'), "training_approval_history.created_by = u.id", array('email', 'first_name', 'last_name'))->where("training_id = {$training_id} AND u.is_blocked = 0");
     $previous_history_rows = $this->fetchAll($select);
     $recipients = array();
     foreach ($previous_history_rows as $rec) {
         $recipients[$rec->created_by] = array('email' => $rec->email, 'name' => $rec->first_name . ' ' . $rec->last_name);
     }
     //send to anyone other than creator
     unset($recipients[$user_id]);
     //insert the row
     $data['recipients'] = implode(',', array_keys($recipients));
     parent::insert($data);
     //send the mail
     #echo print_r($recipients, true) . '//'.$data['approval_status'];
     #$recipients = array('name' => '*****@*****.**', 'email' => '*****@*****.**');
     if ($recipients && $data['approval_status']) {
         require_once 'models/table/Training.php';
         $training = new Training();
         $training_name = $training->getCourseName($training_id);
         $view = new Zend_View();
         $view->setScriptPath(Globals::$BASE_PATH . '/app/views/scripts/email');
         $view->assign('creator', $user_row->first_name . ' ' . $user_row->last_name);
         $view->assign('training_name', $training_name);
         $view->assign('comments', $data['message']);
         $view->assign('link', Settings::$COUNTRY_BASE_URL . '/training/edit/id/' . $training_id);
         $mail = new Zend_Mail();
         switch ($data['approval_status']) {
             case 'approved':
                 $text = $view->render('text/approved.phtml');
                 $html = $view->render('html/approved.phtml');
                 $mail->setSubject(t('Training') . ' ' . t('Approved'));
                 break;
             case 'rejected':
                 $text = $view->render('text/rejected.phtml');
                 $html = $view->render('html/rejected.phtml');
                 $mail->setSubject(t('Training') . ' ' . t('Rejected'));
                 break;
             case 'resubmitted':
                 $text = $view->render('text/resubmitted.phtml');
                 $html = $view->render('html/resubmitted.phtml');
                 $mail->setSubject(t('Training') . ' ' . t('Resubmitted'));
                 break;
         }
         $mail->setBodyText($text);
         $mail->setBodyHtml($html);
         $mail->setFrom(Settings::$EMAIL_ADDRESS, Settings::$EMAIL_NAME);
         foreach ($recipients as $guy) {
             $mail->addTo($guy['email'], $guy['name']);
         }
         //$mail->send();
     }
 }
 public function assignTrainingAction()
 {
     $id = $this->getSanParam('id');
     $this->view->assign('id', $id);
     require_once 'models/table/Training.php';
     require_once 'models/table/OptionList.php';
     $training = new Training();
     $rows = $training->find($id);
     $row = $rows->current();
     $this->view->assign('training', $row);
     $this->view->assign('training_name', $training->getCourseName($id));
     $evaluations = OptionList::suggestionList('evaluation', array('id', 'title'));
     $this->view->assign('evaluations', $evaluations);
     //find currently selected
     $evalTable = new OptionList(array('name' => 'evaluation_to_training'));
     $select = $evalTable->select()->from('evaluation_to_training', array('evaluation_id'))->where('training_id = ' . $id);
     $row = $evalTable->fetchRow($select);
     if ($row) {
         $this->view->assign('evaluation_id', $row->evaluation_id);
     }
     $request = $this->getRequest();
     if ($request->isPost()) {
         $status = ValidationContainer::instance();
         $evaluation_id = $this->getSanParam('evaluation_id');
         $status->setStatusMessage(t('The evaluation has been assigned.'));
         $eval_id = $this->getSanParam('evaluation_id');
         require_once 'models/table/MultiOptionList.php';
         MultiOptionList::updateOptions('evaluation_to_training', 'evaluation', 'training_id', $id, 'evaluation_id', array($eval_id => $eval_id));
         $status->setRedirect('/training/edit/id/' . $id);
         $this->sendData($status);
     }
 }
    public function scoresImportAction()
    {
        require_once 'models/table/Person.php';
        require_once 'models/table/PersonToTraining.php';
        //labels
        $id = $this->getSanParam('training');
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $status = ValidationContainer::instance();
        $trainingObj = new Training();
        $this->viewAssignEscaped('courseName', $trainingObj->getCourseName($id));
        $this->view->assign('training_id', $id);
        //CSV import -- post
        if (@$_FILES['import']['tmp_name']) {
            $filename = $_FILES['import']['tmp_name'];
            if ($filename) {
                // we need a table to compare names to
                $table = new ITechTable(array('name' => 'score'));
                $persons = new ITechTable(array('name' => 'person'));
                $sql = 'select distinct person_to_training.id as pid,person.first_name,person.last_name from person_to_training
					   left join person on person.id = person_id
					   where person_to_training.training_id = ' . $id;
                $ppl = $db->fetchAll($sql);
                while ($row = $this->_csv_get_row($filename)) {
                    if (is_array($row)) {
                        if (isset($row[0]) && isset($row[4]) && !empty($row[0]) && !empty($row[4])) {
                            // find person
                            $row[0] = trim($row[0]);
                            $row[1] = trim($row[1]);
                            $pid = null;
                            foreach ($ppl as $v) {
                                if ($v['first_name'] == $row[0] && $v['last_name'] == $row[1]) {
                                    $pid = $v['pid'];
                                    break;
                                }
                            }
                            if ($pid) {
                                $new_row = $table->createRow();
                                $new_row->person_to_training_id = $pid;
                                $new_row->training_date = $row[2];
                                $new_row->score_label = $row[3];
                                $new_row->score_value = $row[4];
                                $new_row->save();
                            } else {
                                // err
                                if (!isset($notfound)) {
                                    $notfound = array();
                                }
                                if ($row[0] != t('First Name')) {
                                    $notfound[] = $row[0] . ' ' . $row[1] . '<br>';
                                }
                            }
                        }
                    }
                }
            }
            $_POST['redirect'] = null;
            if ($notfound) {
                $status->setStatusMessage(t('The following users could not be found while importing, perhaps they were not adding to the training:<br>'));
                foreach ($notfound as $v) {
                    $status->setStatusMessage($v);
                }
            }
            // done
        }
        // score view (edit table)
        require_once 'views/helpers/EditTableHelper.php';
        $label = 'Score';
        $fields = array('name' => t('Name'), 'score_label' => t('Label'), 'score_value' => t('Score'));
        $rowRay = $db->fetchAll("select score.*,CONCAT(person.first_name, CONCAT(' ', person.last_name)) as name from person_to_training\r\n\t\t\t\t\t\tinner join score on score.person_to_training_id = person_to_training.id\r\n\t\t\t\t\t\tleft join person on person.id = person_id\r\n\t\t\t\t\t\twhere person_to_training.training_id = {$id}\r\n\t\t\t\t\t\t");
        $this->view->assign('editTable', EditTableHelper::generateHtml($label, $rowRay, $fields, array(), array(), true));
    }