Yannick Warnier 14 years ago
parent
commit
e4826742e8

+ 5 - 1
main/gradebook/lib/be/linkfactory.class.php

@@ -24,6 +24,7 @@ require_once 'studentpublicationlink.class.php';
 require_once 'learnpathlink.class.php';
 require_once 'forumthreadlink.class.php';
 require_once 'attendancelink.class.php';
+require_once 'surveylink.class.php';
 
 /**
  * Factory for link objects
@@ -88,6 +89,8 @@ class LinkFactory
 				return new ForumThreadLink();
 			case LINK_ATTENDANCE:
 				return new AttendanceLink();
+			case LINK_SURVEY:
+				return new SurveyLink();
 		}
 		return null;
 	}
@@ -102,7 +105,8 @@ class LinkFactory
 					  LINK_STUDENTPUBLICATION,
 					  LINK_LEARNPATH,
                       LINK_FORUM_THREAD,
-                      LINK_ATTENDANCE
+                      LINK_ATTENDANCE,
+                      LINK_SURVEY
 					  );
 	}
 

+ 248 - 0
main/gradebook/lib/be/surveylink.class.php

@@ -0,0 +1,248 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+/**
+ * Gradebook link to a survey item
+ * @author Ivan Tcholakov <ivantcholakov@gmail.com>, 2010
+ * @package chamilo.gradebook
+ */
+
+class SurveyLink extends AbstractLink
+{
+    private $survey_table = null;
+
+    function SurveyLink() {
+    	$this->set_type(LINK_SURVEY);
+    }
+
+	public function get_name() {
+    	$this->get_survey_data();
+    	return $this->survey_data['code'].': '.self::html_to_text($this->survey_data['title']);
+    }
+
+    public function get_description() {
+    	$this->get_survey_data();
+    	return $this->survey_data['subtitle'];
+    }
+
+    public function get_type_name() {
+    	return get_lang('Survey');
+    }
+
+	public function is_allowed_to_change_name() {
+		return false;
+	}
+
+	public function needs_name_and_description() {
+		return false;
+	}
+
+    public function needs_max() {
+        return false;
+    }
+
+    public function needs_results() {
+        return false;
+    }
+
+    /**
+	 * Generates an array of all surveys available.
+	 * @return array 2-dimensional array - every element contains 2 subelements (id, name)
+	 */
+    public function get_all_links() {
+    	if (empty($this->course_code)) {
+    		die('Error in get_all_links() : course code not set');
+    	}
+    	$tbl_survey = $this->get_survey_table();
+    	$session_id = api_get_session_id();
+    	$sql = 'SELECT survey_id, title, code FROM '.$tbl_survey.' WHERE session_id = '.intval($session_id).'';
+		$result = Database::query($sql);
+		while ($data = Database::fetch_array($result)) {
+			$links[] = array($data['survey_id'], api_trunc_str($data['code'].': '.self::html_to_text($data['title']), 80));
+		};
+		return isset($links) ? $links : null;
+    }
+
+    /**
+	 * Generates an array of surveys that a teacher hasn't created a link for.
+	 * @return array 2-dimensional array - every element contains 2 subelements (id, name)
+	 */
+    public function get_not_created_links() {
+    	if (empty($this->course_code)) {
+    		die('Error in get_not_created_links() : course code not set');
+    	}
+    	$tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
+
+    	$sql = 'SELECT survey_id, title, code
+    			FROM '.$this->get_survey_table().' AS srv
+				WHERE survey_id NOT IN
+					(SELECT ref_id FROM '.$tbl_grade_links.'
+					WHERE type = '.LINK_SURVEY." AND course_code = '".$this->get_course_code()."'"
+				.') AND srv.session_id='.api_get_session_id().'';
+		$result = Database::query($sql);
+
+		$links = array();
+		while ($data = Database::fetch_array($result)) {
+			$links[] = array($data['survey_id'], api_trunc_str($data['code'].': '.self::html_to_text($data['title']), 80));
+		}
+		return $links;
+    }
+
+    /**
+     * Has anyone done this survey yet?
+     */
+    public function has_results() {
+    	$course_info = Database :: get_course_info($this->get_course_code());
+		$database_name = (empty($course_info['db_name'])) ? $course_info['dbName'] : $course_info['db_name'];
+
+		if ($database_name != '') {
+			$ref_id = intval($this->get_ref_id());
+			$session_id = api_get_session_id();
+			$tbl_survey = Database::get_course_table(TABLE_SURVEY, $database_name);
+			$tbl_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION, $database_name);
+
+			$get_individual_score = !is_null($stud_id);
+
+			$sql = 'SELECT COUNT(i.answered) FROM '.$tbl_survey.' AS s JOIN '.$tbl_survey_invitation.' AS i
+				ON s.code = i.survey_code
+				WHERE s.survey_id = '.$ref_id.' AND i.session_id = '.$session_id;
+
+			$sql_result = Database::query($sql);
+			$data = Database::fetch_array($sql_result);
+			return ($data[0] != 0);
+		}
+
+		return false;
+    }
+
+    public function calc_score($stud_id = null) {
+
+    	// Note: Max score is assumed to be always 1 for surveys,
+    	// only student's participation is to be taken into account.
+    	$max_score = 1;
+
+    	$course_info = Database :: get_course_info($this->get_course_code());
+		$database_name = (empty($course_info['db_name'])) ? $course_info['dbName'] : $course_info['db_name'];
+
+		if ($database_name != '') {
+			$ref_id = intval($this->get_ref_id());
+			$session_id = api_get_session_id();
+			$tbl_survey = Database::get_course_table(TABLE_SURVEY, $database_name);
+			$tbl_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION, $database_name);
+
+			$get_individual_score = !is_null($stud_id);
+
+			$sql = 'SELECT i.answered FROM '.$tbl_survey.' AS s JOIN '.$tbl_survey_invitation.' AS i
+				ON s.code = i.survey_code
+				WHERE s.survey_id = '.$ref_id.' AND i.session_id = '.$session_id;
+
+			if ($get_individual_score) {
+	    		$sql .= ' AND i.user = '.intval($stud_id);
+	    	}
+
+			$sql_result = Database::query($sql);
+
+			if ($get_individual_score) {
+
+				// for 1 student
+
+				if ($data = Database::fetch_array($sql_result)) {
+					return array ($data['answered'] ? $max_score : 0, $max_score);
+				}
+				return array(0, $max_score);
+
+			} else {
+
+				// for all the students -> get average
+
+				$students = array();
+				$rescount = 0;
+				$sum = 0;
+
+				while ($data = Database::fetch_array($sql_result)) {
+					$sum += $data['answered'] ? $max_score : 0;
+					$rescount++;
+				}
+				$sum = $sum / $max_score;
+
+				if ($rescount == 0) {
+					return null;
+				}
+				return array($sum, $rescount);
+			}
+		}
+
+		return null;
+    }
+
+    /**
+     * Lazy load function to get the database table of the surveys
+     */
+    private function get_survey_table() {
+    	$course_info = Database :: get_course_info($this->get_course_code());
+		$database_name = isset($course_info['db_name']) ? $course_info['db_name'] : '';
+		if ($database_name != '') {
+			if (!isset($this->survey_table)) {
+				$this->survey_table = Database :: get_course_table(TABLE_SURVEY, $database_name);
+    		}
+   			return $this->survey_table;
+		} else {
+			return '';
+		}
+    }
+
+    /**
+     * Check if this still links to a survey
+     */
+    public function is_valid_link() {
+    	$session_id = api_get_session_id();
+        $sql = 'SELECT count(survey_id) FROM '.$this->get_survey_table().'
+        		 WHERE survey_id = '.intval($this->get_ref_id()).' AND session_id='.intval($session_id).'';
+        $result = Database::query($sql);
+        $number = Database::fetch_row($result);
+        return ($number[0] != 0);
+    }
+
+    public function get_test_id() {
+    	return 'DEBUG:ID';
+    }
+
+    public function get_link() {
+   		if (api_is_allowed_to_create_course()) { // Let students make access only through "Surveys" tool.
+   			$tbl_name = $this->get_survey_table();
+   			$session_id = api_get_session_id();
+   			if ($tbl_name != '') {
+   				$sql = 'SELECT survey_id FROM '.$this->get_survey_table().'
+    				WHERE survey_id = '.intval($this->get_ref_id()).' AND session_id = '.intval($session_id).' ';
+   				$result = Database::query($sql);
+   				$row = Database::fetch_array($result, 'ASSOC');
+   				$survey_id = $row['survey_id'];
+   				return api_get_path(WEB_PATH).'main/survey/reporting.php?cidReq='.$this->get_course_code().'&survey_id='.$survey_id;
+   			}
+   		}
+   		return null;
+    }
+
+	private function get_survey_data() {
+		$tbl_name = $this->get_survey_table();
+		$session_id = api_get_session_id();
+		if ($tbl_name == '') {
+			return false;
+		} elseif (!isset($this->survey_data)) {
+			$sql = 'SELECT * FROM '.$tbl_name.' WHERE survey_id = '.intval($this->get_ref_id()).' AND session_id='.intval($session_id).'';
+			$query = Database::query($sql);
+			$this->survey_data = Database::fetch_array($query);
+    	}
+    	return $this->survey_data;
+    }
+
+    public function get_icon_name() {
+		return 'survey';
+	}
+
+	private static function html_to_text($string) {
+		return trim(api_html_entity_decode(strip_tags(str_ireplace(array('<p>', '</p>', '<br />', '<br/>', '<br>'), array('', ' ', ' ', ' ', ' '), $string)), ENT_QUOTES));
+	}
+
+}
+?>

