mirror of
https://github.com/moodle/moodle.git
synced 2025-08-08 10:26:40 +02:00
MDL-20636 Fix a lot more coding style issues.
This commit is contained in:
parent
e0736817f0
commit
59a3fcd3eb
9 changed files with 601 additions and 750 deletions
|
@ -55,9 +55,8 @@ class backup_qtype_randomsamatch_plugin extends backup_qtype_plugin {
|
|||
$pluginwrapper->add_child($randomsamatch);
|
||||
|
||||
// set source to populate the data
|
||||
$randomsamatch->set_source_table('question_randomsamatch', array('question' => backup::VAR_PARENTID));
|
||||
|
||||
// don't need to annotate ids nor files
|
||||
$randomsamatch->set_source_table('question_randomsamatch',
|
||||
array('question' => backup::VAR_PARENTID));
|
||||
|
||||
return $plugin;
|
||||
}
|
||||
|
|
|
@ -43,10 +43,9 @@ class restore_qtype_randomsamatch_plugin extends restore_qtype_plugin {
|
|||
|
||||
// Add own qtype stuff
|
||||
$elename = 'randomsamatch';
|
||||
$elepath = $this->get_pathfor('/randomsamatch'); // we used get_recommended_name() so this works
|
||||
$elepath = $this->get_pathfor('/randomsamatch');
|
||||
$paths[] = new restore_path_element($elename, $elepath);
|
||||
|
||||
|
||||
return $paths; // And we return the interesting paths
|
||||
}
|
||||
|
||||
|
@ -64,7 +63,8 @@ class restore_qtype_randomsamatch_plugin extends restore_qtype_plugin {
|
|||
$newquestionid = $this->get_new_parentid('question');
|
||||
$questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
|
||||
|
||||
// If the question has been created by restore, we need to create its question_randomsamatch too
|
||||
// If the question has been created by restore, we need to create its
|
||||
// question_randomsamatch too
|
||||
if ($questioncreated) {
|
||||
// Adjust some columns
|
||||
$data->question = $newquestionid;
|
||||
|
@ -72,8 +72,6 @@ class restore_qtype_randomsamatch_plugin extends restore_qtype_plugin {
|
|||
$newitemid = $DB->insert_record('question_randomsamatch', $data);
|
||||
// Create mapping
|
||||
$this->set_mapping('question_randomsamatch', $oldid, $newitemid);
|
||||
} else {
|
||||
// Nothing to remap if the question already existed
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -33,19 +33,15 @@ defined('MOODLE_INTERNAL') || die();
|
|||
* @copyright 2007 Jamie Pratt me@jamiep.org
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||
*/
|
||||
class question_edit_randomsamatch_form extends question_edit_form {
|
||||
/**
|
||||
* Add question-type specific form fields.
|
||||
*
|
||||
* @param MoodleQuickForm $mform the form being built.
|
||||
*/
|
||||
protected function definition_inner(&$mform) {
|
||||
class qtype_randomsamatch_edit_form extends question_edit_form {
|
||||
protected function definition_inner($mform) {
|
||||
$questionstoselect = array();
|
||||
for ($i=2; $i<=QUESTION_NUMANS; $i++){
|
||||
for ($i = 2; $i <= qtype_randomsamatch::MAX_SUBQUESTIONS; $i++) {
|
||||
$questionstoselect[$i] = $i;
|
||||
}
|
||||
|
||||
$mform->addElement('select', 'choose', get_string("randomsamatchnumber", "quiz"), $questionstoselect);
|
||||
$mform->addElement('select', 'choose',
|
||||
get_string('randomsamatchnumber', 'quiz'), $questionstoselect);
|
||||
$mform->setType('feedback', PARAM_RAW);
|
||||
|
||||
$mform->addElement('hidden', 'fraction', 0);
|
||||
|
@ -54,11 +50,11 @@ class question_edit_randomsamatch_form extends question_edit_form {
|
|||
|
||||
protected function data_preprocessing($question) {
|
||||
if (empty($question->name)) {
|
||||
$question->name = get_string("randomsamatch", "quiz");
|
||||
$question->name = get_string('randomsamatch', 'quiz');
|
||||
}
|
||||
|
||||
if (empty($question->questiontext)) {
|
||||
$question->questiontext = get_string("randomsamatchintro", "quiz");
|
||||
$question->questiontext = get_string('randomsamatchintro', 'quiz');
|
||||
}
|
||||
return $question;
|
||||
}
|
||||
|
@ -67,22 +63,22 @@ class question_edit_randomsamatch_form extends question_edit_form {
|
|||
return 'randomsamatch';
|
||||
}
|
||||
|
||||
function validation($data, $files) {
|
||||
global $QTYPES, $DB;
|
||||
public function validation($data, $files) {
|
||||
global $DB;
|
||||
$errors = parent::validation($data, $files);
|
||||
if (isset($data->categorymoveto)) {
|
||||
list($category) = explode(',', $data['categorymoveto']);
|
||||
} else {
|
||||
list($category) = explode(',', $data['category']);
|
||||
}
|
||||
$saquestions = $QTYPES['randomsamatch']->get_sa_candidates($category);
|
||||
$saquestions = question_bank::get_qtype('randomsamatch')->get_sa_candidates($category);
|
||||
$numberavailable = count($saquestions);
|
||||
if ($saquestions === false){
|
||||
if ($saquestions === false) {
|
||||
$a = new stdClass();
|
||||
$a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
|
||||
$errors['choose'] = get_string('nosaincategory', 'qtype_randomsamatch', $a);
|
||||
|
||||
} elseif ($numberavailable < $data['choose']){
|
||||
} else if ($numberavailable < $data['choose']) {
|
||||
$a = new stdClass();
|
||||
$a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
|
||||
$a->nosaquestions = $numberavailable;
|
||||
|
|
|
@ -36,27 +36,21 @@ defined('MOODLE_INTERNAL') || die();
|
|||
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class question_randomsamatch_qtype extends qtype_match {
|
||||
/// Extends 'match' as there are quite a few simularities...
|
||||
class qtype_randomsamatch extends question_type {
|
||||
const MAX_SUBQUESTIONS = 10;
|
||||
|
||||
function name() {
|
||||
return 'randomsamatch';
|
||||
public function requires_qtypes() {
|
||||
return array('shortanswer', 'match');
|
||||
}
|
||||
|
||||
function requires_qtypes() {
|
||||
return array('shortanswer');
|
||||
}
|
||||
|
||||
function is_usable_by_random() {
|
||||
public function is_usable_by_random() {
|
||||
return false;
|
||||
}
|
||||
|
||||
function get_question_options(&$question) {
|
||||
global $DB, $OUTPUT;
|
||||
if (!$question->options = $DB->get_record('question_randomsamatch', array('question' => $question->id))) {
|
||||
echo $OUTPUT->notification('Error: Missing question options for random short answer question '.$question->id.'!');
|
||||
return false;
|
||||
}
|
||||
public function get_question_options($question) {
|
||||
global $DB;
|
||||
$question->options = $DB->get_record('question_randomsamatch',
|
||||
array('question' => $question->id), '*', MUST_EXIST);
|
||||
|
||||
// This could be included as a flag in the database. It's already
|
||||
// supported by the code.
|
||||
|
@ -66,7 +60,7 @@ class question_randomsamatch_qtype extends qtype_match {
|
|||
|
||||
}
|
||||
|
||||
function save_question_options($question) {
|
||||
public function save_question_options($question) {
|
||||
global $DB;
|
||||
$options->question = $question->id;
|
||||
$options->choose = $question->choose;
|
||||
|
@ -76,23 +70,24 @@ class question_randomsamatch_qtype extends qtype_match {
|
|||
return $result;
|
||||
}
|
||||
|
||||
if ($existing = $DB->get_record("question_randomsamatch", array("question" => $options->question))) {
|
||||
if ($existing = $DB->get_record('question_randomsamatch',
|
||||
array('question' => $options->question))) {
|
||||
$options->id = $existing->id;
|
||||
$DB->update_record("question_randomsamatch", $options);
|
||||
$DB->update_record('question_randomsamatch', $options);
|
||||
} else {
|
||||
$DB->insert_record("question_randomsamatch", $options);
|
||||
$DB->insert_record('question_randomsamatch', $options);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function delete_question($questionid, $contextid) {
|
||||
public function delete_question($questionid, $contextid) {
|
||||
global $DB;
|
||||
$DB->delete_records('question_randomsamatch', array('question' => $questionid));
|
||||
|
||||
parent::delete_question($questionid, $contextid);
|
||||
}
|
||||
|
||||
function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
|
||||
public function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
|
||||
// Choose a random shortanswer question from the category:
|
||||
// We need to make sure that no question is used more than once in the
|
||||
// quiz. Therfore the following need to be excluded:
|
||||
|
@ -190,7 +185,7 @@ class question_randomsamatch_qtype extends qtype_match {
|
|||
foreach ($responses as $response) {
|
||||
$wqid = $response[0];
|
||||
$state->responses[$wqid] = $response[1];
|
||||
if (!isset($wrappedquestions[$wqid])){
|
||||
if (!isset($wrappedquestions[$wqid])) {
|
||||
if (!$wrappedquestions[$wqid] = $DB->get_record('question', array('id' => $wqid))) {
|
||||
echo $OUTPUT->notification("Couldn't get question (id=$wqid)!");
|
||||
return false;
|
||||
|
@ -231,23 +226,11 @@ class question_randomsamatch_qtype extends qtype_match {
|
|||
return true;
|
||||
}
|
||||
|
||||
function extract_response($rawresponse, $nameprefix) {
|
||||
/// Simple implementation that does not check with the database
|
||||
/// and thus - does not bother to check whether there has been
|
||||
/// any changes to the question options.
|
||||
$response = array();
|
||||
$rawitems = explode(',', $rawresponse->answer);
|
||||
foreach ($rawitems as $rawitem) {
|
||||
$splits = explode('-', $rawitem, 2);
|
||||
$response[$nameprefix.$splits[0]] = $splits[1];
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
function get_sa_candidates($categorylist, $questionsinuse = 0) {
|
||||
public function get_sa_candidates($categorylist, $questionsinuse = 0) {
|
||||
global $DB;
|
||||
list ($usql, $params) = $DB->get_in_or_equal($categorylist);
|
||||
list ($ques_usql, $ques_params) = $DB->get_in_or_equal(explode(',', $questionsinuse), SQL_PARAMS_QM, null, false);
|
||||
list ($ques_usql, $ques_params) = $DB->get_in_or_equal(explode(',', $questionsinuse),
|
||||
SQL_PARAMS_QM, null, false);
|
||||
$params = array_merge($params, $ques_params);
|
||||
return $DB->get_records_select('question',
|
||||
"qtype = 'shortanswer' " .
|
||||
|
@ -256,80 +239,6 @@ class question_randomsamatch_qtype extends qtype_match {
|
|||
"AND hidden = '0'" .
|
||||
"AND id $ques_usql", $params);
|
||||
}
|
||||
function get_all_responses($question, $state) {
|
||||
$answers = array();
|
||||
if (is_array($question->options->subquestions)) {
|
||||
foreach ($question->options->subquestions as $aid => $answer) {
|
||||
if ($answer->questiontext) {
|
||||
foreach($answer->options->answers as $ans ){
|
||||
$answer->answertext = $ans->answer ;
|
||||
}
|
||||
$r = new stdClass();
|
||||
$r->answer = $answer->questiontext . ": " . $answer->answertext;
|
||||
$r->credit = 1;
|
||||
$answers[$aid] = $r;
|
||||
}
|
||||
}
|
||||
}
|
||||
$result = new stdClass();
|
||||
$result->id = $question->id;
|
||||
$result->responses = $answers;
|
||||
return $result;
|
||||
}
|
||||
/**
|
||||
* The difference between this method an get_all_responses is that this
|
||||
* method is not passed a state object. It is the possible answers to a
|
||||
* question no matter what the state.
|
||||
* This method is not called for random questions.
|
||||
* @return array of possible answers.
|
||||
*/
|
||||
function get_possible_responses(&$question) {
|
||||
global $QTYPES;
|
||||
static $answers = array();
|
||||
if (!isset($answers[$question->id])){
|
||||
if ($question->options->subcats) {
|
||||
// recurse into subcategories
|
||||
$categorylist = question_categorylist($question->category);
|
||||
} else {
|
||||
$categorylist = array($question->category);
|
||||
}
|
||||
|
||||
$question->options->subquestions = $this->get_sa_candidates($categorylist);
|
||||
foreach ($question->options->subquestions as $key => $wrappedquestion) {
|
||||
if (!$QTYPES[$wrappedquestion->qtype]
|
||||
->get_question_options($wrappedquestion)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now we overwrite the $question->options->answers field to only
|
||||
// *one* (the first) correct answer. This loop can be deleted to
|
||||
// take all answers into account (i.e. put them all into the
|
||||
// drop-down menu.
|
||||
$foundcorrect = false;
|
||||
foreach ($wrappedquestion->options->answers as $answer) {
|
||||
if ($foundcorrect || $answer->fraction != 1.0) {
|
||||
unset($wrappedquestion->options->answers[$answer->id]);
|
||||
} else if (!$foundcorrect) {
|
||||
$foundcorrect = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
$answers[$question->id] = array();
|
||||
if (is_array($question->options->subquestions)) {
|
||||
foreach ($question->options->subquestions as $subqid => $answer) {
|
||||
if ($answer->questiontext) {
|
||||
$ans = array_shift($answer->options->answers);
|
||||
$answer->answertext = $ans->answer ;
|
||||
$r = new stdClass();
|
||||
$r->answer = $answer->questiontext . ": " . $answer->answertext;
|
||||
$r->credit = 1;
|
||||
$answers[$question->id][$subqid] = array($ans->id => $r);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $answers[$question->id];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param object $question
|
||||
|
@ -337,7 +246,7 @@ class question_randomsamatch_qtype extends qtype_match {
|
|||
* guess by a student might give or an empty string which means will not
|
||||
* calculate.
|
||||
*/
|
||||
function get_random_guess_score($question) {
|
||||
public function get_random_guess_score($question) {
|
||||
return 1/$question->options->choose;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue