Exemple #1
0
/**
 * 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;
}
Exemple #2
0
/**
 * 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;
}