public function validation($data, $files) { global $CFG, $COURSE, $USER, $DB; require_once $CFG->dirroot . "/mod/emarking/print/locallib.php"; // Calculates context for validating permissions. // If we have the module available, we use it, otherwise we fallback to course. $ctx = context_course::instance($COURSE->id); $errors = array(); // Verify that we have enough markers. if ($data['type'] == EMARKING_TYPE_MARKER_TRAINING) { // Get all users with permission to grade in emarking. $markers = get_enrolled_users($ctx, 'mod/emarking:grade'); $totalmarkers = 0; foreach ($markers as $marker) { if (has_capability('mod/emarking:supervisegrading', $ctx, $marker)) { continue; } $totalmarkers++; } if ($totalmarkers < 2) { $errors['type'] = get_string('notenoughmarkersfortraining', 'mod_emarking'); return $errors; } return $errors; } if ($data['type'] == EMARKING_TYPE_PEER_REVIEW) { // Get all users with permission to grade in emarking. $totalstudents = emarking_get_students_count_for_printing($COURSE->id); if ($totalstudents < 2) { $errors['type'] = get_string('notenoughstudenstforpeerreview', 'mod_emarking'); return $errors; } } if ($data['type'] == EMARKING_TYPE_ON_SCREEN_MARKING || $data['type'] == EMARKING_TYPE_PEER_REVIEW || $data['type'] == EMARKING_TYPE_PRINT_SCAN) { // Get all users with permission to grade in emarking. if (!isset($data['headerqr'])) { $errors['headerqr'] = get_string('headerqrrequired', 'mod_emarking'); return $errors; } } $errors = array_merge($errors, $this->get_exam_date_errors($data)); $errors = array_merge($errors, $this->get_upload_files_errors($data)); $errors = array_merge($errors, $this->get_grades_errors($data)); $errors = array_merge($errors, $this->get_slope_errors($data)); $errors = array_merge($errors, $this->get_regrade_dates_errors($data)); $errors = array_merge($errors, $this->get_custom_marks_errors($data)); $errors = array_merge($errors, $this->get_markers_errors($data, $ctx)); return $errors; }
/** * * @param unknown $emarking * @param unknown $student * @param unknown $context * @return Ambigous <mixed, stdClass, false, boolean>|stdClass */ function emarking_get_or_create_submission($emarking, $student, $context) { global $DB, $USER; if ($submission = $DB->get_record('emarking_submission', array('emarking' => $emarking->id, 'student' => $student->id))) { return $submission; } $submission = new stdClass(); $submission->emarking = $emarking->id; $submission->student = $student->id; $submission->status = EMARKING_STATUS_SUBMITTED; $submission->timecreated = time(); $submission->timemodified = time(); $submission->teacher = $USER->id; $submission->generalfeedback = NULL; $submission->grade = $emarking->grademin; $submission->sort = rand(1, 9999999); $submission->id = $DB->insert_record('emarking_submission', $submission); // Normal marking - One draft default if ($emarking->type == EMARKING_TYPE_NORMAL || $emarking->type == EMARKING_TYPE_PRINT_SCAN) { $draft = new stdClass(); $draft->emarkingid = $emarking->id; $draft->submissionid = $submission->id; $draft->groupid = 0; $draft->timecreated = time(); $draft->timemodified = time(); $draft->grade = $emarking->grademin; $draft->sort = rand(1, 9999999); $draft->qualitycontrol = 0; $draft->teacher = 0; $draft->generalfeedback = NULL; $draft->status = EMARKING_STATUS_SUBMITTED; $DB->insert_record('emarking_draft', $draft); if ($emarking->qualitycontrol) { $qcdrafts = $DB->count_records('emarking_draft', array('emarkingid' => $emarking->id, 'qualitycontrol' => 1)); $totalstudents = emarking_get_students_count_for_printing($emarking->course); if (ceil($totalstudents / 4) > $qcdrafts) { $draft->qualitycontrol = 1; $DB->insert_record('emarking_draft', $draft); } } } else { if ($emarking->type == EMARKING_TYPE_MARKER_TRAINING) { // Get all users with permission to grade in emarking $markers = get_enrolled_users($context, 'mod/emarking:grade'); foreach ($markers as $marker) { if (has_capability('mod/emarking:supervisegrading', $context, $marker)) { continue; } $draft = new stdClass(); $draft->emarkingid = $emarking->id; $draft->submissionid = $submission->id; $draft->groupid = 0; $draft->timecreated = time(); $draft->timemodified = time(); $draft->grade = $emarking->grademin; $draft->sort = rand(1, 9999999); $draft->teacher = $marker->id; $draft->generalfeedback = NULL; $draft->status = EMARKING_STATUS_SUBMITTED; $DB->insert_record('emarking_draft', $draft); } } else { if ($emarking->type == EMARKING_TYPE_STUDENT_TRAINING) { // Get all users with permission to grade in emarking $students = get_enrolled_users($context, 'mod/emarking:submit'); foreach ($students as $student) { $draft = new stdClass(); $draft->emarkingid = $emarking->id; $draft->submissionid = $submission->id; $draft->groupid = 0; $draft->timecreated = time(); $draft->timemodified = time(); $draft->grade = $emarking->grademin; $draft->sort = rand(1, 9999999); $draft->teacher = $student->id; $draft->generalfeedback = NULL; $draft->status = EMARKING_STATUS_SUBMITTED; $DB->insert_record('emarking_draft', $draft); } } else { if ($emarking->type == EMARKING_TYPE_PEER_REVIEW) { // TODO: Implement peer review (this is a hard one) } } } } return $submission; }
/** * Saves a new instance of the emarking into the database * * Given an object containing all the necessary data, * (defined by the form in mod_form.php) this function * will create a new instance and return the id number * of the new instance. * * @param object $emarking * An object from the form in mod_form.php * @param mod_emarking_mod_form $mform * @return int The id of the newly inserted emarking record */ function emarking_add_instance(stdClass $data, mod_emarking_mod_form $mform = null) { global $DB, $CFG, $COURSE, $USER; $data->timecreated = time(); $id = $DB->insert_record('emarking', $data); $data->id = $id; emarking_grade_item_update($data); if ($data->type == EMARKING_TYPE_MARKER_TRAINING) { return $id; } foreach ($data as $k => $v) { $parts = explode('-', $k); if (count($parts) > 1 && $parts[0] === 'marker') { $markerid = intval($parts[1]); $marker = new stdClass(); $marker->emarking = $id; $marker->marker = $markerid; $marker->qualitycontrol = 1; $DB->insert_record('emarking_markers', $marker); } } $context = context_course::instance($COURSE->id); $examid = 0; // If there's no previous exam to associate, and we are creating a new // e-marking, we need the PDF file if ($data->exam == 0) { // We get the draftid from the form $draftid = file_get_submitted_draft_itemid('exam_files'); $usercontext = context_user::instance($USER->id); $fs = get_file_storage(); $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftid); $tempdir = emarking_get_temp_dir_path($COURSE->id); emarking_initialize_directory($tempdir, true); $numpagesprevious = -1; $exampdfs = array(); foreach ($files as $uploadedfile) { if ($uploadedfile->get_mimetype() !== 'application/pdf') { continue; } $filename = $uploadedfile->get_filename(); $filename = emarking_clean_filename($filename); $newfilename = $tempdir . '/' . $filename; $pdffile = emarking_get_path_from_hash($tempdir, $uploadedfile->get_pathnamehash()); // Executes pdftk burst to get all pages separated $numpages = emarking_pdf_count_pages($newfilename, $tempdir, false); $exampdfs[] = array('pathname' => $pdffile, 'filename' => $filename); } } else { $examid = $data->exam; } $studentsnumber = emarking_get_students_count_for_printing($COURSE->id); // A new exam object is created and its attributes filled from form data if ($examid == 0) { $exam = new stdClass(); $exam->course = $COURSE->id; $exam->courseshortname = $COURSE->shortname; $exam->name = $mform->get_data()->name; $exam->examdate = $mform->get_data()->examdate; $exam->emarking = $id; $exam->headerqr = isset($mform->get_data()->headerqr) ? 1 : 0; $exam->printrandom = isset($mform->get_data()->printrandom) ? 1 : 0; $exam->printlist = isset($mform->get_data()->printlist) ? 1 : 0; $exam->extrasheets = $mform->get_data()->extrasheets; $exam->extraexams = $mform->get_data()->extraexams; $exam->usebackside = isset($mform->get_data()->printdoublesided) ? 1 : 0; if ($examid == 0) { $exam->timecreated = time(); } $exam->timemodified = time(); $exam->requestedby = $USER->id; $exam->totalstudents = $studentsnumber; // Get the enrolments as a comma separated values $enrollist = array(); if (!empty($mform->get_data()->enrolments)) { $enrolments = $mform->get_data()->enrolments; foreach ($enrolments as $key => $enrolment) { if (!empty($enrolment)) { $enrollist[] = $key; } } } $exam->enrolments = implode(",", $enrollist); $exam->printdate = 0; $exam->status = EMARKING_EXAM_UPLOADED; // Calculate total pages for exam $exam->totalpages = $numpages; $exam->id = $DB->insert_record('emarking_exams', $exam); foreach ($exampdfs as $exampdf) { // Save the submitted file to check if it's a PDF $filerecord = array('component' => 'mod_emarking', 'filearea' => 'exams', 'contextid' => $context->id, 'itemid' => $exam->id, 'filepath' => '/', 'filename' => $exampdf['filename']); $file = $fs->create_file_from_pathname($filerecord, $exampdf['pathname']); } // Update exam object to store the PDF's file id $exam->file = $file->get_id(); if (!$DB->update_record('emarking_exams', $exam)) { $fs->delete_area_files($contextid, 'emarking', 'exams', $exam->id); print_error(get_string('errorsavingpdf', 'mod_emarking')); } // Send new print order notification emarking_send_newprintorder_notification($exam, $COURSE); // If it is a multi-course submission, insert several exams if (!empty($mform->get_data()->multicourse)) { $multicourse = $mform->get_data()->multicourse; foreach ($multicourse as $key => $mcourse) { if (!empty($key)) { if ($thiscourse = $DB->get_record('course', array('shortname' => $key))) { $studentsnumber = emarking_get_students_count_for_printing($thiscourse->id); $newexam = $exam; $newexam->id = null; $newexam->totalstudents = $studentsnumber; $newexam->course = $thiscourse->id; $newexam->courseshortname = $thiscourse->shortname; $newexam->emarking = 0; $newexam->id = $DB->insert_record('emarking_exams', $newexam); $thiscontext = context_course::instance($thiscourse->id); // Create file records for all new exams foreach ($exampdfs as $exampdf) { // Save the submitted file to check if it's a PDF $filerecord = array('component' => 'mod_emarking', 'filearea' => 'exams', 'contextid' => $thiscontext->id, 'itemid' => $newexam->id, 'filepath' => '/', 'filename' => $exampdf['filename']); $file = $fs->create_file_from_pathname($filerecord, $exampdf['pathname']); } // Send new print order notification emarking_send_newprintorder_notification($newexam, $thiscourse); } } } } } else { $exam = $DB->get_record("emarking_exams", array("id" => $examid)); $exam->emarking = $id; $exam->timemodified = time(); $DB->update_record('emarking_exams', $exam); } $headerqr = isset($mform->get_data()->headerqr) ? 1 : 0; setcookie("emarking_headerqr", $headerqr, time() + 3600 * 24 * 365 * 10, '/'); $defaultexam = new stdClass(); $defaultexam->headerqr = $exam->headerqr; $defaultexam->printrandom = $exam->printrandom; $defaultexam->printlist = $exam->printlist; $defaultexam->extrasheets = $exam->extrasheets; $defaultexam->extraexams = $exam->extraexams; $defaultexam->usebackside = $exam->usebackside; $defaultexam->enrolments = $exam->enrolments; setcookie("emarking_exam_defaults", json_encode($defaultexam), time() + 3600 * 24 * 365 * 10, '/'); return $id; }
/** * * @param unknown $emarking * @param unknown $student * @param unknown $context * @return Ambigous <mixed, stdClass, false, boolean>|stdClass */ function emarking_get_or_create_submission($emarking, $student, $context) { global $DB, $USER; if ($submission = $DB->get_record('emarking_submission', array('emarking' => $emarking->id, 'student' => $student->id))) { return $submission; } $tran = $DB->start_delegated_transaction(); $submission = new stdClass(); $submission->emarking = $emarking->id; $submission->student = $student->id; $submission->status = EMARKING_STATUS_SUBMITTED; $submission->timecreated = time(); $submission->timemodified = time(); $submission->teacher = $USER->id; $submission->generalfeedback = null; $submission->grade = $emarking->grademin; $submission->sort = rand(1, 9999999); $submission->answerkey = 0; $submission->id = $DB->insert_record('emarking_submission', $submission); // Normal marking - One draft default. if ($emarking->type == EMARKING_TYPE_ON_SCREEN_MARKING || $emarking->type == EMARKING_TYPE_PRINT_SCAN || $emarking->type == EMARKING_TYPE_PEER_REVIEW) { $draft = new stdClass(); $draft->emarkingid = $emarking->id; $draft->submissionid = $submission->id; $draft->groupid = 0; $draft->timecreated = time(); $draft->timemodified = time(); $draft->grade = $emarking->grademin; $draft->sort = rand(1, 9999999); $draft->qualitycontrol = 0; $draft->teacher = 0; $draft->generalfeedback = null; $draft->status = EMARKING_STATUS_SUBMITTED; if ($emarking->type == EMARKING_TYPE_PEER_REVIEW) { $draft->teacher = -1; } $draft->id = $DB->insert_record('emarking_draft', $draft); if ($emarking->qualitycontrol) { $qcdrafts = $DB->count_records('emarking_draft', array('emarkingid' => $emarking->id, 'qualitycontrol' => 1)); $totalstudents = emarking_get_students_count_for_printing($emarking->course); if (ceil($totalstudents / 4) > $qcdrafts) { $draft->qualitycontrol = 1; $DB->insert_record('emarking_draft', $draft); } } // Markers training - One draft per marker. } else { if ($emarking->type == EMARKING_TYPE_MARKER_TRAINING) { // Get all users with permission to grade in emarking. $markers = get_enrolled_users($context, 'mod/emarking:grade'); foreach ($markers as $marker) { if (has_capability('mod/emarking:supervisegrading', $context, $marker)) { continue; } $draft = new stdClass(); $draft->emarkingid = $emarking->id; $draft->submissionid = $submission->id; $draft->groupid = 0; $draft->timecreated = time(); $draft->timemodified = time(); $draft->grade = $emarking->grademin; $draft->sort = rand(1, 9999999); $draft->teacher = $marker->id; $draft->generalfeedback = null; $draft->status = EMARKING_STATUS_SUBMITTED; $DB->insert_record('emarking_draft', $draft); } // Students training. } else { if ($emarking->type == EMARKING_TYPE_STUDENT_TRAINING) { // Get all users with permission to grade in emarking. $students = get_enrolled_users($context, 'mod/emarking:submit'); foreach ($students as $student) { $draft = new stdClass(); $draft->emarkingid = $emarking->id; $draft->submissionid = $submission->id; $draft->groupid = 0; $draft->timecreated = time(); $draft->timemodified = time(); $draft->grade = $emarking->grademin; $draft->sort = rand(1, 9999999); $draft->teacher = $student->id; $draft->generalfeedback = null; $draft->status = EMARKING_STATUS_SUBMITTED; $DB->insert_record('emarking_draft', $draft); } } else { $e = new moodle_exception("Invalid emarking type"); $tran->rollback($e); throw $e; } } } $DB->commit_delegated_transaction($tran); return $submission; }
/** * Saves a new instance of the emarking into the database * Given an object containing all the necessary data, * (defined by the form in mod_form.php) this function * will create a new instance and return the id number * of the new instance. * * @param object $emarking * An object from the form in mod_form.php * @param mod_emarking_mod_form $mform * @return int The id of the newly inserted emarking record */ function emarking_add_instance(stdClass $data, mod_emarking_mod_form $mform = null) { global $DB, $CFG, $COURSE, $USER; $data->timecreated = time(); $id = $DB->insert_record('emarking', $data); $data->id = $id; emarking_grade_item_update($data); if ($data->type == EMARKING_TYPE_MARKER_TRAINING || $data->type == EMARKING_TYPE_PEER_REVIEW || !$mform) { return $id; } foreach ($data as $k => $v) { $parts = explode('-', $k); if (count($parts) > 1 && $parts[0] === 'marker') { $markerid = intval($parts[1]); $marker = new stdClass(); $marker->emarking = $id; $marker->marker = $markerid; $marker->qualitycontrol = 1; $DB->insert_record('emarking_markers', $marker); } } $context = context_course::instance($COURSE->id); $examid = 0; // If there's no previous exam to associate, and we are creating a new // EMarking, we need the PDF file. if ($data->exam == 0) { $examfiles = emarking_validate_exam_files_from_draft(); if (count($examfiles) == 0) { throw new Exception('Invalid PDF exam files'); } $numpages = $examfiles[0]['numpages']; } else { $examid = $data->exam; } $studentsnumber = emarking_get_students_count_for_printing($COURSE->id); // A new exam object is created and its attributes filled from form data. if ($examid == 0) { $exam = new stdClass(); $exam->course = $COURSE->id; $exam->courseshortname = $COURSE->shortname; $exam->name = $mform->get_data()->name; $exam->examdate = $mform->get_data()->examdate; $exam->emarking = $id; $exam->headerqr = isset($mform->get_data()->headerqr) ? 1 : 0; $exam->printrandom = isset($mform->get_data()->printrandom) ? 1 : 0; $exam->printlist = isset($mform->get_data()->printlist) ? 1 : 0; $exam->extrasheets = $mform->get_data()->extrasheets; $exam->extraexams = $mform->get_data()->extraexams; $exam->usebackside = isset($mform->get_data()->printdoublesided) ? 1 : 0; if ($examid == 0) { $exam->timecreated = time(); } $exam->timemodified = time(); $exam->requestedby = $USER->id; $exam->totalstudents = $studentsnumber; $exam->comment = $mform->get_data()->comment; // Get the enrolments as a comma separated values. $enrollist = array(); if (!empty($mform->get_data()->enrolments)) { $enrolments = $mform->get_data()->enrolments; foreach ($enrolments as $key => $enrolment) { if (!empty($enrolment)) { $enrollist[] = $key; } } } $exam->enrolments = implode(",", $enrollist); $exam->printdate = 0; $exam->status = EMARKING_EXAM_UPLOADED; // Calculate total pages for exam. $exam->totalpages = $numpages; $exam->printingcost = emarking_get_category_cost($COURSE->id); $exam->id = $DB->insert_record('emarking_exams', $exam); $fs = get_file_storage(); foreach ($examfiles as $exampdf) { // Save the submitted file to check if it's a PDF. $filerecord = array('component' => 'mod_emarking', 'filearea' => 'exams', 'contextid' => $context->id, 'itemid' => $exam->id, 'filepath' => '/', 'filename' => $exampdf['filename']); $file = $fs->create_file_from_pathname($filerecord, $exampdf['pathname']); } // Update exam object to store the PDF's file id. $exam->file = $file->get_id(); if (!$DB->update_record('emarking_exams', $exam)) { $fs->delete_area_files($contextid, 'emarking', 'exams', $exam->id); print_error(get_string('errorsavingpdf', 'mod_emarking')); } // If it is a multi-course submission, insert several exams. if (!empty($mform->get_data()->multicourse)) { $multicourse = $mform->get_data()->multicourse; foreach ($multicourse as $key => $mcourse) { if (!empty($key)) { if ($thiscourse = $DB->get_record('course', array('shortname' => $key))) { $studentsnumber = emarking_get_students_count_for_printing($thiscourse->id); list($newemarkingid, $newcmid, $sectionid) = emarking_copy_to_cm($data, $thiscourse->id); $newexam = $exam; $newexam->id = null; $newexam->totalstudents = $studentsnumber; $newexam->course = $thiscourse->id; $newexam->courseshortname = $thiscourse->shortname; $newexam->emarking = $newemarkingid; $newexam->id = $DB->insert_record('emarking_exams', $newexam); $thiscontext = context_course::instance($thiscourse->id); // Create file records for all new exams. foreach ($examfiles as $exampdf) { // Save the submitted file to check if it's a PDF. $filerecord = array('component' => 'mod_emarking', 'filearea' => 'exams', 'contextid' => $thiscontext->id, 'itemid' => $newexam->id, 'filepath' => '/', 'filename' => $exampdf['filename']); $file = $fs->create_file_from_pathname($filerecord, $exampdf['pathname']); } } } } } } else { $exam = $DB->get_record("emarking_exams", array("id" => $examid)); $exam->emarking = $id; $exam->timemodified = time(); $DB->update_record('emarking_exams', $exam); } $headerqr = isset($mform->get_data()->headerqr) ? 1 : 0; setcookie("emarking_headerqr", $headerqr, time() + 3600 * 24 * 365 * 10, '/'); $defaultexam = new stdClass(); $defaultexam->headerqr = $exam->headerqr; $defaultexam->printrandom = $exam->printrandom; $defaultexam->printlist = $exam->printlist; $defaultexam->extrasheets = $exam->extrasheets; $defaultexam->extraexams = $exam->extraexams; $defaultexam->usebackside = $exam->usebackside; $defaultexam->enrolments = $exam->enrolments; setcookie("emarking_exam_defaults", json_encode($defaultexam), time() + 3600 * 24 * 365 * 10, '/'); return $id; }
print_error('Módulo inválido'); } // Validate module if (!($emarking = $DB->get_record('emarking', array('id' => $cm->instance)))) { print_error('Prueba inválida'); } // Validate course if (!($course = $DB->get_record('course', array('id' => $emarking->course)))) { print_error('Curso inválido'); } if (!($exam = $DB->get_record('emarking_exams', array('emarking' => $emarking->id)))) { print_error('e-marking sin examen'); } // URLs for current page $url = new moodle_url('/mod/emarking/reports/justice.php', array('id' => $cm->id)); $totalstudents = emarking_get_students_count_for_printing($course->id, $exam); // Course context is used in reports $context = context_module::instance($cm->id); // Validate the user has grading capabilities if (!has_capability('mod/assign:grade', $context)) { print_error('No tiene permisos para ver reportes de justicia'); } // First check that the user is logged in require_login($course->id); if (isguestuser()) { die; } // Page settings (URL, breadcrumbs and title) $PAGE->set_context($context); $PAGE->set_course($course); $PAGE->set_cm($cm);
} // Update exam object to store the PDF's file id $exam->file = $file->get_id(); if (!$DB->update_record('emarking_exams', $exam)) { $fs->delete_area_files($contextid, 'emarking', 'exams', $exam->id); print_error(get_string('errorsavingpdf', 'mod_emarking')); } // Send new print order notification emarking_send_newprintorder_notification($exam, $course); // If it is a multi-course submission, insert several exams if (!empty($mform->get_data()->multisecciones)) { $multisecciones = $mform->get_data()->multisecciones; foreach ($multisecciones as $key => $multiseccion) { if (!empty($multiseccion)) { if ($thiscourse = $DB->get_record('course', array('shortname' => $key))) { $studentsnumber = emarking_get_students_count_for_printing($thiscourse->id); $newexam = $exam; $newexam->id = null; $newexam->totalstudents = $studentsnumber; $newexam->course = $thiscourse->id; $newexam->id = $DB->insert_record('emarking_exams', $newexam); // Send new print order notification emarking_send_newprintorder_notification($newexam, $thiscourse); } } } } // Done, now redirect to index if ($cmid > 0) { $previewurl = new moodle_url('/mod/emarking/exams.php', array('id' => $cm->id, 'examid' => $exam->id)); } else {
/** * Saves a new instance of the emarking into the database * Given an object containing all the necessary data, * (defined by the form in mod_form.php) this function * will create a new instance and return the id number * of the new instance. * * @param object $emarking * An object from the form in mod_form.php * @param mod_emarking_mod_form $mform * @return int The id of the newly inserted emarking record */ function emarking_add_instance(stdClass $data, $itemid, mod_emarking_mod_form $mform = null) { global $DB, $CFG, $COURSE, $USER; $data->timecreated = time(); $id = $DB->insert_record('emarking', $data); $data->id = $id; $course = $data->course; emarking_grade_item_update($data); foreach ($data as $k => $v) { $parts = explode('-', $k); if (count($parts) > 1 && $parts[0] === 'marker') { $markerid = intval($parts[1]); $marker = new stdClass(); $marker->emarking = $id; $marker->marker = $markerid; $marker->qualitycontrol = 1; $DB->insert_record('emarking_markers', $marker); } } // entregar id del curso $context = context_course::instance($course); $examid = 0; // If there's no previous exam to associate, and we are creating a new // EMarking, we need the PDF file. if ($data->exam == 0) { $examfiles = emarking_validate_exam_files_from_draft($itemid); if (count($examfiles) == 0) { throw new Exception('Invalid PDF exam files'); } $numpages = $examfiles[0]['numpages']; } else { $examid = $data->exam; } $studentsnumber = emarking_get_students_count_for_printing($course); // A new exam object is created and its attributes filled from form data. if ($examid == 0) { $exam = new stdClass(); $exam->course = $course; $exam->courseshortname = $COURSE->shortname; $exam->name = "a"; $exam->examdate = time(); $exam->emarking = $id; $exam->headerqr = 1; $exam->printrandom = 0; $exam->printlist = 0; $exam->extrasheets = 0; $exam->extraexams = 0; $exam->usebackside = 0; if ($examid == 0) { $exam->timecreated = time(); } $exam->timemodified = time(); $exam->requestedby = $USER->id; $exam->totalstudents = $studentsnumber; $exam->comment = "comment"; // Get the enrolments as a comma separated values. $exam->enrolments = "manual"; $exam->printdate = 0; $exam->status = 10; // Calculate total pages for exam. $exam->totalpages = 2; $exam->printingcost = emarking_get_category_cost($course); $exam->id = $DB->insert_record('emarking_exams', $exam); $fs = get_file_storage(); foreach ($examfiles as $exampdf) { // Save the submitted file to check if it's a PDF. $filerecord = array('component' => 'mod_emarking', 'filearea' => 'exams', 'contextid' => $context->id, 'itemid' => $exam->id, 'filepath' => '/', 'filename' => $exampdf['filename']); $file = $fs->create_file_from_pathname($filerecord, $exampdf['pathname']); } // Update exam object to store the PDF's file id. $exam->file = $file->get_id(); if (!$DB->update_record('emarking_exams', $exam)) { $fs->delete_area_files($contextid, 'emarking', 'exams', $exam->id); print_error(get_string('errorsavingpdf', 'mod_emarking')); } } else { $exam = $DB->get_record("emarking_exams", array("id" => $examid)); $exam->emarking = $id; $exam->timemodified = time(); $DB->update_record('emarking_exams', $exam); } $headerqr = 1; setcookie("emarking_headerqr", $headerqr, time() + 3600 * 24 * 365 * 10, '/'); $defaultexam = new stdClass(); $defaultexam->headerqr = $exam->headerqr; $defaultexam->printrandom = $exam->printrandom; $defaultexam->printlist = $exam->printlist; $defaultexam->extrasheets = $exam->extrasheets; $defaultexam->extraexams = $exam->extraexams; $defaultexam->usebackside = $exam->usebackside; $defaultexam->enrolments = $exam->enrolments; setcookie("emarking_exam_defaults", json_encode($defaultexam), time() + 3600 * 24 * 365 * 10, '/'); return $id; }