Index: admin/report/census/lang/en_utf8/report_census.php
===================================================================
--- admin/report/census/lang/en_utf8/report_census.php	(.../vendor/moodle/1.9weekly)	(revision 0)
+++ admin/report/census/lang/en_utf8/report_census.php	(.../branches/1.9.5-LAE1.0/Census)	(revision 139)
@@ -0,0 +1,11 @@
+<?php
+$string['census'] = 'Census';
+$string['curlerror'] = 'CURL not found. You must have CURL installed to use remote reporting.';
+$string['dbupdated'] = 'Database successfully upgraded';
+$string['error_noroles'] = 'You must define both teacher and student roles';
+$string['form_phonehome'] = 'Allow anonymous reporting of data';
+$string['form_remotesystem'] = 'Remote reporting server';
+$string['form_roleassignment'] = 'Role assignment';
+$string['reportsettings'] = 'Census report settings';
+$string['runcensus'] = 'Run census';
+?>
Index: admin/report/census/db/install.xml
===================================================================
--- admin/report/census/db/install.xml	(.../vendor/moodle/1.9weekly)	(revision 0)
+++ admin/report/census/db/install.xml	(.../branches/1.9.5-LAE1.0/Census)	(revision 139)
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<XMLDB PATH="report/census/db" VERSION="2009010700" COMMENT="XMLDB file for Moodle report/census"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
+>
+  <TABLES>
+    <TABLE NAME="census" COMMENT="Default comment for census, please edit me">
+      <FIELDS>
+        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" NEXT="version"/>
+	<FIELD NAME="version" TYPE="int" LENGTH="10" NOTNULL="true"
+        UNSIGNED="true" SEQUENCE="false" PREVIOUS="id" />
+      </FIELDS>
+      <KEYS>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id" />
+      </KEYS>
+    </TABLE>
+  </TABLES>
+</XMLDB>
Index: admin/report/census/db/upgrade.php
===================================================================
--- admin/report/census/db/upgrade.php	(.../vendor/moodle/1.9weekly)	(revision 0)
+++ admin/report/census/db/upgrade.php	(.../branches/1.9.5-LAE1.0/Census)	(revision 139)
@@ -0,0 +1,69 @@
+<?php
+  require_once ($CFG->libdir.'/ddllib.php');
+
+  function xmldb_newmodule_upgrade($oldversion=0) {
+      global $CFG, $report, $db;
+      $result = true;
+      
+      // No database found
+      if ($result && $oldversion <= 0) {
+          /// Define table census to be created
+              $table = new XMLDBTable('census');
+          
+          /// Adding fields to table census
+              $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
+              $table->addFieldInfo('version', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, $report->version);
+              $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, null, 'census');
+              $table->addFieldInfo('allow_remote_reports', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+              $table->addFieldInfo('remote_hash', XMLDB_TYPE_CHAR, '32', null, null, null, null, null. null);
+                                
+          /// Adding keys to table census
+              $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
+                                   
+          /// Launch create table for census
+              $result = $result && create_table($table);
+          
+          /// Create default row in database
+              $record = new stdClass;
+              $record->name = 'census';
+              $record->version = $report->version;
+              $result = insert_record('census', $record);
+      }
+      
+      if ($result && $oldversion < '2009060300') {
+      	  /// Add remote system field to database
+      	      $table = new XMLDBTable('census');
+      	      $field = new XMLDBField('remote_server_name');
+      	      $field->setAttributes(XMLDB_TYPE_CHAR, '120', null, XMLDB_NULL, null, null, null, 'http://');
+      	      $result = $result && add_field($table, $field);	
+      	      
+      	  /// Update database version
+      	      $record = new stdClass;
+      	      $record->id = 1;
+      	      $record->name = 'census';
+      	      $record->version = $report->version;
+      	      $result = update_record('census', $record);      	  
+      }
+
+      if ($result && $oldversion < '2009060301') {
+          /// Add role storage
+              $table = new XMLDBTable('census');
+              $field = new XMLDBField('teacherroles');
+              $field->setAttributes(XMLDB_TYPE_CHAR, '32', null, XMLDB_NULL, null, null, null, '3');
+              $result = $result && add_field($table, $field);
+              
+              $field = new XMLDBField('studentroles');
+              $field->setAttributes(XMLDB_TYPE_CHAR, '32', null, XMLDB_NULL, null, null, null, '5');
+              $result = $result && add_field($table, $field);
+              
+          /// Update database version
+              $record = new stdClass;
+              $record->id = 1;
+              $record->name = 'census';
+              $record->version = $report->version;
+              $result = update_record('census', $record);  
+      }
+      
+      return $result;
+  }
+?>
Index: admin/report/census/cron.php
===================================================================
--- admin/report/census/cron.php	(.../vendor/moodle/1.9weekly)	(revision 0)
+++ admin/report/census/cron.php	(.../branches/1.9.5-LAE1.0/Census)	(revision 139)
@@ -0,0 +1,11 @@
+<?php
+  // Cron job for census report
+  // Currently does nothing
+  
+  // Cron function
+  function report_census_cron() {
+      global $CFG;
+      $cronjob = true;
+      require($CFG->dirroot.'/admin/report/census/index.php');
+  }
+?>
\ No newline at end of file
Index: admin/report/census/version.php
===================================================================
--- admin/report/census/version.php	(.../vendor/moodle/1.9weekly)	(revision 0)
+++ admin/report/census/version.php	(.../branches/1.9.5-LAE1.0/Census)	(revision 139)
@@ -0,0 +1,5 @@
+<?php
+
+$report->version	= 2009060301;	// Report version
+
+?>
Index: admin/report/census/lib.php
===================================================================
--- admin/report/census/lib.php	(.../vendor/moodle/1.9weekly)	(revision 0)
+++ admin/report/census/lib.php	(.../branches/1.9.5-LAE1.0/Census)	(revision 139)
@@ -0,0 +1,36 @@
+<?php
+
+function getCourseMembersByID($course_id = null, $roles)
+{
+    // if we're not passed a course id we can't proceed
+    if($course_id == null){ return Array() ; }
+
+    // if we're not passed a role we can't proceed
+    if(count($roles) == 0){ return Array(); }
+
+    $moodle_enrolled_people = array();
+
+    // get the course context
+    $context = get_context_instance(CONTEXT_COURSE, $course_id);
+
+    // get the list of users who currently have this role
+    foreach($roles as $role) {
+	$users = get_role_users($role->id, $context);
+        foreach($users as $user) {
+	    $moodle_enrolled_people[$user->id] = $user;        
+        }
+    }
+
+    return $moodle_enrolled_people ;
+}
+
+function distribution( $active_teachers_count )
+{
+	$distribution = array();
+	foreach ( $active_teachers_count as $teacher )
+	{
+		$distribution[$teacher] += 1;
+	}
+	return $distribution;
+}
+?>
Index: admin/report/census/index.php
===================================================================
--- admin/report/census/index.php	(.../vendor/moodle/1.9weekly)	(revision 0)
+++ admin/report/census/index.php	(.../branches/1.9.5-LAE1.0/Census)	(revision 139)
@@ -0,0 +1,341 @@
+<?php
+/**
+ *
+ * @author cfulton
+ **/
+
+    if(!isset($CFG)) {
+        require('../../../config.php');
+    }
+    require_once($CFG->libdir.'/datalib.php');
+    require_once($CFG->libdir.'/adminlib.php');
+    require($CFG->dirroot.'/admin/report/census/lib.php');
+    require($CFG->dirroot.'/admin/report/census/version.php');
+
+/// Is Cron running this?
+    $cronjob = (isset($cronjob)) ? $cronjob : false;
+
+/// Permissions
+    if(!$cronjob) {
+        require_login();
+        $systemcontext = get_context_instance(CONTEXT_SYSTEM);
+        require_capability('moodle/site:doanything', $systemcontext);
+
+    /// Log request
+        add_to_log(SITEID, "admin", "report census", "report/census/index.php");
+
+    /// Header
+        admin_externalpage_setup('reportcensus');
+        admin_externalpage_print_header();
+    }
+/// Version check
+    // Check the version in the database
+    // If not present, instantiate the database
+    if(!$versioninfo = get_field('census','version','name','census')) {
+        $versioninfo = 0;
+    }
+    
+    // Check version
+    if($versioninfo != $report->version) {
+        require($CFG->dirroot.'/admin/report/census/db/upgrade.php');
+        if(!xmldb_newmodule_upgrade($versioninfo)) {
+            echo 'ACK!';
+            exit;
+        } else {
+            print_heading(get_string('dbupdated','report_census'));
+        }
+    }
+
+/// Parse settings
+    $allow_remote = get_field('census','allow_remote_reports','name','census');
+    $remote_system = get_field('census','remote_server_name','name','census');
+    $censusrolesstudents = get_field('census','studentroles','name','census');
+    $censusrolesteachers = get_field('census','teacherroles','name','census');
+    if(optional_param('submitsettings','',PARAM_TEXT) == 'Save settings') {
+        $allow_remote_form = optional_param('phonehome','',PARAM_TEXT);
+	$remote_system = optional_param('remoteserver','',PARAM_TEXT);
+        $censusrolesstudents = implode(',',optional_param('censusrolesstudents','',PARAM_TEXT));
+        $censusrolesteachers = implode(',',optional_param('censusrolesteachers','',PARAM_TEXT));     
+	$record = new stdClass();
+	$record->id = get_field('census','id','name','census');
+	$record->remote_server_name = $remote_system;
+        $new_allow_remote = ($allow_remote_form == 'on') ? 1 : 0;	
+	if($allow_remote != $new_allow_remote) {
+            $record->allow_remote_reports = $new_allow_remote;
+            $allow_remote = $new_allow_remote;
+        }	
+        $record->teacherroles = $censusrolesteachers;
+        $record->studentroles = $censusrolesstudents;
+        $result = update_record('census', $record);
+    }
+    $censusrolesstudents = explode(',',$censusrolesstudents);
+    $censusrolesteachers = explode(',',$censusrolesteachers);
+
+/// Remote operation check
+    // return control if not allowed
+    $remote_mode_value = optional_param('remote','',PARAM_TEXT);
+    if(!isset($cronjob)) {
+        $cronjob = ($remote_mode_value == 'yes') ? true : false;
+    }
+    if($cronjob && !($allow_remote == 1)) {
+        return;
+    }
+    
+/// Hard-coded configuration options (will make moddable later)
+    // Sets the time to wait before considering a module active
+    // Value is stored in seconds; default is five minutes
+    // This is done to ignore the news forum, which is created by default
+    $module_creation_time_buffer = 5 * 60;
+
+    // The database prefix for your instance
+    // Unless you have multiple instances on the same server, this should be 'mdl'
+    $database_prefix = $CFG->prefix;
+
+/// Census method definitions
+    $census_method_definitions = array(
+        'module' => array('text' => 'Active modules',
+                     'db_method' => 'count_records_sql',
+                     'db_sql' => "SELECT count($database_prefix"."course_modules.module) 
+                                  from $database_prefix"."course_modules inner join $database_prefix"."course on 
+                                  $database_prefix"."course_modules.course=$database_prefix"."course.id WHERE 
+                                  $database_prefix"."course_modules.added > ($database_prefix"."course.timecreated + $module_creation_time_buffer) 
+				  AND $database_prefix"."course.id='{{COURSE_ID}}'"),
+        'resource' => array('text' => 'Resources',
+                      'db_method' => 'count_records',
+                      'db_object' => 'resource',
+                      'db_subject' => 'course'),
+        'forum_posts' => array('text' => 'Forum posts',
+                      'db_method' => 'count_records_sql',
+                      'db_sql' => "SELECT count($database_prefix"."forum_posts.id) 
+                                  from $database_prefix"."forum_posts,$database_prefix"."forum_discussions 
+                                  where $database_prefix"."forum_discussions.id = $database_prefix"."forum_posts.discussion 
+                                  and $database_prefix"."forum_discussions.course ='{{COURSE_ID}}'")                    );
+    $census_report_definitions = array(
+        'teachers' => array(
+                       'method' => 'count',
+                       'variable' => 'active_teachers',
+                       'title' => array('Teachers with active courses')),
+        'courses' => array(
+                       'method' => 'count',
+                       'variable' => 'active_courses',
+                       'title' => array('Active courses')),
+        'teacher-dist' => array(
+                       'method' => 'distribution',
+                       'variable' => 'active_teachers',
+                       'title' => array('Courses taught','Teachers')),
+        'student-dist' => array(
+                       'method' => 'distribution',
+                       'variable' => 'active_students',
+                       'title' => array('Courses taken','Students')),
+        'department-dist' => array(
+                       'method' => 'simple',
+                       'variable' => 'active_department',
+                       'title' => array('Department name','Active courses')),
+        'class-size' => array(
+                       'method' => 'simple',
+                       'variable' => 'class_size',
+                       'title' => array('Class size','Active courses')),
+        'course-format' => array(
+                       'method' => 'simple',
+                       'variable' => 'course_format',
+                       'title' => array('Course format','Active courses')),
+    );
+                       
+/// Census options
+    $census_active_options = array('module','resource','forum_posts');    
+    $census_collect_options = array_merge($census_active_options, array('template'));
+    $census_report_options = array('teachers','courses','teacher-dist','student-dist','department-dist','class-size','course-format');
+
+/// Variable initialization
+    // Active course counter
+    $stats = array();
+
+/// Get roles from database
+    $roles = get_records('role');
+
+/// Settings form
+    if(!$cronjob) {
+        $checked_text = ($allow_remote == 1) ? 'checked=\"checked\" ' : '';
+        print_box_start('generalbox boxwidthwide boxaligncenter centerpara');
+        echo '<form method="post" action="." id="settingsform">';
+        print_heading(get_string('reportsettings','report_census'));
+        echo '<p><input type="checkbox" name="phonehome" id="phonehome" '.$checked_text.'/>'.get_string('form_phonehome','report_census').'</p>';    
+        echo '<p><input type="text" name="remoteserver" id="remoteserver" value="'.$remote_system.'" />'.get_string('form_remotesystem','report_census').'</p>';
+        print_heading(get_string('form_roleassignment','report_census'));
+        echo '<p><table><tr><th>Role</th><th>Teachers</th><th>Students</th></tr>';
+        foreach($roles as $role) {
+            $teacherchecked = (in_array($role->id,$censusrolesteachers)) ? ' checked="checked"' : '';
+            $studentchecked = (in_array($role->id,$censusrolesstudents)) ? ' checked="checked"' : '';
+            echo '<tr><td>'.$role->name.'</td>';
+            echo '<td><input type="checkbox" name="censusrolesteachers[]" id="censusrolesteachers[]"'.$teacherchecked.' value="'.$role->id.'" /></td>';
+            echo '<td><input type="checkbox" name="censusrolesstudents[]" id="censusrolesstudents[]"'.$studentchecked.' value="'.$role->id.'" /></td></tr>';
+        }
+        echo '</table></p>';
+	echo '<p><input type="submit" name="submitsettings" id="submitsettings" value="Save settings" /></p>';
+        echo '</form>';
+        print_box_end();
+
+    /// Execute census
+        print_box_start('generalbox boxwidthwide boxaligncenter centerpara');
+        echo '<form method="get" action="." id="runcensusform">';
+        print_heading(get_string('runcensus','report_census'));
+	echo '<input type="hidden" name="run" id="run" value="yes" />';
+        echo '<p><input type="submit" id="submitrun" value="Run census" /></p>';
+        echo '</form>';
+        print_box_end();
+    }
+
+    $run = optional_param('run','no',PARAM_TEXT);
+    if($run == 'yes' || $cronjob) {
+/// Get all the appropriate data
+    // Load roles (cron job for now assumes defaults)
+    if(count($censusrolesteachers) < 1 || count($censusrolesstudents) < 1) {
+        die(get_string('error_noroles','report_census'));
+    } else {
+    	$roles = get_records('role');
+    	foreach($roles as $role) {
+    	    if(in_array($role->id, $censusrolesteachers)) {
+    	    	$teacher_roles[] = $role;
+    	    }
+    	    if(in_array($role->id, $censusrolesstudents)) {
+    	        $student_roles[] = $role;
+    	    }
+    	}
+    }
+    
+    // Get all course ids from given instance
+    $courseids = get_fieldset_sql("SELECT DISTINCT id FROM $database_prefix"."course");
+    // Go through the course and get all teachers and students
+    foreach($courseids as $id) {
+        $course_teachers_ids = array();
+        $course_students_ids = array();
+        $course_census_data = array();
+
+        // Load ids for a given course
+        $course_teachers = getCourseMembersByID($id, $teacher_roles);
+        $course_students = getCourseMembersByID($id, $student_roles);
+
+        // Merge in id information
+        $course_teachers_ids = array_merge(array_keys($course_teachers),$course_teachers_ids);
+        $course_students_ids = array_merge(array_keys($course_students),$course_students_ids); 
+
+        // Run census checks
+        foreach($census_active_options as $option_id) {
+            $option = $census_method_definitions[$option_id];
+            $function = $option['db_method'];
+            switch($function) {
+                case 'count_records':
+                  $data = $function($option['db_object'], $option['db_subject'], $id);
+                  break;
+                case 'count_records_sql':
+                  $command = $option['db_sql'];
+                  $command = str_replace('{{COURSE_ID}}',$id,$command);
+                  $data = $function($command);
+		  break;
+            }
+            $course_census_data[$option_id] = $data;
+        }
+
+        // Get course category information
+        $cat_id   = get_field( 'course', 'category', 'id', $id);
+        $cat_name = get_field( 'course_categories', 'name', 'id', $cat_id);
+
+        // Test whether a course is active
+        if(array_sum($course_census_data) > 0) {
+            // Add active teachers
+            foreach($course_teachers_ids as $item_id) {
+                $stats['active_teachers'][$item_id]++;
+            }
+            
+            // Add active students
+            foreach($course_students_ids as $item_id) {
+                $stats['active_students'][$item_id]++;
+            }
+
+            // Increment the course counter
+            $stats['active_courses']++;             
+            
+            // Increment the department/category counter
+            $stats['active_department'][$cat_name]++;                   
+            
+            // Increment class size data
+            $class_size_rounded = ceil(count($course_students) / 10);
+            $class_size_key = (($class_size_rounded - 1)*10+1) .'-'.($class_size_rounded *10);
+            $stats['class_size'][$class_size_key]++;
+            
+            // Get format information
+            $format = get_field('course','format','id',$id);
+            $stats['course_format'][$format]++;            
+        }	
+    }
+    // end loop through courses
+    
+/// Print/Send results
+    $stats['census_version'] = $report->version;
+    $stats['moodle_version'] = get_field('config','value','name','version');
+    foreach($census_report_options as $report) {
+        $mode = $census_report_definitions[$report];
+        $table->head = $mode['title'];
+        $table->data = array();
+        switch($mode['method']) {
+            case 'count':
+                $variable = $mode['variable'];
+                $data = (is_array($stats[$variable])) ? count($stats[$variable]) : $stats[$variable];
+                $table->data[] = array($data);
+                break;
+            case 'distribution':
+                $variable = $mode['variable'];
+                $dist = distribution($stats[$variable]);
+                foreach( array_keys($dist) as $seg_idx) {
+                    $table->data[] = array($seg_idx, $dist[$seg_idx]);
+                }          
+                break;
+            case 'simple':
+                $variable = $mode['variable'];
+                foreach( $stats[$variable] as  $key => $item) {
+                    $table->data[] = array($key, $item);
+                }
+                break;
+        }
+        if(!$cronjob) {
+            print_table($table);    
+        }
+    }
+    
+    // If remote enabled, send data
+    if($cronjob) {
+      $hash = get_field('census','remote_hash','name','census');
+      if(strlen($hash) != '32') {
+          $site = get_field('course','fullname','id',1);
+          $hash = md5($site . time());
+          $record = new stdClass();
+          $record->id = get_field('census','id','name','census');
+          $record->remote_hash = $hash;
+          $result = update_record('census', $record);
+      }
+      $stats['id'] = $hash;
+
+      // Setup CURL and transmit
+      if(function_exists('curl_init')) {
+          $ch = curl_init();
+          
+          curl_setopt($ch, CURLOPT_URL, $remote_system);
+          curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
+          curl_setopt($ch, CURLOPT_POST, TRUE);
+          curl_setopt($ch, CURLOPT_POSTFIELDS, $stats);
+          
+          curl_exec($ch);
+          curl_close($ch);
+          
+      } else {
+          print_error(get_string('curlerror','report_census'));
+      }
+    }
+
+    } // end GET test for running census
+
+/// Finish the page
+    if(!$cronjob) {
+        admin_externalpage_print_footer();
+    }
+?>
