Browse Source

Add option to copy gradebook with links to the new course-session #9966

Julio Montoya 9 years ago
parent
commit
53e475a992

+ 7 - 4
main/gradebook/lib/be/category.class.php

@@ -451,7 +451,9 @@ class Category implements GradebookItem
 
         $categories = array();
         if (Database::num_rows($result) > 0) {
-            $categories = Category::create_category_objects_from_sql_result($result);
+            $categories = Category::create_category_objects_from_sql_result(
+                $result
+            );
         }
 
         return $categories;
@@ -570,7 +572,8 @@ class Category implements GradebookItem
             $em->persist($category);
             $em->flush();
 
-            $id = Database::insert_id();
+            $id = $category->getId();
+
             $this->set_id($id);
 
             if (!empty($id)) {
@@ -959,7 +962,6 @@ class Category implements GradebookItem
                     return array($ressum, $weightsum);
                     break;
                 case 'ranking':
-                    //var_dump($students);
                     return null;
                     return AbstractLink::getCurrentUserRanking($students);
                     break;
@@ -1487,6 +1489,7 @@ class Category implements GradebookItem
             // All students
             // Course admin
             if (api_is_allowed_to_edit() && !api_is_platform_admin()) {
+
                 // root
                 if ($this->id == 0) {
                     return $this->get_root_categories_for_teacher(api_get_user_id(), $course_code, $session_id, false);
@@ -1501,7 +1504,7 @@ class Category implements GradebookItem
                 }
             } elseif (api_is_platform_admin()) {
                 // platform admin
-                //we explicitly avoid listing subcats from another session
+                // we explicitly avoid listing subcats from another session
                 return Category::load(null, null, $course_code, $this->id, null, $session_id, $order);
             }
         }

+ 83 - 46
main/inc/lib/sessionmanager.lib.php

@@ -1999,14 +1999,16 @@ class SessionManager
      * @author Carlos Vargas from existing code
      * @param	int		$sessionId
      * @param	array	$courseList List of courses int ids
-     * @param	bool	$removeExistingCoursesUsers Whether to unsubscribe
+     * @param	bool	$removeExistingCoursesWithUsers Whether to unsubscribe
      * existing courses and users (true, default) or not (false)
