PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/moodle/mod/wwassignment/lib.php

https://bitbucket.org/matthaught/wwassignment4
PHP | 741 lines | 384 code | 111 blank | 246 comment | 54 complexity | 3247b1bc7cc6a9e5985ec8d70d49a9c6 MD5 | raw file
  1. <?php
  2. global $CFG;
  3. require_once("locallib.php");
  4. // debug switch defined in locallib.php define('WWASSIGNMENT_DEBUG',0);
  5. ////////////////////////////////////////////////////////////////
  6. // External grade triggers
  7. // wwassignment_update_grades(wwassignment,userid=0) is called from
  8. // grade_update_mod_grades in gradlib.php and also from wwassignment/upgrade.php file
  9. // grade_update_mod_grades is called by $grade_item->refresh_grades
  10. //
  11. // wwassignment_grade_item_update(wwassignment)
  12. // is called from grade_update_mod_grades (before update_grades(wwassignment,userid=0)))
  13. //
  14. // wwassignment_get_user_grades
  15. // could be called from wwassignment/index.php pages (legacy??)
  16. //
  17. // High level grade calls in gradelib.php (see end of file)
  18. //
  19. //
  20. //
  21. // Internal grade calling structure
  22. //
  23. // wwassignment_update_grades($wwassignment=null, $userid=0, $nullifnone=true) -- updates grades for assignment instance or all instances
  24. // wwassignment_get_user_grades($wwassignment,$userid=0) -- fetches homework grades from WeBWorK
  25. // _wwassignment_get_course_students($courseid) -- collects users from moodle database
  26. // $wwclient->grade_users_sets($webworkcourse,$webworkusers,$webworkset) -- fetches grades from a given course, set and user collection
  27. // wwassignment_grade_item_update(wwassignment, grades)
  28. // grade_update(...) -- fills record in grade_item table and possibly in grade_grades table as well
  29. //
  30. // wwassignment_update_grade_item(wwassignment) -- just updates grade_item table
  31. // wwassignment_update_grade_item(wwassignment, grades) updates grade_item table and grade_grades table
  32. ////////////////////////////////////////////////////////////////
  33. ////////////////////////////////////////////////////////////////
  34. //Functions that are called by the Moodle System
  35. ////////////////////////////////////////////////////////////////
  36. /**
  37. * @desc Called when the module is installed into the Moodle System
  38. */
  39. function wwassignment_install() {
  40. }
  41. /**
  42. * @desc Creates a new Moodle assignment <-> Webwork problem set tie.
  43. * @param $wwassignment object The data of the record to be entered in the DB.
  44. * @return integer The ID of the new record.
  45. */
  46. function wwassignment_add_instance($wwassignment) {
  47. global $COURSE;
  48. debugLog("Begin wwassignment_add_instance");
  49. debugLog("input wwassignment ");
  50. //debugLog( print_r($wwassignment, true) );
  51. //Get data about the set from WebWorK
  52. $wwclient = new wwassignment_client();
  53. $wwassignment->webwork_course = _wwassignment_mapped_course($COURSE->id,false);
  54. $wwsetdata = $wwclient->get_assignment_data($wwassignment->webwork_course,$wwassignment->webwork_set,false);
  55. //Attaching Moodle Set to WeBWorK Set
  56. debugLog("saving wwassignment ");
  57. debugLog( print_r($wwassignment,true));
  58. $wwassignment->timemodified = time();
  59. if ($returnid = insert_record("wwassignment", $wwassignment)) {
  60. $wwassignment->id = $returnid;
  61. //Creating events
  62. _wwassignment_create_events($wwassignment,$wwsetdata);
  63. debugLog("notify gradebook");
  64. //notify gradebook
  65. wwassignment_grade_item_update($wwassignment);
  66. }
  67. debugLog("End wwassignment_add_instance");
  68. return $returnid;
  69. }
  70. /**
  71. * @desc Updates and resynchronizes all information related to the a moodle assignment <-> webwork problem set tie.
  72. * except for grades
  73. * @param $wwassignment object The data of the record to be updated in the DB.
  74. * @return integer The result of the update_record function.
  75. */
  76. function wwassignment_update_instance($wwassignment) {
  77. global $COURSE;
  78. require_once("locallib.php");
  79. debugLog("Begin wwassignment_update_instance");
  80. //checking mappings
  81. $wwclient = new wwassignment_client();
  82. $wwcoursename = _wwassignment_mapped_course($COURSE->id,false);
  83. $wwassignment->webwork_course = $wwcoursename;
  84. $wwsetname = $wwassignment->webwork_set;
  85. $wwsetdata = $wwclient->get_assignment_data($wwcoursename,$wwsetname,false);
  86. //get data from WeBWorK
  87. $wwsetdata = $wwclient->get_assignment_data($wwcoursename,$wwsetname,false);
  88. $wwassignment->id = $wwassignment->instance;
  89. $wwassignment->grade = $wwclient->get_max_grade($wwcoursename,$wwsetname,false);
  90. $wwassignment->timemodified = time();
  91. $returnid = update_record('wwassignment',$wwassignment);
  92. _wwassignment_delete_events($wwassignment->id);
  93. _wwassignment_create_events($wwassignment,$wwsetdata);
  94. //notify gradebook -- update grades for this wwassignment only
  95. wwassignment_grade_item_update($wwassignment);
  96. wwassignment_update_grades($wwassignment);
  97. debugLog("End wwassignment_update_instance");
  98. return $returnid;
  99. }
  100. /**
  101. * @desc Deletes a tie in Moodle. Deletes nothing in webwork.
  102. * @param integer $wwassignmentid The id of the assignment to delete.
  103. * @return bool Delete was successful or not.
  104. */
  105. function wwassignment_delete_instance($wwassignmentid) {
  106. debugLog("Begin wwassignment_delete_instance -- input wwassignmentid:");
  107. debugLog(print_r($wwassignmentid,true));
  108. $result = true;
  109. #delete DB record
  110. if ( ! $wwassignment = get_record('wwassignment', 'id',$wwassignmentid)) {
  111. $result = false;
  112. }
  113. $wwassignment->courseid = $wwassignment->course;
  114. #delete events
  115. _wwassignment_delete_events($wwassignmentid);
  116. // Get the cm id to properly clean up the grade_items for this assignment
  117. // bug 4976
  118. // if (! $cm = get_record('modules', 'name', 'wwassignment')) {
  119. // $result = false;
  120. // } else {
  121. // if (! delete_records('grade_item', 'modid', $cm->id, 'cminstance', $wwassignment->id)) {
  122. // $result = false;
  123. // }
  124. // }
  125. if (! delete_records('wwassignment', 'id', $wwassignment->id)) {
  126. $result = false;
  127. }
  128. //notify gradebook
  129. wwassignment_grade_item_delete($wwassignment);
  130. debugLog("End wwassignment_delete_instance -- input wwassignmentid:");
  131. return $result;
  132. }
  133. /** gradebook upgrades
  134. * add xxx_update_grades() function into mod/xxx/lib.php
  135. Ê Ê * add xxx_grade_item_update() function into mod/xxx/lib.php
  136. Ê Ê * patch xxx_update_instance(), xxx_insert_instance()? xxx_add_instance() and xxx_delete_instance() to call xxx_grade_item_update()
  137. Ê Ê * patch all places of code that change grade values to call xxx_update_grades()
  138. Ê Ê * patch code that displays grades to students to use final grades from the gradebookÊ
  139. **/
  140. /**
  141. * Return grade for given user or all users.
  142. *
  143. * @param int $assignmentid id of assignment
  144. * @param int $userid optional user id, 0 means all users
  145. * @return array array of grades, false if none
  146. */
  147. function wwassignment_get_user_grades($wwassignment,$userid=0) {
  148. debugLog("Begin wwassignment_get_user_grades");
  149. //debugLog("inputs -- wwassignment" . print_r($wwassignment,true));
  150. //debugLog("userid = $userid");
  151. require_once("locallib.php");
  152. //checking mappings
  153. $courseid = $wwassignment->course;
  154. $wwclient = new wwassignment_client();
  155. $wwcoursename = _wwassignment_mapped_course($courseid,false);
  156. $wwsetname = $wwassignment->webwork_set;
  157. $usernamearray = array();
  158. $students = array();
  159. $studentgrades = array();
  160. if ($userid) {
  161. $user = get_complete_user_data('id',$userid);
  162. $username = $user->username;
  163. array_push($usernamearray, $username);
  164. array_push($students, $user);
  165. } else { // get all student names
  166. $students = _wwassignment_get_course_students( $courseid);
  167. foreach($students as $student) {
  168. array_push($usernamearray,$student->username);
  169. }
  170. }
  171. $gradearray = $wwclient->grade_users_sets($wwcoursename,$usernamearray,$wwsetname); // FIXME? return key/value pairs instead?
  172. // returns an array of grades -- the number of questions answered correctly?
  173. // debugLog("usernamearray " . print_r($usernamearray, true));
  174. // debugLog("grades($wwcoursename,usernamearray,$wwsetname) = " . print_r($gradearray, true));
  175. // model for output of grades
  176. $i =0;
  177. foreach($students as $student) {
  178. $studentid = $student->id;
  179. $grade = new stdClass();
  180. $grade->userid = $studentid;
  181. $grade->rawgrade =$gradearray[$i];
  182. $grade->feedback = "some text";
  183. $grade->feedbackformat = 0;
  184. $grade->usermodified = 0;
  185. $grade->dategraded = 0;
  186. $grade->datesubmitted = 0;
  187. $grade->id = $studentid;
  188. $studentgrades[$studentid] = $grade;
  189. $i++;
  190. }
  191. // end model
  192. //debugLog("output student grades:" . print_r($studentgrades,true) );
  193. debugLog("End wwassignment_get_user_grades");
  194. return $studentgrades;
  195. }
  196. /**
  197. * Update grades by firing grade_updated event
  198. *
  199. * @param object $wwassignment object with extra cmidnumber ??
  200. * @param object $wwassignment null means all wwassignments
  201. * @param int $userid specific user only, 0 mean all
  202. */
  203. function wwassignment_update_grades($wwassignment=null, $userid=0, $nullifnone=true) {
  204. debugLog("Begin wwassignment_update_grades");
  205. //debugLog("inputs wwassignment = " . print_r($wwassignment,true));
  206. debugLog("userid = $userid");
  207. global $CFG;
  208. if (!function_exists('grade_update')) { //workaround for buggy PHP versions
  209. require_once($CFG->libdir.'/gradelib.php');
  210. }
  211. if ($wwassignment != null) {
  212. // Make sure wwassignment has cmid defined isnce wwassignment_grade_item_update requires it
  213. if (!$wwassignment->cmidnumber) { // is this ever needed?
  214. $wwassignment->cmidnumber =_wwassignment_cmid() ;
  215. //error_log("adding cmidnumber to wwassignment".$wwassignment->cmidnumber);
  216. }
  217. if ($grades = wwassignment_get_user_grades($wwassignment, $userid)) { # fetches all students if userid=0
  218. foreach($grades as $k=>$v) {
  219. if ($v->rawgrade == -1) {
  220. $grades[$k]->rawgrade = null;
  221. }
  222. }
  223. wwassignment_grade_item_update($wwassignment, $grades);
  224. } else {
  225. wwassignment_grade_item_update($wwassignment);
  226. }
  227. } else { // find all the assignments
  228. debugLog("import grades for all wwassignments for all courses");
  229. $sql = "SELECT a.*, cm.idnumber as cmidnumber, a.course as courseid
  230. FROM {$CFG->prefix}wwassignment a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
  231. WHERE m.name='wwassignment' AND m.id=cm.module AND cm.instance=a.id";
  232. //$sql = " SELECT a.* FROM {$CFG->prefix}wwassignment a";
  233. //debugLog ("sql string = $sql");
  234. //$tmp = get_recordset_sql($sql);
  235. //error_log("result is ".print_r($tmp,true) );
  236. if ($rs = get_recordset_sql($sql)) {
  237. debugLog("record set found");
  238. while ($wwassignment = rs_fetch_next_record($rs)) {
  239. if (!$wwassignment->cmidnumber) { // is this ever needed?
  240. $wwassignment->cmidnumber =_wwassignment_cmid() ;
  241. }
  242. //debugLog("processing next grade wwassignment is ".print_r($wwassignment,true) );
  243. if ($wwassignment->grade != 0) {
  244. wwassignment_update_grades($wwassignment);
  245. } else {
  246. wwassignment_grade_item_update($wwassignment);
  247. }
  248. }
  249. rs_close($rs);
  250. }
  251. }
  252. debugLog("End wwassignment_update_grades");
  253. }
  254. /**
  255. * Create grade item for given assignment
  256. *
  257. * @param object $wwassignment object with extra cmidnumber
  258. * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
  259. * @return int 0 if ok, error code otherwise
  260. */
  261. function wwassignment_grade_item_update ($wwassignment, $grades=NULL) {
  262. $msg = "Begin wwassignment_grade_item_update";
  263. $msg = ($grades)? $msg . " with grades (updates grade_grades table)" :$msg;
  264. debugLog($msg);
  265. // debugLog("inputs wwassignment " . print_r($wwassignment, true));
  266. // debugLog("grades " . print_r($grades, true) );
  267. global $CFG;
  268. if (!function_exists('grade_update')) { //workaround for buggy PHP versions
  269. require_once($CFG->libdir.'/gradelib.php');
  270. }
  271. if (!isset($wwassignment->courseid)) {
  272. $wwassignment->courseid = $wwassignment->course;
  273. }
  274. if (!isset($wwassignment->grade) ) { // this case occurs when the set link is edited from moodle activity editor
  275. $wwclient = new wwassignment_client();
  276. $wwcoursename = _wwassignment_mapped_course($wwassignment->courseid,false); //last 'false' means report errors
  277. $wwsetname = _wwassignment_mapped_set($wwassignment->id,false);
  278. $wwassignment->grade = $wwclient->get_max_grade($wwcoursename,$wwsetname,false);
  279. }
  280. // set grade in wwassignment
  281. $params = array('itemname'=>$wwassignment->name, 'idnumber'=>$wwassignment->cmidnumber);
  282. if ($wwassignment->grade > 0) {
  283. $params['gradetype'] = GRADE_TYPE_VALUE;
  284. $params['grademax'] = $wwassignment->grade;
  285. $params['grademin'] = 0;
  286. } else if ($wwassignment->grade < 0) {
  287. $params['gradetype'] = GRADE_TYPE_SCALE;
  288. $params['scaleid'] = -$wwassignment->grade;
  289. } else {
  290. $params['gradetype'] = GRADE_TYPE_TEXT; // allow text comments only
  291. }
  292. if ($grades === 'reset') {
  293. $params['reset'] = true;
  294. $grades = NULL;
  295. }
  296. # grade_update() defined in gradelib.php
  297. # $grades=NULL means update grade_item table only, otherwise post grades in grade_grades
  298. debugLog("End wwassignment_grade_item_update");
  299. error_log("update grades for courseid: ". $wwassignment->courseid . " assignment id: ".$wwassignment->id." time modified ".$wwassignment->timemodified);
  300. return grade_update('mod/wwassignment', $wwassignment->courseid, 'mod', 'wwassignment', $wwassignment->id, 0, $grades, $params);
  301. }
  302. /**
  303. * Delete grade item for given assignment
  304. *
  305. * @param object $wwassignment object
  306. * @return object wwassignment ????
  307. */
  308. function wwassignment_grade_item_delete($wwassignment) {
  309. debugLog("Begin wwassignment_grade_item_delete");
  310. debugLog("inputs wwassignment " . print_r($wwassignment, true) );
  311. global $CFG;
  312. require_once($CFG->libdir.'/gradelib.php');
  313. if (!isset($wwassignment->courseid)) {
  314. $wwassignment->courseid = $wwassignment->course;
  315. }
  316. debugLog("End wwassignment_grade_item_delete");
  317. return grade_update('mod/wwassignment', $wwassignment->courseid, 'mod', 'wwassignment', $wwassignment->id, 0, NULL, array('deleted'=>1));
  318. }
  319. /**
  320. * Updates an assignment instance
  321. *
  322. * This is done by calling the update_instance() method of the assignment type class
  323. */
  324. function wwassignment_item_update($wwassignment) {
  325. error_log("Begin wwassignment_item_update -- not yet defined!!!!");
  326. error_log("input wwassignment " . print_r($wwassignment,true) );
  327. error_log("End wwassignment_item_update -- not yet defined!!!!");
  328. }
  329. /**
  330. * @desc Contacts webwork to find out the completion status of a problem set for all users in a course.
  331. * @param integer $wwassignmentid The problem set
  332. * @return object The student grades indexed by student ID.
  333. */
  334. function wwassignment_grades($wwassignmentid) {
  335. error_log("Begin wwassignment_grades -- legacy function?");
  336. global $COURSE;
  337. $wwclient = new wwassignment_client();
  338. $wwassignment = get_record('wwassignment', 'id',$wwassignmentid);
  339. $courseid = $wwassignment->course;
  340. $studentgrades = new stdClass;
  341. $studentgrades->grades = array();
  342. $studentgrades->maxgrade = 0;
  343. $gradeformula = '$finalgrade += ($problem->status > 0) ? 1 : 0;';
  344. $wwcoursename = _wwassignment_mapped_course($courseid,false);
  345. $wwsetname = _wwassignment_mapped_set($wwassignmentid,false);
  346. // enumerate over the students in the course:
  347. $students = get_course_students( $courseid);
  348. $usernamearray = array();
  349. foreach($students as $student) {
  350. array_push($usernamearray,$student->username);
  351. }
  352. $gradearray = $wwclient->grade_users_sets($wwcoursename,$usernamearray,$wwsetname);
  353. $i = 0;
  354. foreach($students as $student) {
  355. $studentgrades->grades[$student->id] = $gradearray[$i];
  356. $i++;
  357. }
  358. $studentgrades->maxgrade = $wwclient->get_max_grade($wwcoursename,$wwsetname);
  359. error_log("End wwassignment_grades -- legacy function?");
  360. return $studentgrades;
  361. }
  362. /**
  363. * @desc Returns a small object with summary information about a wwassignment instance. Used for user activity repots.
  364. * @param string $course The ID of the course.
  365. * @param string $user The ID of the user.
  366. * @param string $wwassignment The ID of the wwassignment instance.
  367. * @return array Representing time, info pairing.
  368. */
  369. function wwassignment_user_outline($course, $user, $mod, $wwassignment) {
  370. $aLogs = get_logs("l.userid=$user AND l.course=$course AND l.cmid={$wwassignment->id}");
  371. if( count($aLogs) > 0 ) {
  372. $return->time = $aLogs[0]->time;
  373. $return->info = $aLogs[0]->info;
  374. }
  375. return $return;
  376. }
  377. /**
  378. * @desc Prints a detailed representation of what a user has done with a instance of this module.
  379. * @param string $course The ID of the course.
  380. * @param string $user The ID of the user.
  381. * @param string $wwassignment The ID of the wwassignment instance.
  382. * @return array Representing time, info pairing.
  383. */
  384. function wwassignment_user_complete($course, $user, $mod, $wwassignment) {
  385. return true;
  386. }
  387. function wwassignment_delete_course() {
  388. error_log("Begin wwassignment_delete_course --not used yet");
  389. }
  390. function wwassignment_process_options() {
  391. error_log("Begin wwassignment_process_options --not used yet");
  392. }
  393. function wwassignment_reset_course_form() {
  394. error_log("Begin wwassignment_reset_course_form --not used yet");
  395. }
  396. function wwassignment_delete_userdata() {
  397. error_log("Begin wwassignment_delete_userdata --not used yet");
  398. }
  399. /**
  400. * @desc Finds recent activity that has occured in wwassignment activities.
  401. */
  402. function wwassignment_print_recent_activity($course, $isteacher, $timestart) {
  403. global $CFG;
  404. error_log("Begin wwassignment_print_recent_activity --not used yet");
  405. return false; // True if anything was printed, otherwise false
  406. }
  407. /**
  408. * @desc Function that is run by the cron job. This makes sure that
  409. * the grades and all other data are pulled from webwork.
  410. * returns true if successful
  411. */
  412. function wwassignment_cron() {
  413. error_log("Begin wwassignment_cron");
  414. //FIXME: Add a call that updates all events with dates (in case people forgot to push)
  415. //wwassignment_refresh_events();
  416. //FIXME: Add a call that updates all grades in all courses
  417. //wwassignment_update_grades(null,0);
  418. //try { // try didn't work on some php systems -- leave it out.
  419. wwassignment_update_dirty_sets();
  420. error_log("End wwassignment_cron");
  421. return true;
  422. }
  423. // reference material for improving update dirty sets:
  424. // from wiki/lib /print_recent_activity
  425. // $sql = "SELECT l.*, cm.instance FROM {$CFG->prefix}log l
  426. // INNER JOIN {$CFG->prefix}course_modules cm ON l.cmid = cm.id
  427. // WHERE l.time > '$timestart' AND l.course = {$course->id}
  428. // AND l.module = 'wiki' AND action LIKE 'edit%'
  429. // ORDER BY l.time ASC";
  430. //
  431. // if (!$logs = get_records_sql($sql)){
  432. // return false;
  433. // }
  434. //
  435. // $modinfo = get_fast_modinfo($course);
  436. //
  437. function wwassignment_update_dirty_sets() { // update grades for all instances which have been modified since last cronjob
  438. global $CFG;
  439. $timenow = time();
  440. $lastcron = get_field("modules","lastcron","name","wwassignment");
  441. error_log ("lastcron is $lastcron and time now is $timenow");
  442. //error_log ("sql string = $sql");
  443. // Could we speed this up by getting all of the log records pertaining to webwork in one go?
  444. // Or perhaps just the log records which have occured after the lastcron date
  445. // Then create a hash with wwassignment->id => timemodified
  446. // means just one database lookup
  447. $logRecords = get_logs("l.module LIKE \"wwassignment\" AND l.time >$lastcron ", "l.time ASC");
  448. // possible actions generating a log entry include view, update and 'view all'
  449. $wwmodificationtime=array();
  450. foreach ($logRecords as $record) {
  451. $wwid =$record->info;
  452. if ($wwid > 0) { // the $wwid must not be 0 or blank -- blank id's occur for view all.
  453. $wwmodtimes[$wwid] = $record->time;
  454. }
  455. }
  456. // Create an array with the wwid values
  457. $idValues= "( ".implode(",", array_keys($wwmodtimes) ). " )";
  458. error_log("values string $idValues");
  459. //error_log("last modification times".print_r($wwmodtimes,true));
  460. $sql = "SELECT a.*, cm.idnumber as cmidnumber, a.course as courseid, cm.id as wwinstanceid
  461. FROM {$CFG->prefix}wwassignment a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
  462. WHERE m.name='wwassignment' AND m.id=cm.module AND cm.instance=a.id AND a.id IN $idValues";
  463. error_log("sql $sql");
  464. //$sql3 = "SELECT a.* FROM {$CFG->prefix}wwassignment a WHERE a.id IN $idValues";
  465. //error_log("last modification times".print_r($wwmodificationtime,true));
  466. $rs = get_recordset_sql($sql);
  467. if ($rs) {
  468. while ($wwassignment = rs_fetch_next_record($rs)) {
  469. if (!$wwassignment->cmidnumber) { // is this ever needed?
  470. $wwassignment->cmidnumber =_wwassignment_cmid() ;
  471. }
  472. $wwassignment->timemodified = $wwmodtimes[$wwassignment->id];
  473. if ($wwassignment->timemodified > $lastcron) {
  474. error_log("instance needs update. timemodified ".$wwassignment->timemodified.
  475. ", lastcron $lastcron, course id ".$wwassignment->course.", wwassignment id ".$wwassignment->id.
  476. ", set name ".$wwassignment->name.", cm.id ".$wwassignment->wwinstanceid);
  477. if ($wwassignment->grade != 0) {
  478. wwassignment_update_grades($wwassignment);
  479. } else {
  480. wwassignment_grade_item_update($wwassignment);
  481. }
  482. // refresh events for this assignment
  483. _wwassignment_refresh_event($wwassignment);
  484. } else {
  485. error_log("no update needed. timemodified ".$wwassignment->timemodified.
  486. ", lastcron $lastcron, course id ".$wwassignment->course.", wwassignment id ".$wwassignment->id.
  487. ", set name ".$wwassignment->name.", cm.id ".$wwassignment->wwinstanceid);
  488. }
  489. }
  490. rs_close($rs);
  491. }
  492. error_log("done with updating dirty sets");
  493. return(true);
  494. }
  495. /**
  496. * @desc Finds all the participants in the course
  497. * @param string $wwassignmentid The Moodle wwassignment ID.
  498. * @return array An array of course users (IDs).
  499. */
  500. function wwassignment_get_participants($wwassignmentid) {
  501. $wwassignment = get_record('wwassignment', 'id', $wwassignmentid);
  502. if(!isset($wwassignment)) {
  503. return array();
  504. }
  505. return _wwassignment_get_course_students( $wwassignment->course );
  506. }
  507. function wwassignment_refresh_events($courseid = 0) {
  508. // error_log('wwassignment_refresh_events called --not yet defined');
  509. // This standard function will check all instances of this module
  510. // and make sure there are up-to-date events created for each of them.
  511. // If courseid = 0, then every wwassignment event in the site is checked, else
  512. // only wwassignment events belonging to the course specified are checked.
  513. // This function is used, in its new format, by restore_refresh_events() and by the cron function
  514. //
  515. // find wwassignment instances associated with this course or all wwassignment modules
  516. $courses = array(); # create array of courses
  517. if ($courseid) {
  518. if (! $wwassignments = get_records("wwassignment", "course", $courseid)) {
  519. return true;
  520. } else {
  521. $courses[$courseid]= array(); // collect wwassignments for this course
  522. array_push( $courses[$courseid], $wwassignments );
  523. }
  524. } else {
  525. if (! $wwassignments = get_records("wwassignment")) {
  526. return true;
  527. } else {
  528. foreach ($wwassignments as $ww ) {
  529. // collect wwassignments for each course
  530. error_log("course id ".$ww->course);
  531. if (! ($courses[$ww->course] ) ) {
  532. $courses[$ww->course] = array();
  533. }
  534. array_push($courses[$ww->course], $ww) ; // push wwassignment onto an exisiting one
  535. }
  536. }
  537. }
  538. // $courses now holds a list of course wwassignments
  539. if ( $wwclient = new wwassignment_client() ) {
  540. $moduleid = _wwassignment_cmid();
  541. $cids = array_keys($courses); # collect course ids
  542. error_log("course ids ".print_r($cids, true));
  543. foreach ($cids as $cid) {
  544. error_log("processing cid $cid");
  545. // connect to WeBWorK
  546. $wwcoursename = _wwassignment_mapped_course($cid,false);
  547. $wwassignment->webwork_course = $wwcoursename;
  548. if ( $wwcoursename== -1) {
  549. error_log("Can't connect course $cid to webwork");
  550. break;
  551. }
  552. // retrieve wwassignments associated with this course
  553. foreach($courses[$cid] as $wwassignment ) {
  554. if (!isset($wwassignment)) {
  555. error_log("no wwassignments yet");
  556. break;
  557. }
  558. //checking mappings
  559. $wwsetname = $wwassignment->webwork_set;
  560. error_log(" wwassignment_refresh_events updating events for course |$wwcoursename| set |$wwsetname| " );
  561. if ( isset($wwsetname) ) {
  562. //get data from WeBWorK
  563. $wwsetdata = $wwclient->get_assignment_data($wwcoursename,$wwsetname,false);
  564. $wwassignment->grade = $wwclient->get_max_grade($wwcoursename,$wwsetname,false);
  565. $wwassignment->timemodified = time();
  566. $returnid = update_record('wwassignment',$wwassignment);
  567. // update event
  568. //this part won't work because these items implicitly require the course.
  569. if ( isset( $wwsetdata ) ) {
  570. _wwassignment_delete_events($wwassignment->id);
  571. _wwassignment_create_events($wwassignment, $wwsetdata);
  572. } else {
  573. error_log("Can't update events for course |$wwcoursename| set |$wwsetname|");
  574. }
  575. error_log("Done updating course |$wwcoursename| set |$wwsetname|");
  576. } else {
  577. error_log ("no wwsetname |$wwsetname|" );
  578. }
  579. }
  580. }
  581. error_log("done with cids");
  582. } else {
  583. error_log("Can't connect to webwork course");
  584. }
  585. error_log("done with wwassignment_refresh_events() ");
  586. return(true);
  587. }
  588. // High level grade calls ins gradelib.php
  589. /**
  590. * Returns grading information for given activity - optionally with users grades
  591. * Manual, course or category items can not be queried.
  592. * @public
  593. * @param int $courseid id of course
  594. * @param string $itemtype 'mod', 'block'
  595. * @param string $itemmodule 'forum, 'quiz', etc.
  596. * @param int $iteminstance id of the item module
  597. * @param int $userid_or_ids optional id of the graded user or array of ids; if userid not used, returns only information about grade_item
  598. * @return array of grade information objects (scaleid, name, grade and locked status, etc.) indexed with itemnumbers
  599. */
  600. // function grade_get_grades($courseid, $itemtype, $itemmodule, $iteminstance, $userid_or_ids=null) {
  601. /**
  602. * Submit new or update grade; update/create grade_item definition. Grade must have userid specified,
  603. * rawgrade and feedback with format are optional. rawgrade NULL means 'Not graded', missing property
  604. * or key means do not change existing.
  605. *
  606. * Only following grade item properties can be changed 'itemname', 'idnumber', 'gradetype', 'grademax',
  607. * 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted' and 'hidden'. 'reset' means delete all current grades including locked ones.
  608. *
  609. * Manual, course or category items can not be updated by this function.
  610. * @public
  611. * @param string $source source of the grade such as 'mod/assignment'
  612. * @param int $courseid id of course
  613. * @param string $itemtype type of grade item - mod, block
  614. * @param string $itemmodule more specific then $itemtype - assignment, forum, etc.; maybe NULL for some item types
  615. * @param int $iteminstance instance it of graded subject
  616. * @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
  617. * @param mixed $grades grade (object, array) or several grades (arrays of arrays or objects), NULL if updating grade_item definition only
  618. * @param mixed $itemdetails object or array describing the grading item, NULL if no change
  619. */
  620. // function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=NULL, $itemdetails=NULL) {
  621. /**
  622. * Refetches data from all course activities
  623. * @param int $courseid
  624. * @param string $modname
  625. * @return success
  626. */
  627. // function grade_grab_course_grades($courseid, $modname=null) {
  628. ?>