search_course_widget.class.php 12 KB

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