+     * @param $copyEvaluation from base course to session course
      * @return	void	Nothing, or false on error
      * */
     public static function add_courses_to_session(
         $sessionId,
         $courseList,
-        $removeExistingCoursesWithUsers = true
+        $removeExistingCoursesWithUsers = true,
+        $copyEvaluation = false
     ) {
         $sessionId = intval($sessionId);
 
@@ -2041,7 +2043,6 @@ class SessionManager
 
             foreach ($existingCourses as $existingCourse) {
                 if (!in_array($existingCourse['c_id'], $courseList)) {
-                    $courseInfo = api_get_course_info_by_id($existingCourse['c_id']);
 
                     $sql = "DELETE FROM $tbl_session_rel_course
                             WHERE c_id = " . $existingCourse['c_id'] . " AND session_id = $sessionId";
@@ -2089,7 +2090,43 @@ class SessionManager
             }
 
             if (!$exists) {
-                //if the course isn't subscribed yet
+                // Copy gradebook categories and links (from base course)
+                // to the new course session
+                if ($copyEvaluation) {
+                    $cats = Category::load(null, null, $courseInfo['code']);
+                    if (!empty($cats)) {
+                        $categoryIdList = [];
+                        /** @var Category $cat */
+                        foreach ($cats as $cat) {
+                            $categoryIdList[$cat->get_id()] = $cat->get_id();
+                        }
+                        $newCategoryIdList = [];
+                        foreach ($cats as $cat) {
+                            $links = $cat->get_links(null, false, $courseInfo['code'], 0);
+
+                            $cat->set_session_id($sessionId);
+                            $oldCategoryId= $cat->get_id();
+                            $newId = $cat->add();
+                            $newCategoryIdList[$oldCategoryId] = $newId;
+
+                            $parentId = $cat->get_parent_id();
+                            if (!empty($parentId)) {
+                                $newParentId = $newCategoryIdList[$parentId];
+                                $cat->set_parent_id($newParentId);
+                                $cat->save();
+                            }
+
+                            /** @var AbstractLink $link */
+                            foreach ($links as $link) {
+                                $newCategoryId = $newCategoryIdList[$link->get_category_id()];
+                                $link->set_category_id($newCategoryId);
+                                $link->add();
+                            }
+                        }
+                    }
+                }
+
+                // If the course isn't subscribed yet
                 $sql = "INSERT INTO $tbl_session_rel_course (session_id, c_id)
                         VALUES ($sessionId, $courseId)";
                 Database::query($sql);
@@ -2231,7 +2268,7 @@ class SessionManager
         $params = [
             'item_id' => $sessionId,
             'variable' => $variable,
-            'value' => $value
+            'value' => $value,
         ];
         $extraFieldValue->save($params);
     }
@@ -2494,14 +2531,14 @@ class SessionManager
 
         $availableFields = array(
             's.id',
-            's.name'
+            's.name',
         );
 
         $availableOperator = array(
             'like',
             '>=',
             '<=',
-            '='
+            '=',
         );
 
         if (count($conditions) > 0) {
@@ -4072,7 +4109,7 @@ class SessionManager
                             'access_start_date' => $date_start,
                             'access_end_date' => $date_end,
                             'visibility' => $visibilityAfterExpirationPerSession,
-                            'session_category_id' => $session_category_id
+                            'session_category_id' => $session_category_id,
                         );
 
                         if (!empty($sessionDescription)) {
@@ -4445,7 +4482,7 @@ class SessionManager
         return array(
             'error_message' => $error_message,
             'session_counter' => $session_counter,
-            'session_list' => $sessionList
+            'session_list' => $sessionList,
         );
     }
 
@@ -5043,7 +5080,7 @@ class SessionManager
     {
         return array(
             'courseDescription',
-            'courseIntroduction'
+            'courseIntroduction',
         );
     }
 
@@ -5232,7 +5269,7 @@ class SessionManager
                 if (!empty($userInfo) && !empty($sessionInfo)) {
                     $userSessionList[$userInfo['user_id']]['session_list'][] = array(
                         'session_id' => $sessionInfo['id'],
-                        'session_info' => $sessionInfo
+                        'session_info' => $sessionInfo,
                     );
                     $userSessionList[$userInfo['user_id']]['user_info'] = $userInfo;
                 }
@@ -5572,16 +5609,16 @@ class SessionManager
 
         $idResult = Database::select('DISTINCT user_id', $scuTable, array(
             'where' => array(
-                'status = ?' => 2
-            )
+                'status = ?' => 2,
+            ),
         ));
 
         if ($idResult != false) {
             foreach ($idResult as $idData) {
                 $userResult = Database::select('user_id, lastname, firstname, username', $userTable, array(
                     'where' => array(
-                        'user_id = ?' => $idData['user_id']
-                    )
+                        'user_id = ?' => $idData['user_id'],
+                    ),
                 ), 'first');
 
                 if ($userResult != false) {
@@ -5616,7 +5653,7 @@ class SessionManager
         $trackLoginTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
 
         $whereConditions = array(
-            'login_user_id = ? ' => $userId
+            'login_user_id = ? ' => $userId,
         );
 
         if (!empty($from) && !empty($until)) {
@@ -5628,7 +5665,7 @@ class SessionManager
             'SEC_TO_TIME(SUM(UNIX_TIMESTAMP(logout_date) - UNIX_TIMESTAMP(login_date))) as total_time',
             $trackLoginTable,
             array(
-                'where' => $whereConditions
+                'where' => $whereConditions,
             ), 'first'
         );
 
@@ -5650,8 +5687,8 @@ class SessionManager
         return  Database::select('*', $table, array(
             'where' => array(
                 'user_id = ? AND ' => $coachId,
-                'status = ?' => 2
-            )
+                'status = ?' => 2,
+            ),
         ));
     }
 
@@ -5721,8 +5758,8 @@ class SessionManager
                                 sfv.item_id = session.id AND
                                 sf.variable = 'target' AND
                                 sfv.value = ?
-                        )" => array($categoryId, $target)
-                    )
+                        )" => array($categoryId, $target),
+                    ),
                 )
             );
             $whereFieldVariables = array();
