Przeglądaj źródła

Integrate is_mandatory extra field as a survey field - refs BT#12915

Angel Fernando Quiroz Campos 7 lat temu
rodzic
commit
4a27fee9a5

+ 6 - 0
app/Migrations/Schema/V200/Version200.php

@@ -217,6 +217,12 @@ class Version200 extends AbstractMigrationChamilo
                 "ALTER TABLE sys_announcement ADD COLUMN visible_boss INT DEFAULT 0;"
             );
         }
+
+        $cSurvey = $schema->getTable('c_survey');
+
+        if (!$cSurvey->hasColumn('is_mandatory')) {
+            $cSurvey->addColumn('is_mandatory', Type::BOOLEAN)->setDefault(false);
+        }
     }
 
     /**

+ 76 - 0
app/Migrations/Schema/V200/Version20170627122900.php

@@ -0,0 +1,76 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+namespace Application\Migrations\Schema\V200;
+
+use Application\Migrations\AbstractMigrationChamilo,
+    Doctrine\DBAL\Schema\Schema,
+    Chamilo\CoreBundle\Entity\ExtraField,
+    Chamilo\CourseBundle\Entity\CSurvey;
+
+/**
+ * Class Version20170627122900
+ * @package Application\Migrations\Schema\V200
+ */
+class Version20170627122900 extends AbstractMigrationChamilo
+{
+
+    /**
+     * @param \Doctrine\DBAL\Schema\Schema $schema
+     */
+    public function up(Schema $schema)
+    {
+        if (!api_get_configuration_value('survey_answered_at_field')) {
+            return;
+        }
+
+        $em = $this->getEntityManager();
+
+        /** @var ExtraField $extraField */
+        $extraField = $em
+            ->getRepository('ChamiloCoreBundle:ExtraField')
+            ->findOneBy([
+                'variable' => 'is_mandatory',
+                'extraFieldType' => ExtraField::SURVEY_FIELD_TYPE
+            ]);
+
+        if (!$extraField) {
+            return;
+        }
+
+        $surveys = $em
+            ->createQuery('
+                SELECT s FROM ChamiloCourseBundle:CSurvey s
+                INNER JOIN ChamiloCoreBundle:ExtraFieldValues efv WITH s.iid = efv.item_id
+                INNER JOIN ChamiloCoreBundle:ExtraField ef WITH efv.field = ef
+                WHERE ef.variable = :variable
+                    AND ef.extraFieldType = :ef_type
+                    AND efv.value = 1
+            ')
+            ->setParameters([
+                'variable' => 'is_mandatory',
+                'ef_type' => ExtraField::SURVEY_FIELD_TYPE
+            ])
+            ->getResult();
+
+        if (!$surveys) {
+            return;
+        }
+
+        /** @var CSurvey $survey */
+        foreach ($surveys as $survey) {
+            $survey->setIsMandatory(true);
+
+            $em->persist($survey);
+        }
+
+        $em->flush();
+    }
+
+    /**
+     * @param \Doctrine\DBAL\Schema\Schema $schema
+     */
+    public function down(Schema $schema)
+    {
+    }
+}

+ 1 - 1
main/inc/local.inc.php

@@ -1526,6 +1526,6 @@ if ((isset($cas_login) && $cas_login && exist_firstpage_parameter()) ||
 
 Redirect::session_request_uri($logging_in, $user_id);
 
-if (!ChamiloApi::isAjaxRequest() && api_get_configuration_value('survey_answered_at_field')) {
+if (!ChamiloApi::isAjaxRequest()) {
     SurveyManager::protectByMandatory();
 }

+ 0 - 10
main/install/configuration.dist.php

@@ -383,13 +383,3 @@ $_configuration['agenda_legend'] = [
 // $_configuration['document_pdf_orientation'] = 'landscape'; // It can be 'portrait' or 'landscape'
 // Use alternative footer when exporting document to PDF
 //$_configuration['use_alternative_document_pdf_footer'] = false;
-//
-// ------ Survey configuration settings
-// Add support to mandatory surveys. The user will not be able to enter to the course until fill the mandatory surveys
-// Requires DB change:
-/*
-INSERT INTO extra_field (extra_field_type, field_type, variable, display_text, visible_to_self, changeable, created_at)
-VALUES (12, 13, 'is_mandatory', 'IsMandatory', 1, 1, NOW());
-*/
-//$_configuration['allow_mandatory_survey'] = false;
-// ------

+ 1 - 7
main/survey/create_new_survey.php

@@ -157,10 +157,7 @@ $form->addElement('select', 'visible_results', get_lang('ResultsVisibility'), $v
 //$defaults['visible_results'] = 0;
 $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);
-
+$form->addCheckBox('is_mandatory', get_lang('IsMandatory'), get_lang('Yes'));
 // Additional Parameters
 $form->addButtonAdvancedSettings('advanced_params');
 $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
@@ -274,9 +271,6 @@ 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;

+ 5 - 7
main/survey/survey.lib.php

@@ -318,7 +318,8 @@ class SurveyManager
                 'creation_date' => api_get_utc_datetime(),
                 'anonymous' => $values['anonymous'],
                 'session_id' => api_get_session_id(),
-                'visible_results' => $values['visible_results']
+                'visible_results' => $values['visible_results'],
+                'is_mandatory' => isset($values['is_mandatory'])
             ];
 
             $params = array_merge($params, $extraParams);
@@ -423,6 +424,7 @@ class SurveyManager
                 'anonymous' => $values['anonymous'],
                 'session_id' => api_get_session_id(),
                 'visible_results' => $values['visible_results'],
+                'is_mandatory' => isset($values['is_mandatory'])
             ];
 
             $params = array_merge($params, $extraParams);
@@ -1746,15 +1748,12 @@ class SurveyManager
                 ->createQuery("
                     SELECT i FROM ChamiloCourseBundle:CSurveyInvitation i
                     INNER JOIN ChamiloCourseBundle:CSurvey s WITH s.code = i.surveyCode
-                    INNER JOIN ChamiloCoreBundle:ExtraFieldValues efv WITH efv.itemId = s.iid
-                    INNER JOIN ChamiloCoreBundle:ExtraField ef WITH efv.field = ef.id
                     WHERE i.answered = 0
                         AND i.cId = :course
                         AND i.user = :user
                         AND i.sessionId = :session
                         AND :now BETWEEN s.availFrom AND s.availTill
-                        AND ef.variable = :variable
-                        AND efv.value = 1
+                        AND s.isMandatory IS TRUE
                     ORDER BY s.availTill ASC
                 ")
                 ->setMaxResults(1)
@@ -1762,8 +1761,7 @@ class SurveyManager
                     'course' => $courseId,
                     'user' => $userId,
                     'session' => $sessionId,
-                    'now' => new DateTime('UTC', new DateTimeZone('UTC')),
-                    'variable' => 'is_mandatory'
+                    'now' => new DateTime('UTC', new DateTimeZone('UTC'))
                 ])
                 ->getSingleResult();
         } catch (Exception $e) {

+ 25 - 0
src/Chamilo/CourseBundle/Entity/CSurvey.php

@@ -238,6 +238,12 @@ class CSurvey
      */
     private $visibleResults;
 
+    /**
+     * @var bool
+     * @ORM\Column(name="is_mandatory", type="boolean", options={"default":false})
+     */
+    private $isMandatory = false;
+
     /**
      * Set code
      *
@@ -927,4 +933,23 @@ class CSurvey
     {
         return $this->cId;
     }
+
+    /**
+     * @param bool $isMandatory
+     * @return CSurvey
+     */
+    public function setIsMandatory($isMandatory)
+    {
+        $this->isMandatory = $isMandatory;
+
+        return $this;
+    }
+
+    /**
+     * @return bool
+     */
+    public function isMandatory()
+    {
+        return $this->isMandatory;
+    }
 }