public function test_match_grade_options()
 {
     $gradeoptions = question_bank::fraction_options_full();
     $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.3333333, 'error'));
     $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.333333, 'error'));
     $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33333, 'error'));
     $this->assertFalse(match_grade_options($gradeoptions, 0.3333, 'error'));
     $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.3333333, 'nearest'));
     $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.333333, 'nearest'));
     $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33333, 'nearest'));
     $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33, 'nearest'));
     $this->assertEquals(-0.1428571, match_grade_options($gradeoptions, -0.15, 'nearest'));
 }
Example #2
0
    /**
     * Process the file
     * This method should not normally be overidden
     * @param object $category
     * @return bool success
     */
    public function importprocess($category) {
        global $USER, $CFG, $DB, $OUTPUT;

        $context = $category->context;
        $this->importcontext = $context;

        // reset the timer in case file upload was slow
        set_time_limit(0);

        // STAGE 1: Parse the file
        echo $OUTPUT->notification(get_string('parsingquestions', 'question'), 'notifysuccess');

        if (! $lines = $this->readdata($this->filename)) {
            echo $OUTPUT->notification(get_string('cannotread', 'question'));
            return false;
        }

        if (!$questions = $this->readquestions($lines, $context)) {   // Extract all the questions
            echo $OUTPUT->notification(get_string('noquestionsinfile', 'question'));
            return false;
        }

        // STAGE 2: Write data to database
        echo $OUTPUT->notification(get_string('importingquestions', 'question',
                $this->count_questions($questions)), 'notifysuccess');

        // check for errors before we continue
        if ($this->stoponerror and ($this->importerrors>0)) {
            echo $OUTPUT->notification(get_string('importparseerror', 'question'));
            return true;
        }

        // get list of valid answer grades
        $gradeoptionsfull = question_bank::fraction_options_full();

        // check answer grades are valid
        // (now need to do this here because of 'stop on error': MDL-10689)
        $gradeerrors = 0;
        $goodquestions = array();
        foreach ($questions as $question) {
            if (!empty($question->fraction) and (is_array($question->fraction))) {
                $fractions = $question->fraction;
                $answersvalid = true; // in case they are!
                foreach ($fractions as $key => $fraction) {
                    $newfraction = match_grade_options($gradeoptionsfull, $fraction,
                            $this->matchgrades);
                    if ($newfraction === false) {
                        $answersvalid = false;
                    } else {
                        $fractions[$key] = $newfraction;
                    }
                }
                if (!$answersvalid) {
                    echo $OUTPUT->notification(get_string('invalidgrade', 'question'));
                    ++$gradeerrors;
                    continue;
                } else {
                    $question->fraction = $fractions;
                }
            }
            $goodquestions[] = $question;
        }
        $questions = $goodquestions;

        // check for errors before we continue
        if ($this->stoponerror && $gradeerrors > 0) {
            return false;
        }

        // count number of questions processed
        $count = 0;

        foreach ($questions as $question) {   // Process and store each question

            // reset the php timeout
            set_time_limit(0);

            // check for category modifiers
            if ($question->qtype == 'category') {
                if ($this->catfromfile) {
                    // find/create category object
                    $catpath = $question->category;
                    $newcategory = $this->create_category_path($catpath);
                    if (!empty($newcategory)) {
                        $this->category = $newcategory;
                    }
                }
                continue;
            }
            $question->context = $context;

            $count++;

            echo "<hr /><p><b>$count</b>. ".$this->format_question_text($question)."</p>";

            $question->category = $this->category->id;
            $question->stamp = make_unique_id_code();  // Set the unique code (not to be changed)

            $question->createdby = $USER->id;
            $question->timecreated = time();
            $question->modifiedby = $USER->id;
            $question->timemodified = time();

            $question->id = $DB->insert_record('question', $question);
            if (isset($question->questiontextfiles)) {
                foreach ($question->questiontextfiles as $file) {
                    question_bank::get_qtype($question->qtype)->import_file(
                            $context, 'question', 'questiontext', $question->id, $file);
                }
            }
            if (isset($question->generalfeedbackfiles)) {
                foreach ($question->generalfeedbackfiles as $file) {
                    question_bank::get_qtype($question->qtype)->import_file(
                            $context, 'question', 'generalfeedback', $question->id, $file);
                }
            }

            $this->questionids[] = $question->id;

            // Now to save all the answers and type-specific options

            $result = question_bank::get_qtype($question->qtype)->save_question_options($question);

            if (!empty($CFG->usetags) && isset($question->tags)) {
                require_once($CFG->dirroot . '/tag/lib.php');
                tag_set('question', $question->id, $question->tags);
            }

            if (!empty($result->error)) {
                echo $OUTPUT->notification($result->error);
                return false;
            }

            if (!empty($result->notice)) {
                echo $OUTPUT->notification($result->notice);
                return true;
            }

            // Give the question a unique version stamp determined by question_hash()
            $DB->set_field('question', 'version', question_hash($question),
                    array('id' => $question->id));
        }
        return true;
    }
