Esempio n. 1
0
/**
 * Main function to import an exercise.
 *
 * @param string $file path of the file
 * @param array $backlog_message
 *
 * @return the id of the created exercise or false if the operation failed
 */
function import_exercise($file, &$backlog)
{
    global $exercise_info;
    global $element_pile;
    global $non_HTML_tag_to_avoid;
    global $record_item_body;
    // used to specify the question directory where files could be found in relation in any question
    global $questionTempDir;
    // get required table names
    $tbl_cdb_names = get_module_course_tbl(array('qwz_exercise', 'qwz_question'), claro_get_current_course_id());
    $tbl_quiz_exercise = $tbl_cdb_names['qwz_exercise'];
    $tbl_quiz_question = $tbl_cdb_names['qwz_question'];
    // paths
    $baseWorkDir = get_path('rootSys') . get_conf('tmpPathSys') . 'upload/';
    // create temp dir for upload
    if (!file_exists($baseWorkDir)) {
        claro_mkdir($baseWorkDir, CLARO_FILE_PERMISSIONS);
    }
    $uploadDir = claro_mkdir_tmp($baseWorkDir);
    // this function should return the dir name and not the full path ...
    $uploadPath = str_replace($baseWorkDir, '', $uploadDir);
    // set some default values for the new exercise
    $exercise_info = array();
    $exercise_info['name'] = preg_replace('/.zip$/i', '', $file);
    $exercise_info['description'] = '';
    $exercise_info['question'] = array();
    // create parser and array to retrieve info from manifest
    $element_pile = array();
    //pile to known the depth in which we are
    $module_info = array();
    //array to store the info we need
    // if file is not a .zip, then we cancel all
    if (!preg_match('/.zip$/i', $_FILES['uploadedExercise']['name'])) {
        $backlog->failure(get_lang('You must upload a zip file'));
        return false;
    }
    //unzip the uploaded file in a tmp directory
    if (!get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath)) {
        $backlog->failure(get_lang('Upload failed'));
        return false;
    }
    // find the different manifests for each question and parse them.
    $exerciseHandle = opendir($uploadDir);
    $question_number = 0;
    $file_found = false;
    // parse every subdirectory to search xml question files
    while (false !== ($file = readdir($exerciseHandle))) {
        if (is_dir($uploadDir . '/' . $file) && $file != "." && $file != "..") {
            //find each manifest for each question repository found
            $questionHandle = opendir($uploadDir . '/' . $file);
            while (false !== ($questionFile = readdir($questionHandle))) {
                if (preg_match('/.xml$/i', $questionFile)) {
                    list($parsingBacklog, $success) = parse_file($uploadDir, $file, $questionFile);
                    $backlog->append($parsingBacklog);
                    $file_found = true;
                }
            }
        } elseif (preg_match('/.xml$/i', $file)) {
            list($parsingBacklog, $success) = parse_file($uploadDir, '', $file);
            $backlog->append($parsingBacklog);
            $file_found = true;
        }
        // else ignore file
    }
    if (!$file_found) {
        $backlog->failure(get_lang('No XML file found in the zip'));
        return false;
    }
    //---------------------
    //add exercise in tool
    //---------------------
    //1.create exercise
    $exercise = new Exercise();
    $exercise->setTitle($exercise_info['name']);
    $exercise->setDescription($exercise_info['description']);
    if ($exercise->validate()) {
        $exercise_id = $exercise->save();
    } else {
        $backlog->failure(get_lang('There is an error in exercise data of imported file.'));
        return false;
    }
    ksort($exercise_info['question'], SORT_NUMERIC);
    //For each question found...
    foreach ($exercise_info['question'] as $key => $question_array) {
        //2.create question
        $question = new Qti2Question();
        $question->import($question_array);
        if ($question->validate()) {
            // I need to save the question after the answer because I need question id in answers
            $question_id = $question->save();
            //3.create answers
            $question->setAnswer();
            $question->answer->import($question_array);
            if ($question->answer->validate()) {
                $question->setGrade($question->answer->getGrade());
                $question->save();
                // save computed grade
                $question->answer->save();
                $exercise->addQuestion($question_id);
            } else {
                $backlog->failure(get_lang('Invalid answer') . ' : ' . $key);
            }
        } else {
            $backlog->failure(get_lang('Invalid question') . ' : ' . $key);
        }
    }
    // delete the temp dir where the exercise was unzipped
    claro_delete_file($uploadDir);
    return $exercise_id;
}
Esempio n. 2
0
$displaySettings = true;
/*
 * Execute commands
 */
