PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/moodle/mod/swf/lib.php

http://swf-activity-module.googlecode.com/
PHP | 358 lines | 132 code | 39 blank | 187 comment | 14 complexity | 0b7829218798f14a431d3d16e273ae00 MD5 | raw file
  1. <?php // $Id: lib.php,v 1.1 2010/02/02 matbury Exp $
  2. /**
  3. * Library of functions and constants for module swf
  4. *
  5. * @author Matt Bury - matbury@gmail.com - http://matbury.com/
  6. * @licence http://www.gnu.org/copyleft/gpl.html GNU Public Licence
  7. * @package swf
  8. */
  9. /*
  10. * Copyright (C) 2009 Matt Bury - matbury@gmail.com - http://matbury.com/
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /**
  26. * Given an object containing all the necessary data,
  27. * (defined by the form in mod.html) this function
  28. * will create a new instance and return the id number
  29. * of the new instance.
  30. *
  31. * @param object $instance An object from the form in mod.html
  32. * @return int The id of the newly inserted swf record
  33. */
  34. require_once('swfformlib.php'); // functions for mod_form.php
  35. require_once('swfviewlib.php'); // functions for view.php
  36. function swf_add_instance($swf)
  37. {
  38. $swf->timecreated = time();
  39. $swf->timemodified = time();
  40. // Try to store it in the database.
  41. if (!$swf->id = insert_record('swf', $swf))
  42. {
  43. return false;
  44. }
  45. // Insert corresponding grade item into grade book
  46. swf_grade_item_update($swf);
  47. return $swf->id;
  48. }
  49. /**
  50. * Given an object containing all the necessary data,
  51. * (defined by the form in mod.html) this function
  52. * will update an existing instance with new data.
  53. *
  54. * @param object $instance An object from the form in mod.html
  55. * @return boolean Success/Fail
  56. **/
  57. function swf_update_instance($swf)
  58. {
  59. $swf->timemodified = time();
  60. // Update the database.
  61. $swf->id = $swf->instance;
  62. if (!update_record('swf', $swf))
  63. {
  64. return false; // some error occurred
  65. }
  66. // Update changes to corresponding grade_items record
  67. swf_grade_item_update($swf);
  68. return true;
  69. }
  70. /**
  71. * Inserts or updates corresponding grade item in grade book
  72. *
  73. * @param object $swf
  74. * @return boolean or int??
  75. **/
  76. function swf_grade_item_update($swf)
  77. {
  78. global $CFG;
  79. if (!function_exists('grade_update')) { //workaround for buggy PHP versions
  80. require_once($CFG->libdir.'/gradelib.php');
  81. }
  82. /*if (array_key_exists('cmidnumber', $swf)) { //it may not be always present
  83. $params = array('itemname'=>$swf->name, 'idnumber'=>$swf->cmidnumber);
  84. } else {
  85. $params = array('itemname'=>$swf->name);
  86. }*/
  87. $params = array('itemname' => $swf->name);
  88. // If set grade is more than 0, otherwise don't grade
  89. if ($swf->grademax > 0) {
  90. $params['gradetype'] = $swf->gradetype;
  91. $params['grademax'] = $swf->grademax;
  92. $params['grademin'] = $swf->grademin;
  93. $params['gradepass'] = 60; //$swf->gradepass; // This isn't working. Gives 0 value.
  94. } else {
  95. $params['gradetype'] = GRADE_TYPE_NONE;
  96. }
  97. // Insert/Update grade item
  98. //grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=NULL, $itemdetails=NULL)
  99. return grade_update('mod/swf', $swf->course, 'mod', 'swf', $swf->id, 0, NULL, $params);
  100. }
  101. /**
  102. * Given an ID of an instance of this module,
  103. * this function will permanently delete the instance
  104. * and any data that depends on it.
  105. *
  106. * @param int $id Id of the module instance
  107. * @return boolean Success/Failure
  108. **/
  109. function swf_delete_instance($id)
  110. {
  111. if (! $swf = get_record('swf', 'id', "$id")) {
  112. return false;
  113. }
  114. $result = true;
  115. if (! delete_records('swf', 'id', "$swf->id")) {
  116. $result = false;
  117. }
  118. // TODO - update changes to corresponding grade_items record
  119. //quiz_grade_item_delete($swf);
  120. return $result;
  121. }
  122. /**
  123. * Return a small object with summary information about what a
  124. * user has done with a given particular instance of this module
  125. * Used for user activity reports.
  126. * $return->time = the time they did it
  127. * $return->info = a short text description
  128. *
  129. * @return null
  130. * @todo Finish documenting this function
  131. **/
  132. function swf_user_outline($course, $user, $mod, $swf)
  133. {
  134. global $CFG;
  135. require_once($CFG->libdir.'/gradelib.php');
  136. $grades = grade_get_grades($course->id, 'mod', 'swf', $swf->id, $user->id);
  137. if (empty($grades->items[0]->grades))
  138. {
  139. return null;
  140. } else {
  141. $grade = reset($grades->items[0]->grades);
  142. }
  143. $result = new stdClass;
  144. $result->info = get_string('grade') . ': ' . $grade->str_long_grade;
  145. $result->time = $grade->dategraded;
  146. return $result;
  147. /*
  148. if($logs = get_records_select("log", "userid = '$user->id' AND module = 'swf' AND action = 'view' AND info = '$swf->id'", "time ASC")) {
  149. $numviews = count($logs);
  150. $lastlog = array_pop($logs);
  151. $result = new object();
  152. $result->info = get_string("numviews", "", $numviews);
  153. $result->time = $lastlog->time;
  154. return $result;
  155. }
  156. return NULL;
  157. */
  158. }
  159. /**
  160. * Print a detailed representation of what a user has done with
  161. * a given particular instance of this module, for user activity reports.
  162. *
  163. * @return boolean
  164. * @todo Finish documenting this function
  165. **/
  166. function swf_user_complete($course, $user, $mod, $swf)
  167. {
  168. global $CFG;
  169. require_once($CFG->libdir.'/gradelib.php');
  170. $grades = grade_get_grades($course->id,'mod','swf',$swf->id,$user->id);
  171. if (!empty($grades->items[0]->grades)) {
  172. $grade = reset($grades->items[0]->grades);
  173. echo '<p>'.get_string('grade').': '.$grade->str_long_grade.'</p>';
  174. if ($grade->str_feedback) {
  175. echo '<p>'.get_string('feedback').': '.$grade->str_feedback.'<p/>';
  176. }
  177. }
  178. return true;
  179. }
  180. /**
  181. * Given a course and a time, this module should find recent activity
  182. * that has occurred in swf activities and print it out.
  183. * Return true if there was output, or false is there was none.
  184. *
  185. * @uses $CFG
  186. * @return boolean
  187. * @todo Finish documenting this function
  188. **/
  189. function swf_print_recent_activity($course, $isteacher, $timestart)
  190. {
  191. global $CFG;
  192. $result = false;
  193. return $result; // True if anything was printed, otherwise false
  194. }
  195. /**
  196. *
  197. *
  198. * @uses $CFG
  199. * @return array
  200. **/
  201. function swf_get_view_actions() {
  202. //return array('view');
  203. return array('view', 'view all', 'report');
  204. }
  205. /**
  206. *
  207. *
  208. * @uses $CFG
  209. * @return array
  210. **/
  211. function swf_get_post_actions() {
  212. //return array();
  213. return array('attempt', 'submit');
  214. }
  215. /**
  216. * Function to be run periodically according to the moodle cron
  217. * This function searches for things that need to be done, such
  218. * as sending out mail, toggling flags etc ...
  219. *
  220. * @uses $CFG
  221. * @return boolean
  222. * @todo Finish documenting this function
  223. **/
  224. function swf_cron ()
  225. {
  226. //global $CFG;
  227. return true;
  228. }
  229. /**
  230. * Must return an array of grades for a given instance of this module,
  231. * indexed by user. It also returns a maximum allowed grade.
  232. *
  233. * Example:
  234. * $return->grades = array of grades;
  235. * $return->maxgrade = maximum allowed grade;
  236. *
  237. * return $return;
  238. *
  239. * @param int $swfid ID of an instance of this module
  240. * @return mixed Null or object with an array of grades and with the maximum grade
  241. **/
  242. function swf_grades($swfid)
  243. {
  244. global $CFG;
  245. global $COURSE;
  246. require_once($CFG->libdir.'/gradelib.php');
  247. if($grades = grade_get_grades($COURSE->id,'mod','swf',$swfid))
  248. {
  249. return $grades;
  250. }
  251. return NULL;
  252. }
  253. /**
  254. * Must return an array of user records (all data) who are participants
  255. * for a given instance of swf. Must include every user involved
  256. * in the instance, independient of his role (student, teacher, admin...)
  257. * See other modules as example.
  258. *
  259. * @param int $swfid ID of an instance of this module
  260. * @return mixed boolean/array of students
  261. **/
  262. function swf_get_participants($swfid)
  263. {
  264. return false;
  265. }
  266. /**
  267. * This function returns if a scale is being used by one swf
  268. * if it has support for grading and scales. Commented code should be
  269. * modified if necessary. See forum, glossary or journal modules
  270. * as reference.
  271. *
  272. * @param int $swfid ID of an instance of this module
  273. * @return mixed
  274. * @todo Finish documenting this function
  275. **/
  276. function swf_scale_used($swfid,$scaleid) {
  277. $return = false;
  278. //$rec = get_record("swf","id","$swfid","scale","-$scaleid");
  279. //
  280. //if (!empty($rec) && !empty($scaleid)) {
  281. // $return = true;
  282. //}
  283. return $return;
  284. }
  285. /**
  286. * Checks if scale is being used by any instance of swf.
  287. * This function was added in 1.9
  288. *
  289. * This is used to find out if scale used anywhere
  290. * @param $scaleid int
  291. * @return boolean True if the scale is used by any swf
  292. */
  293. function swf_scale_used_anywhere($scaleid) {
  294. if ($scaleid and record_exists('swf', 'grade', -$scaleid)) {
  295. return true;
  296. } else {
  297. return false;
  298. }
  299. }
  300. /**
  301. * Execute post-install custom actions for the module
  302. * This function was added in 1.9
  303. *
  304. * @return boolean true if success, false on error
  305. */
  306. function swf_install() {
  307. return true;
  308. }
  309. /**
  310. * Execute post-uninstall custom actions for the module
  311. * This function was added in 1.9
  312. *
  313. * @return boolean true if success, false on error
  314. */
  315. function swf_uninstall() {
  316. return true;
  317. }
  318. ?>