PageRenderTime 104ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/hotpot/lib.php

https://bitbucket.org/ceu/moodle_demo
PHP | 2724 lines | 2065 code | 344 blank | 315 comment | 360 complexity | 7153cb35f1f596cfbf3cf05a9839527b MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?PHP // $Id: lib.php,v 1.79.2.27 2012/05/19 11:04:52 moodlerobot Exp $
  2. //////////////////////////////////
  3. /// CONFIGURATION settings
  4. if (!isset($CFG->hotpot_showtimes)) {
  5. set_config("hotpot_showtimes", 0);
  6. }
  7. if (!isset($CFG->hotpot_excelencodings)) {
  8. set_config("hotpot_excelencodings", "");
  9. }
  10. //////////////////////////////////
  11. /// CONSTANTS and GLOBAL VARIABLES
  12. $CFG->hotpotroot = "$CFG->dirroot/mod/hotpot";
  13. $CFG->hotpottemplate = "$CFG->hotpotroot/template";
  14. if (!empty($_SERVER['HTTP_USER_AGENT'])) {
  15. $CFG->hotpotismobile = preg_match('/Alcatel|ATTWS|DoCoMo|Doris|Hutc3G|J-PHONE|Java|KDDI|KGT|LGE|MOT|Nokia|portalmmm|ReqwirelessWeb|SAGEM|SHARP|SIE-|SonyEricsson|Teleport|UP\.Browser|UPG1|Wapagsim/', $_SERVER['HTTP_USER_AGENT']);
  16. } else {
  17. $CFG->hotpotismobile = false;
  18. }
  19. define("HOTPOT_JS", "$CFG->wwwroot/mod/hotpot/hotpot-full.js");
  20. define("HOTPOT_NO", "0");
  21. define("HOTPOT_YES", "1");
  22. define ("HOTPOT_TEXTSOURCE_QUIZ", "0");
  23. define ("HOTPOT_TEXTSOURCE_FILENAME", "1");
  24. define ("HOTPOT_TEXTSOURCE_FILEPATH", "2");
  25. define ("HOTPOT_TEXTSOURCE_SPECIFIC", "3");
  26. define("HOTPOT_LOCATION_COURSEFILES", "0");
  27. define("HOTPOT_LOCATION_SITEFILES", "1");
  28. $HOTPOT_LOCATION = array (
  29. HOTPOT_LOCATION_COURSEFILES => get_string("coursefiles"),
  30. HOTPOT_LOCATION_SITEFILES => get_string("sitefiles"),
  31. );
  32. define("HOTPOT_OUTPUTFORMAT_BEST", "1");
  33. define("HOTPOT_OUTPUTFORMAT_V3", "10");
  34. define("HOTPOT_OUTPUTFORMAT_V4", "11");
  35. define("HOTPOT_OUTPUTFORMAT_V5", "12");
  36. define("HOTPOT_OUTPUTFORMAT_V5_PLUS", "13");
  37. define("HOTPOT_OUTPUTFORMAT_V6", "14");
  38. define("HOTPOT_OUTPUTFORMAT_V6_PLUS", "15");
  39. define("HOTPOT_OUTPUTFORMAT_FLASH", "20");
  40. define("HOTPOT_OUTPUTFORMAT_MOBILE", "30");
  41. $HOTPOT_OUTPUTFORMAT = array (
  42. HOTPOT_OUTPUTFORMAT_BEST => get_string("outputformat_best", "hotpot"),
  43. HOTPOT_OUTPUTFORMAT_V6_PLUS => get_string("outputformat_v6_plus", "hotpot"),
  44. HOTPOT_OUTPUTFORMAT_V6 => get_string("outputformat_v6", "hotpot"),
  45. HOTPOT_OUTPUTFORMAT_V5_PLUS => get_string("outputformat_v5_plus", "hotpot"),
  46. HOTPOT_OUTPUTFORMAT_V5 => get_string("outputformat_v5", "hotpot"),
  47. HOTPOT_OUTPUTFORMAT_V4 => get_string("outputformat_v4", "hotpot"),
  48. HOTPOT_OUTPUTFORMAT_V3 => get_string("outputformat_v3", "hotpot"),
  49. HOTPOT_OUTPUTFORMAT_FLASH => get_string("outputformat_flash", "hotpot"),
  50. HOTPOT_OUTPUTFORMAT_MOBILE => get_string("outputformat_mobile", "hotpot"),
  51. );
  52. $HOTPOT_OUTPUTFORMAT_DIR = array (
  53. HOTPOT_OUTPUTFORMAT_V6_PLUS => 'v6',
  54. HOTPOT_OUTPUTFORMAT_V6 => 'v6',
  55. HOTPOT_OUTPUTFORMAT_V5_PLUS => 'v5',
  56. HOTPOT_OUTPUTFORMAT_V5 => 'v5',
  57. HOTPOT_OUTPUTFORMAT_V4 => 'v4',
  58. HOTPOT_OUTPUTFORMAT_V3 => 'v3',
  59. HOTPOT_OUTPUTFORMAT_FLASH => 'flash',
  60. HOTPOT_OUTPUTFORMAT_MOBILE => 'mobile',
  61. );
  62. foreach ($HOTPOT_OUTPUTFORMAT_DIR as $format=>$dir) {
  63. if (is_file("$CFG->hotpottemplate/$dir.php") && is_dir("$CFG->hotpottemplate/$dir")) {
  64. // do nothing ($format is available)
  65. } else {
  66. // $format is not available, so remove it
  67. unset($HOTPOT_OUTPUTFORMAT[$format]);
  68. unset($HOTPOT_OUTPUTFORMAT_DIR[$format]);
  69. }
  70. }
  71. define("HOTPOT_NAVIGATION_BAR", "1");
  72. define("HOTPOT_NAVIGATION_FRAME", "2");
  73. define("HOTPOT_NAVIGATION_IFRAME", "3");
  74. define("HOTPOT_NAVIGATION_BUTTONS", "4");
  75. define("HOTPOT_NAVIGATION_GIVEUP", "5");
  76. define("HOTPOT_NAVIGATION_NONE", "6");
  77. $HOTPOT_NAVIGATION = array (
  78. HOTPOT_NAVIGATION_BAR => get_string("navigation_bar", "hotpot"),
  79. HOTPOT_NAVIGATION_FRAME => get_string("navigation_frame", "hotpot"),
  80. HOTPOT_NAVIGATION_IFRAME => get_string("navigation_iframe", "hotpot"),
  81. HOTPOT_NAVIGATION_BUTTONS => get_string("navigation_buttons", "hotpot"),
  82. HOTPOT_NAVIGATION_GIVEUP => get_string("navigation_give_up", "hotpot"),
  83. HOTPOT_NAVIGATION_NONE => get_string("navigation_none", "hotpot"),
  84. );
  85. define("HOTPOT_JCB", "1");
  86. define("HOTPOT_JCLOZE", "2");
  87. define("HOTPOT_JCROSS", "3");
  88. define("HOTPOT_JMATCH", "4");
  89. define("HOTPOT_JMIX", "5");
  90. define("HOTPOT_JQUIZ", "6");
  91. define("HOTPOT_TEXTOYS_RHUBARB", "7");
  92. define("HOTPOT_TEXTOYS_SEQUITUR", "8");
  93. $HOTPOT_QUIZTYPE = array(
  94. HOTPOT_JCB => 'JCB',
  95. HOTPOT_JCLOZE => 'JCloze',
  96. HOTPOT_JCROSS => 'JCross',
  97. HOTPOT_JMATCH => 'JMatch',
  98. HOTPOT_JMIX => 'JMix',
  99. HOTPOT_JQUIZ => 'JQuiz',
  100. HOTPOT_TEXTOYS_RHUBARB => 'Rhubarb',
  101. HOTPOT_TEXTOYS_SEQUITUR => 'Sequitur'
  102. );
  103. define("HOTPOT_JQUIZ_MULTICHOICE", "1");
  104. define("HOTPOT_JQUIZ_SHORTANSWER", "2");
  105. define("HOTPOT_JQUIZ_HYBRID", "3");
  106. define("HOTPOT_JQUIZ_MULTISELECT", "4");
  107. define("HOTPOT_GRADEMETHOD_HIGHEST", "1");
  108. define("HOTPOT_GRADEMETHOD_AVERAGE", "2");
  109. define("HOTPOT_GRADEMETHOD_FIRST", "3");
  110. define("HOTPOT_GRADEMETHOD_LAST", "4");
  111. $HOTPOT_GRADEMETHOD = array (
  112. HOTPOT_GRADEMETHOD_HIGHEST => get_string("gradehighest", "quiz"),
  113. HOTPOT_GRADEMETHOD_AVERAGE => get_string("gradeaverage", "quiz"),
  114. HOTPOT_GRADEMETHOD_FIRST => get_string("attemptfirst", "quiz"),
  115. HOTPOT_GRADEMETHOD_LAST => get_string("attemptlast", "quiz"),
  116. );
  117. define("HOTPOT_STATUS_INPROGRESS", "1");
  118. define("HOTPOT_STATUS_TIMEDOUT", "2");
  119. define("HOTPOT_STATUS_ABANDONED", "3");
  120. define("HOTPOT_STATUS_COMPLETED", "4");
  121. $HOTPOT_STATUS = array (
  122. HOTPOT_STATUS_INPROGRESS => get_string("inprogress", "hotpot"),
  123. HOTPOT_STATUS_TIMEDOUT => get_string("timedout", "hotpot"),
  124. HOTPOT_STATUS_ABANDONED => get_string("abandoned", "hotpot"),
  125. HOTPOT_STATUS_COMPLETED => get_string("completed", "hotpot"),
  126. );
  127. define("HOTPOT_FEEDBACK_NONE", "0");
  128. define("HOTPOT_FEEDBACK_WEBPAGE", "1");
  129. define("HOTPOT_FEEDBACK_FORMMAIL", "2");
  130. define("HOTPOT_FEEDBACK_MOODLEFORUM", "3");
  131. define("HOTPOT_FEEDBACK_MOODLEMESSAGING", "4");
  132. $HOTPOT_FEEDBACK = array (
  133. HOTPOT_FEEDBACK_NONE => get_string("feedbacknone", "hotpot"),
  134. HOTPOT_FEEDBACK_WEBPAGE => get_string("feedbackwebpage", "hotpot"),
  135. HOTPOT_FEEDBACK_FORMMAIL => get_string("feedbackformmail", "hotpot"),
  136. HOTPOT_FEEDBACK_MOODLEFORUM => get_string("feedbackmoodleforum", "hotpot"),
  137. HOTPOT_FEEDBACK_MOODLEMESSAGING => get_string("feedbackmoodlemessaging", "hotpot"),
  138. );
  139. if (empty($CFG->messaging)) { // Moodle 1.4 (and less)
  140. unset($HOTPOT_FEEDBACK[HOTPOT_FEEDBACK_MOODLEMESSAGING]);
  141. }
  142. define("HOTPOT_DISPLAYNEXT_QUIZ", "0");
  143. define("HOTPOT_DISPLAYNEXT_COURSE", "1");
  144. define("HOTPOT_DISPLAYNEXT_INDEX", "2");
  145. /**
  146. * If start and end date for the quiz are more than this many seconds apart
  147. * they will be represented by two separate events in the calendar
  148. */
  149. define("HOTPOT_MAX_EVENT_LENGTH", "432000"); // 5 days maximum
  150. //////////////////////////////////
  151. /// CORE FUNCTIONS
  152. // possible return values:
  153. // false:
  154. // display moderr.html (if exists) OR "Could not update" and return to couse view
  155. // string:
  156. // display as error message and return to course view
  157. // true (or non-zero number):
  158. // continue to $hotpot->redirect (if set) OR hotpot/view.php (to displsay quiz)
  159. // $hotpot is an object containing the values of the form in mod.html
  160. // i.e. all the fields in the 'hotpot' table, plus the following:
  161. // $hotpot->course : an id in the 'course' table
  162. // $hotpot->coursemodule : an id in the 'course_modules' table
  163. // $hotpot->section : an id in the 'course_sections' table
  164. // $hotpot->module : an id in the 'modules' table
  165. // $hotpot->modulename : always 'hotpot'
  166. // $hotpot->instance : an id in the 'hotpot' table
  167. // $hotpot->mode : 'add' or 'update'
  168. // $hotpot->sesskey : unique string required for Moodle's session management
  169. function hotpot_add_instance(&$hotpot) {
  170. if (hotpot_set_form_values($hotpot)) {
  171. if ($result = insert_record('hotpot', $hotpot)) {
  172. $hotpot->id = $result;
  173. hotpot_update_events($hotpot);
  174. hotpot_grade_item_update(stripslashes_recursive($hotpot));
  175. }
  176. } else {
  177. $result= false;
  178. }
  179. return $result;
  180. }
  181. function hotpot_update_instance(&$hotpot) {
  182. if (hotpot_set_form_values($hotpot)) {
  183. $hotpot->id = $hotpot->instance;
  184. if ($result = update_record('hotpot', $hotpot)) {
  185. hotpot_update_events($hotpot);
  186. //hotpot_grade_item_update(stripslashes_recursive($hotpot));
  187. hotpot_update_grades(stripslashes_recursive($hotpot));
  188. }
  189. } else {
  190. $result= false;
  191. }
  192. return $result;
  193. }
  194. function hotpot_update_events($hotpot) {
  195. // remove any previous calendar events for this hotpot
  196. delete_records('event', 'modulename', 'hotpot', 'instance', $hotpot->id);
  197. $event = new stdClass();
  198. $event->description = addslashes($hotpot->summary);
  199. $event->courseid = $hotpot->course;
  200. $event->groupid = 0;
  201. $event->userid = 0;
  202. $event->modulename = 'hotpot';
  203. $event->instance = $hotpot->id;
  204. $event->timestart = $hotpot->timeopen;
  205. if ($cm = get_coursemodule_from_id('hotpot', $hotpot->id)) {
  206. $event->visible = hotpot_is_visible($cm);
  207. } else {
  208. $event->visible = 1;
  209. }
  210. if ($hotpot->timeclose && $hotpot->timeopen) {
  211. // we have both a start and an end date
  212. $event->eventtype = 'open';
  213. $event->timeduration = ($hotpot->timeclose - $hotpot->timeopen);
  214. if ($event->timeduration > HOTPOT_MAX_EVENT_LENGTH) { /// Long durations create two events
  215. $event->name = addslashes($hotpot->name).' ('.get_string('hotpotopens', 'hotpot').')';
  216. $event->timeduration = 0;
  217. add_event($event);
  218. $event->timestart = $hotpot->timeclose;
  219. $event->eventtype = 'close';
  220. $event->name = addslashes($hotpot->name).' ('.get_string('hotpotcloses', 'hotpot').')';
  221. unset($event->id);
  222. add_event($event);
  223. } else { // single event with duration
  224. $event->name = $hotpot->name;
  225. add_event($event);
  226. }
  227. } elseif ($hotpot->timeopen) { // only an open date
  228. $event->name = addslashes($hotpot->name).' ('.get_string('hotpotopens', 'hotpot').')';
  229. $event->eventtype = 'open';
  230. $event->timeduration = 0;
  231. add_event($event);
  232. } elseif ($hotpot->timeclose) { // only a closing date
  233. $event->name = addslashes($hotpot->name).' ('.get_string('hotpotcloses', 'hotpot').')';
  234. $event->timestart = $hotpot->timeclose;
  235. $event->eventtype = 'close';
  236. $event->timeduration = 0;
  237. add_event($event);
  238. }
  239. }
  240. function hotpot_set_form_values(&$hotpot) {
  241. $ok = true;
  242. $hotpot->errors = array(); // these will be reported by moderr.html
  243. if (empty($hotpot->reference)) {
  244. $ok = false;
  245. $hotpot->errors['reference']= get_string('error_nofilename', 'hotpot');
  246. }
  247. if (empty($hotpot->studentfeedbackurl) || $hotpot->studentfeedbackurl=='http://') {
  248. $hotpot->studentfeedbackurl = '';
  249. switch ($hotpot->studentfeedback) {
  250. case HOTPOT_FEEDBACK_WEBPAGE:
  251. $ok = false;
  252. $hotpot->errors['studentfeedbackurl']= get_string('error_nofeedbackurlwebpage', 'hotpot');
  253. break;
  254. case HOTPOT_FEEDBACK_FORMMAIL:
  255. $ok = false;
  256. $hotpot->errors['studentfeedbackurl']= get_string('error_nofeedbackurlformmail', 'hotpot');
  257. break;
  258. }
  259. }
  260. $time = time();
  261. $hotpot->timecreated = $time;
  262. $hotpot->timemodified = $time;
  263. if (empty($hotpot->mode)) {
  264. // moodle 1.9 (from mod_form.lib)
  265. if ($hotpot->add) {
  266. $hotpot->mode = 'add';
  267. } else if ($hotpot->update) {
  268. $hotpot->mode = 'update';
  269. } else {
  270. $hotpot->mode = '';
  271. }
  272. }
  273. if ($hotpot->quizchain==HOTPOT_YES) {
  274. switch ($hotpot->mode) {
  275. case 'add':
  276. $ok = hotpot_add_chain($hotpot);
  277. break;
  278. case 'update':
  279. $ok = hotpot_update_chain($hotpot);
  280. break;
  281. }
  282. } else { // $hotpot->quizchain==HOTPOT_NO
  283. hotpot_set_name_summary_reference($hotpot);
  284. }
  285. if (isset($hotpot->displaynext)) {
  286. switch ($hotpot->displaynext) {
  287. // N.B. redirection only works for Moodle 1.5+
  288. case HOTPOT_DISPLAYNEXT_COURSE:
  289. $hotpot->redirect = true;
  290. $hotpot->redirecturl = "view.php?id=$hotpot->course";
  291. break;
  292. case HOTPOT_DISPLAYNEXT_INDEX:
  293. $hotpot->redirect = true;
  294. $hotpot->redirecturl = "../mod/hotpot/index.php?id=$hotpot->course";
  295. break;
  296. default:
  297. // use Moodle default action (i.e. go on to display the hotpot quiz)
  298. }
  299. } else {
  300. $hotpot->displaynext = HOTPOT_DISPLAYNEXT_QUIZ;
  301. }
  302. // if ($ok && $hotpot->setdefaults) {
  303. if ($ok) {
  304. set_user_preference('hotpot_timeopen', $hotpot->timeopen);
  305. set_user_preference('hotpot_timeclose', $hotpot->timeclose);
  306. set_user_preference('hotpot_navigation', $hotpot->navigation);
  307. set_user_preference('hotpot_outputformat', $hotpot->outputformat);
  308. set_user_preference('hotpot_studentfeedback', $hotpot->studentfeedback);
  309. set_user_preference('hotpot_studentfeedbackurl', $hotpot->studentfeedbackurl);
  310. set_user_preference('hotpot_forceplugins', $hotpot->forceplugins);
  311. set_user_preference('hotpot_shownextquiz', $hotpot->shownextquiz);
  312. set_user_preference('hotpot_review', $hotpot->review);
  313. set_user_preference('hotpot_grade', $hotpot->grade);
  314. set_user_preference('hotpot_grademethod', $hotpot->grademethod);
  315. set_user_preference('hotpot_attempts', $hotpot->attempts);
  316. set_user_preference('hotpot_subnet', $hotpot->subnet);
  317. set_user_preference('hotpot_displaynext', $hotpot->displaynext);
  318. if ($hotpot->mode=='add') {
  319. set_user_preference('hotpot_quizchain', $hotpot->quizchain);
  320. set_user_preference('hotpot_namesource', $hotpot->namesource);
  321. set_user_preference('hotpot_summarysource', $hotpot->summarysource);
  322. }
  323. }
  324. return $ok;
  325. }
  326. function hotpot_get_chain(&$cm) {
  327. // get details of course_modules in this section
  328. $course_module_ids = get_field('course_sections', 'sequence', 'id', $cm->section);
  329. if (empty($course_module_ids)) {
  330. $hotpot_modules = array();
  331. } else {
  332. $hotpot_modules = get_records_select('course_modules', "id IN ($course_module_ids) AND module=$cm->module");
  333. if (empty($hotpot_modules)) {
  334. $hotpot_modules = array();
  335. }
  336. }
  337. // get ids of hotpot modules in this section
  338. $ids = array();
  339. foreach ($hotpot_modules as $hotpot_module) {
  340. $ids[] = $hotpot_module->instance;
  341. }
  342. // get details of hotpots in this section
  343. if (empty($ids)) {
  344. $hotpots = array();
  345. } else {
  346. $hotpots = get_records_list('hotpot', 'id', implode(',', $ids));
  347. }
  348. $found = false;
  349. $chain = array();
  350. // loop through course_modules in this section
  351. $ids = explode(',', $course_module_ids);
  352. foreach ($ids as $id) {
  353. // check this course_module is a hotpot activity
  354. if (isset($hotpot_modules[$id])) {
  355. // store details of this course module and hotpot activity
  356. $hotpot_id = $hotpot_modules[$id]->instance;
  357. $chain[$id] = &$hotpot_modules[$id];
  358. $chain[$id]->hotpot = &$hotpots[$hotpot_id];
  359. // set $found, if this is the course module we're looking for
  360. if (isset($cm->coursemodule)) {
  361. if ($id==$cm->coursemodule) {
  362. $found = true;
  363. }
  364. } else {
  365. if ($id==$cm->id) {
  366. $found = true;
  367. }
  368. }
  369. // is this the end of a chain
  370. if (empty($hotpots[$hotpot_id]->shownextquiz)) {
  371. if ($found) {
  372. break; // out of loop
  373. } else {
  374. // restart chain (target cm has not been found yet)
  375. $chain = array();
  376. }
  377. }
  378. }
  379. } // end foreach $ids
  380. return $found ? $chain : false;
  381. }
  382. function hotpot_is_visible(&$cm) {
  383. global $CFG, $COURSE;
  384. // check grouping
  385. $modulecontext = get_context_instance(CONTEXT_MODULE, $cm->id);
  386. if (empty($CFG->enablegroupings) || empty($cm->groupmembersonly) || has_capability('moodle/site:accessallgroups', $modulecontext)) {
  387. // groupings not applicable
  388. } else if (!isguestuser() && groups_has_membership($cm)) {
  389. // user is in one of the groups in the allowed grouping
  390. } else {
  391. // user is not in the required grouping and does not have sufficiently privileges to view this hotpot activity
  392. return false;
  393. }
  394. // check if user can view hidden activities
  395. if (isset($COURSE->context)) {
  396. $coursecontext = &$COURSE->context;
  397. } else {
  398. $coursecontext = get_context_instance(CONTEXT_COURSE, $cm->course);
  399. }
  400. if (has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
  401. return true; // user can view hidden activities
  402. }
  403. if (!isset($cm->sectionvisible)) {
  404. if (! $section = get_record('course_sections', 'id', $cm->section)) {
  405. error('Course module record contains invalid section');
  406. }
  407. $cm->sectionvisible = $section->visible;
  408. }
  409. if (empty($cm->sectionvisible)) {
  410. $visible = HOTPOT_NO;
  411. } else {
  412. $visible = HOTPOT_YES;
  413. if (empty($cm->visible)) {
  414. if ($chain = hotpot_get_chain($cm)) {
  415. $startofchain = array_shift($chain);
  416. $visible = $startofchain->visible;
  417. }
  418. }
  419. }
  420. return $visible;
  421. }
  422. function hotpot_add_chain(&$hotpot) {
  423. /// add a chain of hotpot actiivities
  424. global $CFG, $course;
  425. $ok = true;
  426. $hotpot->names = array();
  427. $hotpot->summaries = array();
  428. $hotpot->references = array();
  429. $xml_quiz = new hotpot_xml_quiz($hotpot, false, false, false, false, false);
  430. if (isset($xml_quiz->error)) {
  431. $hotpot->errors['reference'] = $xml_quiz->error;
  432. $ok = false;
  433. } else if (is_dir($xml_quiz->filepath)) {
  434. // get list of hotpot files in this folder
  435. if ($dh = @opendir($xml_quiz->filepath)) {
  436. while (false !== ($file = @readdir($dh))) {
  437. if (preg_match('/\.(jbc|jcl|jcw|jmt|jmx|jqz|htm|html)$/', $file)) {
  438. $hotpot->references[] = "$xml_quiz->reference/$file";
  439. }
  440. }
  441. closedir($dh);
  442. // get titles
  443. foreach ($hotpot->references as $i=>$reference) {
  444. $filepath = $xml_quiz->fileroot.'/'.$reference;
  445. hotpot_get_titles_and_next_ex($hotpot, $filepath);
  446. $hotpot->names[$i] = $hotpot->exercisetitle;
  447. $hotpot->summaries[$i] = $hotpot->exercisesubtitle;
  448. }
  449. } else {
  450. $ok = false;
  451. $hotpot->errors['reference'] = get_string('error_couldnotopenfolder', 'hotpot', $hotpot->reference);
  452. }
  453. } else if (is_file($xml_quiz->filepath)) {
  454. $filerootlength = strlen($xml_quiz->fileroot) + 1;
  455. while ($xml_quiz->filepath) {
  456. hotpot_get_titles_and_next_ex($hotpot, $xml_quiz->filepath, true);
  457. $hotpot->names[] = $hotpot->exercisetitle;
  458. $hotpot->summaries[] = $hotpot->exercisesubtitle;
  459. $hotpot->references[] = substr($xml_quiz->filepath, $filerootlength);
  460. if ($hotpot->nextexercise) {
  461. $filepath = $xml_quiz->fileroot.'/'.$xml_quiz->filesubdir.$hotpot->nextexercise;
  462. // check file is not already in chain
  463. $reference = substr($filepath, $filerootlength);
  464. if (in_array($reference, $hotpot->references)) {
  465. $filepath = '';
  466. }
  467. } else {
  468. $filepath = '';
  469. }
  470. if ($filepath && file_exists($filepath) && is_file($filepath) && is_readable($filepath)) {
  471. $xml_quiz->filepath = $filepath;
  472. } else {
  473. $xml_quiz->filepath = false; // finish while loop
  474. }
  475. } // end while
  476. } else {
  477. $ok = false;
  478. $hotpot->errors['reference'] = get_string('error_notfileorfolder', 'hotpot', $hotpot->reference);
  479. }
  480. if (empty($hotpot->references) && empty($hotpot->errors['reference'])) {
  481. $ok = false;
  482. $hotpot->errors['reference'] = get_string('error_noquizzesfound', 'hotpot', $hotpot->reference);
  483. }
  484. if ($ok) {
  485. $hotpot->visible = HOTPOT_YES;
  486. if (trim($hotpot->name)=='') {
  487. $hotpot->name = get_string("modulename", $hotpot->modulename);
  488. }
  489. $hotpot->specificname = $hotpot->name;
  490. $hotpot->specificsummary = $hotpot->summary;
  491. // add all except last activity in chain
  492. $i_max = count($hotpot->references)-1;
  493. for ($i=0; $i<$i_max; $i++) {
  494. hotpot_set_name_summary_reference($hotpot, $i);
  495. $hotpot->reference = addslashes($hotpot->reference);
  496. if (!$hotpot->instance = insert_record("hotpot", $hotpot)) {
  497. error("Could not add a new instance of $hotpot->modulename", "view.php?id=$hotpot->course");
  498. }
  499. // store (hotpot table) id of start of chain
  500. if ($i==0) {
  501. $hotpot->startofchain = $hotpot->instance;
  502. }
  503. if (isset($course->groupmode)) {
  504. $hotpot->groupmode = $course->groupmode;
  505. }
  506. if (! $hotpot->coursemodule = add_course_module($hotpot)) {
  507. error("Could not add a new course module");
  508. }
  509. if (! $sectionid = add_mod_to_section($hotpot) ) {
  510. error("Could not add the new course module to that section");
  511. }
  512. if (! set_field("course_modules", "section", $sectionid, "id", $hotpot->coursemodule)) {
  513. error("Could not update the course module with the correct section");
  514. }
  515. add_to_log($hotpot->course, "course", "add mod",
  516. "../mod/$hotpot->modulename/view.php?id=$hotpot->coursemodule",
  517. "$hotpot->modulename $hotpot->instance"
  518. );
  519. add_to_log($hotpot->course, $hotpot->modulename, "add",
  520. "view.php?id=$hotpot->coursemodule",
  521. "$hotpot->instance", $hotpot->coursemodule
  522. );
  523. // hide tail of chain
  524. if ($hotpot->shownextquiz==HOTPOT_YES) {
  525. $hotpot->visible = HOTPOT_NO;
  526. }
  527. } // end for ($hotpot->references)
  528. // settings for final activity in chain
  529. hotpot_set_name_summary_reference($hotpot, $i);
  530. $hotpot->reference = addslashes($hotpot->references[$i]);
  531. $hotpot->shownextquiz = HOTPOT_NO;
  532. if (isset($hotpot->startofchain)) {
  533. // redirection only works for Moodle 1.5+
  534. $hotpot->redirect = true;
  535. $hotpot->redirecturl = "$CFG->wwwroot/mod/hotpot/view.php?hp=$hotpot->startofchain";
  536. }
  537. } // end if $ok
  538. return $ok;
  539. }
  540. function hotpot_set_name_summary_reference(&$hotpot, $chain_index=NULL) {
  541. $xml_quiz = NULL;
  542. $textfields = array('name', 'summary');
  543. foreach ($textfields as $textfield) {
  544. $textsource = $textfield.'source';
  545. // are we adding a chain?
  546. if (isset($chain_index)) {
  547. switch ($hotpot->$textsource) {
  548. case HOTPOT_TEXTSOURCE_QUIZ:
  549. if ($textfield=='name') {
  550. $hotpot->exercisetitle = $hotpot->names[$chain_index];
  551. } else if ($textfield=='summary') {
  552. $hotpot->exercisesubtitle = $hotpot->summaries[$chain_index];
  553. }
  554. break;
  555. case HOTPOT_TEXTSOURCE_SPECIFIC:
  556. $specifictext = 'specific'.$textfield;
  557. if (empty($hotpot->$specifictext) && trim($hotpot->$specifictext)=='') {
  558. $hotpot->$textfield = '';
  559. } else {
  560. $hotpot->$textfield = $hotpot->$specifictext.' ('.($chain_index+1).')';
  561. }
  562. break;
  563. }
  564. $hotpot->reference = $hotpot->references[$chain_index];
  565. }
  566. if ($hotpot->$textsource==HOTPOT_TEXTSOURCE_QUIZ) {
  567. if (empty($xml_quiz) && !isset($chain_index)) {
  568. $xml_quiz = new hotpot_xml_quiz($hotpot, false, false, false, false, false);
  569. hotpot_get_titles_and_next_ex($hotpot, $xml_quiz->filepath);
  570. }
  571. if ($textfield=='name') {
  572. $hotpot->$textfield = addslashes($hotpot->exercisetitle);
  573. } else if ($textfield=='summary') {
  574. $hotpot->$textfield = addslashes($hotpot->exercisesubtitle);
  575. }
  576. }
  577. switch ($hotpot->$textsource) {
  578. case HOTPOT_TEXTSOURCE_FILENAME:
  579. $hotpot->$textfield = basename($hotpot->reference);
  580. break;
  581. case HOTPOT_TEXTSOURCE_FILEPATH:
  582. $hotpot->$textfield = '';
  583. // continue to next lines
  584. default:
  585. if (empty($hotpot->$textfield)) {
  586. $hotpot->$textfield = str_replace('/', ' ', $hotpot->reference);
  587. }
  588. } // end switch
  589. } // end foreach
  590. }
  591. function hotpot_get_titles_and_next_ex(&$hotpot, $filepath, $get_next=false) {
  592. $hotpot->exercisetitle = '';
  593. $hotpot->exercisesubtitle = '';
  594. $hotpot->nextexercise = '';
  595. // read the quiz file source
  596. if ($source = file_get_contents($filepath)) {
  597. $next = '';
  598. $title = '';
  599. $subtitle = '';
  600. if (preg_match('|\.html?$|', $filepath)) {
  601. // html file
  602. if (preg_match('|<h2[^>]*class="ExerciseTitle"[^>]*>(.*?)</h2>|is', $source, $matches)) {
  603. $title = trim(strip_tags($matches[1]));
  604. }
  605. if (empty($title)) {
  606. if (preg_match('|<title[^>]*>(.*?)</title>|is', $source, $matches)) {
  607. $title = trim(strip_tags($matches[1]));
  608. }
  609. }
  610. if (preg_match('|<h3[^>]*class="ExerciseSubtitle"[^>]*>(.*?)</h3>|is', $source, $matches)) {
  611. $subtitle = trim(strip_tags($matches[1]));
  612. }
  613. if ($get_next) {
  614. if (preg_match('|<div[^>]*class="NavButtonBar"[^>]*>(.*?)</div>|is', $source, $matches)) {
  615. $navbuttonbar = $matches[1];
  616. if (preg_match_all('|<button[^>]*onclick="'."location='([^']*)'".'[^"]*"[^>]*>|is', $navbuttonbar, $matches)) {
  617. $lastbutton = count($matches[0])-1;
  618. $next = $matches[1][$lastbutton];
  619. }
  620. }
  621. }
  622. } else {
  623. // xml file (...maybe)
  624. $xml_tree = new hotpot_xml_tree($source);
  625. $xml_tree->filetype = '';
  626. $keys = array_keys($xml_tree->xml);
  627. foreach ($keys as $key) {
  628. if (preg_match('/^(hotpot|textoys)-(\w+)-file$/i', $key, $matches)) {
  629. $xml_tree->filetype = 'xml';
  630. $xml_tree->xml_root = "['$key']['#']";
  631. $xml_tree->quiztype = strtolower($matches[2]);
  632. break;
  633. }
  634. }
  635. if ($xml_tree->filetype=='xml') {
  636. $title = strip_tags($xml_tree->xml_value('data,title'));
  637. $subtitle = $xml_tree->xml_value('hotpot-config-file,'.$xml_tree->quiztype.',exercise-subtitle');
  638. if ($get_next) {
  639. $include = $xml_tree->xml_value('hotpot-config-file,global,include-next-ex');
  640. if (!empty($include)) {
  641. $next = $xml_tree->xml_value("hotpot-config-file,$xml_tree->quiztype,next-ex-url");
  642. if (is_array($next)) {
  643. $next = $next[0]; // in case "next-ex-url" was repeated in the xml file
  644. }
  645. }
  646. }
  647. }
  648. }
  649. $hotpot->nextexercise = $next;
  650. $hotpot->exercisetitle = (empty($title) || is_array($title)) ? basename($filepath) : $title;
  651. $hotpot->exercisesubtitle = (empty($subtitle) || is_array($subtitle)) ? $hotpot->exercisetitle : $subtitle;
  652. }
  653. }
  654. function hotpot_get_all_instances_in_course($modulename, $course) {
  655. /// called from index.php
  656. global $CFG;
  657. $instances = array();
  658. if (isset($CFG->release) && substr($CFG->release, 0, 3)>=1.2) {
  659. $groupmode = 'cm.groupmode,';
  660. } else {
  661. $groupmode = '';
  662. }
  663. $query = "
  664. SELECT
  665. cm.id AS coursemodule,
  666. cm.course AS course,
  667. cm.module AS module,
  668. cm.instance AS instance,
  669. -- cm.section AS section,
  670. cm.visible AS visible,
  671. $groupmode
  672. -- cs.section AS sectionnumber,
  673. cs.section AS section,
  674. cs.sequence AS sequence,
  675. cs.visible AS sectionvisible,
  676. thismodule.*
  677. FROM
  678. {$CFG->prefix}course_modules cm,
  679. {$CFG->prefix}course_sections cs,
  680. {$CFG->prefix}modules m,
  681. {$CFG->prefix}$modulename thismodule
  682. WHERE
  683. m.name = '$modulename' AND
  684. m.id = cm.module AND
  685. cm.course = '$course->id' AND
  686. cm.section = cs.id AND
  687. cm.instance = thismodule.id
  688. ";
  689. if ($rawmods = get_records_sql($query)) {
  690. // cache $isteacher setting
  691. $isteacher = has_capability('mod/hotpot:viewreport', get_context_instance(CONTEXT_COURSE, $course->id));
  692. $explodesection = array();
  693. $order = array();
  694. foreach ($rawmods as $rawmod) {
  695. if (empty($explodesection[$rawmod->section])) {
  696. $explodesection[$rawmod->section] = true;
  697. $coursemodules = explode(',', $rawmod->sequence);
  698. foreach ($coursemodules as $i=>$coursemodule) {
  699. $order[$coursemodule] = sprintf('%d.%04d', $rawmod->section, $i);
  700. }
  701. }
  702. if ($isteacher) {
  703. $visible = true;
  704. } else if ($modulename=='hotpot') {
  705. $visible = hotpot_is_visible($rawmod);
  706. } else {
  707. $visible = $rawmod->visible;
  708. }
  709. if ($visible) {
  710. $instances[$order[$rawmod->coursemodule]] = $rawmod;
  711. }
  712. } // end foreach $modinfo
  713. ksort($instances);
  714. $instances = array_values($instances);
  715. }
  716. return $instances;
  717. }
  718. function hotpot_update_chain(&$hotpot) {
  719. /// update a chain of hotpot actiivities
  720. $ok = true;
  721. if ($hotpot_modules = hotpot_get_chain($hotpot)) {
  722. // skip updating of these fields
  723. $skipfields = array('id', 'course', 'name', 'reference', 'summary', 'shownextquiz');
  724. $fields = array();
  725. foreach ($hotpot_modules as $hotpot_module) {
  726. if ($hotpot->instance==$hotpot_module->id) {
  727. // don't need to update this hotpot
  728. } else {
  729. // shortcut to hotpot record
  730. $thishotpot = &$hotpot_module->hotpot;
  731. // get a list of fields to update (first time only)
  732. if (empty($fields)) {
  733. $fields = array_keys(get_object_vars($thishotpot));
  734. }
  735. // assume update is NOT required
  736. $require_update = false;
  737. // update field values (except $skipfields)
  738. foreach($fields as $field) {
  739. if (in_array($field, $skipfields) || $thishotpot->$field==$hotpot->$field) {
  740. // update not required for this field
  741. } else {
  742. $require_update = true;
  743. $thishotpot->$field = $hotpot->$field;
  744. }
  745. }
  746. // update $thishotpot, if required
  747. if ($require_update && !update_record("hotpot", $thishotpot)) {
  748. error("Could not update the $hotpot->modulename", "view.php?id=$hotpot->course");
  749. }
  750. }
  751. } // end foreach $ids
  752. }
  753. return $ok;
  754. }
  755. function hotpot_delete_instance($id) {
  756. /// Given an ID of an instance of this module,
  757. /// this function will permanently delete the instance
  758. /// and any data that depends on it.
  759. if (! $hotpot = get_record("hotpot", "id", $id)) {
  760. return false;
  761. }
  762. if (! delete_records("hotpot", "id", "$id")) {
  763. return false;
  764. }
  765. delete_records("hotpot_questions", "hotpot", "$id");
  766. if ($attempts = get_records_select("hotpot_attempts", "hotpot='$id'")) {
  767. $ids = implode(',', array_keys($attempts));
  768. delete_records_select("hotpot_attempts", "id IN ($ids)");
  769. delete_records_select("hotpot_details", "attempt IN ($ids)");
  770. delete_records_select("hotpot_responses", "attempt IN ($ids)");
  771. }
  772. // remove calendar events for this hotpot
  773. delete_records('event', 'modulename', 'hotpot', 'instance', $id);
  774. // remove grade item for this hotpot
  775. hotpot_grade_item_delete($hotpot);
  776. return true;
  777. }
  778. function hotpot_delete_and_notify($table, $select, $strtable) {
  779. $count = max(0, count_records_select($table, $select));
  780. if ($count) {
  781. delete_records_select($table, $select);
  782. $count -= max(0, count_records_select($table, $select));
  783. if ($count) {
  784. notify(get_string('deleted')." $count x $strtable");
  785. }
  786. }
  787. }
  788. function hotpot_user_complete($course, $user, $mod, $hotpot) {
  789. /// Print a detailed representation of what a user has done with
  790. /// a given particular instance of this module, for user activity reports.
  791. $report = hotpot_user_outline($course, $user, $mod, $hotpot);
  792. if (empty($report)) {
  793. print get_string("noactivity", "hotpot");
  794. } else {
  795. $date = userdate($report->time, get_string('strftimerecentfull'));
  796. print $report->info.' '.get_string('mostrecently').': '.$date;
  797. }
  798. return true;
  799. }
  800. function hotpot_user_outline($course, $user, $mod, $hotpot) {
  801. /// Return a small object with summary information about what a
  802. /// user has done with a given particular instance of this module
  803. /// Used for user activity reports.
  804. /// $report->time = the time they did it
  805. /// $report->info = a short text description
  806. $report = NULL;
  807. if ($records = get_records_select("hotpot_attempts", "hotpot='$hotpot->id' AND userid='$user->id'", "timestart ASC", "*")) {
  808. $report = new stdClass();
  809. $scores = array();
  810. foreach ($records as $record){
  811. if (empty($report->time)) {
  812. $report->time = $record->timestart;
  813. }
  814. $scores[] = hotpot_format_score($record);
  815. }
  816. if (empty($scores)) {
  817. $report->time = 0;
  818. $report->info = get_string('noactivity', 'hotpot');
  819. } else {
  820. $report->info = get_string('score', 'quiz').': '.implode(', ', $scores);
  821. }
  822. }
  823. return $report;
  824. }
  825. function hotpot_format_score($record, $undefined='&nbsp;') {
  826. if (isset($record->score)) {
  827. $score = $record->score;
  828. } else {
  829. $score = $undefined;
  830. }
  831. return $score;
  832. }
  833. function hotpot_format_status($record, $undefined='&nbsp;') {
  834. global $HOTPOT_STATUS;
  835. if (isset($record->status) || isset($HOTPOT_STATUS[$record->status])) {
  836. $status = $HOTPOT_STATUS[$record->status];
  837. } else {
  838. $status = $undefined;
  839. }
  840. return $status;
  841. }
  842. function hotpot_print_recent_activity($course, $isteacher, $timestart) {
  843. /// Given a course and a time, this module should find recent activity
  844. /// that has occurred in hotpot activities and print it out.
  845. /// Return true if there was output, or false is there was none.
  846. global $CFG;
  847. $result = false;
  848. $records = get_records_sql("
  849. SELECT
  850. h.id AS id,
  851. h.name AS name,
  852. COUNT(*) AS count_attempts
  853. FROM
  854. {$CFG->prefix}hotpot h,
  855. {$CFG->prefix}hotpot_attempts a
  856. WHERE
  857. h.course = $course->id
  858. AND h.id = a.hotpot
  859. AND a.id = a.clickreportid
  860. AND a.starttime > $timestart
  861. GROUP BY
  862. h.id, h.name
  863. ");
  864. // note that PostGreSQL requires h.name in the GROUP BY clause
  865. if($records) {
  866. $names = array();
  867. foreach ($records as $id => $record){
  868. if ($cm = get_coursemodule_from_instance('hotpot', $record->id, $course->id)) {
  869. $context = get_context_instance(CONTEXT_MODULE, $cm->id);
  870. if (has_capability('mod/hotpot:viewreport', $context)) {
  871. $href = "$CFG->wwwroot/mod/hotpot/view.php?hp=$id";
  872. $name = '&nbsp;<a href="'.$href.'">'.$record->name.'</a>';
  873. if ($record->count_attempts > 1) {
  874. $name .= " ($record->count_attempts)";
  875. }
  876. $names[] = $name;
  877. }
  878. }
  879. }
  880. if (count($names) > 0) {
  881. print_headline(get_string('modulenameplural', 'hotpot').':');
  882. if ($CFG->version >= 2005050500) { // Moodle 1.5+
  883. echo '<div class="head"><div class="name">'.implode('<br />', $names).'</div></div>';
  884. } else { // Moodle 1.4.x (or less)
  885. echo '<font size="1">'.implode('<br />', $names).'</font>';
  886. }
  887. $result = true;
  888. }
  889. }
  890. return $result; // True if anything was printed, otherwise false
  891. }
  892. function hotpot_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $cmid="", $userid="", $groupid="") {
  893. // Returns all quizzes since a given time.
  894. global $CFG;
  895. // If $cmid or $userid are specified, then this restricts the results
  896. $cm_select = empty($cmid) ? "" : " AND cm.id = '$cmid'";
  897. $user_select = empty($userid) ? "" : " AND u.id = '$userid'";
  898. $records = get_records_sql("
  899. SELECT
  900. a.*,
  901. h.name, h.course,
  902. cm.instance, cm.section,
  903. u.firstname, u.lastname, u.picture
  904. FROM
  905. {$CFG->prefix}hotpot_attempts a,
  906. {$CFG->prefix}hotpot h,
  907. {$CFG->prefix}course_modules cm,
  908. {$CFG->prefix}user u
  909. WHERE
  910. a.timefinish > '$sincetime'
  911. AND a.id = a.clickreportid
  912. AND a.userid = u.id $user_select
  913. AND a.hotpot = h.id $cm_select
  914. AND cm.instance = h.id
  915. AND cm.course = '$courseid'
  916. AND h.course = cm.course
  917. ORDER BY
  918. a.timefinish ASC
  919. ");
  920. if (!empty($records)) {
  921. foreach ($records as $record) {
  922. if (empty($groupid) || groups_is_member($groupid, $record->userid)) {
  923. unset($activity);
  924. $activity->type = "hotpot";
  925. $activity->defaultindex = $index;
  926. $activity->instance = $record->hotpot;
  927. $activity->name = $record->name;
  928. $activity->section = $record->section;
  929. $activity->content->attemptid = $record->id;
  930. $activity->content->attempt = $record->attempt;
  931. $activity->content->score = $record->score;
  932. $activity->content->timestart = $record->timestart;
  933. $activity->content->timefinish = $record->timefinish;
  934. $activity->user->userid = $record->userid;
  935. $activity->user->fullname = fullname($record);
  936. $activity->user->picture = $record->picture;
  937. $activity->timestamp = $record->timefinish;
  938. $activities[] = $activity;
  939. $index++;
  940. }
  941. } // end foreach
  942. }
  943. }
  944. function hotpot_print_recent_mod_activity($activity, $courseid, $detail=false) {
  945. /// Basically, this function prints the results of "hotpot_get_recent_activity"
  946. global $CFG, $THEME, $USER;
  947. if (isset($THEME->cellcontent2)) {
  948. $bgcolor = ' bgcolor="'.$THEME->cellcontent2.'"';
  949. } else {
  950. $bgcolor = '';
  951. }
  952. if (is_object($courseid) && isset($courseid->id)) {
  953. $courseid = $courseid->id; // shouldn't happen !!
  954. }
  955. print '<table border="0" cellpadding="3" cellspacing="0">';
  956. print '<tr><td'.$bgcolor.' class="forumpostpicture" width="35" valign="top">';
  957. print_user_picture($activity->user->userid, $courseid, $activity->user->picture);
  958. print '</td><td width="100%"><font size="2">';
  959. if ($detail) {
  960. // activity icon
  961. $src = "$CFG->modpixpath/$activity->type/icon.gif";
  962. print '<img src="'.$src.'" class="icon" alt="'.$activity->type.'" /> ';
  963. // link to activity
  964. $href = "$CFG->wwwroot/mod/hotpot/view.php?hp=$activity->instance";
  965. print '<a href="'.$href.'">'.$activity->name.'</a> - ';
  966. }
  967. if (has_capability('mod/hotpot:viewreport',get_context_instance(CONTEXT_COURSE, $courseid))) {
  968. // score (with link to attempt details)
  969. $href = "$CFG->wwwroot/mod/hotpot/review.php?hp=$activity->instance&attempt=".$activity->content->attemptid;
  970. print '<a href="'.$href.'">('.hotpot_format_score($activity->content).')</a> ';
  971. // attempt number
  972. print get_string('attempt', 'quiz').' - '.$activity->content->attempt.'<br />';
  973. }
  974. // link to user
  975. $href = "$CFG->wwwroot/user/view.php?id=$activity->user->userid&course=$courseid";
  976. print '<a href="'.$href.'">'.$activity->user->fullname.'</a> ';
  977. // time and date
  978. print ' - ' . userdate($activity->timestamp);
  979. // duration
  980. $duration = format_time($activity->content->timestart - $activity->content->timefinish);
  981. print " &nbsp; ($duration)";
  982. print "</font></td></tr>";
  983. print "</table>";
  984. }
  985. function hotpot_cron () {
  986. /// Function to be run periodically according to the moodle cron
  987. /// This function searches for things that need to be done, such
  988. /// as sending out mail, toggling flags etc ...
  989. global $CFG;
  990. return true;
  991. }
  992. function hotpot_grades($hotpotid) {
  993. /// Must return an array of grades for a given instance of this module,
  994. /// indexed by user. It also returns a maximum allowed grade.
  995. $hotpot = get_record('hotpot', 'id', $hotpotid);
  996. $return->grades = hotpot_get_grades($hotpot);
  997. $return->maxgrade = $hotpot->grade;
  998. return $return;
  999. }
  1000. function hotpot_get_grades($hotpot, $user_ids='') {
  1001. global $CFG;
  1002. $grades = array();
  1003. $weighting = $hotpot->grade / 100;
  1004. $precision = hotpot_get_precision($hotpot);
  1005. // set the SQL string to determine the $grade
  1006. $grade = "";
  1007. switch ($hotpot->grademethod) {
  1008. case HOTPOT_GRADEMETHOD_HIGHEST:
  1009. $grade = "ROUND(MAX(score) * $weighting, $precision) AS grade";
  1010. break;
  1011. case HOTPOT_GRADEMETHOD_AVERAGE:
  1012. // the 'AVG' function skips abandoned quizzes, so use SUM(score)/COUNT(id)
  1013. $grade = "ROUND(SUM(score)/COUNT(id) * $weighting, $precision) AS grade";
  1014. break;
  1015. case HOTPOT_GRADEMETHOD_FIRST:
  1016. $grade = "ROUND(score * $weighting, $precision)";
  1017. $grade = sql_concat('timestart', "'_'", $grade);
  1018. $grade = "MIN($grade) AS grade";
  1019. break;
  1020. case HOTPOT_GRADEMETHOD_LAST:
  1021. $grade = "ROUND(score * $weighting, $precision)";
  1022. $grade = sql_concat('timestart', "'_'", $grade);
  1023. $grade = "MAX($grade) AS grade";
  1024. break;
  1025. }
  1026. if ($grade) {
  1027. $userid_condition = empty($user_ids) ? '' : "AND userid IN ($user_ids) ";
  1028. $grades = get_records_sql_menu("
  1029. SELECT userid, $grade
  1030. FROM {$CFG->prefix}hotpot_attempts
  1031. WHERE timefinish>0 AND hotpot='$hotpot->id' $userid_condition
  1032. GROUP BY userid
  1033. ");
  1034. if ($grades) {
  1035. if ($hotpot->grademethod==HOTPOT_GRADEMETHOD_FIRST || $hotpot->grademethod==HOTPOT_GRADEMETHOD_LAST) {
  1036. // remove left hand characters in $grade (up to and including the underscore)
  1037. foreach ($grades as $userid=>$grade) {
  1038. $grades[$userid] = substr($grades[$userid], strpos($grades[$userid], '_')+1);
  1039. }
  1040. }
  1041. }
  1042. }
  1043. return $grades;
  1044. }
  1045. function hotpot_get_precision(&$hotpot) {
  1046. return ($hotpot->grademethod==HOTPOT_GRADEMETHOD_AVERAGE || $hotpot->grade<100) ? 1 : 0;
  1047. }
  1048. /**
  1049. * Return grade for given user or all users.
  1050. *
  1051. * @param object $hotpot
  1052. * @param int $userid optional user id, 0 means all users
  1053. * @return array array of grades, false if none
  1054. */
  1055. function hotpot_get_user_grades($hotpot, $userid=0) {
  1056. $grades = array();
  1057. if ($hotpotgrades = hotpot_get_grades($hotpot, $userid)) {
  1058. foreach ($hotpotgrades as $hotpotuserid => $hotpotgrade) {
  1059. $grades[$hotpotuserid] = new stdClass();
  1060. $grades[$hotpotuserid]->id = $hotpotuserid;
  1061. $grades[$hotpotuserid]->userid = $hotpotuserid;
  1062. $grades[$hotpotuserid]->rawgrade = $hotpotgrade;
  1063. }
  1064. }
  1065. if (count($grades)) {
  1066. return $grades;
  1067. } else {
  1068. return false;
  1069. }
  1070. }
  1071. /**
  1072. * Update grades in central gradebook
  1073. * this function is called from db/upgrade.php
  1074. * it is initially called with no arguments, which forces it to get a list of all hotpots
  1075. * it then iterates through the hotpots, calling itself to create a grade record for each hotpot
  1076. *
  1077. * @param object $hotpot null means all hotpots
  1078. * @param int $userid specific user only, 0 means all users
  1079. */
  1080. function hotpot_update_grades($hotpot=null, $userid=0, $nullifnone=true) {
  1081. global $CFG;
  1082. if (! function_exists('grade_update')) {
  1083. require_once($CFG->libdir.'/gradelib.php');
  1084. }
  1085. if (is_null($hotpot)) {
  1086. // update (=create) grades for all hotpots
  1087. $sql = "
  1088. SELECT h.*, cm.idnumber as cmidnumber
  1089. FROM {$CFG->prefix}hotpot h, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
  1090. WHERE m.name='hotpot' AND m.id=cm.module AND cm.instance=h.id"
  1091. ;
  1092. if ($rs = get_recordset_sql($sql)) {
  1093. while ($hotpot = rs_fetch_next_record($rs)) {
  1094. hotpot_update_grades($hotpot, 0, false);
  1095. }
  1096. rs_close($rs);
  1097. }
  1098. } else {
  1099. // update (=create) grade for a single hotpot
  1100. if ($grades = hotpot_get_user_grades($hotpot, $userid)) {
  1101. hotpot_grade_item_update($hotpot, $grades);
  1102. } else if ($userid && $nullifnone) {
  1103. // no grades for this user, but we must force the creation of a "null" grade record
  1104. $grade = new object();
  1105. $grade->userid = $userid;
  1106. $grade->rawgrade = null;
  1107. hotpot_grade_item_update($hotpot, $grade);
  1108. } else {
  1109. // no grades and no userid
  1110. hotpot_grade_item_update($hotpot);
  1111. }
  1112. }
  1113. }
  1114. /**
  1115. * Update/create grade item for given hotpot
  1116. *
  1117. * @param object $hotpot object with extra cmidnumber
  1118. * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
  1119. * @return object grade_item
  1120. */
  1121. function hotpot_grade_item_update($hotpot, $grades=null) {
  1122. global $CFG;
  1123. if (! function_exists('grade_update')) {
  1124. require_once($CFG->libdir.'/gradelib.php');
  1125. }
  1126. $params = array('itemname' => $hotpot->name);
  1127. if (array_key_exists('cmidnumber', $hotpot)) {
  1128. //cmidnumber may not be always present
  1129. $params['idnumber'] = $hotpot->cmidnumber;
  1130. }
  1131. if ($hotpot->grade > 0) {
  1132. $params['gradetype'] = GRADE_TYPE_VALUE;
  1133. $params['grademax'] = $hotpot->grade;
  1134. $params['grademin'] = 0;
  1135. } else {
  1136. $params['gradetype'] = GRADE_TYPE_NONE;
  1137. // Note: when adding a new activity, a gradeitem will *not*
  1138. // be created in the grade book if gradetype==GRADE_TYPE_NONE
  1139. // A gradeitem will be created later if gradetype changes to GRADE_TYPE_VALUE
  1140. // However, the gradeitem will *not* be deleted if the activity's
  1141. // gradet…

Large files files are truncated, but you can click here to view the full file