|
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 require_once($CFG->libdir . '/formslib.php'); 00030 00031 00038 class preview_options_form extends moodleform { 00039 public function definition() { 00040 $mform = $this->_form; 00041 00042 $hiddenofvisible = array( 00043 question_display_options::HIDDEN => get_string('notshown', 'question'), 00044 question_display_options::VISIBLE => get_string('shown', 'question'), 00045 ); 00046 00047 $mform->addElement('header', 'optionsheader', get_string('changeoptions', 'question')); 00048 00049 $behaviours = question_engine::get_behaviour_options( 00050 $this->_customdata['quba']->get_preferred_behaviour()); 00051 $mform->addElement('select', 'behaviour', 00052 get_string('howquestionsbehave', 'question'), $behaviours); 00053 $mform->addHelpButton('behaviour', 'howquestionsbehave', 'question'); 00054 00055 $mform->addElement('text', 'maxmark', get_string('markedoutof', 'question'), 00056 array('size' => '5')); 00057 $mform->setType('maxmark', PARAM_FLOAT); 00058 00059 if ($this->_customdata['maxvariant'] > 1) { 00060 $variants = range(1, $this->_customdata['maxvariant']); 00061 $mform->addElement('select', 'variant', get_string('questionvariant', 'question'), 00062 array_combine($variants, $variants)); 00063 } 00064 $mform->setType('variant', PARAM_INT); 00065 00066 $mform->addElement('select', 'correctness', get_string('whethercorrect', 'question'), 00067 $hiddenofvisible); 00068 00069 $marksoptions = array( 00070 question_display_options::HIDDEN => get_string('notshown', 'question'), 00071 question_display_options::MAX_ONLY => get_string('showmaxmarkonly', 'question'), 00072 question_display_options::MARK_AND_MAX => get_string('showmarkandmax', 'question'), 00073 ); 00074 $mform->addElement('select', 'marks', get_string('marks', 'question'), $marksoptions); 00075 00076 $mform->addElement('select', 'markdp', get_string('decimalplacesingrades', 'question'), 00077 question_engine::get_dp_options()); 00078 00079 $mform->addElement('select', 'feedback', 00080 get_string('specificfeedback', 'question'), $hiddenofvisible); 00081 00082 $mform->addElement('select', 'generalfeedback', 00083 get_string('generalfeedback', 'question'), $hiddenofvisible); 00084 00085 $mform->addElement('select', 'rightanswer', 00086 get_string('rightanswer', 'question'), $hiddenofvisible); 00087 00088 $mform->addElement('select', 'history', 00089 get_string('responsehistory', 'question'), $hiddenofvisible); 00090 00091 $mform->addElement('submit', 'submit', 00092 get_string('restartwiththeseoptions', 'question')); 00093 } 00094 } 00095 00096 00104 class question_preview_options extends question_display_options { 00106 public $behaviour; 00107 00109 public $maxmark; 00110 00112 public $variant; 00113 00115 const OPTIONPREFIX = 'question_preview_options_'; 00116 00120 public function __construct($question) { 00121 global $CFG; 00122 $this->behaviour = 'deferredfeedback'; 00123 $this->maxmark = $question->defaultmark; 00124 $this->variant = null; 00125 $this->correctness = self::VISIBLE; 00126 $this->marks = self::MARK_AND_MAX; 00127 $this->markdp = get_config('quiz', 'decimalpoints'); 00128 $this->feedback = self::VISIBLE; 00129 $this->numpartscorrect = $this->feedback; 00130 $this->generalfeedback = self::VISIBLE; 00131 $this->rightanswer = self::VISIBLE; 00132 $this->history = self::HIDDEN; 00133 $this->flags = self::HIDDEN; 00134 $this->manualcomment = self::HIDDEN; 00135 } 00136 00140 protected function get_user_pref_fields() { 00141 return array('behaviour', 'correctness', 'marks', 'markdp', 'feedback', 00142 'generalfeedback', 'rightanswer', 'history'); 00143 } 00144 00148 protected function get_field_types() { 00149 return array( 00150 'behaviour' => PARAM_ALPHA, 00151 'maxmark' => PARAM_NUMBER, 00152 'variant' => PARAM_INT, 00153 'correctness' => PARAM_BOOL, 00154 'marks' => PARAM_INT, 00155 'markdp' => PARAM_INT, 00156 'feedback' => PARAM_BOOL, 00157 'generalfeedback' => PARAM_BOOL, 00158 'rightanswer' => PARAM_BOOL, 00159 'history' => PARAM_BOOL, 00160 ); 00161 } 00162 00166 public function load_user_defaults() { 00167 foreach ($this->get_user_pref_fields() as $field) { 00168 $this->$field = get_user_preferences( 00169 self::OPTIONPREFIX . $field, $this->$field); 00170 } 00171 $this->numpartscorrect = $this->feedback; 00172 } 00173 00178 public function save_user_preview_options($newoptions) { 00179 foreach ($this->get_user_pref_fields() as $field) { 00180 if (isset($newoptions->$field)) { 00181 set_user_preference(self::OPTIONPREFIX . $field, $newoptions->$field); 00182 } 00183 } 00184 } 00185 00189 public function set_from_request() { 00190 foreach ($this->get_field_types() as $field => $type) { 00191 $this->$field = optional_param($field, $this->$field, $type); 00192 } 00193 $this->numpartscorrect = $this->feedback; 00194 } 00195 00200 public function get_url_params() { 00201 $params = array(); 00202 foreach ($this->get_field_types() as $field => $notused) { 00203 if ($field == 'behaviour' || $field == 'maxmark' || is_null($this->$field)) { 00204 continue; 00205 } 00206 $params[$field] = $this->$field; 00207 } 00208 return $params; 00209 } 00210 } 00211 00212 00227 function question_preview_question_pluginfile($course, $context, $component, 00228 $filearea, $qubaid, $slot, $args, $forcedownload) { 00229 global $USER, $DB, $CFG; 00230 00231 $quba = question_engine::load_questions_usage_by_activity($qubaid); 00232 00233 if (!question_has_capability_on($quba->get_question($slot), 'use')) { 00234 send_file_not_found(); 00235 } 00236 00237 $options = new question_display_options(); 00238 $options->feedback = question_display_options::VISIBLE; 00239 $options->numpartscorrect = question_display_options::VISIBLE; 00240 $options->generalfeedback = question_display_options::VISIBLE; 00241 $options->rightanswer = question_display_options::VISIBLE; 00242 $options->manualcomment = question_display_options::VISIBLE; 00243 $options->history = question_display_options::VISIBLE; 00244 if (!$quba->check_file_access($slot, $options, $component, 00245 $filearea, $args, $forcedownload)) { 00246 send_file_not_found(); 00247 } 00248 00249 $fs = get_file_storage(); 00250 $relativepath = implode('/', $args); 00251 $fullpath = "/$context->id/$component/$filearea/$relativepath"; 00252 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { 00253 send_file_not_found(); 00254 } 00255 00256 send_stored_file($file, 0, 0, $forcedownload); 00257 } 00258 00265 function question_preview_action_url($questionid, $qubaid, 00266 question_preview_options $options, $context) { 00267 $params = array( 00268 'id' => $questionid, 00269 'previewid' => $qubaid, 00270 ); 00271 if ($context->contextlevel == CONTEXT_MODULE) { 00272 $params['cmid'] = $context->instanceid; 00273 } else if ($context->contextlevel == CONTEXT_COURSE) { 00274 $params['courseid'] = $context->instanceid; 00275 } 00276 $params = array_merge($params, $options->get_url_params()); 00277 return new moodle_url('/question/preview.php', $params); 00278 } 00279 00286 function question_preview_form_url($questionid, $context) { 00287 $params = array( 00288 'id' => $questionid, 00289 ); 00290 if ($context->contextlevel == CONTEXT_MODULE) { 00291 $params['cmid'] = $context->instanceid; 00292 } else if ($context->contextlevel == CONTEXT_COURSE) { 00293 $params['courseid'] = $context->instanceid; 00294 } 00295 return new moodle_url('/question/preview.php', $params); 00296 } 00297 00305 function restart_preview($previewid, $questionid, $displayoptions, $context) { 00306 global $DB; 00307 00308 if ($previewid) { 00309 $transaction = $DB->start_delegated_transaction(); 00310 question_engine::delete_questions_usage_by_activity($previewid); 00311 $transaction->allow_commit(); 00312 } 00313 redirect(question_preview_url($questionid, $displayoptions->behaviour, 00314 $displayoptions->maxmark, $displayoptions, $displayoptions->variant, $context)); 00315 }