|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 00066 class question_attempt_step { 00071 private $id = null; 00072 00077 private $state; 00078 00080 private $fraction = null; 00081 00083 private $timecreated; 00084 00086 private $userid; 00087 00089 private $data; 00090 00092 private $files = array(); 00093 00104 public function __construct($data = array(), $timecreated = null, $userid = null, 00105 $existingstepid = null) { 00106 global $USER; 00107 00108 if (!is_array($data)) { 00109 echo format_backtrace(debug_backtrace()); 00110 } 00111 $this->state = question_state::$unprocessed; 00112 $this->data = $data; 00113 if (is_null($timecreated)) { 00114 $this->timecreated = time(); 00115 } else { 00116 $this->timecreated = $timecreated; 00117 } 00118 if (is_null($userid)) { 00119 $this->userid = $USER->id; 00120 } else { 00121 $this->userid = $userid; 00122 } 00123 00124 if (!is_null($existingstepid)) { 00125 $this->id = $existingstepid; 00126 } 00127 } 00128 00133 public function get_id() { 00134 return $this->id; 00135 } 00136 00138 public function get_state() { 00139 return $this->state; 00140 } 00141 00146 public function set_state($state) { 00147 $this->state = $state; 00148 } 00149 00154 public function get_fraction() { 00155 return $this->fraction; 00156 } 00157 00162 public function set_fraction($fraction) { 00163 $this->fraction = $fraction; 00164 } 00165 00167 public function get_user_id() { 00168 return $this->userid; 00169 } 00170 00172 public function get_timecreated() { 00173 return $this->timecreated; 00174 } 00175 00180 public function has_qt_var($name) { 00181 return array_key_exists($name, $this->data); 00182 } 00183 00188 public function get_qt_var($name) { 00189 if (!$this->has_qt_var($name)) { 00190 return null; 00191 } 00192 return $this->data[$name]; 00193 } 00194 00200 public function set_qt_var($name, $value) { 00201 if ($name[0] != '_') { 00202 throw new coding_exception('Cannot set question type data ' . $name . 00203 ' on an attempt step. You can only set variables with names begining with _.'); 00204 } 00205 $this->data[$name] = $value; 00206 } 00207 00215 public function get_qt_files($name, $contextid) { 00216 if (array_key_exists($name, $this->files)) { 00217 return $this->files[$name]; 00218 } 00219 00220 if (!$this->has_qt_var($name)) { 00221 $this->files[$name] = array(); 00222 return array(); 00223 } 00224 00225 $fs = get_file_storage(); 00226 $this->files[$name] = $fs->get_area_files($contextid, 'question', 00227 'response_' . $name, $this->id, 'sortorder', false); 00228 00229 return $this->files[$name]; 00230 } 00231 00240 public function prepare_response_files_draft_itemid($name, $contextid) { 00241 list($draftid, $notused) = $this->prepare_response_files_draft_itemid_with_text( 00242 $name, $contextid, null); 00243 return $draftid; 00244 } 00245 00255 public function prepare_response_files_draft_itemid_with_text($name, $contextid, $text) { 00256 $draftid = 0; // Will be filled in by file_prepare_draft_area. 00257 $newtext = file_prepare_draft_area($draftid, $contextid, 'question', 00258 'response_' . $name, $this->id, null, $text); 00259 return array($draftid, $newtext); 00260 } 00261 00274 public function rewrite_response_pluginfile_urls($text, $contextid, $name, $extras) { 00275 return question_rewrite_question_urls($text, 'pluginfile.php', $contextid, 00276 'question', 'response_' . $name, $extras, $this->id); 00277 } 00278 00283 public function get_qt_data() { 00284 $result = array(); 00285 foreach ($this->data as $name => $value) { 00286 if ($name[0] != '-' && $name[0] != ':') { 00287 $result[$name] = $value; 00288 } 00289 } 00290 return $result; 00291 } 00292 00297 public function has_behaviour_var($name) { 00298 return array_key_exists('-' . $name, $this->data); 00299 } 00300 00305 public function get_behaviour_var($name) { 00306 if (!$this->has_behaviour_var($name)) { 00307 return null; 00308 } 00309 return $this->data['-' . $name]; 00310 } 00311 00317 public function set_behaviour_var($name, $value) { 00318 if ($name[0] != '_') { 00319 throw new coding_exception('Cannot set question type data ' . $name . 00320 ' on an attempt step. You can only set variables with names begining with _.'); 00321 } 00322 return $this->data['-' . $name] = $value; 00323 } 00324 00329 public function get_behaviour_data() { 00330 $result = array(); 00331 foreach ($this->data as $name => $value) { 00332 if ($name[0] == '-') { 00333 $result[substr($name, 1)] = $value; 00334 } 00335 } 00336 return $result; 00337 } 00338 00346 public function get_submitted_data() { 00347 $result = array(); 00348 foreach ($this->data as $name => $value) { 00349 if ($name[0] == '_' || ($name[0] == '-' && $name[1] == '_')) { 00350 continue; 00351 } 00352 $result[$name] = $value; 00353 } 00354 return $result; 00355 } 00356 00365 public function get_all_data() { 00366 return $this->data; 00367 } 00368 00375 public static function load_from_records($records, $attemptstepid) { 00376 $currentrec = $records->current(); 00377 while ($currentrec->attemptstepid != $attemptstepid) { 00378 $records->next(); 00379 if (!$records->valid()) { 00380 throw new coding_exception('Question attempt step ' . $attemptstepid . 00381 ' not found in the database.'); 00382 } 00383 $currentrec = $records->current(); 00384 } 00385 00386 $record = $currentrec; 00387 $data = array(); 00388 while ($currentrec && $currentrec->attemptstepid == $attemptstepid) { 00389 if ($currentrec->name) { 00390 $data[$currentrec->name] = $currentrec->value; 00391 } 00392 $records->next(); 00393 if ($records->valid()) { 00394 $currentrec = $records->current(); 00395 } else { 00396 $currentrec = false; 00397 } 00398 } 00399 00400 $step = new question_attempt_step_read_only($data, $record->timecreated, $record->userid); 00401 $step->state = question_state::get($record->state); 00402 $step->id = $record->attemptstepid; 00403 if (!is_null($record->fraction)) { 00404 $step->fraction = $record->fraction + 0; 00405 } 00406 return $step; 00407 } 00408 } 00409 00410 00417 class question_attempt_pending_step extends question_attempt_step { 00419 protected $newresponsesummary = null; 00420 00427 public function set_new_response_summary($responsesummary) { 00428 $this->newresponsesummary = $responsesummary; 00429 } 00430 00432 public function get_new_response_summary() { 00433 return $this->newresponsesummary; 00434 } 00435 00437 public function response_summary_changed() { 00438 return !is_null($this->newresponsesummary); 00439 } 00440 } 00441 00442 00449 class question_attempt_step_read_only extends question_attempt_step { 00450 public function set_state($state) { 00451 throw new coding_exception('Cannot modify a question_attempt_step_read_only.'); 00452 } 00453 public function set_fraction($fraction) { 00454 throw new coding_exception('Cannot modify a question_attempt_step_read_only.'); 00455 } 00456 public function set_qt_var($name, $value) { 00457 throw new coding_exception('Cannot modify a question_attempt_step_read_only.'); 00458 } 00459 public function set_behaviour_var($name, $value) { 00460 throw new coding_exception('Cannot modify a question_attempt_step_read_only.'); 00461 } 00462 } 00463 00464 00473 class question_null_step { 00474 public function get_state() { 00475 return question_state::$notstarted; 00476 } 00477 00478 public function set_state($state) { 00479 throw new coding_exception('This question has not been started.'); 00480 } 00481 00482 public function get_fraction() { 00483 return null; 00484 } 00485 } 00486 00487 00496 class question_attempt_step_subquestion_adapter extends question_attempt_step { 00498 protected $realstep; 00500 protected $extraprefix; 00501 00508 public function __construct($realqas, $extraprefix) { 00509 $this->realqas = $realqas; 00510 $this->extraprefix = $extraprefix; 00511 } 00512 00518 public function add_prefix($field) { 00519 if (substr($field, 0, 2) === '!_') { 00520 return '-_' . $this->extraprefix . substr($field, 2); 00521 } else if (substr($field, 0, 1) === '-') { 00522 return '-' . $this->extraprefix . substr($field, 1); 00523 } else if (substr($field, 0, 1) === '_') { 00524 return '_' . $this->extraprefix . substr($field, 1); 00525 } else { 00526 return $this->extraprefix . $field; 00527 } 00528 } 00529 00536 public function remove_prefix($field) { 00537 if (preg_match('~^(-?_?)' . preg_quote($this->extraprefix) . '(.*)$~', $field, $matches)) { 00538 return $matches[1] . $matches[2]; 00539 } else { 00540 return null; 00541 } 00542 } 00543 00550 public function filter_array($data) { 00551 $result = array(); 00552 foreach ($data as $fullname => $value) { 00553 if ($name = $this->remove_prefix($fullname)) { 00554 $result[$name] = $value; 00555 } 00556 } 00557 return $result; 00558 } 00559 00560 public function get_state() { 00561 return $this->realqas->get_state(); 00562 } 00563 00564 public function set_state($state) { 00565 throw new coding_exception('Cannot modify a question_attempt_step_subquestion_adapter.'); 00566 } 00567 00568 public function get_fraction() { 00569 return $this->realqas->get_fraction(); 00570 } 00571 00572 public function set_fraction($fraction) { 00573 throw new coding_exception('Cannot modify a question_attempt_step_subquestion_adapter.'); 00574 } 00575 00576 public function get_user_id() { 00577 return $this->realqas->get_user_id; 00578 } 00579 00580 public function get_timecreated() { 00581 return $this->realqas->get_timecreated(); 00582 } 00583 00584 public function has_qt_var($name) { 00585 return $this->realqas->has_qt_var($this->add_prefix($name)); 00586 } 00587 00588 public function get_qt_var($name) { 00589 return $this->realqas->get_qt_var($this->add_prefix($name)); 00590 } 00591 00592 public function set_qt_var($name, $value) { 00593 return $this->realqas->set_qt_var($this->add_prefix($name), $value); 00594 } 00595 00596 public function get_qt_data() { 00597 return $this->filter_array($this->realqas->get_qt_data()); 00598 } 00599 00600 public function has_behaviour_var($name) { 00601 return $this->realqas->has_im_var($this->add_prefix($name)); 00602 } 00603 00604 public function get_behaviour_var($name) { 00605 return $this->realqas->get_im_var($this->add_prefix($name)); 00606 } 00607 00608 public function set_behaviour_var($name, $value) { 00609 return $this->realqas->set_im_var($this->add_prefix($name), $value); 00610 } 00611 00612 public function get_behaviour_data() { 00613 return $this->filter_array($this->realqas->get_behaviour_data()); 00614 } 00615 00616 public function get_submitted_data() { 00617 return $this->filter_array($this->realqas->get_submitted_data()); 00618 } 00619 00620 public function get_all_data() { 00621 return $this->filter_array($this->realqas->get_all_data()); 00622 } 00623 00624 public function get_qt_files($name, $contextid) { 00625 throw new coding_exception('No attempt has yet been made to implement files support in ' . 00626 'question_attempt_step_subquestion_adapter.'); 00627 } 00628 00629 public function prepare_response_files_draft_itemid($name, $contextid) { 00630 throw new coding_exception('No attempt has yet been made to implement files support in ' . 00631 'question_attempt_step_subquestion_adapter.'); 00632 } 00633 00634 public function prepare_response_files_draft_itemid_with_text($name, $contextid, $text) { 00635 throw new coding_exception('No attempt has yet been made to implement files support in ' . 00636 'question_attempt_step_subquestion_adapter.'); 00637 } 00638 00639 public function rewrite_response_pluginfile_urls($text, $contextid, $name, $extras) { 00640 throw new coding_exception('No attempt has yet been made to implement files support in ' . 00641 'question_attempt_step_subquestion_adapter.'); 00642 } 00643 }