move code up one directory
[atutor.git] / mods / _core / editor / edit_content_folder.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id$
14 define('AT_INCLUDE_PATH', '../../../include/');
15 require(AT_INCLUDE_PATH.'vitals.inc.php');
16 require(AT_INCLUDE_PATH.'../mods/_core/editor/editor_tab_functions.inc.php');
17
18 if (isset($_GET['cid'])) $cid = intval($_GET['cid']);
19 if (isset($_GET['pid'])) $pid = intval($_GET['pid']);
20
21 if ($cid > 0)
22 {
23         $result = $contentManager->getContentPage($cid);
24         $content_row = mysql_fetch_assoc($result);
25 }
26
27 if (defined('AT_FORCE_GET_FILE') && AT_FORCE_GET_FILE) {
28         $course_base_href = 'get.php/';
29 } else {
30         $course_base_href = 'content/' . $_SESSION['course_id'] . '/';
31 }
32
33 // save changes
34 if ($_POST['submit'])
35 {
36         if ($_POST['title'] == '') {
37                 $msg->addError(array('EMPTY_FIELDS', _AT('title')));
38         }
39                 
40         if (!($release_date = generate_release_date())) {
41                 $msg->addError('BAD_DATE');
42         }
43         
44         if (!$msg->containsErrors()) 
45         {
46                 $_POST['title'] = $content_row['title'] = $addslashes($_POST['title']);
47         
48                 if ($cid > 0)
49                 { // edit existing content
50                         $err = $contentManager->editContent($cid, 
51                                                             $_POST['title'], 
52                                                             '', 
53                                                             '', 
54                                                             '', 
55                                                             $content_row['formatting'], 
56                                                             $release_date, 
57                                                             '', 
58                                                             $content_row['use_customized_head'], 
59                                                             '', 
60                                                             $content_row['allow_test_export'],
61                                                 CONTENT_TYPE_FOLDER);
62                 }
63                 else
64                 { // add new content
65                         // find out ordering and content_parent_id
66                         if ($pid)
67                         { // insert sub content folder
68                                 $ordering = count($contentManager->getContent($pid))+1;
69                         }
70                         else
71                         { // insert a top content folder
72                                 $ordering = count($contentManager->getContent(0)) + 1;
73                                 $pid = 0;
74                         }
75                         
76                         $cid = $contentManager->addContent($_SESSION['course_id'],
77                                                            $pid,
78                                                            $ordering,
79                                                            $_POST['title'],
80                                                            '',
81                                                            '',
82                                                            '',
83                                                            0,
84                                                            $release_date,
85                                                            '',
86                                                            0,
87                                                            '',
88                                                            1,
89                                                            CONTENT_TYPE_FOLDER);
90                 }
91                 
92                 // save pre-tests
93                 $sql = "DELETE FROM ". TABLE_PREFIX . "content_prerequisites 
94                          WHERE content_id=".$cid." AND type='".CONTENT_PRE_TEST."'";
95                 $result = mysql_query($sql, $db);
96                 
97                 if (is_array($_POST['tid']) && sizeof($_POST['tid']) > 0)
98                 {
99                         foreach ($_POST['tid'] as $i => $tid){
100                                 $tid = intval($tid);
101                                 $sql = "INSERT INTO ". TABLE_PREFIX . "content_prerequisites 
102                                            SET content_id=".$cid.", type='".CONTENT_PRE_TEST."', item_id=$tid";
103                                 $result = mysql_query($sql, $db);
104
105                                 if ($result===false) $msg->addError('MYSQL_FAILED');
106                         }
107                 }
108                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
109                 header('Location: '.$_base_path.'mods/_core/editor/edit_content_folder.php?cid='.$cid);
110                 exit;
111         }
112 }
113
114 if ($cid > 0)
115 { // edit existing content folder
116         if (!$content_row) {
117                 $_pages['mods/_core/editor/edit_content_folder.php']['title_var'] = 'missing_content';
118                 $_pages['mods/_core/editor/edit_content_folder.php']['parent']    = 'index.php';
119                 $_pages['mods/_core/editor/edit_content_folder.php']['ignore']  = true;
120
121                 require(AT_INCLUDE_PATH.'header.inc.php');
122         
123                 $msg->addError('PAGE_NOT_FOUND');
124                 $msg->printAll();
125         
126                 require (AT_INCLUDE_PATH.'footer.inc.php');
127                 exit;
128         } /* else: */
129         
130         /* the "heading navigation": */
131         $path   = $contentManager->getContentPath($cid);
132         
133         if ($content_row['content_path']) {
134                 $content_base_href = $content_row['content_path'].'/';
135         }
136         
137         $parent_headings = '';
138         $num_in_path = count($path);
139         
140         /* the page title: */
141         $page_title = '';
142         $page_title .= $content_row['title'];
143         
144         for ($i=0; $i<$num_in_path; $i++) {
145                 $content_info = $path[$i];
146                 if ($_SESSION['prefs']['PREF_NUMBERING']) {
147                         if ($contentManager->_menu_info[$content_info['content_id']]['content_parent_id'] == 0) {
148                                 $top_num = $contentManager->_menu_info[$content_info['content_id']]['ordering'];
149                                 $parent_headings .= $top_num;
150                         } else {
151                                 $top_num = $top_num.'.'.$contentManager->_menu_info[$content_info['content_id']]['ordering'];
152                                 $parent_headings .= $top_num;
153                         }
154                         if ($_SESSION['prefs']['PREF_NUMBERING']) {
155                                 $path[$i]['content_number'] = $top_num . ' ';
156                         }
157                         $parent_headings .= ' ';
158                 }
159         }
160         
161         if ($_SESSION['prefs']['PREF_NUMBERING']) {
162                 if ($top_num != '') {
163                         $top_num = $top_num.'.'.$content_row['ordering'];
164                         $page_title .= $top_num.' ';
165                 } else {
166                         $top_num = $content_row['ordering'];
167                         $page_title .= $top_num.' ';
168                 }
169         }
170         
171         $parent = 0;
172 //      foreach ($path as $i=>$page) {
173 //              if (!$parent) {
174 //                      $_pages['editor/edit_content_folder.php?cid='.$page['content_id']]['title']    = $page['content_number'] . $page['title'];
175 //                      $_pages['editor/edit_content_folder.php?cid='.$page['content_id']]['parent']   = 'index.php';
176 //              } else {
177 //                      $_pages['editor/edit_content_folder.php?cid='.$page['content_id']]['title']    = $page['content_number'] . $page['title'];
178 //                      $_pages['editor/edit_content_folder.php?cid='.$page['content_id']]['parent']   = 'editor/edit_content_folder.php?cid='.$parent;
179 //              }
180 //      
181 //              $_pages['editor/edit_content_folder.php?cid='.$page['content_id']]['ignore'] = true;
182 //              $parent = $page['content_id'];
183 //      }
184 //      $last_page = array_pop($_pages);
185 //      $_pages['editor/edit_content_folder.php'] = $last_page;
186         
187         reset($path);
188         $first_page = current($path);
189         
190         save_last_cid($cid);
191         
192         if (isset($top_num) && $top_num != (int) $top_num) {
193                 $top_num = substr($top_num, 0, strpos($top_num, '.'));
194         }
195         
196         // used by header.inc.php
197         $_tool_shortcuts = $contentManager->getToolShortcuts($content_row);
198         $release_date = $content_row['release_date'];
199
200         // display pre-tests
201         $sql = 'SELECT * FROM '.TABLE_PREFIX."content_prerequisites WHERE content_id=$_REQUEST[cid] AND type='".CONTENT_PRE_TEST."'";
202         $result = mysql_query($sql, $db);
203         while ($row = mysql_fetch_assoc($result)) {
204                 $_POST['pre_tid'][] = $row['item_id'];
205         }
206
207         $savant->assign('ftitle', $content_row['title']);
208         $savant->assign('shortcuts', $shortcuts);
209         $savant->assign('cid', $cid);
210 }
211
212 // display pre-tests
213 // get a list of all the tests we have, and links to create, edit, delete, preview 
214 $sql    = "SELECT *, UNIX_TIMESTAMP(start_date) AS us, UNIX_TIMESTAMP(end_date) AS ue 
215              FROM ".TABLE_PREFIX."tests 
216             WHERE course_id=$_SESSION[course_id] 
217             ORDER BY start_date DESC";
218 $result = mysql_query($sql, $db);
219 $num_tests = mysql_num_rows($result);
220
221 $i = 0;
222 while($row = mysql_fetch_assoc($result))
223 {
224         $results[$i]['test_id'] = $row['test_id'];
225         $results[$i]['title'] = $row['title'];
226         
227         if ( ($row['us'] <= time()) && ($row['ue'] >= time() ) ) {
228                 $results[$i]['status'] = '<strong>'._AT('ongoing').'</strong>';
229         } else if ($row['ue'] < time() ) {
230                 $results[$i]['status'] = '<strong>'._AT('expired').'</strong>';
231         } else if ($row['us'] > time() ) {
232                 $results[$i]['status'] = '<strong>'._AT('pending').'</strong>';
233         } 
234
235         $startend_date_format=_AT('startend_date_format'); 
236
237         $results[$i]['availability'] = AT_date($startend_date_format, $row['start_date'], AT_DATE_MYSQL_DATETIME). ' ' ._AT('to_2').' ';
238         $results[$i]['availability'] .= AT_date($startend_date_format, $row['end_date'], AT_DATE_MYSQL_DATETIME);
239         
240         // get result release
241         if ($row['result_release'] == AT_RELEASE_IMMEDIATE)
242                 $results[$i]['result_release'] = _AT('release_immediate');
243         else if ($row['result_release'] == AT_RELEASE_MARKED)
244                 $results[$i]['result_release'] = _AT('release_marked');
245         else if ($row['result_release'] == AT_RELEASE_NEVER)
246                 $results[$i]['result_release'] = _AT('release_never');
247                 
248         //get # marked submissions
249         $sql_sub = "SELECT COUNT(*) AS sub_cnt FROM ".TABLE_PREFIX."tests_results WHERE status=1 AND test_id=".$row['test_id'];
250         $result_sub     = mysql_query($sql_sub, $db);
251         $row_sub = mysql_fetch_assoc($result_sub);
252         $results[$i]['submissions'] = $row_sub['sub_cnt'].' '._AT('submissions').', ';
253
254         //get # submissions
255         $sql_sub = "SELECT COUNT(*) AS marked_cnt FROM ".TABLE_PREFIX."tests_results WHERE status=1 AND test_id=".$row['test_id']." AND final_score=''";
256         $result_sub     = mysql_query($sql_sub, $db);
257         $row_sub = mysql_fetch_assoc($result_sub);
258         $results[$i]['submissions'] .= $row_sub['marked_cnt'].' '._AT('unmarked');
259
260         //get assigned groups
261         $sql_sub = "SELECT G.title FROM ".TABLE_PREFIX."groups G INNER JOIN ".TABLE_PREFIX."tests_groups T USING (group_id) WHERE T.test_id=".$row['test_id'];
262         $result_sub     = mysql_query($sql_sub, $db);
263         if (mysql_num_rows($result_sub) == 0) {
264                 $results[$i]['assign_to'] = _AT('everyone');
265         } else {
266                 $row_sub = mysql_fetch_assoc($result_sub);
267                 $results[$i]['assign_to'] = $row_sub['title'];
268                 do {
269                         $results[$i]['assign_to'] .= ', '.$row_sub['title'];
270                 } while ($row_sub = mysql_fetch_assoc($result_sub));
271         }
272         
273         if ($row['passscore'] == 0 && $row['passpercent'] == 0)
274                 $results[$i]['pass_score'] = _AT('no_pass_score');
275         else if ($row['passscore'] <> 0)
276                 $results[$i]['pass_score'] = $row['passscore'];
277         else if ($row['passpercent'] <> 0)
278                 $results[$i]['pass_score'] = $row['passpercent'].'%';
279                 
280         $i++;
281 }
282
283 if (isset($results)) $savant->assign('pretests', $results);
284
285 // set release date
286 if (!isset($release_date)) $release_date = date('Y-m-d H:i:s');
287
288 $_POST['day']   = substr($release_date, 8, 2);
289 $_POST['month'] = substr($release_date, 5, 2);
290 $_POST['year']  = substr($release_date, 0, 4);
291 $_POST['hour']  = substr($release_date, 11, 2);
292 $_POST['min']= substr($release_date, 14, 2);
293
294 if ($pid > 0) $savant->assign('pid', $pid);
295
296 require(AT_INCLUDE_PATH.'header.inc.php');
297 $savant->display('editor/edit_content_folder.tmpl.php');
298 require(AT_INCLUDE_PATH.'footer.inc.php');
299
300 //save last visit page.
301 $_SESSION['last_visited_page'] = $server_protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
302 ?>