Example #1
0
/**
 * Update grades by firing grade_updated event
 *
 * @param object $assignment null means all assignments
 * @param int $userid specific user only, 0 mean all
 */
function questionnaire_update_grades($questionnaire = null, $userid = 0, $nullifnone = true)
{
    global $CFG, $DB;
    if (!function_exists('grade_update')) {
        // Workaround for buggy PHP versions.
        require_once $CFG->libdir . '/gradelib.php';
    }
    if ($questionnaire != null) {
        if ($graderecs = questionnaire_get_user_grades($questionnaire, $userid)) {
            $grades = array();
            foreach ($graderecs as $v) {
                if (!isset($grades[$v->userid])) {
                    $grades[$v->userid] = new stdClass();
                    if ($v->rawgrade == -1) {
                        $grades[$v->userid]->rawgrade = null;
                    } else {
                        $grades[$v->userid]->rawgrade = $v->rawgrade;
                    }
                    $grades[$v->userid]->userid = $v->userid;
                } else {
                    if (isset($grades[$v->userid]) && $v->rawgrade > $grades[$v->userid]->rawgrade) {
                        $grades[$v->userid]->rawgrade = $v->rawgrade;
                    }
                }
            }
            questionnaire_grade_item_update($questionnaire, $grades);
        } else {
            questionnaire_grade_item_update($questionnaire);
        }
    } else {
        $sql = "SELECT q.*, cm.idnumber as cmidnumber, q.course as courseid\n                  FROM {questionnaire} q, {course_modules} cm, {modules} m\n                 WHERE m.name='questionnaire' AND m.id=cm.module AND cm.instance=q.id";
        if ($rs = $DB->get_recordset_sql($sql)) {
            foreach ($rs as $questionnaire) {
                if ($questionnaire->grade != 0) {
                    questionnaire_update_grades($questionnaire);
                } else {
                    questionnaire_grade_item_update($questionnaire);
                }
            }
            $rs->close();
        }
    }
}
 public function view()
 {
     global $CFG, $USER, $PAGE, $OUTPUT;
     $PAGE->set_title(format_string($this->name));
     $PAGE->set_heading(format_string($this->course->fullname));
     // Initialise the JavaScript.
     $PAGE->requires->js_init_call('M.mod_questionnaire.init_attempt_form', null, false, questionnaire_get_js_module());
     echo $OUTPUT->header();
     $questionnaire = $this;
     if (!$this->cm->visible && !$this->capabilities->viewhiddenactivities) {
         notice(get_string("activityiscurrentlyhidden"));
     }
     if (!$this->capabilities->view) {
         echo '<br/>';
         questionnaire_notify(get_string("noteligible", "questionnaire", $this->name));
         echo '<div><a href="' . $CFG->wwwroot . '/course/view.php?id=' . $this->course->id . '">' . get_string("continue") . '</a></div>';
         exit;
     }
     // Print the main part of the page.
     if (!$this->is_active()) {
         echo '<div class="notifyproblem">' . get_string('notavail', 'questionnaire') . '</div>';
     } else {
         if (!$this->is_open()) {
             echo '<div class="notifyproblem">' . get_string('notopen', 'questionnaire', userdate($this->opendate)) . '</div>';
         } else {
             if ($this->is_closed()) {
                 echo '<div class="notifyproblem">' . get_string('closed', 'questionnaire', userdate($this->closedate)) . '</div>';
             } else {
                 if (!$this->user_is_eligible($USER->id)) {
                     echo '<div class="notifyproblem">' . get_string('noteligible', 'questionnaire') . '</div>';
                 } else {
                     if ($this->user_can_take($USER->id)) {
                         $quser = $USER->id;
                         if ($this->survey->realm == 'template') {
                             print_string('templatenotviewable', 'questionnaire');
                             echo $OUTPUT->footer($this->course);
                             exit;
                         }
                         $msg = $this->print_survey($USER->id, $quser);
                         // If Questionnaire was submitted with all required fields completed ($msg is empty),
                         // then record the submittal.
                         $viewform = data_submitted($CFG->wwwroot . "/mod/questionnaire/complete.php");
                         if (!empty($viewform->rid)) {
                             $viewform->rid = (int) $viewform->rid;
                         }
                         if (!empty($viewform->sec)) {
                             $viewform->sec = (int) $viewform->sec;
                         }
                         if (data_submitted() && confirm_sesskey() && isset($viewform->submit) && isset($viewform->submittype) && $viewform->submittype == "Submit Survey" && empty($msg)) {
                             $this->response_delete($viewform->rid, $viewform->sec);
                             $this->rid = $this->response_insert($this->survey->id, $viewform->sec, $viewform->rid, $quser);
                             $this->response_commit($this->rid);
                             // If it was a previous save, rid is in the form...
                             if (!empty($viewform->rid) && is_numeric($viewform->rid)) {
                                 $rid = $viewform->rid;
                                 // Otherwise its in this object.
                             } else {
                                 $rid = $this->rid;
                             }
                             questionnaire_record_submission($this, $USER->id, $rid);
                             if ($this->grade != 0) {
                                 $questionnaire = new stdClass();
                                 $questionnaire->id = $this->id;
                                 $questionnaire->name = $this->name;
                                 $questionnaire->grade = $this->grade;
                                 $questionnaire->cmidnumber = $this->cm->idnumber;
                                 $questionnaire->courseid = $this->course->id;
                                 questionnaire_update_grades($questionnaire, $quser);
                             }
                             // Update completion state.
                             $completion = new completion_info($this->course);
                             if ($completion->is_enabled($this->cm) && $this->completionsubmit) {
                                 $completion->update_state($this->cm, COMPLETION_COMPLETE);
                             }
                             // Log this submitted response.
                             $context = context_module::instance($this->cm->id);
                             $anonymous = $this->respondenttype == 'anonymous';
                             $params = array('context' => $context, 'courseid' => $this->course->id, 'relateduserid' => $USER->id, 'anonymous' => $anonymous, 'other' => array('questionnaireid' => $questionnaire->id));
                             $event = \mod_questionnaire\event\attempt_submitted::create($params);
                             $event->trigger();
                             $this->response_send_email($this->rid);
                             $this->response_goto_thankyou();
                         }
                     } else {
                         switch ($this->qtype) {
                             case QUESTIONNAIREDAILY:
                                 $msgstring = ' ' . get_string('today', 'questionnaire');
                                 break;
                             case QUESTIONNAIREWEEKLY:
                                 $msgstring = ' ' . get_string('thisweek', 'questionnaire');
                                 break;
                             case QUESTIONNAIREMONTHLY:
                                 $msgstring = ' ' . get_string('thismonth', 'questionnaire');
                                 break;
                             default:
                                 $msgstring = '';
                                 break;
                         }
                         echo '<div class="notifyproblem">' . get_string("alreadyfilled", "questionnaire", $msgstring) . '</div>';
                     }
                 }
             }
         }
     }
     // Finish the page.
     echo $OUTPUT->footer($this->course);
 }
