Demo code to parse the Google SoC 2008 applications list available to mentors

  1. <?php
  2. /**
  3.  * Parse the Google SoC 2008 available to mentors at
  4.  * http://code.google.com/soc/2008/drupal/open.html
  5.  * in order to include it in a Drupal app
  6.  *
  7.  * @author FG Marand http://drupal.org/user/27985
  8.  * @license GPL2
  9.  *
  10.  * Usage: log to the SoC2008 site as a mentor, save the page to <your site>/files/soc.html,
  11.  * and go to <your site>/testcode
  12.  *
  13.  * WARNING: this is just demo code
  14.  */
  15.  
  16.  
  17. function testcode_menu($may_cache)
  18.   {
  19.   $items = array();
  20.   if ($may_cache)
  21.     {
  22.     $items[] = array
  23.       (
  24.       'title'    => 'Code de test',
  25.       'path'     => 'testcode',
  26.       'access'   => user_access('administer nodes'),
  27.       'callback' => 'testcode_test',
  28.       );
  29.     }
  30.  
  31.   return $items;
  32.   }
  33.  
  34. function testcode_test()
  35.   {
  36.   $ret = '';
  37.  
  38.   $unsafeFile = file_get_contents('files/soc.html');
  39.   @$htmlDom = DOMDocument::loadHTML($unsafeFile);
  40.   $xml = simplexml_import_dom($htmlDom);
  41.  
  42.   $arApps = array();
  43.  
  44.   $appList = $xml->xpath('//table[@class="applist"]/tr');
  45.   array_shift($appList);
  46.   foreach($appList as $row)
  47.     {
  48.     /**
  49.      * Note: trim() silently casts the SimpleXML elements to strings
  50.      */
  51.  
  52.     $class = trim($row['class']) == 'listrequestapp' ? 'Request' : '&nbsp;';
  53.  
  54.     $link = $row->td[0]->table->tr[0]->td->a;
  55.     $l  = l(trim($link[0]), $link['href']);
  56.  
  57.     $ts = $row->td[0]->table->tr[1]->td;
  58.  
  59.     $student = trim($row->td[1]);
  60.     $mentor  = trim(strip_tags($row->td[2]->asXml()));
  61.     $score   = trim($row->td[3]);
  62.  
  63.     $arApps[] = array
  64.       (
  65.       'class'   => $class,
  66.       'link'    => $l,
  67.       'student' => $student,
  68.       'mentor'  => $mentor,
  69.       'score'   => $score,
  70.       );
  71.     }
  72.  
  73.   /**
  74.    * Now we can format the table as wished
  75.    */
  76.   $header = array(t('Status'), t('Project'), t('Student'), t('Mentor'), t('Score'));
  77.  
  78.   $ret = theme('table', $header, $arApps);
  79.   return $ret;
  80.   }