ATutor 2.0
[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: 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.'../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                 }
62                 else
63                 { // add new content
64                         // find out ordering and content_parent_id
65                         if ($pid)
66                         { // insert sub content folder
67                                 $ordering = count($contentManager->getContent($pid))+1;
68                         }
69                         else
70                         { // insert a top content folder
71                                 $ordering = count($contentManager->getContent(0)) + 1;
72                                 $pid = 0;
73                         }
74                         
75                         $cid = $contentManager->addContent($_SESSION['course_id'],
76                                                            $pid,
77                                                            $ordering,
78                                                            $_POST['title'],
79                                                            '',
80                                                            '',
81                                                            '',
82                                                            0,
83                                                            $release_date,
84                                                            '',
85                                                            0,
86                                                            '',
87                                                            1,
88                                                            CONTENT_TYPE_FOLDER);
89                 }
90                 
91                 // save pre-tests
92                 $sql = "DELETE FROM ". TABLE_PREFIX . "content_prerequisites 
93                          WHERE content_id=".$cid." AND type='".CONTENT_PRE_TEST."'";
94                 $result = mysql_query($sql, $db);
95                 
96                 if (is_array($_POST['tid']) && sizeof($_POST['tid']) > 0)
97                 {
98                         foreach ($_POST['tid'] as $i => $tid){
99                                 $tid = intval($tid);
100                                 $sql = "INSERT INTO ". TABLE_PREFIX . "content_prerequisites 
101                                            SET content_id=".$cid.", type='".CONTENT_PRE_TEST."', item_id=$tid";
102                                 $result = mysql_query($sql, $db);
103
104                                 if ($result===false) $msg->addError('MYSQL_FAILED');
105                         }
106                 }
107                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
108                 header('Location: '.$_base_path.'mods/_core/editor/edit_content_folder.php?cid='.$cid);
109                 exit;
110         }
111 }
112
113 if ($cid > 0)
114 { // edit existing content folder
115         if (!$content_row) {
116                 $_pages['mods/_core/editor/edit_content_folder.php']['title_var'] = 'missing_content';
117                 $_pages['mods/_core/editor/edit_content_folder.php']['parent']    = 'index.php';
118                 $_pages['mods/_core/editor/edit_content_folder.php']['ignore']  = true;
119
120                 require(AT_INCLUDE_PATH.'header.inc.php');
121         
122                 $msg->addError('PAGE_NOT_FOUND');
123                 $msg->printAll();
124         
125                 require (AT_INCLUDE_PATH.'footer.inc.php');
126                 exit;
127         } /* else: */
128         
129         /* the "heading navigation": */
130         $path   = $contentManager->getContentPath($cid);
131         
132         if ($content_row['content_path']) {
133                 $content_base_href = $content_row['content_path'].'/';
134         }
135         
136         $parent_headings = '';
137         $num_in_path = count($path);
138         
139         /* the page title: */
140         $page_title = '';
141         $page_title .= $content_row['title'];
142         
143         for ($i=0; $i<$num_in_path; $i++) {
144                 $content_info = $path[$i];
145                 if ($_SESSION['prefs']['PREF_NUMBERING']) {
146                         if ($contentManager->_menu_info[$content_info['content_id']]['content_parent_id'] == 0) {
147                                 $top_num = $contentManager->_menu_info[$content_info['content_id']]['ordering'];
148                                 $parent_headings .= $top_num;
149                         } else {
150                                 $top_num = $top_num.'.'.$contentManager->_menu_info[$content_info['content_id']]['ordering'];
151                                 $parent_headings .= $top_num;
152                         }
153                         if ($_SESSION['prefs']['PREF_NUMBERING']) {
154                                 $path[$i]['content_number'] = $top_num . ' ';
155                         }
156                         $parent_headings .= ' ';
157                 }
158         }
159         
160         if ($_SESSION['prefs']['PREF_NUMBERING']) {
161                 if ($top_num != '') {
162                         $top_num = $top_num.'.'.$content_row['ordering'];
163                         $page_title .= $top_num.' ';
164                 } else {
165                         $top_num = $content_row['ordering'];
166                         $page_title .= $top_num.' ';
167                 }
168         }
169         
170         $parent = 0;
171 //      foreach ($path as $i=>$page) {
172 //              if (!$parent) {
173 //                      $_pages['editor/edit_content_folder.php?cid='.$page['content_id']]['title']    = $page['content_number'] . $page['title'];
174 //                      $_pages['editor/edit_content_folder.php?cid='.$page['content_id']]['parent']   = 'index.php';
175 //              } else {
176 //                      $_pages['editor/edit_content_folder.php?cid='.$page['content_id']]['title']    = $page['content_number'] . $page['title'];
177 //                      $_pages['editor/edit_content_folder.php?cid='.$page['content_id']]['parent']   = 'editor/edit_content_folder.php?cid='.$parent;
178 //              }
179 //      
180 //              $_pages['editor/edit_content_folder.php?cid='.$page['content_id']]['ignore'] = true;
181 //              $parent = $page['content_id'];
182 //      }
183 //      $last_page = array_pop($_pages);
184 //      $_pages['editor/edit_content_folder.php'] = $last_page;
185         
186         reset($path);
187         $first_page = current($path);
188         
189         save_last_cid($cid);
190         
191         if (isset($top_num) && $top_num != (int) $top_num) {
192                 $top_num = substr($top_num, 0, strpos($top_num, '.'));
193         }
194         
195         // used by header.inc.php
196         $_tool_shortcuts = $contentManager->getToolShortcuts($content_row);
197         $release_date = $content_row['release_date'];
198
199         // display pre-tests
200         $sql = 'SELECT * FROM '.TABLE_PREFIX."content_prerequisites WHERE content_id=$_REQUEST[cid] AND type='".CONTENT_PRE_TEST."'";
201         $result = mysql_query($sql, $db);
202         while ($row = mysql_fetch_assoc($result)) {
203                 $_POST['pre_tid'][] = $row['item_id'];
204         }
205
206         $savant->assign('ftitle', $content_row['title']);
207         $savant->assign('shortcuts', $shortcuts);
208         $savant->assign('cid', $cid);
209 }
210
211 // display pre-tests
212 // get a list of all the tests we have, and links to create, edit, delete, preview 
213 $sql    = "SELECT *, UNIX_TIMESTAMP(start_date) AS us, UNIX_TIMESTAMP(end_date) AS ue 
214              FROM ".TABLE_PREFIX."tests 
215             WHERE course_id=$_SESSION[course_id] 
216             ORDER BY start_date DESC";
217 $result = mysql_query($sql, $db);
218 $num_tests = mysql_num_rows($result);
219
220 $i = 0;
221 while($row = mysql_fetch_assoc($result))
222 {
223         $results[$i]['test_id'] = $row['test_id'];
224         $results[$i]['title'] = $row['title'];
225         
226         if ( ($row['us'] <= time()) && ($row['ue'] >= time() ) ) {
227                 $results[$i]['status'] = '<em>'._AT('ongoing').'</em>';
228         } else if ($row['ue'] < time() ) {
229                 $results[$i]['status'] = '<em>'._AT('expired').'</em>';
230         } else if ($row['us'] > time() ) {
231                 $results[$i]['status'] = '<em>'._AT('pending').'</em>';
232         } 
233
234         $startend_date_format=_AT('startend_date_format'); 
235
236         $results[$i]['availability'] = AT_date($startend_date_format, $row['start_date'], AT_DATE_MYSQL_DATETIME). ' ' ._AT('to_2').' ';
237         $results[$i]['availability'] .= AT_date($startend_date_format, $row['end_date'], AT_DATE_MYSQL_DATETIME);
238         
239         // get result release
240         if ($row['result_release'] == AT_RELEASE_IMMEDIATE)
241                 $results[$i]['result_release'] = _AT('release_immediate');
242         else if ($row['result_release'] == AT_RELEASE_MARKED)
243                 $results[$i]['result_release'] = _AT('release_marked');
244         else if ($row['result_release'] == AT_RELEASE_NEVER)
245                 $results[$i]['result_release'] = _AT('release_never');
246                 
247         //get # marked submissions
248         $sql_sub = "SELECT COUNT(*) AS sub_cnt FROM ".TABLE_PREFIX."tests_results WHERE status=1 AND test_id=".$row['test_id'];
249         $result_sub     = mysql_query($sql_sub, $db);
250         $row_sub = mysql_fetch_assoc($result_sub);
251         $results[$i]['submissions'] = $row_sub['sub_cnt'].' '._AT('submissions').', ';
252
253         //get # submissions
254         $sql_sub = "SELECT COUNT(*) AS marked_cnt FROM ".TABLE_PREFIX."tests_results WHERE status=1 AND test_id=".$row['test_id']." AND final_score=''";
255         $result_sub     = mysql_query($sql_sub, $db);
256         $row_sub = mysql_fetch_assoc($result_sub);
257         $results[$i]['submissions'] .= $row_sub['marked_cnt'].' '._AT('unmarked');
258
259         //get assigned groups
260         $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'];
261         $result_sub     = mysql_query($sql_sub, $db);
262         if (mysql_num_rows($result_sub) == 0) {
263                 $results[$i]['assign_to'] = _AT('everyone');
264         } else {
265                 $row_sub = mysql_fetch_assoc($result_sub);
266                 $results[$i]['assign_to'] = $row_sub['title'];
267                 do {
268                         $results[$i]['assign_to'] .= ', '.$row_sub['title'];
269                 } while ($row_sub = mysql_fetch_assoc($result_sub));
270         }
271         
272         if ($row['passscore'] == 0 && $row['passpercent'] == 0)
273                 $results[$i]['pass_score'] = _AT('no_pass_score');
274         else if ($row['passscore'] <> 0)
275                 $results[$i]['pass_score'] = $row['passscore'];
276         else if ($row['passpercent'] <> 0)
277                 $results[$i]['pass_score'] = $row['passpercent'].'%';
278                 
279         $i++;
280 }
281
282 if (isset($results)) $savant->assign('pretests', $results);
283
284 // set release date
285 if (!isset($release_date)) $release_date = date('Y-m-d H:i:s');
286
287 $_POST['day']   = substr($release_date, 8, 2);
288 $_POST['month'] = substr($release_date, 5, 2);
289 $_POST['year']  = substr($release_date, 0, 4);
290 $_POST['hour']  = substr($release_date, 11, 2);
291 $_POST['min']= substr($release_date, 14, 2);
292
293 if ($pid > 0) $savant->assign('pid', $pid);
294
295 require(AT_INCLUDE_PATH.'header.inc.php');
296 $savant->display('editor/edit_content_folder.tmpl.php');
297 require(AT_INCLUDE_PATH.'footer.inc.php');
298
299 //save last visit page.
300 $_SESSION['last_visited_page'] = $server_protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
301 ?>