search_course_widget.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. /* For license terms, see /license.txt */
  3. require_once __DIR__.'/register_course_widget.class.php';
  4. /**
  5. * Search course widget.
  6. * Display a search form and a list of courses that matches the search.
  7. *
  8. * @copyright (c) 2011 University of Geneva
  9. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  10. * @author Laurent Opprecht
  11. */
  12. class SearchCourseWidget
  13. {
  14. const PARAM_ACTION = 'action';
  15. const ACTION_SUBSCRIBE = 'subscribe';
  16. /**
  17. * Returns $_POST data for $key is it exists or $default otherwise.
  18. *
  19. * @param string $key
  20. * @param object $default
  21. * @return string
  22. */
  23. public static function post($key, $default = '')
  24. {
  25. return isset($_POST[$key]) ? $_POST[$key] : $default;
  26. }
  27. /**
  28. * Returns $_GET data for $key is it exists or $default otherwise.
  29. *
  30. * @param string $key
  31. * @param object $default
  32. * @return string
  33. */
  34. public static function get($key, $default = '')
  35. {
  36. return isset($_GET[$key]) ? $_GET[$key] : $default;
  37. }
  38. public static function server($key, $default = '')
  39. {
  40. return isset($_SERVER[$key]) ? $_SERVER[$key] : $default;
  41. }
  42. public static function get_lang($name)
  43. {
  44. return SearchCoursePlugin::create()->get_lang($name);
  45. }
  46. /**
  47. *
  48. * @return bool
  49. */
  50. function is_homepage()
  51. {
  52. $url = self::server('REQUEST_URI');
  53. $url = explode('?', $url);
  54. $url = reset($url);
  55. $url = self::server('SERVER_NAME').$url;
  56. $root = api_get_path('WEB_PATH');
  57. $root = str_replace('https://', '', $root);
  58. $root = str_replace('http://', '', $root);
  59. $index_url = $root.'index.php';
  60. return $url == $index_url || $url == $root;
  61. }
  62. /**
  63. *
  64. * @return bool
  65. */
  66. function is_user_portal()
  67. {
  68. $url = self::server('REQUEST_URI');
  69. $url = explode('?', $url);
  70. $url = reset($url);
  71. $url = self::server('SERVER_NAME').$url;
  72. $root = api_get_path('WEB_PATH');
  73. $root = str_replace('https://', '', $root);
  74. $root = str_replace('http://', '', $root);
  75. $index_url = $root.'user_portal.php';
  76. return $url == $index_url || $url == $root;
  77. }
  78. /**
  79. *
  80. */
  81. function accept()
  82. {
  83. return $this->is_homepage() || $this->is_user_portal();
  84. }
  85. /**
  86. * Display the search course widget:
  87. *
  88. * Title
  89. * Search form
  90. *
  91. * Search results
  92. */
  93. function run()
  94. {
  95. if (!$this->accept())
  96. {
  97. return;
  98. }
  99. $this->display_header();
  100. $this->display_form();
  101. $search_term = self::post('search_term');
  102. $action = self::get('action');
  103. $has_content = !empty($search_term) || !empty($action);
  104. if ($has_content)
  105. {
  106. echo '<div class="list">';
  107. }
  108. else
  109. {
  110. echo '<div>';
  111. }
  112. if (RegisterCourseWidget::factory()->run())
  113. {
  114. $result = true;
  115. }
  116. else
  117. {
  118. $result = $this->action_display();
  119. }
  120. echo '</div>';
  121. $this->display_footer();
  122. return $result;
  123. }
  124. function get_url($action = '')
  125. {
  126. $self = $_SERVER['PHP_SELF'];
  127. $parameters = array();
  128. if ($action)
  129. {
  130. $parameters[self::PARAM_ACTION] = $action;
  131. }
  132. $parameters = implode('&', $parameters);
  133. $parameters = $parameters ? '?'.$parameters : '';
  134. return $self.$parameters;
  135. }
  136. /**
  137. * Handle the display action
  138. */
  139. function action_display()
  140. {
  141. global $charset;
  142. $search_term = self::post('search_term');
  143. if ($search_term)
  144. {
  145. $search_result_for_label = self::get_lang('SearchResultsFor');
  146. $search_term_html = htmlentities($search_term, ENT_QUOTES, $charset);
  147. echo "<h5>$search_result_for_label $search_term_html</h5>";
  148. $courses = $this->retrieve_courses($search_term);
  149. $this->display_list($courses);
  150. }
  151. return true;
  152. }
  153. function display_header()
  154. {
  155. $search_course_label = self::get_lang('SearchCourse');
  156. echo <<<EOT
  157. <div class="well course_search">
  158. <div class="menusection">
  159. <h4>$search_course_label</h4>
  160. EOT;
  161. }
  162. function display_footer()
  163. {
  164. echo '</div></div>';
  165. }
  166. /**
  167. * Display the search course form.
  168. */
  169. function display_form()
  170. {
  171. global $stok;
  172. $search_label = self::get_lang('_search');
  173. $self = api_get_self();
  174. $search_term = self::post('search_term');
  175. $form = <<<EOT
  176. <form class="course_list" method="post" action="$self">
  177. <input type="hidden" name="sec_token" value="$stok" />
  178. <input type="hidden" name="search_course" value="1" />
  179. <input type="text" name="search_term" class="span2" value="$search_term" />
  180. &nbsp;<input class="btn btn-default" type="submit" value="$search_label" />
  181. </form>
  182. EOT;
  183. echo $form;
  184. }
  185. /**
  186. *
  187. * @param array $courses
  188. * @return bool
  189. */
  190. function display_list($courses)
  191. {
  192. if (empty($courses)) {
  193. return false;
  194. }
  195. $user_courses = $this->retrieve_user_courses();
  196. $display_coursecode = (api_get_setting('display_coursecode_in_courselist') == 'true');
  197. $display_teacher = (api_get_setting('display_teacher_in_courselist') == 'true');
  198. echo '<table cellpadding="4">';
  199. foreach ($courses as $key => $course) {
  200. $details = array();
  201. if ($display_coursecode) {
  202. $details[] = $course['visual_code'];
  203. }
  204. if ($display_teacher) {
  205. $details[] = $course['tutor'];
  206. }
  207. $details = implode(' - ', $details);
  208. $title = $course['title'];
  209. $href = api_get_path(WEB_COURSE_PATH).$course['code'].'/index.php';
  210. echo '<tr><td><b><a href="'.$href.'">'."$title</a></b><br/>$details</td><td>";
  211. if (!api_is_anonymous()) {
  212. if ($course['registration_code']) {
  213. Display::display_icon(
  214. 'passwordprotected.png',
  215. '',
  216. array('style' => 'float:left;')
  217. );
  218. }
  219. $this->display_subscribe_icon($course, $user_courses);
  220. }
  221. echo '</td></tr>';
  222. }
  223. echo '</table>';
  224. return true;
  225. }
  226. /**
  227. * Displays the subscribe icon if subscribing is allowed and
  228. * if the user is not yet subscribed to this course
  229. *
  230. * @global type $stok
  231. * @param array $current_course
  232. * @param array $user_courses
  233. * @return bool
  234. */
  235. function display_subscribe_icon($current_course, $user_courses)
  236. {
  237. global $stok;
  238. //Already subscribed
  239. $code = $current_course['code'];
  240. if (isset($user_courses[$code]))
  241. {
  242. echo self::get_lang('AlreadySubscribed');
  243. return false;
  244. }
  245. //Not authorized to subscribe
  246. if ($current_course['subscribe'] != SUBSCRIBE_ALLOWED) {
  247. echo self::get_lang('SubscribingNotAllowed');
  248. return false;
  249. }
  250. //Subscribe form
  251. $self = $_SERVER['PHP_SELF'];
  252. echo <<<EOT
  253. <form action="$self?action=subscribe" method="post">
  254. <input type="hidden" name="sec_token" value="$stok" />
  255. <input type="hidden" name="subscribe" value="$code" />
  256. EOT;
  257. $search_term = $this->post('search_term');
  258. if ($search_term)
  259. {
  260. $search_term = Security::remove_XSS($search_term);
  261. echo <<<EOT
  262. <input type="hidden" name="search_course" value="1" />
  263. <input type="hidden" name="search_term" value="$search_term" />
  264. EOT;
  265. }
  266. echo '<input type="image" name="unsub" src="'.Display::returnIconPath('enroll.gif').'" alt="'.get_lang('Subscribe').'" />
  267. '.get_lang('Subscribe').'
  268. </form>
  269. ';
  270. return true;
  271. }
  272. /**
  273. * DB functions - DB functions - DB functions
  274. */
  275. /**
  276. * Search courses that match the search term.
  277. * Search is done on the code, title and tutor fields.
  278. *
  279. * @param string $search_term
  280. * @return array
  281. */
  282. function retrieve_courses($search_term)
  283. {
  284. if (empty($search_term))
  285. {
  286. return array();
  287. }
  288. $search_term = Database::escape_string($search_term);
  289. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  290. if (api_is_anonymous())
  291. {
  292. $course_fiter = 'visibility = '.COURSE_VISIBILITY_OPEN_WORLD;
  293. }
  294. else
  295. {
  296. $course_fiter = 'visibility = '.COURSE_VISIBILITY_OPEN_WORLD.' OR ';
  297. $course_fiter .= 'visibility = '.COURSE_VISIBILITY_OPEN_PLATFORM.' OR ';
  298. $course_fiter .= '(visibility = '.COURSE_VISIBILITY_REGISTERED.' AND subscribe = 1)';
  299. }
  300. $sql = <<<EOT
  301. SELECT * FROM $course_table
  302. WHERE ($course_fiter) AND (code LIKE '%$search_term%' OR visual_code LIKE '%$search_term%' OR title LIKE '%$search_term%' OR tutor_name LIKE '%$search_term%')
  303. ORDER BY title, visual_code ASC
  304. EOT;
  305. $result = array();
  306. $resultset = Database::query($sql);
  307. while ($row = Database::fetch_array($resultset)) {
  308. $code = $row['code'];
  309. $result[$code] = array(
  310. 'code' => $code,
  311. 'directory' => $row['directory'],
  312. 'visual_code' => $row['visual_code'],
  313. 'title' => $row['title'],
  314. 'tutor' => $row['tutor_name'],
  315. 'subscribe' => $row['subscribe'],
  316. 'unsubscribe' => $row['unsubscribe']
  317. );
  318. }
  319. return $result;
  320. }
  321. /**
  322. * Retrieves courses that the user is subscribed to
  323. *
  324. * @param int $user_id
  325. * @return array
  326. */
  327. function retrieve_user_courses($user_id = null)
  328. {
  329. if (is_null($user_id))
  330. {
  331. global $_user;
  332. $user_id = $_user['user_id'];
  333. }
  334. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  335. $user_course_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  336. $user_id = intval($user_id);
  337. $sql_select_courses = "SELECT course.code k, course.visual_code vc, course.subscribe subscr, course.unsubscribe unsubscr,
  338. course.title i, course.tutor_name t, course.directory dir, course_rel_user.status status,
  339. course_rel_user.sort sort, course_rel_user.user_course_cat user_course_cat
  340. FROM $course_table course, $user_course_table course_rel_user
  341. WHERE course.id = course_rel_user.c_id
  342. AND course_rel_user.user_id = $user_id
  343. ORDER BY course_rel_user.sort ASC";
  344. $result = array();
  345. $resultset = Database::query($sql_select_courses);
  346. while ($row = Database::fetch_array($resultset)) {
  347. $code = $row['k'];
  348. $result[$code] = array(
  349. 'code' => $code,
  350. 'visual_code' => $row['vc'],
  351. 'title' => $row['i'],
  352. 'directory' => $row['dir'],
  353. 'status' => $row['status'],
  354. 'tutor' => $row['t'],
  355. 'subscribe' => $row['subscr'],
  356. 'unsubscribe' => $row['unsubscr'],
  357. 'sort' => $row['sort'],
  358. 'user_course_category' => $row['user_course_cat']);
  359. }
  360. return $result;
  361. }
  362. /*
  363. * Utility functions - Utility functions - Utility functions
  364. */
  365. /**
  366. * Removes from $courses all courses the user is subscribed to.
  367. *
  368. * @global array $_user
  369. * @param array $courses
  370. * @return array
  371. */
  372. function filter_out_user_courses($courses)
  373. {
  374. if (empty($courses))
  375. {
  376. return $courses;
  377. }
  378. global $_user;
  379. $user_id = $_user['user_id'];
  380. $user_courses = $this->retrieve_user_courses($user_id);
  381. foreach ($user_courses as $key => $value)
  382. {
  383. unset($courses[$key]);
  384. }
  385. return $courses;
  386. }
  387. }