+ 61 - 23
main/survey/create_new_survey.php

@@ -31,6 +31,7 @@ require_once api_get_path(LIBRARY_PATH).'course.lib.php';
 require_once api_get_path(LIBRARY_PATH).'groupmanager.lib.php';
 require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
 require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
+require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
 
 $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
 $htmlHeadXtra[] = '<script type="text/javascript" language="javascript">
@@ -57,6 +58,7 @@ $table_survey 				= Database :: get_course_table(TABLE_SURVEY);
 $table_user 				= Database :: get_main_table(TABLE_MAIN_USER);
 $table_course 				= Database :: get_main_table(TABLE_MAIN_COURSE);
 $table_course_survey_rel 	= Database :: get_main_table(TABLE_MAIN_COURSE_SURVEY);
+$table_gradebook_link 		= Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
 
 /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
 // If user is not teacher or if he's a coach trying to access an element out of his session
@@ -73,6 +75,11 @@ if (!api_is_allowed_to_edit()) {
 $survey_id = Security::remove_XSS($_GET['survey_id']);
 $survey_data = survey_manager::get_survey($survey_id);
 
+// Additional information
+$course_id = api_get_course_id();
+$session_id = api_get_session_id();
+$gradebook_link_type = 8; // LINK_SURVEY
+
 $urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40));
 if (api_strlen(strip_tags($survey_data['title'])) > 40) {
 	$urlname .= '...';
@@ -95,6 +102,14 @@ if ($_GET['action'] == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
 	$defaults = $survey_data;
 	$defaults['survey_id'] = $survey_id;
 	$defaults['anonymous'] = $survey_data['anonymous'];
+
+	$gradebook_link_id = is_resource_in_course_gradebook($course_id, $gradebook_link_type, $survey_id, $session_id);
+	if ($gradebook_link_id) {
+		if ($sql_result_array = Database::fetch_array(Database::query('SELECT weight FROM '.$table_gradebook_link.' WHERE id='.$gradebook_link_id))) {
+			$defaults['survey_qualify_gradebook'] = $gradebook_link_id;
+			$defaults['survey_weight'] = number_format($sql_result_array['weight'], 2, '.', '');
+		}
+	}
 } else {
 	$defaults['survey_language'] = $_course['language'];
 	$defaults['start_date'] = date('d-F-Y H:i');
@@ -151,14 +166,25 @@ $form->addElement('checkbox', 'anonymous', get_lang('Anonymous'));
 $form->addElement('html_editor', 'survey_introduction', get_lang('SurveyIntroduction'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false));
 $form->addElement('html_editor', 'survey_thanks', get_lang('SurveyThanks'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false));
 
-/*
 // Aditional Parameters
-$form -> addElement('html','<div class="row">
-<div class="label">&nbsp;</div>
-<div class="formw">
-	<a href="javascript://" onclick="javascript: if(document.getElementById(\'options\').style.display == \'none\'){document.getElementById(\'options\').style.display = \'block\';}else{document.getElementById(\'options\').style.display = \'none\';}"><img src="../img/add_na.gif" alt="" />'.get_lang('AdvancedParameters').'</a>
-</div>
-</div>');*/
+$form->addElement('html', '
+<div class="row">
+	<div class="label">
+		<a href="javascript: void(0);" onclick="javascript: advanced_parameters();" ><span id="plus_minus">&nbsp;'.Display::return_icon('div_show.gif',null,array('style'=>'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'</span></a>
+	</div>
+	<div class="formw">
+		&nbsp;
+	</div>
+</div>');
+
+$form -> addElement('html', '<div id="options" style="display: none;">');
+
+// An option: Qualify the fact that survey has been answered in the gradebook
+$form->addElement('checkbox', 'survey_qualify_gradebook', '', get_lang('QualifyInGradebook'), 'onclick="javascript: if (this.checked) { document.getElementById(\'gradebook_options\').style.display = \'block\'; } else { document.getElementById(\'gradebook_options\').style.display = \'none\'; }"');
+$form->addElement('html', '<div id="gradebook_options"'.($gradebook_link_id ? '' : ' style="display:none"').'>');
+$form->addElement('text', 'survey_weight', get_lang('QualifyWeight'), 'value="0.00" style="width: 40px;" onfocus="javascript: this.select();"');
+$form->applyFilter('survey_weight', 'html_filter');
+$form->addElement('html', '</div>');
 
 // Personality/Conditional Test Options
 $surveytypes[0] = get_lang('Normal');
@@ -166,7 +192,6 @@ $surveytypes[1] = get_lang('Conditional');
 
 if ($_GET['action'] == 'add') {
 	$form->addElement('hidden', 'survey_type', 0);
-    $form -> addElement('html', '<div id="options" style="display: none;">');
 	require_once api_get_path(LIBRARY_PATH).'surveymanager.lib.php';
 	$survey_tree = new SurveyTree();
 	$list_surveys = $survey_tree->createList($survey_tree->surveylist);
@@ -183,17 +208,6 @@ if ($survey_data['survey_type'] == 1 || $_GET['action'] == 'add') {
 if ((isset($_GET['action']) && $_GET['action'] == 'edit') && !empty($survey_id)) {
 	if ($survey_data['anonymous'] == 0) {
 
-		// Aditional Parameters
-		$form -> addElement('html','<div class="row">
-		<div class="label">
-			<a href="javascript: void(0);" onclick="javascript: advanced_parameters();" ><span id="plus_minus">&nbsp;'.Display::return_icon('div_show.gif',null,array('style'=>'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'</span></a>
-		</div>
-		<div class="formw">
-			&nbsp;
-		</div>
-		</div>');
-
-		$form->addElement('html', '<div id="options" style="display:none">');
 		$form->addElement('checkbox', 'show_form_profile', get_lang('ShowFormProfile'), '', 'onclick="javascript: if(this.checked==true){document.getElementById(\'options_field\').style.display = \'block\';}else{document.getElementById(\'options_field\').style.display = \'none\';}"');
 
 		if ($survey_data['show_form_profile'] == 1) {
@@ -225,12 +239,12 @@ if ((isset($_GET['action']) && $_GET['action'] == 'edit') && !empty($survey_id))
 				}
 			}
 		}
-		$form->addElement('html', '</div></div>');
+
+		$form->addElement('html', '</div>');
 	}
 }
-if ($_GET['action'] == 'add') {
-	$form -> addElement('html', '</div><br />');
-}
+
+$form -> addElement('html', '</div><br />');
 
 if (isset($_GET['survey_id']) && $_GET['action'] == 'edit') {
 	$class = 'save';
@@ -271,6 +285,7 @@ if ($form->validate()) {
 	}
 	*/
 	if ($return['type'] == 'error') {
+
 		// Displaying the header
 		Display::display_header($tool_name);
 
@@ -279,6 +294,29 @@ if ($form->validate()) {
 
 		// Display the form
 		$form->display();
+
+	} else {
+
+		$gradebook_option = $values['survey_qualify_gradebook'] > 0;
+		if ($gradebook_option) {
+			$survey_id = intval($return['id']);
+			if ($survey_id > 0) {
+
+				$title_gradebook = ''; // Not needed here.
+				$description_gradebook = ''; // Not needed here.
+				$survey_weight = floatval($_POST['survey_weight']);
+				$max_score = 1;
+				$date = time(); // TODO: Maybe time zones implementation is needed here.
+				$visible = 1;	// 1 = visible
+
+				$gradebook_link_id = is_resource_in_course_gradebook($course_id, $gradebook_link_type, $survey_id, $session_id);
+				if (!$gradebook_link_id) {
+					add_resource_to_course_gradebook($course_id, $gradebook_link_type, $survey_id, $title_gradebook, $survey_weight, $max_score, $description_gradebook, time(), 1, $session_id);
+				} else {
+					Database::query('UPDATE '.$table_gradebook_link.' SET weight='.$survey_weight.' WHERE id='.$gradebook_link_id);
+				}
+			}
+		}
 	}
 	if ($config['survey']['debug']) {
 		// Displaying a feedback message

+ 1 - 1
tests/main/inc/lib/document.lib.test.php

@@ -235,7 +235,7 @@ class TestDocumentManager extends UnitTestCase {
 		if (!headers_sent()) {	
 			$res=DocumentManager::string_send_for_download($full_string, $forced, $name);
 		}
-		$this->assertFalse(empty($res));
+		$this->assertTrue(empty($res));
 	}
 
 	/**