if ($cmd == 'rmQu' && !is_null($quId)) {
    $exercise->removeQuestion($quId);
}
if ($cmd == 'mvUp' && !is_null($quId)) {
    $exercise->moveQuestionUp($quId);
}
if ($cmd == 'mvDown' && !is_null($quId)) {
    $exercise->moveQuestionDown($quId);
}
if ($cmd == 'exEdit') {
    $exercise->setTitle($_REQUEST['title']);
    $exercise->setDescription($_REQUEST['description']);
    $exercise->setDisplayType($_REQUEST['displayType']);
    if (isset($_REQUEST['randomize']) && $_REQUEST['randomize']) {
        $exercise->setShuffle($_REQUEST['questionDrawn']);
    } else {
        $exercise->setShuffle(0);
    }
    if (isset($_REQUEST['useSameShuffle']) && $_REQUEST['useSameShuffle']) {
        $exercise->setUseSameShuffle($_REQUEST['useSameShuffle']);
    } else {
        $exercise->setUseSameShuffle(0);
    }
    $exercise->setShowAnswers($_REQUEST['showAnswers']);
    $exercise->setStartDate(mktime($_REQUEST['startHour'], $_REQUEST['startMinute'], 0, $_REQUEST['startMonth'], $_REQUEST['startDay'], $_REQUEST['startYear']));
    if (isset($_REQUEST['useEndDate']) && $_REQUEST['useEndDate']) {
        $exercise->setEndDate(mktime($_REQUEST['endHour'], $_REQUEST['endMinute'], 0, $_REQUEST['endMonth'], $_REQUEST['endDay'], $_REQUEST['endYear']));
Esempio n. 3
0
 $pdf->SetSubject(claro_utf8_encode($exercise->getTitle()));
 //set margins
 $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
 $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
 $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
 //set auto page breaks
 $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
 //set image scale factor
 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
 $pdf->setPrintHeader(false);
 // add a page
 $pdf->AddPage();
 $htmlcontent = '<div style="font-size: xx-large; font-weight: bold;">' . claro_htmlspecialchars($exercise->getTitle()) . '<div>' . "\n";
 $pdf->writeHTML(claro_utf8_encode($htmlcontent, get_conf('charset')), true, 0, true, 0);
 //change Img URL
 $exercise->setDescription(claro_utf8_encode(change_img_url_for_pdf($exercise->getDescription()), get_conf('charset')));
 //End change Img URL
 $htmlcontent = '<div style="font-size: normal; font-weight: normal;">' . $exercise->getDescription() . '</div><br /><br />' . "\n";
 $pdf->writeHTML(claro_utf8_encode($htmlcontent, get_conf('charset')), true, 0, true, 0);
 $i = 1;
 foreach ($questionList as $question) {
     $htmlcontent = '<p><table cellspacing="4">' . "\n" . '<tbody>' . "\n" . '<tr>' . "\n" . '<th colspan="2" style="text-align: center; font-weight: bold; color: #693; background-color: #DEEECE;">' . get_lang('Question') . ' ' . $i . '</th>' . "\n" . '</tr>' . "\n" . '<tr>' . "\n" . '<td colspan="2">' . claro_htmlspecialchars(strip_tags($question['title'])) . '</td>' . "\n" . '</tr>' . "\n";
     // Question description
     if (trim(claro_htmlspecialchars($question['description']))) {
         $htmlcontent .= '<tr>' . "\n" . '<td colspan="2" style="font-size: x-small; font-style: italic;">' . change_img_url_for_pdf(claro_parse_user_text($question['description'])) . '</td>' . "\n" . '</tr>' . "\n";
     }
     // Attachment
     if (!empty($question['attachment'])) {
         $extensionsList = array('jpg', 'jpeg', 'bmp', 'gif', 'png');
         $ext = strtolower(get_file_extension($question['attachment']));
         if (in_array($ext, $extensionsList)) {