PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/forumng/lib.php

https://github.com/galitush2005/RTL-BIDI-Hebrew-Moodle-Plugins
PHP | 264 lines | 164 code | 34 blank | 66 comment | 27 complexity | fa00442b13eb84155d7dbb4714f38fd2 MD5 | raw file
  1. <?php
  2. function forumng_add_instance($forumng) {
  3. // Avoid including forum libraries in large areas of Moodle code that
  4. // require this lib.php; only include when functions are called
  5. require_once(dirname(__FILE__).'/forum.php');
  6. $useshared = !empty($forumng->usesharedgroup['useshared']);
  7. if ($useshared) {
  8. $idnumber = $forumng->usesharedgroup['originalcmidnumber'];
  9. if (!($originalcm = get_record('course_modules', 'idnumber', $idnumber,
  10. 'module', get_field('modules', 'id', 'name', 'forumng')))) {
  11. return false;
  12. }
  13. if (!($originalforumng = get_record('forumng', 'id', $originalcm->instance))) {
  14. return false;
  15. }
  16. // Create appropriate data for forumng table
  17. $forumng = (object)array(
  18. 'name' => addslashes($originalforumng->name),
  19. 'course' => $forumng->course,
  20. 'type' => 'clone',
  21. 'originalcmid' => $originalcm->id);
  22. }
  23. // Pick a random magic number
  24. $part1 = mt_rand(0, 99999999);
  25. $part2 = mt_rand(0, 99999999);
  26. while(strlen($part2)<8) {
  27. $part2 = '0'.$part2;
  28. }
  29. $forumng->magicnumber = $part1.$part2;
  30. if(!($id=insert_record('forumng', $forumng))) {
  31. return false;
  32. }
  33. // Handle post-creation actions (but only if a new forum actually was
  34. // created, and not just a new reference to a shared one!)
  35. if (!$useshared) {
  36. $forum=forum::get_from_id($id, forum::CLONE_DIRECT, false);
  37. $forum->created($forumng->cmidnumber);
  38. }
  39. return $id;
  40. }
  41. function forumng_update_instance($forumng) {
  42. require_once(dirname(__FILE__).'/forum.php');
  43. $forumng->id = $forumng->instance;
  44. $previous = get_record('forumng','id',$forumng->id);
  45. if (class_exists('ouflags')
  46. && (has_capability('local/course:revisioneditor', get_context_instance(CONTEXT_COURSE, $forumng->course), null, false))) {
  47. global $CFG;
  48. // handle insitu editing updates
  49. include_once($CFG->dirroot.'/local/insitu/lib.php');
  50. oci_mod_make_backup_and_save_instance($forumng);
  51. } else {
  52. if (!update_record('forumng', $forumng)) {
  53. return false;
  54. }
  55. }
  56. try {
  57. $forum=forum::get_from_id($forumng->id, forum::CLONE_DIRECT);
  58. $forum->updated($previous);
  59. } catch(Exception $e) {
  60. return false;
  61. }
  62. return true;
  63. }
  64. // extra parameter to control skipping for OU OCI upload
  65. function forumng_delete_instance($id, $ociskip=true) {
  66. require_once(dirname(__FILE__).'/forum.php');
  67. try {
  68. $forum = forum::get_from_id($id, forum::CLONE_DIRECT);
  69. // avoid deleting OCI specific forum if running in upload block
  70. if ($ociskip) {
  71. global $restore;
  72. if (isset($restore) && $restore->restoreto==0 && strpos($_SERVER['HTTP_REFERER'],'blocks/versions/upload.php')!==false) {
  73. if ($forum->get_name()==get_string('newunitforumname', 'createcourse')) { //Unit forum
  74. echo ' found forumng '.$forum->get_id().' '.$forum->get_name();
  75. return true;
  76. }
  77. }
  78. }
  79. $forum->delete_all_data();
  80. if(forum::search_installed()) {
  81. $cm = $forum->get_course_module();
  82. ousearch_document::delete_module_instance_data($cm);
  83. }
  84. } catch(Exception $e) {
  85. return false;
  86. }
  87. return delete_records('forumng','id',$id);
  88. }
  89. function forumng_cron() {
  90. require_once(dirname(__FILE__).'/forum_cron.php');
  91. try {
  92. forum_cron::cron();
  93. } catch(forum_exception $e) {
  94. mtrace("A forum exception occurred and forum cron was aborted: " .
  95. $e->getMessage() . "\n\n" .
  96. $e->getTraceAsString()."\n\n");
  97. }
  98. }
  99. /**
  100. * Obtains a search document given the ousearch parameters.
  101. * @param object $document Object containing fields from the ousearch documents table
  102. * @return mixed False if object can't be found, otherwise object containing the following
  103. * fields: ->content, ->title, ->url, ->activityname, ->activityurl,
  104. * and optionally ->extrastrings array and ->data
  105. */
  106. function forumng_ousearch_get_document($document) {
  107. require_once(dirname(__FILE__).'/forum.php');
  108. return forum_post::search_get_page($document);
  109. }
  110. /**
  111. * Update all documents for ousearch.
  112. * @param bool $feedback If true, prints feedback as HTML list items
  113. * @param int $courseid If specified, restricts to particular courseid
  114. */
  115. function forumng_ousearch_update_all($feedback=false, $courseid=0) {
  116. require_once(dirname(__FILE__).'/forum.php');
  117. forum::search_update_all($feedback, $courseid);
  118. }
  119. /**
  120. * Returns all other caps used in module
  121. */
  122. function forumng_get_extra_capabilities() {
  123. return array('moodle/site:accessallgroups', 'moodle/site:viewfullnames', 'moodle/site:trustcontent');
  124. }
  125. /**
  126. * Given a course_module object, this function returns any
  127. * "extra" information that may be needed when printing
  128. * this activity in a course listing.
  129. * See get_array_of_activities() in course/lib.php
  130. */
  131. function forumng_get_coursemodule_info($coursemodule) {
  132. if ($forumng = get_record('forumng', 'id', $coursemodule->instance, '', '', '', '', 'id, name, type')) {
  133. $info = new object();
  134. // $info->name = $forumng->name;
  135. $info->extra = 'class="forumng-type-' . $forumng->type . '"';
  136. return $info;
  137. }
  138. }
  139. /**
  140. * Create html fragment for display on myMoodle page, forums changed since
  141. * user last visited
  142. *
  143. * @param $courses list of courses to output information from
  144. * @param $htmlarray returned results appended html to display
  145. */
  146. function forumng_print_overview($courses,&$htmlarray) {
  147. global $USER, $CFG;
  148. require_once($CFG->dirroot . '/mod/forumng/forum.php');
  149. if (empty($courses) || !is_array($courses) || count($courses) == 0) {
  150. return array();
  151. }
  152. $strnumunread = get_string('discussionsunread','forumng');
  153. $strforum = get_string('modulename','forumng');
  154. foreach($courses as $course) {
  155. $str = "";
  156. $forums = forum::get_course_forums($course, $USER->id,forum::UNREAD_DISCUSSIONS);
  157. if (!empty($forums)) {
  158. foreach($forums as $forum) {
  159. // note like all mymoodle, there's no check current user can see each forum
  160. // ok for openlearn & vital but might need addressing if VLE ever use it
  161. if ($forum->has_unread_discussions()) { // only listing unread, not new & unread for performance
  162. $str .= '<div class="overview forumng"><div class="name">' .
  163. $strforum . ':' . ' <a title="' . $strforum . '" href="' .
  164. $forum->get_url(forum::PARAM_HTML).'">' .
  165. $forum->get_name() . '</a></div>';
  166. $str .= '<div class="info">'.$forum->get_num_unread_discussions(). ' '.$strnumunread.'</div></div>';
  167. }
  168. }
  169. }
  170. if (!empty($str)) {
  171. if (!array_key_exists($course->id,$htmlarray)) {
  172. $htmlarray[$course->id] = array();
  173. }
  174. if (!array_key_exists('forumng',$htmlarray[$course->id])) {
  175. $htmlarray[$course->id]['forumng'] = ''; // initialize, avoid warnings
  176. }
  177. $htmlarray[$course->id]['forumng'] .= $str;
  178. }
  179. }
  180. }
  181. /**
  182. * Indicates API features that the forum supports.
  183. *
  184. * @param string $feature
  185. * @return mixed True if yes (some features may use other values)
  186. */
  187. function forumng_supports($feature) {
  188. switch($feature) {
  189. case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
  190. case FEATURE_COMPLETION_HAS_RULES: return true;
  191. case FEATURE_GRADE_HAS_GRADE: return true;
  192. default: return false;
  193. }
  194. }
  195. /**
  196. * Obtains the automatic completion state for this forum based on any conditions
  197. * in forum settings.
  198. *
  199. * @param object $course Course
  200. * @param object $cm Course-module
  201. * @param int $userid User ID
  202. * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
  203. * @return bool True if completed, false if not. (If no conditions, then return
  204. * value depends on comparison type)
  205. */
  206. function forumng_get_completion_state($course, $cm, $userid, $type) {
  207. // Use forum object to handle this request
  208. $forum = forum::get_from_cmid($cm->id, forum::CLONE_DIRECT);
  209. return $forum->get_completion_state($userid, $type);
  210. }
  211. /**
  212. * Used by course/user.php to display this module's user activity outline.
  213. * @param object $course as this is a standard function this is required but not used here
  214. * @param object $user Moodle user ob
  215. * @param object $mod not used here
  216. * @param object $forum Moodle forumng object
  217. * @return object A standard object with 2 variables: info (number of posts for this user) and time (last modified)
  218. */
  219. function forumng_user_outline($course, $user, $mod, $forum) {
  220. require_once(dirname(__FILE__).'/forum.php');
  221. if ($posts = forum::get_user_activityreport($forum->id, $user->id)) {
  222. $result = new object();
  223. $result->info = get_string("numposts", "forumng", $posts->postcount);
  224. $result->time = $posts->lastpost;
  225. return $result;
  226. } else {
  227. return NULL;
  228. }
  229. }
  230. ?>