/**
 * Delete an offlinequiz result, including the questions_usage_by_activity corresponding to it.
 *
 * @param mixed $attempt an integer attempt id or an attempt object
 *      (row of the offlinequiz_results table).
 * @param object $offlinequiz the offlinequiz object.
 */
function offlinequiz_delete_result($resultid, $context)
{
    global $DB;
    if ($result = $DB->get_record('offlinequiz_results', array('id' => $resultid))) {
        // First delete the result itself.
        $DB->delete_records('offlinequiz_results', array('id' => $result->id));
        // Now we delete all scanned pages that refer to the result.
        $scannedpages = $DB->get_records_sql("\n                SELECT *\n                  FROM {offlinequiz_scanned_pages}\n                 WHERE resultid = :resultid", array('resultid' => $result->id));
        foreach ($scannedpages as $page) {
            offlinequiz_delete_scanned_page($page, $context);
        }
        // Finally, delete the question usage that belongs to the result.
        if ($result->usageid) {
            question_engine::delete_questions_usage_by_activity($result->usageid);
        }
    }
}
Example #2
0
/**
 * Given an ID of an instance of this module,
 * this function will permanently delete the instance
 * and any data that depends on it.
 *
 * @param int $id Id of the module instance
 * @return boolean Success/Failure
 */
