course_edit.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php // $Id: course_edit.php 20441 2009-05-10 07:39:15Z ivantcholakov $
  2. /* For licensing terms, see /dokeos_license.txt */
  3. /**
  4. ==============================================================================
  5. * @package dokeos.admin
  6. ==============================================================================
  7. */
  8. /*
  9. ==============================================================================
  10. INIT SECTION
  11. ==============================================================================
  12. */
  13. // name of the language file that needs to be included
  14. $language_file = 'admin';
  15. $cidReset = true;
  16. include ('../inc/global.inc.php');
  17. $this_section=SECTION_PLATFORM_ADMIN;
  18. api_protect_admin_script();
  19. include (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  20. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  21. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  22. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  23. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  24. $course_code = isset($_GET['course_code']) ? $_GET['course_code'] : $_POST['code'];
  25. $noPHP_SELF = true;
  26. $tool_name = get_lang('ModifyCourseInfo');
  27. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  28. $interbreadcrumb[] = array ("url" => "course_list.php", "name" => get_lang('AdminCourses'));
  29. define('USER_FIELD_TYPE_CHECKBOX', 10);
  30. /*
  31. -----------------------------------------------------------
  32. Libraries
  33. -----------------------------------------------------------
  34. */
  35. /*
  36. ==============================================================================
  37. FUNCTIONS
  38. ==============================================================================
  39. */
  40. /*
  41. ==============================================================================
  42. MAIN CODE
  43. ==============================================================================
  44. */
  45. // Get all course categories
  46. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  47. //Get the course infos
  48. $sql = "SELECT * FROM $course_table WHERE code='".Database::escape_string($course_code)."'";
  49. $result = Database::query($sql);
  50. if (Database::num_rows($result) != 1)
  51. {
  52. header('Location: course_list.php');
  53. exit ();
  54. }
  55. $course = Database::fetch_array($result,'ASSOC');
  56. // Get course teachers
  57. $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  58. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname' : ' ORDER BY lastname, firstname';
  59. $sql = "SELECT user.user_id,lastname,firstname FROM $table_user as user,$table_course_user as course_user WHERE course_user.status='1' AND course_user.user_id=user.user_id AND course_user.course_code='".$course_code."'".$order_clause;
  60. $res = Database::query($sql);
  61. $course_teachers = array();
  62. while($obj = Database::fetch_object($res))
  63. {
  64. $course_teachers[$obj->user_id] = api_get_person_name($obj->firstname, $obj->lastname);
  65. }
  66. // Get all possible teachers without the course teachers
  67. $sql = "SELECT user_id,lastname,firstname FROM $table_user WHERE status='1'".$order_clause;
  68. $res = Database::query($sql);
  69. $teachers = array();
  70. $platform_teachers[0] = '-- '.get_lang('NoManager').' --';
  71. while($obj = Database::fetch_object($res))
  72. {
  73. if(!array_key_exists($obj->user_id,$course_teachers)){
  74. $teachers[$obj->user_id] = api_get_person_name($obj->firstname, $obj->lastname);
  75. }
  76. if($course['tutor_name']==$course_teachers[$obj->user_id]){
  77. $course['tutor_name']=$obj->user_id;
  78. }
  79. //We add in the array platform teachers
  80. $platform_teachers[$obj->user_id] = $obj->firstname.' '.$obj->lastname;
  81. }
  82. //Case where there is no teacher in the course
  83. if(count($course_teachers)==0){
  84. $sql='SELECT tutor_name FROM '.$course_table.' WHERE code="'.$course_code.'"';
  85. $res = Database::query($sql);
  86. $tutor_name=Database::result($res,0,0);
  87. $course['tutor_name']=array_search($tutor_name,$platform_teachers);
  88. }
  89. // Build the form
  90. $form = new FormValidator('update_course');
  91. $form->addElement('hidden','code',$course_code);
  92. //title
  93. $form->add_textfield( 'title', get_lang('Title'),true, array ('size' => '60'));
  94. $form->applyFilter('title','html_filter');
  95. $form->applyFilter('title','trim');
  96. // code
  97. $form->add_textfield('visual_code', get_lang('CourseCode'));
  98. $form->applyFilter('visual_code','strtoupper');
  99. $form->applyFilter('visual_code','html_filter');
  100. //$form->add_textfield('tutor_name', get_lang('CourseTitular'));
  101. $form->addElement('select', 'tutor_name', get_lang('CourseTitular'), $platform_teachers);
  102. $form->applyFilter('tutor_name','html_filter');
  103. //$form->addElement('select', 'course_teachers', get_lang('CourseTeachers'), $teachers, 'multiple=multiple size="4" style="width: 150px;"');
  104. $group=array();
  105. $group[] = FormValidator::createElement('select', 'platform_teachers', '', $teachers, 'id="platform_teachers" multiple=multiple size="4" style="width: 150px;"');
  106. $group[] = FormValidator::createElement('select', 'course_teachers', '', $course_teachers, 'id="course_teachers" multiple=multiple size="4" style="width: 150px;"');
  107. $element_template = <<<EOT
  108. <div class="row">
  109. <div class="label">
  110. <!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->{label}
  111. </div>
  112. <div class="formw" style="display:inline">
  113. <table cellpadding="0" cellspacing="0">
  114. <tr>
  115. <!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error --> <td>{element}</td>
  116. </tr>
  117. </table>
  118. </div>
  119. </div>
  120. EOT;
  121. $renderer = $form->defaultRenderer();
  122. $renderer -> setElementTemplate($element_template, 'group');
  123. $form -> addGroup($group,'group',get_lang('CourseTeachers'),'</td><td width="80" align="center">'.
  124. '<input class="arrowr" style="width:30px;height:30px;padding-right:12px" type="button" onclick="moveItem(document.getElementById(\'platform_teachers\'), document.getElementById(\'course_teachers\'))" ><br><br>' .
  125. '<input class="arrowl" style="width:30px;height:30px;padding-left:13px" type="button" onclick="moveItem(document.getElementById(\'course_teachers\'), document.getElementById(\'platform_teachers\'))" ></td><td>');
  126. $categories_select = $form->addElement('select', 'category_code', get_lang('CourseFaculty'), $categories);
  127. CourseManager::select_and_sort_categories($categories_select);
  128. $form->add_textfield( 'department_name', get_lang('CourseDepartment'), false,array ('size' => '60'));
  129. $form->applyFilter('department_name','html_filter');
  130. $form->applyFilter('department_name','trim');
  131. $form->add_textfield( 'department_url', get_lang('CourseDepartmentURL'),false, array ('size' => '60'));
  132. $form->applyFilter('department_url','html_filter');
  133. $form->applyFilter('department_url','trim');
  134. $form->addElement('select_language', 'course_language', get_lang('CourseLanguage'));
  135. $form->applyFilter('select_language','html_filter');
  136. $form->addElement('radio', 'visibility', get_lang("CourseAccess"), get_lang('OpenToTheWorld'), COURSE_VISIBILITY_OPEN_WORLD);
  137. $form->addElement('radio', 'visibility', null, get_lang('OpenToThePlatform'), COURSE_VISIBILITY_OPEN_PLATFORM);
  138. $form->addElement('radio', 'visibility', null, get_lang('Private'), COURSE_VISIBILITY_REGISTERED);
  139. $form->addElement('radio', 'visibility', null, get_lang('CourseVisibilityClosed'), COURSE_VISIBILITY_CLOSED);
  140. $form->addElement('radio', 'subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
  141. $form->addElement('radio', 'subscribe', null, get_lang('Denied'), 0);
  142. $form->addElement('radio', 'unsubscribe', get_lang('Unsubscription'), get_lang('AllowedToUnsubscribe'), 1);
  143. $form->addElement('radio', 'unsubscribe', null, get_lang('NotAllowedToUnsubscribe'), 0);
  144. $form->addElement('text','disk_quota',get_lang('CourseQuota'));
  145. $form->addRule('disk_quota', get_lang('ThisFieldIsRequired'),'required');
  146. $form->addRule('disk_quota',get_lang('ThisFieldShouldBeNumeric'),'numeric');
  147. $list_course_extra_field = CourseManager::get_course_extra_field_list($course_code);
  148. foreach($list_course_extra_field as $extra_field){
  149. switch($extra_field['field_type']){
  150. /* case USER_FIELD_TYPE_TEXT:
  151. case USER_FIELD_TYPE_TEXTAREA:
  152. case USER_FIELD_TYPE_RADIO: */
  153. case USER_FIELD_TYPE_CHECKBOX:
  154. $checked = (array_key_exists('extra_field_value', $extra_field) && $extra_field['extra_field_value'] == 1)? array('checked'=>'checked'): '';
  155. $form->addElement('hidden', '_extra_'.$extra_field['field_variable'], 0);
  156. $field_display_text=$extra_field['field_display_text'];
  157. $form->addElement('checkbox', 'extra_'.$extra_field['field_variable'],get_lang('SpecialCourse') , get_lang($extra_field['field_default_value']), $checked);
  158. break;
  159. /* case USER_FIELD_TYPE_SELECT_MULTIPLE:
  160. case USER_FIELD_TYPE_DATE:
  161. case USER_FIELD_TYPE_DATETIME:
  162. case USER_FIELD_TYPE_DOUBLE_SELECT:
  163. case USER_FIELD_TYPE_DIVIDER: */
  164. }
  165. }
  166. $form->addElement('style_submit_button', 'button', get_lang('ModifyCourseInfo'),'onclick="valide()"; class="save"');
  167. // Set some default values
  168. $course_db_name = $course['db_name'];
  169. $course['title'] = api_html_entity_decode($course['title'], ENT_QUOTES, $charset);
  170. $form->setDefaults($course);
  171. // Validate form
  172. if( $form->validate())
  173. {
  174. $course = $form->getSubmitValues();
  175. $dbName = $_POST['dbName'];
  176. $course_code = $course['code'];
  177. $visual_code = $course['visual_code'];
  178. // Check if the visual code is already used by *another* course
  179. $visual_code_is_used = false;
  180. $warn = get_lang('TheFollowingCoursesAlreadyUseThisVisualCode').':';
  181. if (!empty($visual_code)) {
  182. $list = CourseManager::get_courses_info_from_visual_code($visual_code);
  183. foreach ($list as $course_temp) {
  184. if ($course_temp['code'] != $course_code) {
  185. $visual_code_is_used = true;
  186. $warn .= ' '.$course_temp['title'].' ('.$course_temp['code'].'),';
  187. }
  188. }
  189. $warn = substr($warn,0,-1);
  190. }
  191. // an extra field
  192. $extras = array();
  193. foreach($course as $key => $value) {
  194. if(substr($key,0,6)=='extra_') {
  195. $extras[substr($key,6)] = $value;
  196. }
  197. if(substr($key,0,7)=='_extra_') {
  198. if(!array_key_exists(substr($key,7), $extras)) $extras[substr($key,7)] = $value;
  199. }
  200. }
  201. $tutor_id = $course['tutor_name'];
  202. $tutor_name=$platform_teachers[$tutor_id];
  203. $teachers = $course['group']['course_teachers'];
  204. $title = $course['title'];
  205. $category_code = $course['category_code'];
  206. $department_name = $course['department_name'];
  207. $department_url = $course['department_url'];
  208. $course_language = $course['course_language'];
  209. $disk_quota = $course['disk_quota'];
  210. $visibility = $course['visibility'];
  211. $subscribe = $course['subscribe'];
  212. $unsubscribe = $course['unsubscribe'];
  213. if (!stristr($department_url, 'http://'))
  214. {
  215. $department_url = 'http://'.$department_url;
  216. }
  217. $sql = "UPDATE $course_table SET course_language='".Database::escape_string($course_language)."',
  218. title='".Database::escape_string($title)."',
  219. category_code='".Database::escape_string($category_code)."',
  220. tutor_name='".Database::escape_string($tutor_name)."',
  221. visual_code='".Database::escape_string($visual_code)."',
  222. department_name='".Database::escape_string($department_name)."',
  223. department_url='".Database::escape_string($department_url)."',
  224. disk_quota='".Database::escape_string($disk_quota)."',
  225. visibility = '".Database::escape_string($visibility)."',
  226. subscribe = '".Database::escape_string($subscribe)."',
  227. unsubscribe='".Database::escape_string($unsubscribe)."'
  228. WHERE code='".Database::escape_string($course_code)."'";
  229. Database::query($sql);
  230. //update the extra fields
  231. if(count($extras) > 0){
  232. foreach($extras as $key => $value) {
  233. CourseManager::update_course_extra_field_value($course_code, $key, $value);
  234. }
  235. }
  236. //Delete only teacher relations that doesn't match the selected teachers
  237. $cond='';
  238. if(count($teachers)>0){
  239. foreach($teachers as $key) $cond.=" AND user_id<>'".$key."'";
  240. }
  241. $sql='DELETE FROM '.$course_user_table.' WHERE course_code="'.Database::escape_string($course_code).'" AND status="1"'.$cond;
  242. Database::query($sql);
  243. if(count($teachers)>0){
  244. foreach($teachers as $key){
  245. //We check if the teacher is already subscribed in this course
  246. $sql_select_teacher = 'SELECT 1 FROM '.$course_user_table.' WHERE user_id = "'.$key.'" AND course_code = "'.$course_code.'" ';
  247. $result = Database::query($sql_select_teacher);
  248. if(Database::num_rows($result) == 1){
  249. $sql = 'UPDATE '.$course_user_table.' SET status = "1" WHERE course_code = "'.$course_code.'" AND user_id = "'.$key.'" ';
  250. }
  251. else{
  252. $sql = "INSERT INTO ".$course_user_table . " SET
  253. course_code = '".Database::escape_string($course_code). "',
  254. user_id = '".$key . "',
  255. status = '1',
  256. role = '',
  257. tutor_id='0',
  258. sort='0',
  259. user_course_cat='0'";
  260. }
  261. Database::query($sql);
  262. }
  263. }
  264. $sql = "INSERT IGNORE INTO ".$course_user_table . " SET
  265. course_code = '".Database::escape_string($course_code). "',
  266. user_id = '".$tutor_id . "',
  267. status = '1',
  268. role = '',
  269. tutor_id='0',
  270. sort='0',
  271. user_course_cat='0'";
  272. Database::query($sql);
  273. $forum_config_table = Database::get_course_table(TOOL_FORUM_CONFIG_TABLE,$course_db_name);
  274. $sql = "UPDATE ".$forum_config_table." SET default_lang='".Database::escape_string($course_language)."'";
  275. if ($visual_code_is_used == true) {
  276. header('Location: course_list.php?action=show_msg&warn='.urlencode($warn));
  277. } else {
  278. header('Location: course_list.php');
  279. }
  280. exit ();
  281. }
  282. Display::display_header($tool_name);
  283. echo "<script>
  284. function moveItem(origin , destination){
  285. for(var i = 0 ; i<origin.options.length ; i++) {
  286. if(origin.options[i].selected) {
  287. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  288. origin.options[i]=null;
  289. i = i-1;
  290. }
  291. }
  292. destination.selectedIndex = -1;
  293. sortOptions(destination.options);
  294. }
  295. function sortOptions(options) {
  296. newOptions = new Array();
  297. for (i = 0 ; i<options.length ; i++)
  298. newOptions[i] = options[i];
  299. newOptions = newOptions.sort(mysort);
  300. options.length = 0;
  301. for(i = 0 ; i < newOptions.length ; i++)
  302. options[i] = newOptions[i];
  303. }
  304. function mysort(a, b){
  305. if(a.text.toLowerCase() > b.text.toLowerCase()){
  306. return 1;
  307. }
  308. if(a.text.toLowerCase() < b.text.toLowerCase()){
  309. return -1;
  310. }
  311. return 0;
  312. }
  313. function valide(){
  314. var options = document.getElementById('course_teachers').options;
  315. for (i = 0 ; i<options.length ; i++)
  316. options[i].selected = true;
  317. document.update_course.submit();
  318. }
  319. </script>";
  320. //api_display_tool_title($tool_name);
  321. // Display the form
  322. $form->display();
  323. /*
  324. ==============================================================================
  325. FOOTER
  326. ==============================================================================
  327. */
  328. Display :: display_footer();
  329. ?>