Example #3
0
 /**
  * Process the file
  * This method should not normally be overidden
  * @return boolean success
  */
 function importprocess()
 {
     global $USER;
     // reset the timer in case file upload was slow
     @set_time_limit();
     // STAGE 1: Parse the file
     notify(get_string('parsingquestions', 'quiz'));
     if (!($lines = $this->readdata($this->filename))) {
         notify(get_string('cannotread', 'quiz'));
         return false;
     }
     if (!($questions = $this->readquestions($lines))) {
         // Extract all the questions
         notify(get_string('noquestionsinfile', 'quiz'));
         return false;
     }
     // STAGE 2: Write data to database
     notify(get_string('importingquestions', 'quiz', $this->count_questions($questions)));
     // check for errors before we continue
     if ($this->stoponerror and $this->importerrors > 0) {
         notify(get_string('importparseerror', 'quiz'));
         return true;
     }
     // get list of valid answer grades
     $grades = get_grade_options();
     $gradeoptionsfull = $grades->gradeoptionsfull;
     // check answer grades are valid
     // (now need to do this here because of 'stop on error': MDL-10689)
     $gradeerrors = 0;
     $goodquestions = array();
     foreach ($questions as $question) {
         if (!empty($question->fraction) and is_array($question->fraction)) {
             $fractions = $question->fraction;
             $answersvalid = true;
             // in case they are!
             foreach ($fractions as $key => $fraction) {
                 $newfraction = match_grade_options($gradeoptionsfull, $fraction, $this->matchgrades);
                 if ($newfraction === false) {
                     $answersvalid = false;
                 } else {
                     $fractions[$key] = $newfraction;
                 }
             }
             if (!$answersvalid) {
                 notify(get_string('matcherror', 'quiz'));
                 ++$gradeerrors;
                 continue;
             } else {
                 $question->fraction = $fractions;
             }
         }
         $goodquestions[] = $question;
     }
     $questions = $goodquestions;
     // check for errors before we continue
     if ($this->stoponerror and $gradeerrors > 0) {
         return false;
     }
     // count number of questions processed
     $count = 0;
     foreach ($questions as $question) {
         // Process and store each question
         // reset the php timeout
         @set_time_limit();
         // check for category modifiers
         if ($question->qtype == 'category') {
             if ($this->catfromfile) {
                 // find/create category object
                 $catpath = $question->category;
                 $newcategory = $this->create_category_path($catpath, '/');
                 if (!empty($newcategory)) {
                     $this->category = $newcategory;
                 }
             }
             continue;
         }
         $count++;
         echo "<hr /><p><b>{$count}</b>. " . $this->format_question_text($question) . "</p>";
         $question->category = $this->category->id;
         $question->stamp = make_unique_id_code();
         // Set the unique code (not to be changed)
         $question->createdby = $USER->id;
         $question->timecreated = time();
         if (!($question->id = insert_record("question", $question))) {
             error(get_string('cannotinsert', 'quiz'));
         }
         $this->questionids[] = $question->id;
         // Now to save all the answers and type-specific options
         global $QTYPES;
         $result = $QTYPES[$question->qtype]->save_question_options($question);
         if (!empty($result->error)) {
             notify($result->error);
             return false;
         }
         if (!empty($result->notice)) {
             notify($result->notice);
             return true;
         }
         // Give the question a unique version stamp determined by question_hash()
         set_field('question', 'version', question_hash($question), 'id', $question->id);
     }
     return true;
 }
