[system] / trunk / wwmoodle / wwquestion / edit_webwork_form.php Repository:
ViewVC logotype

View of /trunk/wwmoodle/wwquestion/edit_webwork_form.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5477 - (download) (as text) (annotate)
Sun Sep 9 02:53:16 2007 UTC (12 years, 5 months ago) by mleventi
File size: 9412 byte(s)
*** empty log message ***

    1 <?php
    2 /**
    3  * The editing form code for this question type.
    4  *
    5  * @copyright &copy; 2007 Matthew Leventi
    6  * @author mleventi@gmail.com
    7  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
    8  * @package webwork_qtype
    9  *//** */
   10 
   11 require_once("$CFG->dirroot/question/type/edit_question_form.php");
   12 require_once("$CFG->dirroot/question/type/webwork/locallib.php");
   13 require_once("$CFG->dirroot/question/type/webwork/questiontype.php");
   14 require_once("$CFG->dirroot/backup/lib.php");
   15 
   16 /**
   17  * webwork editing form definition.
   18  *
   19  * See http://docs.moodle.org/en/Development:lib/formslib.php for information
   20  * about the Moodle forms library, which is based on the HTML Quickform PEAR library.
   21  */
   22 class question_edit_webwork_form extends question_edit_form {
   23 
   24     function definition_inner(&$mform) {
   25 
   26         //CODE HEADER
   27         $mform->addElement('header', 'codeheader', get_string("edit_codeheader", 'qtype_webwork'));
   28         $mform->setHelpButton('codeheader',array('codeheader', get_string('edit_codeheader','qtype_webwork'),'webwork'));
   29 
   30         //CODE
   31         $mform->addElement('textarea', 'code', get_string('edit_code', 'qtype_webwork'),
   32                 array('rows' => 10,'cols' => 60));
   33         $mform->setType('code', PARAM_RAW);
   34         $mform->setHelpButton('code', array('code', get_string('edit_code', 'qtype_webwork'), 'webwork'));
   35 
   36         //FILES HEADER
   37         $mform->addElement('header', 'fileheader', get_string("edit_fileheader", 'qtype_webwork'));
   38         $mform->setHelpButton('fileheader',array('fileheader', get_string('edit_fileheader','qtype_webwork'),'webwork'));
   39 
   40         //FILES
   41         if(!isset($this->question->webworkid)) {
   42             $tempid = -1;
   43         } else {
   44             $tempid = $this->question->webworkid;
   45         }
   46         $src = webwork_get_filemanager_url($tempid);
   47         $html = "<iframe src='".$src."' width='100%' height='300px' style='border:0px;'></iframe>";
   48         $mform->addElement('html',$html);
   49 
   50         //OPTIONS HEADER
   51         $mform->addElement('header', 'optionheader', get_string("edit_optionheader","qtype_webwork"));
   52 
   53         //CODECHECK
   54         $codecheckoptions = array(
   55             0 => get_string('edit_codecheck0','qtype_webwork'),
   56             1 => get_string('edit_codecheck1','qtype_webwork'),
   57             2 => get_string('edit_codecheck2','qtype_webwork'));
   58         $mform->addElement('select','codecheck', get_string('edit_codecheck', 'qtype_webwork'),$codecheckoptions);
   59         $mform->setType('codecheck',PARAM_INT);
   60         $mform->setHelpButton('codecheck', array('codecheck', get_string('edit_codecheck', 'qtype_webwork'), 'webwork'));
   61         $mform->setDefault('codecheck',2);
   62 
   63         //SEED
   64         $mform->addElement('text', 'seed', get_string('edit_seed','qtype_webwork'),
   65             array('size' => 3));
   66         $mform->setType('seed', PARAM_INT);
   67         $mform->setHelpButton('seed', array('webwork', get_string('edit_seed', 'qtype_webwork'), 'webwork'));
   68         $mform->setDefault('seed', 0);
   69         $mform->addRule('seed', null, 'required', null, 'client');
   70 
   71         //TRIALS
   72         $mform->addElement('text', 'trials', get_string('edit_trials','qtype_webwork'),
   73             array('size' => 3));
   74         $mform->setType('trials', PARAM_INT);
   75         $mform->setHelpButton('trials', array('webwork', get_string('edit_trials', 'qtype_webwork'), 'webwork'));
   76         $mform->setDefault('trials', 10);
   77         $mform->addRule('trials', null, 'required', null, 'client');
   78     }
   79 
   80     function set_data($question) {
   81         parent::set_data($question);
   82     }
   83 
   84     function validation($data) {
   85         global $CFG;
   86         //check that the code is valid
   87         $err = $this->codecheck($data);
   88         if($err != false) {
   89             return $err;
   90         }
   91         return true;
   92     }
   93 
   94     function codecheck($data) {
   95 
   96         //codechecklevel
   97         $codechecklevel = $data['codecheck'];
   98         //here we construct a temp question object
   99         $question = new stdClass;
  100         $question->code = base64_encode(stripslashes($data['code']));
  101         $question->seed = $data['seed'];
  102         $question->trials = $data['trials'];
  103 
  104         //temporary solution
  105         if(!isset($this->question->webworkid)) {
  106             $path = webwork_get_tmp_path_full();
  107             $urlpath = webwork_get_filehandler_path() . '/' . webwork_get_tmp_path() . '/';
  108         } else {
  109             $path = webwork_get_wwquestion_path_full($this->question->webworkid);
  110             $urlpath = webwork_get_filehandler_path() . '/' . webwork_get_wwquestion_path($this->question->webworkid) . '/';
  111         }
  112         $filelist = list_directories_and_files($path);
  113         $filearray = array();
  114 
  115         foreach($filelist as $file) {
  116             if(!is_dir($path . '/' . $file)) {
  117                 $encode = base64_encode($urlpath . '/' . $file);
  118                 array_push($filearray,$encode);
  119             }
  120 
  121         }
  122         $question->files = $filearray;
  123 
  124         //one call to the server will return response for this code and keep it static in the function
  125         $results = webwork_get_derivations($question);
  126         //127.0.0.1
  127         //no code check
  128         if($codechecklevel == 0) {
  129             webwork_qtype::_derivations($results);
  130             return false;
  131         }
  132 
  133         //init error array
  134         $errorresults = array();
  135         $noerrorresults = array();
  136 
  137         //see if we got errors (split)
  138         foreach($results as $record) {
  139             if((isset($record['errors'])) && ($record['errors'] != '') && ($record['errors'] != null)) {
  140                 array_push($errorresults,$record);
  141             } else {
  142                 array_push($noerrorresults,$record);
  143             }
  144         }
  145 
  146         $goodresults = array();
  147         $warningresults = array();
  148 
  149         foreach($noerrorresults as $record) {
  150             if((isset($record['warnings'])) && ($record['warnings'] != '') && ($record['warnings'] != null)) {
  151                 array_push($warningresults,$record);
  152             } else {
  153                 array_push($goodresults,$record);
  154             }
  155 
  156         }
  157 
  158 
  159         //if there are good seeds we use those
  160         if((count($goodresults) > 0) && ($codechecklevel == 1)) {
  161             webwork_qtype::_derivations($goodresults);
  162             return false;
  163         }
  164 
  165         //if code check is strict
  166         if(count($goodresults) == count($results)) {
  167             webwork_qtype::_derivations($results);
  168             return false;
  169         }
  170 
  171         $errormsgs = array();
  172         $warningmsgs = array();
  173         //at this point we are going to be invalid
  174         //this correlates seeds with certain error messages for better output
  175         //ERRORS
  176         foreach($errorresults as $record) {
  177             $found = 0;
  178             $candidate = $record['errors'] . "<br>";
  179             $candidateseed = $record['seed'];
  180             for($i=0;$i<count($errormsgs);$i++) {
  181                 if($candidate == $errormsgs[$i]['errors']) {
  182                     $found = 1;
  183                     $errormsgs[$i]['seeds'][] = $candidateseed;
  184                 }
  185             }
  186             if($found == 0) {
  187                 //new error message
  188                 $msg = array();
  189                 $msg['errors'] = $candidate;
  190                 $msg['seeds'] = array();
  191                 $msg['seeds'][] = $candidateseed;
  192                 $errormsgs[] = $msg;
  193             }
  194         }
  195         //WARNINGS
  196         foreach($warningresults as $record) {
  197             $found = 0;
  198             $candidate = $record['warnings'] . "<br>";
  199             $candidateseed = $record['seed'];
  200             for($i=0;$i<count($warningmsgs);$i++) {
  201                 if($candidate == $warningmsgs[$i]['errors']) {
  202                     $found = 1;
  203                     $warningmsgs[$i]['seeds'][] = $candidateseed;
  204                 }
  205             }
  206             if($found == 0) {
  207                 //new error message
  208                 $msg = array();
  209                 $msg['warnings'] = $candidate;
  210                 $msg['seeds'] = array();
  211                 $msg['seeds'][] = $candidateseed;
  212                 $warningmsgs[] = $msg;
  213             }
  214 
  215         }
  216         $output = "Errors in PG Code on: " . count($errorresults) . " out of " . count($results) . " seeds tried:<br>";
  217         //construct error statement
  218         $counter = 1;
  219         foreach($errormsgs as $msg) {
  220             $output .= "$counter) ";
  221             $output .= "Seeds (";
  222             foreach ($msg['seeds'] as $seed) {
  223                 $output .= $seed . " ";
  224             }
  225             $output .= ") gave Errors:" . $msg['errors'] . "<br><br>";
  226             $counter++;
  227         }
  228         $output .= "Warnings in PG Code on: " . count($warningresults) . " out of " . count($results) . " seeds tried:<br>";
  229         $counter = 1;
  230         foreach($warningmsgs as $msg) {
  231             $output .= "$counter) ";
  232             $output .= "Seeds (";
  233             foreach ($msg['seeds'] as $seed) {
  234                 $output .= $seed . " ";
  235             }
  236             $output .= ") gave Warnings:" . $msg['warnings'] . "<br><br>";
  237             $counter++;
  238         }
  239         $returner =array();
  240         $returner['code'] = $output;
  241         return $returner;
  242     }
  243 
  244     function qtype() {
  245         return 'webwork';
  246     }
  247 }
  248 ?>

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9