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