Browse Source

Deleting exam_password extra user field only to students see BT#5637

Julio Montoya 12 years ago
parent
commit
3f0a244bf8
4 changed files with 135 additions and 100 deletions
  1. 93 82
      main/inc/lib/extra_field_value.lib.php
  2. 1 4
      main/inc/lib/main_api.lib.php
  3. 1 0
      main/inc/lib/plugin.lib.php
  4. 40 14
      main/inc/routes.php

+ 93 - 82
main/inc/lib/extra_field_value.lib.php

@@ -1,6 +1,8 @@
 <?php
+/* For licensing terms, see /license.txt */
+
 /**
- * Declaration for the ExtraFieldValue class, managing the values in extra 
+ * Declaration for the ExtraFieldValue class, managing the values in extra
  * fields for any datatype
  * @package chamilo.library
  */
@@ -8,7 +10,8 @@
  * Class managing the values in extra fields for any datatype
  * @package chamilo.library.extrafields
  */
-class ExtraFieldValue extends Model {
+class ExtraFieldValue extends Model
+{
     public $type = null;
     public $columns = array('id', 'field_id', 'field_value', 'tms');
     public $handler_id = null;//session_id, course_code, user_id
@@ -17,7 +20,7 @@ class ExtraFieldValue extends Model {
      * @param string The type of data to which this extra field applies (user, course, session, ...)
      * @return void (or false if unmanaged datatype)
      * @assert (-1) === false
-     */ 
+     */
     public function __construct($type) {
         $this->type = $type;
         $extra_field = new ExtraField($this->type);
@@ -25,15 +28,15 @@ class ExtraFieldValue extends Model {
         switch ($this->type) {
             case 'course':
                 $this->table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
-                $this->table_handler_field = Database::get_main_table(TABLE_MAIN_COURSE_FIELD); 
+                $this->table_handler_field = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
                 break;
             case 'user':
                 $this->table = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
-                $this->table_handler_field = Database::get_main_table(TABLE_MAIN_USER_FIELD); 
+                $this->table_handler_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
                 break;
             case 'session':
                 $this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
-                $this->table_handler_field = Database::get_main_table(TABLE_MAIN_SESSION_FIELD); 
+                $this->table_handler_field = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
                 break;
             default:
                 //unmanaged datatype, return false to let the caller know it
@@ -43,11 +46,11 @@ class ExtraFieldValue extends Model {
         $this->columns[] = $this->handler_id;
     }
     /**
-     * Gets the number of values stored in the table (all fields together) 
+     * Gets the number of values stored in the table (all fields together)
      * for this type of resource
      * @return integer Number of rows in the table
      * @assert () !== false
-     */ 
+     */
     public function get_count() {
         $row = Database::select('count(*) as count', $this->table, array(), 'first');
         return $row['count'];
@@ -57,29 +60,30 @@ class ExtraFieldValue extends Model {
      * @param array  Structured parameter for the insertion into the *_field_values table
      * @return mixed false on empty params, void otherwise
      * @assert (array()) === false
-     */ 
+     */
     public function save_field_values($params) {
         $extra_field = new ExtraField($this->type);
         if (empty($params[$this->handler_id])) {
-            return false; 
+            return false;
         }
- 
-        //Parse params 
+
+        //Parse params
         foreach ($params as $key => $value) {
             if (substr($key, 0, 6) == 'extra_') { //an extra field
                 $field_variable = substr($key, 6);
-                $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($field_variable); 
-                if ($extra_field_info) { 
+                $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($field_variable);
+                if ($extra_field_info) {
                     $new_params = array(
                         $this->handler_id   => $params[$this->handler_id],
                         'field_id'          => $extra_field_info['id'],
                         'field_value'       => $value
-                    ); 
+                    );
                     self::save($new_params);
                 }
             }
-        } 
+        }
     }
+
     /**
      * Save values in the *_field_values table
      * @param array Structured array with the values to save
@@ -87,24 +91,25 @@ class ExtraFieldValue extends Model {
      * @result mixed The result sent from the parent method
      * @assert (array()) === false
      */
-    public function save($params, $show_query = false) { 
+    public function save($params, $show_query = false)
+    {
         $extra_field = new ExtraField($this->type);
- 
+
         //Setting value to insert
-        $value = $params['field_value']; 
- 
+        $value = $params['field_value'];
+
         $value_to_insert = null;
- 
+
         if (is_array($value)) {
-            $value_to_insert = implode(';', $value); 
+            $value_to_insert = implode(';', $value);
         } else {
             $value_to_insert = Database::escape_string($value);
-        } 
+        }
         $params['field_value'] = $value_to_insert;
- 
+
         //If field id exists
         $extra_field_info = $extra_field->get($params['field_id']);
- 
+
         if ($extra_field_info) {
             switch ($extra_field_info['field_type']) {
                 case ExtraField::FIELD_TYPE_TAG :
@@ -112,8 +117,8 @@ class ExtraFieldValue extends Model {
                 case ExtraField::FIELD_TYPE_RADIO:
                 case ExtraField::FIELD_TYPE_SELECT:
                 case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
-                    //$field_options = $session_field_option->get_field_options_by_field($params['field_id']); 
-					//$params['field_value'] = split(';', $value_to_insert); 
+                    //$field_options = $session_field_option->get_field_options_by_field($params['field_id']);
+					//$params['field_value'] = split(';', $value_to_insert);
                /*
                    if ($field_options) {
                        $check = false;
@@ -134,55 +139,56 @@ class ExtraFieldValue extends Model {
                 case ExtraField::FIELD_TYPE_TEXTAREA:
                     break;
                 case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
-                    if (is_array($value)) { 
-                        if (isset($value['extra_'.$extra_field_info['field_variable']]) && 
+                    if (is_array($value)) {
+                        if (isset($value['extra_'.$extra_field_info['field_variable']]) &&
                             isset($value['extra_'.$extra_field_info['field_variable'].'_second'])
                              ) {
-                            $value_to_insert = $value['extra_'.$extra_field_info['field_variable']].'::'.$value['extra_'.$extra_field_info['field_variable'].'_second']; 
+                            $value_to_insert = $value['extra_'.$extra_field_info['field_variable']].'::'.$value['extra_'.$extra_field_info['field_variable'].'_second'];
                         } else {
                             $value_to_insert = null;
                         }
                     }
                 default:
                     break;
-            } 
-            $field_values = self::get_values_by_handler_and_field_id($params[$this->handler_id], $params['field_id']); 
+            }
+            $field_values = self::get_values_by_handler_and_field_id($params[$this->handler_id], $params['field_id']);
             if ($field_values) {
-                self::delete_values_by_handler_and_field_id($params[$this->handler_id], $params['field_id']); 
-            } 
+                self::delete_values_by_handler_and_field_id($params[$this->handler_id], $params['field_id']);
+            }
             $params['field_value'] = $value_to_insert;
-            $params['tms'] = api_get_utc_datetime(); 
+            $params['tms'] = api_get_utc_datetime();
             return parent::save($params, $show_query);
-        } 
+        }
     }
- 
+
     /**
      * Returns the value of the given extra field on the given resource
      * @param int Item ID (It could be a session_id, course_id or user_id)
      * @param int Field ID (the ID from the *_field table)
      * @param bool Whether to transform the result to a human readable strings
      * @return mixed A structured array with the field_id and field_value, or fals on error
-     * @assert (-1,-1) === false 
+     * @assert (-1,-1) === false
      */
-    public function get_values_by_handler_and_field_id($item_id, $field_id, $transform = false) {
+    public function get_values_by_handler_and_field_id($item_id, $field_id, $transform = false)
+    {
         $field_id = intval($field_id);
         $item_id = Database::escape_string($item_id);
- 
-        $sql = "SELECT s.*, field_type FROM {$this->table} s 
+
+        $sql = "SELECT s.*, field_type FROM {$this->table} s
                 INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
-                WHERE {$this->handler_id} = '$item_id'  AND 
-                      field_id = '".$field_id."' 
-                ORDER BY id"; 
-        $result = Database::query($sql); 
-        if (Database::num_rows($result)) { 
-            $result = Database::fetch_array($result, 'ASSOC'); 
+                WHERE {$this->handler_id} = '$item_id'  AND
+                      field_id = '".$field_id."'
+                ORDER BY id";
+        $result = Database::query($sql);
+        if (Database::num_rows($result)) {
+            $result = Database::fetch_array($result, 'ASSOC');
             if ($transform) {
                 if (!empty($result['field_value'])) {
                     switch ($result['field_type']) {
                         case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
- 
+
                             $field_option = new ExtraFieldOption($this->type);
-                            $options = explode('::', $result['field_value']); 
+                            $options = explode('::', $result['field_value']);
                             // only available for PHP 5.4  :( $result['field_value'] = $field_option->get($options[0])['id'].' -> ';
                             $result = $field_option->get($options[0]);
                             $result_second = $field_option->get($options[1]);
@@ -194,18 +200,19 @@ class ExtraFieldValue extends Model {
                         case ExtraField::FIELD_TYPE_SELECT:
                             $field_option = new ExtraFieldOption($this->type);
                             $extra_field_option_result = $field_option->get_field_option_by_field_and_option($result['field_id'], $result['field_value']);
-                            if (isset($extra_field_option_result[0])) { 
-                                $result['field_value'] = $extra_field_option_result[0]['option_display_text']; 
-                            } 
+                            if (isset($extra_field_option_result[0])) {
+                                $result['field_value'] = $extra_field_option_result[0]['option_display_text'];
+                            }
                             break;
                     }
                 }
-            } 
+            }
             return $result;
         } else {
             return false;
         }
     }
+
     /**
      * Gets a structured array of the original item and its extra values, using
      * a specific original item and a field name (like "branch", or "birthdate")
@@ -213,25 +220,25 @@ class ExtraFieldValue extends Model {
      * @param string The name of the field we are looking for
      * @return mixed Array of results, or false on error or not found
      * @assert (-1,'') === false
-     */ 
-    public function get_values_by_handler_and_field_variable($item_id, $field_variable, $transform = false) {
-        $field_id = intval($field_id);
+     */
+    public function get_values_by_handler_and_field_variable($item_id, $field_variable, $transform = false)
+    {
         $item_id = Database::escape_string($item_id);
         $field_variable = Database::escape_string($field_variable);
- 
-        $sql = "SELECT s.*, field_type FROM {$this->table} s 
+
+        $sql = "SELECT s.*, field_type FROM {$this->table} s
                 INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
-                WHERE   {$this->handler_id} = '$item_id'  AND 
-                        field_variable = '".$field_variable."' 
-                ORDER BY id"; 
-        $result = Database::query($sql); 
-        if (Database::num_rows($result)) { 
-            $result = Database::fetch_array($result, 'ASSOC'); 
+                WHERE   {$this->handler_id} = '$item_id'  AND
+                        field_variable = '".$field_variable."'
+                ORDER BY id";
+        $result = Database::query($sql);
+        if (Database::num_rows($result)) {
+            $result = Database::fetch_array($result, 'ASSOC');
             if ($transform) {
                 if ($result['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
                     if (!empty($result['field_value'])) {
                         $field_option = new ExtraFieldOption($this->type);
-                        $options = explode('::', $result['field_value']); 
+                        $options = explode('::', $result['field_value']);
                         // only available for PHP 5.4  :( $result['field_value'] = $field_option->get($options[0])['id'].' -> ';
                         $result = $field_option->get($options[0]);
                         $result_second = $field_option->get($options[1]);
@@ -239,7 +246,7 @@ class ExtraFieldValue extends Model {
                             $result['field_value'] = $result['option_display_text'].' -> ';
                             $result['field_value'] .= $result_second['option_display_text'];
                         }
-                    } 
+                    }
                 }
             }
             return $result;
@@ -255,19 +262,19 @@ class ExtraFieldValue extends Model {
      * @return mixed Give the ID if found, or false on failure or not found
      * @assert (-1,-1) === false
      */
-    public function get_item_id_from_field_variable_and_field_value($field_variable, $field_value, $transform = false) { 
+    public function get_item_id_from_field_variable_and_field_value($field_variable, $field_value, $transform = false) {
         $field_value = Database::escape_string($field_value);
         $field_variable = Database::escape_string($field_variable);
- 
+
         $sql = "SELECT {$this->handler_id} FROM {$this->table} s
                 INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
-                WHERE  field_value  = '$field_value'  AND 
-                       field_variable = '".$field_variable."' 
-                "; 
+                WHERE  field_value  = '$field_value'  AND
+                       field_variable = '".$field_variable."'
+                ";
 
-        $result = Database::query($sql); 
-        if ($result !== false && Database::num_rows($result)) { 
-            $result = Database::fetch_array($result, 'ASSOC'); 
+        $result = Database::query($sql);
+        if ($result !== false && Database::num_rows($result)) {
+            $result = Database::fetch_array($result, 'ASSOC');
             return $result;
         } else {
             return false;
@@ -279,12 +286,13 @@ class ExtraFieldValue extends Model {
      * @return mixed Array of values on success, false on failure or not found
      * @assert (-1) === false
      */
-    public function get_values_by_field_id($field_id) { 
+    public function get_values_by_field_id($field_id)
+    {
         $sql = "SELECT s.*, field_type FROM {$this->table} s INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
                 WHERE field_id = '".$field_id."' ORDER BY id";
-        $result = Database::query($sql); 
-        if (Database::num_rows($result)) { 
-            return Database::store_result($result, 'ASSOC'); 
+        $result = Database::query($sql);
+        if (Database::num_rows($result)) {
+            return Database::store_result($result, 'ASSOC');
         }
         return false;
     }
@@ -297,8 +305,9 @@ class ExtraFieldValue extends Model {
     public function delete_all_values_by_field_id($field_id) {
         $field_id = intval($field_id);
         $sql = "DELETE FROM  {$this->table} WHERE field_id = $field_id";
-        Database::query($sql); 
+        Database::query($sql);
     }
+
     /**
      * Deletes values of a specific field for a specific item
      * @param int Item ID (session id, course id, etc)
@@ -306,11 +315,12 @@ class ExtraFieldValue extends Model {
      * @return void
      * @assert (-1,-1) == null
      */
-    public function delete_values_by_handler_and_field_id($item_id, $field_id) {
+    public function delete_values_by_handler_and_field_id($item_id, $field_id)
+    {
         $field_id = intval($field_id);
         $item_id = Database::escape_string($item_id);
         $sql = "DELETE FROM {$this->table} WHERE {$this->handler_id} = '$item_id' AND field_id = '".$field_id."' ";
-        Database::query($sql); 
+        Database::query($sql);
     }
     /**
      * Not yet implemented - Compares the field values of two items
@@ -318,6 +328,7 @@ class ExtraFieldValue extends Model {
      * @param int Item 2
      * @return mixed Differential array generated from the comparison
      */
-    public function compare_item_values($item_id, $item_to_compare) { 
+    public function compare_item_values($item_id, $item_to_compare)
+    {
     }
 }

+ 1 - 4
main/inc/lib/main_api.lib.php

@@ -6478,7 +6478,6 @@ function api_set_settings_and_plugins() {
     }
 
     $result = api_get_settings('Plugins', 'list', $access_url_id);
-    $_plugins = array();
     foreach ($result as & $row) {
         $key = & $row['variable'];
         if (is_string($_setting[$key])) {
@@ -6487,7 +6486,6 @@ function api_set_settings_and_plugins() {
         $_setting[$key][] = $row['selected_value'];
         $_plugins[$key][] = $row['selected_value'];
     }
-    //global $app;
     $_SESSION['_setting'] = $_setting;
     $_SESSION['_plugins'] = $_plugins;
 }
@@ -6825,7 +6823,6 @@ function api_get_language_interface() {
 
     $user_language = api_get_user_language();
     $_course = api_get_course_info();
-
     $language_interface = 'english';
 
     if (!empty($valid_languages)) {
@@ -6833,7 +6830,7 @@ function api_get_language_interface() {
         if (!in_array($user_language, $valid_languages['folder'])) {
             $user_language = api_get_setting('platformLanguage');
         }
-
+        /* @todo fix the language priority feature */
         $language_priority1 = api_get_setting('languagePriority1');
         $language_priority2 = api_get_setting('languagePriority2');
         $language_priority3 = api_get_setting('languagePriority3');

+ 1 - 0
main/inc/lib/plugin.lib.php

@@ -373,6 +373,7 @@ class AppPlugin
             }
         }
     }
+
     /**
      * When saving the plugin values in the course settings, check whether
      * a callback method should be called and send it the updated settings

+ 40 - 14
main/inc/routes.php

@@ -353,12 +353,36 @@ $userPermissionsInsideACourse = function (Request $request) use ($app) {
     }
 };
 
+/**
+ * Removes course-session data
+ * @param Request $request
+ */
 $cleanCourseSession = function (Request $request) use ($app) {
     Session::erase('_cid');
     Session::erase('_real_cid');
     Session::erase('_course');
 };
 
+/**
+ * Deletes the exam_password user extra field *only* to students
+ * @todo improve the login hook system
+ * @param Request $request
+ */
+$afterLogin = function (Request $request) use ($app) {
+    if (isset($app['current_user']) && isset($app['current_user']['user_id']) && $app['current_user']['status'] == STUDENT) {
+        $extraField = new ExtraField('user');
+        $extraFieldData = $extraField->get_handler_field_info_by_field_variable('exam_password');
+        if ($extraFieldData && !empty($extraFieldData)) {
+            $extraField = new ExtraFieldValue('user');
+            $extraFieldValue = $extraField->get_values_by_handler_and_field_variable($app['current_user']['user_id'], 'exam_password');
+            if (!empty($extraFieldValue)) {
+                $extraField->delete_values_by_handler_and_field_id($app['current_user']['user_id'], $extraFieldValue['id']);
+            }
+        }
+    }
+};
+
+/** Legacy controller */
 $app->get('/', 'legacy.controller:classicAction')
     ->before($userAccessConditions)
     ->before($settingCourseConditions)
@@ -369,8 +393,9 @@ $app->post('/', 'legacy.controller:classicAction')
     ->before($settingCourseConditions)
     ->before($userPermissionsInsideACourse);
 
-// web/index
+/** web/index */
 $app->match('/index', 'index.controller:indexAction', 'GET|POST')
+    ->after($afterLogin)
     ->bind('index');
 
 // web/login
@@ -378,7 +403,7 @@ $app->match('/index', 'index.controller:indexAction', 'GET|POST')
 ->bind('login');*/
 
 
-// Userportal
+/** Userportal */
 $app->get('/userportal', 'userPortal.controller:indexAction');
 $app->get('/userportal/{type}/{filter}/{page}', 'userPortal.controller:indexAction')
     ->value('type', 'courses') //default values
@@ -386,14 +411,15 @@ $app->get('/userportal/{type}/{filter}/{page}', 'userPortal.controller:indexActi
     ->value('page', '1')
     ->bind('userportal')
     ->after($cleanCourseSession);
+
 //->assert('type', '.+'); //allowing slash "/"
 
-// Logout
+/**  Logout */
 $app->get('/logout', 'index.controller:logoutAction')
     ->bind('logout')
     ->after($cleanCourseSession);
 
-// Course home instead of courses/MATHS the new URL is web/courses/MATHS
+/** Course home instead of courses/MATHS the new URL is web/courses/MATHS  */
 $app->match('/courses/{cidReq}/{id_session}/', 'course_home.controller:indexAction', 'GET|POST')
     ->assert('id_session', '\d+')
     ->assert('type', '.+')
@@ -405,41 +431,41 @@ $app->match('/courses/{cidReq}/', 'course_home.controller:indexAction', 'GET|POS
     ->before($settingCourseConditions)
     ->before($userPermissionsInsideACourse); //allowing slash "/"
 
-// Course documents
+/**  Course documents */
 $app->get('/courses/{courseCode}/document/', 'index.controller:getDocumentAction')
     ->assert('type', '.+');
 
-// Certificates
+/** Certificates */
 $app->match('/certificates/{id}', 'certificate.controller:indexAction', 'GET');
 
-// Username
+/**  Username */
 $app->match('/user/{username}', 'user.controller:indexAction', 'GET');
 
-// Who is online
+/** Who is online */
 /*$app->match('/users/online', 'user.controller:onlineAction', 'GET');
 $app->match('/users/online-in-course', 'user.controller:onlineInCourseAction', 'GET');
 $app->match('/users/online-in-session', 'user.controller:onlineInSessionAction', 'GET');*/
 
-// Portal news
+/** Portal news */
 $app->match('/news/{id}', 'news.controller:indexAction', 'GET')
     ->bind('portal_news');
 
-// LP controller (subscribe users to a LP)
+/** LP controller (subscribe users to a LP) */
 $app->match('/learnpath/subscribe_users/{lpId}', 'learnpath.controller:indexAction', 'GET|POST')
     ->bind('subscribe_users');
 
-// Data document_templates files
+/** Data document_templates files */
 $app->get('/data/document_templates/{file}', 'index.controller:getDocumentTemplateAction')
     ->bind('data');
 
-// Data default_platform_document files
+/** Data default_platform_document files */
 $app->get('/data/default_platform_document/', 'index.controller:getDefaultPlatformDocumentAction')
     ->assert('type', '.+');
 
-// Group files
+/** Group files */
 $app->get('/data/upload/groups/{groupId}/{file}', 'index.controller:getGroupFile')
     ->assert('type', '.+');
 
-// User files
+/** User files */
 $app->match('/data/upload/users/', 'index.controller:getUserFile', 'GET|POST')
     ->assert('type', '.+');