search_course_widget.class.php 12 KB

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