示例#1
0
文件: format.php 项目: nottmoo/moodle
    function importprocess($filename, $lesson, $pageid) {
        global $DB, $OUTPUT;

    /// Processes a given file.  There's probably little need to change this
        $timenow = time();

        if (! $lines = $this->readdata($filename)) {
            echo $OUTPUT->notification("File could not be read, or was empty");
            return false;
        }

        if (! $questions = $this->readquestions($lines)) {   // Extract all the questions
            echo $OUTPUT->notification("There are no questions in this file!");
            return false;
        }

        echo $OUTPUT->notification(get_string('importcount', 'lesson', sizeof($questions)));

        $count = 0;

        $unsupportedquestions = 0;

        foreach ($questions as $question) {   // Process and store each question
            switch ($question->qtype) {
                // the good ones
                case SHORTANSWER :
                case NUMERICAL :
                case TRUEFALSE :
                case MULTICHOICE :
                case MATCH :
                    $count++;

                    echo "<hr><p><b>$count</b>. ".$question->questiontext."</p>";
                    $newpage = new stdClass;
                    $newpage->lessonid = $lesson->id;
                    $newpage->qtype = $this->qtypeconvert[$question->qtype];
                    switch ($question->qtype) {
                        case SHORTANSWER :
                            if (isset($question->usecase)) {
                                $newpage->qoption = $question->usecase;
                            }
                            break;
                        case MULTICHOICE :
                            if (isset($question->single)) {
                                $newpage->qoption = !$question->single;
                            }
                            break;
                    }
                    $newpage->timecreated = $timenow;
                    if ($question->name != $question->questiontext) {
                        $newpage->title = $question->name;
                    } else {
                        $newpage->title = "Page $count";
                    }
                    $newpage->contents = $question->questiontext;

                    // set up page links
                    if ($pageid) {
                        // the new page follows on from this page
                        if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
                            print_error('invalidpageid', 'lesson');
                        }
                        $newpage->prevpageid = $pageid;
                        $newpage->nextpageid = $page->nextpageid;
                        // insert the page and reset $pageid
                        $newpageid = $DB->insert_record("lesson_pages", $newpage);
                        // update the linked list
                        $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));

                    } else {
                        // new page is the first page
                        // get the existing (first) page (if any)
                        $params = array ("lessonid" => $lesson->id, "prevpageid" => 0);
                        if (!$page = $DB->get_record_select("lesson_pages", "lessonid = :lessonid AND prevpageid = :prevpageid", $params)) {
                            // there are no existing pages
                            $newpage->prevpageid = 0; // this is a first page
                            $newpage->nextpageid = 0; // this is the only page
                            $newpageid = $DB->insert_record("lesson_pages", $newpage);
                        } else {
                            // there are existing pages put this at the start
                            $newpage->prevpageid = 0; // this is a first page
                            $newpage->nextpageid = $page->id;
                            $newpageid = $DB->insert_record("lesson_pages", $newpage);
                            // update the linked list
                            $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->id));
                        }
                    }
                    // reset $pageid and put the page ID in $question, used in save_question_option()
                    $pageid = $newpageid;
                    $question->id = $newpageid;

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

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

                    $question->lessonid = $lesson->id; // needed for foreign key
                    $question->qtype = $this->qtypeconvert[$question->qtype];
                    $result = lesson_save_question_options($question, $lesson);

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

                    if (!empty($result->notice)) {
                        echo $OUTPUT->notification($result->notice);
                        return true;
                    }
                    break;
            // the Bad ones
                default :
                    $unsupportedquestions++;
                    break;
            }

        }
        if ($unsupportedquestions) {
            echo $OUTPUT->notification(get_string('unknownqtypesnotimported', 'lesson', $unsupportedquestions));
        }
        return true;
    }