Example #4
0
 /**
  * Process the file
  * This method should not normally be overidden
  * @param object $category
  * @return bool success
  */
 public function importprocess($category)
 {
     global $USER, $CFG, $DB, $OUTPUT;
     // Raise time and memory, as importing can be quite intensive.
     core_php_time_limit::raise();
     raise_memory_limit(MEMORY_EXTRA);
     // STAGE 1: Parse the file
     echo $OUTPUT->notification(get_string('parsingquestions', 'question'), 'notifysuccess');
     if (!($lines = $this->readdata($this->filename))) {
         echo $OUTPUT->notification(get_string('cannotread', 'question'));
         return false;
     }
     if (!($questions = $this->readquestions($lines))) {
         // Extract all the questions
         echo $OUTPUT->notification(get_string('noquestionsinfile', 'question'));
         return false;
     }
     // STAGE 2: Write data to database
     echo $OUTPUT->notification(get_string('importingquestions', 'question', $this->count_questions($questions)), 'notifysuccess');
     // check for errors before we continue
     if ($this->stoponerror and $this->importerrors > 0) {
         echo $OUTPUT->notification(get_string('importparseerror', 'question'));
         return true;
     }
     // get list of valid answer grades
     $gradeoptionsfull = question_bank::fraction_options_full();
     // check answer grades are valid
     // (now need to do this here because of 'stop on error': MDL-10689)
     $gradeerrors = 0;
     $goodquestions = array();
     foreach ($questions as $question) {
         if (!empty($question->fraction) and is_array($question->fraction)) {
             $fractions = $question->fraction;
             $invalidfractions = array();
             foreach ($fractions as $key => $fraction) {
                 $newfraction = match_grade_options($gradeoptionsfull, $fraction, $this->matchgrades);
                 if ($newfraction === false) {
                     $invalidfractions[] = $fraction;
                 } else {
                     $fractions[$key] = $newfraction;
                 }
             }
             if ($invalidfractions) {
                 echo $OUTPUT->notification(get_string('invalidgrade', 'question', implode(', ', $invalidfractions)));
                 ++$gradeerrors;
                 continue;
             } else {
                 $question->fraction = $fractions;
             }
         }
         $goodquestions[] = $question;
     }
     $questions = $goodquestions;
     // check for errors before we continue
     if ($this->stoponerror && $gradeerrors > 0) {
         return false;
     }
     // count number of questions processed
     $count = 0;
     foreach ($questions as $question) {
         // Process and store each question
         $transaction = $DB->start_delegated_transaction();
         // reset the php timeout
         core_php_time_limit::raise();
         // check for category modifiers
         if ($question->qtype == 'category') {
             if ($this->catfromfile) {
                 // find/create category object
                 $catpath = $question->category;
                 $newcategory = $this->create_category_path($catpath);
                 if (!empty($newcategory)) {
                     $this->category = $newcategory;
                 }
             }
             $transaction->allow_commit();
             continue;
         }
         $question->context = $this->importcontext;
         $count++;
         echo "<hr /><p><b>{$count}</b>. " . $this->format_question_text($question) . "</p>";
         $question->category = $this->category->id;
         $question->stamp = make_unique_id_code();
         // Set the unique code (not to be changed)
         $question->createdby = $USER->id;
         $question->timecreated = time();
         $question->modifiedby = $USER->id;
         $question->timemodified = time();
         $fileoptions = array('subdirs' => true, 'maxfiles' => -1, 'maxbytes' => 0);
         $question->id = $DB->insert_record('question', $question);
         if (isset($question->questiontextitemid)) {
             $question->questiontext = file_save_draft_area_files($question->questiontextitemid, $this->importcontext->id, 'question', 'questiontext', $question->id, $fileoptions, $question->questiontext);
         } else {
             if (isset($question->questiontextfiles)) {
                 foreach ($question->questiontextfiles as $file) {
                     question_bank::get_qtype($question->qtype)->import_file($this->importcontext, 'question', 'questiontext', $question->id, $file);
                 }
             }
         }
         if (isset($question->generalfeedbackitemid)) {
             $question->generalfeedback = file_save_draft_area_files($question->generalfeedbackitemid, $this->importcontext->id, 'question', 'generalfeedback', $question->id, $fileoptions, $question->generalfeedback);
         } else {
             if (isset($question->generalfeedbackfiles)) {
                 foreach ($question->generalfeedbackfiles as $file) {
                     question_bank::get_qtype($question->qtype)->import_file($this->importcontext, 'question', 'generalfeedback', $question->id, $file);
                 }
             }
         }
         $DB->update_record('question', $question);
         $this->questionids[] = $question->id;
         // Now to save all the answers and type-specific options
         $result = question_bank::get_qtype($question->qtype)->save_question_options($question);
         if (!empty($CFG->usetags) && isset($question->tags)) {
             require_once $CFG->dirroot . '/tag/lib.php';
             tag_set('question', $question->id, $question->tags, 'core_question', $question->context->id);
         }
         if (!empty($result->error)) {
             echo $OUTPUT->notification($result->error);
             // Can't use $transaction->rollback(); since it requires an exception,
             // and I don't want to rewrite this code to change the error handling now.
             $DB->force_transaction_rollback();
             return false;
         }
         $transaction->allow_commit();
         if (!empty($result->notice)) {
             echo $OUTPUT->notification($result->notice);
             return true;
         }
         // Give the question a unique version stamp determined by question_hash()
         $DB->set_field('question', 'version', question_hash($question), array('id' => $question->id));
     }
     return true;
 }