Prechádzať zdrojové kódy

Add mandatory surveys to course

Angel Fernando Quiroz Campos 7 rokov pred
rodič
commit
829a93c966

+ 4 - 0
main/inc/local.inc.php

@@ -2,6 +2,7 @@
 /* For licensing terms, see /license.txt */
 
 use ChamiloSession as Session;
+use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
 
 /**
  *
@@ -1529,3 +1530,6 @@ if ((isset($cas_login) && $cas_login && exist_firstpage_parameter()) ||
 
 Redirect::session_request_uri($logging_in, $user_id);
 
+if (!ChamiloApi::isAjaxRequest()) {
+    SurveyManager::protectByMandatory();
+}

+ 6 - 0
main/survey/create_new_survey.php

@@ -158,6 +158,9 @@ $form->addElement('select', 'visible_results', get_lang('ResultsVisibility'), $v
 $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));
 
+$extraField = new ExtraField('survey');
+$extraField->addElements($form, $survey_id);
+
 // Additional Parameters
 $form->addButtonAdvancedSettings('advanced_params');
 $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
@@ -271,6 +274,9 @@ if ($form->validate()) {
     // Storing the survey
     $return = SurveyManager::store_survey($values);
 
+    $extraFieldValue = new ExtraFieldValue('survey');
+    $extraFieldValue->saveFieldValues($values);
+
     // Redirecting to the survey page (whilst showing the return message)
     header('location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$return['id'].'&'.api_get_cidreq());
     exit;

+ 2 - 0
main/survey/fillsurvey.php

@@ -16,6 +16,8 @@ if (!isset($_GET['cidReq'])) {
     $_cid = $_GET['cidReq'];
 }
 
+$fillingSurvey = true;
+
 // Including the global initialization file
 require_once __DIR__.'/../inc/global.inc.php';
 

+ 67 - 0
main/survey/survey.lib.php

@@ -1,6 +1,9 @@
 <?php
 /* For licensing terms, see /license.txt */
 
+use Chamilo\CourseBundle\Entity\CSurveyInvitation,
+    Doctrine\Common\Collections\Criteria;
+
 /**
  * Class SurveyManager
  * @package chamilo.survey
@@ -1723,4 +1726,68 @@ class SurveyManager
         $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;
     }
+
+    /**
+     * Check if the current user has mandatory surveys no-answered
+     * and redirect to fill the first found survey
+     */
+    public static function protectByMandatory()
+    {
+        if (isset($GLOBALS['fillingSurvey']) && $GLOBALS['fillingSurvey']) {
+            return;
+        }
+
+        $userId = api_get_user_id();
+        $courseId = api_get_course_int_id();
+        $sessionId = api_get_session_id();
+
+        if (!$userId) {
+            return;
+        }
+
+        if (!$courseId) {
+            return;
+        }
+
+        try {
+            /** @var CSurveyInvitation $invitation */
+            $invitation = Database::getManager()
+                ->createQuery("
+                    SELECT i FROM ChamiloCourseBundle:CSurveyInvitation i
+                    INNER JOIN ChamiloCourseBundle:CSurvey s WITH i.surveyCode = s.code
+                    WHERE i.answered = 0
+                        AND i.cId = :course
+                        AND i.user = :user
+                        AND i.sessionId = :session
+                        AND :now BETWEEN s.availFrom AND s.availTill
+                    ORDER BY i.invitationDate ASC
+                ")
+                ->setMaxResults(1)
+                ->setParameters([
+                    'course' => $courseId,
+                    'user' => $userId,
+                    'session' => $sessionId,
+                    'now' => new DateTime('UTC', new DateTimeZone('UTC'))
+                ])
+                ->getSingleResult();
+        } catch (Exception $e) {
+            $invitation = null;
+        }
+
+        if (!$invitation) {
+            return;
+        }
+
+        $urlParams = http_build_query([
+            'course' => api_get_course_id(),
+            'invitationcode' => $invitation->getInvitationCode()
+        ]);
+
+        Display::addFlash(
+            Display::return_message(get_lang('MandatorySurveyNoAnswered'), 'warning')
+        );
+
+        header('Location: '.api_get_path(WEB_CODE_PATH).'survey/fillsurvey.php?'.$urlParams);
+        exit;
+    }
 }

+ 11 - 0
src/Chamilo/CoreBundle/Component/Utils/ChamiloApi.php

@@ -193,4 +193,15 @@ class ChamiloApi
         }
         return Session::read('_real_cid', 0);
     }
+
+    /**
+     * Check if the current HTTP request is by AJAX
+     * @return bool
+     */
+    public static function isAjaxRequest()
+    {
+        $requestedWith = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? $_SERVER['HTTP_X_REQUESTED_WITH'] : null;
+
+        return $requestedWith === 'XMLHttpRequest';
+    }
 }