Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

model_explore.php

Go to the documentation of this file.
00001 
00002 /** @file model_explore.php
00003 * Definition of class model_explore.
00004 */
00005 #include "model_abstract.php"
00006 #include "entities_handler.php"
00007 #include "stdlib.php"
00008 #include "xml_structure_builder.php"
00009 /**
00010 * Process explore request to learn course.
00011 */
00012 class model_explore : public  model_abstract
00013 {
00014 public:
00015   var structure = array();
00016   /** Name of model. */
00017   var name = '';
00018   /** Brief information about model. */
00019   var info = '';
00020   /** Entities handler. */
00021   var entities_handler = 0;
00022   /** Owner of instance. 
00023   * Used for callback to enviroment class.
00024   */
00025   var owner;
00026   var temp_task_number = 0;
00027   var temp_answer_number = 0;
00028   var temp_r_num_t_id = 0;
00029   var iname = '';
00030   function import( fname )
00031   {
00032 public:
00033     if( !is_file( fname ) )
00034       return this->err( 'file does not exist: '.fname, 'import', __LINE__, __FILE__ );
00035     this->iname = fname;
00036     xsb = new xml_structure_builder;
00037     this->structure = xsb->parse_file( fname );
00038     if(
00039       !isset( this->structure[ 'element_name' ] ) ||
00040       !this->structure[ 'element_name' ] == 'model' ||
00041       !isset( this->structure[ 'content' ] ) ||
00042       !is_array( this->structure[ 'content' ] ) ||
00043       !sizeof( this->structure[ 'content' ] ) > 0 )
00044       return;
00045     foreach( this->structure[ 'content' ] as val )
00046     {
00047       switch( val[ 'element_name' ] )
00048       {
00049         case 'name':
00050           this->name = val[ 'attributes' ][ 'the' ];
00051           break;
00052         case 'info':
00053           if( 
00054             isset( val[ 'content' ] ) &&
00055             isset( val[ 'content' ][ '0' ] ) &&
00056             isset( val[ 'content' ][ '0' ][ 'attributes' ] ) &&
00057             isset( val[ 'content' ][ '0' ][ 'attributes' ][ 'text' ] ) )
00058             this->info = val[ 'content' ][ '0' ][ 'attributes' ][ 'text' ];
00059           break;
00060         case 'theory':
00061           this->theory = val;
00062           break;
00063         case 'test':
00064           this->test = val;
00065           break;
00066       }
00067     }
00068   }
00069   /**
00070   * Return location.
00071   * @param string name - name of location to return.
00072   * @todo check for is_a
00073   */
00074   function location( name )
00075   {
00076       return this->owner->location( name );
00077   }
00078   /** Correct locations in text.
00079   * @param string data - text to correct
00080   * @return all location will become relative to model`s dir
00081   * @bug relative/absolute paths in templates: xxx://yyy links are coverted to
00082   * <templates>/xxx://yyy
00083   * @todo do something with paths, by now xxx://yyy links are coverted too
00084   */
00085   function correct_locs( data )
00086   {
00087     template_dir = dirname( this->iname );
00088     res = preg_replace(
00089       '/(href|action|src|background)=[\'"]\/(\S*)[\'"]/',
00090       '1=\'2\'',
00091       preg_replace(
00092         '/(href|action|src|background)=[\'"]([^\/]\S*)[\'"]/',
00093         '1=\''.template_dir.'/2\'',
00094         data ) );
00095     return res;
00096   }
00097   /**
00098   * Register entities.
00099   * @param array entities - entities to register.
00100   */
00101   function set_entities( entities )
00102   {
00103     if( !this->entities_handler )
00104     {
00105       this->entities_handler = new entities_handler;
00106     }
00107     this->entities_handler->clear();
00108     foreach( entities as entv => entk )
00109     {
00110       this->entities_handler->set( entv, entk );
00111     }
00112   }
00113   /**
00114   * Process template.
00115   * Process HTML template converting entities.
00116   * @param string filename - template file name
00117   * @param array entities - entities to replace
00118   * @return string contains processed template
00119   * @see set_entities(), entities_handler
00120   */
00121   function process_template( filename, entities )
00122   {
00123     if( !is_file( filename ) )
00124       return this->err( 'Nonexisted file name is given: '.filename, 'process_template', __LINE__, __FILE__ );
00125     this->set_entities( entities );
00126     //print_structure( entities );
00127     res = this->entities_handler->process_file( filename, true, '/%\S*%/' );
00128     return res;
00129   }
00130   /** Return HTML representation of course theory. */
00131   function get_theory_representation()
00132   {
00133     return this->process_element( this->theory );
00134   }
00135   /** Return HTML representation of course test. */
00136   function get_test_representation( data )
00137   {
00138     res = this->process_element( this->test );
00139     //!debug
00140     //fh = fopen( '1.html', 'w' );
00141     //fwrite( fh, res );
00142     //fclose( fh );
00143     return res;
00144   }
00145   /** Return HTML representation of results of test. */
00146   function get_test_results( post_data )
00147   {
00148     p_tasks = ( isset( post_data[ 'test' ] ) ) ?
00149       post_data[ 'test' ] : array();
00150     was_tested = ( isset( _SESSION[ 'was_tested' ] ) ) ?
00151       _SESSION[ 'was_tested' ] : 0;
00152     tested_tasks = ( isset( _SESSION[ 'tested_tasks' ] ) ) ?
00153       _SESSION[ 'tested_tasks' ] : array();
00154     if( isset( _SESSION[ 'was_tested' ] ) )
00155       unset( _SESSION[ 'was_tested' ] );
00156     if( isset( _SESSION[ 'tested_tasks' ] ) )
00157       unset( _SESSION[ 'tested_tasks' ] );
00158     if( !was_tested )
00159     {
00160       //!debug
00161       //echo "was not tested<br>";
00162       return '';
00163     }
00164     //Check integrity of answers
00165     ok = ( sizeof( tested_tasks ) == sizeof( p_tasks ) ) ? 1 : 0;
00166     if( !ok )
00167     {
00168       //!debug
00169       //echo "sizes differs<br>";
00170       //print_r( p_tasks );
00171       //echo "<br>";
00172       //print_r( _POST );
00173       //echo "<br>";
00174       return;
00175     }
00176     if( ok )
00177       foreach( tested_tasks as id => task )
00178         if( !isset( p_tasks[ id ] ) )
00179         {
00180           ok = 0;
00181           break;
00182         }
00183     if( !ok )
00184     {
00185       //!debug
00186       //echo "is not ok<br>";
00187       return;
00188     }
00189     entities = array();
00190     entities[ '%total_t%' ] = sizeof( p_tasks );
00191     r_a = 0;
00192     foreach( tested_tasks as id => task )
00193     {
00194       //!debug
00195       //print_r( this->test[ 'tasks' ][ id ] );
00196       //echo "*********<br>";
00197       task_c = this->test[ 'content' ][ id ][ 'content' ];
00198       switch( this->test[ 'content' ][ id ][ 'element_name' ] )
00199       {
00200         case 'task_single':
00201           if( isset( task_c[ p_tasks[ id ] ][ 'attributes' ][ 'is_right' ] ) )
00202             if( task_c[ p_tasks[ id ] ][ 'attributes' ][ 'is_right' ] == 'yes' )
00203               r_a++;
00204           break;
00205         case 'task_multi':
00206           r_a++;
00207           foreach( task_c as aid => ans )
00208           {
00209             if( ans[ 'element_name' ] != 'manswer' )
00210               continue;
00211             if( 
00212               ( !isset( ans[ 'attributes' ][ 'is_right' ] ) || 
00213                 ans[ 'attributes' ][ 'is_right' ] == 'no' ) )
00214               //Right answer is 'no'
00215               if(
00216                 isset( p_tasks[ id ][ aid ] ) &&
00217                 p_tasks[ id ][ aid ] == 'yes' )
00218                 //User answered 'yes'
00219               {
00220                 r_a--;
00221                 break;
00222               }else;
00223             else //Right answer is 'yes'
00224               if(
00225                 !isset( p_tasks[ id ][ aid ] ) ||
00226                 p_tasks[ id ][ aid ] != 'yes' )
00227                 //User answered 'no'
00228               {
00229                 r_a--;
00230                 break;
00231               }else;
00232           }
00233           break;
00234         default:
00235           //!debug
00236           echo "<hr>Unknown task type<br/>";
00237           print_structure( this->test[ 'content' ][ id ] );
00238       }
00239     }
00240     entities[ '%right_answers%' ] = r_a;
00241     return this->process_template(
00242       this->location( 'ent_results' ),
00243       entities );
00244   }
00245   function gen_attr_str( attrs )
00246   {
00247     s = '';
00248     foreach( attrs as keyv => val )
00249     {
00250       switch( keyv )
00251       {
00252         case 'src':
00253           val = location_to_filename( val, dirname( this->iname ) );
00254         default:
00255           s .= " keyv=\"val\"";
00256       }
00257     }
00258     return s;
00259   }
00260   /** Return HTML representation of element according to its template.
00261   * @param array element - element to process.
00262   */
00263   function process_element( element, e_i = array() )
00264   {
00265     if( !is_array( element ) )
00266       return '';
00267     entities = e_i;
00268     switch( element[ 'element_name' ] )
00269     {
00270       case 'CDATA':
00271         entities[ '%content%' ] = element[ 'attributes' ][ 'text' ];
00272         break;
00273       case 'definition':
00274       case 'img':
00275       case 'theory':
00276       case 'proof':
00277       case 'theory_block':
00278       case 'sample':
00279       case 'question':
00280       case 'object':
00281       case 'object_definition':
00282       case 'assertion':
00283       case 'br':
00284       case 'code':
00285       case 'pre':
00286       case 'li':
00287       case 'ol':
00288       case 'ul':
00289       case 'table':
00290       case 'th':
00291       case 'tr':
00292       case 'td':
00293       case 'par':
00294         content = '';
00295         if( isset( element[ 'content' ] ) )
00296           foreach( element[ 'content' ] as c_e )
00297             content .= this->process_element( c_e );
00298         entities[ '%content%' ] = content;
00299         entities[ '%attributes%' ] = this->gen_attr_str( element[ 'attributes' ] );
00300         break;
00301       case 'theorem':
00302         entities[ '%title%' ] = element[ 'attributes' ][ 'title' ];
00303         content = '';
00304         if( isset( element[ 'content' ] ) )
00305           foreach( element[ 'content' ] as c_e )
00306             content .= this->process_element( c_e, entities );
00307         entities[ '%content%' ] = content;
00308         entities[ '%attributes%' ] = this->gen_attr_str( element[ 'attributes' ] );
00309         break;
00310       case 'test':
00311         list( usec, sec ) = explode( ' ', microtime() );
00312         srand( (float) sec + ( (float) usec * 100000 ) );
00313         this->temp_task_number = 0;
00314         entities[ '%task_count%' ] = element[ 'attributes' ][ 'task_count' ];
00315         tsk_blk = '';
00316         prev_tasks = array();
00317         if( element[ 'attributes' ][ 'task_count' ] >= sizeof( element[ 'content' ] ) - 1 )
00318           //Show all tasks
00319           foreach( element[ 'content' ] as value )
00320           {
00321             this->temp_r_num_t_id = this->temp_task_number;
00322             prev_tasks[ this->temp_task_number ] = 1;
00323             this->temp_task_number++;
00324             tsk_blk .= this->process_element( value, entities );
00325           }
00326         else
00327         //Show task_count random tasks
00328         {
00329           prev_tasks_count = 0;
00330           while( prev_tasks_count < element[ 'attributes' ][ 'task_count' ] )
00331           {
00332             do
00333             {
00334               r_num = rand( 0, sizeof( element[ 'content' ] ) - 1 - 1 );
00335             }
00336             while( isset( prev_tasks[ r_num ] ) );
00337             prev_tasks_count++;
00338             prev_tasks[ r_num ] = 1;
00339             this->temp_task_number = prev_tasks_count;
00340             this->temp_r_num_t_id = r_num;
00341             tsk_blk .= 
00342               this->process_element( element[ 'content' ][ r_num ], entities );
00343           }
00344         }
00345         _SESSION[ 'tested_tasks' ] = prev_tasks;
00346         _SESSION[ 'was_tested' ] = 1;
00347         entities[ '%content%' ] = tsk_blk;
00348         break;
00349       case 'task_single':
00350       case 'task_multi':
00351         entities[ '%task_number%' ] = this->temp_task_number;
00352         entities[ '%task_id%' ] = this->temp_r_num_t_id;
00353         content = '';
00354         foreach( element[ 'content' ] as answer_number => value )
00355         {
00356           this->temp_answer_number = answer_number;
00357           content .= this->process_element( value, entities );
00358         }
00359         entities[ '%content%' ] = content;
00360         break;
00361       case 'sanswer':
00362       case 'manswer':
00363         entities[ '%answer_number%' ] = this->temp_answer_number;
00364         content = '';
00365         if( isset( element[ 'content' ] ) )
00366           foreach( element[ 'content' ] as c_e )
00367             content .= this->process_element( c_e, entities );
00368         entities[ '%content%' ] = content;
00369         break;
00370       default:
00371         echo "<hr>Unknown element<br>";
00372         print_structure( element );
00373         return '';
00374     }
00375     return this->process_template(
00376       this->location( 'ent_'.element[ 'element_name' ] ),
00377       entities );
00378   }
00379   function &produce( data )
00380   {
00381     return this;
00382   }
00383 }
00384 
00385 X-Powered-By: PHP/4.0.8-dev
00386 Content-type: text/html
00387 
00388 

Generated on Tue Mar 11 08:35:13 2003 for SES by doxygen1.2.16