示例#2
0
 function importprocess($filename, $lesson, $pageid)
 {
     /// Processes a given file.  There's probably little need to change this
     $timenow = time();
     if (!($lines = $this->readdata($filename))) {
         notify("File could not be read, or was empty");
         return false;
     }
     if (!($questions = $this->readquestions($lines))) {
         // Extract all the questions
         notify("There are no questions in this file!");
         return false;
     }
     notify(get_string('importcount', 'lesson', sizeof($questions)));
     $count = 0;
     foreach ($questions as $question) {
         // Process and store each question
         switch ($question->qtype) {
             // the good ones
             case SHORTANSWER:
             case NUMERICAL:
             case TRUEFALSE:
             case MULTICHOICE:
             case MATCH:
                 $count++;
                 echo "<hr><p><b>{$count}</b>. " . stripslashes($question->questiontext) . "</p>";
                 $newpage = new stdClass();
                 $newpage->lessonid = $lesson->id;
                 $newpage->qtype = $this->qtypeconvert[$question->qtype];
                 switch ($question->qtype) {
                     case SHORTANSWER:
                         if (isset($question->usecase)) {
                             $newpage->qoption = $question->usecase;
                         }
                         break;
                     case MULTICHOICE:
                         if (isset($question->single)) {
                             $newpage->qoption = !$question->single;
                         }
                         break;
                 }
                 $newpage->timecreated = $timenow;
                 if ($question->name != $question->questiontext) {
                     $newpage->title = $question->name;
                 } else {
                     $newpage->title = "Page {$count}";
                 }
                 $newpage->contents = $question->questiontext;
                 // set up page links
                 if ($pageid) {
                     // the new page follows on from this page
                     if (!($page = get_record("lesson_pages", "id", $pageid))) {
                         error("Format: Page {$pageid} not found");
                     }
                     $newpage->prevpageid = $pageid;
                     $newpage->nextpageid = $page->nextpageid;
                     // insert the page and reset $pageid
                     if (!($newpageid = insert_record("lesson_pages", $newpage))) {
                         error("Format: Could not insert new page!");
                     }
                     // update the linked list
                     if (!set_field("lesson_pages", "nextpageid", $newpageid, "id", $pageid)) {
                         error("Format: unable to update link");
                     }
                 } else {
                     // new page is the first page
                     // get the existing (first) page (if any)
                     if (!($page = get_record_select("lesson_pages", "lessonid = {$lesson->id} AND prevpageid = 0"))) {
                         // there are no existing pages
                         $newpage->prevpageid = 0;
                         // this is a first page
                         $newpage->nextpageid = 0;
                         // this is the only page
                         $newpageid = insert_record("lesson_pages", $newpage);
                         if (!$newpageid) {
                             error("Insert page: new first page not inserted");
                         }
                     } else {
                         // there are existing pages put this at the start
                         $newpage->prevpageid = 0;
                         // this is a first page
                         $newpage->nextpageid = $page->id;
                         $newpageid = insert_record("lesson_pages", $newpage);
                         if (!$newpageid) {
                             error("Insert page: first page not inserted");
                         }
                         // update the linked list
                         if (!set_field("lesson_pages", "prevpageid", $newpageid, "id", $page->id)) {
                             error("Insert page: unable to update link");
                         }
                     }
                 }
                 // reset $pageid and put the page ID in $question, used in save_question_option()
                 $pageid = $newpageid;
                 $question->id = $newpageid;
                 $this->questionids[] = $question->id;
                 // Now to save all the answers and type-specific options
                 $question->lessonid = $lesson->id;
                 // needed for foreign key
                 $question->qtype = $this->qtypeconvert[$question->qtype];
                 $result = lesson_save_question_options($question);
                 if (!empty($result->error)) {
                     notify($result->error);
                     return false;
                 }
                 if (!empty($result->notice)) {
                     notify($result->notice);
                     return true;
                 }
                 break;
                 // the Bad ones
             // the Bad ones
             default:
                 notify(get_string('unsupportedqtype', 'lesson', $question->qtype));
         }
     }
     return true;
 }