function offlinequiz_delete_instance($id)
{
    global $DB, $CFG;
    require_once $CFG->dirroot . '/mod/offlinequiz/locallib.php';
    require_once $CFG->dirroot . '/calendar/lib.php';
    if (!($offlinequiz = $DB->get_record('offlinequiz', array('id' => $id)))) {
        return false;
    }
    if (!($cm = get_coursemodule_from_instance("offlinequiz", $offlinequiz->id, $offlinequiz->course))) {
        return false;
    }
    $context = context_module::instance($cm->id);
    // Delete any dependent records here.
    if ($results = $DB->get_records("offlinequiz_results", array('offlinequizid' => $offlinequiz->id))) {
        foreach ($results as $result) {
            offlinequiz_delete_result($result->id, $context);
        }
    }
    if ($scannedpages = $DB->get_records('offlinequiz_scanned_pages', array('offlinequizid' => $offlinequiz->id))) {
        foreach ($scannedpages as $page) {
            offlinequiz_delete_scanned_page($page, $context);
        }
    }
    if ($scannedppages = $DB->get_records('offlinequiz_scanned_p_pages', array('offlinequizid' => $offlinequiz->id))) {
        foreach ($scannedppages as $page) {
            offlinequiz_delete_scanned_p_page($page, $context);
        }
    }
    if ($events = $DB->get_records('event', array('modulename' => 'offlinequiz', 'instance' => $offlinequiz->id))) {
        foreach ($events as $event) {
            $event = calendar_event::load($event);
            $event->delete();
        }
    }
    if ($plists = $DB->get_records('offlinequiz_p_lists', array('offlinequizid' => $offlinequiz->id))) {
        foreach ($plists as $plist) {
            $DB->delete_records('offlinequiz_participants', array('listid' => $plist->id));
            $DB->delete_records('offlinequiz_p_lists', array('id' => $plist->id));
        }
    }
    // Remove the grade item.
    offlinequiz_grade_item_delete($offlinequiz);
    // Delete template question usages of offlinequiz groups.
    offlinequiz_delete_template_usages($offlinequiz);
    // All the tables with no dependencies...
    $tablestopurge = array('offlinequiz_groups' => 'offlinequizid', 'offlinequiz' => 'id');
    foreach ($tablestopurge as $table => $keyfield) {
        if (!$DB->delete_records($table, array($keyfield => $offlinequiz->id))) {
            $result = false;
        }
    }
    return true;
}
 /**
  * (non-PHPdoc)
  * @see offlinequiz_default_report::display()
  */
 public function display($offlinequiz, $cm, $course)
 {
     global $CFG, $COURSE, $DB, $OUTPUT, $USER;
     $this->context = context_module::instance($cm->id);
     $pageoptions = array();
     $pageoptions['id'] = $cm->id;
     $pageoptions['mode'] = 'rimport';
     $reporturl = new moodle_url('/mod/offlinequiz/report.php', $pageoptions);
     $action = optional_param('action', '', PARAM_ACTION);
     if ($action != 'delete') {
         $this->print_header_and_tabs($cm, $course, $offlinequiz, 'rimport');
         if (!$offlinequiz->docscreated) {
             echo $OUTPUT->heading(format_string($offlinequiz->name));
             echo $OUTPUT->heading(get_string('nopdfscreated', 'offlinequiz'));
             return true;
         }
         echo $OUTPUT->box_start('linkbox');
         echo $OUTPUT->heading(format_string($offlinequiz->name));
         echo $OUTPUT->heading_with_help(get_string('resultimport', 'offlinequiz'), 'importnew', 'offlinequiz');
         echo $OUTPUT->box_end();
     }
     $importform = new offlinequiz_upload_form($reporturl, array('offlinequiz' => $offlinequiz, 'context' => $this->context));
     // Has the user submitted a file?
     if ($fromform = $importform->get_data() && confirm_sesskey()) {
         // File checks out ok.
         $fileisgood = false;
         // Work out if this is an uploaded file
         // or one from the filesarea.
         $realfilename = $importform->get_new_filename('newfile');
         // Create a unique temp dir.
         srand(microtime() * 1000000);
         $unique = str_replace('.', '', microtime(true) . rand(0, 100000));
         $dirname = "{$CFG->tempdir}/offlinequiz/import/{$unique}";
         check_dir_exists($dirname, true, true);
         $importfile = $dirname . '/' . $realfilename;
         if (!($result = $importform->save_file('newfile', $importfile, true))) {
             throw new moodle_exception('uploadproblem');
         }
         $files = array();
         $mimetype = mimeinfo('type', $importfile);
         if ($mimetype == 'application/zip') {
             if (unzip_file($importfile)) {
                 unlink($importfile);
                 $files = get_directory_list($dirname);
             } else {
                 echo $OUTPUT->notification(get_string('couldnotunzip', 'offlinequiz_rimport', $realfilename), 'notifyproblem');
             }
         } else {
             if (preg_match('/^image/', $mimetype)) {
                 $files[] = $realfilename;
             }
         }
         $added = count($files);
         // Create a new queue job.
         $job = new stdClass();
         $job->offlinequizid = $offlinequiz->id;
         $job->importuserid = $USER->id;
         $job->timecreated = time();
         $job->timestart = 0;
         $job->timefinish = 0;
         $job->status = 'new';
         if (!($job->id = $DB->insert_record('offlinequiz_queue', $job))) {
             echo $OUTPUT->notification(get_string('couldnotcreatejob', 'offlinequiz_rimport'), 'notifyproblem');
         }
         // Add the files to the job.
         foreach ($files as $file) {
             $jobfile = new stdClass();
             $jobfile->queueid = $job->id;
             $jobfile->filename = $dirname . '/' . $file;
             $jobfile->status = 'new';
             if (!($jobfile->id = $DB->insert_record('offlinequiz_queue_data', $jobfile))) {
                 echo $OUTPUT->notification(get_string('couldnotcreatejobfile', 'offlinequiz_rimport'), 'notifyproblem');
                 $added--;
             }
         }
         // Notify the user.
         echo $OUTPUT->notification(get_string('addingfilestoqueue', 'offlinequiz_rimport', $added), 'notifysuccess');
         echo $OUTPUT->continue_button($CFG->wwwroot . '/mod/offlinequiz/report.php?q=' . $offlinequiz->id . '&mode=rimport');
     } else {
         // Print info about offlinequiz_queue jobs.
         $sql = 'SELECT COUNT(*) as count
                   FROM {offlinequiz_queue} q
                   JOIN {offlinequiz_queue_data} qd on q.id = qd.queueid
                  WHERE (qd.status = :status1 OR qd.status = :status3)
                    AND q.offlinequizid = :offlinequizid
                    AND q.status = :status2
                 ';
         $newforms = $DB->get_record_sql($sql, array('offlinequizid' => $offlinequiz->id, 'status1' => 'new', 'status2' => 'new', 'status3' => ''));
         $processingforms = $DB->get_record_sql($sql, array('offlinequizid' => $offlinequiz->id, 'status1' => 'processing', 'status2' => 'processing', 'status3' => 'new'));
         if ($newforms->count > 0) {
             echo $OUTPUT->notification(get_string('newformsinqueue', 'offlinequiz_rimport', $newforms->count), 'notifysuccess');
         }
         if ($processingforms->count > 0) {
             echo $OUTPUT->notification(get_string('processingformsinqueue', 'offlinequiz_rimport', $processingforms->count), 'notifysuccess');
         }
         $action = optional_param('action', '', PARAM_ACTION);
         switch ($action) {
             case 'delete':
                 if (confirm_sesskey()) {
                     $selectedpageids = array();
                     $params = (array) data_submitted();
                     foreach ($params as $key => $value) {
                         if (preg_match('!^p([0-9]+)$!', $key, $matches)) {
                             $selectedpageids[] = $matches[1];
                         }
                     }
                     foreach ($selectedpageids as $pageid) {
                         if ($pageid && ($page = $DB->get_record('offlinequiz_scanned_pages', array('id' => $pageid)))) {
                             offlinequiz_delete_scanned_page($page, $this->context);
                         }
                     }
                     redirect($CFG->wwwroot . '/mod/offlinequiz/report.php?q=' . $offlinequiz->id . '&mode=rimport');
                 } else {
                     print_error('invalidsesskey');
                 }
                 break;
             default:
                 // Print the table with answer forms that need correction.
                 $this->print_error_report($offlinequiz);
                 // Display the upload form.
                 $importform->display();
         }
     }
 }