public function validation($data, $files) { $errors = parent::validation($data, $files); $answers = $data['answer']; $answercount = 0; $maxgrade = false; foreach ($answers as $key => $answer) { $trimmedanswer = trim($answer); if ($trimmedanswer !== '') { $answercount++; if ($data['fraction'][$key] == 1) { $maxgrade = true; } } else { if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key]['text'])) { $errors["answer[{$key}]"] = get_string('answermustbegiven', 'qtype_shortanswer'); $answercount++; } } } if ($answercount == 0) { $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_shortanswer', 1); } if ($maxgrade == false) { $errors['fraction[0]'] = get_string('fractionsnomax', 'question'); } return $errors; }
function save_question_options($question) { global $DB; $result = new stdClass(); if (!($oldanswers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC'))) { $oldanswers = array(); } $answers = array(); $maxfraction = -1; // Insert all the new answers foreach ($question->answer as $key => $dataanswer) { // Check for, and ingore, completely blank answer from the form. if (trim($dataanswer) == '' && $question->fraction[$key] == 0 && html_is_blank($question->feedback[$key])) { continue; } if ($oldanswer = array_shift($oldanswers)) { // Existing answer, so reuse it $answer = $oldanswer; $answer->answer = trim($dataanswer); $answer->fraction = $question->fraction[$key]; $answer->feedback = $question->feedback[$key]; $DB->update_record("question_answers", $answer); } else { // This is a completely new answer $answer = new stdClass(); $answer->answer = trim($dataanswer); $answer->question = $question->id; $answer->fraction = $question->fraction[$key]; $answer->feedback = $question->feedback[$key]; $answer->id = $DB->insert_record("question_answers", $answer); } $answers[] = $answer->id; if ($question->fraction[$key] > $maxfraction) { $maxfraction = $question->fraction[$key]; } } $question->answers = implode(',', $answers); $parentresult = parent::save_question_options($question); if ($parentresult !== null) { // Parent function returns null if all is OK return $parentresult; } // delete old answer records if (!empty($oldanswers)) { foreach ($oldanswers as $oa) { $DB->delete_records('question_answers', array('id' => $oa->id)); } } /// Perform sanity checks on fractional grades if ($maxfraction != 1) { $maxfraction = $maxfraction * 100; $result->noticeyesno = get_string("fractionsnomax", "quiz", $maxfraction); return $result; } else { return true; } }
function save_question_options($question) { global $DB; $result = new stdClass(); $context = $question->context; $oldanswers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC'); // Insert all the new answers $answers = array(); $maxfraction = -1; foreach ($question->answer as $key => $answerdata) { // Check for, and ignore, completely blank answer from the form. if (trim($answerdata) == '' && $question->fraction[$key] == 0 && html_is_blank($question->feedback[$key]['text'])) { continue; } // Update an existing answer if possible. $answer = array_shift($oldanswers); if (!$answer) { $answer = new stdClass(); $answer->question = $question->id; $answer->answer = ''; $answer->feedback = ''; $answer->id = $DB->insert_record('question_answers', $answer); } $answer->answer = trim($answerdata); $answer->fraction = $question->fraction[$key]; $answer->feedback = $this->import_or_save_files($question->feedback[$key], $context, 'question', 'answerfeedback', $answer->id); $answer->feedbackformat = $question->feedback[$key]['format']; $DB->update_record('question_answers', $answer); $answers[] = $answer->id; if ($question->fraction[$key] > $maxfraction) { $maxfraction = $question->fraction[$key]; } } // Delete any left over old answer records. $fs = get_file_storage(); foreach ($oldanswers as $oldanswer) { $fs->delete_area_files($context->id, 'question', 'answerfeedback', $oldanswer->id); $DB->delete_records('question_answers', array('id' => $oldanswer->id)); } $question->answers = implode(',', $answers); $parentresult = parent::save_question_options($question); if ($parentresult !== null) { // Parent function returns null if all is OK return $parentresult; } // Perform sanity checks on fractional grades if ($maxfraction != 1) { $result->noticeyesno = get_string('fractionsnomax', 'quiz', $maxfraction * 100); return $result; } return true; }
/** * Output the quiz intro. * @param object $quiz the quiz settings. * @param object $cm the course_module object. * @return string HTML to output. */ public function quiz_intro($quiz, $cm) { if (html_is_blank($quiz->intro)) { return ''; } return $this->box(format_module_intro('quiz', $quiz, $cm->id), 'generalbox', 'intro'); }
public function read_submitted_permissions() { global $DB; $this->errors = array(); // Role short name. We clean this in a special way. We want to end up // with only lowercase safe ASCII characters. $shortname = optional_param('shortname', null, PARAM_RAW); if (!is_null($shortname)) { $this->role->shortname = $shortname; $this->role->shortname = textlib::specialtoascii($this->role->shortname); $this->role->shortname = textlib::strtolower(clean_param($this->role->shortname, PARAM_ALPHANUMEXT)); if (empty($this->role->shortname)) { $this->errors['shortname'] = get_string('errorbadroleshortname', 'role'); } } if ($DB->record_exists_select('role', 'shortname = ? and id <> ?', array($this->role->shortname, $this->roleid))) { $this->errors['shortname'] = get_string('errorexistsroleshortname', 'role'); } // Role name. $name = optional_param('name', null, PARAM_TEXT); if (!is_null($name)) { $this->role->name = $name; // Hack: short names of standard roles are equal to archetypes, empty name means localised via lang packs. $archetypes = get_role_archetypes(); if (!isset($archetypes[$shortname]) and html_is_blank($this->role->name)) { $this->errors['name'] = get_string('errorbadrolename', 'role'); } } if ($this->role->name !== '' and $DB->record_exists_select('role', 'name = ? and id <> ?', array($this->role->name, $this->roleid))) { $this->errors['name'] = get_string('errorexistsrolename', 'role'); } // Description. $description = optional_param('description', null, PARAM_RAW); if (!is_null($description)) { $this->role->description = $description; } // Legacy type. $archetype = optional_param('archetype', null, PARAM_RAW); if (isset($archetype)) { $archetypes = get_role_archetypes(); if (isset($archetypes[$archetype])) { $this->role->archetype = $archetype; } else { $this->role->archetype = ''; } } // Assignable context levels. foreach ($this->allcontextlevels as $cl => $notused) { $assignable = optional_param('contextlevel' . $cl, null, PARAM_BOOL); if (!is_null($assignable)) { if ($assignable) { $this->contextlevels[$cl] = $cl; } else { unset($this->contextlevels[$cl]); } } } // Now read the permissions for each capability. parent::read_submitted_permissions(); }
function save_question_options($question) { $result = new stdClass(); if (!($oldanswers = get_records('question_answers', 'question', $question->id, 'id ASC'))) { $oldanswers = array(); } $answers = array(); $maxfraction = -1; // Insert all the new answers foreach ($question->answer as $key => $dataanswer) { // Check for, and ingore, completely blank answer from the form. if (trim($dataanswer) == '' && $question->fraction[$key] == 0 && html_is_blank($question->feedback[$key])) { continue; } if ($oldanswer = array_shift($oldanswers)) { // Existing answer, so reuse it $answer = $oldanswer; $answer->answer = trim($dataanswer); $answer->fraction = $question->fraction[$key]; $answer->feedback = $question->feedback[$key]; if (!update_record("question_answers", $answer)) { $result->error = "Could not update quiz answer! (id={$answer->id})"; return $result; } } else { // This is a completely new answer $answer = new stdClass(); $answer->answer = trim($dataanswer); $answer->question = $question->id; $answer->fraction = $question->fraction[$key]; $answer->feedback = $question->feedback[$key]; if (!($answer->id = insert_record("question_answers", $answer))) { $result->error = "Could not insert quiz answer!"; return $result; } } $answers[] = $answer->id; if ($question->fraction[$key] > $maxfraction) { $maxfraction = $question->fraction[$key]; } } if ($options = get_record("question_shortanswer", "question", $question->id)) { $options->answers = implode(",", $answers); $options->usecase = $question->usecase; if (!update_record("question_shortanswer", $options)) { $result->error = "Could not update quiz shortanswer options! (id={$options->id})"; return $result; } } else { unset($options); $options->question = $question->id; $options->answers = implode(",", $answers); $options->usecase = $question->usecase; if (!insert_record("question_shortanswer", $options)) { $result->error = "Could not insert quiz shortanswer options!"; return $result; } } // delete old answer records if (!empty($oldanswers)) { foreach ($oldanswers as $oa) { delete_records('question_answers', 'id', $oa->id); } } /// Perform sanity checks on fractional grades if ($maxfraction != 1) { $maxfraction = $maxfraction * 100; $result->noticeyesno = get_string("fractionsnomax", "quiz", $maxfraction); return $result; } else { return true; } }
/** * Validate the answers. * @param array $data the submitted data. * @param array $errors the errors array to add to. * @return array the updated errors array. */ protected function validate_answers($data, $errors) { // Check the answers. $answercount = 0; $maxgrade = false; $answers = $data['answer']; foreach ($answers as $key => $answer) { $trimmedanswer = trim($answer); if ($trimmedanswer != '') { $answercount++; if (!$this->is_valid_answer($trimmedanswer, $data)) { $errors['answer[' . $key . ']'] = $this->valid_answer_message(); } if ($data['fraction'][$key] == 1) { $maxgrade = true; } if (!is_numeric($data['tolerance'][$key])) { $errors['tolerance['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated'); } } else if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key]['text'])) { $errors['answer[' . $key . ']'] = $this->valid_answer_message(); $answercount++; } } if ($answercount == 0) { $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_numerical'); } if ($maxgrade == false) { $errors['fraction[0]'] = get_string('fractionsnomax', 'question'); } }
function validation($data, $files) { global $QTYPES; $errors = parent::validation($data, $files); // Check the answers. $answercount = 0; $maxgrade = false; $answers = $data['answer']; foreach ($answers as $key => $answer) { $trimmedanswer = trim($answer); if ($trimmedanswer != '') { $answercount++; if (!(is_numeric($trimmedanswer) || $trimmedanswer == '*')) { $errors["answer[{$key}]"] = get_string('answermustbenumberorstar', 'qtype_numerical'); } if ($data['fraction'][$key] == 1) { $maxgrade = true; } } else { if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key]['text'])) { $errors["answer[{$key}]"] = get_string('answermustbenumberorstar', 'qtype_numerical'); $answercount++; } } } if ($answercount == 0) { $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_numerical'); } if ($maxgrade == false) { $errors['fraction[0]'] = get_string('fractionsnomax', 'question'); } $QTYPES['numerical']->validate_numerical_options($data, $errors); return $errors; }
/** * Save the units and the answers associated with this question. */ function save_question_options($question) { // Get old versions of the objects if (!($oldanswers = get_records('question_answers', 'question', $question->id, 'id ASC'))) { $oldanswers = array(); } if (!($oldoptions = get_records('question_numerical', 'question', $question->id, 'answer ASC'))) { $oldoptions = array(); } // Save the units. $result = $this->save_numerical_units($question); if (isset($result->error)) { return $result; } else { $units =& $result->units; } // Insert all the new answers foreach ($question->answer as $key => $dataanswer) { // Check for, and ingore, completely blank answer from the form. if (trim($dataanswer) == '' && $question->fraction[$key] == 0 && html_is_blank($question->feedback[$key])) { continue; } $answer = new stdClass(); $answer->question = $question->id; if (trim($dataanswer) === '*') { $answer->answer = '*'; } else { $answer->answer = $this->apply_unit($dataanswer, $units); if ($answer->answer === false) { $result->notice = get_string('invalidnumericanswer', 'quiz'); } } $answer->fraction = $question->fraction[$key]; $answer->feedback = trim($question->feedback[$key]); if ($oldanswer = array_shift($oldanswers)) { // Existing answer, so reuse it $answer->id = $oldanswer->id; if (!update_record("question_answers", $answer)) { $result->error = "Could not update quiz answer! (id={$answer->id})"; return $result; } } else { // This is a completely new answer if (!($answer->id = insert_record("question_answers", $answer))) { $result->error = "Could not insert quiz answer!"; return $result; } } // Set up the options object if (!($options = array_shift($oldoptions))) { $options = new stdClass(); } $options->question = $question->id; $options->answer = $answer->id; if (trim($question->tolerance[$key]) == '') { $options->tolerance = ''; } else { $options->tolerance = $this->apply_unit($question->tolerance[$key], $units); if ($options->tolerance === false) { $result->notice = get_string('invalidnumerictolerance', 'quiz'); } } // Save options if (isset($options->id)) { // reusing existing record if (!update_record('question_numerical', $options)) { $result->error = "Could not update quiz numerical options! (id={$options->id})"; return $result; } } else { // new options if (!insert_record('question_numerical', $options)) { $result->error = "Could not insert quiz numerical options!"; return $result; } } } // delete old answer records if (!empty($oldanswers)) { foreach ($oldanswers as $oa) { delete_records('question_answers', 'id', $oa->id); } } // delete old answer records if (!empty($oldoptions)) { foreach ($oldoptions as $oo) { delete_records('question_numerical', 'id', $oo->id); } } // Report any problems. if (!empty($result->notice)) { return $result; } return true; }
/** * Checks whether two manual grading actions are the same. That is, whether * the comment, and the mark (if given) is the same. * * @param question_attempt_step $pendingstep contains the new responses. * @return bool whether the new response is the same as we already have. */ protected function is_same_comment($pendingstep) { $previouscomment = $this->qa->get_last_behaviour_var('comment'); $newcomment = $pendingstep->get_behaviour_var('comment'); if (is_null($previouscomment) && !html_is_blank($newcomment) || $previouscomment != $newcomment) { return false; } // So, now we know the comment is the same, so check the mark, if present. $previousfraction = $this->qa->get_fraction(); $newmark = $pendingstep->get_behaviour_var('mark'); if (is_null($previousfraction)) { return is_null($newmark) || $newmark === ''; } else if (is_null($newmark) || $newmark === '') { return false; } $newfraction = $newmark / $pendingstep->get_behaviour_var('maxmark'); return abs($newfraction - $previousfraction) < 0.0000001; }
print_error('queryfailed', 'report_customsql', report_customsql_url('index.php'), $e); } } else { $cvstimestamp = optional_param('timestamp', time(), PARAM_INT); } // Start the page. admin_externalpage_setup('reportcustomsql'); admin_externalpage_print_header(); print_heading(format_string($report->displayname)); echo '<style>'; // (nadavkav) //include('styles.php'); echo '.reporttitle { font-size:1.1em; } '; //echo '#admin-report-customsql-edit #id_querysql { direction:ltr; } '; echo '</style>'; if (!html_is_blank($report->description)) { echo '<p>' . format_text($report->description, FORMAT_HTML) . '</p>'; } $count = 0; if (is_null($cvstimestamp)) { echo '<p>' . get_string('nodatareturned', 'report_customsql') . '</p>'; } else { list($csvfilename, $cvstimestamp) = report_customsql_csv_filename($report, $cvstimestamp); if (!is_readable($csvfilename)) { echo '<p>' . get_string('notrunyet', 'report_customsql') . '</p>'; } else { $handle = fopen($csvfilename, 'r'); if ($report->runable != 'manual' && !$report->singlerow) { print_heading(get_string('reportfor', 'report_customsql', userdate($cvstimestamp, get_string('strftimedate'))), '', 3); } $table = new stdClass();
public function read_submitted_permissions() { global $DB; $this->errors = array(); // Role name. $name = optional_param('name', null, PARAM_MULTILANG); if (!is_null($name)) { $this->role->name = $name; if (html_is_blank($this->role->name)) { $this->errors['name'] = get_string('errorbadrolename', 'role'); } } if ($DB->record_exists_select('role', 'name = ? and id <> ?', array($this->role->name, $this->roleid))) { $this->errors['name'] = get_string('errorexistsrolename', 'role'); } // Role short name. We clean this in a special way. We want to end up // with only lowercase safe ASCII characters. $shortname = optional_param('shortname', null, PARAM_RAW); if (!is_null($shortname)) { $this->role->shortname = $shortname; $this->role->shortname = textlib_get_instance()->specialtoascii($this->role->shortname); $this->role->shortname = moodle_strtolower(clean_param($this->role->shortname, PARAM_ALPHANUMEXT)); if (empty($this->role->shortname)) { $this->errors['shortname'] = get_string('errorbadroleshortname', 'role'); } } if ($DB->record_exists_select('role', 'shortname = ? and id <> ?', array($this->role->shortname, $this->roleid))) { $this->errors['shortname'] = get_string('errorexistsroleshortname', 'role'); } // Description. $description = optional_param('description', null, PARAM_CLEAN); if (!is_null($description)) { $this->role->description = $description; } // Legacy type. $legacytype = optional_param('legacytype', null, PARAM_RAW); if (!is_null($legacytype)) { if (array_key_exists($legacytype, $this->legacyroles)) { $this->role->legacytype = $legacytype; } else { $this->role->legacytype = ''; } } // Assignable context levels. foreach ($this->allcontextlevels as $cl => $notused) { $assignable = optional_param('contextlevel' . $cl, null, PARAM_BOOL); if (!is_null($assignable)) { if ($assignable) { $this->contextlevels[$cl] = $cl; } else { unset($this->contextlevels[$cl]); } } } // Now read the permissions for each capability. parent::read_submitted_permissions(); }
function validation($data, $files) { $errors = parent::validation($data, $files); // Check the answers. $answercount = 0; $maxgrade = false; $answers = $data['answer']; foreach ($answers as $key => $answer) { $trimmedanswer = trim($answer); if ($trimmedanswer != '') { $answercount++; if (!(is_numeric($trimmedanswer) || $trimmedanswer == '*')) { $errors["answer[{$key}]"] = get_string('answermustbenumberorstar', 'qtype_numerical'); } if ($data['fraction'][$key] == 1) { $maxgrade = true; } } else { if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key])) { $errors["answer[{$key}]"] = get_string('answermustbenumberorstar', 'qtype_numerical'); $answercount++; } } } if ($answercount == 0) { $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_numerical'); } if ($maxgrade == false) { $errors['fraction[0]'] = get_string('fractionsnomax', 'question'); } // Check units. $alreadyseenunits = array(); if (isset($data['unit'])) { foreach ($data['unit'] as $key => $unit) { $trimmedunit = trim($unit); if ($trimmedunit != '' && in_array($trimmedunit, $alreadyseenunits)) { $errors["unit[{$key}]"] = get_string('errorrepeatedunit', 'qtype_numerical'); if (trim($data['multiplier'][$key]) == '') { $errors["multiplier[{$key}]"] = get_string('errornomultiplier', 'qtype_numerical'); } } else { $alreadyseenunits[] = $trimmedunit; } } } return $errors; }
public function get_question_summary() { $summary = ''; if (!html_is_blank($this->questiontext)) { $question = $this->html_to_text($this->questiontext, $this->questiontextformat); $summary .= $question . '; '; } $places = array(); foreach ($this->places as $place) { $cs = array(); foreach ($this->choices[$place->group] as $choice) { $cs[] = $choice->summarise(); } $places[] = '[[' . $place->summarise() . ']] -> {' . implode(' / ', $cs) . '}'; } $summary .= implode('; ', $places); return $summary; }
public function validation($data, $files) { global $CFG; require_once $CFG->dirroot . '/question/type/regexp/locallib.php'; $errors = parent::validation($data, $files); $answers = $data['answer']; $data['fraction'][0] = 1; $grades = $data['fraction']; $answercount = 0; $illegalmetacharacters = ". ^ \$ * + ? { } \\"; foreach ($answers as $key => $answer) { $trimmedanswer = trim($answer); if ($trimmedanswer !== '') { $answercount++; $parenserror = ''; $metacharserror = ''; // we do not check parenthesis and square brackets in Answer 1 (correct answer) if ($key > 0) { $markedline = ''; for ($i = 0; $i < strlen($trimmedanswer); $i++) { $markedline .= ' '; } $parenserror = check_my_parens($trimmedanswer, $markedline); if ($parenserror) { $errors["answer[{$key}]"] = get_string("regexperrorparen", "qtype_regexp") . '<br />'; $markedline = $parenserror; } // we do not test unescaped metacharacters in Answers expressions for incorrect responses (grade = None) if ($data['fraction'][$key] > 0) { $metacharserror = check_unescaped_metachars($trimmedanswer, $markedline); if ($metacharserror) { $errormessage = get_string("illegalcharacters", "qtype_regexp", $illegalmetacharacters); if (empty($errors["answer[{$key}]"])) { $errors["answer[{$key}]"] = $errormessage; } else { $errors["answer[{$key}]"] .= $errormessage; } } } if ($metacharserror || $parenserror) { $answerstringchunks = splitstring($trimmedanswer); $nbchunks = count($answerstringchunks); $errors["answer[{$key}]"] .= '<pre><div class="displayvalidationerrors">'; if ($metacharserror) { $illegalcharschunks = splitstring($metacharserror); for ($i = 0; $i < $nbchunks; $i++) { $errors["answer[{$key}]"] .= '<br />' . $answerstringchunks[$i] . '<br />' . $illegalcharschunks[$i]; } } elseif ($parenserror) { $illegalcharschunks = splitstring($parenserror); for ($i = 0; $i < $nbchunks; $i++) { $errors["answer[{$key}]"] .= '<br />' . $answerstringchunks[$i] . '<br />' . $illegalcharschunks[$i]; } } $errors["answer[{$key}]"] .= '</div></pre>'; } } } else { if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key]['text'])) { if ($key === 0) { $errors['answer[0]'] = get_string('answer1mustbegiven', 'qtype_regexp'); } else { $errors["answer[{$key}]"] = get_string('answermustbegiven', 'qtype_regexp'); } $answercount++; } } } return $errors; }
function wrsqz_html_is_blank($str){ if (function_exists("html_is_blank")){ //If moodle in new enough return html_is_blank($str); }else{ //If moodle is old return wrsqz_deprecated_html_is_blank($str); } }
/** * Prints a maintenance message from $CFG->maintenance_message or default if empty. */ function print_maintenance_message() { global $CFG, $SITE, $PAGE, $OUTPUT; header($_SERVER['SERVER_PROTOCOL'] . ' 503 Moodle under maintenance'); header('Status: 503 Moodle under maintenance'); header('Retry-After: 300'); $PAGE->set_pagetype('maintenance-message'); $PAGE->set_pagelayout('maintenance'); $PAGE->set_title(strip_tags($SITE->fullname)); $PAGE->set_heading($SITE->fullname); echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('sitemaintenance', 'admin')); if (isset($CFG->maintenance_message) and !html_is_blank($CFG->maintenance_message)) { echo $OUTPUT->box_start('maintenance_message generalbox boxwidthwide boxaligncenter'); echo $CFG->maintenance_message; echo $OUTPUT->box_end(); } echo $OUTPUT->footer(); die; }
/** * Enables CLI maintenance mode by creating new dataroot/climaintenance.html file. */ function enable_cli_maintenance_mode() { global $CFG; if (file_exists("{$CFG->dataroot}/climaintenance.html")) { unlink("{$CFG->dataroot}/climaintenance.html"); } if (isset($CFG->maintenance_message) and !html_is_blank($CFG->maintenance_message)) { $data = $CFG->maintenance_message; $data = bootstrap_renderer::early_error_content($data, null, null, null); $data = bootstrap_renderer::plain_page(get_string('sitemaintenance', 'admin'), $data); } else { if (file_exists("{$CFG->dataroot}/climaintenance.template.html")) { $data = file_get_contents("{$CFG->dataroot}/climaintenance.template.html"); } else { $data = get_string('sitemaintenance', 'admin'); $data = bootstrap_renderer::early_error_content($data, null, null, null); $data = bootstrap_renderer::plain_page(get_string('sitemaintenance', 'admin'), $data); } } file_put_contents("{$CFG->dataroot}/climaintenance.html", $data); chmod("{$CFG->dataroot}/climaintenance.html", $CFG->filepermissions); }
public function save_hints($formdata, $withparts = false) { global $DB; $context = $formdata->context; $oldhints = $DB->get_records('question_hints', array('questionid' => $formdata->id), 'id ASC'); $numhints = $this->count_hints_on_form($formdata, $withparts); for ($i = 0; $i < $numhints; $i += 1) { if (html_is_blank($formdata->hint[$i]['text'])) { $formdata->hint[$i]['text'] = ''; } if ($withparts) { $clearwrong = !empty($formdata->hintclearwrong[$i]); $shownumcorrect = !empty($formdata->hintshownumcorrect[$i]); } if ($this->is_hint_empty_in_form_data($formdata, $i, $withparts)) { continue; } // Update an existing hint if possible. $hint = array_shift($oldhints); if (!$hint) { $hint = new stdClass(); $hint->questionid = $formdata->id; $hint->hint = ''; $hint->id = $DB->insert_record('question_hints', $hint); } $hint->hint = $this->import_or_save_files($formdata->hint[$i], $context, 'question', 'hint', $hint->id); $hint->hintformat = $formdata->hint[$i]['format']; if ($withparts) { $hint->clearwrong = $clearwrong; $hint->shownumcorrect = $shownumcorrect; } $hint->options = $this->save_hint_options($formdata, $i, $withparts); $DB->update_record('question_hints', $hint); } // Delete any remaining old hints. $fs = get_file_storage(); foreach ($oldhints as $oldhint) { $fs->delete_area_files($context->id, 'question', 'hint', $oldhint->id); $DB->delete_records('question_hints', array('id' => $oldhint->id)); } }
function save_question_options($question) { global $DB, $SESSION; $result = new stdClass(); $context = $question->context; $oldanswers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC'); $answers = array(); //$maxfraction = -1; // Insert all the new answers foreach ($question->answer as $key => $answerdata) { // Check for, and ignore, completely blank answer from the form. if (trim($answerdata) == '' && $question->fraction[$key] == 0 && html_is_blank($question->feedback[$key]['text'])) { continue; } // Update an existing answer if possible. $answer = array_shift($oldanswers); if (!$answer) { $answer = new stdClass(); $answer->question = $question->id; $answer->answer = ''; $answer->feedback = ''; $answer->id = $DB->insert_record('question_answers', $answer); } $answer->answer = trim($answerdata); // set grade for Answer 1 to 1 (100%) if ($key === 0) { $question->fraction[$key] = 1; } $answer->fraction = $question->fraction[$key]; $answer->feedback = $this->import_or_save_files($question->feedback[$key], $context, 'question', 'answerfeedback', $answer->id); $answer->feedbackformat = $question->feedback[$key]['format']; $DB->update_record('question_answers', $answer); $answers[] = $answer->id; /*if ($question->fraction[$key] > $maxfraction) { $maxfraction = $question->fraction[$key]; }*/ } $question->answers = implode(',', $answers); $parentresult = parent::save_question_options($question); if ($parentresult !== null) { // Parent function returns null if all is OK return $parentresult; } // Delete any left over old answer records. $fs = get_file_storage(); foreach ($oldanswers as $oldanswer) { $fs->delete_area_files($context->id, 'question', 'answerfeedback', $oldanswer->id); $DB->delete_records('question_answers', array('id' => $oldanswer->id)); } $this->save_hints($question); // JR dec 2011 unset alternateanswers and alternatecorrectanswers after question has been edited, just in case $qid = $question->id; if (isset($SESSION->qtype_regexp_question->alternateanswers[$qid])) { unset($SESSION->qtype_regexp_question->alternateanswers[$qid]); } if (isset($SESSION->qtype_regexp_question->alternatecorrectanswers[$qid])) { unset($SESSION->qtype_regexp_question->alternatecorrectanswers[$qid]); } // Perform sanity checks on fractional grades /* if ($maxfraction != 1) { $result->noticeyesno = get_string('fractionsnomax', 'quiz', $maxfraction * 100); return $result; }*/ }
/** * Returns localised role description if available. * If the name is empty it tries to find the default role name using * hardcoded list of default role names or other methods in the future. * * @param stdClass $role * @return string localised role name */ function role_get_description(stdClass $role) { if (!html_is_blank($role->description)) { return format_text($role->description, FORMAT_HTML, array('context'=>context_system::instance())); } switch ($role->shortname) { case 'manager': return get_string('managerdescription', 'role'); case 'coursecreator': return get_string('coursecreatorsdescription'); case 'editingteacher': return get_string('defaultcourseteacherdescription'); case 'teacher': return get_string('noneditingteacherdescription'); case 'student': return get_string('defaultcoursestudentdescription'); case 'guest': return get_string('guestdescription'); case 'user': return get_string('authenticateduserdescription'); case 'frontpage': return get_string('frontpageuserdescription', 'role'); default: return ''; } }
function report_customsql_get_message($report, $csvfilename) { $handle = fopen($csvfilename, 'r'); $table = new html_table(); $table->head = fgetcsv($handle); $countrows = 0; while ($row = fgetcsv($handle)) { $rowdata = array(); foreach ($row as $value) { $rowdata[] = $value; } $table->data[] = $rowdata; $countrows++; } fclose($handle); // Construct subject. $subject = get_string('emailsubject', 'report_customsql', $report->displayname); // Construct message without the table. $fullmessage = ''; if (!html_is_blank($report->description)) { $fullmessage .= html_writer::tag('p', format_text($report->description, FORMAT_HTML)); } if ($countrows === 1) { $returnrows = html_writer::tag('span', get_string('emailrow', 'report_customsql', $countrows)); } else { $returnrows = html_writer::tag('span', get_string('emailrows', 'report_customsql', $countrows)); } $url = new moodle_url('/report/customsql/view.php', array('id' => $report->id)); $link = get_string('emailink', 'report_customsql', html_writer::tag('a', $url, array('href' => $url))); $fullmessage .= html_writer::tag('p', $returnrows . ' ' . $link); // Construct message in html. $fullmessagehtml = null; if ($report->emailwhat === 'emailresults') { $fullmessagehtml = html_writer::table($table); } $fullmessagehtml .= $fullmessage; // Create the message object. $message = new stdClass(); $message->subject = $subject; $message->fullmessage = $fullmessage; $message->fullmessageformat = FORMAT_HTML; $message->fullmessagehtml = $fullmessagehtml; $message->smallmessage = null; return $message; }
/** * Prints a maintenance message from $CFG->maintenance_message or default if empty * @return void */ function print_maintenance_message() { global $CFG, $SITE, $PAGE, $OUTPUT; $PAGE->set_pagetype('maintenance-message'); $PAGE->set_pagelayout('maintenance'); $PAGE->set_title(strip_tags($SITE->fullname)); $PAGE->set_heading($SITE->fullname); echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('sitemaintenance', 'admin')); if (isset($CFG->maintenance_message) and !html_is_blank($CFG->maintenance_message)) { echo $OUTPUT->box_start('maintenance_message generalbox boxwidthwide boxaligncenter'); echo $CFG->maintenance_message; echo $OUTPUT->box_end(); } echo $OUTPUT->footer(); die; }
/** * Save the units and the answers associated with this question. */ public function save_question_options($question) { global $DB; $context = $question->context; // Get old versions of the objects $oldanswers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC'); $oldoptions = $DB->get_records('question_numerical', array('question' => $question->id), 'answer ASC'); // Save the units. $result = $this->save_units($question); if (isset($result->error)) { return $result; } else { $units = $result->units; } // Insert all the new answers foreach ($question->answer as $key => $answerdata) { // Check for, and ingore, completely blank answer from the form. if (trim($answerdata) == '' && $question->fraction[$key] == 0 && html_is_blank($question->feedback[$key]['text'])) { continue; } // Update an existing answer if possible. $answer = array_shift($oldanswers); if (!$answer) { $answer = new stdClass(); $answer->question = $question->id; $answer->answer = ''; $answer->feedback = ''; $answer->id = $DB->insert_record('question_answers', $answer); } if (trim($answerdata) === '*') { $answer->answer = '*'; } else { $answer->answer = $this->apply_unit($answerdata, $units, !empty($question->unitsleft)); if ($answer->answer === false) { $result->notice = get_string('invalidnumericanswer', 'quiz'); } } $answer->fraction = $question->fraction[$key]; $answer->feedback = $this->import_or_save_files($question->feedback[$key], $context, 'question', 'answerfeedback', $answer->id); $answer->feedbackformat = $question->feedback[$key]['format']; $DB->update_record('question_answers', $answer); // Set up the options object if (!$options = array_shift($oldoptions)) { $options = new stdClass(); } $options->question = $question->id; $options->answer = $answer->id; if (trim($question->tolerance[$key]) == '') { $options->tolerance = ''; } else { $options->tolerance = $this->apply_unit($question->tolerance[$key], $units, !empty($question->unitsleft)); if ($options->tolerance === false) { $result->notice = get_string('invalidnumerictolerance', 'quiz'); } } if (isset($options->id)) { $DB->update_record('question_numerical', $options); } else { $DB->insert_record('question_numerical', $options); } } // Delete any left over old answer records. $fs = get_file_storage(); foreach ($oldanswers as $oldanswer) { $fs->delete_area_files($context->id, 'question', 'answerfeedback', $oldanswer->id); $DB->delete_records('question_answers', array('id' => $oldanswer->id)); } foreach ($oldoptions as $oldoption) { $DB->delete_records('question_numerical', array('id' => $oldoption->id)); } $result = $this->save_unit_options($question); if (!empty($result->error) || !empty($result->notice)) { return $result; } $this->save_hints($question); return true; }