/**
 * Update grades by firing grade_updated event
 *
 * @param object $assignment null means all assignments
 * @param int $userid specific user only, 0 mean all
 */
function questionnaire_update_grades($questionnaire = null, $userid = 0, $nullifnone = true)
{
    global $CFG;
    if (!function_exists('grade_update')) {
        //workaround for buggy PHP versions
        require_once $CFG->libdir . '/gradelib.php';
    }
    if ($questionnaire != null) {
        if ($grades = questionnaire_get_user_grades($questionnaire, $userid)) {
            foreach ($grades as $k => $v) {
                if ($v->rawgrade == -1) {
                    $grades[$k]->rawgrade = null;
                }
                $grades[$k]->feedback = '';
                $grades[$k]->format = '';
            }
            questionnaire_grade_item_update($questionnaire, $grades);
        } else {
            questionnaire_grade_item_update($questionnaire);
        }
    } else {
        $sql = "SELECT q.*, cm.idnumber as cmidnumber, q.course as courseid\n                  FROM {$CFG->prefix}questionnaire q, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m\n                 WHERE m.name='questionnaire' AND m.id=cm.module AND cm.instance=q.id";
        if ($rs = get_recordset_sql($sql)) {
            while ($questionnaire = rs_fetch_next_record($rs)) {
                if ($questionnaire->grade != 0) {
                    questionnaire_update_grades($questionnaire);
                } else {
                    questionnaire_grade_item_update($questionnaire);
                }
            }
            rs_close($rs);
        }
    }
}
 public function test_questionnaire_update_grades()
 {
     // Don't know how to test this yet! It doesn't return anything.
     $this->assertNull(questionnaire_update_grades());
 }
 function view()
 {
     global $CFG, $USER, $QUESTIONNAIRE_STUDENTVIEWRESPONSES_WHENANSWERED, $QUESTIONNAIRE_STUDENTVIEWRESPONSES_WHENCLOSED, $QUESTIONNAIRE_STUDENTVIEWRESPONSES_ALWAYS, $QUESTIONNAIRE_TYPES, $SESSION;
     $currentcss = '';
     if (!empty($this->survey->theme)) {
         $currentcss = '<link rel="stylesheet" type="text/css" href="' . $CFG->wwwroot . '/mod/questionnaire/css/' . $this->survey->theme . '" />';
     }
     $navigation = build_navigation('', $this->cm);
     print_header_simple($this->name, "", $navigation, '', $currentcss, true, update_module_button($this->cm->id, $this->course->id, $this->strquestionnaire), navmenu($this->course, $this->cm));
     /// print the tabs
     $questionnaire = $this;
     include 'tabs.php';
     $strprint = get_string('print', 'questionnaire');
     $strprinttooltip = get_string('printtooltip', 'questionnaire');
     $strprintblank = get_string('printblank', 'questionnaire');
     $strprintblanktooltip = get_string('printblanktooltip', 'questionnaire');
     $blankquestionnaire = true;
     if (!$this->cm->visible) {
         notice(get_string("activityiscurrentlyhidden"));
     }
     if (!$this->capabilities->view) {
         echo '<br/>';
         questionnaire_notify(get_string("guestsno", "questionnaire", $this->name));
         echo '<div class="returnLink"><a href="' . $CFG->wwwroot . '/course/view.php?id=' . $this->course->id . '">' . get_string("continue") . '</a></div>';
         exit;
     }
     /// Print the main part of the page
     if (!$this->is_active()) {
         echo '<div class="message">' . get_string('notavail', 'questionnaire') . '</div>';
     } else {
         if (!$this->is_open()) {
             echo '<div class="message">' . get_string('notopen', 'questionnaire', userdate($this->opendate)) . '</div>';
         } else {
             if ($this->is_closed()) {
                 echo '<div class="message">' . get_string('closed', 'questionnaire', userdate($this->closedate)) . '</div>';
             } else {
                 if (!$this->user_is_eligible($USER->id)) {
                     echo '<div class="message">' . get_string('noteligible', 'questionnaire') . '</div>';
                 } else {
                     if ($this->user_can_take($USER->id)) {
                         $sid = $this->sid;
                         $quser = $USER->id;
                         if ($this->survey->realm == 'template') {
                             print_string('templatenotviewable', 'questionnaire');
                             print_footer($this->course);
                             exit;
                         }
                         echo '<div class="box generalbox boxaligncenter boxwidthwide">';
                         echo '<div style="text-align:right; margin-right:5px; margin-top:-5px; margin-bottom:5px;">';
                         if (isset($this->questions) && $this->capabilities->printblank) {
                             // open print friendly as popup window to fix XML strict bug contrib-200
                             $linkname = get_string('printblank', 'questionnaire');
                             $height = '';
                             $width = '';
                             $title = get_string('printblanktooltip', 'questionnaire');
                             $options = 'menubar=1, location=0, scrollbars, resizable';
                             $image_url = $CFG->wwwroot . '/mod/questionnaire/images/';
                             $name = 'popup';
                             $linkname = '<img src="' . $image_url . 'print.gif" alt="Printer-friendly version" />';
                             $url = '/mod/questionnaire/print.php?qid=' . $this->id . '&amp;rid=0&amp;' . 'courseid=' . $this->course->id . '&amp;sec=1';
                             link_to_popup_window($url, $name, $linkname, $height, $width, $title, $options, false);
                         }
                         echo '</div>';
                         $msg = $this->print_survey($USER->id, $quser);
                         $viewform = data_submitted($CFG->wwwroot . "/mod/questionnaire/view.php");
                         ///     If Survey was submitted with all required fields completed ($msg is empty),
                         ///     then record the submittal.
                         if (isset($viewform->submit) && isset($viewform->submittype) && $viewform->submittype == "Submit Survey" && empty($msg)) {
                             $this->response_delete($viewform->rid, $viewform->sec);
                             $this->rid = $this->response_insert($this->survey->id, $viewform->sec, $viewform->rid, $quser, $viewform);
                             $this->response_commit($this->rid);
                             /// If it was a previous save, rid is in the form...
                             if (!empty($viewform->rid) && is_numeric($viewform->rid)) {
                                 $rid = $viewform->rid;
                                 /// Otherwise its in this object.
                             } else {
                                 $rid = $this->rid;
                             }
                             questionnaire_record_submission($this, $USER->id, $rid);
                             $questionnaire = new Object();
                             $questionnaire->id = $this->id;
                             $questionnaire->name = $this->name;
                             $questionnaire->grade = $this->grade;
                             $questionnaire->cmidnumber = $this->cm->idnumber;
                             $questionnaire->courseid = $this->course->id;
                             questionnaire_update_grades($questionnaire, $quser);
                             add_to_log($this->course->id, "questionnaire", "submit", "view.php?id={$this->cm->id}", "{$this->name}", $this->cm->id, $USER->id);
                             $this->response_send_email($this->rid);
                             $this->response_goto_thankyou();
                         }
                         echo '</div>';
                         //div class="box generalbox boxaligncenter boxwidthwide"
                     } else {
                         switch ($this->qtype) {
                             case QUESTIONNAIREDAILY:
                                 $msgstring = ' ' . get_string('today', 'questionnaire');
                                 break;
                             case QUESTIONNAIREWEEKLY:
                                 $msgstring = ' ' . get_string('thisweek', 'questionnaire');
                                 break;
                             case QUESTIONNAIREMONTHLY:
                                 $msgstring = ' ' . get_string('thismonth', 'questionnaire');
                                 break;
                             default:
                                 $msgstring = '';
                                 break;
                         }
                         echo '<div class="message">' . get_string("alreadyfilled", "questionnaire", $msgstring) . '</div>';
                     }
                 }
             }
         }
     }
     /// Finish the page
     print_footer($this->course);
 }