Ejemplo n.º 1
0
/**
 * Replace variable names using $nameMap in the tables of $questionId.
 */
function wrsqz_replaceVarReferencesInDatabase($questionType, $questionId, $nameMap){
    $extradbfields = array(
        'essay'       => array(),
        'match'       => array(
                         'question_match_sub'=>array('questiontext','answertext')
                         ),
        'multianswer' => array(),
        'multichoice' => array(
                         'question_multichoice'=>array('correctfeedback','partiallycorrectfeedback','incorrectfeedback'),
                         'question_answers'=>array('answer','feedback')
                         ),
        'shortanswer' => array(
                         'question_answers'=>array('answer','feedback')
                         ),
        'truefalse'   => array(
                         'question_answers'=>array('answer','feedback')
                         )
        );
    //update common text fields
    $question = get_record('question','id',$questionId);
    $question->questiontext = wrsqz_replaceVars($nameMap,$question->questiontext);
    $question->generalfeedback = wrsqz_replaceVars($nameMap,$question->generalfeedback);
    update_record('question', $question);

    //update type specific text fields
    $textfields = $extradbfields[$questionType];
    foreach($textfields as $table=>$fields){
        $values = get_records($table,'question',$questionId);
        foreach($values as $value){
            foreach($fields as $field){
                $value->$field = wrsqz_replaceVars($nameMap,$value->$field);
            }
            update_record($table, $value);
        }
    }
    //update special-encoded fields
    if($questionType == 'shortanswer' || $questionType=='multianswer'){
        $table = 'question_'.$questionType.'wiris';
        $wirisrecord = get_record($table,'question',$questionId);
        mb_parse_str($wirisrecord->eqoption, $eqoptionArray);
        if(isset($eqoptionArray['testFunctionName'])){
            foreach($eqoptionArray['testFunctionName'] as $index=>$value){
                $eqoptionArray['testFunctionName'][$index] = wrsqz_replaceVars($nameMap,$value, false);
            }
        }
        if(function_exists('http_build_query')){
            $wirisrecord->eqoption = http_build_query($eqoptionArray,null,'&');
        }else{
            $wirisrecord->eqoption = wrsqz_http_build_query($eqoptionArray);
        }
        if(!isset($wirisrecord->eqoption) || $wirisrecord->eqoption == null){
            $wirisrecord->eqoption = '';
        }
        update_record($table, $wirisrecord);
    }

    //recursive calls for multianswer question
    if($questionType == 'multianswer'){
        $subquestions = get_records('question','parent',$questionId,'id,qtype');
        foreach($subquestions as $id=>$subquestion){
            $qtype = $subquestion->qtype;
            if(substr($qtype,-5)=='wiris'){
                $qtype = substr($qtype,0,-5);
            }
            wrsqz_replaceVarReferencesInDatabase($qtype, $id, $nameMap);
        }
    }
    
}
function wrsqz_restore($questionType, $dbType, &$old_question_id, &$new_question_id, &$info, &$restore) {
	if (array_key_exists(strtoupper($questionType) . 'WIRIS', $info['#'])) {
		$questionList = $info['#'][strtoupper($questionType) . 'WIRIS'];
	}else{
		$questionList = array();
	}
	
	$questionList_length = count($questionList);
	$status = true;
	
	for($i = 0; $i < $questionList_length; ++$i) {
		$question = $questionList[$i];
		$problem = new stdClass;

		if ($questionType == 'truefalse' || $questionType == 'multichoice') {
			$problem->override = backup_todb($question['#']['WIRISOVERRIDEANSWER']['0']['#']);
		}else if ($questionType == 'shortanswer') {
			$eqoption = backup_todb($question['#']['WIRISEDITOR']['0']['#']);
      mb_parse_str($eqoption,$eqoptionArray);
      if(isset($eqoptionArray['testFunction'])){ //recompute testFunction indexes with new answer id's
        $testFunctions = array();
        foreach($eqoptionArray['testFunction'] as $oldid=>$testFunction ){
          $newid = backup_getid($restore->backup_unique_code,'question_answers',$oldid);
          if($newid!==false){
            $testFunctions[$newid->new_id]=$testFunction;
          }
        }
        $eqoptionArray['testFunction'] = $testFunctions;
        $eqoption = http_build_query($eqoptionArray,null,'&');
             
        if(!isset($eqoption) || $eqoption == null){
          $eqoption='';
        }
      }
      $problem->eqoption = $eqoption;
		}else if ($questionType == 'multianswer') {
            //TODO
            //$problem->eqoption = backup_todb($question['#']['WIRISEDITOR']['0']['#']);
    }
    
    //restore options
    $optionsxml = $question['#']['WIRISOPTIONS'];
    $options = array();
    $keys = array(
      'WIRISCASFORCOMPUTATIONS'=>'wirisCASForComputations',
      'HIDDENINITIALCASVALUE'=>'hiddenInitialCASValue',
    );
    foreach($optionsxml as $option){
      foreach($keys as $keyxml=>$keyarray){
        if(isset($option['#'][$keyxml])){
          $options[$keyarray] = backup_todb($option['#'][$keyxml]['0']['#']);
        }
      }
    }
    if(isset($options['hiddenInitialCASValue'])){
      //TODO translate if needed.
      $options['hiddenInitialCASValue'] = wrsqz_mathmlDecode($options['hiddenInitialCASValue']);
    }
    $problem->options = serialize($options);

    $program = wrsqz_mathmlDecode(backup_todb($question['#']['WIRISPROGRAM']['0']['#']));
    $imported = wrsqz_importCASSession($program);
    if($imported === false){
      return false;
    }
    $program = $imported[0];
    $translation = $imported[1];
		$program = str_replace('"','\"',$program);

		$problem->id = 0;
		$problem->question = $new_question_id;
		$problem->idcache = 0;
    $problem->md5 = md5($program);
		
		$newid = insert_record('question_' . $questionType . 'wiris', $problem);
		
		$problem = new stdClass;
		$problem->id = 0;
		$problem->question = $new_question_id;
		$problem->program = $program;
		
		if ((insert_record('question_' . $dbType, $problem) === false) || !$newid) {
			$status = false;
		}

    if(!empty($translation)){
      wrsqz_replaceVarReferencesInDatabase($questionType, $new_question_id, $translation);
    }
	}
	return $status;
}