@@ -5742,7 +5779,7 @@ class SessionManager
             $extraField = new ExtraField('session');
             $questionMarks = substr(str_repeat('?, ', count($fieldsArray)), 0, -2);
             $fieldsList = $extraField->get_all(array(
-                ' variable IN ( ' . $questionMarks . ' )' => $fieldsArray
+                ' variable IN ( ' . $questionMarks . ' )' => $fieldsArray,
             ));
             // Index session fields
             foreach ($fieldsList as $field) {
@@ -5798,7 +5835,7 @@ class SessionManager
                 array(
                     'where' => array(
                         'name = ?' => $categoryName,
-                    )
+                    ),
                 )
             );
             // Check the result
@@ -5853,8 +5890,8 @@ class SessionManager
                 $sessionTable,
                 array(
                     'where' => array(
-                        'session_category_id = ?' => $sessionCategoryId
-                    )
+                        'session_category_id = ?' => $sessionCategoryId,
+                    ),
                 )
             );
 
@@ -5975,8 +6012,8 @@ class SessionManager
                 Database::get_main_table(TABLE_MAIN_SESSION),
                 array(
                     'where' => array(
-                        'id = ?' => $sessionId
-                    )
+                        'id = ?' => $sessionId,
+                    ),
                 )
             );
 
@@ -6014,15 +6051,15 @@ class SessionManager
                     FROM $sfvTable v INNER JOIN $extraFieldTable e
                     ON (v.field_id = e.id)
                     WHERE value LIKE %?% AND extra_field_type = $extraFieldType
-                ) " => $term
-                )
+                ) " => $term,
+                ),
             ));
         } else {
             $resultData = Database::select('*', $sTable, array(
                 'where' => array(
                     "name LIKE %?% " => $term,
-                    "OR description LIKE %?% " => $term
-                )
+                    "OR description LIKE %?% " => $term,
+                ),
             ));
 
             return $resultData;
@@ -6054,7 +6091,7 @@ class SessionManager
 
         $sessionExtraField = new ExtraField('session');
         $fieldList = $sessionExtraField->get_all(array(
-            "variable IN ( " . implode(", ", $variablePlaceHolders) . " ) " => $variables
+            "variable IN ( " . implode(", ", $variablePlaceHolders) . " ) " => $variables,
         ));
 
         $fields = array();
@@ -6068,7 +6105,7 @@ class SessionManager
         $extra = new ExtraFieldValue('session');
         $sessionFieldValueList = $extra->get_all(
             array(
-                "field_id IN ( " . implode(", ", $variablePlaceHolders) . " )" => array_keys($fields)
+                "field_id IN ( " . implode(", ", $variablePlaceHolders) . " )" => array_keys($fields),
             )
         );
 
@@ -6088,7 +6125,7 @@ class SessionManager
 
             $extraData[] = array(
                 'variable' => $extrafieldVariable,
-                'value' => $extrafieldValue
+                'value' => $extrafieldValue,
             );
         }
 
@@ -6265,7 +6302,7 @@ class SessionManager
         $userInfo = api_get_user_info();
 
         $categoriesOptions = array(
-            '0' => get_lang('None')
+            '0' => get_lang('None'),
         );
 
         if ($categoriesList != false) {
@@ -6292,7 +6329,7 @@ class SessionManager
                 array(
                     'id' => 'coach_username',
                     'class' => 'chzn-select',
-                    'style' => 'width:370px;'
+                    'style' => 'width:370px;',
                 )
             );
         } else {
@@ -6351,7 +6388,7 @@ class SessionManager
                     null,
                     [
                         'url' => api_get_path(WEB_AJAX_PATH) . 'session.ajax.php?a=search_general_coach',
-                        'width' => '100%'
+                        'width' => '100%',
                     ]
                 );
             }
@@ -6366,7 +6403,7 @@ class SessionManager
         $form->addSelect('session_category', get_lang('SessionCategory'), $categoriesOptions, array(
             'id' => 'session_category',
             'class' => 'chzn-select',
-            'style' => 'width:370px;'
+            'style' => 'width:370px;',
         ));
 
         $form->addHtmlEditor(
@@ -6375,7 +6412,7 @@ class SessionManager
             false,
             false,
             array(
-                'ToolbarSet' => 'Minimal'
+                'ToolbarSet' => 'Minimal',
             )
         );
 
