// create DB object
$DB =& Database::singleton();
if (PEAR::isError($DB)) {
    return PEAR::raiseError("Could not connect to database: " . $DB->getMessage());
}
// user is logged in, let's continue with the show...
$user =& User::singleton($_SESSION['State']->getUsername());
if (PEAR::isError($user)) {
    die("Error creating user object: " . $user->getMessage());
}
// check permissions
if ($user->hasPermission('imaging_browser_qc')) {
    $tpl_data['has_permission'] = true;
}
// instantiate feedback mri object
$comments = new FeedbackMRI($_REQUEST['fileID'], $_REQUEST['sessionID']);
/*
 * UPDATE SECTION
 */
if ($_POST['fire_away'] && $user->hasPermission('imaging_browser_qc')) {
    // clear all predefined comments
    $comments->clearAllComments();
    // set selected predefined comments
    $comments->setPredefinedComments($_POST['savecomments']['predefined']);
    // save all textual comments but only if there is an entry [sebas]
    foreach ($_POST['savecomments']['text'] as $comment_type_id => $comment_message) {
        if (trim($comment_message)) {
            $comments->addTextComment(trim($comment_message), $comment_type_id);
        }
    }
    // save all comment status fields
 /**
  * Builds data dictionary for given scan types
  *
  * @param array $types list of scan types
  *
  * @return void
  */
 private function _buildDataDictionary($types)
 {
     $this->Dictionary = array('QCComment' => array('Type' => 'varchar(255)', 'Description' => 'QC Comment for Session'));
     /* creating dummy mri feedback object so all the mri feedback
      *  types can be added to the dictionary */
     $mri_feedback = new FeedbackMRI(1, "");
     foreach ($types as $type) {
         $ScanType = $type['ScanType'];
         $SelectedArray = array('Type' => 'varchar(255)', 'Description' => "Selected {$ScanType} file for session", 'IsFile' => true);
         $QCStatusArray = array('Type' => "enum('Pass', 'Fail')", 'Description' => "QC Status for {$ScanType} file");
         $this->Dictionary["Selected_{$ScanType}"] = $SelectedArray;
         $this->Dictionary[$ScanType . "_QCStatus"] = $QCStatusArray;
         $feedback_types = $mri_feedback->getAllCommentTypes();
         foreach ($feedback_types as $CommentTypeID => $comments) {
             if (!empty($comments['field'])) {
                 $fieldName = $comments['field'];
                 $type = "enum ('" . implode("','", $comments['values']) . "')";
                 $cmt_field = "Comment_" . $fieldName . "_{$ScanType}";
                 $scanfield = "{$fieldName} {$ScanType}";
                 $cmt_Array = array('Type' => 'varchar(255)', 'Description' => "Overall Comment for {$scanfield}");
                 $this->Dictionary[$cmt_field] = $cmt_Array;
             } else {
                 $fieldName = $comments['name'];
                 $type = 'varchar(255)';
             }
             $this->feedback_Comments[$CommentTypeID] = $fieldName;
             $cmt_scanArray = array('Type' => $type, 'Description' => $comments['name'] . " {$ScanType}");
             $this->Dictionary[$fieldName . "_{$ScanType}"] = $cmt_scanArray;
             $preDefinedComments = $mri_feedback->getAllPredefinedComments($CommentTypeID);
             $this->feedback_PreDefinedComments[$CommentTypeID] = array();
             $pre = array();
             foreach ($preDefinedComments as $preDefinedCommentTypeID => $preDefinedComment) {
                 $preDef_field = $preDefinedComment['field'] . "_{$ScanType}";
                 $preDef_cmt = $preDefinedComment['Comment'];
                 $preDef_Array = array('Type' => "enum('Yes', 'No')", 'Description' => "{$preDef_cmt} {$ScanType}");
                 $this->Dictionary[$preDef_field] = $preDef_Array;
                 $pre[$preDefinedCommentTypeID] = $preDefinedComment['field'];
             }
             $this->feedback_PreDefinedComments[$CommentTypeID] = $pre;
         }
         $mri_array = array('ScannerID' => 'Scanner ID', 'Pipeline' => 'Pipeline', 'OutputType' => 'Output Type', 'AcquisitionProtocol' => 'Protocol', 'CoordinateSpace' => 'Space', 'Algorithm' => 'Algorithm', 'AcquisitionDate' => 'Acquisition Date', 'FileInsertDate' => 'Insert date', 'SeriesDescription' => 'Series Description', 'SeriesNumber' => 'Series Number', 'EchoTime' => 'Echo Time', 'RepetitionTime' => 'Repetition Time', 'SliceThickness' => 'Slice Thickness', 'Time' => 'No of volumes', 'Comment' => 'Comment', 'SlicewiseRejected' => 'Slicewise correlations (Nb)', 'InterlaceRejected' => 'Interlace correlations (Nb)', 'IntergradientReject' => 'Gradient-wise correlations (Nb)', 'TotalRejected' => 'No. of rejected directions', 'Caveat' => 'Caveat', 'ProcessingPipeline' => 'Processing Pipleline');
         $this->mri_header_fields = $mri_array;
         foreach ($this->mri_header_fields as $field => $desc) {
             $mri_field = $field . "_{$ScanType}";
             $this->Dictionary[$mri_field] = array('Type' => "varchar(255)", 'Description' => $desc . " {$ScanType}");
         }
     }
 }