/** * Crea un archivo PDF a partir de un quiz, agregando una hoja de respuestas de opción múltiple * * @param unknown $cm * @param string $debug * @param string $context * @param string $course * @param string $logofilepath * @param boolean $answersheetsonly * @return void|NULL */ function emarking_create_quiz_pdf($cm, $debug = false, $context = null, $course = null, $answersheetsonly = false, $pbar = false) { global $DB, $CFG, $OUTPUT; // Inclusión de librerías require_once $CFG->dirroot . '/mod/assign/feedback/editpdf/fpdi/fpdi2tcpdf_bridge.php'; require_once $CFG->dirroot . '/mod/assign/feedback/editpdf/fpdi/fpdi.php'; require_once $CFG->libdir . '/pdflib.php'; require_once $CFG->dirroot . '/mod/quiz/locallib.php'; require_once $CFG->dirroot . '/mod/emarking/print/locallib.php'; $filedir = $CFG->dataroot . "/temp/emarking/{$context->id}"; emarking_initialize_directory($filedir, true); $fileimg = $CFG->dataroot . "/temp/emarking/{$context->id}/qr"; emarking_initialize_directory($fileimg, true); $userimgdir = $CFG->dataroot . "/temp/emarking/{$context->id}/u"; emarking_initialize_directory($userimgdir, true); $logofile = emarking_get_logo_file(); $logofilepath = $logofile ? emarking_get_path_from_hash($filedir, $logofile->get_pathnamehash()) : null; $fullhtml = array(); $numanswers = array(); $attemptids = array(); $images = array(); $imageshtml = array(); $users = emarking_get_enroled_students($course->id); if ($pbar) { echo $OUTPUT->heading(get_string('loadingquestions', 'mod_emarking'), 3); $progressbar = new progress_bar(); $progressbar->create(); $progressbar->update(0, count($users), get_string('processing', 'mod_emarking')); } $current = 0; foreach ($users as $user) { $current++; if ($pbar) { $progressbar->update($current, count($users), "{$user->firstname}, {$user->lastname}"); } // Get the quiz object $quizobj = quiz::create($cm->instance, $user->id); // Create the new attempt and initialize the question sessions $attemptnumber = 1; $lastattempt = null; $timenow = time(); // Update time now, in case the server is running really slowly. $attempts = quiz_get_user_attempts($quizobj->get_quizid(), $user->id, 'all'); $numattempts = count($attempts); foreach ($attempts as $attempt) { $attemptobj = quiz_attempt::create($attempt->id); $slots = $attemptobj->get_slots(); foreach ($slots as $slot) { $qattempt = $attemptobj->get_question_attempt($slot); $question = $qattempt->get_question(); if ($question->get_type_name() === 'multianswer') { $q = $question->subquestions[1]; $numanswers[$user->id][] = count($q->answers); } else { if ($question->get_type_name() === 'multichoice') { $numanswers[$user->id][] = count($question->answers); } } $attemptids[$user->id] = $attempt->id; $qhtml = $attemptobj->render_question($slot, false); $qhtml = emarking_clean_question_html($qhtml); $currentimages = emarking_extract_images_url($qhtml); $idx = 0; foreach ($currentimages[1] as $imageurl) { if (!array_search($imageurl, $images)) { $images[] = $imageurl; $imageshtml[] = $currentimages[0][$idx]; } $idx++; } $fullhtml[$user->id][] = $qhtml; } // One attempt per user break; } } $save_to = $CFG->tempdir . '/emarking/printquiz/' . $cm->id . '/'; emarking_initialize_directory($save_to, true); // Bajar las imágenes del HTML a dibujar $search = array(); $replace = array(); $replaceweb = array(); $imagesize = array(); $idx = 0; if ($pbar) { $progressbar->update_full(100, get_string('finished', 'mod_emarking')); echo $OUTPUT->heading(get_string('downloadingimages', 'mod_emarking'), 3); $progressbar = new progress_bar(); $progressbar->create(); $progressbar->update(0, count($images), get_string('processing', 'mod_emarking')); } foreach ($images as $image) { if ($pbar) { $imagefilename = explode("/", $image); $progressbar->update($idx + 1, count($images), $imagefilename[count($imagefilename) - 1]); } // Si solamente incluiremos hojas de respuesta terminamos el ciclo if ($answersheetsonly) { break; } if (!(list($filename, $imageinfo) = emarking_get_file_from_url($image, $save_to))) { echo "Problem downloading file {$image} <hr>"; } else { // Buscamos el src de la imagen $search[] = 'src="' . $image . '"'; $replacehtml = ' src="' . $filename . '"'; $replacehtmlxweb = ' src="' . $image . '"'; // Si el html de la misma contiene ancho o alto, se deja tal cual $imghtml = $imageshtml[$idx]; if (substr_count($imghtml, "width") + substr_count($imghtml, "height") == 0) { $width = $imageinfo[0]; $height = $imageinfo[1]; $ratio = floatval(10) / floatval($height); $height = 10; $width = (int) ($ratio * floatval($width)); $sizehtml = 'width="' . $width . '" height="' . $height . '"'; $replacehtml = $sizehtml . ' ' . $replacehtml; $replacehtmlxweb = $sizehtml . ' ' . $replacehtmlxweb; } $replace[] = $replacehtml; $replaceweb[] = $replacehtmlxweb; $imagesize[] = $imageinfo; } $idx++; } if ($debug) { foreach ($fullhtml as $uid => $questions) { $index = 0; foreach ($questions as $question) { echo str_replace($search, $replaceweb, $fullhtml[$uid][$index]); $index++; } } return; } // Now we create the pdf file with the modified html $doc = new FPDI(); $doc->setPrintHeader(false); $doc->setPrintFooter(false); $doc->SetFont('times', '', 12); // set margins $doc->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $doc->SetHeaderMargin(250); $doc->SetFooterMargin(PDF_MARGIN_FOOTER); if ($pbar) { $progressbar->update_full(100, get_string('finished', 'mod_emarking')); echo $OUTPUT->heading(get_string('creatingpdffile', 'mod_emarking'), 3); $progressbar = new progress_bar(); $progressbar->create(); } $current = 0; foreach ($fullhtml as $uid => $questions) { $current++; $stinfo = $DB->get_record('user', array('id' => $uid)); $stinfo->name = $stinfo->firstname . ' ' . $stinfo->lastname; $stinfo->picture = emarking_get_student_picture($stinfo, $userimgdir); $stinfo->idnumber = $uid . '-' . $attemptids[$uid]; if ($pbar) { $progressbar->update($current, count($fullhtml), $stinfo->name); } $groups = groups_get_user_groups($course->id, $uid); if ($groups && isset($groups[0][0]) && ($group = $DB->get_record('groups', array('id' => $groups[0][0])))) { $stinfo->group = $group->name; } else { $stinfo->group = ''; } emarking_add_answer_sheet($doc, $filedir, $stinfo, $logofilepath, null, $fileimg, $course, $quizobj->get_quiz_name(), $numanswers[$uid], $attemptids[$uid]); // Una vez agregada la página de respuestas, si es todo lo que hay que hacer saltar al siguiente if ($answersheetsonly) { continue; } $doc->AddPage(); emarking_draw_header($doc, $stinfo, $quizobj->get_quiz_name(), 2, $fileimg, $logofilepath, $course, null, false, 0); $doc->SetFont('times', '', 12); $doc->SetAutoPageBreak(true); $doc->SetXY(PDF_MARGIN_LEFT, 40); $index = 0; foreach ($questions as $question) { $prevy = $doc->getY(); $fullhtml[$uid][$index] = str_replace($search, $replace, $fullhtml[$uid][$index]); $doc->writeHTML($fullhtml[$uid][$index]); $y = $doc->getY(); $fmargin = $doc->getFooterMargin(); $height = $doc->getPageHeight(); $spaceleft = $height - $fmargin - $y; $questionsize = $y - $prevy; if ($spaceleft < 70) { $doc->AddPage(); } $index++; } } if ($pbar) { $progressbar->update_full(100, get_string('finished', 'mod_emarking')); } $qid = $quizobj->get_quizid(); $pdfquizfilename = 'quiz-' . $qid . '-' . random_string() . '.pdf'; $fs = get_file_storage(); $filerecord = array('component' => 'mod_emarking', 'filearea' => 'pdfquiz', 'contextid' => $context->id, 'itemid' => $quizobj->get_quizid(), 'filepath' => '/', 'filename' => $pdfquizfilename); $doc->Output($filedir . '/' . $pdfquizfilename, 'F'); $file = $fs->create_file_from_pathname($filerecord, $filedir . '/' . $pdfquizfilename); $downloadurl = moodle_url::make_file_url("{$CFG->wwwroot}/pluginfile.php", "/{$context->id}/mod_emarking/pdfquiz/{$qid}/{$pdfquizfilename}", null, true); return $downloadurl; }
if (!($user = $DB->get_record('user', array('id' => $userid)))) { echo "Invalid user {$userid}<br>"; continue; } emarking_insert_user_answers($choices, $user, $attemptid); } echo $OUTPUT->notification(get_string('csvimportsuccessfull', 'mod_emarking'), 'notifysuccess'); echo $OUTPUT->single_button(new moodle_url('/mod/emarking/orm/processomr.php', array('cmid' => $cm->id, 'finish' => true)), get_string('finish', 'mod_emarking')); } else { $answersform->display(); } echo $OUTPUT->footer(); die; } // Get the users enrolled $users = emarking_get_enroled_students($course->id); $pbar = new progress_bar(); $pbar->create(); if ($create) { quiz_delete_all_attempts($quiz); } $cur = 1; $total = count($users); // Insert answers or finish the attempt for each student foreach ($users as $user) { $pbar->update($cur, $total, get_string('processing', 'mod_emarking') . $user->lastname . $user->firstname); flush(); // Get the quiz instance for the specific student $quizobj = quiz::create($cm->instance, $user->id); // Get all the attempts $attempts = quiz_get_user_attempts($quizobj->get_quizid(), $user->id, 'all');