예제 #1
0
 /**
  * store
  *
  * @param xxx $hotpot
  */
 public static function store($hotpot)
 {
     global $CFG, $DB, $USER;
     if (empty($hotpot->attempt)) {
         return;
         // no attempt record - shouldn't happen !!
     }
     if ($hotpot->attempt->userid != $USER->id) {
         return;
         // wrong userid - shouldn't happen !!
     }
     // update quiz attempt fields using incoming data
     $hotpot->attempt->score = max(0, optional_param(self::scorefield, 0, PARAM_INT));
     $hotpot->attempt->status = max(0, optional_param('status', 0, PARAM_INT));
     $hotpot->attempt->redirect = max(0, optional_param('redirect', 0, PARAM_INT));
     $hotpot->attempt->details = optional_param(self::detailsfield, '', PARAM_RAW);
     // update timemodified for this attempt
     $hotpot->attempt->timemodified = $hotpot->time;
     // time values, e.g. "2008-09-12 16:18:18 +0900",
     // need to be converted to numeric date stamps
     $timefields = array('starttime', 'endtime');
     foreach ($timefields as $timefield) {
         $hotpot->attempt->{$timefield} = 0;
         // default
         if ($time = optional_param($timefield, '', PARAM_RAW)) {
             // make sure the timezone has a "+" sign
             // Note: sometimes it gets stripped (by optional_param?)
             $time = preg_replace('/(?<= )\\d{4}$/', '+$0', trim($time));
             // convert $time to numeric date stamp
             // PHP4 gives -1 on error, whereas PHP5 give false
             $time = strtotime($time);
             if ($time && $time > 0) {
                 $hotpot->attempt->{$timefield} = $time;
             }
         }
     }
     unset($timefields, $timefield, $time);
     // set finish times
     $hotpot->attempt->timefinish = $hotpot->time;
     // increment quiz attempt duration
     $startfield = self::durationstartfield;
     // "starttime" or "timestart"
     $finishfield = self::durationfinishfield;
     // "endtime" or "timefinish"
     $duration = $hotpot->attempt->{$finishfield} - $hotpot->attempt->{$startfield};
     if (empty($hotpot->attempt->duration)) {
         $hotpot->attempt->duration = $duration;
     } else {
         if ($duration > 0) {
             $hotpot->attempt->duration += $duration;
         }
     }
     unset($duration, $startfield, $finishfield);
     // set clickreportid, (for click reporting)
     $hotpot->attempt->clickreportid = $hotpot->attempt->id;
     // check if there are any previous results stored for this attempt
     // this could happen if ...
     //     - the quiz has been resumed
     //     - clickreporting is enabled for this quiz
     if ($DB->get_field('hotpot_attempts', 'timefinish', array('id' => $hotpot->attempt->id))) {
         if ($hotpot->clickreporting) {
             // self::can_clickreporting()
             // add quiz attempt record for each form submission
             // records are linked via the "clickreportid" field
             // update timemodified and status in previous records in this clickreportid group
             $DB->set_field('hotpot_attempts', 'timemodified', $hotpot->time, array('clickreportid' => $hotpot->attempt->clickreportid));
             $DB->set_field('hotpot_attempts', 'status', $hotpot->attempt->status, array('clickreportid' => $hotpot->attempt->clickreportid));
             // add new attempt record
             unset($hotpot->attempt->id);
             if (!($hotpot->attempt->id = $DB->insert_record('hotpot_attempts', $hotpot->attempt))) {
                 print_error('error_insertrecord', 'hotpot', '', 'hotpot_attempts');
             }
         } else {
             // remove previous responses for this attempt, if required
             // (N.B. this does NOT remove the attempt record, just the responses)
             $DB->delete_records('hotpot_responses', array('attemptid' => $hotpot->attempt->id));
         }
     }
     // add details of this quiz attempt, if required
     // "hotpot_storedetails" is set by administrator
     // Site Admin -> Modules -> Activities -> HotPot
     if ($CFG->hotpot_storedetails) {
         // delete/update/add the details record
         if ($DB->record_exists('hotpot_details', array('attemptid' => $hotpot->attempt->id))) {
             $DB->set_field('hotpot_details', 'details', $hotpot->attempt->details, array('attemptid' => $hotpot->attempt->id));
         } else {
             $details = (object) array('attemptid' => $hotpot->attempt->id, 'details' => $hotpot->attempt->details);
             if (!$DB->insert_record('hotpot_details', $details, false)) {
                 print_error('error_insertrecord', 'hotpot', '', 'hotpot_details');
             }
             unset($details);
         }
     }
     // add details of this attempt
     self::store_details($hotpot->attempt);
     // update the attempt record
     if (!$DB->update_record('hotpot_attempts', $hotpot->attempt)) {
         print_error('error_updaterecord', 'hotpot', '', 'hotpot_attempts');
     }
     // regrade the quiz to take account of the latest quiz attempt score
     hotpot_update_grades($hotpot->to_stdclass(), $hotpot->attempt->userid);
 }