44fb2f50d2240daa4b5acf09fdfeedb425ec7c17
[atutor.git] / docs / mods / _core / glossary / tools / add.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
15 define('AT_INCLUDE_PATH', '../../../../include/');
16 require(AT_INCLUDE_PATH.'vitals.inc.php');
17
18 authenticate(AT_PRIV_GLOSSARY);
19
20 if ($_POST['cancel']) { 
21         $msg->addFeedback('CANCELLED');
22         header('Location: index.php');
23         exit;
24 }
25
26 if (isset($_POST['submit'])) {
27         $num_terms = intval($_POST['num_terms']);
28         $missing_fields = array();
29
30         for ($i=0; $i<$num_terms; $i++) {
31
32                 if ($_POST['ignore'][$i] == '') {
33                         $_POST['word'][$i] = trim($_POST['word'][$i]);
34                         $_POST['definition'][$i] = trim($_POST['definition'][$i]);
35
36                         if ($_POST['word'][$i] == '') {
37                                 $missing_fields[] = _AT('glossary_term');
38                         } else{
39                                 //60 is defined by the sql
40                                 $_POST['word'][$i] = validate_length($_POST['word'][$i], 60);
41                         }
42                         
43
44                         if ($_POST['definition'][$i] == '') {
45                                 $missing_fields[] = _AT('glossary_definition');
46                         }
47
48                         if ($terms_sql != '') {
49                                 $terms_sql .= ', ';
50                         }
51
52                         $_POST['related_term'][$i] = intval($_POST['related_term'][$i]);
53
54                         /* for each item check if it exists: */
55
56                         if ($glossary[urlencode($_POST['word'][$i])] != '' ) {
57                                 $errors = array('TERM_EXISTS', $_POST['word'][$i]);
58                                 $msg->addError($errors);
59                         } else {
60                                 $_POST['word'][$i]         = $addslashes($_POST['word'][$i]);
61                                 $_POST['definition'][$i]   = $addslashes($_POST['definition'][$i]);
62                                 $_POST['related_term'][$i] = $addslashes($_POST['related_term'][$i]);
63
64                                 $terms_sql .= "(NULL, $_SESSION[course_id], '{$_POST[word][$i]}', '{$_POST[definition][$i]}', {$_POST[related_term][$i]})";
65                         }
66                 }
67         }
68
69         if ($missing_fields) {
70                 $missing_fields = implode(', ', $missing_fields);
71                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
72         }
73
74         if (!$msg->containsErrors()) {
75                 $sql = "INSERT INTO ".TABLE_PREFIX."glossary VALUES $terms_sql";
76                 $result = mysql_query($sql, $db);
77
78                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
79                 header('Location: index.php');
80                 exit;
81         }
82         $_GET['pcid'] = $_POST['pcid'];
83 }
84
85 $onload = 'document.form.title0.focus();';
86
87 unset($word);
88
89 $num_terms = 1;
90
91 require(AT_INCLUDE_PATH.'header.inc.php');
92
93 ?>
94
95 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
96 <input type="hidden" name="num_terms" value="<?php echo $num_terms; ?>" />
97 <?php
98 for ($i=0;$i<$num_terms;$i++) {
99         if ($glossary[$word[$i]] != '') {
100                 echo '<input type="hidden" name="ignore['.$i.']" value="1" />';
101                 continue;
102         }
103         
104         for ($j=0;$j<$i;$j++) {
105                 if ($word[$j] == $word[$i]) {
106                         echo '<input type="hidden" name="ignore['.$i.']" value="1" />';
107                         continue 2;
108                 }
109         }
110
111         if ($word[$i] == '') {
112                 $word[$i] = ContentManager::cleanOutput($_POST['word'][$i]);
113         }
114 ?>
115 <div class="input-form">
116         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('add_glossary'); ?></legend>
117         <div class="row">
118                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="title<?php echo $i; ?>"><?php echo _AT('glossary_term');  ?></label><br />
119                 <input type="text" name="word[<?php echo $i; ?>]" size="30" value="<?php echo trim($word[$i]); ?>" id="title<?php echo $i; ?>" /><?php                  
120                 if ($_GET['pcid'] != '') { 
121                         echo '<input type="checkbox" name="ignore['.$i.']" value="1" id="ig'.$i.'" /><label for="ig'.$i.'">Ignore this term</label>.';  
122                 }
123                 ?>
124         </div>
125
126         <div class="row">
127                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="body<?php echo $i; ?>"><?php echo _AT('glossary_definition');  ?></label><br />
128                 <textarea name="definition[<?php echo $i; ?>]" class="formfield" cols="55" rows="7" id="body<?php echo $i; ?>" style="width:90%;"><?php echo ContentManager::cleanOutput($_POST['definition'][$i]); ?></textarea>
129         </div>
130
131         <div class="row">
132         <?php echo _AT('glossary_related');  ?><br />
133         <?php
134                         $sql = "SELECT * FROM ".TABLE_PREFIX."glossary WHERE course_id=$_SESSION[course_id] ORDER BY word";
135                         $result = mysql_query($sql, $db);
136                         if ($row_g = mysql_fetch_assoc($result)) {
137                                 echo '<select name="related_term['.$i.']">';
138                                 echo '<option value="0"></option>';
139                                 do {
140                                         echo '<option value="'.$row_g['word_id'].'">'.$row_g['word'].'</option>';
141                                 } while ($row_g = mysql_fetch_assoc($result));
142                                 echo '</select>';
143                         } else {
144                                 echo _AT('none_available');
145                         }
146                 } // endfor
147         ?>
148         </div>
149
150         <div class="row buttons">
151                 <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" accesskey="s" />
152                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
153         </div>
154         </fieldset>
155 </div>
156 </form>
157
158 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>