, Ghent University: cleanup, refactoring and rewriting large parts (if not all) of the code * @author Julio Montoya Armas , Chamilo: Personality Test modification and rewriting large parts of the code * @version $Id: survey.lib.php 22296 2009-07-22 22:05:50Z cfasanando $ * @todo move this file to inc/lib * @todo use consistent naming for the functions (save vs store for instance) */ /** * Code */ $config['survey']['debug'] = false; $htmlHeadXtra[] = ''; class survey_manager { /** * Deletes all survey invitations of a user * @param int $user_id * @return boolean * @assert ('') === false */ public static function delete_all_survey_invitations_by_user($user_id) { $user_id = intval($user_id); if (empty($user_id)) { return false; } $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION); $table_survey = Database :: get_course_table(TABLE_SURVEY); $sql = "SELECT survey_invitation_id, survey_code FROM $table_survey_invitation WHERE user = '$user_id' AND c_id <> 0 "; $result = Database::query($sql); $deleted = array(); while ($row = Database::fetch_array($result ,'ASSOC')){ $survey_invitation_id = $row['survey_invitation_id']; $survey_code = $row['survey_code']; $sql2 = "DELETE FROM $table_survey_invitation WHERE survey_invitation_id = '$survey_invitation_id' AND c_id <> 0"; if (Database::query($sql2)) { $sql3 = "UPDATE $table_survey SET invited = invited-1 ". " WHERE c_id <> 0 AND code ='$survey_code'"; Database::query($sql3); } } } /** * * @param type $course_code * @param type $session_id * @return type * @assert ('') === false */ public static function get_surveys($course_code, $session_id = 0) { $table_survey = Database :: get_course_table(TABLE_SURVEY); if (empty($course_code)) { return false; } $course_info = api_get_course_info($course_code); $session_condition = api_get_session_condition($session_id, true, true); $sql = "SELECT * FROM $table_survey WHERE c_id = {$course_info['real_id']} $session_condition "; $result = Database::query($sql); $result = Database::store_result($result, 'ASSOC'); return $result; } /** * Retrieves all the survey information * * @param integer $survey_id the id of the survey * @param boolean $shared this parameter determines if * we have to get the information of a survey from the central (shared) database or from the * course database * @param string course code optional * * @author Patrick Cool , Ghent University * @version February 2007 * @assert ('') === false * * @todo this is the same function as in create_new_survey.php */ public static function get_survey($survey_id, $shared = 0, $course_code = '') { // Table definition if (!empty($course_code)) { $my_course_id = $course_code; } else if (isset($_GET['course'])) { $my_course_id = Security::remove_XSS($_GET['course']); } else { $my_course_id = api_get_course_id(); } $my_course_info = api_get_course_info($my_course_id); $table_survey = Database :: get_course_table(TABLE_SURVEY); if ($shared != 0) { $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION); $sql = "SELECT * FROM $table_survey WHERE survey_id='".Database::escape_string($survey_id)."' "; } else { $sql = "SELECT * FROM $table_survey WHERE survey_id='".Database::escape_string($survey_id)."' AND c_id = ".$my_course_info['real_id']; } $result = Database::query($sql); $return = array(); if (Database::num_rows($result)> 0) { $return = Database::fetch_array($result,'ASSOC'); // We do this (temporarily) to have the array match the quickform elements immediately // idealiter the fields in the db match the quickform fields $return['survey_code'] = $return['code']; $return['survey_title'] = $return['title']; $return['survey_subtitle'] = $return['subtitle']; $return['survey_language'] = $return['lang']; $return['start_date'] = $return['avail_from']; $return['end_date'] = $return['avail_till']; $return['survey_share'] = $return['is_shared']; $return['survey_introduction'] = $return['intro']; $return['survey_thanks'] = $return['surveythanks']; $return['survey_type'] = $return['survey_type']; $return['one_question_per_page']= $return['one_question_per_page']; $return['show_form_profile'] = $return['show_form_profile']; $return['input_name_list'] = isset($return['input_name_list']) ? $return['input_name_list'] : null; $return['shuffle'] = $return['shuffle']; $return['parent_id'] = $return['parent_id']; $return['survey_version'] = $return['survey_version']; } return $return; } /** * This function stores a survey in the database. * * @param array $values * @return array $return the type of return message that has to be displayed and the message in it * * @author Patrick Cool , Ghent University * @version February 2007 */ public static function store_survey($values) { $_user = api_get_user_info(); $course_id = api_get_course_int_id(); $table_survey = Database :: get_course_table(TABLE_SURVEY); $shared_survey_id = 0; if (!$values['survey_id'] || !is_numeric($values['survey_id'])) { // Check if the code doesn't soon exists in this language $sql = 'SELECT 1 FROM '.$table_survey.' WHERE c_id = '.$course_id.' AND code="'.Database::escape_string($values['survey_code']).'" AND lang="'.Database::escape_string($values['survey_language']).'"'; $rs = Database::query($sql); if (Database::num_rows($rs) > 0) { $return['message'] = 'ThisSurveyCodeSoonExistsInThisLanguage'; $return['type'] = 'error'; $return['id'] = isset($values['survey_id']) ? $values['survey_id'] : 0; return $return; } if ($values['anonymous'] == '') { $values['anonymous'] = 0; } $additional['columns'] = ''; $additional['values'] = ''; if ($values['anonymous'] == 0) { // Input_name_list $additional['columns'] .= ', show_form_profile'; $additional['values'] .= ",'".Database::escape_string($values['show_form_profile'])."'"; if ($values['show_form_profile'] == 1) { // Input_name_list $fields = explode(',', $values['input_name_list']); $field_values = ''; foreach ($fields as & $field) { if ($field != '') { if ($values[$field] == '') { $values[$field] = 0; } $field_values.= $field.':'.$values[$field].'@'; } } $additional['columns'] .= ', form_fields'; $additional['values'] .= ",'".Database::escape_string($field_values)."'"; } else { $additional['columns'] .= ', form_fields'; $additional['values'] .= ",''"; } } else { // Input_name_list $additional['columns'] .= ', show_form_profile'; $additional['values'] .= ",'0'"; $additional['columns'] .= ', form_fields'; $additional['values'] .= ",''"; } if ($values['survey_type'] == 1) { $additional['columns'] .= ', survey_type'; $additional['values'] .= ",'1'"; $additional['columns'] .= ', shuffle'; $additional['values'] .= ",'".Database::escape_string($values['shuffle'])."'"; $additional['columns'] .= ', one_question_per_page'; $additional['values'] .= ",'".Database::escape_string($values['one_question_per_page'])."'"; $additional['columns'] .= ', parent_id'; $additional['values'] .= ",'".Database::escape_string($values['parent_id'])."'"; // Logic for versioning surveys if (!empty($values['parent_id'])) { $additional['columns'] .= ', survey_version'; $sql = 'SELECT survey_version FROM '.$table_survey.' WHERE c_id = '.$course_id.' AND parent_id = '.Database::escape_string($values['parent_id']).' ORDER BY survey_version DESC LIMIT 1'; $rs = Database::query($sql); if (Database::num_rows($rs) === 0) { $sql = 'SELECT survey_version FROM '.$table_survey.' WHERE c_id = '.$course_id.' survey_id = '.Database::escape_string($values['parent_id']); $rs = Database::query($sql); $getversion = Database::fetch_array($rs, 'ASSOC'); if (empty($getversion['survey_version'])) { $additional['values'] .= ",'".++$getversion['survey_version']."'"; } else { $additional['values'] .= ",'".$getversion['survey_version'].".1'"; } } else { $row = Database::fetch_array($rs, 'ASSOC'); $pos = api_strpos($row['survey_version']); if ($pos === false) { //$new_version = substr($row['survey_version'],$pos, count()) $row['survey_version'] = $row['survey_version'] + 1; $additional['values'] .= ",'".$row['survey_version']."'"; } else { $getlast = explode('\.', $row['survey_version']); $lastversion = array_pop($getlast); $lastversion = $lastversion + 1; $add = implode('.', $getlast); if ($add != '') { $insertnewversion = $add.'.'.$lastversion; } else { $insertnewversion = $lastversion; } $additional['values'] .= ",'".$insertnewversion."'"; } } } } $course_id = api_get_course_int_id(); $sql = "INSERT INTO $table_survey (c_id, code, title, subtitle, author, lang, avail_from, avail_till, is_shared, template, intro, surveythanks, creation_date, anonymous".$additional['columns'].", session_id) VALUES ( $course_id, '".Database::escape_string(strtolower(generate_course_code(api_substr($values['survey_code'],0))))."', '".Database::escape_string($values['survey_title'])."', '".Database::escape_string($values['survey_subtitle'])."', '".Database::escape_string($_user['user_id'])."', '".Database::escape_string($values['survey_language'])."', '".Database::escape_string($values['start_date'])."', '".Database::escape_string($values['end_date'])."', '".Database::escape_string($shared_survey_id)."', '".Database::escape_string('template')."', '".Database::escape_string($values['survey_introduction'])."', '".Database::escape_string($values['survey_thanks'])."', '".date('Y-m-d H:i:s')."', '".Database::escape_string($values['anonymous'])."'".$additional['values'].", ".api_get_session_id()." )"; Database::query($sql); $survey_id = Database::insert_id(); if ($survey_id > 0) { // Insert into item_property api_item_property_update(api_get_course_info(), TOOL_SURVEY, $survey_id, 'SurveyAdded', api_get_user_id()); } if ($values['survey_type'] == 1 && !empty($values['parent_id'])) { survey_manager::copy_survey($values['parent_id'],$survey_id); } $return['message'] = 'SurveyCreatedSuccesfully'; $return['type'] = 'confirmation'; $return['id'] = $survey_id; } else { // Check whether the code doesn't soon exists in this language $sql = 'SELECT 1 FROM '.$table_survey.' WHERE c_id = '.$course_id.' AND code="'.Database::escape_string($values['survey_code']).'" AND lang="'.Database::escape_string($values['survey_language']).'" AND survey_id!='.intval($values['survey_id']); $rs = Database::query($sql); if (Database::num_rows($rs) > 0) { $return['message'] = 'ThisSurveyCodeSoonExistsInThisLanguage'; $return['type'] = 'error'; $return['id'] = isset($values['survey_id']) ? $values['survey_id'] : 0; return $return; } if ($values['anonymous'] == '') { $values['anonymous'] = 0; } $additionalsets = ", shuffle = '".Database::escape_string($values['shuffle'])."'"; $additionalsets .= ", one_question_per_page = '".Database::escape_string($values['one_question_per_page'])."'"; if ($values['anonymous'] == 0) { $additionalsets .= ", show_form_profile = '".Database::escape_string($values['show_form_profile'])."'"; if ($values['show_form_profile'] == 1) { $fields = explode(',',$values['input_name_list']); $field_values = ''; foreach ($fields as & $field) { if ($field != '') { if ($values[$field] == '') { $values[$field] = 0; } $field_values.= $field.':'.$values[$field].'@'; } } $additionalsets .= ", form_fields = '".Database::escape_string($field_values)."'"; } else { $additionalsets .= ", form_fields = '' "; } } else { $additionalsets .= ", show_form_profile = '0'"; $additionalsets .= ", form_fields = '' "; } $sql = "UPDATE $table_survey SET title = '".Database::escape_string($values['survey_title'])."', subtitle = '".Database::escape_string($values['survey_subtitle'])."', author = '".Database::escape_string($_user['user_id'])."', lang = '".Database::escape_string($values['survey_language'])."', avail_from = '".Database::escape_string($values['start_date'])."', avail_till = '".Database::escape_string($values['end_date'])."', is_shared = '".Database::escape_string($shared_survey_id)."', template = '".Database::escape_string('template')."', intro = '".Database::escape_string($values['survey_introduction'])."', surveythanks = '".Database::escape_string($values['survey_thanks'])."', anonymous = '".Database::escape_string($values['anonymous'])."'".$additionalsets." WHERE c_id = $course_id AND survey_id = '".Database::escape_string($values['survey_id'])."'"; Database::query($sql); // Update into item_property (update) api_item_property_update(api_get_course_info(), TOOL_SURVEY, $values['survey_id'], 'SurveyUpdated', api_get_user_id()); $return['message'] = 'SurveyUpdatedSuccesfully'; $return['type'] = 'confirmation'; $return['id'] = $values['survey_id']; } return $return; } /** * This function stores a shared survey in the central database. * * @param array $values * @return array $return the type of return message that has to be displayed and the message in it * * @author Patrick Cool , Ghent University * @version February 2007 */ public function store_shared_survey($values) { $_user = api_get_user_info(); $_course = api_get_course_info(); // Table defnitions $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY); if (!$values['survey_id'] || !is_numeric($values['survey_id']) || $values['survey_share']['survey_share'] == 'true') { $sql = "INSERT INTO $table_survey (code, title, subtitle, author, lang, template, intro, surveythanks, creation_date, course_code) VALUES ( '".Database::escape_string($values['survey_code'])."', '".Database::escape_string($values['survey_title'])."', '".Database::escape_string($values['survey_subtitle'])."', '".Database::escape_string($_user['user_id'])."', '".Database::escape_string($values['survey_language'])."', '".Database::escape_string('template')."', '".Database::escape_string($values['survey_introduction'])."', '".Database::escape_string($values['survey_thanks'])."', '".date('Y-m-d H:i:s')."', '".$_course['id']."')"; Database::query($sql); $return = Database::insert_id(); } else { $sql = "UPDATE $table_survey SET code = '".Database::escape_string($values['survey_code'])."', title = '".Database::escape_string($values['survey_title'])."', subtitle = '".Database::escape_string($values['survey_subtitle'])."', author = '".Database::escape_string($_user['user_id'])."', lang = '".Database::escape_string($values['survey_language'])."', template = '".Database::escape_string('template')."', intro = '".Database::escape_string($values['survey_introduction'])."', surveythanks = '".Database::escape_string($values['survey_thanks'])."' WHERE survey_id = '".Database::escape_string($values['survey_share']['survey_share'])."'"; Database::query($sql); $return = $values['survey_share']['survey_share']; } return $return; } /** * This function deletes a survey (and also all the question in that survey * * @param $survey_id the id of the survey that has to be deleted * @return true * * @author Patrick Cool , Ghent University * @version January 2007 */ public static function delete_survey($survey_id, $shared = false, $course_id = '') { // Database table definitions if (empty($course_id)) { $course_id = api_get_course_int_id(); } $survey_id = intval($survey_id); if (empty($survey_id)) { return false; } $course_info = api_get_course_info_by_id($course_id); $course_id = $course_info['real_id']; $table_survey = Database :: get_course_table(TABLE_SURVEY); $table_survey_question_group = Database :: get_course_table(TABLE_SURVEY_QUESTION_GROUP); if ($shared) { $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY); // Deleting the survey $sql = "DELETE FROM $table_survey WHERE survey_id='".$survey_id."'"; Database::query($sql); } else { $sql = "DELETE FROM $table_survey WHERE c_id = $course_id AND survey_id='".$survey_id."'"; Database::query($sql); } // Deleting groups of this survey $sql = "DELETE FROM $table_survey_question_group WHERE c_id = $course_id AND survey_id='".$survey_id."'"; Database::query($sql); // Deleting the questions of the survey survey_manager::delete_all_survey_questions($survey_id, $shared); // Update into item_property (delete) api_item_property_update($course_info, TOOL_SURVEY, $survey_id, 'SurveyDeleted', api_get_user_id()); return true; } /** * @param int $parent_survey * @param int $new_survey_id * @return bool */ public function copy_survey($parent_survey, $new_survey_id) { $course_id = api_get_course_int_id(); // Database table definitions $table_survey = Database::get_course_table(TABLE_SURVEY); $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP); $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION); $table_survey_options = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION); $parent_survey = Database::escape_string($parent_survey); // Get groups $sql = "SELECT * from $table_survey_question_group WHERE c_id = $course_id AND survey_id='".$parent_survey."'"; $res = Database::query($sql); if (Database::num_rows($res) === 0) { return true; } $new_survey_id = intval($new_survey_id); while($row = Database::fetch_array($res, 'ASSOC')){ $sql1 = 'INSERT INTO '.$table_survey_question_group.' (c_id, name,description,survey_id) VALUES ('.$course_id.', \''.Database::escape_string($row['name']).'\',\''.Database::escape_string($row['description']).'\',\''.$new_survey_id.'\')'; Database::query($sql1); $group_id[$row['id']] = Database::insert_id(); } // Get questions $sql = "SELECT * FROM $table_survey_question WHERE c_id = $course_id AND survey_id='".$parent_survey."'"; $res = Database::query($sql); while($row = Database::fetch_array($res, 'ASSOC')){ $sql2 = 'INSERT INTO '.$table_survey_question.' (c_id, survey_id,survey_question,survey_question_comment,type,display,sort,shared_question_id,max_value,survey_group_pri,survey_group_sec1,survey_group_sec2) VALUES '. '('.$course_id.', \''.$new_survey_id.'\',\''.Database::escape_string($row['survey_question']).'\',\''.Database::escape_string($row['survey_comment']).'\',\''.$row['type'].'\',\''.$row['display'].'\',\''.$row['sort'].'\',\''.$row['shared_question_id'].'\',\''.$row['max_value']. '\',\''.$group_id[$row['survey_group_pri']].'\',\''.$group_id[$row['survey_group_sec1']].'\',\''.$group_id[$row['survey_group_sec2']].'\')'; Database::query($sql2); $question_id[$row['question_id']] = Database::insert_id(); } // Get questions options $sql = "SELECT * FROM $table_survey_options WHERE c_id = $course_id AND survey_id='".$parent_survey."'"; $res = Database::query($sql); while($row = Database::fetch_array($res ,'ASSOC')){ $sql3 = 'INSERT INTO '.$table_survey_options.' (c_id, question_id,survey_id,option_text,sort,value) VALUES ('. " $course_id , '".$question_id[$row['question_id']]."','".$new_survey_id."','".Database::escape_string($row['option_text'])."','".$row['sort']."','".$row['value']."')"; Database::query($sql3); } return true; } /** * This function duplicates a survey (and also all the question in that survey * * @param $survey_id the id of the survey that has to be duplicated * @return true * * @author Eric Marguin , Elixir Interactive * @version October 2007 */ public function empty_survey($survey_id) { // Database table definitions $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION); $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); $table_survey = Database :: get_course_table(TABLE_SURVEY); $course_id = api_get_course_int_id(); $datas = survey_manager::get_survey($survey_id); $session_where = ''; if (api_get_session_id() != 0) { $session_where = ' AND session_id = "'.api_get_session_id().'" '; } $sql = 'DELETE FROM '.$table_survey_invitation.' WHERE c_id = '.$course_id.' AND survey_code = "'.Database::escape_string($datas['code']).'" '.$session_where.' '; Database::query($sql); $sql = 'DELETE FROM '.$table_survey_answer.' WHERE c_id = '.$course_id.' AND survey_id='.intval($survey_id); Database::query($sql); $sql = 'UPDATE '.$table_survey.' SET invited=0, answered=0 WHERE c_id = '.$course_id.' AND survey_id='.intval($survey_id); Database::query($sql); return true; } /** * This function recalculates the number of people who have taken the survey (=filled at least one question) * * @param int $survey_id the id of the survey somebody * @return true * * @author Patrick Cool , Ghent University * @version February 2007 */ public static function update_survey_answered($survey_data, $user, $survey_code) { // Database table definitions $table_survey = Database :: get_course_table(TABLE_SURVEY); $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION); $survey_id = $survey_data['survey_id']; $course_id = $survey_data['c_id']; $session_id = $survey_data['session_id']; // Getting a list with all the people who have filled the survey $people_filled = survey_manager::get_people_who_filled_survey($survey_id, false, $course_id); $number = intval(count($people_filled)); // Storing this value in the survey table $sql = "UPDATE $table_survey SET answered = $number WHERE c_id = $course_id AND survey_id = ".Database::escape_string($survey_id); Database::query($sql); // Storing that the user has finished the survey. $sql = "UPDATE $table_survey_invitation SET answered='1' WHERE c_id = $course_id AND session_id='".$session_id."' AND user='".Database::escape_string($user)."' AND survey_code='".Database::escape_string($survey_code)."'"; Database::query($sql); } /** * This function gets a complete structure of a survey (all survey information, all question information * of all the questions and all the options of all the questions. * * @param integer $survey_id the id of the survey * @param boolean $shared this parameter determines if we have to get the information of a survey from the central (shared) database or from the * course database * * @author Patrick Cool , Ghent University * @version February 2007 */ public static function get_complete_survey_structure($survey_id, $shared = 0) { $structure = survey_manager::get_survey($survey_id, $shared); $structure['questions'] = survey_manager::get_questions($survey_id); } /*** * SURVEY QUESTION FUNCTIONS */ /** * This function return the "icon" of the question type * * @author Patrick Cool , Ghent University * @version February 2007 */ public static function icon_question($type) { // the possible question types $possible_types = array('personality', 'yesno', 'multiplechoice', 'multipleresponse', 'open', 'dropdown', 'comment', 'pagebreak', 'percentage', 'score'); // the images array $icon_question = array( 'yesno' => 'yesno.gif', 'personality' => 'yesno.gif', 'multiplechoice' => 'mcua.gif', 'multipleresponse' => 'mcma.gif', 'open' => 'open_answer.gif', 'dropdown' => 'dropdown.gif', 'percentage' => 'percentagequestion.gif', 'score' => 'scorequestion.gif', 'comment' => 'commentquestion.gif', 'pagebreak' => 'page_end.gif', ); if (in_array($type, $possible_types)) { return $icon_question[$type]; } else { return false; } } /** * This function retrieves all the information of a question * * @param integer $question_id the id of the question * @return array * * @author Patrick Cool , Ghent University * @version January 2007 * * @todo one sql call should do the trick */ public static function get_question($question_id, $shared = false) { // Table definitions $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); $course_id = api_get_course_int_id(); $sql = "SELECT * FROM $tbl_survey_question WHERE c_id = $course_id AND question_id='".Database::escape_string($question_id)."' ORDER BY `sort` "; $sql_survey_question_option = "SELECT * FROM $table_survey_question_option WHERE c_id = $course_id AND question_id='".Database::escape_string($question_id)."' ORDER BY `sort` "; if ($shared) { $tbl_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION); $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); $sql = "SELECT * FROM $tbl_survey_question WHERE question_id='".Database::escape_string($question_id)."' ORDER BY `sort` "; $sql_survey_question_option = "SELECT * FROM $table_survey_question_option WHERE question_id='".Database::escape_string($question_id)."' ORDER BY `sort` "; } // Getting the information of the question $result = Database::query($sql); $row = Database::fetch_array($result,'ASSOC'); $return['survey_id'] = $row['survey_id']; $return['question_id'] = $row['question_id']; $return['type'] = $row['type']; $return['question'] = $row['survey_question']; $return['horizontalvertical'] = $row['display']; $return['shared_question_id'] = $row['shared_question_id']; $return['maximum_score'] = $row['max_value']; if ($row['survey_group_pri'] != 0) { $return['assigned'] = $row['survey_group_pri']; $return['choose'] = 1; } else { $return['assigned1'] = $row['survey_group_sec1']; $return['assigned2'] = $row['survey_group_sec2']; $return['choose'] = 2; } // Getting the information of the question options $result = Database::query($sql_survey_question_option); while ($row = Database::fetch_array($result, 'ASSOC')) { /** @todo this should be renamed to options instead of answers */ $return['answers'][] = $row['option_text']; $return['values'][] = $row['value']; /** @todo this can be done more elegantly (used in reporting) */ $return['answersid'][] = $row['question_option_id']; } return $return; } /** * This function gets all the question of any given survey * * @param integer $survey_id the id of the survey * @return array containing all the questions of the survey * * @author Patrick Cool , Ghent University * @version February 2007 * * @todo one sql call should do the trick */ public static function get_questions($survey_id, $course_id = '') { // Table definitions $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); if (empty($course_id)) { $course_id = api_get_course_int_id(); } // Getting the information of the question $sql = "SELECT * FROM $tbl_survey_question WHERE c_id = $course_id AND survey_id='".Database::escape_string($survey_id)."'"; $result = Database::query($sql); while ($row = Database::fetch_array($result, 'ASSOC')) { $return[$row['question_id']]['survey_id'] = $row['survey_id']; $return[$row['question_id']]['question_id'] = $row['question_id']; $return[$row['question_id']]['type'] = $row['type']; $return[$row['question_id']]['question'] = $row['survey_question']; $return[$row['question_id']]['horizontalvertical'] = $row['display']; $return[$row['question_id']]['maximum_score'] = $row['max_value']; $return[$row['question_id']]['sort'] = $row['sort']; } // Getting the information of the question options $sql = "SELECT * FROM $table_survey_question_option WHERE c_id = $course_id AND survey_id='".Database::escape_string($survey_id)."'"; $result = Database::query($sql); while ($row = Database::fetch_array($result, 'ASSOC')) { $return[$row['question_id']]['answers'][] = $row['option_text']; } return $return; } /** * This function saves a question in the database. * This can be either an update of an existing survey or storing a new survey * * @param array $form_content all the information of the form * * @author Patrick Cool , Ghent University * @version January 2007 */ public function save_question($survey_data, $form_content) { if (strlen($form_content['question']) > 1) { // Checks lenght of the question $empty_answer = false; if ($survey_data['survey_type'] == 1) { if (empty($form_content['choose'])) { $return_message = 'PleaseChooseACondition'; return $return_message; } if (($form_content['choose']==2)&&($form_content['assigned1'] == $form_content['assigned2'])) { $return_message = 'ChooseDifferentCategories'; return $return_message; } } if ($form_content['type'] != 'percentage') { for ($i = 0; $i < count($form_content['answers']); $i++) { if (strlen($form_content['answers'][$i]) < 1) { $empty_answer = true; break; } } } if ($form_content['type'] == 'score') { if (strlen($form_content['maximum_score']) < 1) { $empty_answer = true; } } $additional = array(); $course_id = api_get_course_int_id(); if (!$empty_answer) { // Table definitions $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); // Getting all the information of the survey $survey_data = survey_manager::get_survey($form_content['survey_id']); // Storing the question in the shared database if (is_numeric($survey_data['survey_share']) && $survey_data['survey_share'] != 0) { $shared_question_id = survey_manager::save_shared_question($form_content, $survey_data); $form_content['shared_question_id'] = $shared_question_id; } // Storing a new question if ($form_content['question_id'] == '' || !is_numeric($form_content['question_id'])) { // Finding the max sort order of the questions in the given survey $sql = "SELECT max(sort) AS max_sort FROM $tbl_survey_question WHERE c_id = $course_id AND survey_id='".Database::escape_string($form_content['survey_id'])."'"; $result = Database::query($sql); $row = Database::fetch_array($result,'ASSOC'); $max_sort = $row['max_sort']; // Some variables defined for survey-test type $additional['column'] = ''; $additional['value'] = ''; if ($_POST['choose'] == 1) { $additional['column'] = ',survey_group_pri'; $additional['value'] = ",'".Database::escape_string($_POST['assigned'])."'"; } elseif($_POST['choose'] == 2) { $additional['column'] = ',survey_group_sec1, survey_group_sec2'; $additional['value'] = ",'".Database::escape_string($_POST['assigned1'])."'".",'".Database::escape_string($_POST['assigned2'])."'"; } // Adding the question to the survey_question table $sql = "INSERT INTO $tbl_survey_question (c_id, survey_id,survey_question,survey_question_comment,type,display, sort, shared_question_id, max_value".$additional['column'].") VALUES ( $course_id, '".Database::escape_string($form_content['survey_id'])."', '".Database::escape_string($form_content['question'])."', '".Database::escape_string($form_content['question_comment'])."', '".Database::escape_string($form_content['type'])."', '".Database::escape_string($form_content['horizontalvertical'])."', '".Database::escape_string($max_sort+1)."', '".Database::escape_string($form_content['shared_question_id'])."', '".Database::escape_string($form_content['maximum_score'])."'". $additional['value']." )"; Database::query($sql); $question_id = Database::insert_id(); $form_content['question_id'] = $question_id; $return_message = 'QuestionAdded'; } else { // Updating an existing question $additionalsets = ''; if ($_POST['choose'] == 1) { $additionalsets = ',survey_group_pri = \''.Database::escape_string($_POST['assigned']).'\', survey_group_sec1 = \'0\', survey_group_sec2 = \'0\' '; } elseif ($_POST['choose'] == 2) { $additionalsets = ',survey_group_pri = \'0\', survey_group_sec1 = \''.Database::escape_string($_POST['assigned1']).'\', survey_group_sec2 = \''.Database::escape_string($_POST['assigned2']).'\' '; } $setadditionals = $additional['set'][1].$additional['set'][2].$additional['set'][3]; // Adding the question to the survey_question table $sql = "UPDATE $tbl_survey_question SET survey_question = '".Database::escape_string($form_content['question'])."', survey_question_comment = '".Database::escape_string($form_content['question_comment'])."', display = '".Database::escape_string($form_content['horizontalvertical'])."', max_value = '".Database::escape_string($form_content['maximum_score'])."'" . $additionalsets." WHERE c_id = $course_id AND question_id = '".Database::escape_string($form_content['question_id'])."'"; Database::query($sql); $return_message = 'QuestionUpdated'; } if (!empty($form_content['survey_id'])) { //Updating survey api_item_property_update(api_get_course_info(), TOOL_SURVEY, $form_content['survey_id'], 'SurveyUpdated', api_get_user_id()); } // Storing the options of the question $message_options=survey_manager::save_question_options($form_content, $survey_data); } else { $return_message = 'PleasFillAllAnswer'; } } else { $return_message = 'PleaseEnterAQuestion'; } return $return_message; } /** * This function saves the question in the shared database * * @param array $form_content all the information of the form * @param array $survey_data all the information of the survey * * @author Patrick Cool , Ghent University * @version February 2007 * * @todo editing of a shared question */ public function save_shared_question($form_content, $survey_data) { $_course = api_get_course_info(); // Table definitions $tbl_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION); // Storing a new question if ($form_content['shared_question_id'] == '' || !is_numeric($form_content['shared_question_id'])) { // Finding the max sort order of the questions in the given survey $sql = "SELECT max(sort) AS max_sort FROM $tbl_survey_question WHERE survey_id='".Database::escape_string($survey_data['survey_share'])."' AND code='".Database::escape_string($_course['id'])."'"; $result = Database::query($sql); $row = Database::fetch_array($result,'ASSOC'); $max_sort = $row['max_sort']; // Adding the question to the survey_question table $sql = "INSERT INTO $tbl_survey_question (survey_id, survey_question, survey_question_comment, type, display, sort, code) VALUES ( '".Database::escape_string($survey_data['survey_share'])."', '".Database::escape_string($form_content['question'])."', '".Database::escape_string($form_content['question_comment'])."', '".Database::escape_string($form_content['type'])."', '".Database::escape_string($form_content['horizontalvertical'])."', '".Database::escape_string($max_sort+1)."', '".Database::escape_string($_course['id'])."')"; Database::query($sql); $shared_question_id = Database::insert_id(); } else { // Updating an existing question // adding the question to the survey_question table $sql = "UPDATE $tbl_survey_question SET survey_question = '".Database::escape_string($form_content['question'])."', survey_question_comment = '".Database::escape_string($form_content['question_comment'])."', display = '".Database::escape_string($form_content['horizontalvertical'])."' WHERE question_id = '".Database::escape_string($form_content['shared_question_id'])."' AND code = '".Database::escape_string($_course['id'])."'"; Database::query($sql); $shared_question_id = $form_content['shared_question_id']; } return $shared_question_id; } /** * This functions moves a question of a survey up or down * * @param string $direction * @param integer $survey_question_id * @param integer $survey_id * * @author Patrick Cool , Ghent University * @version January 2007 */ public static function move_survey_question($direction, $survey_question_id, $survey_id) { // Table definition $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); $course_id = api_get_course_int_id(); if ($direction == 'moveup') { $sort = 'DESC'; } if ($direction == 'movedown') { $sort = 'ASC'; } // Finding the two questions that needs to be swapped $sql = "SELECT * FROM $table_survey_question WHERE c_id = $course_id AND survey_id='".Database::escape_string($survey_id)."' ORDER BY sort $sort"; $result = Database::query($sql); $found = false; while ($row = Database::fetch_array($result, 'ASSOC')) { if ($found) { $question_id_two = $row['question_id']; $question_sort_two = $row['sort']; $found = false; } if ($row['question_id'] == $survey_question_id) { $found = true; $question_id_one = $row['question_id']; $question_sort_one = $row['sort']; } } $sql1 = "UPDATE $table_survey_question SET sort = '".Database::escape_string($question_sort_two)."' WHERE c_id = $course_id AND question_id='".Database::escape_string($question_id_one)."'"; Database::query($sql1); $sql2 = "UPDATE $table_survey_question SET sort = '".Database::escape_string($question_sort_one)."' WHERE c_id = $course_id AND question_id='".Database::escape_string($question_id_two)."'"; Database::query($sql2); } /** * This function deletes all the questions of a given survey * This function is normally only called when a survey is deleted * * @param int $survey_id the id of the survey that has to be deleted * @return true * * @author Patrick Cool , Ghent University * @version January 2007 */ public static function delete_all_survey_questions($survey_id, $shared = false) { $course_id = api_get_course_int_id(); // Table definitions $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); $course_condition = " c_id = $course_id AND "; if ($shared) { $course_condition = ""; $table_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION); } $sql = "DELETE FROM $table_survey_question WHERE $course_condition survey_id='".Database::escape_string($survey_id)."'"; // Deleting the survey questions Database::query($sql); // Deleting all the options of the questions of the survey survey_manager::delete_all_survey_questions_options($survey_id, $shared); // Deleting all the answers on this survey survey_manager::delete_all_survey_answers($survey_id); } /** * This function deletes a survey question and all its options * * @param integer $survey_id the id of the survey * @param integer $question_id the id of the question * @param integer $shared * * @todo also delete the answers to this question * * @author Patrick Cool , Ghent University * @version March 2007 */ public static function delete_survey_question($survey_id, $question_id, $shared = false) { $course_id = api_get_course_int_id(); // Table definitions $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION); if ($shared) { survey_manager::delete_shared_survey_question($survey_id, $question_id); } // Deleting the survey questions $sql = "DELETE FROM $table_survey_question WHERE c_id = $course_id AND survey_id='".Database::escape_string($survey_id)."' AND question_id='".Database::escape_string($question_id)."'"; Database::query($sql); // Deleting the options of the question of the survey survey_manager::delete_survey_question_option($survey_id, $question_id, $shared); } /** * This function deletes a shared survey question from the main database and all its options * * @param int $question_id the id of the question * @param int $shared * * @todo delete all the options of this question * * @author Patrick Cool , Ghent University * @version March 2007 */ public static function delete_shared_survey_question($survey_id, $question_id) { // Table definitions $table_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION); $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); // First we have to get the shared_question_id $question_data = survey_manager::get_question($question_id); // Deleting the survey questions $sql = "DELETE FROM $table_survey_question WHERE question_id='".Database::escape_string($question_data['shared_question_id'])."'"; Database::query($sql); // Deleting the options of the question of the survey question $sql = "DELETE FROM $table_survey_question_option WHERE question_id='".Database::escape_string($question_data['shared_question_id'])."'"; Database::query($sql); } /** * SURVEY QUESTION OPTIONS FUNCTIONS */ /** * This function stores the options of the questions in the table * * @param array $form_content * @author Patrick Cool , Ghent University * @version January 2007 * * @todo writing the update statement when editing a question */ public function save_question_options($form_content, $survey_data) { $course_id = api_get_course_int_id(); // A percentage question type has options 1 -> 100 if ($form_content['type'] == 'percentage') { for($i = 1; $i < 101; $i++) { $form_content['answers'][] = $i; } } if (is_numeric($survey_data['survey_share']) && $survey_data['survey_share'] != 0) { survey_manager::save_shared_question_options($form_content, $survey_data); } // Table definition $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); // We are editing a question so we first have to remove all the existing options from the database if (is_numeric($form_content['question_id'])) { $sql = "DELETE FROM $table_survey_question_option WHERE c_id = $course_id AND question_id = '".Database::escape_string($form_content['question_id'])."'"; Database::query($sql); } $counter = 1; if (is_array($form_content['answers'])) { //foreach ($form_content['answers'] as $key => $answer) { for ($i = 0; $i < count($form_content['answers']); $i++) { $sql = "INSERT INTO $table_survey_question_option (c_id, question_id, survey_id, option_text, value,sort) VALUES ( $course_id, '".Database::escape_string($form_content['question_id'])."', '".Database::escape_string($form_content['survey_id'])."', '".Database::escape_string($form_content['answers'][$i])."', '".Database::escape_string($form_content['values'][$i])."', '".Database::escape_string($counter)."')"; Database::query($sql); $counter++; } } } /** * This function stores the options of the questions in the shared table * * @param array $form_content * * @author Patrick Cool , Ghent University * @version February 2007 * * @todo writing the update statement when editing a question */ public function save_shared_question_options($form_content, $survey_data) { if (is_array($form_content) && is_array($form_content['answers'])) { // Table defintion $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); // We are editing a question so we first have to remove all the existing options from the database $sql = "DELETE FROM $table_survey_question_option WHERE question_id = '".Database::escape_string($form_content['shared_question_id'])."'"; Database::query($sql); $counter = 1; foreach ($form_content['answers'] as & $answer) { $sql = "INSERT INTO $table_survey_question_option (question_id, survey_id, option_text, sort) VALUES ( '".Database::escape_string($form_content['shared_question_id'])."', '".Database::escape_string($survey_data['is_shared'])."', '".Database::escape_string($answer)."', '".Database::escape_string($counter)."')"; Database::query($sql); $counter++; } } } /** * This function deletes all the options of the questions of a given survey * This function is normally only called when a survey is deleted * * @param $survey_id the id of the survey that has to be deleted * @return true * * @author Patrick Cool , Ghent University * @version January 2007 */ static function delete_all_survey_questions_options($survey_id, $shared = false) { // Table definitions $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); $course_id = api_get_course_int_id(); $course_condition = " c_id = $course_id AND "; if ($shared) { $course_condition = ""; $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); } $sql = "DELETE FROM $table_survey_question_option WHERE $course_condition survey_id='".Database::escape_string($survey_id)."'"; // Deleting the options of the survey questions Database::query($sql); return true; } /** * This function deletes the options of a given question * * @param int $survey_id * @param int $question_id * @param int $shared * @return bool * * @author Patrick Cool , Ghent University * @author Julio Montoya * @version March 2007 */ public static function delete_survey_question_option($survey_id, $question_id, $shared = false) { $course_id = api_get_course_int_id(); $course_condition = " c_id = $course_id AND "; // Table definitions $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION); if ($shared) { $course_condition = ""; $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); } // Deleting the options of the survey questions $sql = "DELETE from $table_survey_question_option WHERE $course_condition survey_id='".Database::escape_string($survey_id)."' AND question_id='".Database::escape_string($question_id)."'"; Database::query($sql); return true; } /** * SURVEY ANSWERS FUNCTIONS */ /** * This function deletes all the answers anyone has given on this survey * This function is normally only called when a survey is deleted * * @param $survey_id the id of the survey that has to be deleted * @return true * * @todo write the function * * @author Patrick Cool , Ghent University * @version January 2007,december 2008 */ public static function delete_all_survey_answers($survey_id) { $course_id = api_get_course_int_id(); $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); $survey_id = intval($survey_id); Database::query("DELETE FROM $table_survey_answer WHERE c_id = $course_id AND survey_id=$survey_id"); return true; } /** * @param int $user_id * @param int $survey_id * @param int $course_id * @return bool */ public static function is_user_filled_survey($user_id, $survey_id, $course_id) { $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); $user_id = intval($user_id); $course_id = intval($course_id); $survey_id = intval($survey_id); $sql = "SELECT DISTINCT user FROM $table_survey_answer WHERE c_id = $course_id AND user = $user_id AND survey_id = $survey_id"; $result = Database::query($sql); if (Database::num_rows($result)) { return true; } return false; } /** * This function gets all the persons who have filled the survey * * @param integer $survey_id * @return array * * @author Patrick Cool , Ghent University * @version February 2007 */ static function get_people_who_filled_survey($survey_id, $all_user_info = false, $course_id = null) { // Database table definition $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER); $table_user = Database :: get_main_table(TABLE_MAIN_USER); // Variable initialisation $return = array(); if (empty($course_id)) { $course_id = api_get_course_int_id(); } else { $course_id = intval($course_id); } if ($all_user_info) { $order_clause = api_sort_by_first_name() ? ' ORDER BY user.firstname, user.lastname' : ' ORDER BY user.lastname, user.firstname'; $sql = "SELECT DISTINCT answered_user.user as invited_user, user.firstname, user.lastname, user.user_id FROM $table_survey_answer answered_user LEFT JOIN $table_user as user ON answered_user.user = user.user_id WHERE answered_user.c_id = $course_id AND survey_id= '".Database::escape_string($survey_id)."' ". $order_clause; } else { $sql = "SELECT DISTINCT user FROM $table_survey_answer WHERE c_id = $course_id AND survey_id= '".Database::escape_string($survey_id)."' "; } $res = Database::query($sql); while ($row = Database::fetch_array($res, 'ASSOC')) { if ($all_user_info) { $return[] = $row; } else { $return[] = $row['user']; } } return $return; } static function survey_generation_hash_available() { if (extension_loaded('mcrypt')) { return true; } return false; } static function generate_survey_hash($survey_id, $course_id, $session_id, $group_id) { $hash = hash('sha512', api_get_security_key().'_'.$course_id.'_'.$session_id.'_'.$group_id.'_'.$survey_id); return $hash; } static function validate_survey_hash($survey_id, $course_id, $session_id, $group_id, $hash) { $survey_generated_hash = self::generate_survey_hash($survey_id, $course_id, $session_id, $group_id); if ($survey_generated_hash == $hash) { return true; } return false; } static function generate_survey_link($survey_id, $course_id, $session_id, $group_id) { $code = self::generate_survey_hash($survey_id, $course_id, $session_id, $group_id); return api_get_path(WEB_CODE_PATH).'survey/link.php?h='.$code.'&i='.$survey_id.'&c='.intval($course_id).'&s='.intval($session_id).'&g='.$group_id; } } class survey_question { // The html code of the form public $html; /** * This function does the generic part of any survey question: the question field * * @author Patrick Cool , Ghent University * @version January 2007 * * @todo the form_text has to become a wysiwyg editor or adding a question_comment field * @todo consider adding a question_comment form element */ function create_form($survey_data, $form_content) { $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : null; $questionId = isset($_GET['question_id']) ? Security::remove_XSS($_GET['question_id']) : null; $tool_name = Display::return_icon( survey_manager::icon_question(Security::remove_XSS($_GET['type'])), get_lang(ucfirst(Security::remove_XSS($_GET['type']))), array('align' => 'middle', 'height' => '22px') ).' '; if ($action == 'add') { $tool_name .= get_lang('AddQuestion'); } if ($action == 'edit') { $tool_name .= get_lang('EditQuestion'); } if ($_GET['type'] == 'yesno') { $tool_name .= ': '.get_lang('YesNo'); } else if ($_GET['type'] == 'multiplechoice') { $tool_name .= ': '.get_lang('UniqueSelect'); } else { $tool_name .= ': '.get_lang(api_ucfirst(Security::remove_XSS($_GET['type']))); } $url = api_get_self().'?action='.$action.'&type='.Security::remove_XSS($_GET['type']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&question_id='.$questionId; $this->html .= '
'; $this->html .= ''.$tool_name.''; $this->html .= ' '; $this->html .= ' '; $this->html .= ' '; $this->html .= ' '; // question field $this->html .= '
'; $this->html .= ' '; $this->html .= '
'; $this->html .= api_return_html_area('question', Security::remove_XSS(stripslashes($form_content['question']), STUDENT), '', '', null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120')); $this->html .= '
'; $this->html .= '
'; if ($survey_data['survey_type'] == 1) { $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP); $sql = 'SELECT id,name FROM '.$table_survey_question_group.' WHERE survey_id = '.(int)$_GET['survey_id'].' ORDER BY name'; $rs = Database::query($sql); while ($row = Database::fetch_array($rs, 'NUM')) { $glist .= ''; } $grouplist = $grouplist1 = $grouplist2 = $glist; if (!empty($form_content['assigned'])) { $grouplist = str_replace('