PageRenderTime 52ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/glossary/view.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 507 lines | 369 code | 71 blank | 67 comment | 102 complexity | dd413d7909ae0c87ba8f3faaee891799 MD5 | raw file
  1. <?php
  2. /// This page prints a particular instance of glossary
  3. require_once("../../config.php");
  4. require_once("lib.php");
  5. require_once($CFG->libdir . '/completionlib.php');
  6. require_once("$CFG->libdir/rsslib.php");
  7. $id = optional_param('id', 0, PARAM_INT); // Course Module ID
  8. $g = optional_param('g', 0, PARAM_INT); // Glossary ID
  9. $tab = optional_param('tab', GLOSSARY_NO_VIEW, PARAM_ALPHA); // browsing entries by categories?
  10. $displayformat = optional_param('displayformat',-1, PARAM_INT); // override of the glossary display format
  11. $mode = optional_param('mode', '', PARAM_ALPHA); // term entry cat date letter search author approval
  12. $hook = optional_param('hook', '', PARAM_CLEAN); // the term, entry, cat, etc... to look for based on mode
  13. $fullsearch = optional_param('fullsearch', 0,PARAM_INT); // full search (concept and definition) when searching?
  14. $sortkey = optional_param('sortkey', '', PARAM_ALPHA);// Sorted view: CREATION | UPDATE | FIRSTNAME | LASTNAME...
  15. $sortorder = optional_param('sortorder', 'ASC', PARAM_ALPHA); // it defines the order of the sorting (ASC or DESC)
  16. $offset = optional_param('offset', 0,PARAM_INT); // entries to bypass (for paging purposes)
  17. $page = optional_param('page', 0,PARAM_INT); // Page to show (for paging purposes)
  18. $show = optional_param('show', '', PARAM_ALPHA); // [ concept | alias ] => mode=term hook=$show
  19. if (!empty($id)) {
  20. if (! $cm = get_coursemodule_from_id('glossary', $id)) {
  21. print_error('invalidcoursemodule');
  22. }
  23. if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
  24. print_error('coursemisconf');
  25. }
  26. if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
  27. print_error('invalidid', 'glossary');
  28. }
  29. } else if (!empty($g)) {
  30. if (! $glossary = $DB->get_record("glossary", array("id"=>$g))) {
  31. print_error('invalidid', 'glossary');
  32. }
  33. if (! $course = $DB->get_record("course", array("id"=>$glossary->course))) {
  34. print_error('invalidcourseid');
  35. }
  36. if (!$cm = get_coursemodule_from_instance("glossary", $glossary->id, $course->id)) {
  37. print_error('invalidcoursemodule');
  38. }
  39. $id = $cm->id;
  40. } else {
  41. print_error('invalidid', 'glossary');
  42. }
  43. require_course_login($course->id, true, $cm);
  44. $context = context_module::instance($cm->id);
  45. require_capability('mod/glossary:view', $context);
  46. // Prepare format_string/text options
  47. $fmtoptions = array(
  48. 'context' => $context);
  49. require_once($CFG->dirroot . '/comment/lib.php');
  50. comment::init();
  51. /// redirecting if adding a new entry
  52. if ($tab == GLOSSARY_ADDENTRY_VIEW ) {
  53. redirect("edit.php?cmid=$cm->id&amp;mode=$mode");
  54. }
  55. /// setting the defaut number of entries per page if not set
  56. if ( !$entriesbypage = $glossary->entbypage ) {
  57. $entriesbypage = $CFG->glossary_entbypage;
  58. }
  59. /// If we have received a page, recalculate offset
  60. if ($page != 0 && $offset == 0) {
  61. $offset = $page * $entriesbypage;
  62. }
  63. /// setting the default values for the display mode of the current glossary
  64. /// only if the glossary is viewed by the first time
  65. if ( $dp = $DB->get_record('glossary_formats', array('name'=>$glossary->displayformat)) ) {
  66. /// Based on format->defaultmode, we build the defaulttab to be showed sometimes
  67. switch ($dp->defaultmode) {
  68. case 'cat':
  69. $defaulttab = GLOSSARY_CATEGORY_VIEW;
  70. break;
  71. case 'date':
  72. $defaulttab = GLOSSARY_DATE_VIEW;
  73. break;
  74. case 'author':
  75. $defaulttab = GLOSSARY_AUTHOR_VIEW;
  76. break;
  77. default:
  78. $defaulttab = GLOSSARY_STANDARD_VIEW;
  79. }
  80. /// Fetch the rest of variables
  81. $printpivot = $dp->showgroup;
  82. if ( $mode == '' and $hook == '' and $show == '') {
  83. $mode = $dp->defaultmode;
  84. $hook = $dp->defaulthook;
  85. $sortkey = $dp->sortkey;
  86. $sortorder = $dp->sortorder;
  87. }
  88. } else {
  89. $defaulttab = GLOSSARY_STANDARD_VIEW;
  90. $printpivot = 1;
  91. if ( $mode == '' and $hook == '' and $show == '') {
  92. $mode = 'letter';
  93. $hook = 'ALL';
  94. }
  95. }
  96. if ( $displayformat == -1 ) {
  97. $displayformat = $glossary->displayformat;
  98. }
  99. if ( $show ) {
  100. $mode = 'term';
  101. $hook = $show;
  102. $show = '';
  103. }
  104. /// Processing standard security processes
  105. if ($course->id != SITEID) {
  106. require_login($course);
  107. }
  108. if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
  109. echo $OUTPUT->header();
  110. notice(get_string("activityiscurrentlyhidden"));
  111. }
  112. add_to_log($course->id, "glossary", "view", "view.php?id=$cm->id&amp;tab=$tab", $glossary->id, $cm->id);
  113. // Mark as viewed
  114. $completion = new completion_info($course);
  115. $completion->set_module_viewed($cm);
  116. /// stablishing flag variables
  117. if ( $sortorder = strtolower($sortorder) ) {
  118. if ($sortorder != 'asc' and $sortorder != 'desc') {
  119. $sortorder = '';
  120. }
  121. }
  122. if ( $sortkey = strtoupper($sortkey) ) {
  123. if ($sortkey != 'CREATION' and
  124. $sortkey != 'UPDATE' and
  125. $sortkey != 'FIRSTNAME' and
  126. $sortkey != 'LASTNAME'
  127. ) {
  128. $sortkey = '';
  129. }
  130. }
  131. switch ( $mode = strtolower($mode) ) {
  132. case 'search': /// looking for terms containing certain word(s)
  133. $tab = GLOSSARY_STANDARD_VIEW;
  134. //Clean a bit the search string
  135. $hook = trim(strip_tags($hook));
  136. break;
  137. case 'entry': /// Looking for a certain entry id
  138. $tab = GLOSSARY_STANDARD_VIEW;
  139. if ( $dp = $DB->get_record("glossary_formats", array("name"=>$glossary->displayformat)) ) {
  140. $displayformat = $dp->popupformatname;
  141. }
  142. break;
  143. case 'cat': /// Looking for a certain cat
  144. $tab = GLOSSARY_CATEGORY_VIEW;
  145. if ( $hook > 0 ) {
  146. $category = $DB->get_record("glossary_categories", array("id"=>$hook));
  147. }
  148. break;
  149. case 'approval': /// Looking for entries waiting for approval
  150. $tab = GLOSSARY_APPROVAL_VIEW;
  151. // Override the display format with the approvaldisplayformat
  152. if ($glossary->approvaldisplayformat !== 'default' && ($df = $DB->get_record("glossary_formats",
  153. array("name" => $glossary->approvaldisplayformat)))) {
  154. $displayformat = $df->popupformatname;
  155. }
  156. if ( !$hook and !$sortkey and !$sortorder) {
  157. $hook = 'ALL';
  158. }
  159. break;
  160. case 'term': /// Looking for entries that include certain term in its concept, definition or aliases
  161. $tab = GLOSSARY_STANDARD_VIEW;
  162. break;
  163. case 'date':
  164. $tab = GLOSSARY_DATE_VIEW;
  165. if ( !$sortkey ) {
  166. $sortkey = 'UPDATE';
  167. }
  168. if ( !$sortorder ) {
  169. $sortorder = 'desc';
  170. }
  171. break;
  172. case 'author': /// Looking for entries, browsed by author
  173. $tab = GLOSSARY_AUTHOR_VIEW;
  174. if ( !$hook ) {
  175. $hook = 'ALL';
  176. }
  177. if ( !$sortkey ) {
  178. $sortkey = 'FIRSTNAME';
  179. }
  180. if ( !$sortorder ) {
  181. $sortorder = 'asc';
  182. }
  183. break;
  184. case 'letter': /// Looking for entries that begin with a certain letter, ALL or SPECIAL characters
  185. default:
  186. $tab = GLOSSARY_STANDARD_VIEW;
  187. if ( !$hook ) {
  188. $hook = 'ALL';
  189. }
  190. break;
  191. }
  192. switch ( $tab ) {
  193. case GLOSSARY_IMPORT_VIEW:
  194. case GLOSSARY_EXPORT_VIEW:
  195. case GLOSSARY_APPROVAL_VIEW:
  196. $showcommonelements = 0;
  197. break;
  198. default:
  199. $showcommonelements = 1;
  200. break;
  201. }
  202. /// Printing the heading
  203. $strglossaries = get_string("modulenameplural", "glossary");
  204. $strglossary = get_string("modulename", "glossary");
  205. $strallcategories = get_string("allcategories", "glossary");
  206. $straddentry = get_string("addentry", "glossary");
  207. $strnoentries = get_string("noentries", "glossary");
  208. $strsearchindefinition = get_string("searchindefinition", "glossary");
  209. $strsearch = get_string("search");
  210. $strwaitingapproval = get_string('waitingapproval', 'glossary');
  211. /// If we are in approval mode, prit special header
  212. $PAGE->set_title(format_string($glossary->name));
  213. $PAGE->set_heading($course->fullname);
  214. $url = new moodle_url('/mod/glossary/view.php', array('id'=>$cm->id));
  215. if (isset($mode)) {
  216. $url->param('mode', $mode);
  217. }
  218. $PAGE->set_url($url);
  219. if (!empty($CFG->enablerssfeeds) && !empty($CFG->glossary_enablerssfeeds)
  220. && $glossary->rsstype && $glossary->rssarticles) {
  221. $rsstitle = format_string($course->shortname, true, array('context' => context_course::instance($course->id))) . ': '. format_string($glossary->name);
  222. rss_add_http_header($context, 'mod_glossary', $glossary, $rsstitle);
  223. }
  224. if ($tab == GLOSSARY_APPROVAL_VIEW) {
  225. require_capability('mod/glossary:approve', $context);
  226. $PAGE->navbar->add($strwaitingapproval);
  227. echo $OUTPUT->header();
  228. echo $OUTPUT->heading($strwaitingapproval);
  229. } else { /// Print standard header
  230. echo $OUTPUT->header();
  231. }
  232. /// All this depends if whe have $showcommonelements
  233. if ($showcommonelements) {
  234. /// To calculate available options
  235. $availableoptions = '';
  236. /// Decide about to print the import link
  237. /*if (has_capability('mod/glossary:import', $context)) {
  238. $availableoptions = '<span class="helplink">' .
  239. '<a href="' . $CFG->wwwroot . '/mod/glossary/import.php?id=' . $cm->id . '"' .
  240. ' title="' . s(get_string('importentries', 'glossary')) . '">' .
  241. get_string('importentries', 'glossary') . '</a>' .
  242. '</span>';
  243. }
  244. /// Decide about to print the export link
  245. if (has_capability('mod/glossary:export', $context)) {
  246. if ($availableoptions) {
  247. $availableoptions .= '&nbsp;/&nbsp;';
  248. }
  249. $availableoptions .='<span class="helplink">' .
  250. '<a href="' . $CFG->wwwroot . '/mod/glossary/export.php?id=' . $cm->id .
  251. '&amp;mode='.$mode . '&amp;hook=' . urlencode($hook) . '"' .
  252. ' title="' . s(get_string('exportentries', 'glossary')) . '">' .
  253. get_string('exportentries', 'glossary') . '</a>' .
  254. '</span>';
  255. }*/
  256. /// Decide about to print the approval link
  257. if (has_capability('mod/glossary:approve', $context)) {
  258. /// Check we have pending entries
  259. if ($hiddenentries = $DB->count_records('glossary_entries', array('glossaryid'=>$glossary->id, 'approved'=>0))) {
  260. if ($availableoptions) {
  261. $availableoptions .= '<br />';
  262. }
  263. $availableoptions .='<span class="helplink">' .
  264. '<a href="' . $CFG->wwwroot . '/mod/glossary/view.php?id=' . $cm->id .
  265. '&amp;mode=approval' . '"' .
  266. ' title="' . s(get_string('waitingapproval', 'glossary')) . '">' .
  267. get_string('waitingapproval', 'glossary') . ' ('.$hiddenentries.')</a>' .
  268. '</span>';
  269. }
  270. }
  271. /// Start to print glossary controls
  272. // print_box_start('glossarycontrol clearfix');
  273. echo '<div class="glossarycontrol" style="text-align: right">';
  274. echo $availableoptions;
  275. /// The print icon
  276. if ( $showcommonelements and $mode != 'search') {
  277. if (has_capability('mod/glossary:manageentries', $context) or $glossary->allowprintview) {
  278. // print_box_start('printicon');
  279. echo '<span class="wrap printicon">';
  280. echo " <a title =\"". get_string("printerfriendly","glossary") ."\" href=\"print.php?id=$cm->id&amp;mode=$mode&amp;hook=".urlencode($hook)."&amp;sortkey=$sortkey&amp;sortorder=$sortorder&amp;offset=$offset\"><img class=\"icon\" src=\"".$OUTPUT->pix_url('print', 'glossary')."\" alt=\"". get_string("printerfriendly","glossary") . "\" /></a>";
  281. echo '</span>';
  282. // print_box_end();
  283. }
  284. }
  285. /// End glossary controls
  286. // print_box_end(); /// glossarycontrol
  287. echo '</div>';
  288. // print_box('&nbsp;', 'clearer');
  289. }
  290. /// Info box
  291. if ($glossary->intro && $showcommonelements) {
  292. echo $OUTPUT->box(format_module_intro('glossary', $glossary, $cm->id), 'generalbox', 'intro');
  293. }
  294. /// Search box
  295. if ($showcommonelements ) {
  296. echo '<form method="post" action="view.php">';
  297. echo '<table class="boxaligncenter" width="70%" border="0">';
  298. echo '<tr><td align="center" class="glossarysearchbox">';
  299. echo '<input type="submit" value="'.$strsearch.'" name="searchbutton" /> ';
  300. if ($mode == 'search') {
  301. echo '<input type="text" name="hook" size="20" value="'.s($hook).'" alt="'.$strsearch.'" /> ';
  302. } else {
  303. echo '<input type="text" name="hook" size="20" value="" alt="'.$strsearch.'" /> ';
  304. }
  305. if ($fullsearch || $mode != 'search') {
  306. $fullsearchchecked = 'checked="checked"';
  307. } else {
  308. $fullsearchchecked = '';
  309. }
  310. echo '<input type="checkbox" name="fullsearch" id="fullsearch" value="1" '.$fullsearchchecked.' />';
  311. echo '<input type="hidden" name="mode" value="search" />';
  312. echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
  313. echo '<label for="fullsearch">'.$strsearchindefinition.'</label>';
  314. echo '</td></tr></table>';
  315. echo '</form>';
  316. echo '<br />';
  317. }
  318. /// Show the add entry button if allowed
  319. if (has_capability('mod/glossary:write', $context) && $showcommonelements ) {
  320. echo '<div class="singlebutton glossaryaddentry">';
  321. echo "<form id=\"newentryform\" method=\"get\" action=\"$CFG->wwwroot/mod/glossary/edit.php\">";
  322. echo '<div>';
  323. echo "<input type=\"hidden\" name=\"cmid\" value=\"$cm->id\" />";
  324. echo '<input type="submit" value="'.get_string('addentry', 'glossary').'" />';
  325. echo '</div>';
  326. echo '</form>';
  327. echo "</div>\n";
  328. }
  329. echo '<br />';
  330. require("tabs.php");
  331. require("sql.php");
  332. /// printing the entries
  333. $entriesshown = 0;
  334. $currentpivot = '';
  335. $paging = NULL;
  336. if ($allentries) {
  337. //Decide if we must show the ALL link in the pagebar
  338. $specialtext = '';
  339. if ($glossary->showall) {
  340. $specialtext = get_string("allentries","glossary");
  341. }
  342. //Build paging bar
  343. $paging = glossary_get_paging_bar($count, $page, $entriesbypage, "view.php?id=$id&amp;mode=$mode&amp;hook=".urlencode($hook)."&amp;sortkey=$sortkey&amp;sortorder=$sortorder&amp;fullsearch=$fullsearch&amp;",9999,10,'&nbsp;&nbsp;', $specialtext, -1);
  344. echo '<div class="paging">';
  345. echo $paging;
  346. echo '</div>';
  347. //load ratings
  348. require_once($CFG->dirroot.'/rating/lib.php');
  349. if ($glossary->assessed != RATING_AGGREGATE_NONE) {
  350. $ratingoptions = new stdClass;
  351. $ratingoptions->context = $context;
  352. $ratingoptions->component = 'mod_glossary';
  353. $ratingoptions->ratingarea = 'entry';
  354. $ratingoptions->items = $allentries;
  355. $ratingoptions->aggregate = $glossary->assessed;//the aggregation method
  356. $ratingoptions->scaleid = $glossary->scale;
  357. $ratingoptions->userid = $USER->id;
  358. $ratingoptions->returnurl = $CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id;
  359. $ratingoptions->assesstimestart = $glossary->assesstimestart;
  360. $ratingoptions->assesstimefinish = $glossary->assesstimefinish;
  361. $rm = new rating_manager();
  362. $allentries = $rm->get_ratings($ratingoptions);
  363. }
  364. foreach ($allentries as $entry) {
  365. // Setting the pivot for the current entry
  366. $pivot = $entry->glossarypivot;
  367. $upperpivot = textlib::strtoupper($pivot);
  368. $pivottoshow = textlib::strtoupper(format_string($pivot, true, $fmtoptions));
  369. // Reduce pivot to 1cc if necessary
  370. if ( !$fullpivot ) {
  371. $upperpivot = textlib::substr($upperpivot, 0, 1);
  372. $pivottoshow = textlib::substr($pivottoshow, 0, 1);
  373. }
  374. // if there's a group break
  375. if ( $currentpivot != $upperpivot ) {
  376. // print the group break if apply
  377. if ( $printpivot ) {
  378. $currentpivot = $upperpivot;
  379. echo '<div>';
  380. echo '<table cellspacing="0" class="glossarycategoryheader">';
  381. echo '<tr>';
  382. if ( isset($entry->userispivot) ) {
  383. // printing the user icon if defined (only when browsing authors)
  384. echo '<th align="left">';
  385. $user = $DB->get_record("user", array("id"=>$entry->userid));
  386. echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
  387. $pivottoshow = fullname($user, has_capability('moodle/site:viewfullnames', context_course::instance($course->id)));
  388. } else {
  389. echo '<th >';
  390. }
  391. echo $OUTPUT->heading($pivottoshow);
  392. echo "</th></tr></table></div>\n";
  393. }
  394. }
  395. /// highlight the term if necessary
  396. if ($mode == 'search') {
  397. //We have to strip any word starting by + and take out words starting by -
  398. //to make highlight works properly
  399. $searchterms = explode(' ', $hook); // Search for words independently
  400. foreach ($searchterms as $key => $searchterm) {
  401. if (preg_match('/^\-/',$searchterm)) {
  402. unset($searchterms[$key]);
  403. } else {
  404. $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
  405. }
  406. //Avoid highlight of <2 len strings. It's a well known hilight limitation.
  407. if (strlen($searchterm) < 2) {
  408. unset($searchterms[$key]);
  409. }
  410. }
  411. $strippedsearch = implode(' ', $searchterms); // Rebuild the string
  412. $entry->highlight = $strippedsearch;
  413. }
  414. /// and finally print the entry.
  415. glossary_print_entry($course, $cm, $glossary, $entry, $mode, $hook,1,$displayformat);
  416. $entriesshown++;
  417. }
  418. }
  419. if ( !$entriesshown ) {
  420. echo $OUTPUT->box(get_string("noentries","glossary"), "generalbox boxaligncenter boxwidthwide");
  421. }
  422. if (!empty($formsent)) {
  423. // close the form properly if used
  424. echo "</div>";
  425. echo "</form>";
  426. }
  427. if ( $paging ) {
  428. echo '<hr />';
  429. echo '<div class="paging">';
  430. echo $paging;
  431. echo '</div>';
  432. }
  433. echo '<br />';
  434. glossary_print_tabbed_table_end();
  435. /// Finish the page
  436. echo $OUTPUT->footer();