示例#3
0
 function importprocess($filename, $lesson, $pageid)
 {
     global $DB, $OUTPUT;
     /// Processes a given file.  There's probably little need to change this
     $timenow = time();
     if (!($lines = $this->readdata($filename))) {
         echo $OUTPUT->notification("File could not be read, or was empty");
         return false;
     }
     if (!($questions = $this->readquestions($lines))) {
         // Extract all the questions
         echo $OUTPUT->notification("There are no questions in this file!");
         return false;
     }
     //Avoid category as question type
     echo $OUTPUT->notification(get_string('importcount', 'lesson', $this->count_questions($questions)), 'notifysuccess');
     $count = 0;
     $addquestionontop = false;
     if ($pageid == 0) {
         $addquestionontop = true;
         $updatelessonpage = $DB->get_record('lesson_pages', array('lessonid' => $lesson->id, 'prevpageid' => 0));
     } else {
         $updatelessonpage = $DB->get_record('lesson_pages', array('lessonid' => $lesson->id, 'id' => $pageid));
     }
     $unsupportedquestions = 0;
     foreach ($questions as $question) {
         // Process and store each question
         switch ($question->qtype) {
             //TODO: Bad way to bypass category in data... Quickfix for MDL-27964
             case 'category':
                 break;
                 // the good ones
             // the good ones
             case 'shortanswer':
             case 'numerical':
             case 'truefalse':
             case 'multichoice':
             case 'match':
                 $count++;
                 //Show nice formated question in one line.
                 echo "<hr><p><b>{$count}</b>. " . $this->format_question_text($question) . "</p>";
                 $newpage = new stdClass();
                 $newpage->lessonid = $lesson->id;
                 $newpage->qtype = $this->qtypeconvert[$question->qtype];
                 switch ($question->qtype) {
                     case 'shortanswer':
                         if (isset($question->usecase)) {
                             $newpage->qoption = $question->usecase;
                         }
                         break;
                     case 'multichoice':
                         if (isset($question->single)) {
                             $newpage->qoption = !$question->single;
                         }
                         break;
                 }
                 $newpage->timecreated = $timenow;
                 if ($question->name != $question->questiontext) {
                     $newpage->title = $question->name;
                 } else {
                     $newpage->title = "Page {$count}";
                 }
                 $newpage->contents = $question->questiontext;
                 $newpage->contentsformat = isset($question->questionformat) ? $question->questionformat : FORMAT_HTML;
                 // set up page links
                 if ($pageid) {
                     // the new page follows on from this page
                     if (!($page = $DB->get_record("lesson_pages", array("id" => $pageid)))) {
                         print_error('invalidpageid', 'lesson');
                     }
                     $newpage->prevpageid = $pageid;
                     $newpage->nextpageid = $page->nextpageid;
                     // insert the page and reset $pageid
                     $newpageid = $DB->insert_record("lesson_pages", $newpage);
                     // update the linked list
                     $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
                 } else {
                     // new page is the first page
                     // get the existing (first) page (if any)
                     $params = array("lessonid" => $lesson->id, "prevpageid" => 0);
                     if (!($page = $DB->get_record_select("lesson_pages", "lessonid = :lessonid AND prevpageid = :prevpageid", $params))) {
                         // there are no existing pages
                         $newpage->prevpageid = 0;
                         // this is a first page
                         $newpage->nextpageid = 0;
                         // this is the only page
                         $newpageid = $DB->insert_record("lesson_pages", $newpage);
                     } else {
                         // there are existing pages put this at the start
                         $newpage->prevpageid = 0;
                         // this is a first page
                         $newpage->nextpageid = $page->id;
                         $newpageid = $DB->insert_record("lesson_pages", $newpage);
                         // update the linked list
                         $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->id));
                     }
                 }
                 // reset $pageid and put the page ID in $question, used in save_question_option()
                 $pageid = $newpageid;
                 $question->id = $newpageid;
                 $this->questionids[] = $question->id;
                 // Import images in question text.
                 if (isset($question->questiontextitemid)) {
                     $questiontext = file_save_draft_area_files($question->questiontextitemid, $this->importcontext->id, 'mod_lesson', 'page_contents', $newpageid, null, $question->questiontext);
                     // Update content with recoded urls.
                     $DB->set_field("lesson_pages", "contents", $questiontext, array("id" => $newpageid));
                 }
                 // Now to save all the answers and type-specific options
                 $question->lessonid = $lesson->id;
                 // needed for foreign key
                 $question->qtype = $this->qtypeconvert[$question->qtype];
                 $result = lesson_save_question_options($question, $lesson);
                 if (!empty($result->error)) {
                     echo $OUTPUT->notification($result->error);
                     return false;
                 }
                 if (!empty($result->notice)) {
                     echo $OUTPUT->notification($result->notice);
                     return true;
                 }
                 break;
                 // the Bad ones
             // the Bad ones
             default:
                 $unsupportedquestions++;
                 break;
         }
     }
     // Update the prev links if there were existing pages.
     if (!empty($updatelessonpage)) {
         if ($addquestionontop) {
             $DB->set_field("lesson_pages", "prevpageid", $pageid, array("id" => $updatelessonpage->id));
         } else {
             $DB->set_field("lesson_pages", "prevpageid", $pageid, array("id" => $updatelessonpage->nextpageid));
         }
     }
     if ($unsupportedquestions) {
         echo $OUTPUT->notification(get_string('unknownqtypesnotimported', 'lesson', $unsupportedquestions));
     }
     return true;
 }