changed git call from https to git readonly
[atutor.git] / mods / basiclti / tool / content_edit.php
1 <?php
2 define('AT_INCLUDE_PATH', '../../../include/');
3 require(AT_INCLUDE_PATH.'vitals.inc.php');
4 authenticate(AT_PRIV_BASICLTI);
5
6 require_once('forms.php');
7
8 ;
9 // Filter all GET data //
10 $_POST['framed'] = intval($_POST['framed']);
11 $_POST['popup'] = intval($_POST['popup']);
12 $_POST['cid'] = intval($_POST['cid']);
13
14 if ( !is_int($_SESSION['course_id']) || $_SESSION['course_id'] < 1 ) {
15     $msg->addFeedback('NEED_COURSE_ID');
16     exit;
17 }
18
19 // Add/Update The Tool
20 if ( isset($_POST['toolid']) && at_form_validate($blti_content_edit_form, $msg)) {
21     $toolid = $_POST['toolid']; // Escaping is done in the at_form_util code
22     $sql = "SELECT * FROM ".TABLE_PREFIX."basiclti_content
23             WHERE content_id=".$_POST[cid]." AND course_id=".$_SESSION[course_id];
24
25
26     $result = mysql_query($sql, $db);
27     if ( $toolid == '--none--' ) {
28         $sql = "DELETE FROM ". TABLE_PREFIX . "basiclti_content 
29                        WHERE content_id=".$_POST[cid]." AND 
30                              course_id=".$_SESSION[course_id];
31             $result = mysql_query($sql, $db);
32             if ($result===false) {
33                 $msg->addError('MYSQL_FAILED');
34             } else {
35                 $msg->addFeedback('BASICLTI_DELETED');
36             }
37     } else if ( mysql_num_rows($result) == 0 ) {
38             $sql = "INSERT INTO ". TABLE_PREFIX . "basiclti_content 
39                        SET toolid='".$toolid."', content_id=".$_POST[cid].",
40                              course_id=".$_SESSION[course_id];
41
42             $result = mysql_query($sql, $db);
43             if ($result===false) {
44                 $msg->addError('MYSQL_FAILED');
45             } else {
46                 $msg->addFeedback('BASICLTI_SAVED');
47             }
48
49     } else if ( $result !== false ) {
50
51             $gradebook_test_id = 0;
52             $basiclti_content_row = mysql_fetch_assoc($result);
53             $placementsecret = $basiclti_content_row['placementsecret'];
54             $gradebook_check = intval($_POST['gradebook_test_id']);
55             if ( isset($_POST['gradebook_test_id']) && $gradebook_check > 0 ) {
56                 $gradebook_test_id = $gradebook_check;
57                 $sql = "SELECT g.gradebook_test_id AS id, g.title AS title
58                         FROM  ".TABLE_PREFIX."gradebook_tests AS g
59                         WHERE g.course_id = ".$_SESSION[course_id]."
60                         AND g.type = 'External' and g.grade_scale_id = 0
61                         AND gradebook_test_id = ".$gradebook_test_id;
62                 $result = mysql_query($sql, $db);
63                 if ( $result === false ) {
64                     $gradebook_test_id = 0;
65                 } else {
66                     if ( strlen($placementsecret) < 1 ) {
67                         $placementsecret = uniqid("bl",true);
68                     }
69                 }
70             }
71             // Override these fields (don't take from form)
72             $fields = array('toolid' => $toolid, 'gradebook_test_id' => $gradebook_test_id,
73                             'placementsecret' => $placementsecret);
74             $sql = at_form_update($_POST, $blti_content_edit_form, $fields);
75             $sql = "UPDATE ". TABLE_PREFIX . "basiclti_content 
76                        SET ".$sql." WHERE content_id=".$_POST[cid]." AND 
77                            course_id=".$_SESSION[course_id];
78             $result = mysql_query($sql, $db);
79             if ($result===false) {
80                 $msg->addError('MYSQL_FAILED');
81             } else {
82                 $msg->addFeedback('BASICLTI_SAVED');
83             }
84     }
85 }
86
87 // echo("<hr>$sql<hr>\n");
88
89 $cid = intval($_REQUEST['cid']);
90
91 global $framed, $popup;
92
93 if ((isset($_REQUEST['popup']) && $_REQUEST['popup']) &&
94     (!isset($_REQUEST['framed']) || !$_REQUEST['framed'])) {
95     $popup = TRUE;
96     $framed = FALSE;
97 } elseif (isset($_REQUEST['framed']) && $_REQUEST['framed'] && isset($_REQUEST['popup']) && $_REQUEST['popup']) {
98     $popup = TRUE;
99     $framed = TRUE;
100     $tool_flag = TRUE;
101 } else {
102     $popup = FALSE;
103     $framed = FALSE;
104 }
105
106 require(AT_INCLUDE_PATH.'header.inc.php');
107
108 /* get a list of all the tools, we have */
109 $sql    = "SELECT * FROM ".TABLE_PREFIX."basiclti_tools WHERE course_id = 0".
110           " OR course_id=".$_SESSION[course_id]." ORDER BY course_id,title";
111
112 $toolresult = mysql_query($sql, $db);
113 $num_tools = mysql_num_rows($toolresult);
114
115 //If there are no Tools, don't display anything except a message
116 if ($num_tools == 0){
117         $msg->addInfo('NO_PROXY_TOOLS');
118         $msg->printInfos();
119         return;
120 }
121
122 ?>
123 <div class="input-form">
124
125 <form name="datagrid" action="" method="POST">
126
127 <fieldset class="group_form">
128    <legend class="group_form"><?php echo _AT('bl_content_title'); ?></legend>
129 <br/>
130 <?php echo _AT('basiclti_comment');?>
131 <br/>
132 <?php echo $msg->printFeedbacks();
133
134 // Get the current content item
135 $sql = "SELECT * FROM ".TABLE_PREFIX."basiclti_content 
136                 WHERE content_id=$cid";
137 $contentresult = mysql_query($sql, $db);
138 $basiclti_content_row = mysql_fetch_assoc($contentresult);
139 // if ( $basiclti_content_row ) echo("FOUND"); else echo("NOT");
140 ?>
141 <div class="row">
142    <?php echo _AT('bl_choose_tool'); ?><br/>
143    <select id="toolid" name="toolid" onchange="datagrid.submit();"> 
144       <option value="--none--">&nbsp;</option><?php
145       $basiclti_tool_row = false;
146       $found = false;  // Only the first one
147       while ( $tool = mysql_fetch_assoc($toolresult) ) {
148          $selected = "";
149          if ( ! $found && $tool['toolid'] == $basiclti_content_row['toolid'] ) {
150            $selected = ' selected="yes"';
151            $basiclti_tool_row = $tool;
152            $found = true;
153          }
154          echo '<option value="'.$tool['toolid'].'"'.$selected.'>'.$tool['title']."</option>\n";
155       } ?>
156    </select>
157 <div>
158 <?php
159 if ( $basiclti_tool_row != false && $basiclti_tool_row['acceptgrades'] == 1 ) {
160     $sql = "SELECT g.gradebook_test_id AS id, g.title AS title
161             FROM  ".TABLE_PREFIX."gradebook_tests AS g
162             WHERE g.course_id = ".$_SESSION[course_id]."
163             AND g.type = 'External' and g.grade_scale_id = 0";
164     $graderesult = mysql_query($sql, $db);
165     if ( $graderesult !== false && mysql_num_rows($graderesult) > 0) { ?>
166 <div class="row">
167    <?php echo _AT('bl_choose_gradbook_entry'); ?><br/>
168         <select id="gradebook_test_id" name="gradebook_test_id"> 
169            <option value="--none--">&nbsp;</option><?php
170         while ( $gradeitem = mysql_fetch_assoc($graderesult) ) {
171             echo($gradeitem['title']);
172             $selected = "";
173             if ( $gradeitem['id'] == $basiclti_content_row['gradebook_test_id'] ) {
174               $selected = ' selected="yes"';
175             }
176             echo '<option value="'.$gradeitem['id'].'"'.$selected.'>'.$gradeitem['title']."</option>\n";
177         } ?>
178         </select> 
179 </div> <?php
180     }
181 }
182 ?>
183    <input type="hidden" name="cid" value="<?php echo($cid);?>" />
184 <?php
185 if ( $basiclti_tool_row !== false ) {
186     $blti_content_edit_form = filterForm($basiclti_tool_row, $blti_content_edit_form);
187     at_form_generate($basiclti_content_row, $blti_content_edit_form);
188    echo('<input type="submit" name="save" value="Save" class="button" />'."\n");
189 }
190 ?>
191 </div>
192 </legend>
193 </form>
194 </div>
195 <?php 
196 if($basiclti_tool_row){
197         echo '<h3>'.$basiclti_tool_row['title'].' '._AT('bl_settings').'</h3>';
198         echo '<ul style="list-style-type:none;">';
199         foreach($basiclti_tool_row as $title=>$setting){
200                 if($title == "password" || $title == "resourcekey" && $basiclti_tool_row['course_id'] == 0){
201                         // Hide the tool password if its not an instructor created tool //
202                         // Don't print out the key or password id its an admin added tool
203                         continue;
204                 } else {
205
206                         echo '<li>'.$title.' = '.$setting.'</li>';
207                 }
208         }
209         echo '</ul>';
210 }
211 //echo("<hr><pre>\n");print_r($basiclti_tool_row); echo("\n</pre>\n"); 
212 ?>
213 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>