/**
  * Get the sort buttons to add under the table with sortable rows
  * 
  * @param string $sortAction The name of the ajax action
  * @param string $urlIdParam The namr used to refer to the record ID in the url
  * @return MUtil_Html_HtmlElement
  */
 public function direct($sortAction = 'sort', $urlIdParam = 'id')
 {
     $view = $this->getView();
     $jquery = $view->jQuery();
     $jquery->enable();
     //Just to make sure
     $handler = \ZendX_JQuery_View_Helper_JQuery::getJQueryHandler();
     $url = $view->serverUrl() . $view->baseUrl() . '/' . $this->getRequest()->getControllerName() . '/' . $sortAction;
     $script = file_get_contents(__DIR__ . '/js/SortableTable.js');
     $fields = array('jQuery' => $handler, 'AJAXURL' => $url, 'IDPARAM' => $urlIdParam);
     $js = str_replace(array_keys($fields), $fields, $script);
     $jquery->addOnLoad($js);
     $buttons = \Mutil_Html::div();
     $buttons->class = 'buttons pull-right';
     $buttons->div($this->getActionController()->_('Sort'), array('id' => 'sort', 'class' => "btn"));
     $buttons->div($this->getActionController()->_('Ok'), array('id' => 'sort-ok', 'class' => "btn btn-success", 'style' => 'display:none;'));
     $buttons->div($this->getActionController()->_('Cancel'), array('id' => 'sort-cancel', 'class' => "btn btn-warning", 'style' => 'display:none;'));
     return $buttons;
 }
 /**
  * Create the snippets content
  *
  * This is a stub function either override getHtmlOutput() or override render()
  *
  * @param \Zend_View_Abstract $view Just in case it is needed here
  * @return \MUtil_Html_HtmlInterface Something that can be rendered
  */
 public function getHtmlOutput(\Zend_View_Abstract $view)
 {
     $div = \Mutil_Html::create('p');
     $div->append($this->getQuestion());
     $div->append(' ');
     $div->a(array($this->confirmParameter => 1), $this->_('Yes'), array('class' => $this->buttonYesClass));
     $div->append(' ');
     $div->a(array($this->request->getControllerKey() => $this->abortController ?: $this->request->getControllerName(), $this->request->getActionKey() => $this->abortAction ?: $this->request->getActionName()), $this->_('No'), array('class' => $this->buttonNoClass));
     return $div;
 }
 public function getChart()
 {
     $token = $this->token;
     $data = $this->data;
     $questions = $token->getSurvey()->getQuestionList($this->loader->getCurrentUser()->getLocale());
     $surveyName = $token->getSurveyName();
     // Convert questioncode to array if not already so
     $questionCodes = $this->question_code;
     if (!is_array($questionCodes)) {
         $questionCodes = (array) $questionCodes;
     }
     if (count($questionCodes) > 1) {
         $grouped = true;
     } else {
         $grouped = false;
     }
     if (empty($this->question_text)) {
         if ($grouped) {
             $question = join(', ', $questionCodes);
         } else {
             $question = isset($questions[$questionCodes[0]]) ? $questions[$questionCodes[0]] : $questionCodes[0];
         }
     } else {
         $question = $this->question_text;
     }
     $html = \MUtil_Html::create();
     $wrapper = $html->div(null, array('class' => 'barchart-wrapper'));
     $wrapper->div('', array('class' => 'header'))->append($surveyName)->append($html->br())->append($html->i($question));
     $chart = $wrapper->div(null, array('class' => 'barchart'));
     // the legend, only for printing since it is a hover for screen
     $legend = $html->div('', array('class' => 'legend'));
     $legendrow = $html->div('', array('class' => 'legendrow header'));
     $legendrow[] = $html->div($this->_('Date'), array('class' => 'date'));
     $legendrow[] = $html->div($this->_('Round'), array('class' => 'round', 'renderClosingTag' => true));
     if ($grouped) {
         $legendrow[] = $html->div($this->_('Code'), array('class' => 'code', 'renderClosingTag' => true));
     }
     $legendrow[] = $html->div($this->_('Value'), array('class' => 'value', 'renderClosingTag' => true));
     $legend[] = $legendrow;
     $range = $this->max - $this->min;
     $maxcols = 5;
     $chart[] = $html->div($this->max, array('class' => 'max'));
     $chart[] = $html->div($this->min, array('class' => 'min'));
     $this->doRulers($chart);
     foreach ($data as $row) {
         $token = $this->loader->getTracker()->getToken($row)->refresh();
         if ($token->getReceptionCode()->isSuccess() && $token->isCompleted()) {
             $answers = $token->getRawAnswers();
             foreach ($questionCodes as $idx => $questionCode) {
                 if (array_key_exists($questionCode, $answers)) {
                     $value = (double) $answers[$questionCode];
                     // Cast to number
                     $height = max(min($this->getPercentage($value), 100), 10);
                     // Add some limits
                     $valueBar = $html->div('', array('class' => 'bar col' . ($idx % $maxcols + 1), 'style' => sprintf('height: %s%%;', $height), 'onclick' => 'location.href=\'' . new \MUtil_Html_HrefArrayAttribute(array('controller' => 'track', 'action' => 'answer', \MUtil_Model::REQUEST_ID => $token->getTokenId())) . '\';'));
                     $date = $token->getCompletionTime()->get('dd-MM-yyyy HH:mm');
                     $info = $html->div($date, array('class' => 'info'));
                     $info[] = $html->br();
                     if ($grouped) {
                         $question = isset($questions[$questionCode]) ? $questions[$questionCode] : '';
                         $info[] = $questionCode;
                         $info[] = $html->br();
                         $info[] = $question;
                         $info[] = $html->br();
                     }
                     $info[] = $answers[$questionCode];
                     // The raw answer
                     $info[] = $html->br();
                     $info[] = $token->getRoundDescription();
                     $legendrow = $html->div('', array('class' => 'legendrow'));
                     $legendrow[] = $html->div($date, array('class' => 'date', 'renderClosingTag' => true));
                     $legendrow[] = $html->div($token->getRoundDescription(), array('class' => 'round', 'renderClosingTag' => true));
                     if ($grouped) {
                         $legendrow[] = $html->div($questionCode, array('class' => 'code', 'renderClosingTag' => true));
                     }
                     $legendrow[] = $html->div($answers[$questionCode], array('class' => 'value', 'renderClosingTag' => true));
                     $legend[] = $legendrow;
                     if (empty($value)) {
                         $value = 'N/A';
                     }
                     // Link the value to the answer view in a new window (not the bar to avoid usability issues on touch devices)
                     //$valueBar[] = $html->a(array('controller'=>'track', 'action'=>'answer', \MUtil_Model::REQUEST_ID => $token->getTokenId()), array('target'=>'_blank'), $html->div($value, array('class'=>'value')));
                     $valueBar[] = $info;
                     $chart[] = $valueBar;
                 }
             }
             // Add spacer between (groups of) bars
             if ($grouped) {
                 $class = 'spacer wide bar';
             } else {
                 $class = 'spacer bar';
             }
             $chart[] = $html->div(Mutil_Html::raw(' '), array('class' => $class));
         }
     }
     $wrapper[] = $legend;
     return $wrapper;
 }