@@ -6385,18 +6422,18 @@ class SessionManager
         $visibilityGroup[] = $form->createElement('select', 'session_visibility', null, array(
             SESSION_VISIBLE_READ_ONLY => get_lang('SessionReadOnly'),
             SESSION_VISIBLE => get_lang('SessionAccessible'),
-            SESSION_INVISIBLE => api_ucfirst(get_lang('SessionNotAccessible'))
+            SESSION_INVISIBLE => api_ucfirst(get_lang('SessionNotAccessible')),
         ));
         $form->addGroup($visibilityGroup, 'visibility_group', get_lang('SessionVisibility'), null, false);
 
         $options = [
             0 => get_lang('ByDuration'),
-            1 => get_lang('ByDates')
+            1 => get_lang('ByDates'),
         ];
 
         $form->addSelect('access', get_lang('Access'), $options, array(
             'onchange' => 'accessSwitcher()',
-            'id' => 'access'
+            'id' => 'access',
         ));
 
         $form->addElement('html', '<div id="duration" style="display:none">');
@@ -6406,10 +6443,10 @@ class SessionManager
             'duration',
             array(
                 get_lang('SessionDurationTitle'),
-                get_lang('SessionDurationDescription')
+                get_lang('SessionDurationDescription'),
             ),
             array(
-                'maxlength' => 50
+                'maxlength' => 50,
             )
         );
 
@@ -6651,7 +6688,7 @@ class SessionManager
                             'defaultValue'  => '1',
                             'value'         => '1:'.get_lang('Active').';0:'.get_lang('Inactive')),
                         // for the top bar
-                        'editoptions' => array('value' => '" ":'.get_lang('All').';1:'.get_lang('Active').';0:'.get_lang('Inactive'))
+                        'editoptions' => array('value' => '" ":'.get_lang('All').';1:'.get_lang('Active').';0:'.get_lang('Inactive')),
                     ),
                     array('name'=>'visibility',     'index'=>'visibility',      'width'=>'40',   'align'=>'left', 'search' => 'false'),
                     array('name'=>'course_title',    'index'=>'course_title',   'width'=>'50',   'hidden' => 'true', 'search' => 'true', 'searchoptions' => array('searchhidden' =>'true','sopt' => $operators)),
@@ -6674,7 +6711,7 @@ class SessionManager
             'columns' => $columns,
             'column_model' => $column_model,
             'rules' => $rules,
-            'simple_column_name' => $simple_column_name
+            'simple_column_name' => $simple_column_name,
         );
 
         return $return_array;

+ 11 - 1
main/session/add_courses_to_session.php

@@ -80,7 +80,9 @@ $courses = $sessions = array();
 if (isset($_POST['formSent']) && $_POST['formSent']) {
 
     $courseList = $_POST['SessionCoursesList'];
-    SessionManager::add_courses_to_session($sessionId, $courseList, true);
+    $copyEvaluation = isset($_POST['copy_evaluation']);
+
+    SessionManager::add_courses_to_session($sessionId, $courseList, true, $copyEvaluation);
     Display::addFlash(Display::return_message(get_lang('Updated')));
 
     if (isset($add)) {
@@ -269,13 +271,21 @@ unset($Courses);
                 <?php
                 }
                 ?>
+                    <br />
+                    <label>
+                        <input type="checkbox" name="copy_evaluation">
+                        <?php echo get_lang('CopyEvaluation'); ?>
+                    </label>
+                    <br />
                 <?php
+
                 if (isset($_GET['add'])) {
                     echo '<button class="btn btn-success" type="button" value="" onclick="valide()" >'.get_lang('NextStep').'</button>';
                 } else {
                     echo '<button class="btn btn-success" type="button" value="" onclick="valide()" >'.get_lang('SubscribeCoursesToSession').'</button>';
                 }
                 ?>
+
                 </div>
             </div>
             <div class="col-md-5">