[svn r14822] Added user fields data to survey's complete exports

skala
Yannick Warnier 17 years ago
parent 639d4851ce
commit 3c443a78f6
  1. 42
      main/inc/lib/usermanager.lib.php
  2. 132
      main/survey/survey.lib.php

@ -1,4 +1,4 @@
<?php // $Id: usermanager.lib.php 14811 2008-04-09 18:50:44Z yannoo $
<?php // $Id: usermanager.lib.php 14822 2008-04-10 04:44:54Z yannoo $
/*
==============================================================================
Dokeos - elearning and course management software
@ -835,10 +835,12 @@ class UserManager
* Get an array of extra fieds with field details (type, default value and options)
* @param integer Offset (from which row)
* @param integer Number of items
* @param integer
* @param integer Column on which sorting is made
* @param string Sorting direction
* @param boolean Optional. Whether we get all the fields or just the visible ones
* @return array Extra fields details (e.g. $list[2]['type'], $list[4]['options'][2]['title']
*/
function get_extra_fields($from=0, $number_of_items=20, $column=5, $direction='ASC')
function get_extra_fields($from=0, $number_of_items=0, $column=5, $direction='ASC', $all_visibility=true)
{
$fields = array();
$t_uf = Database :: get_main_table(TABLE_MAIN_USER_FIELD);
@ -849,7 +851,16 @@ class UserManager
{
$sort_direction = strtoupper($direction);
}
$sqlf = "SELECT * FROM $t_uf ORDER BY ".$columns[$column]." $sort_direction LIMIT ".Database::escape_string($from).','.Database::escape_string($number_of_items);
$sqlf = "SELECT * FROM $t_uf ";
if($all_visibility==false)
{
$sqlf .= " WHERE field_visible = 1 ";
}
$sqlf .= " ORDER BY ".$columns[$column]." $sort_direction " ;
if($number_of_items != 0)
{
$sqlf .= " LIMIT ".Database::escape_string($from).','.Database::escape_string($number_of_items);
}
$resf = api_sql_query($sqlf,__FILE__,__LINE__);
if(Database::num_rows($resf)>0)
{
@ -886,12 +897,18 @@ class UserManager
}
/**
* Get the number of extra fields currently recorded
* @param boolean Optional switch. true (default) returns all fields, false returns only visible fields
* @return integer Number of fields
*/
function get_number_of_extra_fields()
function get_number_of_extra_fields($all_visibility=true)
{
$t_uf = Database :: get_main_table(TABLE_MAIN_USER_FIELD);
$sqlf = "SELECT * FROM $t_uf ORDER BY field_order";
$sqlf = "SELECT * FROM $t_uf ";
if($all_visibility == false)
{
$sqlf .= " WHERE field_visible = 1 ";
}
$sqlf .= " ORDER BY field_order";
$resf = api_sql_query($sqlf,__FILE__,__LINE__);
return Database::num_rows($resf);
}
@ -990,15 +1007,24 @@ class UserManager
* Gets user extra fields data
* @param integer User ID
* @param boolean Whether to prefix the fields indexes with "extra_" (might be used by formvalidator)
* @param boolean Whether to return invisible fields as well
* @return array Array of fields => value for the given user
*/
function get_extra_user_data($user_id, $prefix=false)
function get_extra_user_data($user_id, $prefix=false, $all_visibility = true)
{
$extra_data = array();
$t_uf = Database::get_main_table(TABLE_MAIN_USER_FIELD);
$t_ufv = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
$user_id = Database::escape_string($user_id);
$sql = "SELECT f.id as fid, f.field_variable as fvar, fv.field_value as fval FROM $t_uf f, $t_ufv fv WHERE fv.user_id = $user_id AND fv.field_id = f.id ORDER BY f.field_order";
$sql = "SELECT f.id as fid, f.field_variable as fvar, fv.field_value as fval " .
"FROM $t_uf f, $t_ufv fv " .
"WHERE fv.user_id = $user_id " .
"AND fv.field_id = f.id ";
if($all_visibility == false)
{
$sql .= " AND f.field_visible = 1 ";
}
$sql .= " ORDER BY f.field_order";
$res = api_sql_query($sql,__FILE__,__LINE__);
if(Database::num_rows($res)>0)
{

@ -2,6 +2,8 @@
$config['survey']['debug'] = false;
/*
DOKEOS - elearning and course management software
Copyright (c) 2008 Dokeos SPRL
Copyright (c) 2006-2008 Patrick Cool
For a full list of contributors, see documentation/credits.html
@ -13,19 +15,20 @@ $config['survey']['debug'] = false;
Contact:
Dokeos
Rue des Palais 44 Paleizenstraat
Rue du Corbeau, 108
B-1030 Brussels - Belgium
Tel. +32 (2) 211 34 56
info@dokeos.com
*/
/**
* @package dokeos.survey
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts (if not all) of the code
* @version $Id: survey.lib.php 10680 2007-01-11 21:26:23Z pcool $
* @version $Id: survey.lib.php 14822 2008-04-10 04:44:54Z yannoo $
*
* @todo move this file to inc/lib
* @todo use consistent naming for the functions (save vs store for instance)
*/
require_once(api_get_path(LIBRARY_PATH).'usermanager.lib.php');
class survey_manager
{
@ -2439,6 +2442,21 @@ class SurveyUtil {
}
echo ' <input type="submit" name="submit_question_filter" value="'.get_lang('SubmitQuestionFilter').'" />';
echo '</th>';
$display_extra_user_fields = false;
if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
{
//show user fields section with a big th colspan that spans over all fields
$extra_user_fields = UserManager::get_extra_fields(0,0,5,'ASC',false);
$num = count($extra_user_fields);
echo '<th '.($num>0?' colspan="'.$num.'"':'').'>';
echo '<label><input type="checkbox" name="fields_filter" value="1" checked="checked"/> ';
echo get_lang('UserFields');
echo '</label>';
echo '</th>';
$display_extra_user_fields = true;
}
$sql = "SELECT questions.question_id, questions.type, questions.survey_question, count(options.question_option_id) as number_of_options
FROM $table_survey_question questions LEFT JOIN $table_survey_question_option options
ON questions.question_id = options.question_id
@ -2475,6 +2493,16 @@ class SurveyUtil {
// getting all the questions and options
echo ' <tr>';
echo ' <th>&nbsp;</th>'; // the user column
if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
{
//show the fields names for user fields
foreach($extra_user_fields as $field)
{
echo '<th>'.$field[3].'</th>';
}
}
$sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
FROM $table_survey_question survey_question
@ -2511,7 +2539,7 @@ class SurveyUtil {
{
if ($old_user <> $row['user'] AND $old_user<>'')
{
SurveyUtil::display_complete_report_row($possible_answers, $answers_of_user, $old_user, $questions);
SurveyUtil::display_complete_report_row($possible_answers, $answers_of_user, $old_user, $questions, $display_extra_user_fields);
$answers_of_user=array();
}
if ($questions[$row['question_id']]['type']<> 'open')
@ -2524,7 +2552,7 @@ class SurveyUtil {
}
$old_user = $row['user'];
}
SurveyUtil::display_complete_report_row($possible_answers, $answers_of_user, $old_user, $questions); // this is to display the last user
SurveyUtil::display_complete_report_row($possible_answers, $answers_of_user, $old_user, $questions, $display_extra_user_fields); // this is to display the last user
echo '</table>';
@ -2538,10 +2566,11 @@ class SurveyUtil {
* @param array Possible options
* @param array User answers
* @param mixed User ID or user details string
* @param boolean Whether to show extra user fields or not
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version February 2007 - Updated March 2008
*/
function display_complete_report_row($possible_options, $answers_of_user, $user, $questions)
function display_complete_report_row($possible_options, $answers_of_user, $user, $questions, $display_extra_user_fields=false)
{
global $survey_data;
@ -2573,6 +2602,15 @@ class SurveyUtil {
echo '<th>-</th>';
}
if ($display_extra_user_fields)
{
//show user fields data, if any, for this user
$user_fields_values = UserManager::get_extra_user_data(intval($user),false,false);
foreach($user_fields_values as $value)
{
echo '<td align="center">'.$value.'</td>';
}
}
foreach ($possible_options as $question_id=>$possible_option)
{
@ -2624,6 +2662,18 @@ class SurveyUtil {
// the first column
$return = ';';
//show extra fields blank space (enough for extra fields on next line)
$display_extra_user_fields = false;
if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
{
//show user fields section with a big th colspan that spans over all fields
$extra_user_fields = UserManager::get_extra_fields(0,0,5,'ASC',false);
$num = count($extra_user_fields);
$return .= str_repeat(';',$num);
$display_extra_user_fields = true;
}
$sql = "SELECT questions.question_id, questions.type, questions.survey_question, count(options.question_option_id) as number_of_options
FROM $table_survey_question questions LEFT JOIN $table_survey_question_option options
ON questions.question_id = options.question_id "
@ -2658,6 +2708,17 @@ class SurveyUtil {
// getting all the questions and options
$return .= ';';
//show extra field values
if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
{
//show the fields names for user fields
foreach($extra_user_fields as $field)
{
$return .= '"'.str_replace("\r\n",' ',html_entity_decode(strip_tags($field[3]))).'";';
}
}
$sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
FROM $table_survey_question survey_question
@ -2697,7 +2758,7 @@ class SurveyUtil {
{
if ($old_user <> $row['user'] AND $old_user <> '')
{
$return .= SurveyUtil::export_complete_report_row($possible_answers, $answers_of_user, $old_user);
$return .= SurveyUtil::export_complete_report_row($possible_answers, $answers_of_user, $old_user, $display_extra_user_fields);
$answers_of_user=array();
}
if($possible_answers_type[$row['question_id']] == 'open')
@ -2712,7 +2773,7 @@ class SurveyUtil {
}
$old_user = $row['user'];
}
$return .= SurveyUtil::export_complete_report_row($possible_answers, $answers_of_user, $old_user); // this is to display the last user
$return .= SurveyUtil::export_complete_report_row($possible_answers, $answers_of_user, $old_user, $display_extra_user_fields); // this is to display the last user
return $return;
}
@ -2723,13 +2784,25 @@ class SurveyUtil {
* @param array Possible answers
* @param array User's answers
* @param mixed User ID or user details as string - Used as a string in the result string
* @param boolean Whether to display user fields or not
* @return string One line of the csv file
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version February 2007
*/
function export_complete_report_row($possible_options, $answers_of_user, $user)
function export_complete_report_row($possible_options, $answers_of_user, $user, $display_extra_user_fields=false)
{
$return = $user.';'; // the user column
if($display_extra_user_fields)
{
//show user fields data, if any, for this user
$user_fields_values = UserManager::get_extra_user_data(intval($user),false,false);
foreach($user_fields_values as $value)
{
$return .= '"'.str_replace('"','""',html_entity_decode(strip_tags($value))).'";';
}
}
if(is_array($possible_options))
{
@ -2772,6 +2845,21 @@ class SurveyUtil {
$worksheet =& $workbook->addWorksheet('Report 1');
$line = 0;
$column = 1; //skip the first column (row titles)
//show extra fields blank space (enough for extra fields on next line)
$display_extra_user_fields = false;
if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
{
//show user fields section with a big th colspan that spans over all fields
$extra_user_fields = UserManager::get_extra_fields(0,0,5,'ASC',false);
$num = count($extra_user_fields);
for($i=0;$i<$num;$i++)
{
$worksheet->write($line,$column,'');
$column ++;
}
$display_extra_user_fields = true;
}
// Database table definitions
$table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
@ -2813,6 +2901,17 @@ class SurveyUtil {
}
$line++;
$column = 1;
//show extra field values
if ($display_extra_user_fields)
{
//show the fields names for user fields
foreach($extra_user_fields as $field)
{
$worksheet->write($line,$column,html_entity_decode(strip_tags($field[3])));
$column++;
}
}
// getting all the questions and options (second line)
$sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
@ -2857,7 +2956,7 @@ class SurveyUtil {
{
if ($old_user <> $row['user'] AND $old_user <> '')
{
$return = SurveyUtil::export_complete_report_row_xls($possible_answers, $answers_of_user, $old_user);
$return = SurveyUtil::export_complete_report_row_xls($possible_answers, $answers_of_user, $old_user, $display_extra_user_fields);
foreach($return as $elem)
{
$worsheet->write($line,$column,$elem);
@ -2896,13 +2995,24 @@ class SurveyUtil {
* @param array Possible answers
* @param array User's answers
* @param mixed User ID or user details as string - Used as a string in the result string
* @param boolean Whether to display user fields or not
* @return string One line of the csv file
*/
function export_complete_report_row_xls($possible_options, $answers_of_user, $user)
function export_complete_report_row_xls($possible_options, $answers_of_user, $user, $display_extra_user_fields=false)
{
$return = array();
$return[] = $user; // the user column
if($display_extra_user_fields)
{
//show user fields data, if any, for this user
$user_fields_values = UserManager::get_extra_user_data(intval($user),false,false);
foreach($user_fields_values as $value)
{
$return[] = html_entity_decode(strip_tags($value));
}
}
if(is_array($possible_options))
{
foreach ($possible_options as $question_id=>$possible_option)

Loading…
Cancel
Save