<?php require_once('includes/requirecompere.inc.php'); require_once('includes/requirepost.inc.php'); $action=TPOST('action'); $q_id=TPOST('q_id'); $q_text=TPOST('q_text'); $q_type=TPOST('q_type'); $qc_delete=TPOST('q_delete'); $qc_ordinal=TPOST('qc_ordinal'); $qc_text=TPOST('qc_text'); $addchoice=TPOST('addchoice'); $mb_id=current_mb_id(); if ($action=='modify' && !$q_id) { redirect('./'); return; } $data=array( 'q_id'=>$q_id, 'q_text'=>$q_text, 'q_type'=>$q_type, 'qc_ordinal'=>array(), 'qc_text'=>array() ); $qc_arr=array(); if ($qc_text) { foreach ($qc_ordinal as $qcid=>$v) $qc_arr[$qcid]['ordinal']=$v; foreach ($qc_text as $qcid=>$t) $qc_arr[$qcid]['text']=$t; } function _compare_question_ordinal($a,$b) { return $a['ordinal']-$b['ordinal']; } uasort($qc_arr,'_compare_question_ordinal'); $ordinal=1; $newcount=0; foreach ($qc_arr as $qcid=>$v) { if ($qc_delete[$qcid] || !$v['text']) continue; if ($qcid[0]=='_') $qcid='_'.($newcount++); $data['qc_ordinal'][$qcid]=$ordinal++; $data['qc_text'][$qcid]=$v['text']; } $DBConn->connect(); $errorareas=array(); $question_addable=get_compere_limit($DBConn,$mb_id,'question.count')-db_count_compere_question($DBConn,$mb_id); if ($addchoice) { for ($i=0;$i<5;$i++) { $qcid='_'.($newcount++); $data['qc_ordinal'][$qcid]=$ordinal++; $data['qc_text'][$qcid]=''; } } else { $errorareas+=iif_array4(array( array(!$q_text,'ERROR-TEXT-INVALID',array(0)), array($q_type=='SINGLE' && sizeof($data['qc_text'])<2,'ERROR-CHOICE-NEEDMORE',array(0)), array($q_type=='MULTI' && sizeof($data['qc_text'])<1,'ERROR-CHOICE-NEEDMORE',array(0)), array($action=='addnew' && $question_addable<=0,'ERROR-COUNT-LIMIT',array(0)) )); } if ($addchoice || count_true_entries($errorareas)>0) { $_SESSION['reserv.inputcache.domain']='question.addnew'; $_SESSION['reserv.inputcache.data']=$data; $_SESSION['reserv.inputcache.errorareas']=$errorareas; if ($action=='addnew') redirect("qx-cmd-question.addnew-op-revise.html"); else if ($action=='modify') redirect("qx-cmd-question.modify-q_id-".$q_id."-op-revise.html"); return; } $DBConn->begin(true); $continue_ok=true; if ($action=="addnew") { $q_id=db_insert_question($DBConn,$mb_id,$q_type,$q_text); } else if ($action=="modify") { $question=db_load_question_by_id($DBConn,$q_id); if ($question['mb_id_owner']==$mb_id) db_update_question($DBConn,$q_id,$mb_id,$q_text); else $continue_ok=false; } if ($continue_ok) { $ordinal=1; foreach ($data['qc_text'] as $qc_id=>$qc_text) { if ($qc_id[0]=='_') db_insert_question_choice($DBConn,$q_id,$ordinal++,$qc_text); else db_update_question_choice($DBConn,$mb_id,$qc_id,$ordinal++,$qc_text); } db_delete_question_choice($DBConn,$qc_ids); } purge_array_by_key_prefix($_SESSION,'reserv.inputcache.'); $DBConn->commit(); redirect("qx-cmd-question.manage-op-question.saved-focus-question.html"); ?>